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
bufferedRandomDevice->get(data, size);
}
-folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
-ThreadLocalPRNG::localInstance;
+ThreadLocalPRNG::ThreadLocalPRNG() {
+ static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
+ local_ = localInstance.get();
+}
class ThreadLocalPRNG::LocalInstancePRNG {
public:
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();
}
}
friend class Random;
- ThreadLocalPRNG() {
- local_ = localInstance.get();
- if (!local_) {
- local_ = initLocal();
- }
- }
+ ThreadLocalPRNG();
private:
class LocalInstancePRNG;
- static LocalInstancePRNG* initLocal();
- static folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
- localInstance;
static result_type getImpl(LocalInstancePRNG* local);
LocalInstancePRNG* local_;