llvm::LazyAtomicPointer

Lock-free atomic pointer that coordinates concurrent writes from a lazy generator.

Synopsis

Declared in <llvm/ADT/LazyAtomicPointer.h>

template<class T>
class LazyAtomicPointer;

Description

Should be reserved for cases where concurrent uses of a generator for the same storage is unlikely.

The laziness comes in with loadOrGenerate(), which lazily calls the provided generator ONLY when the value is currently nullptr. With concurrent calls, only one generator is called and the rest see that value.

Most other APIs treat an in-flight loadOrGenerate() as if nullptr were stored. APIs that are required to write a value will spin.

The underlying storage is std::atomic<uintptr_t>.

TODO: In C++20, use std::atomic<T>::wait() instead of spinning and call std::atomic<T>::notify_all() in loadOrGenerate().

Member Functions

NameDescription
LazyAtomicPointer [constructor]Constructors
operator= Assignment operators
compare_exchange_strong Compare-exchange. Keeps trying if there is a concurrent loadOrGenerate() call.
compare_exchange_weak Compare-exchange. Returns false if there is a concurrent loadOrGenerate() call, setting ExistingValue to nullptr.
exchange Set a value. Return the old value. Waits for concurrent loadOrGenerate() calls.
load Return the current stored value. Returns None if there is a concurrent loadOrGenerate() in flight.
loadOrGenerate Get the current value, or call Generator to generate a value. Guarantees that only one thread's Generator will run.
operator* Dereference the stored pointer; asserts if null or busy.
operator-> Access a member through the stored pointer; asserts if null or busy.
store Store a value. Waits for concurrent loadOrGenerate() calls.
operator T* Convert to the currently stored pointer, or null if busy.
operator bool Return true if a non-null pointer is currently stored.