Fibers of execution.
Seastar continuations are normally short, but often chained to one another, so that one continuation does a bit of work and then schedules another continuation for later. Such chains can be long, and often even involve loopings - see for example repeat. We call such chains "fibers" of execution.
These fibers are not threads - each is just a string of continuations - but they share some common requirements with traditional threads. For example, we want to avoid one fiber getting starved while a second fiber continuously runs its continuations one after another. As another example, fibers may want to communicate - e.g., one fiber produces data that a second fiber consumes, and we wish to ensure that both fibers get a chance to run, and that if one stops prematurely, the other doesn't hang forever.
Consult the following table to see which APIs are useful for fiber tasks:
|
template<typename ExceptionFactory , typename Clock = typename timer<>::clock> |
semaphore_units< ExceptionFactory, Clock > | seastar::consume_units (basic_semaphore< ExceptionFactory, Clock > &sem, size_t units) noexcept |
| Consume units from semaphore temporarily. More...
|
|
template<typename Func > |
std::enable_if_t<!std::is_nothrow_move_constructible_v< Func >, futurize_t< std::result_of_t< Func()> > > | seastar::with_shared (shared_mutex &sm, Func &&func) noexcept |
|
template<typename Func > |
std::enable_if_t<!std::is_nothrow_move_constructible_v< Func >, futurize_t< std::result_of_t< Func()> > > | seastar::with_lock (shared_mutex &sm, Func &&func) noexcept |
|
template<typename Func > |
auto | with_gate (gate &g, Func &&func) |
|
template<typename Func > |
auto | try_with_gate (gate &g, Func &&func) noexcept |
|
void | seastar::basic_semaphore< ExceptionFactory, Clock >::broken (std::exception_ptr ex) noexcept |
|
template<typename Func > |
std::enable_if_t< std::is_nothrow_move_constructible_v< Func >, futurize_t< std::result_of_t< Func()> > > | with_shared (shared_mutex &sm, Func &&func) noexcept |
|
template<typename Func > |
std::enable_if_t< std::is_nothrow_move_constructible_v< Func >, futurize_t< std::result_of_t< Func()> > > | with_lock (shared_mutex &sm, Func &&func) noexcept |
|
◆ semaphore
default basic_semaphore specialization that throws semaphore specific exceptions on error conditions.
◆ broken()
template<typename ExceptionFactory , typename Clock >
Signal to waiters that an error occurred. wait() will see an exceptional future<> containing the provided exception parameter. The future is made available immediately.
◆ consume_units()
template<typename ExceptionFactory , typename Clock = typename timer<>::clock>
Consume units from semaphore temporarily.
Consume units from the semaphore and returns them when the semaphore_units object goes out of scope. This provides a safe way to temporarily take units from a semaphore and ensure that they are eventually returned under all circumstances (exceptions, premature scope exits, etc).
Unlike get_units(), this calls the non-blocking consume() API.
Unlike with_semaphore(), the scope of unit holding is not limited to the scope of a single async lambda.
- Parameters
-
sem | The semaphore to take units from |
units | Number of units to consume |
◆ try_with_gate()
template<typename Func >
auto try_with_gate |
( |
gate & |
g, |
|
|
Func && |
func |
|
) |
| |
|
related |
Executes the function func
if the gate g
can be entered and later on, properly left.
- Parameters
-
func | function to be executed |
g | the gate. Caller must make sure that it outlives this function. |
If the gate is already closed, an exception future holding gate_closed_exception is returned, otherwise
- Returns
- whatever
func
returns.
◆ with_gate()
template<typename Func >
auto with_gate |
( |
gate & |
g, |
|
|
Func && |
func |
|
) |
| |
|
related |
Executes the function func
making sure the gate g
is properly entered and later on, properly left.
- Parameters
-
func | function to be executed |
g | the gate. Caller must make sure that it outlives this function. |
- Returns
- whatever
func
returns
◆ with_lock()
template<typename Func >
std::enable_if_t< std::is_nothrow_move_constructible_v< Func >, futurize_t< std::result_of_t< Func()> > > with_lock |
( |
shared_mutex & |
sm, |
|
|
Func && |
func |
|
) |
| |
|
related |
Executes a function while holding exclusive access to a resource.
Executes a function while holding exclusive access to a resource. When the function returns, the mutex is automatically unlocked.
- Parameters
-
sm | a shared_mutex guarding access to the shared resource |
func | callable object to invoke while the mutex is held for shared access |
- Returns
- whatever
func
returns, as a future
◆ with_shared()
template<typename Func >
std::enable_if_t< std::is_nothrow_move_constructible_v< Func >, futurize_t< std::result_of_t< Func()> > > with_shared |
( |
shared_mutex & |
sm, |
|
|
Func && |
func |
|
) |
| |
|
related |
Executes a function while holding shared access to a resource.
Executes a function while holding shared access to a resource. When the function returns, the mutex is automatically unlocked.
- Parameters
-
sm | a shared_mutex guarding access to the shared resource |
func | callable object to invoke while the mutex is held for shared access |
- Returns
- whatever
func
returns, as a future