ALib C++ Library
Library Version: 2510 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{
41 ALIB_ASSERT_ERROR( DbgFulfillCI.File == nullptr, "THREADS",
42 "Promise was already fulfilled. Repeated calls not allowed.\n"
43 " This call: {}\n"
44 " Earlier call: {}", ci, DbgFulfillCI )
45 DbgFulfillCI= ci;
46
47 promise.set_value(state);
48}
49
51{
52 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
53 "Promise was already awaited. Repeated calls not allowed.\n"
54 " Received with: ", DbgWaitCI )
55
56 if ( !DbgWaitTimeLimit.IsZero() )
57 {
58 Ticks::Duration waitDuration= DbgWaitTimeLimit;
59 Ticks overallTimer;
60 Ticks waitTimer;
61 while ( future.wait_for( (waitDuration - waitTimer.Age()).Export() )
62 != std::future_status::ready )
63 {
64 if ( waitTimer.Age() < waitDuration )
65 continue; // spurious wakeup
66
67 ALIB_WARNING( "THREADS", "Waiting for a Promise since {}.", overallTimer.Age() )
68 waitTimer.Reset();
69 }
70 }
71
72 DbgWaitCI= ci;
73 return future.get();
74}
75Promise::State Promise::WaitFor( const Ticks::Duration::TDuration& maxWaitTimeSpan, const CallerInfo& ci )
76{
77 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
78 "Promise was already awaited. Repeated calls not allowed.\n"
79 " Received with: ", DbgWaitCI )
80
81 if ( future.wait_for(maxWaitTimeSpan) == std::future_status::timeout )
82 return State::Unfulfilled;
83
84 DbgWaitCI= ci;
85 return future.get();
86}
87
88Promise::State Promise::WaitUntil( const Ticks& wakeUpTime, const CallerInfo& ci )
89{
90 ALIB_ASSERT_ERROR( DbgWaitCI.File == nullptr, "THREADS",
91 "Promise was already awaited. Repeated calls not allowed.\n"
92 " Received with: ", DbgWaitCI )
93
94 if ( future.wait_until(wakeUpTime.Export()) == std::future_status::timeout )
95 return State::Unfulfilled;
96
97 DbgWaitCI= ci;
98 return future.get();
99}
100
101} // namespace [alib::threads]
102
103#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:1046
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
lang::CallerInfo CallerInfo
Type alias in namespace alib.
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:109