-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_mutex.cppm
More file actions
182 lines (145 loc) · 4.47 KB
/
Copy pathasync_mutex.cppm
File metadata and controls
182 lines (145 loc) · 4.47 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
export module mcpplibs.cmp:async_mutex;
import std;
import :when_all;
export namespace mcpplibs::cmp {
class AsyncMutex final {
public:
class LockOperation;
class [[nodiscard]] Guard final {
private:
friend class AsyncMutex;
friend class LockOperation;
AsyncMutex* mutex_ {};
explicit Guard(AsyncMutex& mutex) noexcept
: mutex_ { &mutex } {}
public:
Guard(const Guard&) = delete;
Guard& operator=(const Guard&) = delete;
Guard(Guard&& other) noexcept
: mutex_ { std::exchange(other.mutex_, nullptr) } {}
Guard& operator=(Guard&&) = delete;
~Guard() {
if (mutex_) {
mutex_->unlock_();
}
}
};
class LockOperation final {
private:
friend class AsyncMutex;
AsyncMutex* mutex_ {};
LockOperation* next_ {};
LockOperation* dispatchNext_ {};
std::coroutine_handle<> continuation_ {};
explicit LockOperation(AsyncMutex& mutex) noexcept
: mutex_ { &mutex } {}
public:
LockOperation(const LockOperation&) = delete;
LockOperation& operator=(const LockOperation&) = delete;
LockOperation(LockOperation&&) = delete;
LockOperation& operator=(LockOperation&&) = delete;
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
[[nodiscard]] bool await_suspend(
std::coroutine_handle<> continuation) noexcept {
// 入队后 Guard 可能立即交接所有权并销毁当前 operation
auto* const mutex = mutex_;
continuation_ = continuation;
return mutex->acquire_or_enqueue_(*this);
}
[[nodiscard]] Guard await_resume() const noexcept {
return Guard { *mutex_ };
}
};
private:
std::mutex stateMutex_ {};
bool locked_ { false };
LockOperation* head_ {};
LockOperation* tail_ {};
inline static thread_local bool dispatching_ { false };
inline static thread_local LockOperation* dispatchHead_ {};
inline static thread_local LockOperation* dispatchTail_ {};
[[nodiscard]] bool acquire_or_enqueue_(LockOperation& operation) noexcept;
void unlock_() noexcept;
static void dispatch_(LockOperation& operation) noexcept;
public:
AsyncMutex() = default;
AsyncMutex(const AsyncMutex&) = delete;
AsyncMutex& operator=(const AsyncMutex&) = delete;
AsyncMutex(AsyncMutex&&) = delete;
AsyncMutex& operator=(AsyncMutex&&) = delete;
~AsyncMutex();
[[nodiscard]] LockOperation lock_async() noexcept {
return LockOperation { *this };
}
};
inline AsyncMutex::~AsyncMutex() {
const std::lock_guard lock { stateMutex_ };
if (locked_ || head_ != nullptr || tail_ != nullptr) {
std::terminate();
}
}
inline bool AsyncMutex::acquire_or_enqueue_(
LockOperation& operation) noexcept {
const std::lock_guard lock { stateMutex_ };
if (!locked_) {
locked_ = true;
return false;
}
operation.next_ = nullptr;
if (tail_) {
tail_->next_ = &operation;
} else {
head_ = &operation;
}
tail_ = &operation;
return true;
}
inline void AsyncMutex::unlock_() noexcept {
LockOperation* next {};
{
const std::lock_guard lock { stateMutex_ };
if (!locked_) {
std::terminate();
}
next = head_;
if (next) {
head_ = next->next_;
if (!head_) {
tail_ = nullptr;
}
} else {
locked_ = false;
}
}
if (next) {
dispatch_(*next);
}
}
inline void AsyncMutex::dispatch_(LockOperation& operation) noexcept {
operation.dispatchNext_ = nullptr;
if (dispatchTail_) {
dispatchTail_->dispatchNext_ = &operation;
} else {
dispatchHead_ = &operation;
}
dispatchTail_ = &operation;
if (dispatching_) {
return;
}
dispatching_ = true;
while (dispatchHead_) {
auto* const current = dispatchHead_;
dispatchHead_ = current->dispatchNext_;
if (!dispatchHead_) {
dispatchTail_ = nullptr;
}
// 恢复后 operation 可能销毁,循环不再访问 current
const auto continuation = current->continuation_;
detail::JoinStartQueue::Scope startScope {};
continuation.resume();
}
dispatching_ = false;
}
} // namespace mcpplibs::cmp