ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
promise.cpp
1
2#if !ALIB_SINGLE_THREADED && ALIB_DEBUG && ALIB_STRINGS && !DOXYGEN
3namespace alib::threads {
4
5void Promise::Fulfill(const CallerInfo& ci, State state) {
6 ALIB_ASSERT_ERROR( DbgFulfillCI.File == nullptr, "THREADS",
7 "Promise was already fulfilled. Repeated calls not allowed.\n"
8 " This call: {}\n"
9 " Earlier call: {}", ci, DbgFulfillCI )
10 DbgFulfillCI= ci;
11
12 promise.set_value(state);
13}
14
16 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
17 "Promise was already awaited. Repeated calls not allowed.\n"
18 " Received with: ", DbgWaitCI )
19
20 if ( !DbgWaitTimeLimit.IsZero() ) {
21 Ticks::Duration waitDuration= DbgWaitTimeLimit;
22 Ticks overallTimer;
23 Ticks waitTimer;
24 while ( future.wait_for( (waitDuration - waitTimer.Age()).Export() )
25 != std::future_status::ready )
26 {
27 if ( waitTimer.Age() < waitDuration )
28 continue; // spurious wakeup
29
30 ALIB_WARNING( "THREADS", "Waiting for a Promise since {}.", overallTimer.Age() )
31 waitTimer.Reset();
32 } }
33
34 DbgWaitCI= ci;
35 return future.get();
36}
37Promise::State Promise::WaitFor( const Ticks::Duration::TDuration& maxWaitTimeSpan,
38 const CallerInfo& ci ) {
39 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
40 "Promise was already awaited. Repeated calls not allowed.\n"
41 " Received with: ", DbgWaitCI )
42
43 if ( future.wait_for(maxWaitTimeSpan) == std::future_status::timeout )
44 return State::Unfulfilled;
45
46 DbgWaitCI= ci;
47 return future.get();
48}
49
50Promise::State Promise::WaitUntil( const Ticks& wakeUpTime, const CallerInfo& ci ) {
51 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
52 "Promise was already awaited. Repeated calls not allowed.\n"
53 " Received with: ", DbgWaitCI )
54
55 if ( future.wait_until(wakeUpTime.Export()) == std::future_status::timeout )
56 return State::Unfulfilled;
57
58 DbgWaitCI= ci;
59 return future.get();
60}
61
62} // namespace [alib::threads]
63
64#endif // !ALIB_SINGLE_THREADED
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
void Fulfill(const CallerInfo &ci, State state=State::OK)
lang::CallerInfo DbgFulfillCI
Definition promise.hpp:78
State Wait(const CallerInfo &ci)
lang::CallerInfo DbgWaitCI
Definition promise.hpp:83
Ticks::Duration DbgWaitTimeLimit
Definition promise.hpp:73
@ Unfulfilled
The state after construction.
Definition promise.hpp:52
State WaitUntil(const Ticks &wakeUpTime, const CallerInfo &ci)
std::future< State > future
Used for implementation.
Definition promise.hpp:64
State WaitFor(const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)
Promise()
Default constructor.
Definition promise.hpp:89
std::promise< State > promise
Used for implementation.
Definition promise.hpp:63
lang::CallerInfo CallerInfo
Type alias in namespace #"%alib".
time::Ticks Ticks
Type alias in namespace #"%alib".
Definition ticks.hpp:86