proxygen
folly::TLRefCount Class Reference

#include <TLRefCount.h>

Classes

class  LocalRefCount
 

Public Types

using Int = int64_t
 

Public Member Functions

 TLRefCount ()
 
 ~TLRefCount () noexcept
 
Int operator++ () noexcept
 
Int operator-- () noexcept
 
Int operator* () const
 
void useGlobal () noexcept
 

Static Public Member Functions

template<typename Container >
static void useGlobal (const Container &refCountPtrs)
 

Private Types

enum  State { State::LOCAL, State::GLOBAL_TRANSITION, State::GLOBAL }
 
using AtomicInt = std::atomic< Int >
 

Private Attributes

std::atomic< Statestate_ {State::LOCAL}
 
folly::ThreadLocal< LocalRefCount, TLRefCountlocalCount_
 
std::atomic< int64_tglobalCount_ {1}
 
std::mutex globalMutex_
 
std::shared_ptr< void > collectGuard_
 

Detailed Description

Definition at line 23 of file TLRefCount.h.

Member Typedef Documentation

using folly::TLRefCount::AtomicInt = std::atomic<Int>
private

Definition at line 128 of file TLRefCount.h.

Definition at line 25 of file TLRefCount.h.

Member Enumeration Documentation

enum folly::TLRefCount::State
strongprivate
Enumerator
LOCAL 
GLOBAL_TRANSITION 
GLOBAL 

Definition at line 130 of file TLRefCount.h.

130  {
131  LOCAL,
132  GLOBAL_TRANSITION,
133  GLOBAL,
134  };

Constructor & Destructor Documentation

folly::TLRefCount::TLRefCount ( )
inline

Definition at line 27 of file TLRefCount.h.

References collectGuard_.

28  : localCount_([&]() { return new LocalRefCount(*this); }),
29  collectGuard_(this, [](void*) {}) {}
folly::ThreadLocal< LocalRefCount, TLRefCount > localCount_
Definition: TLRefCount.h:206
std::shared_ptr< void > collectGuard_
Definition: TLRefCount.h:209
folly::TLRefCount::~TLRefCount ( )
inlinenoexcept

Definition at line 31 of file TLRefCount.h.

References GLOBAL, globalCount_, and state_.

31  {
32  assert(globalCount_.load() == 0);
33  assert(state_.load() == State::GLOBAL);
34  }
std::atomic< int64_t > globalCount_
Definition: TLRefCount.h:207
std::atomic< State > state_
Definition: TLRefCount.h:205

Member Function Documentation

Int folly::TLRefCount::operator* ( ) const
inline

Definition at line 76 of file TLRefCount.h.

References GLOBAL, globalCount_, and state_.

76  {
77  if (state_ != State::GLOBAL) {
78  return 42;
79  }
80  return globalCount_.load();
81  }
std::atomic< int64_t > globalCount_
Definition: TLRefCount.h:207
std::atomic< State > state_
Definition: TLRefCount.h:205
Int folly::TLRefCount::operator++ ( )
inlinenoexcept

Definition at line 37 of file TLRefCount.h.

References GLOBAL, GLOBAL_TRANSITION, globalCount_, globalMutex_, localCount_, state_, and folly::value().

37  {
38  auto& localCount = *localCount_;
39 
40  if (++localCount) {
41  return 42;
42  }
43 
44  if (state_.load() == State::GLOBAL_TRANSITION) {
45  std::lock_guard<std::mutex> lg(globalMutex_);
46  }
47 
48  assert(state_.load() == State::GLOBAL);
49 
50  auto value = globalCount_.load();
51  do {
52  if (value == 0) {
53  return 0;
54  }
55  } while (!globalCount_.compare_exchange_weak(value, value + 1));
56 
57  return value + 1;
58  }
folly::ThreadLocal< LocalRefCount, TLRefCount > localCount_
Definition: TLRefCount.h:206
std::mutex globalMutex_
Definition: TLRefCount.h:208
std::atomic< int64_t > globalCount_
Definition: TLRefCount.h:207
std::atomic< State > state_
Definition: TLRefCount.h:205
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
Int folly::TLRefCount::operator-- ( )
inlinenoexcept

