PatchworkOS  966e257
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 Initializes a rwmutex.
53 *
54 * @param mtx Pointer to the rwmutex to initialize.
55 */
56void rwmutex_init(rwmutex_t* mtx);
57
58/**
59 * @brief Deinitializes a rwmutex.
60 *
61 * @param mtx Pointer to the rwmutex to deinitialize.
62 */
63void rwmutex_deinit(rwmutex_t* mtx);
64
65/**
66 * @brief Acquires a rwmutex for reading, blocking until it is available.
67 *
68 * @param mtx Pointer to the rwmutex to acquire.
69 */
71
72/**
73 * @brief Tries to acquire a rwmutex for reading.
74 *
75 * If the rwmutex is owned by another thread for writing or a writer is waiting, this function
76 * will fail with `EWOULDBLOCK`.
77 *
78 * If the function succeeds, `rwmutex_read_release()` must be called to release the rwmutex.
79 *
80 * @param mtx Pointer to the rwmutex to acquire.
81 * @return On success, `0`. On error, `ERR` and `errno` is set.
82 */
84
85/**
86 * @brief Releases a rwmutex from reading.
87 *
88 * @param mtx Pointer to the rwmutex to release.
89 */
91
92/**
93 * @brief Acquires a rwmutex for writing, blocking until it is available.
94 *
95 * @param mtx Pointer to the rwmutex to acquire.
96 */
98
99/**
100 * @brief Tries to acquire a rwmutex for writing.
101 *
102 * If the function succeeds, `rwmutex_write_release()` must be called to release the rwmutex.
103 *
104 * @param mtx Pointer to the rwmutex to acquire.
105 * @return On success, `0`. On error, `ERR` and `errno` is set.
106 */
108
109/**
110 * @brief Releases a rwmutex from writing.
111 *
112 * @param mtx Pointer to the rwmutex to release.
113 */
115
117{
119}
120
122{
124}
125
126/** @} */
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:116
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:121
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
static mtx_t mutex
Definition heap.c:35
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT16_TYPE__ uint16_t
Definition stdint.h:13
A simple ticket lock implementation.
Definition lock.h:43
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:182