|
Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
|
A fiber blocking mutex. More...
Go to the source code of this file.
Data Structures | |
| struct | srn_fiber_mutex_t |
Typedefs | |
| typedef struct srn_fiber_mutex_t | srn_fiber_mutex_t |
Functions | |
| void | srn_fiber_mutex_init (srn_fiber_mutex_t *m) |
| Initialise m to the unlocked state. | |
| void | srn_fiber_mutex_lock (srn_fiber_mutex_t *m) |
| Acquire m. | |
| void | srn_fiber_mutex_unlock (srn_fiber_mutex_t *m) |
| Release m. | |
A fiber blocking mutex.
This lock suspends the calling fiber while the lock is held elsewhere and lets the worker run other fibers meanwhile. It is therefore only valid to call from INSIDE A RUNNING FIBER. Calling it from the worker thread, a signal handler, or any other non-fiber context is an error, since there would be no fiber to suspend.
The lock is not recursive, a fiber that locks a mutex it already holds deadlocks against itself. state carries no owner identity to detect it.
Waiters are served in FIFO order, so no waiter starves under sustained contention. We achieve that by having the state of a like to act as a stack and unlucky fibers add themselves to the stack and use srn_fiber_t.link to point to the next fiber in the stack. Then a lucky fiber, on the unlocking time, will walk the stack, reverse the order to make it a FIFO queue and populates the waiters_head of the lock. This way the next time a fiber will unlock the lock, it will hand the lock to the oldest waiting fiber.
Definition in file mutex.h.
| typedef struct srn_fiber_mutex_t srn_fiber_mutex_t |
| void srn_fiber_mutex_init | ( | srn_fiber_mutex_t * | m | ) |
Initialise m to the unlocked state.
Definition at line 76 of file mutex.c.
| void srn_fiber_mutex_lock | ( | srn_fiber_mutex_t * | m | ) |
Acquire m.
If the lock is free it is taken at once, otherwise the calling fiber is suspended and resumes owning the lock, handed off directly by the unlocking fiber. Must be called from a running fiber.
Definition at line 81 of file mutex.c.
| void srn_fiber_mutex_unlock | ( | srn_fiber_mutex_t * | m | ) |
Release m.
If any fibers are waiting, the longest waiting one is handed the lock and made ready, otherwise the lock is dropped. Must be called by the fiber that currently holds m.
Definition at line 99 of file mutex.c.