template<typename ExceptionFactory, typename Clock = typename timer<>::clock>
class seastar::basic_semaphore< ExceptionFactory, Clock >
Counted resource guard.
This is a standard computer science semaphore, adapted for futures. You can deposit units into a counter, or take them away. Taking units from the counter may wait if not enough units are available.
To support exceptional conditions, a broken() method is provided, which causes all current waiters to stop waiting, with an exceptional future returned. This allows causing all fibers that are blocked on a semaphore to continue. This is similar to POSIX's pthread_cancel()
, with wait() acting as a cancellation point.
- Template Parameters
-
ExceptionFactory | template parameter allows modifying a semaphore to throw customized exceptions on timeout/broken/aborted. It has to provide three functions: ExceptionFactory::timeout() - that returns a semaphore_timed_out exception by default, ExceptionFactory::broken() - that returns a broken_semaphore exception by default, and ExceptionFactory::aborted() - that returns a semaphore_aborted exception by default. |
|
| basic_semaphore (size_t count) noexcept(std::is_nothrow_default_constructible_v< exception_factory >) |
|
| basic_semaphore (size_t count, exception_factory &&factory) noexcept(std::is_nothrow_move_constructible_v< exception_factory >) |
|
| basic_semaphore (basic_semaphore &&other) noexcept(std::is_nothrow_move_constructible_v< exception_factory >) |
|
basic_semaphore & | operator= (basic_semaphore &&other) noexcept(std::is_nothrow_move_assignable_v< exception_factory >) |
|
future | wait (size_t nr=1) noexcept |
|
future | wait (time_point timeout, size_t nr=1) noexcept |
|
future | wait (abort_source &as, size_t nr=1) noexcept |
|
future | wait (duration timeout, size_t nr=1) noexcept |
|
void | signal (size_t nr=1) noexcept |
|
void | consume (size_t nr=1) noexcept |
| Consume the specific number of units without blocking. More...
|
|
bool | try_wait (size_t nr=1) noexcept |
|
size_t | current () const noexcept |
|
ssize_t | available_units () const noexcept |
|
size_t | waiters () const noexcept |
| Returns the current number of waiters.
|
|
void | broken () noexcept |
|
template<typename Exception > |
void | broken (const Exception &ex) noexcept |
|
void | broken (std::exception_ptr ex) noexcept |
|
void | ensure_space_for_waiters (size_t n) |
| Reserve memory for waiters so that wait() will not throw.
|
|
template<typename ExceptionFactory , typename Clock = typename timer<>::clock>
Consume the specific number of units without blocking.
Consume the specific number of units now, regardless of how many units are available in the counter, and reduces the counter by that amount of units. This operation may cause the counter to go negative.
- Parameters
-
nr | Amount of units to consume (default 1). |
template<typename ExceptionFactory , typename Clock = typename timer<>::clock>
Deposits a specified number of units into the counter.
The counter is incremented by the specified number of units. If the new counter value is sufficient to satisfy the request of one or more waiters, their futures (in FIFO order) become ready, and the value of the counter is reduced according to the amount requested.
- Parameters
-
nr | Number of units to deposit (default 1). |
template<typename ExceptionFactory , typename Clock = typename timer<>::clock>
Attempts to reduce the counter value by a specified number of units.
If sufficient units are available in the counter, and if no other fiber is waiting, then the counter is reduced. Otherwise, nothing happens. This is useful for "opportunistic" waits where useful work can happen if the counter happens to be ready, but when it is not worthwhile to wait.
- Parameters
-
nr | number of units to reduce the counter by (default 1). |
- Returns
true
if the counter had sufficient units, and was decremented.