Add a test app for thread local variables with destructors

This commit is contained in:
Martin Storsjö committed 2018-08-03 10:19:50 +03:00
1 parent 11e04f0466
commit 26ab7a4424
4 files changed
+147 -2

No files matched your search

+2 -1
View File
@@ -60,9 +60,10 @@ RUN ./build-libcxx.sh $TOOLCHAIN_PREFIX
COPY test/*.cpp ./test/
RUN cd test && \
for arch in $TOOLCHAIN_ARCHS; do \
for test in hello-cpp hello-exception; do \
for test in hello-cpp hello-exception tlstest-main; do \
$arch-w64-mingw32-clang++ $test.cpp -o $test-$arch.exe || exit 1; \
done; \
$arch-w64-mingw32-clang++ tlstest-lib.cpp -shared -o tlstest-lib-$arch.dll || exit 1; \
done
# Build libssp
+7 -1
View File
@@ -14,7 +14,7 @@ export PATH=$PREFIX/bin:$PATH
cd test
TESTS_C="hello hello-tls crt-test setjmp"
TESTS_C_NO_BUILTIN="crt-test"
TESTS_CPP="hello-cpp hello-exception"
TESTS_CPP="hello-cpp hello-exception tlstest-main"
for arch in $ARCHS; do
for test in $TESTS_C; do
$arch-w64-mingw32-clang $test.c -o $test-$arch.exe
@@ -27,6 +27,7 @@ for arch in $ARCHS; do
for test in $TESTS_CPP; do
$arch-w64-mingw32-clang++ $test.cpp -o $test-$arch.exe
done
$arch-w64-mingw32-clang++ tlstest-lib.cpp -shared -o tlstest-lib-$arch.dll
case $arch in
i686|x86_64)
RUN=wine
@@ -41,6 +42,10 @@ for arch in $ARCHS; do
COPY="$COPY_AARCH64"
;;
esac
cp tlstest-lib-$arch.dll tlstest-lib.dll
if [ -n "$COPY" ]; then
$COPY tlstest-lib.dll
fi
for test in $TESTS_C $TESTS_CPP $TESTS_EXTRA; do
file=$test-$arch.exe
if [ -n "$COPY" ]; then
@@ -50,4 +55,5 @@ for arch in $ARCHS; do
$RUN $file
fi
done
rm -f tlstest-lib.dll
done
+38
View File
@@ -0,0 +1,38 @@
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
class Hello {
public:
Hello(const char* s) {
str = s;
thread = GetCurrentThreadId();
fprintf(stderr, "%s ctor on thread %d\n", str, thread);
}
~Hello() {
fprintf(stderr, "%s dtor from thread %d, now on %d\n", str, thread, (int) GetCurrentThreadId());
}
const char* str;
int thread;
};
Hello lib_h("lib global");
static thread_local Hello lib_tls_h("lib global tls");
static void lib_atexit(void) {
fprintf(stderr, "lib_atexit\n");
}
static class SetAtexit {
public:
SetAtexit() {
atexit(lib_atexit);
}
} sa;
extern "C" void __declspec(dllexport) func(void) {
fprintf(stderr, "func\n");
static thread_local Hello h2("lib local tls");
fprintf(stderr, "func end, thread %d\n", lib_tls_h.thread);
}
+100
View File
@@ -0,0 +1,100 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
HANDLE event1, event2, event3;
void (*func)(void);
DWORD fls_key = FLS_OUT_OF_INDEXES;
class Hello {
public:
Hello(const char* s) {
str = s;
thread = GetCurrentThreadId();
fprintf(stderr, "%s ctor on thread %d\n", str, thread);
}
~Hello() {
fprintf(stderr, "%s dtor from thread %d, now on %d\n", str, thread, (int) GetCurrentThreadId());
}
const char* str;
int thread;
};
unsigned __stdcall threadfunc(void *arg) {
HANDLE event = (HANDLE) arg;
int threadId = GetCurrentThreadId();
fprintf(stderr, "threadfunc thread %d\n", threadId);
static thread_local Hello main_h("main local tls");
SetEvent(event3);
WaitForSingleObject(event, INFINITE);
fprintf(stderr, "thread %d calling func\n", threadId);
if (func)
func();
SetEvent(event3);
WaitForSingleObject(event, INFINITE);
fprintf(stderr, "thread %d finishing\n", threadId);
return 0;
}
static Hello main_h("main global");
static void atexit_func(void) {
fprintf(stderr, "main atexit_func\n");
}
int main(int argc, char* argv[]) {
atexit(atexit_func);
fprintf(stderr, "main\n");
event1 = CreateEvent(NULL, FALSE, FALSE, NULL);
event2 = CreateEvent(NULL, FALSE, FALSE, NULL);
event3 = CreateEvent(NULL, FALSE, FALSE, NULL);
fprintf(stderr, "main, starting thread1\n");
HANDLE thread1 = (HANDLE)_beginthreadex(NULL, 0, threadfunc, event1, 0, NULL);
WaitForSingleObject(event3, INFINITE);
fprintf(stderr, "main, thread1 started\n");
fprintf(stderr, "LoadLibrary tlstest-lib.dll\n");
HMODULE h = LoadLibrary("tlstest-lib.dll");
fprintf(stderr, "LoadLibrary tlstest-lib.dll ret %p\n", h);
if (!h) {
fprintf(stderr, "Unable to load tlstest-lib.dll\n");
return 1;
}
func = (void (*)(void)) GetProcAddress(h, "func");
fprintf(stderr, "main, got func address, calling it\n");
if (func)
func();
fprintf(stderr, "main, starting thread2\n");
HANDLE thread2 = (HANDLE)_beginthreadex(NULL, 0, threadfunc, event2, 0, NULL);
WaitForSingleObject(event3, INFINITE);
fprintf(stderr, "main, thread2 started\n");
SetEvent(event1);
WaitForSingleObject(event3, INFINITE);
fprintf(stderr, "main, thread1 work done\n");
SetEvent(event2);
WaitForSingleObject(event3, INFINITE);
fprintf(stderr, "main, thread2 work done\n");
SetEvent(event1);
WaitForSingleObject(thread1, INFINITE);
fprintf(stderr, "main, thread1 joined\n");
fprintf(stderr, "FreeLibrary\n");
FreeLibrary(h);
fprintf(stderr, "FreeLibrary done\n");
SetEvent(event2);
WaitForSingleObject(thread2, INFINITE);
fprintf(stderr, "main, thread2 joined\n");
static thread_local Hello main_h("main local tls");
atexit(atexit_func);
fprintf(stderr, "main done\n");
return 0;
}