Quick Links: |
#include <bslmt_fastpostsemaphore.h>
Public Types | |
enum | ReturnValue { e_SUCCESS = Impl::e_SUCCESS, e_DISABLED = Impl::e_DISABLED, e_TIMED_OUT = Impl::e_TIMED_OUT, e_WOULD_BLOCK = Impl::e_WOULD_BLOCK, e_FAILED = Impl::e_FAILED } |
Public Member Functions | |
FastPostSemaphore (bsls::SystemClockType::Enum clockType=bsls::SystemClockType::e_REALTIME) | |
FastPostSemaphore (const bsl::chrono::system_clock &) | |
FastPostSemaphore (const bsl::chrono::steady_clock &) | |
FastPostSemaphore (int count, bsls::SystemClockType::Enum clockType=bsls::SystemClockType::e_REALTIME) | |
FastPostSemaphore (int count, const bsl::chrono::system_clock &) | |
FastPostSemaphore (int count, const bsl::chrono::steady_clock &) | |
~FastPostSemaphore () | |
void | enable () |
void | disable () |
void | post () |
void | post (int value) |
int | take (int maximumToTake) |
int | takeAll () |
int | timedWait (const bsls::TimeInterval &absTime) |
template<class CLOCK , class DURATION > | |
int | timedWait (const bsl::chrono::time_point< CLOCK, DURATION > &absTime) |
int | tryWait () |
int | wait () |
bsls::SystemClockType::Enum | clockType () const |
int | getDisabledState () const |
int | getValue () const |
bool | isDisabled () const |
This class implements a semaphore type, optimized for post
, for thread synchronization.
See Component bslmt_fastpostsemaphore
bslmt::FastPostSemaphore::FastPostSemaphore | ( | bsls::SystemClockType::Enum | clockType = bsls::SystemClockType::e_REALTIME |
) | [explicit] |
Create a FastPostSemaphore
object initially having a count of 0. Optionally specify a clockType
indicating the type of the system clock against which the bsls::TimeInterval
absTime
timeouts passed to the timedWait
method are to be interpreted (see Supported Clock-Types in the component-level documentation). If clockType
is not specified then the realtime system clock is used.
bslmt::FastPostSemaphore::FastPostSemaphore | ( | const bsl::chrono::system_clock & | ) | [explicit] |
Create a FastPostSemaphore
object initially having a count of 0. Use the realtime system clock as the clock against which the absTime
timeouts passed to the timedWait
methods are interpreted (see Supported Clock-Types in the component-level documentation).
bslmt::FastPostSemaphore::FastPostSemaphore | ( | const bsl::chrono::steady_clock & | ) | [explicit] |
Create a FastPostSemaphore
object initially having a count of 0. Use the monotonic system clock as the clock against which the absTime
timeouts passed to the timedWait
methods are interpreted (see Supported Clock-Types in the component-level documentation).
bslmt::FastPostSemaphore::FastPostSemaphore | ( | int | count, | |
bsls::SystemClockType::Enum | clockType = bsls::SystemClockType::e_REALTIME | |||
) | [explicit] |
Create a FastPostSemaphore
object initially having the specified count
. Optionally specify a clockType
indicating the type of the system clock against which the bsls::TimeInterval
absTime
timeouts passed to the timedWait
method are to be interpreted (see Supported Clock-Types in the component-level documentation). If clockType
is not specified then the realtime system clock is used.
bslmt::FastPostSemaphore::FastPostSemaphore | ( | int | count, | |
const bsl::chrono::system_clock & | ||||
) |
Create a FastPostSemaphore
object initially having the specified count
. Use the realtime system clock as the clock against which the absTime
timeouts passed to the timedWait
methods are interpreted (see Supported Clock-Types in the component-level documentation).
bslmt::FastPostSemaphore::FastPostSemaphore | ( | int | count, | |
const bsl::chrono::steady_clock & | ||||
) |
Create a FastPostSemaphore
object initially having the specified count
. Use the monotonic system clock as the clock against which the absTime
timeouts passed to the timedWait
methods are interpreted (see Supported Clock-Types in the component-level documentation).
bslmt::FastPostSemaphore::~FastPostSemaphore | ( | ) |
Destroy this object.
void bslmt::FastPostSemaphore::enable | ( | ) |
Enable waiting on this semaphore. If the semaphore is not disabled, this call has no effect.
void bslmt::FastPostSemaphore::disable | ( | ) |
Disable waiting on this semaphore. All subsequent invocations of wait
, tryWait
, and timedWait
will fail immediately. All blocked invocations of wait
and timedWait
will fail immediately. If the semaphore is already disabled, this method will have no effect.
void bslmt::FastPostSemaphore::post | ( | ) |
Atomically increment the count of this semaphore.
void bslmt::FastPostSemaphore::post | ( | int | value | ) |
Atomically increase the count of this semaphore by the specified value
. The behavior is undefined unless value > 0
.
int bslmt::FastPostSemaphore::take | ( | int | maximumToTake | ) |
If the count of this semaphore is positive, reduce the count by the lesser of the count and the specified maximumToTake
and return the magnitude of the change to the count. Otherwise, do nothing and return 0.
int bslmt::FastPostSemaphore::takeAll | ( | ) |
If the count of this semaphore is positive, reduce the count to 0 and return the original value of the count. Otherwise, do nothing and return 0.
int bslmt::FastPostSemaphore::timedWait | ( | const bsls::TimeInterval & | absTime | ) |
If this semaphore is initially disabled, or becomes disabled while blocking, return e_DISABLED
with no effect on the count. Otherwise, block until the count of this semaphore is a positive value or the specified absTime
timeout expires. If the count of this semaphore is a positive value, return 0 and atomically decrement the count. If the absTime
timeout expires, return e_TIMED_OUT
with no effect on the count. Return e_FAILED
if an error occurs. Errors are unrecoverable. After an error, the semaphore may be destroyed, but any other use has undefined behavior. absTime
is an absolute time represented as an interval from some epoch, which is determined by the clock indicated at construction (see Supported Clock-Types in the component documentation).
int bslmt::FastPostSemaphore::timedWait | ( | const bsl::chrono::time_point< CLOCK, DURATION > & | absTime | ) |
If this semaphore is initially disabled, or becomes disabled while blocking, return e_DISABLED
with no effect on the count. Otherwise, block until the count of this semaphore is a positive value or the specified absTime
timeout expires. If the count of this semaphore is a positive value, return 0 and atomically decrement the count. If the absTime
timeout expires, return e_TIMEDOUT
with no effect on the count. Return e_FAILED
if an error occurs. Errors are unrecoverable. After an error, the semaphore may be destroyed, but any other use has undefined behavior. absTime
is an absolute time represented as an interval from some epoch, which is determined by the clock indicated at construction (see Supported Clock-Types in the component documentation).
int bslmt::FastPostSemaphore::tryWait | ( | ) |
If this semaphore is initially disabled, return e_DISABLED
with no effect on the count. Otherwise, if the count of this semaphore is a positive value, return 0 and atomically decrement the count. If this semaphore is not disabled and the count of this semaphore is not a positive value, return e_WOULD_BLOCK
with no effect on the count.
int bslmt::FastPostSemaphore::wait | ( | ) |
If this semaphore is initially disabled, or becomes disabled while blocking, return e_DISABLED
with no effect on the count. Otherwise, block until the count of this semaphore is a positive value, return 0 and atomically decrement the count. Return e_FAILED
if an error occurs.
bsls::SystemClockType::Enum bslmt::FastPostSemaphore::clockType | ( | ) | const |
Return the clock type used for timeouts.
int bslmt::FastPostSemaphore::getDisabledState | ( | ) | const |
Return an odd value if this semaphore is wait disabled, and an even value otherwise. The returned value can be used to detect a rapid short sequence of disable
and enable
invocations by comparing the value returned by getDisabledState
before and after the sequence. For example, for any initial state of a semaphore instance obj
:
int state = obj.getDisabledState();
obj.disable();
obj.enable();
ASSERT(state != obj.getDisabledState());
This functionality is useful in higher-level components to determine if this semaphore was disabled during an operation.
int bslmt::FastPostSemaphore::getValue | ( | ) | const |
Return the current value (count > 0 ? count : 0
) of this semaphore.
bool bslmt::FastPostSemaphore::isDisabled | ( | ) | const |
Return true
if this semaphore is wait disabled, and false
otherwise. Note that the semaphore is created in the "wait enabled" state.