Definition at line 60 of file TLRefCount.h.

References GLOBAL, GLOBAL_TRANSITION, globalCount_, globalMutex_, localCount_, and state_.

60  {
61  auto& localCount = *localCount_;
62 
63  if (--localCount) {
64  return 42;
65  }
66 
67  if (state_.load() == State::GLOBAL_TRANSITION) {
68  std::lock_guard<std::mutex> lg(globalMutex_);
69  }
70 
71  assert(state_.load() == State::GLOBAL);
72 
73  return globalCount_-- - 1;
74  }
folly::ThreadLocal< LocalRefCount, TLRefCount > localCount_
Definition: TLRefCount.h:206
std::mutex globalMutex_
Definition: TLRefCount.h:208
std::atomic< int64_t > globalCount_
Definition: TLRefCount.h:207
std::atomic< State > state_
Definition: TLRefCount.h:205
void folly::TLRefCount::useGlobal ( )
inlinenoexcept

Definition at line 83 of file TLRefCount.h.

Referenced by folly::shutdown().

83  {
84  std::array<TLRefCount*, 1> ptrs{{this}};
85  useGlobal(ptrs);
86  }
void useGlobal() noexcept
Definition: TLRefCount.h:83
template<typename Container >
static void folly::TLRefCount::useGlobal ( const Container &  refCountPtrs)
inlinestatic

Definition at line 89 of file TLRefCount.h.

References folly::asymmetricHeavyBarrier(), count, GLOBAL, and GLOBAL_TRANSITION.

89  {
90 #ifdef FOLLY_SANITIZE_THREAD
91  // TSAN has a limitation for the number of locks held concurrently, so it's
92  // safer to call useGlobal() serially.
93  if (refCountPtrs.size() > 1) {
94  for (auto refCountPtr : refCountPtrs) {
95  refCountPtr->useGlobal();
96  }
97  return;
98  }
99 #endif
100 
101  std::vector<std::unique_lock<std::mutex>> lgs_;
102  for (auto refCountPtr : refCountPtrs) {
103  lgs_.emplace_back(refCountPtr->globalMutex_);
104 
105  refCountPtr->state_ = State::GLOBAL_TRANSITION;
106  }
107 
109 
110  for (auto refCountPtr : refCountPtrs) {
111  std::weak_ptr<void> collectGuardWeak = refCountPtr->collectGuard_;
112 
113  // Make sure we can't create new LocalRefCounts
114  refCountPtr->collectGuard_.reset();
115 
116  while (!collectGuardWeak.expired()) {
117  auto accessor = refCountPtr->localCount_.accessAllThreads();
118  for (auto& count : accessor) {
119  count.collect();
120  }
121  }
122 
123  refCountPtr->state_ = State::GLOBAL;
124  }
125  }
void asymmetricHeavyBarrier(AMBFlags flags)
int * count

Member Data Documentation

std::shared_ptr<void> folly::TLRefCount::collectGuard_
private
std::atomic<int64_t> folly::TLRefCount::globalCount_ {1}
private

Definition at line 207 of file TLRefCount.h.

Referenced by operator*(), operator++(), operator--(), and ~TLRefCount().

std::mutex folly::TLRefCount::globalMutex_
private
folly::ThreadLocal<LocalRefCount, TLRefCount> folly::TLRefCount::localCount_
private

Definition at line 206 of file TLRefCount.h.

Referenced by operator++(), and operator--().

std::atomic<State> folly::TLRefCount::state_ {State::LOCAL}
private

Definition at line 205 of file TLRefCount.h.

Referenced by operator*(), operator++(), operator--(), and ~TLRefCount().


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