PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/sched/wait.h>
4#include <kernel/sync/lock.h>
5
6#include <stdbool.h>
7
8typedef struct thread thread_t;
9
10/**
11 * @brief Recursive Mutex
12 * @defgroup kernel_sync_mutex Mutex
13 * @ingroup kernel_sync
14 *
15 * @{
16 */
17
18/**
19 * @brief Acquires a mutex for the reminder of the current scope.
20 *
21 * @param mutex Pointer to the mutex to acquire.
22 */
23#define MUTEX_SCOPE(mutex) \
24 __attribute__((cleanup(mutex_cleanup))) mutex_t* CONCAT(m, __COUNTER__) = (mutex); \
25 mutex_acquire((mutex))
26
27/**
28 * @brief Create a mutex initializer.
29 *
30 * @param name Name of the mutex variable.
31 * @return A mutex initializer.
32 */
33#define MUTEX_CREATE(name) \
34 { \
35 .waitQueue = WAIT_QUEUE_CREATE(name.waitQueue), \
36 .owner = NULL, \
37 .depth = 0, \
38 .lock = LOCK_CREATE(), \
39 }
40
41/**
42 * @brief Mutex structure.
43 * @struct mutex_t
44 */
52
53/**
54 * @brief Initializes a mutex.
55 *
56 * @param mtx Pointer to the mutex to initialize.
57 */
58void mutex_init(mutex_t* mtx);
59
60/**
61 * @brief Deinitializes a mutex.
62 *
63 * @param mtx Pointer to the mutex to deinitialize.
64 */
65void mutex_deinit(mutex_t* mtx);
66
67/**
68 * @brief Acquires a mutex, blocking until it is available.
69 *
70 * If the mutex is already owned by the current thread, this function will return immediately.
71 *
72 * @param mtx Pointer to the mutex to acquire.
73 */
74void mutex_acquire(mutex_t* mtx);
75
76/**
77 * @brief Acquires a mutex, blocking until it is available or the timeout is reached.
78 *
79 * If the mutex is already owned by the current thread, this function will return immediately.
80 *
81 * @param mtx Pointer to the mutex to acquire.
82 * @param timeout Timeout in clock ticks or `CLOCKS_NEVER` to wait indefinitely.
83 * @return true if the mutex was acquired, false if the timeout was reached.
84 */
85bool mutex_acquire_timeout(mutex_t* mtx, clock_t timeout);
86
87/**
88 * @brief Releases a mutex.
89 *
90 * The mutex must be owned by the current thread.
91 *
92 * @param mtx Pointer to the mutex to release.
93 */
94void mutex_release(mutex_t* mtx);
95
96static inline void mutex_cleanup(mutex_t** mtx)
97{
98 mutex_release(*mtx);
99}
100
101/** @} */
bool mutex_acquire_timeout(mutex_t *mtx, clock_t timeout)
Acquires a mutex, blocking until it is available or the timeout is reached.
Definition mutex.c:35
void mutex_release(mutex_t *mtx)
Releases a mutex.
Definition mutex.c:105
void mutex_acquire(mutex_t *mtx)
Acquires a mutex, blocking until it is available.
Definition mutex.c:28
void mutex_deinit(mutex_t *mtx)
Deinitializes a mutex.
Definition mutex.c:22
static void mutex_cleanup(mutex_t **mtx)
Definition mutex.h:96
void mutex_init(mutex_t *mtx)
Initializes a mutex.
Definition mutex.c:14
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
A simple ticket lock implementation.
Definition lock.h:44
Mutex structure.
Definition mutex.h:46
lock_t lock
Definition mutex.h:50
wait_queue_t waitQueue
Definition mutex.h:47
thread_t * owner
Definition mutex.h:48
uint32_t depth
Definition mutex.h:49
Thread of execution structure.
Definition thread.h:61
The primitive that threads block on.
Definition wait.h:185