File tree 6 files changed +23
-18
lines changed
src/components/async_mutex
6 files changed +23
-18
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ AsyncMutexCoroImpl& AsyncMutex::lock() {
19
19
}
20
20
21
21
AsyncMutex::~AsyncMutex () {
22
- assert (event .waiters .empty ());
22
+ // assert(mutex_impl .waiters.empty());
23
23
}
24
24
25
25
void AsyncMutex::unlock () {
Original file line number Diff line number Diff line change @@ -58,7 +58,8 @@ void AsyncMutexCoroImpl::ParkAwaiter(MutexAwaiter* awaiter) {
58
58
}
59
59
60
60
MutexAwaiter AsyncMutexCoroImpl::operator co_await () {
61
- return MutexAwaiter{*this , spinlock};
61
+ MutexAwaiter::LockGuard guard (spinlock);
62
+ return MutexAwaiter{*this , std::move (guard)};
62
63
}
63
64
64
65
} // namespace NComponents
Original file line number Diff line number Diff line change 17
17
18
18
namespace NComponents {
19
19
20
- struct AsyncMutexCoroImpl final {
20
+ class AsyncMutexCoroImpl final {
21
21
mutable NSync::SpinLock spinlock;
22
22
bool lock_flag{};
23
23
std::list<MutexAwaiter> waiters;
24
24
25
+ public:
26
+
25
27
AsyncMutexCoroImpl () = default ;
26
28
27
29
bool TryLock ();
Original file line number Diff line number Diff line change 13
13
14
14
namespace NComponents {
15
15
16
- MutexAwaiter::MutexAwaiter (AsyncMutexCoroImpl& event, NSync::SpinLock& guard)
17
- : event(event), guard(guard) {
18
- guard.lock ();
16
+ MutexAwaiter::MutexAwaiter (AsyncMutexCoroImpl& event, LockGuard&& guard)
17
+ : event(event), guard(std::move(guard)) {
19
18
std::osyncstream (std::cout) << *this << " create with guard." << std::endl;
20
19
}
21
20
22
21
MutexAwaiter::MutexAwaiter (MutexAwaiter&& o) noexcept
23
- : event(o.event), guard(o.guard), coro(o.coro) {
22
+ : event(o.event), coro(o.coro) {
24
23
o.coro = nullptr ;
24
+ if (o.guard .owns_lock ()) {
25
+ guard = MutexAwaiter::LockGuard (*o.guard .release (), std::adopt_lock);
26
+ }
25
27
}
26
28
27
29
MutexAwaiter::~MutexAwaiter () {
@@ -58,9 +60,7 @@ void MutexAwaiter::await_suspend(std::coroutine_handle<> coro_) noexcept {
58
60
59
61
void MutexAwaiter::await_resume () const noexcept {
60
62
std::osyncstream (std::cout)
61
- << *this
62
- << " [await_resume] call and just resume, status flag=" << event.IsSet ()
63
- << std::endl;
63
+ << *this << " [await_resume] call and just resume." << std::endl;
64
64
}
65
65
66
66
std::ostream& operator <<(std::ostream& stream, const MutexAwaiter& /* w*/ ) {
Original file line number Diff line number Diff line change 12
12
13
13
namespace NComponents {
14
14
15
- struct AsyncMutexCoroImpl ;
16
- using LockGuard = std::unique_lock<NSync::SpinLock>;
15
+ class AsyncMutexCoroImpl ;
17
16
18
17
class MutexAwaiter final {
19
- AsyncMutexCoroImpl& event;
20
- NSync::SpinLock& guard;
21
- std::coroutine_handle<> coro{};
22
-
23
18
public:
24
- MutexAwaiter (AsyncMutexCoroImpl& event, NSync::SpinLock& guard);
19
+ using LockGuard = std::unique_lock<NSync::SpinLock>;
20
+
21
+ MutexAwaiter (AsyncMutexCoroImpl& event, LockGuard&& guard);
25
22
MutexAwaiter (MutexAwaiter&& o) noexcept ;
26
23
27
24
~MutexAwaiter ();
@@ -33,6 +30,11 @@ class MutexAwaiter final {
33
30
bool await_ready () const ;
34
31
void await_suspend (std::coroutine_handle<>) noexcept ;
35
32
void await_resume () const noexcept ;
33
+
34
+ private:
35
+ AsyncMutexCoroImpl& event;
36
+ mutable LockGuard guard;
37
+ std::coroutine_handle<> coro{};
36
38
};
37
39
38
40
std::ostream& operator <<(std::ostream& stream, const MutexAwaiter& w);
Original file line number Diff line number Diff line change 12
12
13
13
namespace NComponents {
14
14
15
- struct AsyncMutexCoroImpl ;
15
+ class AsyncMutexCoroImpl ;
16
16
17
17
struct ResumableNoOwn {
18
18
struct promise_type {
You can’t perform that action at this time.
0 commit comments