diff options
author | Taku Kudo <taku@google.com> | 2018-08-14 11:26:39 +0300 |
---|---|---|
committer | Taku Kudo <taku@google.com> | 2018-08-14 11:26:39 +0300 |
commit | 63b37c8c96d10c769b314ed32351566c0cff4b85 (patch) | |
tree | 7b64c2a0471b4ff9935155b931dcbef6c3d8e0d9 /src/util.cc | |
parent | 083275a89f2bae2010245e56eef02b06aed86a66 (diff) |
Added make_py_wheel_mac.py
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc index 4bf3550..c223053 100644 --- a/src/util.cc +++ b/src/util.cc @@ -264,6 +264,43 @@ bool OutputBuffer::WriteLine(absl::string_view text) { } } // namespace io +namespace random { +#ifdef SPM_NO_THREADLOCAL +namespace { +class RandomGeneratorStorage { + public: + RandomGeneratorStorage() { + pthread_key_create(&key_, &RandomGeneratorStorage::Delete); + } + virtual ~RandomGeneratorStorage() { pthread_key_delete(key_); } + + std::mt19937 *Get() { + auto *result = static_cast<std::mt19937 *>(pthread_getspecific(key_)); + if (result == nullptr) { + result = new std::mt19937(std::random_device{}()); + pthread_setspecific(key_, result); + } + return result; + } + + private: + static void Delete(void *value) { delete static_cast<std::mt19937 *>(value); } + pthread_key_t key_; +}; +} // namespace + +std::mt19937 *GetRandomGenerator() { + static RandomGeneratorStorage *storage = new RandomGeneratorStorage; + return storage->Get(); +} +#else +std::mt19937 *GetRandomGenerator() { + thread_local static std::mt19937 mt(std::random_device{}()); + return &mt; +} +#endif +} // namespace random + namespace util { std::string StrError(int errnum) { |