|
|
@@ -26,45 +26,48 @@ RESUMEF_NS |
|
|
|
struct [[nodiscard]] manual_awaiter; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在协程中加锁。 |
|
|
|
* @brief 在协程中加锁,如果不能立即获得锁,则阻塞当前协程。但不会阻塞当前线程。 |
|
|
|
* @return [co_await] scoped_unlock_t |
|
|
|
*/ |
|
|
|
awaiter/*scoped_unlock_t*/ lock() const noexcept; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 等同调用co_await lock()。 |
|
|
|
* @brief 等同调用 co_await lock()。 |
|
|
|
* @return [co_await] scoped_unlock_t |
|
|
|
*/ |
|
|
|
awaiter/*scoped_unlock_t*/ operator co_await() const noexcept; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在协程中加锁。需要随后调用unlock()函数解锁。lock()/unlock()调用必须在同一个跟协程下配对调用。 |
|
|
|
* @brief 在协程中加锁,如果不能立即获得锁,则阻塞当前协程。但不会阻塞当前线程。 |
|
|
|
* 需要随后调用unlock()函数解锁。lock()/unlock()调用必须在同一个跟协程下配对调用。 |
|
|
|
* @return [co_await] void |
|
|
|
*/ |
|
|
|
manual_awaiter/*void*/ lock(adopt_manual_unlock_t) const noexcept; |
|
|
|
|
|
|
|
|
|
|
|
struct [[nodiscard]] try_awaiter; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 尝试在协程中加锁。此操作无论成功与否都会立即返回。 |
|
|
|
* @brief 尝试在协程中加锁。此操作无论成功与否都会立即返回,不会有协程切换。 |
|
|
|
* 如果加锁成功,则需要调用co_await unlock()解锁。或者使用unlock(root_state())解锁。 |
|
|
|
* 如果加锁失败,且要循环尝试加锁,则最好调用co_await yield()让出一次调度。否则,可能造成本调度器死循环。 |
|
|
|
* @return [co_await] bool |
|
|
|
*/ |
|
|
|
try_awaiter/*bool*/ try_lock() const noexcept; |
|
|
|
|
|
|
|
struct [[nodiscard]] unlock_awaiter; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在协程中解锁。此操作立即返回。 |
|
|
|
* @brief 在协程中解锁。此操作立即返回,不会有协程切换。 |
|
|
|
* @return [co_await] void |
|
|
|
*/ |
|
|
|
struct [[nodiscard]] unlock_awaiter; |
|
|
|
unlock_awaiter/*void*/ unlock() const noexcept; |
|
|
|
|
|
|
|
|
|
|
|
struct [[nodiscard]] timeout_awaiter; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在协程中尝试加锁,直到超时 |
|
|
|
* @brief 在协程中尝试加锁,直到超时。如果不能立即获得锁,则阻塞当前协程。但不会阻塞当前线程。 |
|
|
|
* @param dt 超时时长 |
|
|
|
* @return [co_await] bool |
|
|
|
*/ |
|
|
@@ -72,7 +75,7 @@ RESUMEF_NS |
|
|
|
timeout_awaiter/*bool*/ try_lock_for(const std::chrono::duration<_Rep, _Period>& dt) const noexcept; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在协程中尝试加锁,直到超时 |
|
|
|
* @brief 在协程中尝试加锁,直到超时。如果不能立即获得锁,则阻塞当前协程。但不会阻塞当前线程。 |
|
|
|
* @param tp 超时时刻 |
|
|
|
* @return [co_await] bool |
|
|
|
*/ |
|
|
@@ -80,17 +83,44 @@ RESUMEF_NS |
|
|
|
timeout_awaiter/*bool*/ try_lock_until(const std::chrono::time_point<_Rep, _Period>& tp) const noexcept; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在非协程中加锁。如果不能立即获得锁,则反复尝试,直到获得锁。故会阻塞当前协程 |
|
|
|
* @param unique_address 代表获得锁的拥有者。此地址应当与随后的unlock()的地址一致。 |
|
|
|
* 一般做法,是申明一个跟当前线程关联的局部变量,以此局部变量的地址为参数。 |
|
|
|
*/ |
|
|
|
void lock(void* unique_address) const; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 尝试在非协程中加锁。此操作无论成功与否都会立即返回。 |
|
|
|
* @param unique_address 代表获得锁的拥有者。 |
|
|
|
*/ |
|
|
|
bool try_lock(void* unique_address) const; |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 尝试在非协程中加锁,直到超时。如果不能立即获得锁,则反复尝试,直到获得锁或超时。故会阻塞当前协程 |
|
|
|
* @param dt 超时时长 |
|
|
|
* @param unique_address 代表获得锁的拥有者。 |
|
|
|
*/ |
|
|
|
template <class _Rep, class _Period> |
|
|
|
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& dt, void* unique_address); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 尝试在非协程中加锁,直到超时。如果不能立即获得锁,则反复尝试,直到获得锁或超时。故会阻塞当前协程 |
|
|
|
* @param tp 超时时刻 |
|
|
|
* @param unique_address 代表获得锁的拥有者。 |
|
|
|
*/ |
|
|
|
template <class _Rep, class _Period> |
|
|
|
bool try_lock_until(const std::chrono::time_point<_Rep, _Period>& tp, void* unique_address); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在非协程中解锁。立即返回。 |
|
|
|
* @param unique_address 代表获得锁的拥有者。 |
|
|
|
*/ |
|
|
|
void unlock(void* unique_address) const; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在协程中,无死锁的批量加锁。捕获阻塞当前线程。直到获得所有锁之前,阻塞当前协程。 |
|
|
|
* @brief 在协程中,无死锁的批量加锁。不会阻塞当前线程。直到获得所有锁之前,会阻塞当前协程。 |
|
|
|
* @return [co_await] scoped_unlock_t |
|
|
|
*/ |
|
|
|
template<class... _Mtxs |
|
|
@@ -117,7 +147,7 @@ RESUMEF_NS |
|
|
|
static void lock(adopt_manual_unlock_t, void* unique_address, _Mtxs&... mtxs); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 在非协程中,批量解锁加锁。立即返回。 |
|
|
|
* @brief 在非协程中,批量解锁。立即返回。 |
|
|
|
*/ |
|
|
|
template<class... _Mtxs |
|
|
|
, typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>> |