Skip to content

Commit

Permalink
add assert and [[nodiscard]] for spinlock
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Jan 20, 2025
1 parent abaffa9 commit 929d709
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions async_simple/coro/Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#ifndef ASYNC_SIMPLE_USE_MODULES
#include <atomic>
#include <cassert>
#include <mutex>
#include "async_simple/experimental/coroutine.h"

Expand Down
9 changes: 7 additions & 2 deletions async_simple/coro/SpinLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#ifndef ASYNC_SIMPLE_CORO_SPIN_LOCK_H
#define ASYNC_SIMPLE_CORO_SPIN_LOCK_H

#include <atomic>
#ifndef ASYNC_SIMPLE_USE_MODULES
#include <cassert>
#include <mutex>
#include <thread>
#include "async_simple/coro/Lazy.h"
Expand Down Expand Up @@ -60,9 +62,12 @@ class SpinLock {
}
}

void unlock() noexcept { _locked.store(false, std::memory_order_release); }
void unlock() noexcept {
assert(_locked.load(std::memory_order_acquire) == true);
_locked.store(false, std::memory_order_release);
}

Lazy<std::unique_lock<SpinLock>> coScopedLock() {
[[nodiscard]] Lazy<std::unique_lock<SpinLock>> coScopedLock() {
co_await coLock();
co_return std::unique_lock<SpinLock>{*this, std::adopt_lock};
}
Expand Down

0 comments on commit 929d709

Please sign in to comment.