From edcf22bec44e42f1f0a01f507d52d43c3a43dd6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Sat, 2 Dec 2017 23:41:59 +0200 Subject: [PATCH] Build a test program for testing TLS support --- Dockerfile | 7 ++++++- hello-tls.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 hello-tls.c diff --git a/Dockerfile b/Dockerfile index bc53e99..4a5bcee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -250,7 +250,7 @@ RUN cd libcxx && \ RUN cd $TOOLCHAIN_PREFIX/include && ln -s ../$(echo $TOOLCHAIN_ARCHS | awk '{print $1}')-w64-mingw32/include/c++ . -COPY hello.c hello.cpp hello-exception.cpp /build/hello/ +COPY hello.c hello.cpp hello-exception.cpp hello-tls.c /build/hello/ RUN cd hello && \ for arch in $TOOLCHAIN_ARCHS; do \ $arch-w64-mingw32-clang hello.c -o hello-$arch.exe || exit 1; \ @@ -266,6 +266,11 @@ RUN cd hello && \ $arch-w64-mingw32-clang++ hello-exception.cpp -o hello-exception-$arch.exe || exit 1; \ done +RUN cd hello && \ + for arch in $TOOLCHAIN_ARCHS; do \ + $arch-w64-mingw32-clang hello-tls.c -o hello-tls-$arch.exe || exit 1; \ + done + ENV AR=llvm-ar ENV RANLIB=llvm-ranlib ENV AS=llvm-as diff --git a/hello-tls.c b/hello-tls.c new file mode 100644 index 0000000..40d38c0 --- /dev/null +++ b/hello-tls.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#if defined(_MSC_VER) +static __declspec(thread) int tlsvar = 1; +#else +static __thread int tlsvar = 1; +#endif + +static unsigned __stdcall threadfunc(void* arg) { + int id = (int)arg; + printf("thread %d, tlsvar %p %d\n", id, &tlsvar, tlsvar); + tlsvar = id + 100; + for (int i = 0; i < 5; i++) { + printf("thread %d, tlsvar %p %d\n", id, &tlsvar, tlsvar); + tlsvar += 10; + Sleep(1000); + } + return 0; +} + +int main(int argc, char* argv[]) { + HANDLE threads[5]; + + for (int i = 0; i < 5; i++) { + printf("mainthread, tlsvar %p %d\n", &tlsvar, tlsvar); + tlsvar += 10; + threads[i] = (HANDLE)_beginthreadex(NULL, 0, threadfunc, (void*)(intptr_t) (i + 1), 0, NULL); + Sleep(750); + } + for (int i = 0; i < 5; i++) { + WaitForSingleObject(threads[i], INFINITE); + CloseHandle(threads[i]); + } + return 0; +}