proxygen
folly::threadlocal_detail::PthreadKeyUnregister Class Reference

#include <ThreadLocalDetail.h>

Public Member Functions

 ~PthreadKeyUnregister ()
 

Static Public Member Functions

static void registerKey (pthread_key_t key)
 

Static Public Attributes

static constexpr size_t kMaxKeys = 1UL << 16
 

Private Member Functions

constexpr PthreadKeyUnregister ()
 
void registerKeyImpl (pthread_key_t key)
 

Private Attributes

MicroSpinLock lock_
 
size_t size_
 
pthread_key_t keys_ [kMaxKeys]
 

Static Private Attributes

static PthreadKeyUnregister instance_
 

Friends

struct folly::threadlocal_detail::PthreadKeyUnregisterTester
 

Detailed Description

We want to disable onThreadExit call at the end of shutdown, we don't care about leaking memory at that point.

Otherwise if ThreadLocal is used in a shared library, onThreadExit may be called after dlclose().

This class has one single static instance; however since it's so widely used, directly or indirectly, by so many classes, we need to take care to avoid problems stemming from the Static Initialization/Destruction Order Fiascos. Therefore this class needs to be constexpr-constructible, so as to avoid the need for this to participate in init/destruction order.

Definition at line 256 of file ThreadLocalDetail.h.

Constructor & Destructor Documentation

folly::threadlocal_detail::PthreadKeyUnregister::~PthreadKeyUnregister ( )
inline

Definition at line 260 of file ThreadLocalDetail.h.

260  {
261  // If static constructor priorities are not supported then
262  // ~PthreadKeyUnregister logic is not safe.
263 #if !defined(__APPLE__) && !defined(_MSC_VER)
264  MSLGuard lg(lock_);
265  while (size_) {
266  pthread_key_delete(keys_[--size_]);
267  }
268 #endif
269  }
std::lock_guard< MicroSpinLock > MSLGuard
constexpr folly::threadlocal_detail::PthreadKeyUnregister::PthreadKeyUnregister ( )
inlineprivate

Only one global instance should exist, hence this is private. See also the important note at the top of this class about constexpr usage.

Definition at line 281 of file ThreadLocalDetail.h.

Member Function Documentation

static void folly::threadlocal_detail::PthreadKeyUnregister::registerKey ( pthread_key_t  key)
inlinestatic
void folly::threadlocal_detail::PthreadKeyUnregister::registerKeyImpl ( pthread_key_t  key)
inlineprivate

Definition at line 284 of file ThreadLocalDetail.h.

284  {
285  MSLGuard lg(lock_);
286  if (size_ == kMaxKeys) {
287  throw std::logic_error("pthread_key limit has already been reached");
288  }
289  keys_[size_++] = key;
290  }
std::lock_guard< MicroSpinLock > MSLGuard

Friends And Related Function Documentation

Definition at line 282 of file ThreadLocalDetail.h.

Member Data Documentation

FOLLY_STATIC_CTOR_PRIORITY_MAX PthreadKeyUnregister folly::threadlocal_detail::PthreadKeyUnregister::instance_
staticprivate
pthread_key_t folly::threadlocal_detail::PthreadKeyUnregister::keys_[kMaxKeys]
private

Definition at line 294 of file ThreadLocalDetail.h.

constexpr size_t folly::threadlocal_detail::PthreadKeyUnregister::kMaxKeys = 1UL << 16
static

Definition at line 258 of file ThreadLocalDetail.h.

MicroSpinLock folly::threadlocal_detail::PthreadKeyUnregister::lock_
private

Definition at line 292 of file ThreadLocalDetail.h.

size_t folly::threadlocal_detail::PthreadKeyUnregister::size_
private

Definition at line 293 of file ThreadLocalDetail.h.


The documentation for this class was generated from the following files: