2019-05-22 20:15:35 +00:00
|
|
|
/*
|
2020-07-20 16:35:17 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2019-05-22 20:15:35 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
2020-07-20 16:35:17 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-05-22 20:15:35 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include <folly/Portability.h>
|
|
|
|
|
|
|
|
namespace folly {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An atomic bitset of fixed size (specified at compile time).
|
2020-07-20 16:35:17 +00:00
|
|
|
*
|
|
|
|
* Formerly known as AtomicBitSet. It was renamed while fixing a bug
|
|
|
|
* to avoid any silent breakages during run time.
|
2019-05-22 20:15:35 +00:00
|
|
|
*/
|
|
|
|
template <size_t N>
|
2020-07-20 16:35:17 +00:00
|
|
|
class ConcurrentBitSet {
|
2019-05-22 20:15:35 +00:00
|
|
|
public:
|
|
|
|
/**
|
2020-07-20 16:35:17 +00:00
|
|
|
* Construct a ConcurrentBitSet; all bits are initially false.
|
2019-05-22 20:15:35 +00:00
|
|
|
*/
|
2020-07-20 16:35:17 +00:00
|
|
|
ConcurrentBitSet();
|
|
|
|
|
|
|
|
ConcurrentBitSet(const ConcurrentBitSet&) = delete;
|
|
|
|
ConcurrentBitSet& operator=(const ConcurrentBitSet&) = delete;
|
2019-05-22 20:15:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool set(size_t idx, std::memory_order order = std::memory_order_seq_cst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool reset(size_t idx, std::memory_order order = std::memory_order_seq_cst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool set(
|
|
|
|
size_t idx,
|
|
|
|
bool value,
|
|
|
|
std::memory_order order = std::memory_order_seq_cst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read bit idx.
|
|
|
|
*/
|
|
|
|
bool test(size_t idx, std::memory_order order = std::memory_order_seq_cst)
|
|
|
|
const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as test() with the default memory order.
|
|
|
|
*/
|
|
|
|
bool operator[](size_t idx) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the bitset.
|
|
|
|
*/
|
|
|
|
constexpr size_t size() const {
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Pick the largest lock-free type available
|
|
|
|
#if (ATOMIC_LLONG_LOCK_FREE == 2)
|
|
|
|
typedef unsigned long long BlockType;
|
|
|
|
#elif (ATOMIC_LONG_LOCK_FREE == 2)
|
|
|
|
typedef unsigned long BlockType;
|
|
|
|
#else
|
|
|
|
// Even if not lock free, what can we do?
|
|
|
|
typedef unsigned int BlockType;
|
|
|
|
#endif
|
|
|
|
typedef std::atomic<BlockType> AtomicBlockType;
|
|
|
|
|
|
|
|
static constexpr size_t kBitsPerBlock =
|
|
|
|
std::numeric_limits<BlockType>::digits;
|
|
|
|
|
|
|
|
static constexpr size_t blockIndex(size_t bit) {
|
|
|
|
return bit / kBitsPerBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t bitOffset(size_t bit) {
|
|
|
|
return bit % kBitsPerBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
// avoid casts
|
|
|
|
static constexpr BlockType kOne = 1;
|
2020-07-20 16:35:17 +00:00
|
|
|
static constexpr size_t kNumBlocks = (N + kBitsPerBlock - 1) / kBitsPerBlock;
|
|
|
|
std::array<AtomicBlockType, kNumBlocks> data_;
|
2019-05-22 20:15:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// value-initialize to zero
|
|
|
|
template <size_t N>
|
2020-07-20 16:35:17 +00:00
|
|
|
inline ConcurrentBitSet<N>::ConcurrentBitSet() : data_() {}
|
2019-05-22 20:15:35 +00:00
|
|
|
|
|
|
|
template <size_t N>
|
2020-07-20 16:35:17 +00:00
|
|
|
inline bool ConcurrentBitSet<N>::set(size_t idx, std::memory_order order) {
|
|
|
|
assert(idx < N);
|
2019-05-22 20:15:35 +00:00
|
|
|
BlockType mask = kOne << bitOffset(idx);
|
|
|
|
return data_[blockIndex(idx)].fetch_or(mask, order) & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
2020-07-20 16:35:17 +00:00
|
|
|
inline bool ConcurrentBitSet<N>::reset(size_t idx, std::memory_order order) {
|
|
|
|
assert(idx < N);
|
2019-05-22 20:15:35 +00:00
|
|
|
BlockType mask = kOne << bitOffset(idx);
|
|
|
|
return data_[blockIndex(idx)].fetch_and(~mask, order) & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
inline bool
|
2020-07-20 16:35:17 +00:00
|
|
|
ConcurrentBitSet<N>::set(size_t idx, bool value, std::memory_order order) {
|
2019-05-22 20:15:35 +00:00
|
|
|
return value ? set(idx, order) : reset(idx, order);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
2020-07-20 16:35:17 +00:00
|
|
|
inline bool ConcurrentBitSet<N>::test(size_t idx, std::memory_order order)
|
|
|
|
const {
|
|
|
|
assert(idx < N);
|
2019-05-22 20:15:35 +00:00
|
|
|
BlockType mask = kOne << bitOffset(idx);
|
|
|
|
return data_[blockIndex(idx)].load(order) & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
2020-07-20 16:35:17 +00:00
|
|
|
inline bool ConcurrentBitSet<N>::operator[](size_t idx) const {
|
2019-05-22 20:15:35 +00:00
|
|
|
return test(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace folly
|