BDE 4.14.0 Production release
|
#include <bslmt_once.h>
Public Types | |
typedef QLockGuard | OnceLock |
Public Member Functions | |
bool | enter (OnceLock *onceLock) |
void | leave (OnceLock *onceLock) |
void | cancel (OnceLock *onceLock) |
template<class FUNC > | |
void | callOnce (FUNC &function) |
template<class FUNC > | |
void | callOnce (const FUNC &function) |
bool | isMaybeUninitialized () const |
Public Attributes | |
QLock | d_mutex |
bsls::AtomicOperations::AtomicTypes::Int | d_state |
Gate-keeper class for code that should only execute once per process. This class is a POD-type and can be statically initialized to the value of the BSLMT_ONCE_INITIALIZE
macro. For this reason, it does not have any explicitly-declared constructors or destructor.
See bslmt_once
typedef QLockGuard bslmt::Once::OnceLock |
Special token created by a single thread to pass to the enter
, leave
, and cancel
methods.
|
inline |
|
inline |
If no other thread has yet called enter
or callOnce
, then call the specified function
and set this object to the state where pending and future calls to enter
or callOnce
will return false
or do nothing, respectively. Otherwise, wait for the one-time code to complete and return without calling function
where function
is a function or functor that can be called with no arguments. Note that one-time code is considered not to have run if function
terminates with an exception.
void bslmt::Once::cancel | ( | OnceLock * | onceLock | ) |
Revert this object to the state it was in before enter
or callOnce
was called, then unlock the internal mutex using the specified onceLock
(possibly unblocking pending calls to enter
or callOnce
). This method may only be used to cancel execution of one-time code that has not yet completed. The behavior is undefined unless onceLock
was locked by a matching call to enter
on this object and has not been tampered-with since (especially by calling leave
).
bool bslmt::Once::enter | ( | OnceLock * | onceLock | ) |
Lock the internal mutex using the specified onceLock
(possibly blocking if another thread has already locked the mutex). If no other thread has yet called enter
or callOnce
on this object, return true
. Otherwise, unlock the mutex and return false
. The mutex lock may be skipped if it can be determined that it will not be needed. The behavior is undefined if onceLock
is already in a locked state on entry to this method. Note that if enter
returns true
, the caller must eventually call leave
, or else other threads may block indefinitely.
|
inline |
Return true
if this object may not be in the "done" state (that is, leave
has not been called).
void bslmt::Once::leave | ( | OnceLock * | onceLock | ) |
Set this object into a state such that pending and future calls to enter
or callOnce
will return false
or do nothing, respectively, then unlock the internal mutex using the specified onceLock
(possibly unblocking pending calls to enter
or callOnce
). The behavior is undefined unless onceLock
was locked by a matching call to enter
on this object and has not been tampered-with since.
QLock bslmt::Once::d_mutex |
bsls::AtomicOperations::AtomicTypes::Int bslmt::Once::d_state |