-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutex.cpp
More file actions
124 lines (94 loc) · 3.37 KB
/
mutex.cpp
File metadata and controls
124 lines (94 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright 2020 Michael Beckh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/// @file
#include "m3c/mutex.h"
#include "m3c/exception.h"
#include "m3c.events.h"
namespace m3c {
//
// mutex
//
_Acquires_exclusive_lock_(m_lock) _Requires_lock_not_held_(m_lock) void mutex::lock() noexcept {
AcquireSRWLockExclusive(&m_lock);
}
_Acquires_shared_lock_(m_lock) _Requires_lock_not_held_(m_lock) void mutex::lock_shared() noexcept {
AcquireSRWLockShared(&m_lock);
}
_Requires_exclusive_lock_held_(m_lock) _Releases_exclusive_lock_(m_lock) void mutex::unlock() noexcept {
ReleaseSRWLockExclusive(&m_lock);
}
_Requires_shared_lock_held_(m_lock) _Releases_shared_lock_(m_lock) void mutex::unlock_shared() noexcept {
ReleaseSRWLockShared(&m_lock);
}
//
// scoped_lock
//
_Acquires_exclusive_lock_(mtx.m_lock) _Requires_lock_not_held_(mtx.m_lock)
_Post_same_lock_(mtx.m_lock, m_mutex.m_lock) scoped_lock::scoped_lock(mutex& mtx) noexcept
: m_mutex(mtx) {
mtx.lock();
}
_Requires_shared_lock_held_(m_mutex.m_lock) _Releases_shared_lock_(m_mutex.m_lock)
scoped_lock::~scoped_lock() noexcept {
m_mutex.unlock();
}
//
// shared_lock
//
_Acquires_shared_lock_(mtx.m_lock) _Requires_lock_not_held_(mtx.m_lock)
_Post_same_lock_(mtx.m_lock, m_mutex.m_lock) shared_lock::shared_lock(mutex& mtx) noexcept
: m_mutex(mtx) {
mtx.lock_shared();
}
_Requires_shared_lock_held_(m_mutex.m_lock) _Releases_shared_lock_(m_mutex.m_lock)
shared_lock::~shared_lock() noexcept {
m_mutex.unlock_shared();
}
//
// condition_variable
//
void condition_variable::wait(scoped_lock& lock) {
if (!SleepConditionVariableSRW(&m_conditionVariable, &lock.m_mutex.m_lock, INFINITE, 0)) {
throw windows_error() + evt::condition_variable_Wait_E;
}
}
void condition_variable::wait(shared_lock& lock) {
if (!SleepConditionVariableSRW(&m_conditionVariable, &lock.m_mutex.m_lock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)) {
throw windows_error() + evt::condition_variable_Wait_E;
}
}
bool condition_variable::wait_for(scoped_lock& lock, const DWORD milliseconds) {
if (!SleepConditionVariableSRW(&m_conditionVariable, &lock.m_mutex.m_lock, milliseconds, 0)) {
if (const DWORD lastError = GetLastError(); lastError != ERROR_TIMEOUT) {
throw windows_error(lastError) + evt::condition_variable_Wait_E;
}
return false;
}
return true;
}
bool condition_variable::wait_for(shared_lock& lock, const DWORD milliseconds) {
if (!SleepConditionVariableSRW(&m_conditionVariable, &lock.m_mutex.m_lock, milliseconds, CONDITION_VARIABLE_LOCKMODE_SHARED)) {
if (const DWORD lastError = GetLastError(); lastError != ERROR_TIMEOUT) {
throw windows_error(lastError) + evt::condition_variable_Wait_E;
}
return false;
}
return true;
}
void condition_variable::notify_one() noexcept {
WakeConditionVariable(&m_conditionVariable);
}
void condition_variable::notify_all() noexcept {
WakeAllConditionVariable(&m_conditionVariable);
}
} // namespace m3c