proxygen
folly::ThreadCachedInt< IntT, Tag >::IntCache Struct Reference

Public Member Functions

 IntCache (ThreadCachedInt &parent)
 
void increment (IntT inc)
 
void flush () const
 
 ~IntCache ()
 

Public Attributes

ThreadCachedIntparent_
 
std::atomic< IntT > val_
 
uint32_t numUpdates_
 
std::atomic< bool > reset_
 

Detailed Description

template<class IntT, class Tag = IntT>
struct folly::ThreadCachedInt< IntT, Tag >::IntCache

Definition at line 142 of file ThreadCachedInt.h.

Constructor & Destructor Documentation

template<class IntT, class Tag = IntT>
folly::ThreadCachedInt< IntT, Tag >::IntCache::IntCache ( ThreadCachedInt parent)
inlineexplicit

Definition at line 148 of file ThreadCachedInt.h.

149  : parent_(&parent), val_(0), numUpdates_(0), reset_(false) {}
folly::Function< void()> parent
Definition: AtFork.cpp:34
template<class IntT, class Tag = IntT>
folly::ThreadCachedInt< IntT, Tag >::IntCache::~IntCache ( )
inline

Definition at line 176 of file ThreadCachedInt.h.

References folly::ThreadCachedInt< IntT, Tag >::IntCache::flush().

176  {
177  flush();
178  }

Member Function Documentation

template<class IntT, class Tag = IntT>
void folly::ThreadCachedInt< IntT, Tag >::IntCache::flush ( ) const
inline

Definition at line 170 of file ThreadCachedInt.h.

References folly::ThreadCachedInt< IntT, Tag >::target_.

Referenced by folly::ThreadCachedInt< IntT, Tag >::IntCache::increment(), and folly::ThreadCachedInt< IntT, Tag >::IntCache::~IntCache().

170  {
171  parent_->target_.fetch_add(val_, std::memory_order_release);
172  val_.store(0, std::memory_order_release);
173  numUpdates_ = 0;
174  }
std::atomic< IntT > target_
template<class IntT, class Tag = IntT>
void folly::ThreadCachedInt< IntT, Tag >::IntCache::increment ( IntT  inc)
inline

Definition at line 151 of file ThreadCachedInt.h.

References folly::ThreadCachedInt< IntT, Tag >::cacheSize_, folly::ThreadCachedInt< IntT, Tag >::IntCache::flush(), LIKELY, folly::ThreadCachedInt< IntT, Tag >::IntCache::numUpdates_, and UNLIKELY.

151  {
152  if (LIKELY(!reset_.load(std::memory_order_acquire))) {
153  // This thread is the only writer to val_, so it's fine do do
154  // a relaxed load and do the addition non-atomically.
155  val_.store(
156  val_.load(std::memory_order_relaxed) + inc,
157  std::memory_order_release);
158  } else {
159  val_.store(inc, std::memory_order_relaxed);
160  reset_.store(false, std::memory_order_release);
161  }
162  ++numUpdates_;
163  if (UNLIKELY(
164  numUpdates_ >
165  parent_->cacheSize_.load(std::memory_order_acquire))) {
166  flush();
167  }
168  }
std::atomic< uint32_t > cacheSize_
#define LIKELY(x)
Definition: Likely.h:47
#define UNLIKELY(x)
Definition: Likely.h:48

Member Data Documentation

template<class IntT, class Tag = IntT>
uint32_t folly::ThreadCachedInt< IntT, Tag >::IntCache::numUpdates_
mutable
template<class IntT, class Tag = IntT>
ThreadCachedInt* folly::ThreadCachedInt< IntT, Tag >::IntCache::parent_

Definition at line 143 of file ThreadCachedInt.h.

template<class IntT, class Tag = IntT>
std::atomic<bool> folly::ThreadCachedInt< IntT, Tag >::IntCache::reset_

Definition at line 146 of file ThreadCachedInt.h.

template<class IntT, class Tag = IntT>
std::atomic<IntT> folly::ThreadCachedInt< IntT, Tag >::IntCache::val_
mutable

Definition at line 144 of file ThreadCachedInt.h.


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