proxygen
folly::AtomicBitSet< N > Class Template Reference

#include <AtomicBitSet.h>

Inheritance diagram for folly::AtomicBitSet< N >:

Public Member Functions

 AtomicBitSet ()
 
bool set (size_t idx, std::memory_order order=std::memory_order_seq_cst)
 
bool reset (size_t idx, std::memory_order order=std::memory_order_seq_cst)
 
bool set (size_t idx, bool value, std::memory_order order=std::memory_order_seq_cst)
 
bool test (size_t idx, std::memory_order order=std::memory_order_seq_cst) const
 
bool operator[] (size_t idx) const
 
constexpr size_t size () const
 

Private Types

typedef unsigned int BlockType
 
typedef std::atomic< BlockTypeAtomicBlockType
 

Static Private Member Functions

static constexpr size_t blockIndex (size_t bit)
 
static constexpr size_t bitOffset (size_t bit)
 

Private Attributes

std::array< AtomicBlockType, N > data_
 

Static Private Attributes

static constexpr size_t kBitsPerBlock
 
static constexpr BlockType kOne = 1
 

Detailed Description

template<size_t N>
class folly::AtomicBitSet< N >

An atomic bitset of fixed size (specified at compile time).

Definition at line 35 of file AtomicBitSet.h.

Member Typedef Documentation

template<size_t N>
typedef std::atomic<BlockType> folly::AtomicBitSet< N >::AtomicBlockType
private

Definition at line 103 of file AtomicBitSet.h.

template<size_t N>
typedef unsigned int folly::AtomicBitSet< N >::BlockType
private

Definition at line 101 of file AtomicBitSet.h.

Constructor & Destructor Documentation

template<size_t N>
folly::AtomicBitSet< N >::AtomicBitSet ( )
inline

Construct an AtomicBitSet; all bits are initially false.

Definition at line 124 of file AtomicBitSet.h.

124 : data_() {}
std::array< AtomicBlockType, N > data_
Definition: AtomicBitSet.h:119

Member Function Documentation

template<size_t N>
static constexpr size_t folly::AtomicBitSet< N >::bitOffset ( size_t  bit)
inlinestaticprivate

Definition at line 112 of file AtomicBitSet.h.

References folly::AtomicBitSet< N >::kBitsPerBlock.

Referenced by folly::AtomicBitSet< N >::reset(), folly::AtomicBitSet< N >::set(), and folly::AtomicBitSet< N >::test().

112  {
113  return bit % kBitsPerBlock;
114  }
static constexpr size_t kBitsPerBlock
Definition: AtomicBitSet.h:105
template<size_t N>
static constexpr size_t folly::AtomicBitSet< N >::blockIndex ( size_t  bit)
inlinestaticprivate

Definition at line 108 of file AtomicBitSet.h.

References folly::AtomicBitSet< N >::kBitsPerBlock.

Referenced by folly::AtomicBitSet< N >::reset(), folly::AtomicBitSet< N >::set(), and folly::AtomicBitSet< N >::test().

108  {
109  return bit / kBitsPerBlock;
110  }
static constexpr size_t kBitsPerBlock
Definition: AtomicBitSet.h:105
template<size_t N>
bool folly::AtomicBitSet< N >::operator[] ( size_t  idx) const
inline

Same as test() with the default memory order.

Definition at line 154 of file AtomicBitSet.h.

References folly::AtomicBitSet< N >::test().

154  {
155  return test(idx);
156 }
bool test(size_t idx, std::memory_order order=std::memory_order_seq_cst) const
Definition: AtomicBitSet.h:147
template<size_t N>
bool folly::AtomicBitSet< N >::reset ( size_t  idx,
std::memory_order  order = std::memory_order_seq_cst 
)
inline

Set bit idx to false, using the given memory order. Returns the previous value of the bit.

Note that the operation is a read-modify-write operation due to the use of fetch_and.

Definition at line 134 of file AtomicBitSet.h.

References folly::AtomicBitSet< N >::bitOffset(), folly::AtomicBitSet< N >::blockIndex(), folly::AtomicBitSet< N >::data_, folly::AtomicBitSet< N >::kBitsPerBlock, and folly::AtomicBitSet< N >::kOne.

Referenced by folly::AtomicBitSet< N >::set(), and folly::test::TEST().

