Skip to content

Commit 3ef778f

Browse files
committed
right release lock
1 parent 9dd476d commit 3ef778f

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

src/components/async_mutex/async_mutex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ AsyncMutexCoroImpl& AsyncMutex::lock() {
1919
}
2020

2121
AsyncMutex::~AsyncMutex() {
22-
assert(event.waiters.empty());
22+
// assert(mutex_impl.waiters.empty());
2323
}
2424

2525
void AsyncMutex::unlock() {

src/components/async_mutex/async_mutex_coro_impl.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ void AsyncMutexCoroImpl::ParkAwaiter(MutexAwaiter* awaiter) {
5858
}
5959

6060
MutexAwaiter AsyncMutexCoroImpl::operator co_await() {
61-
return MutexAwaiter{*this, spinlock};
61+
MutexAwaiter::LockGuard guard(spinlock);
62+
return MutexAwaiter{*this, std::move(guard)};
6263
}
6364

6465
} // namespace NComponents

src/components/async_mutex/async_mutex_coro_impl.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
namespace NComponents {
1919

20-
struct AsyncMutexCoroImpl final {
20+
class AsyncMutexCoroImpl final {
2121
mutable NSync::SpinLock spinlock;
2222
bool lock_flag{};
2323
std::list<MutexAwaiter> waiters;
2424

25+
public:
26+
2527
AsyncMutexCoroImpl() = default;
2628

2729
bool TryLock();

src/components/async_mutex/mutex_awaiter.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313

1414
namespace NComponents {
1515

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)) {
1918
std::osyncstream(std::cout) << *this << " create with guard." << std::endl;
2019
}
2120

2221
MutexAwaiter::MutexAwaiter(MutexAwaiter&& o) noexcept
23-
: event(o.event), guard(o.guard), coro(o.coro) {
22+
: event(o.event), coro(o.coro) {
2423
o.coro = nullptr;
24+
if (o.guard.owns_lock()) {
25+
guard = MutexAwaiter::LockGuard(*o.guard.release(), std::adopt_lock);
26+
}
2527
}
2628

2729
MutexAwaiter::~MutexAwaiter() {
@@ -58,9 +60,7 @@ void MutexAwaiter::await_suspend(std::coroutine_handle<> coro_) noexcept {
5860

5961
void MutexAwaiter::await_resume() const noexcept {
6062
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;
6464
}
6565

6666
std::ostream& operator<<(std::ostream& stream, const MutexAwaiter& /*w*/) {

src/components/async_mutex/mutex_awaiter.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212

1313
namespace NComponents {
1414

15-
struct AsyncMutexCoroImpl;
16-
using LockGuard = std::unique_lock<NSync::SpinLock>;
15+
class AsyncMutexCoroImpl;
1716

1817
class MutexAwaiter final {
19-
AsyncMutexCoroImpl& event;
20-
NSync::SpinLock& guard;
21-
std::coroutine_handle<> coro{};
22-
2318
public:
24-
MutexAwaiter(AsyncMutexCoroImpl& event, NSync::SpinLock& guard);
19+
using LockGuard = std::unique_lock<NSync::SpinLock>;
20+
21+
MutexAwaiter(AsyncMutexCoroImpl& event, LockGuard&& guard);
2522
MutexAwaiter(MutexAwaiter&& o) noexcept;
2623

2724
~MutexAwaiter();
@@ -33,6 +30,11 @@ class MutexAwaiter final {
3330
bool await_ready() const;
3431
void await_suspend(std::coroutine_handle<>) noexcept;
3532
void await_resume() const noexcept;
33+
34+
private:
35+
AsyncMutexCoroImpl& event;
36+
mutable LockGuard guard;
37+
std::coroutine_handle<> coro{};
3638
};
3739

3840
std::ostream& operator<<(std::ostream& stream, const MutexAwaiter& w);

src/components/async_mutex/resumable_no_own.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace NComponents {
1414

15-
struct AsyncMutexCoroImpl;
15+
class AsyncMutexCoroImpl;
1616

1717
struct ResumableNoOwn {
1818
struct promise_type {

0 commit comments

Comments
 (0)