Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
TimeOutException.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Execution_TimeOutException_h_
5#define _Stroika_Foundation_Execution_TimeOutException_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <condition_variable> // for cv_status
10
11#include "Stroika/Foundation/Execution/Exceptions.h"
13
15
16 /**
17 * Throw this when something (typically a waitable event, but could be anything code is waiting for) times out.
18 *
19 * \note - Many low level functions map OS / platform exceptions to this type as appropriate (via @see ThrowPOSIXErrNo and @see ThrowSystemErrNo)
20 *
21 * \note Though you can use
22 * \code
23 * catch (const TimeOutException&) {
24 * ...
25 * }
26 * \endcode
27 *
28 * and that will work for catching nearly any timeout exception thrown by Stroika, it is often
29 * better to use:
30 *
31 * \code
32 * catch (const system_error& e) {
33 * if (e.code () == errc::timed_out) { // and maybe check errc::stream_timeout
34 * ...
35 * }
36 * }
37 * \endcode
38 *
39 * since that will catch timeout exceptions thrown by non-Stroika-based components, and this form
40 * will still work with Activities and UNICODE exception message handling, if you use
41 * Characters::ToString (e); alternatively you can use:
42 * catch (const SystemErrorException& e) {
43 * if (e.code () == errc::timed_out) {
44 * ...
45 * }
46 * // now directly look at String message and Activies etc...
47 * }
48 * catch (const system_error& e) {
49 * if (e.code () == errc::timed_out) {
50 * ...
51 * }
52 * }
53 *
54 */
56 public:
57 /**
58 * when not specified, the error_code defaults to make_error_code (errc::timed_out)
59 */
61 TimeOutException (error_code ec);
62 TimeOutException (const Characters::String& message);
63 TimeOutException (error_code ec, const Characters::String& message);
64
65 public:
66 /**
67 * CAN be used when there is no message argument.
68 */
69 static const TimeOutException kThe;
70 };
72
73 /**
74 * \brief Execution::Throw (Execution::TimeOutException::kThe);
75 * but can be more easily forward-declared, so no include deadly embrace
76 */
78
79 /**
80 * \brief Throw TimeOutException if the @Time::GetTickCount () is >= the given value.
81 *
82 * This function facilitates writing code like:
83 * Time::DurationSeconds timeoutAfter = Time::GetTickCount () + 1.0;
84 * do_someting_dont_know_how_long_it_will_take();
85 * Execution::ThrowTimeoutExceptionAfter (timeoutAfter);
86 *
87 * \note ***Cancelation Point***
88 */
89 template <typename EXCEPTION>
90 void ThrowTimeoutExceptionAfter (Time::TimePointSeconds afterTickCount, EXCEPTION&& exception2Throw);
92
93 /**
94 * Translate timed_mutex, or recursive_timed_mutex try_lock_until () calls which fail into TimeOutException exceptions.
95 */
96 template <typename TIMED_MUTEX, typename EXCEPTION>
97 void TryLockUntil (TIMED_MUTEX& m, Time::TimePointSeconds afterTickCount, EXCEPTION&& exception2Throw);
98 template <typename TIMED_MUTEX>
99 void TryLockUntil (TIMED_MUTEX& m, Time::TimePointSeconds afterTickCount);
100
101 /**
102 * \note - this function may not be called outside the context of a running main.
103 */
104 template <typename EXCEPTION>
105 void ThrowIfTimeout (cv_status conditionVariableStatus, EXCEPTION&& exception2Throw);
106 void ThrowIfTimeout (cv_status conditionVariableStatus);
107
108 /**
109 * Simple wrapper on construction of unique_lock<TIMED_MUTEX> - which translates the timeout into a TimeOutException.
110 *
111 * \note if this function returns (doesn't throw) - the required unique_lock<> OWNS the mutex.
112 *
113 * \see also TryLockUntil
114 * \see also TimedLockGuard
115 */
116 template <typename TIMED_MUTEX, typename EXCEPTION>
117 unique_lock<TIMED_MUTEX> UniqueLock (TIMED_MUTEX& m, const chrono::duration<double>& d, EXCEPTION&& exception2Throw);
118 template <typename TIMED_MUTEX>
119 unique_lock<TIMED_MUTEX> UniqueLock (TIMED_MUTEX& m, const chrono::duration<double>& d);
120
121}
122
123/*
124 ********************************************************************************
125 ***************************** Implementation Details ***************************
126 ********************************************************************************
127 */
128#include "TimeOutException.inl"
129
130#endif /*_Stroika_Foundation_Execution_TimeOutException_h_*/
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
Definition Realtime.h:82
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
unique_lock< TIMED_MUTEX > UniqueLock(TIMED_MUTEX &m, const chrono::duration< double > &d, EXCEPTION &&exception2Throw)
void ThrowTimeOutException()
Execution::Throw (Execution::TimeOutException::kThe); but can be more easily forward-declared,...
void ThrowTimeoutExceptionAfter(Time::TimePointSeconds afterTickCount, EXCEPTION &&exception2Throw)
Throw TimeOutException if the @Time::GetTickCount () is >= the given value.
void ThrowIfTimeout(cv_status conditionVariableStatus, EXCEPTION &&exception2Throw)
void TryLockUntil(TIMED_MUTEX &m, Time::TimePointSeconds afterTickCount, EXCEPTION &&exception2Throw)