folly::observer::HazptrObserver

HazptrObserver implements a read-optimized Observer which caches an Observer's snapshot and protects access to it using hazptrs. The cached snapshot is kept up to date using a callback which fires when the original observer changes. This implementation incurs an additional allocation on updates making it less suitable for write-heavy workloads.

Synopsis

Declared in <folly/observer/HazptrObserver.h>

template<
    typename T,
    template<typename> typename Atom = atomic>
class HazptrObserver;

Description

There are 2 main APIs: 1) getSnapshot: Returns a Snapshot containing a const pointer to T and guards access to it using folly::hazptr_holder. The pointer is only safe to use while the returned Snapshot object is alive. 2) getLocalSnapshot: Same as getSnapshot but backed by folly::hazptr_local. This API is ~3ns faster than getSnapshot but is unsafe for the current thread to construct any other hazptr holder type objects (hazptr_holder, hazptr_array and other hazptr_local) while the returned snapshot exists.

See folly/synchronization/Hazptr.h for more details on hazptrs.

Type Aliases

NameDescription
DefaultSnapshot Snapshot type backed by a hazptr_holder.
LocalSnapshot Snapshot type backed by a hazptr_local.

Member Functions

NameDescription
HazptrObserver [constructor]Constructors
~HazptrObserver [destructor]Marks the instance dead and retires the cached state.
operator= Copy-assigns by destroying and reconstructing in place.
getLocalSnapshot Returns a hazptr_local-backed snapshot of the cached value.
getSnapshot Returns a hazptr_holder-backed snapshot of the cached value.
getUnderlyingObserver Returns the underlying observer being cached.
with Invoke a function with the current observed value. The snapshot is held alive for the duration of the call, preventing read-after-free when accessing members of the observed object.

Non-Member Functions

NameDescription
makeHazptrObserverSame as makeObserver(...), but creates HazptrObserver.