LLVM 20.0.0git
Public Member Functions | List of all members
llvm::LazyAtomicPointer< T > Class Template Reference

Atomic pointer that's lock-free, but that can coordinate concurrent writes from a lazy generator. More...

#include "llvm/ADT/LazyAtomicPointer.h"

Public Member Functions

void store (T *Value)
 Store a value. Waits for concurrent loadOrGenerate() calls.
 
Texchange (T *Value)
 Set a value.
 
bool compare_exchange_weak (T *&ExistingValue, T *NewValue)
 Compare-exchange.
 
bool compare_exchange_strong (T *&ExistingValue, T *NewValue)
 Compare-exchange.
 
Tload () const
 Return the current stored value.
 
TloadOrGenerate (function_ref< T *()> Generator)
 Get the current value, or call Generator to generate a value.
 
 operator bool () const
 
 operator T* () const
 
Toperator* () const
 
Toperator-> () const
 
 LazyAtomicPointer ()
 
 LazyAtomicPointer (std::nullptr_t)
 
 LazyAtomicPointer (T *Value)
 
 LazyAtomicPointer (const LazyAtomicPointer &RHS)
 
LazyAtomicPointeroperator= (std::nullptr_t)
 
LazyAtomicPointeroperator= (T *RHS)
 
LazyAtomicPointeroperator= (const LazyAtomicPointer &RHS)
 

Detailed Description

template<class T>
class llvm::LazyAtomicPointer< T >

Atomic pointer that's lock-free, but that can coordinate concurrent writes from a lazy generator.

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().

Definition at line 34 of file LazyAtomicPointer.h.

Constructor & Destructor Documentation

◆ LazyAtomicPointer() [1/4]

template<class T >
llvm::LazyAtomicPointer< T >::LazyAtomicPointer ( )
inline

Definition at line 141 of file LazyAtomicPointer.h.

◆ LazyAtomicPointer() [2/4]

template<class T >
llvm::LazyAtomicPointer< T >::LazyAtomicPointer ( std::nullptr_t  )
inline

Definition at line 142 of file LazyAtomicPointer.h.

◆ LazyAtomicPointer() [3/4]

template<class T >
llvm::LazyAtomicPointer< T >::LazyAtomicPointer ( T Value)
inline

Definition at line 143 of file LazyAtomicPointer.h.

◆ LazyAtomicPointer() [4/4]

template<class T >
llvm::LazyAtomicPointer< T >::LazyAtomicPointer ( const LazyAtomicPointer< T > &  RHS)
inline

Definition at line 144 of file LazyAtomicPointer.h.

Member Function Documentation

◆ compare_exchange_strong()

template<class T >
bool llvm::LazyAtomicPointer< T >::compare_exchange_strong ( T *&  ExistingValue,
T NewValue 
)
inline

Compare-exchange.

Keeps trying if there is a concurrent loadOrGenerate() call.

Keep trying as long as it's busy.

Definition at line 80 of file LazyAtomicPointer.h.

References LLVM_UNLIKELY.

◆ compare_exchange_weak()

template<class T >
bool llvm::LazyAtomicPointer< T >::compare_exchange_weak ( T *&  ExistingValue,
T NewValue 
)
inline

Compare-exchange.

Returns false if there is a concurrent loadOrGenerate() call, setting ExistingValue to nullptr.

Report the existing value as "None" if busy.

Definition at line 65 of file LazyAtomicPointer.h.

Referenced by llvm::LazyAtomicPointer< T >::exchange().

◆ exchange()

template<class T >
T * llvm::LazyAtomicPointer< T >::exchange ( T Value)
inline

Set a value.

Return the old value. Waits for concurrent loadOrGenerate() calls.

Definition at line 54 of file LazyAtomicPointer.h.

References llvm::LazyAtomicPointer< T >::compare_exchange_weak().

Referenced by llvm::LazyAtomicPointer< T >::store().

◆ load()

template<class T >
T * llvm::LazyAtomicPointer< T >::load ( ) const
inline

Return the current stored value.

Returns None if there is a concurrent loadOrGenerate() in flight.

Definition at line 100 of file LazyAtomicPointer.h.

Referenced by llvm::LazyAtomicPointer< T >::operator bool(), llvm::LazyAtomicPointer< T >::operator T*(), and llvm::LazyAtomicPointer< T >::operator*().

◆ loadOrGenerate()

template<class T >
T & llvm::LazyAtomicPointer< T >::loadOrGenerate ( function_ref< T *()>  Generator)
inline

Get the current value, or call Generator to generate a value.

Guarantees that only one thread's Generator will run.

Precondition
Generator doesn't return nullptr.

Definition at line 109 of file LazyAtomicPointer.h.

References assert(), and LLVM_LIKELY.

◆ operator bool()

template<class T >
llvm::LazyAtomicPointer< T >::operator bool ( ) const
inlineexplicit

Definition at line 131 of file LazyAtomicPointer.h.

References llvm::LazyAtomicPointer< T >::load().

◆ operator T*()

template<class T >
llvm::LazyAtomicPointer< T >::operator T* ( ) const
inline

Definition at line 132 of file LazyAtomicPointer.h.

References llvm::LazyAtomicPointer< T >::load().

◆ operator*()

template<class T >
T & llvm::LazyAtomicPointer< T >::operator* ( ) const
inline

◆ operator->()

template<class T >
T * llvm::LazyAtomicPointer< T >::operator-> ( ) const
inline

Definition at line 139 of file LazyAtomicPointer.h.

References llvm::LazyAtomicPointer< T >::operator*().

◆ operator=() [1/3]

template<class T >
LazyAtomicPointer & llvm::LazyAtomicPointer< T >::operator= ( const LazyAtomicPointer< T > &  RHS)
inline

Definition at line 155 of file LazyAtomicPointer.h.

References RHS, and llvm::LazyAtomicPointer< T >::store().

◆ operator=() [2/3]

template<class T >
LazyAtomicPointer & llvm::LazyAtomicPointer< T >::operator= ( std::nullptr_t  )
inline

Definition at line 147 of file LazyAtomicPointer.h.

References llvm::LazyAtomicPointer< T >::store().

◆ operator=() [3/3]

template<class T >
LazyAtomicPointer & llvm::LazyAtomicPointer< T >::operator= ( T RHS)
inline

Definition at line 151 of file LazyAtomicPointer.h.

References RHS, and llvm::LazyAtomicPointer< T >::store().

◆ store()

template<class T >
void llvm::LazyAtomicPointer< T >::store ( T Value)
inline

Store a value. Waits for concurrent loadOrGenerate() calls.

Definition at line 50 of file LazyAtomicPointer.h.

References llvm::LazyAtomicPointer< T >::exchange().

Referenced by llvm::LazyAtomicPointer< T >::operator=().


The documentation for this class was generated from the following file: