PatchworkOS  2ca1c69
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 {.waitQueue = WAIT_QUEUE_CREATE(name.waitQueue), .owner = NULL, .depth = 0, .lock = LOCK_CREATE()}
35
36/**
37 * @brief Mutex structure.
38 * @struct mutex_t
39 */
47
48/**
49 * @brief Initializes a mutex.
50 *
51 * @param mtx Pointer to the mutex to initialize.
52 */
53void mutex_init(mutex_t* mtx);
54
55/**
56 * @brief Deinitializes a mutex.
57 *
58 * @param mtx Pointer to the mutex to deinitialize.
59 */
60void mutex_deinit(mutex_t* mtx);
61
62/**
63 * @brief Acquires a mutex, blocking until it is available.
64 *
65 * If the mutex is already owned by the current thread, this function will return immediately.
66 *
67 * @param mtx Pointer to the mutex to acquire.
68 */
69void mutex_acquire(mutex_t* mtx);
70
71/**
72 * @brief Acquires a mutex, blocking until it is available or the timeout is reached.
73 *
74 * If the mutex is already owned by the current thread, this function will return immediately.
75 *
76 * @param mtx Pointer to the mutex to acquire.
77 * @param timeout Timeout in clock ticks or `CLOCKS_NEVER` to wait indefinitely.
78 * @return true if the mutex was acquired, false if the timeout was reached.
79 */
80bool mutex_acquire_timeout(mutex_t* mtx, clock_t timeout);
81
82/**
83 * @brief Releases a mutex.
84 *
85 * The mutex must be owned by the current thread.
86 *
87 * @param mtx Pointer to the mutex to release.
88 */
89void mutex_release(mutex_t* mtx);
90
91static inline void mutex_cleanup(mutex_t** mtx)
92{
93 mutex_release(*mtx);
94}
95
96/** @} */
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:34
void mutex_release(mutex_t *mtx)
Releases a mutex.
Definition mutex.c:104
void mutex_acquire(mutex_t *mtx)
Acquires a mutex, blocking until it is available.
Definition mutex.c:27
void mutex_deinit(mutex_t *mtx)
Deinitializes a mutex.
Definition mutex.c:21
static void mutex_cleanup(mutex_t **mtx)
Definition mutex.h:91
void mutex_init(mutex_t *mtx)
Initializes a mutex.
Definition mutex.c:13
__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:43
Mutex structure.
Definition mutex.h:41
lock_t lock
Definition mutex.h:45
wait_queue_t waitQueue
Definition mutex.h:42
thread_t * owner
Definition mutex.h:43
uint32_t depth
Definition mutex.h:44
Thread of execution structure.
Definition thread.h:63
The primitive that threads block on.
Definition wait.h:182