Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
TimeOutException.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include <condition_variable>
5
7
9
10 //redeclare to avoid having to #include Thread.h
11 namespace Thread {
13 }
14
15 /*
16 ********************************************************************************
17 ************************* Execution::TimeOutException **************************
18 ********************************************************************************
19 */
20 inline TimeOutException::TimeOutException (error_code ec, const Characters::String& message)
21 : SystemErrorException{ec, message}
22 {
23 }
24
25 /*
26 ********************************************************************************
27 ******************* Execution::ThrowTimeoutExceptionAfter **********************
28 ********************************************************************************
29 */
30 template <typename EXCEPTION>
31 inline void ThrowTimeoutExceptionAfter (Time::TimePointSeconds afterTickCount, EXCEPTION&& exception2Throw)
32 {
33 // note the == part important, so the case of TimeoutSeconds == 0s works as 'no blocking'
34 if (Time::GetTickCount () >= afterTickCount) [[unlikely]] {
35 Throw (forward<EXCEPTION> (exception2Throw));
36 }
37 Thread::CheckForInterruption ();
38 }
39 inline void ThrowTimeoutExceptionAfter (Time::TimePointSeconds afterTickCount)
40 {
41 ThrowTimeoutExceptionAfter (afterTickCount, TimeOutException::kThe);
42 }
43
44 /*
45 ********************************************************************************
46 ************************* Execution::TryLockUntil ******************************
47 ********************************************************************************
48 */
49 template <typename TIMED_MUTEX, typename EXCEPTION>
50 inline void TryLockUntil (TIMED_MUTEX& m, Time::TimePointSeconds afterTickCount, EXCEPTION&& exception2Throw)
51 {
52 if (not m.try_lock_until (Time::Pin2SafeSeconds (afterTickCount))) {
53 Throw (forward<EXCEPTION> (exception2Throw));
54 }
55 }
56 template <typename TIMED_MUTEX>
57 void TryLockUntil (TIMED_MUTEX& m, Time::TimePointSeconds afterTickCount)
58 {
59 TryLockUntil (m, afterTickCount, TimeOutException::kThe);
60 }
61
62 /*
63 ********************************************************************************
64 ************************* Execution::ThrowIfTimeout ****************************
65 ********************************************************************************
66 */
67 template <typename EXCEPTION>
68 inline void ThrowIfTimeout (cv_status conditionVariableStatus, EXCEPTION& exception2Throw)
69 {
70 if (conditionVariableStatus == cv_status::timeout) {
71 Throw (forward<EXCEPTION> (exception2Throw));
72 }
73 }
74 inline void ThrowIfTimeout (cv_status conditionVariableStatus)
75 {
76 ThrowIfTimeout (conditionVariableStatus, TimeOutException::kThe);
77 }
78
79 /*
80 ********************************************************************************
81 ***************************** Execution::UniqueLock ****************************
82 ********************************************************************************
83 */
84 template <typename TIMED_MUTEX, typename EXCEPTION>
85 inline unique_lock<TIMED_MUTEX> UniqueLock (TIMED_MUTEX& m, const chrono::duration<double>& d, EXCEPTION&& exception2Throw)
86 {
87 unique_lock<TIMED_MUTEX> lock{m, d};
88 if (not lock.owns_lock ()) {
89 Throw (forward<EXCEPTION> (exception2Throw));
90 }
91 return lock;
92 }
93 template <typename TIMED_MUTEX>
94 inline unique_lock<TIMED_MUTEX> UniqueLock (TIMED_MUTEX& m, const chrono::duration<double>& d)
95 {
96 return UniqueLock (m, d, TimeOutException::kThe);
97 }
98
99}
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
unique_lock< TIMED_MUTEX > UniqueLock(TIMED_MUTEX &m, const chrono::duration< double > &d, EXCEPTION &&exception2Throw)
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 Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43
void TryLockUntil(TIMED_MUTEX &m, Time::TimePointSeconds afterTickCount, EXCEPTION &&exception2Throw)