1
0

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?
This commit is contained in:
peterbell10
2020-05-10 17:16:49 +01:00
committed by GitHub
parent 154df6b09d
commit 84289a2ba9
9 changed files with 63 additions and 142 deletions

View File

@@ -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;
}