cortex 0.0.1
Loading...
Searching...
No Matches
mutex.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <deque>
6
12namespace cortex::tiny_fiber {
13
21class Mutex {
22public:
27 class Guard {
28 public:
29 explicit Guard(Mutex& mutex);
31
32 Guard(const Guard&) = delete;
33 Guard& operator=(const Guard&) = delete;
34
35 Guard(Guard&& other) noexcept;
36 Guard& operator=(Guard&& other) noexcept;
37
38 private:
39 friend class ConditionVariable;
40
41 Mutex* mutex_ {nullptr};
42 };
43
44 Mutex() = default;
46
47 Mutex(const Mutex&) = delete;
48 Mutex& operator=(const Mutex&) = delete;
49
56 void Lock();
57
63 bool TryLock();
64
70 void Unlock();
71
76 return locked_;
77 }
78
79private:
80 friend class ConditionVariable;
81
82 bool locked_ {false};
83 detail::Fiber* owner_ {nullptr};
84 std::deque<detail::Fiber*> waiters_;
85};
86
94 return Mutex::Guard(mutex);
95}
96
97} // namespace cortex::tiny_fiber
A cooperative condition variable.
Definition condition_variable.hpp:21
Handle to a spawned fiber that returns a value.
Definition future.hpp:46
RAII lock guard for Mutex.
Definition mutex.hpp:27
Guard(Guard &&other) noexcept
Guard & operator=(const Guard &)=delete
Guard & operator=(Guard &&other) noexcept
Guard(const Guard &)=delete
A cooperative mutex that yields instead of blocking.
Definition mutex.hpp:21
void Lock()
Lock the mutex.
bool IsLocked() const noexcept
Check if the mutex is currently locked.
Definition mutex.hpp:75
Mutex & operator=(const Mutex &)=delete
Mutex(const Mutex &)=delete
bool TryLock()
Try to lock the mutex without yielding.
void Unlock()
Unlock the mutex.
Definition fiber.hpp:29
Cooperative multitasking primitives built on cortex::Coroutine.
Definition condition_variable.hpp:13
Mutex::Guard Lock(Mutex &mutex)
Create a lock guard for the mutex.
Definition mutex.hpp:93