summaryrefslogtreecommitdiffstats
path: root/src/FastRandom.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/FastRandom.cpp')
-rw-r--r--src/FastRandom.cpp48
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;
}