Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
TimedLock.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Execution_TimedLock_h_
5#define _Stroika_Foundation_Execution_TimedLock_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <chrono>
10#include <mutex>
11
13#include "Stroika/Foundation/Execution/Timeout.h"
15
16/**
17 * \file
18 *
19 * Acquiring a lock with a DEADLINE, where failing to get it in time is an error rather than
20 * something the caller polls for. Both helpers here report that failure the one way Stroika
21 * reports any timeout - @see Execution/Timeout.h - so the caller writes straight-line code and
22 * handles the timeout with a catch rather than a branch.
23 *
24 * For a lock with no deadline, just use std::lock_guard / std::unique_lock directly.
25 */
26
28
29 /**
30 * Translate timed_mutex, or recursive_timed_mutex try_lock_until () calls which fail into a timeout
31 * exception (@see ThrowError (errc::timed_out)).
32 */
33 template <Common::StdCompat::TimedMutex TIMED_MUTEX, typename EXCEPTION>
34 void TryLockUntil (TIMED_MUTEX& m, Time::TimePointSeconds afterTickCount, EXCEPTION&& exception2Throw);
35 template <Common::StdCompat::TimedMutex TIMED_MUTEX>
36 void TryLockUntil (TIMED_MUTEX& m, Time::TimePointSeconds afterTickCount);
37
38 /**
39 * Simple wrapper on construction of unique_lock<TIMED_MUTEX> - which translates the timeout into a
40 * timeout exception (@see ThrowError (errc::timed_out)).
41 *
42 * \note if this function returns (doesn't throw) - the required unique_lock<> OWNS the mutex.
43 *
44 * \note ALIAS - TimedLockGuard. If you are hunting for a "timed lock guard" - a scope-bound guard over
45 * a timed_mutex that throws rather than quietly failing to lock - UniqueLock is that thing under
46 * a less obvious name. Execution::TimedLockGuard existed until Stroika v3.0d25, and was removed
47 * because unique_lock<> dominates any lock_guard analogue here: it is movable, returnable, can
48 * be released early, and is the only form condition_variable accepts.
49 *
50 * \see also TryLockUntil
51 */
52 template <Common::StdCompat::TimedMutex TIMED_MUTEX, typename EXCEPTION>
53 unique_lock<TIMED_MUTEX> UniqueLock (TIMED_MUTEX& m, const chrono::duration<double>& d, EXCEPTION&& exception2Throw);
54 template <Common::StdCompat::TimedMutex TIMED_MUTEX>
55 unique_lock<TIMED_MUTEX> UniqueLock (TIMED_MUTEX& m, const chrono::duration<double>& d);
56
57}
58
59/*
60 ********************************************************************************
61 ***************************** Implementation Details ***************************
62 ********************************************************************************
63 */
64#include "TimedLock.inl"
65
66#endif /*_Stroika_Foundation_Execution_TimedLock_h_*/
unique_lock< TIMED_MUTEX > UniqueLock(TIMED_MUTEX &m, const chrono::duration< double > &d, EXCEPTION &&exception2Throw)
Definition TimedLock.inl:34
void TryLockUntil(TIMED_MUTEX &m, Time::TimePointSeconds afterTickCount, EXCEPTION &&exception2Throw)
Definition TimedLock.inl:14