38b03c840e
Summary: This ports `folly::DistributedMutex` into RocksDB. The PR includes everything else needed to compile and use DistributedMutex as a component within folly. Most files are unchanged except for some portability stuff and includes. For now, I've put this under `rocksdb/third-party`, but if there is a better folder to put this under, let me know. I also am not sure how or where to put unit tests for third-party stuff like this. It seems like gtest is included already, but I need to link with it from another third-party folder. This also includes some other common components from folly - folly/Optional - folly/ScopeGuard (In particular `SCOPE_EXIT`) - folly/synchronization/ParkingLot (A portable futex-like interface) - folly/synchronization/AtomicNotification (The standard C++ interface for futexes) - folly/Indestructible (For singletons that don't get destroyed without allocations) Pull Request resolved: https://github.com/facebook/rocksdb/pull/5642 Differential Revision: D16544439 fbshipit-source-id: 179b98b5dcddc3075926d31a30f92fd064245731
53 lines
1.8 KiB
C++
53 lines
1.8 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
|
|
namespace folly {
|
|
|
|
/**
|
|
* Sets a bit at the given index in the binary representation of the integer
|
|
* to 1. Returns the previous value of the bit, so true if the bit was not
|
|
* changed, false otherwise
|
|
*
|
|
* On some architectures, using this is more efficient than the corresponding
|
|
* std::atomic::fetch_or() with a mask. For example to set the first (least
|
|
* significant) bit of an integer, you could do atomic.fetch_or(0b1)
|
|
*
|
|
* The efficiency win is only visible in x86 (yet) and comes from the
|
|
* implementation using the x86 bts instruction when possible.
|
|
*
|
|
* When something other than std::atomic is passed, the implementation assumed
|
|
* incompatibility with this interface and calls Atomic::fetch_or()
|
|
*/
|
|
template <typename Atomic>
|
|
bool atomic_fetch_set(
|
|
Atomic& atomic,
|
|
std::size_t bit,
|
|
std::memory_order order = std::memory_order_seq_cst);
|
|
|
|
/**
|
|
* Resets a bit at the given index in the binary representation of the integer
|
|
* to 0. Returns the previous value of the bit, so true if the bit was
|
|
* changed, false otherwise
|
|
*
|
|
* This follows the same underlying principle and implementation as
|
|
* fetch_set(). Using the optimized implementation when possible and falling
|
|
* back to std::atomic::fetch_and() when in debug mode or in an architecture
|
|
* where an optimization is not possible
|
|
*/
|
|
template <typename Atomic>
|
|
bool atomic_fetch_reset(
|
|
Atomic& atomic,
|
|
std::size_t bit,
|
|
std::memory_order order = std::memory_order_seq_cst);
|
|
|
|
} // namespace folly
|
|
|
|
#include <folly/synchronization/AtomicUtil-inl.h>
|