proxygen
|
#include <Tearable.h>
Classes | |
struct | AtomicWord |
union | RawWord |
Public Member Functions | |
Tearable ()=default | |
Tearable (const T &val) | |
void | load (T &dst) const |
void | store (const T &val) |
Private Attributes | |
AtomicWord | data_ [kNumDataWords] |
Static Private Attributes | |
static const std::size_t | kNumDataWords |
This class allows you to perform torn loads and stores on the bits of a trivially-copyable type T without triggering undefined behavior. You may encounter corrupt data, but should not encounter nasal demons.
This class provides no atomicity or memory ordering. Loads and stores are expected often to be data races. Synchronization is expected to be provided externally, and this class is helpful in building higher-level optimistic concurrency tools in combination with externally-provided synchronization.
To see why this is useful, consider the guarantees provided by std::atomic<T>. It ensures that every load returns a T that was stored in the atomic. If T is too large to be read/written with a single load/store instruction, std::atomic<T> falls back to locking to provide this guarantee. Users pay this cost even if they have some higher-level mechanism (an external lock, version numbers, other application-level reasoning) that makes them resilient to torn reads. Tearable<T> allows concurrent access without these costs.
For types smaller than the processor word size, prefer std::atomic<T>.
Definition at line 48 of file Tearable.h.
|
default |
|
inline |
Definition at line 58 of file Tearable.h.
References folly::Tearable< T >::store().
|
inline |
Definition at line 65 of file Tearable.h.
References folly::Tearable< T >::data_, i, folly::Tearable< T >::kNumDataWords, and folly::T.
|
inline |
Definition at line 74 of file Tearable.h.
References folly::Tearable< T >::data_, i, folly::Tearable< T >::kNumDataWords, and folly::T.
Referenced by folly::Tearable< T >::Tearable().
|
private |
Definition at line 98 of file Tearable.h.
Referenced by folly::Tearable< T >::load(), and folly::Tearable< T >::store().
|
staticprivate |
Definition at line 95 of file Tearable.h.
Referenced by folly::Tearable< T >::load(), and folly::Tearable< T >::store().