diff options
author | peterbell10 <peterbell10@live.co.uk> | 2020-05-10 18:16:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-10 18:16:49 +0200 |
commit | 84289a2ba996b41815f148f27aecc52a7864066d (patch) | |
tree | 7434a6b14dde1597bd687e0a71a28135a1d31adc /src/FastRandom.cpp | |
parent | Cleanup unneeded globals (#4736) (diff) | |
download | cuberite-84289a2ba996b41815f148f27aecc52a7864066d.tar cuberite-84289a2ba996b41815f148f27aecc52a7864066d.tar.gz cuberite-84289a2ba996b41815f148f27aecc52a7864066d.tar.bz2 cuberite-84289a2ba996b41815f148f27aecc52a7864066d.tar.lz cuberite-84289a2ba996b41815f148f27aecc52a7864066d.tar.xz cuberite-84289a2ba996b41815f148f27aecc52a7864066d.tar.zst cuberite-84289a2ba996b41815f148f27aecc52a7864066d.zip |
Diffstat (limited to '')
-rw-r--r-- | src/FastRandom.cpp | 48 |
1 files changed, 12 insertions, 36 deletions
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<std::unique_ptr<MTRand>> DeleteList; - - cRandomDeviceSeeder seeder; - auto NewInstance = cpp14::make_unique<MTRand>(seeder); - auto TempPtr = NewInstance.get(); - - std::lock_guard<std::mutex> 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<UInt32> dist; - SeedCounter = dist(rd); - SeedCounterInitialized = true; - } + thread_local UInt32 SeedCounter = [] + { + std::random_device rd; + std::uniform_int_distribution<UInt32> dist; + return dist(rd); + }(); return ++SeedCounter; } |