From 596644b795f3c51d803dfb7acccf49ad3de6aaa7 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 22 Dec 2024 17:51:09 +0100 Subject: [PATCH] Add TLS test --- src/test-sample/test.cpp | 59 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/test-sample/test.cpp b/src/test-sample/test.cpp index e8a8460f..a0ff5167 100644 --- a/src/test-sample/test.cpp +++ b/src/test-sample/test.cpp @@ -16,6 +16,18 @@ using namespace std::literals; // to trick compiler optimizations __declspec(dllexport) bool do_the_task = true; +struct tls_struct +{ + DWORD num = 1337; + + tls_struct() + { + num = GetCurrentThreadId(); + } +}; + +thread_local tls_struct tls_var{}; + // getenv is broken right now :( std::string read_env(const char* env) { @@ -58,6 +70,50 @@ bool test_threads() return counter == (thread_count * 3ULL); } +bool test_tls() +{ + std::atomic_bool kill{false}; + std::atomic_uint32_t successes{0}; + constexpr uint32_t thread_count = 2; + + std::vector ts{}; + kill = false; + + for (size_t i = 0; i < thread_count; ++i) + { + ts.emplace_back([&] + { + while (!kill) + { + std::this_thread::yield(); + } + + if (tls_var.num == GetCurrentThreadId()) + { + ++successes; + } + }); + } + + LoadLibraryA("d3dcompiler_47.dll"); + LoadLibraryA("dsound.dll"); + /*LoadLibraryA("d3d9.dll"); + LoadLibraryA("dxgi.dll"); + LoadLibraryA("wlanapi.dll");*/ + + kill = true; + + for (auto& t : ts) + { + if (t.joinable()) + { + t.join(); + } + } + + return successes == thread_count; +} + bool test_env() { const auto computername = read_env("COMPUTERNAME"); @@ -198,7 +254,7 @@ void print_time() int main(int argc, const char* argv[]) { - if(argc == 2 && argv[1] == "-time"sv) + if (argc == 2 && argv[1] == "-time"sv) { print_time(); return 0; @@ -212,6 +268,7 @@ int main(int argc, const char* argv[]) RUN_TEST(test_env, "Environment") RUN_TEST(test_exceptions, "Exceptions") RUN_TEST(test_native_exceptions, "Native Exceptions") + RUN_TEST(test_tls, "TLS") return valid ? 0 : 1; }