rippled
|
The role of a ClosureCounter
is to assist in shutdown by letting callers wait for the completion of closures (of a specific type signature) that they previously registered.
More...
Classes | |
class | Substitute |
Public Member Functions | |
ClosureCounter ()=default | |
ClosureCounter (ClosureCounter const &)=delete | |
ClosureCounter & | operator= (ClosureCounter const &)=delete |
~ClosureCounter () | |
Destructor verifies all in-flight closures are complete. More... | |
void | join (char const *name, std::chrono::milliseconds wait, beast::Journal j) |
Returns once all counted in-flight closures are destroyed. More... | |
template<class Closure > | |
std::optional< Substitute< Closure > > | wrap (Closure &&closure) |
Wrap the passed closure with a reference counter. More... | |
int | count () const |
Current number of Closures outstanding. More... | |
bool | joined () const |
Returns true if this has been joined. More... | |
Private Member Functions | |
ClosureCounter & | operator++ () |
ClosureCounter & | operator-- () |
Private Attributes | |
std::mutex | mutex_ {} |
std::condition_variable | allClosuresDoneCond_ {} |
bool | waitForClosures_ {false} |
std::atomic< int > | closureCount_ {0} |
The role of a ClosureCounter
is to assist in shutdown by letting callers wait for the completion of closures (of a specific type signature) that they previously registered.
These closures are typically callbacks for asynchronous operations. The lifetime of a ClosureCounter
consists of two phases: the initial expanding "fork" phase, and the subsequent shrinking "join" phase.
In the fork phase, callers register a closure by passing the closure and receiving a substitute in return. The substitute has the same callable interface as the closure, and it informs the ClosureCounter
whenever it is copied or destroyed, so that it can keep an accurate count of copies.
The transition to the join phase is made by a call to join
. In this phase, every substitute returned going forward will be null, signaling to the caller that they should drop the closure and cancel their operation. join
blocks until all existing closure substitutes are destroyed.
Ret_t | The return type of the closure. |
Args_t | The argument types of the closure. |
Definition at line 54 of file ClosureCounter.h.
|
default |
|
delete |
ripple::ClosureCounter< Ret_t, Args_t >::~ClosureCounter | ( | ) |
Destructor verifies all in-flight closures are complete.
Definition at line 153 of file ClosureCounter.h.
|
private |
Definition at line 64 of file ClosureCounter.h.
|
private |
Definition at line 73 of file ClosureCounter.h.
|
delete |
void ripple::ClosureCounter< Ret_t, Args_t >::join | ( | char const * | name, |
std::chrono::milliseconds | wait, | ||
beast::Journal | j | ||
) |
Returns once all counted in-flight closures are destroyed.
name | Name reported if join time exceeds wait. |
wait | If join() exceeds this duration report to Journal. |
j | Journal written to if wait is exceeded. |
Definition at line 166 of file ClosureCounter.h.
std::optional<Substitute<Closure> > ripple::ClosureCounter< Ret_t, Args_t >::wrap | ( | Closure && | closure | ) |
Wrap the passed closure with a reference counter.
closure | Closure that accepts Args_t parameters and returns Ret_t. |
Definition at line 192 of file ClosureCounter.h.
int ripple::ClosureCounter< Ret_t, Args_t >::count | ( | ) | const |
Current number of Closures outstanding.
Only useful for testing.
Definition at line 205 of file ClosureCounter.h.
bool ripple::ClosureCounter< Ret_t, Args_t >::joined | ( | ) | const |
Returns true if this has been joined.
Even if true is returned, counted closures may still be in flight. However if (joined() && (count() == 0)) there should be no more counted closures in flight.
Definition at line 217 of file ClosureCounter.h.
|
mutableprivate |
Definition at line 57 of file ClosureCounter.h.
|
private |
Definition at line 58 of file ClosureCounter.h.
|
private |
Definition at line 59 of file ClosureCounter.h.
|
private |
Definition at line 60 of file ClosureCounter.h.