YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
guard_state.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <yaclib/log.hpp>
4
5#include <cstdint>
6
7namespace yaclib::detail {
8
9class GuardState {
10 static constexpr auto kMask = ~std::uintptr_t{1};
11
12 public:
14
16 : _state{reinterpret_cast<std::uintptr_t>(ptr) | static_cast<std::uintptr_t>(owns)} {
17 }
18
20 other._state &= kMask;
21 }
22
24 Swap(other);
25 return *this;
26 }
27
28 void Swap(GuardState& other) noexcept {
29 std::swap(_state, other._state);
30 }
31
32 void* Ptr() const {
33 return reinterpret_cast<void*>(_state & kMask);
34 }
35
37 return (_state & 1U) != 0;
38 }
39
41 YACLIB_DEBUG(Owns(), "Cannot lock locked guard");
42 _state |= 1U;
43 return Ptr();
44 }
45
47 YACLIB_DEBUG(!Owns(), "Cannot unlock not locked guard");
48 _state &= kMask;
49 return Ptr();
50 }
51
53 void* was = Ptr();
54 _state = 0;
55 return was;
56 }
57
58 protected:
59 std::uintptr_t _state = 0;
60};
61
62} // namespace yaclib::detail
void Swap(GuardState &other) noexcept
void * ReleaseState() noexcept
GuardState & operator=(GuardState &&other) noexcept
bool Owns() const noexcept
GuardState(GuardState &&other) noexcept
void * UnlockState() noexcept
GuardState() noexcept=default
void * LockState() noexcept
#define YACLIB_DEBUG(cond, message)
Definition log.hpp:84
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25