2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_ATOMICBITSET_H_
18 #define FOLLY_ATOMICBITSET_H_
26 #include <boost/noncopyable.hpp>
31 * An atomic bitset of fixed size (specified at compile time).
34 class AtomicBitSet : private boost::noncopyable {
37 * Construct an AtomicBitSet; all bits are initially false.
42 * Set bit idx to true, using the given memory order. Returns the
43 * previous value of the bit.
45 * Note that the operation is a read-modify-write operation due to the use
48 bool set(size_t idx, std::memory_order order = std::memory_order_seq_cst);
51 * Set bit idx to false, using the given memory order. Returns the
52 * previous value of the bit.
54 * Note that the operation is a read-modify-write operation due to the use
57 bool reset(size_t idx, std::memory_order order = std::memory_order_seq_cst);
60 * Set bit idx to the given value, using the given memory order. Returns
61 * the previous value of the bit.
63 * Note that the operation is a read-modify-write operation due to the use
64 * of fetch_and or fetch_or.
66 * Yes, this is an overload of set(), to keep as close to std::bitset's
67 * interface as possible.
71 std::memory_order order = std::memory_order_seq_cst);
77 std::memory_order order = std::memory_order_seq_cst) const;
80 * Same as test() with the default memory order.
82 bool operator[](size_t idx) const;
85 * Return the size of the bitset.
87 constexpr size_t size() const {
92 // Pick the largest lock-free type available
93 #if (ATOMIC_LLONG_LOCK_FREE == 2)
94 typedef unsigned long long BlockType;
95 #elif (ATOMIC_LONG_LOCK_FREE == 2)
96 typedef unsigned long BlockType;
98 // Even if not lock free, what can we do?
99 typedef unsigned int BlockType;
101 typedef std::atomic<BlockType> AtomicBlockType;
103 static constexpr size_t kBitsPerBlock =
104 std::numeric_limits<BlockType>::digits;
106 static constexpr size_t blockIndex(size_t bit) {
107 return bit / kBitsPerBlock;
110 static constexpr size_t bitOffset(size_t bit) {
111 return bit % kBitsPerBlock;
115 static constexpr BlockType kOne = 1;
117 std::array<AtomicBlockType, N> data_;
120 // value-initialize to zero
122 inline AtomicBitSet<N>::AtomicBitSet() : data_() {
126 inline bool AtomicBitSet<N>::set(size_t idx, std::memory_order order) {
127 assert(idx < N * kBitsPerBlock);
128 BlockType mask = kOne << bitOffset(idx);
129 return data_[blockIndex(idx)].fetch_or(mask, order) & mask;
133 inline bool AtomicBitSet<N>::reset(size_t idx, std::memory_order order) {
134 assert(idx < N * kBitsPerBlock);
135 BlockType mask = kOne << bitOffset(idx);
136 return data_[blockIndex(idx)].fetch_and(~mask, order) & mask;
140 inline bool AtomicBitSet<N>::set(size_t idx,
142 std::memory_order order) {
143 return value ? set(idx, order) : reset(idx, order);
147 inline bool AtomicBitSet<N>::test(size_t idx, std::memory_order order) const {
148 assert(idx < N * kBitsPerBlock);
149 BlockType mask = kOne << bitOffset(idx);
150 return data_[blockIndex(idx)].load(order) & mask;
154 inline bool AtomicBitSet<N>::operator[](size_t idx) const {
160 #endif /* FOLLY_ATOMICBITSET_H_ */