Skip to content

Commit 13beb6f

Browse files
committed
fix data race in release
1 parent 3ef778f commit 13beb6f

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/components/async_mutex/mutex_awaiter.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ MutexAwaiter::MutexAwaiter(AsyncMutexCoroImpl& event, LockGuard&& guard)
2121
MutexAwaiter::MutexAwaiter(MutexAwaiter&& o) noexcept
2222
: event(o.event), coro(o.coro) {
2323
o.coro = nullptr;
24-
if (o.guard.owns_lock()) {
25-
guard = MutexAwaiter::LockGuard(*o.guard.release(), std::adopt_lock);
24+
if (auto* p = o.guard.release(); p) {
25+
guard = MutexAwaiter::LockGuard(*p, std::adopt_lock);
2626
}
2727
}
2828

@@ -33,7 +33,10 @@ MutexAwaiter::~MutexAwaiter() {
3333
void MutexAwaiter::ReleaseLock() const {
3434
std::osyncstream(std::cout)
3535
<< *this << "[await_suspend] release guard." << std::endl;
36-
guard.unlock();
36+
37+
if (auto* p = guard.release(); p) {
38+
p->unlock();
39+
}
3740
}
3841

3942
// bool MutexAwaiter::HasLock() const {

test/components/test_async_mutex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ TEST(TestAsyncMutex, JustWorking) {
100100

101101
TEST(TestAsyncMutex, SyncIncrementInThreads) {
102102
constexpr size_t MaxCountThreads = 32;
103-
constexpr size_t CountIterations = 10;
103+
constexpr size_t CountIterations = 100;
104104

105105
TestSyncIncrement worker(MaxCountThreads, CountIterations);
106106
{

0 commit comments

Comments
 (0)