From 84289a2ba996b41815f148f27aecc52a7864066d Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Sun, 10 May 2020 17:16:49 +0100 Subject: Cleanup some workarounds and warnings (#4735) * Cleanup thread_local usage in FastRandom * Use constexpr to avoid clang warning * Fix more Wglobal-constructor warnings * Make MSVC happy? --- src/FastRandom.cpp | 48 ++++++++++++------------------------------------ 1 file changed, 12 insertions(+), 36 deletions(-) (limited to 'src/FastRandom.cpp') diff --git a/src/FastRandom.cpp b/src/FastRandom.cpp index 0cd44ace0..0ba5fb64a 100644 --- a/src/FastRandom.cpp +++ b/src/FastRandom.cpp @@ -5,38 +5,18 @@ #include "Globals.h" #include "FastRandom.h" -#if defined (__GNUC__) - #define ATTRIBUTE_TLS static __thread -#elif defined (_MSC_VER) - #define ATTRIBUTE_TLS static __declspec(thread) -#else - #define ATTRIBUTE_TLS thread_local -#endif - MTRand & GetRandomProvider() { - // Some compilers don't support thread_local for non-POD types, this is purely a work around for that restriction. - // There should be minimal overhead for the non-initializing case and all thread's instances are deleted properly. - ATTRIBUTE_TLS MTRand * LocalPtr = nullptr; - if (LocalPtr == nullptr) - { - // This list allows deletion of elements as if they had static storage duration - static std::mutex CSDeleteList; - static std::list> DeleteList; - - cRandomDeviceSeeder seeder; - auto NewInstance = cpp14::make_unique(seeder); - auto TempPtr = NewInstance.get(); - - std::lock_guard Lock(CSDeleteList); - DeleteList.push_front(std::move(NewInstance)); - LocalPtr = TempPtr; // Set after push_back so LocalPtr won't dangle if it throws - } - return *LocalPtr; + thread_local MTRand Random = [] + { + cRandomDeviceSeeder Seeder; + return MTRand(Seeder); + }(); + return Random; } @@ -45,15 +25,11 @@ MTRand & GetRandomProvider() UInt32 Detail::GetRandomSeed() { - ATTRIBUTE_TLS bool SeedCounterInitialized = false; - ATTRIBUTE_TLS UInt32 SeedCounter = 0; - - if (!SeedCounterInitialized) - { - std::random_device rd; - std::uniform_int_distribution dist; - SeedCounter = dist(rd); - SeedCounterInitialized = true; - } + thread_local UInt32 SeedCounter = [] + { + std::random_device rd; + std::uniform_int_distribution dist; + return dist(rd); + }(); return ++SeedCounter; } -- cgit v1.2.3