ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
promise.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
15#include "alib/alib.inl"
16#if !ALIB_SINGLE_THREADED
17# include <condition_variable>
18# include <atomic>
19#endif // !ALIB_SINGLE_THREADED
20#include <unordered_map>
21#include <future>
22//============================================== Module ============================================
23#if ALIB_C20_MODULES
24 module ALib.Threads;
25 import ALib.Lang;
26# if ALIB_STRINGS
27 import ALib.Strings;
28# endif
29#else
30# include "ALib.Lang.H"
31# include "ALib.Threads.H"
32# include "ALib.Strings.H"
33#endif
34//========================================== Implementation ========================================
35
36#if !ALIB_SINGLE_THREADED && ALIB_DEBUG && ALIB_STRINGS && !DOXYGEN
37namespace alib::threads {
38
39void Promise::Fulfill(const CallerInfo& ci, State state) {
40 ALIB_ASSERT_ERROR( DbgFulfillCI.File == nullptr, "THREADS",
41 "Promise was already fulfilled. Repeated calls not allowed.\n"
42 " This call: {}\n"
43 " Earlier call: {}", ci, DbgFulfillCI )
44 DbgFulfillCI= ci;
45
46 promise.set_value(state);
47}
48
50 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
51 "Promise was already awaited. Repeated calls not allowed.\n"
52 " Received with: ", DbgWaitCI )
53
54 if ( !DbgWaitTimeLimit.IsZero() ) {
55 Ticks::Duration waitDuration= DbgWaitTimeLimit;
56 Ticks overallTimer;
57 Ticks waitTimer;
58 while ( future.wait_for( (waitDuration - waitTimer.Age()).Export() )
59 != std::future_status::ready )
60 {
61 if ( waitTimer.Age() < waitDuration )
62 continue; // spurious wakeup
63
64 ALIB_WARNING( "THREADS", "Waiting for a Promise since {}.", overallTimer.Age() )
65 waitTimer.Reset();
66 } }
67
68 DbgWaitCI= ci;
69 return future.get();
70}
71Promise::State Promise::WaitFor( const Ticks::Duration::TDuration& maxWaitTimeSpan,
72 const CallerInfo& ci ) {
73 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
74 "Promise was already awaited. Repeated calls not allowed.\n"
75 " Received with: ", DbgWaitCI )
76
77 if ( future.wait_for(maxWaitTimeSpan) == std::future_status::timeout )
78 return State::Unfulfilled;
79
80 DbgWaitCI= ci;
81 return future.get();
82}
83
84Promise::State Promise::WaitUntil( const Ticks& wakeUpTime, const CallerInfo& ci ) {
85 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
86 "Promise was already awaited. Repeated calls not allowed.\n"
87 " Received with: ", DbgWaitCI )
88
89 if ( future.wait_until(wakeUpTime.Export()) == std::future_status::timeout )
90 return State::Unfulfilled;
91
92 DbgWaitCI= ci;
93 return future.get();
94}
95
96} // namespace [alib::threads]
97
98#endif // !ALIB_SINGLE_THREADED
void Fulfill(const CallerInfo &ci, State state=State::OK)
lang::CallerInfo DbgFulfillCI
Definition promise.inl:80
State Wait(const CallerInfo &ci)
lang::CallerInfo DbgWaitCI
Definition promise.inl:85
Ticks::Duration DbgWaitTimeLimit
Definition promise.inl:75
@ Unfulfilled
The state after construction.
Definition promise.inl:54
State WaitUntil(const Ticks &wakeUpTime, const CallerInfo &ci)
std::future< State > future
Used for implementation.
Definition promise.inl:66
State WaitFor(const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)
Promise()
Default constructor. Sets the state to State::Unfulfilled.
Definition promise.inl:91
std::promise< State > promise
Used for implementation.
Definition promise.inl:65
#define ALIB_WARNING(domain,...)
Definition alib.inl:1063
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
lang::CallerInfo CallerInfo
Type alias in namespace alib.
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:79