From 895c8a62d25a854afc1cbf9d1f6fba97ba1b9d83 Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Wed, 30 Nov 2016 16:50:04 -0800 Subject: [PATCH] Use SingletonThreadLocal in folly::Random Summary: folly::SingletonThreadLocal should be used for all thread-local singletons to avoid SDOF. Reviewed By: yfeldblum Differential Revision: D4253009 fbshipit-source-id: 5b4ca57a7a77b7b54c94630a2a29d13b84a3af27 --- folly/Random.cpp | 19 +++++++++++-------- folly/Random.h | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/folly/Random.cpp b/folly/Random.cpp index d4fb39b4..d991990e 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -21,13 +21,14 @@ #include #include -#include #include #include #include +#include #include #include #include +#include #ifdef _MSC_VER # include @@ -123,28 +124,30 @@ void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { ptr_ += size; } +struct RandomTag {}; -} // namespace +} // namespace void Random::secureRandom(void* data, size_t size) { - static ThreadLocal bufferedRandomDevice; - bufferedRandomDevice->get(data, size); + static SingletonThreadLocal + bufferedRandomDevice; + bufferedRandomDevice.get().get(data, size); } class ThreadLocalPRNG::LocalInstancePRNG { public: - LocalInstancePRNG() : rng(Random::create()) { } + LocalInstancePRNG() : rng(Random::create()) {} Random::DefaultGenerator rng; }; ThreadLocalPRNG::ThreadLocalPRNG() { - static folly::ThreadLocal localInstance; - local_ = localInstance.get(); + static SingletonThreadLocal + localInstancePRNG; + local_ = &localInstancePRNG.get(); } uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) { return local->rng(); } - } diff --git a/folly/Random.h b/folly/Random.h index 0962d07e..0e52772a 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -65,9 +65,9 @@ class ThreadLocalPRNG { ThreadLocalPRNG(); - private: class LocalInstancePRNG; + private: static result_type getImpl(LocalInstancePRNG* local); LocalInstancePRNG* local_; }; -- 2.34.1