PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
rwmutex.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/sched/wait.h>
4#include <stdint.h>
5
6typedef struct rwmutex rwmutex_t;
7
8/**
9 * @brief Read-Write Mutex
10 * @defgroup kernel_sync_rwmutex Read-Write Mutex
11 * @ingroup kernel_sync
12 *
13 * @{
14 */
15
16/**
17 * @brief Acquires a rwmutex for reading for the reminder of the current scope.
18 *
19 * @param mutex Pointer to the rwmutex to acquire.
20 */
21#define RWMUTEX_READ_SCOPE(mutex) \
22 __attribute__((cleanup(rwmutex_read_cleanup))) rwmutex_t* CONCAT(rm, __COUNTER__) = (mutex); \
23 rwmutex_read_acquire((mutex))
24
25/**
26 * @brief Acquires a rwmutex for writing for the reminder of the current scope.
27 *
28 * @param mutex Pointer to the rwmutex to acquire.
29 */
30#define RWMUTEX_WRITE_SCOPE(mutex) \
31 __attribute__((cleanup(rwmutex_write_cleanup))) rwmutex_t* CONCAT(wm, __COUNTER__) = (mutex); \
32 rwmutex_write_acquire((mutex))
33
34/**
35 * @brief Read-Write Mutex structure.
36 * @struct rwmutex_t
37 *
38 * A Read-Write Mutex allows one only writer or multiple readers to access a shared resource at the same time. This
39 * implementation prioritizes writers over readers and does not support recursive locking.
40 */
50
51/**
52 * @brief Create a rwmutex initializer.
53 * @def RWMUTEX_CREATE
54 *
55 * @param name The name of the rwmutex variable to initialize.
56 * @return A `rwmutex_t` initializer.
57 */
58#define RWMUTEX_CREATE(name) \
59 { \
60 .activeReaders = 0, \
61 .waitingWriters = 0, \
62 .readerQueue = WAIT_QUEUE_CREATE(name.readerQueue), \
63 .writerQueue = WAIT_QUEUE_CREATE(name.writerQueue), \
64 .hasWriter = false, \
65 .lock = LOCK_CREATE(), \
66 }
67
68/**
69 * @brief Initializes a rwmutex.
70 *
71 * @param mtx Pointer to the rwmutex to initialize.
72 */
73void rwmutex_init(rwmutex_t* mtx);
74
75/**
76 * @brief Deinitializes a rwmutex.
77 *
78 * @param mtx Pointer to the rwmutex to deinitialize.
79 */
80void rwmutex_deinit(rwmutex_t* mtx);
81
82/**
83 * @brief Acquires a rwmutex for reading, blocking until it is available.
84 *
85 * @param mtx Pointer to the rwmutex to acquire.
86 */
88
89/**
90 * @brief Tries to acquire a rwmutex for reading.
91 *
92 * If the rwmutex is owned by another thread for writing or a writer is waiting, this function
93 * will fail with `EWOULDBLOCK`.
94 *
95 * If the function succeeds, `rwmutex_read_release()` must be called to release the rwmutex.
96 *
97 * @param mtx Pointer to the rwmutex to acquire.
98 * @return On success, `0`. On error, `ERR` and `errno` is set.
99 */
101
102/**
103 * @brief Releases a rwmutex from reading.
104 *
105 * @param mtx Pointer to the rwmutex to release.
106 */
108
109/**
110 * @brief Acquires a rwmutex for writing, blocking until it is available.
111 *
112 * @param mtx Pointer to the rwmutex to acquire.
113 */
115
116/**
117 * @brief Tries to acquire a rwmutex for writing.
118 *
119 * If the function succeeds, `rwmutex_write_release()` must be called to release the rwmutex.
120 *
121 * @param mtx Pointer to the rwmutex to acquire.
122 * @return On success, `0`. On error, `ERR` and `errno` is set.
123 */
125
126/**
127 * @brief Releases a rwmutex from writing.
128 *
129 * @param mtx Pointer to the rwmutex to release.
130 */
132
133static inline void rwmutex_read_cleanup(rwmutex_t** mutex)
134{
135 rwmutex_read_release(*mutex);
136}
137
138static inline void rwmutex_write_cleanup(rwmutex_t** mutex)
139{
140 rwmutex_write_release(*mutex);
141}
142
143/** @} */
void rwmutex_write_acquire(rwmutex_t *mtx)
Acquires a rwmutex for writing, blocking until it is available.
Definition rwmutex.c:85
void rwmutex_init(rwmutex_t *mtx)
Initializes a rwmutex.
Definition rwmutex.c:10
uint64_t rwmutex_write_try_acquire(rwmutex_t *mtx)
Tries to acquire a rwmutex for writing.
Definition rwmutex.c:104
void rwmutex_read_acquire(rwmutex_t *mtx)
Acquires a rwmutex for reading, blocking until it is available.
Definition rwmutex.c:30
uint64_t rwmutex_read_try_acquire(rwmutex_t *mtx)
Tries to acquire a rwmutex for reading.
Definition rwmutex.c:47
static void rwmutex_read_cleanup(rwmutex_t **mutex)
Definition rwmutex.h:133
void rwmutex_read_release(rwmutex_t *mtx)
Releases a rwmutex from reading.
Definition rwmutex.c:67
static void rwmutex_write_cleanup(rwmutex_t **mutex)
Definition rwmutex.h:138
void rwmutex_deinit(rwmutex_t *mtx)
Deinitializes a rwmutex.
Definition rwmutex.c:20
void rwmutex_write_release(rwmutex_t *mtx)
Releases a rwmutex from writing.
Definition rwmutex.c:124
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT16_TYPE__ uint16_t
Definition stdint.h:13
A simple ticket lock implementation.
Definition lock.h:44
Read-Write Mutex structure.
Definition rwmutex.h:42
wait_queue_t writerQueue
Definition rwmutex.h:46
bool hasWriter
Definition rwmutex.h:47
wait_queue_t readerQueue
Definition rwmutex.h:45
uint16_t waitingWriters
Definition rwmutex.h:44
uint16_t activeReaders
Definition rwmutex.h:43
lock_t lock
Definition rwmutex.h:48
The primitive that threads block on.
Definition wait.h:185