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
15 explicit GuardState(void* ptr, bool owns) noexcept : _state{reinterpret_cast<std::uintptr_t>(ptr) | owns} {
16 }
17
19 other._state &= kMask;
20 }
21
23 Swap(other);
24 return *this;
25 }
26
27 void Swap(GuardState& other) noexcept {
28 std::swap(_state, other._state);
29 }
30
31 void* Ptr() const {
32 return reinterpret_cast<void*>(_state & kMask);
33 }
34
36 return (_state & 1U) != 0;
37 }
38
40 YACLIB_DEBUG(Owns(), "Cannot lock locked guard");
41 _state |= 1U;
42 return Ptr();
43 }
44
46 YACLIB_DEBUG(!Owns(), "Cannot unlock not locked guard");
47 _state &= kMask;
48 return Ptr();
49 }
50
52 void* was = Ptr();
53 _state = 0;
54 return was;
55 }
56
57 protected:
58 std::uintptr_t _state = 0;
59};
60
61} // 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