YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
shared_mutex.cpp
Go to the documentation of this file.
1
#include <
fault/util.hpp
>
2
3
#include <
yaclib/fault/detail/fiber/shared_mutex.hpp
>
4
5
namespace
yaclib::detail::fiber
{
6
7
void
fiber::SharedMutex::lock
() {
8
if
(_occupied) {
9
_exclusive_queue.Wait(
NoTimeoutTag
{});
10
}
11
LockHelper();
12
}
13
14
bool
SharedMutex::try_lock
()
noexcept
{
15
if
(
_occupied
) {
16
return
false
;
17
}
18
LockHelper
();
19
return
true
;
20
}
21
22
void
SharedMutex::unlock
()
noexcept
{
23
const
bool
unlock_shared
= !
_shared_queue
.
Empty
() && (
_exclusive_queue
.
Empty
() ||
GetRandNumber
(2) == 0);
24
_occupied
=
false
;
25
if
(
unlock_shared
) {
26
_shared_queue
.
NotifyAll
();
27
}
else
{
28
_exclusive_queue
.
NotifyOne
();
29
}
30
}
31
32
void
SharedMutex::lock_shared
() {
33
if
(
_occupied
&&
_exclusive_mode
) {
34
_exclusive_queue
.
Wait
(
NoTimeoutTag
{});
35
}
36
SharedLockHelper
();
37
}
38
39
bool
SharedMutex::try_lock_shared
() {
40
if
(
_occupied
&&
_exclusive_mode
) {
41
return
false
;
42
}
43
SharedLockHelper
();
44
return
true
;
45
}
46
47
void
SharedMutex::unlock_shared
() {
48
_shared_owners_count
--;
49
if
(
_shared_owners_count
== 0) {
50
_occupied
=
false
;
51
_exclusive_queue
.
NotifyOne
();
52
}
53
}
54
55
void
SharedMutex::LockHelper
() {
56
_occupied
=
true
;
57
_exclusive_mode
=
true
;
58
}
59
60
void
SharedMutex::SharedLockHelper
() {
61
_occupied
=
true
;
62
_exclusive_mode
=
false
;
63
_shared_owners_count
++;
64
}
65
66
SharedMutex::native_handle_type
SharedMutex::native_handle
() {
67
return
nullptr
;
68
}
69
70
}
// namespace yaclib::detail::fiber
yaclib::detail::fiber::FiberQueue::Empty
bool Empty() const noexcept
Definition
queue.cpp:39
yaclib::detail::fiber::FiberQueue::Wait
WaitStatus Wait(NoTimeoutTag)
Definition
queue.cpp:5
yaclib::detail::fiber::FiberQueue::NotifyAll
void NotifyAll()
Definition
queue.cpp:12
yaclib::detail::fiber::FiberQueue::NotifyOne
void NotifyOne()
Definition
queue.cpp:21
yaclib::detail::fiber::SharedMutex::lock_shared
void lock_shared()
Definition
shared_mutex.cpp:32
yaclib::detail::fiber::SharedMutex::LockHelper
void LockHelper()
Definition
shared_mutex.cpp:55
yaclib::detail::fiber::SharedMutex::unlock
void unlock() noexcept
Definition
shared_mutex.cpp:22
yaclib::detail::fiber::SharedMutex::_exclusive_mode
bool _exclusive_mode
Definition
shared_mutex.hpp:37
yaclib::detail::fiber::SharedMutex::_shared_owners_count
std::uint32_t _shared_owners_count
Definition
shared_mutex.hpp:35
yaclib::detail::fiber::SharedMutex::try_lock_shared
bool try_lock_shared()
Definition
shared_mutex.cpp:39
yaclib::detail::fiber::SharedMutex::SharedLockHelper
void SharedLockHelper()
Definition
shared_mutex.cpp:60
yaclib::detail::fiber::SharedMutex::native_handle_type
void * native_handle_type
Definition
shared_mutex.hpp:25
yaclib::detail::fiber::SharedMutex::unlock_shared
void unlock_shared()
Definition
shared_mutex.cpp:47
yaclib::detail::fiber::SharedMutex::_shared_queue
FiberQueue _shared_queue
Definition
shared_mutex.hpp:33
yaclib::detail::fiber::SharedMutex::_occupied
bool _occupied
Definition
shared_mutex.hpp:36
yaclib::detail::fiber::SharedMutex::lock
void lock()
Definition
shared_mutex.cpp:7
yaclib::detail::fiber::SharedMutex::native_handle
native_handle_type native_handle()
Definition
shared_mutex.cpp:66
yaclib::detail::fiber::SharedMutex::_exclusive_queue
FiberQueue _exclusive_queue
Definition
shared_mutex.hpp:34
yaclib::detail::fiber::SharedMutex::try_lock
bool try_lock() noexcept
Definition
shared_mutex.cpp:14
shared_mutex.hpp
yaclib::detail::fiber
Definition
atomic.hpp:7
yaclib::detail::GetRandNumber
std::uint64_t GetRandNumber(std::uint64_t max)
Definition
util.cpp:22
yaclib::MakeContract
Contract< V, E > MakeContract()
Creates related future and promise.
Definition
contract.hpp:25
yaclib::detail::fiber::NoTimeoutTag
Definition
queue.hpp:12
util.hpp
src
fault
fiber
shared_mutex.cpp
Generated by
1.9.8