From: Daniel Sommermann Date: Tue, 14 Oct 2014 23:39:00 +0000 (-0700) Subject: Fix thread local random number generator static issue X-Git-Tag: v0.22.0~279 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=651eb23ca2cfb7b64481f3091160cc8a1112dbc9;p=folly.git Fix thread local random number generator static issue Summary: See https://gcc.gnu.org/onlinedocs/gcc-4.8.3/gcc/Thread-Local.html Thanks to @subodh for finding the first instance of this problem in the proxygen codebase. Test Plan: unit tests, tested proxygen unit tests on my laptop and I don't get segfaults on program exit now. Reviewed By: subodh@fb.com Subscribers: trunkagent, doug, njormrod, subodh FB internal diff: D1616149 Tasks: 5217022 --- diff --git a/folly/Random.cpp b/folly/Random.cpp index 99a74f55..06b3d5ae 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -98,8 +98,10 @@ void Random::secureRandom(void* data, size_t size) { bufferedRandomDevice->get(data, size); } -folly::ThreadLocalPtr -ThreadLocalPRNG::localInstance; +ThreadLocalPRNG::ThreadLocalPRNG() { + static folly::ThreadLocal localInstance; + local_ = localInstance.get(); +} class ThreadLocalPRNG::LocalInstancePRNG { public: @@ -108,12 +110,6 @@ class ThreadLocalPRNG::LocalInstancePRNG { Random::DefaultGenerator rng; }; -ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() { - auto ret = new LocalInstancePRNG; - localInstance.reset(ret); - return ret; -} - uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) { return local->rng(); } diff --git a/folly/Random.h b/folly/Random.h index 8930526e..6846e1d1 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -64,18 +64,10 @@ class ThreadLocalPRNG { } friend class Random; - ThreadLocalPRNG() { - local_ = localInstance.get(); - if (!local_) { - local_ = initLocal(); - } - } + ThreadLocalPRNG(); private: class LocalInstancePRNG; - static LocalInstancePRNG* initLocal(); - static folly::ThreadLocalPtr - localInstance; static result_type getImpl(LocalInstancePRNG* local); LocalInstancePRNG* local_;