134  {
135  assert(idx < N * kBitsPerBlock);
136  BlockType mask = kOne << bitOffset(idx);
137  return data_[blockIndex(idx)].fetch_and(~mask, order) & mask;
138 }
unsigned int BlockType
Definition: AtomicBitSet.h:101
static constexpr size_t kBitsPerBlock
Definition: AtomicBitSet.h:105
static constexpr BlockType kOne
Definition: AtomicBitSet.h:117
std::array< AtomicBlockType, N > data_
Definition: AtomicBitSet.h:119
static constexpr size_t blockIndex(size_t bit)
Definition: AtomicBitSet.h:108
static constexpr size_t bitOffset(size_t bit)
Definition: AtomicBitSet.h:112
int order
template<size_t N>
bool folly::AtomicBitSet< N >::set ( size_t  idx,
std::memory_order  order = std::memory_order_seq_cst 
)
inline

Set bit idx to true, using the given memory order. Returns the previous value of the bit.

Note that the operation is a read-modify-write operation due to the use of fetch_or.

Definition at line 127 of file AtomicBitSet.h.

References folly::AtomicBitSet< N >::bitOffset(), folly::AtomicBitSet< N >::blockIndex(), folly::AtomicBitSet< N >::data_, folly::AtomicBitSet< N >::kBitsPerBlock, and folly::AtomicBitSet< N >::kOne.

Referenced by folly::test::TEST().

127  {
128  assert(idx < N * kBitsPerBlock);
129  BlockType mask = kOne << bitOffset(idx);
130  return data_[blockIndex(idx)].fetch_or(mask, order) & mask;
131 }
unsigned int BlockType
Definition: AtomicBitSet.h:101
static constexpr size_t kBitsPerBlock
Definition: AtomicBitSet.h:105
static constexpr BlockType kOne
Definition: AtomicBitSet.h:117
std::array< AtomicBlockType, N > data_
Definition: AtomicBitSet.h:119
static constexpr size_t blockIndex(size_t bit)
Definition: AtomicBitSet.h:108
static constexpr size_t bitOffset(size_t bit)
Definition: AtomicBitSet.h:112
int order
template<size_t N>
bool folly::AtomicBitSet< N >::set ( size_t  idx,
bool  value,
std::memory_order  order = std::memory_order_seq_cst 
)
inline

Set bit idx to the given value, using the given memory order. Returns the previous value of the bit.

Note that the operation is a read-modify-write operation due to the use of fetch_and or fetch_or.

Yes, this is an overload of set(), to keep as close to std::bitset's interface as possible.

Definition at line 142 of file AtomicBitSet.h.

References order, and folly::AtomicBitSet< N >::reset().

142  {
143  return value ? set(idx, order) : reset(idx, order);
144 }
bool reset(size_t idx, std::memory_order order=std::memory_order_seq_cst)
Definition: AtomicBitSet.h:134
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
int order
template<size_t N>
constexpr size_t folly::AtomicBitSet< N >::size ( ) const
inline

Return the size of the bitset.

Definition at line 89 of file AtomicBitSet.h.

Referenced by folly::test::TEST().

89  {
90  return N;
91  }
template<size_t N>
bool folly::AtomicBitSet< N >::test ( size_t  idx,
std::memory_order  order = std::memory_order_seq_cst 
) const
inline

Read bit idx.

Definition at line 147 of file AtomicBitSet.h.

References folly::AtomicBitSet< N >::bitOffset(), folly::AtomicBitSet< N >::blockIndex(), folly::AtomicBitSet< N >::data_, folly::AtomicBitSet< N >::kBitsPerBlock, and folly::AtomicBitSet< N >::kOne.

Referenced by folly::AtomicBitSet< N >::operator[]().

147  {
148  assert(idx < N * kBitsPerBlock);
149  BlockType mask = kOne << bitOffset(idx);
150  return data_[blockIndex(idx)].load(order) & mask;
151 }
unsigned int BlockType
Definition: AtomicBitSet.h:101
static constexpr size_t kBitsPerBlock
Definition: AtomicBitSet.h:105
static constexpr BlockType kOne
Definition: AtomicBitSet.h:117
std::array< AtomicBlockType, N > data_
Definition: AtomicBitSet.h:119
static constexpr size_t blockIndex(size_t bit)
Definition: AtomicBitSet.h:108
static constexpr size_t bitOffset(size_t bit)
Definition: AtomicBitSet.h:112
int order

Member Data Documentation

template<size_t N>
std::array<AtomicBlockType, N> folly::AtomicBitSet< N >::data_
private
template<size_t N>
constexpr size_t folly::AtomicBitSet< N >::kBitsPerBlock
staticprivate
template<size_t N>
constexpr BlockType folly::AtomicBitSet< N >::kOne = 1
staticprivate

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