#include <lazyinitthreaded.h>
Helper class for multithreaded lazy initialization.
Typical usage case is a method which initializes data on first call, for example
Performance note: Declaring a global LazyInitThreaded as a static member of a class will degrade its performance because the compiler will guard its access with a slow and unnecessary mutex. To avoid this move the global state outside of the class.
By default cancellation of the caller will be forwarded to the lambda passed to Init() and after a OperationCancelledError Init() will retry the initialization.
THREADSAFE.
Classes | |
class | CancellationForwarder |
Public Types | |
enum class | OPTIONS : Int { NONE , FORWARD_CANCELLATION , RETRY_ON_FAILURE , RETRY_ON_CANCELLATION , NO_EXCLUSIVE_RESOURCE , DEFAULT } |
Public Member Functions | |
enum maxon::LazyInitThreaded::OPTIONS Int | MAXON_ENUM_FLAGS_CLASS (OPTIONS) |
~LazyInitThreaded () | |
template<typename FN > | |
MAXON_ATTRIBUTE_FORCE_INLINE auto | Init (FN &&fn, OPTIONS options=OPTIONS::DEFAULT) -> decltype(fn()) |
template<typename FN = decltype(Dummy)> | |
void | Reset (FN &&fn=Dummy) |
Bool | IsInitialized () const |
Bool | HasFailed () const |
operator Bool () const | |
Static Private Member Functions | |
static void | Dummy () |
Private Attributes | |
maxon::LazyInitThreaded::CancellationForwarder | MAXON_ENUM_FLAGS_CLASS |
AtomicInt | _state |
Static Private Attributes | |
static const Int | UNINITIALIZED |
static const Int | FAILED |
static const Int | PENDING |
static const Int | ENQUEUED |
static const Int | CHECKING |
static const Int | INITIALIZED |
static const Int | STATE_MASK |
static const Int | VALUE_MASK |
~LazyInitThreaded | ( | ) |
|
staticprivate |
enum maxon::LazyInitThreaded::OPTIONS Int MAXON_ENUM_FLAGS_CLASS | ( | OPTIONS | ) |
MAXON_ATTRIBUTE_FORCE_INLINE auto Init | ( | FN && | fn, |
OPTIONS | options = OPTIONS::DEFAULT |
||
) | -> decltype(fn()) |
Initializes an object by calling the specified method (and does nothing if the object has already been initialized). The method #fnwill be executed by a job and is free to execute long running multithreaded code for initialization. Waiting threads will either participate in the initialization or go to sleep. They will not idle unnecessarily. If the initialization failed once and #retryOnFailure was false (the default) all following Init() calls will return the initial error. THREADSAFE.
[in] | fn | Method (usually a lambda) to initialize something, must return Result<void>. |
[in] | options | OPTIONS::FORWARD_CANCELLATION by default. |
void Reset | ( | FN && | fn = Dummy | ) |
Resets an object by calling the specified method. Does nothing if the object has already been reset. If the initialization failed the optional reset method won't be invoked. THREADSAFE.
[in] | fn | Optional method (usually a lambda) to reset something. |
Bool IsInitialized | ( | ) | const |
Returns if the object already has been initialized. THREADSAFE.
Bool HasFailed | ( | ) | const |
|
explicit |
Returns if the object already has been initialized. THREADSAFE.
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
private |
|
private |