YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
mutex_awaiter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <yaclib/config.hpp>
5
6#include <mutex>
7
8namespace yaclib::detail {
9
10template <typename M, bool Shared = false>
12 public:
13 explicit LockAwaiter(M& m) noexcept : _mutex{m} {
14 }
15
16 YACLIB_INLINE bool await_ready() noexcept {
17 if constexpr (Shared) {
18 return _mutex.TryLockSharedAwait();
19 } else {
20 return _mutex.TryLockAwait();
21 }
22 }
23
24 template <typename Promise>
25 YACLIB_INLINE bool await_suspend(yaclib_std::coroutine_handle<Promise> handle) noexcept {
26 if constexpr (Shared) {
27 return _mutex.AwaitLockShared(handle.promise());
28 } else {
29 return _mutex.AwaitLock(handle.promise());
30 }
31 }
32
33 constexpr void await_resume() noexcept {
34 }
35
36 protected:
38};
39
40template <template <typename> typename Guard, typename M, bool Shared = false>
41class [[nodiscard]] GuardAwaiter : public LockAwaiter<typename M::Base, Shared> {
42 public:
43 using LockAwaiter<typename M::Base, Shared>::LockAwaiter;
44
45 YACLIB_INLINE auto await_resume() noexcept {
46 return Guard<M>{M::template Cast<M>(this->_mutex), std::adopt_lock};
47 }
48};
49
50} // namespace yaclib::detail
YACLIB_INLINE auto await_resume() noexcept
constexpr void await_resume() noexcept
YACLIB_INLINE bool await_suspend(yaclib_std::coroutine_handle< Promise > handle) noexcept
YACLIB_INLINE bool await_ready() noexcept
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25