proxygen
folly::TLRefCount::LocalRefCount Class Reference

Public Member Functions

 LocalRefCount (TLRefCount &refCount)
 
 ~LocalRefCount ()
 
void collect ()
 
bool operator++ ()
 
bool operator-- ()
 

Private Member Functions

bool update (Int delta)
 

Private Attributes

AtomicInt count_ {0}
 
TLRefCountrefCount_
 
std::mutex collectMutex_
 
Int collectCount_ {0}
 
std::shared_ptr< void > collectGuard_
 

Detailed Description

Definition at line 136 of file TLRefCount.h.

Constructor & Destructor Documentation

folly::TLRefCount::LocalRefCount::LocalRefCount ( TLRefCount refCount)
inlineexplicit

Definition at line 138 of file TLRefCount.h.

References folly::TLRefCount::collectGuard_, and folly::TLRefCount::globalMutex_.

138  : refCount_(refCount) {
139  std::lock_guard<std::mutex> lg(refCount.globalMutex_);
140 
141  collectGuard_ = refCount.collectGuard_;
142  }
std::shared_ptr< void > collectGuard_
Definition: TLRefCount.h:202
folly::TLRefCount::LocalRefCount::~LocalRefCount ( )
inline

Definition at line 144 of file TLRefCount.h.

References folly::collect().

144  {
145  collect();
146  }

Member Function Documentation

void folly::TLRefCount::LocalRefCount::collect ( )
inline

Definition at line 148 of file TLRefCount.h.

References folly::TLRefCount::collectGuard_.

148  {
149  std::lock_guard<std::mutex> lg(collectMutex_);
150 
151  if (!collectGuard_) {
152  return;
153  }
154 
155  collectCount_ = count_.load();
157  collectGuard_.reset();
158  }
std::atomic< int64_t > globalCount_
Definition: TLRefCount.h:207
std::shared_ptr< void > collectGuard_
Definition: TLRefCount.h:202
bool folly::TLRefCount::LocalRefCount::operator++ ( )
inline

Definition at line 160 of file TLRefCount.h.

160  {
161  return update(1);
162  }
bool folly::TLRefCount::LocalRefCount::operator-- ( )
inline

Definition at line 164 of file TLRefCount.h.

164  {
165  return update(-1);
166  }
bool folly::TLRefCount::LocalRefCount::update ( Int  delta)
inlineprivate

Definition at line 169 of file TLRefCount.h.

References folly::asymmetricLightBarrier(), folly::TLRefCount::collectGuard_, count, folly::TLRefCount::LOCAL, and UNLIKELY.

169  {
170  if (UNLIKELY(refCount_.state_.load() != State::LOCAL)) {
171  return false;
172  }
173 
174  // This is equivalent to atomic fetch_add. We know that this operation
175  // is always performed from a single thread. asymmetricLightBarrier()
176  // makes things faster than atomic fetch_add on platforms with native
177  // support.
178  auto count = count_.load(std::memory_order_relaxed) + delta;
179  count_.store(count, std::memory_order_relaxed);
180 
182 
183  if (UNLIKELY(refCount_.state_.load() != State::LOCAL)) {
184  std::lock_guard<std::mutex> lg(collectMutex_);
185 
186  if (collectGuard_) {
187  return true;
188  }
189  if (collectCount_ != count) {
190  return false;
191  }
192  }
193 
194  return true;
195  }
std::shared_ptr< void > collectGuard_
Definition: TLRefCount.h:202
FOLLY_ALWAYS_INLINE void asymmetricLightBarrier()
int * count
std::atomic< State > state_
Definition: TLRefCount.h:205
#define UNLIKELY(x)
Definition: Likely.h:48

Member Data Documentation

Int folly::TLRefCount::LocalRefCount::collectCount_ {0}
private

Definition at line 201 of file TLRefCount.h.

std::shared_ptr<void> folly::TLRefCount::LocalRefCount::collectGuard_
private

Definition at line 202 of file TLRefCount.h.

std::mutex folly::TLRefCount::LocalRefCount::collectMutex_
private

Definition at line 200 of file TLRefCount.h.

AtomicInt folly::TLRefCount::LocalRefCount::count_ {0}
private

Definition at line 197 of file TLRefCount.h.

TLRefCount& folly::TLRefCount::LocalRefCount::refCount_
private

Definition at line 198 of file TLRefCount.h.


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