proxygen
folly::ThreadCachedInt< IntT, Tag > Class Template Reference

#include <ThreadCachedInt.h>

Inheritance diagram for folly::ThreadCachedInt< IntT, Tag >:

Classes

struct  IntCache
 

Public Member Functions

 ThreadCachedInt (IntT initialVal=0, uint32_t cacheSize=1000)
 
void increment (IntT inc)
 
IntT readFast () const
 
IntT readFull () const
 
IntT readFastAndReset ()
 
IntT readFullAndReset ()
 
void setCacheSize (uint32_t newSize)
 
uint32_t getCacheSize () const
 
ThreadCachedIntoperator+= (IntT inc)
 
ThreadCachedIntoperator-= (IntT inc)
 
ThreadCachedIntoperator++ ()
 
ThreadCachedIntoperator-- ()
 
void set (IntT newVal)
 

Private Attributes

std::atomic< IntT > target_
 
std::atomic< uint32_tcacheSize_
 
ThreadLocalPtr< IntCache, Tag, AccessModeStrictcache_
 

Detailed Description

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

Definition at line 39 of file ThreadCachedInt.h.

Constructor & Destructor Documentation

template<class IntT, class Tag = IntT>
folly::ThreadCachedInt< IntT, Tag >::ThreadCachedInt ( IntT  initialVal = 0,
uint32_t  cacheSize = 1000 
)
inlineexplicit

Definition at line 43 of file ThreadCachedInt.h.

44  : target_(initialVal), cacheSize_(cacheSize) {}
std::atomic< uint32_t > cacheSize_
std::atomic< IntT > target_

Member Function Documentation

template<class IntT, class Tag = IntT>
uint32_t folly::ThreadCachedInt< IntT, Tag >::getCacheSize ( ) const
inline

Definition at line 103 of file ThreadCachedInt.h.

103  {
104  return cacheSize_.load();
105  }
std::atomic< uint32_t > cacheSize_
template<class IntT, class Tag = IntT>
void folly::ThreadCachedInt< IntT, Tag >::increment ( IntT  inc)
inline

Definition at line 46 of file ThreadCachedInt.h.

Referenced by folly::ThreadCachedInt< uint64_t >::operator++(), folly::ThreadCachedInt< uint64_t >::operator+=(), folly::ThreadCachedInt< uint64_t >::operator--(), and folly::ThreadCachedInt< uint64_t >::operator-=().

46  {
47  auto cache = cache_.get();
48  if (UNLIKELY(cache == nullptr)) {
49  cache = new IntCache(*this);
50  cache_.reset(cache);
51  }
52  cache->increment(inc);
53  }
ThreadLocalPtr< IntCache, Tag, AccessModeStrict > cache_
#define UNLIKELY(x)
Definition: Likely.h:48
template<class IntT, class Tag = IntT>
ThreadCachedInt& folly::ThreadCachedInt< IntT, Tag >::operator++ ( )
inline

Definition at line 116 of file ThreadCachedInt.h.

116  {
117  increment(1);
118  return *this;
119  }
void increment(IntT inc)
template<class IntT, class Tag = IntT>
ThreadCachedInt& folly::ThreadCachedInt< IntT, Tag >::operator+= ( IntT  inc)
inline

Definition at line 107 of file ThreadCachedInt.h.

107  {
108  increment(inc);
109  return *this;
110  }
void increment(IntT inc)
template<class IntT, class Tag = IntT>
ThreadCachedInt& folly::ThreadCachedInt< IntT, Tag >::operator-- ( )
inline

Definition at line 120 of file ThreadCachedInt.h.

120  {
121  increment(IntT(-1));
122  return *this;
123  }
void increment(IntT inc)
template<class IntT, class Tag = IntT>
ThreadCachedInt& folly::ThreadCachedInt< IntT, Tag >::operator-= ( IntT  inc)
inline

Definition at line 111 of file ThreadCachedInt.h.

111  {
112  increment(-inc);
113  return *this;
114  }
void increment(IntT inc)
template<class IntT, class Tag = IntT>
IntT folly::ThreadCachedInt< IntT, Tag >::readFast ( ) const
inline
template<class IntT, class Tag = IntT>
IntT folly::ThreadCachedInt< IntT, Tag >::readFastAndReset ( )
inline

Definition at line 77 of file ThreadCachedInt.h.

Referenced by folly::ThreadCachedInt< uint64_t >::readFullAndReset().

77  {
78  return target_.exchange(0, std::memory_order_release);
79  }
std::atomic< IntT > target_
template<class IntT, class Tag = IntT>
IntT folly::ThreadCachedInt< IntT, Tag >::readFull ( ) const
inline

Definition at line 63 of file ThreadCachedInt.h.

Referenced by BENCHMARK(), folly::AtomicHashArray< KeyT, ValueT, HashFcn, EqualFcn, Allocator, ProbeFcn, KeyConvertFcn >::insertInternal(), folly::AtomicHashMap< KeyT, ValueT, HashFcn, EqualFcn, Allocator, ProbeFcn, KeyConvertFcn >::spaceRemaining(), and TEST().

63  {
64  // This could race with thread destruction and so the access lock should be
65  // acquired before reading the current value
66  const auto accessor = cache_.accessAllThreads();
67  IntT ret = readFast();
68  for (const auto& cache : accessor) {
69  if (!cache.reset_.load(std::memory_order_acquire)) {
70  ret += cache.val_.load(std::memory_order_relaxed);
71  }
72  }
73  return ret;
74  }
ThreadLocalPtr< IntCache, Tag, AccessModeStrict > cache_
template<class IntT, class Tag = IntT>
IntT folly::ThreadCachedInt< IntT, Tag >::readFullAndReset ( )
inline

Definition at line 85 of file ThreadCachedInt.h.

85  {
86  // This could race with thread destruction and so the access lock should be
87  // acquired before reading the current value
88  auto accessor = cache_.accessAllThreads();
89  IntT ret = readFastAndReset();
90  for (auto& cache : accessor) {
91  if (!cache.reset_.load(std::memory_order_acquire)) {
92  ret += cache.val_.load(std::memory_order_relaxed);
93  cache.reset_.store(true, std::memory_order_release);
94  }
95  }
96  return ret;
97  }
ThreadLocalPtr< IntCache, Tag, AccessModeStrict > cache_
template<class IntT, class Tag = IntT>
void folly::ThreadCachedInt< IntT, Tag >::set ( IntT  newVal)
inline

Definition at line 128 of file ThreadCachedInt.h.

Referenced by folly::AtomicHashArray< KeyT, ValueT, HashFcn, EqualFcn, Allocator, ProbeFcn, KeyConvertFcn >::clear(), and TEST().

128  {
129  for (auto& cache : cache_.accessAllThreads()) {
130  cache.reset_.store(true, std::memory_order_release);
131  }
132  target_.store(newVal, std::memory_order_release);
133  }
std::atomic< IntT > target_
ThreadLocalPtr< IntCache, Tag, AccessModeStrict > cache_
template<class IntT, class Tag = IntT>
void folly::ThreadCachedInt< IntT, Tag >::setCacheSize ( uint32_t  newSize)
inline

Definition at line 99 of file ThreadCachedInt.h.

99  {
100  cacheSize_.store(newSize, std::memory_order_release);
101  }
std::atomic< uint32_t > cacheSize_

Member Data Documentation


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