libcoro
1.0
Coroutine support library for C++20
|
Mutex which allows locking across co_await and co_yield suspend points. More...
#include <mutex.h>
Classes | |
class | ownership |
tracks ownership More... | |
Public Member Functions | |
ownership | try_lock () |
try to lock More... | |
future< ownership > | lock () |
lock the mutex, retrieve future ownership More... | |
future< ownership > | operator co_await () |
lock the mutex co_awaitable More... | |
ownership | lock_sync () |
lock synchronously More... | |
Protected Member Functions | |
bool | try_acquire () |
tries to acquire More... | |
promise< ownership >::notify | do_lock (future< ownership > *fut_awt) |
initiate lock operation More... | |
void | build_queue (awaiter *r, awaiter *stop) |
builds internal queue More... | |
promise< ownership >::notify | unlock () |
unlock the lock More... | |
ownership | make_ownership () |
creates ownership object More... | |
Static Protected Member Functions | |
static awaiter * | locked () |
generates special pointer, which is used as locked flag (value 0x00000001) More... | |
Protected Attributes | |
std::atomic< awaiter * > | _req = {nullptr} |
awaiter * | _que = nullptr |
contains queue of requests already registered by the lock More... | |
Mutex which allows locking across co_await and co_yield suspend points.
This object can be used to hold exclusive access to a resource while the coroutine is suspended on co_await on co_yield. Standard mutex can't support such feature. This mutex is also co_awaitable.
The object suports lock and try_lock. However the lock protocol is different. When lock success, you receive an ownership object, which must be held to keep exclusive access. Once the ownership is released, the mutex is unlocked. The ownership object uses RAII to track to mutex ownership. So there is no explicit unlock() function.
The mutex object support co_await, lock_sync()
Attempt to lock the mutex always return a future with ownership.