blob: 0cd44ace0c1d2959e0721a4776d1d847c0c4ccfc (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
// FastRandom.cpp
#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;
}
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;
}
return ++SeedCounter;
}
|