PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
rwlock.h
Go to the documentation of this file.
1#pragma once
2
4#include <kernel/defs.h>
5
6#include <stdatomic.h>
7
8typedef struct thread thread_t;
9
10/**
11 * @brief Read-Write Ticket Lock
12 * @defgroup kernel_sync_rwlock Read-Write Ticket Lock
13 * @ingroup kernel_sync
14 *
15 * @{
16 */
17
18/**
19 * @brief Number of iterations before we consider a deadlock to have occurred in a rwlock operation.
20 * This is only used in debug builds.
21 */
22#define RWLOCK_DEADLOCK_ITERATIONS 10000000
23
24/**
25 * @brief Acquires a rwlock for reading for the reminder of the current scope.
26 *
27 * @param lock Pointer to the rwlock to acquire.
28 */
29#define RWLOCK_READ_SCOPE(lock) \
30 __attribute__((cleanup(rwlock_read_cleanup))) rwlock_t* CONCAT(rl, __COUNTER__) = (lock); \
31 rwlock_read_acquire((lock))
32
33/**
34 * @brief Acquires a rwlock for writing for the reminder of the current scope.
35 *
36 * @param lock Pointer to the rwlock to acquire.
37 */
38#define RWLOCK_WRITE_SCOPE(lock) \
39 __attribute__((cleanup(rwlock_write_cleanup))) rwlock_t* CONCAT(wl, __COUNTER__) = (lock); \
40 rwlock_write_acquire((lock))
41
42/**
43 * @brief Create a rwlock initializer.
44 *
45 * @return A rwlock_t initializer.
46 */
47#define RWLOCK_CREATE() \
48 (rwlock_t) \
49 { \
50 .readTicket = ATOMIC_VAR_INIT(0), .readServe = ATOMIC_VAR_INIT(0), .writeTicket = ATOMIC_VAR_INIT(0), \
51 .writeServe = ATOMIC_VAR_INIT(0), .activeReaders = ATOMIC_VAR_INIT(0), .activeWriter = ATOMIC_VAR_INIT(false) \
52 }
53
54/**
55 * @brief Read-Write Ticket Lock structure.
56 * @struct rwlock_t
57 *
58 * A Read-Write Ticket Lock allows one only writer or multiple readers to access a shared resource at the same time.
59 */
60typedef struct
61{
62 atomic_uint_fast16_t readTicket;
63 atomic_uint_fast16_t readServe;
64 atomic_uint_fast16_t writeTicket;
65 atomic_uint_fast16_t writeServe;
66 atomic_uint_fast16_t activeReaders;
67 atomic_bool activeWriter;
68} rwlock_t;
69
70/**
71 * @brief Initializes a rwlock.
72 *
73 * @param lock Pointer to the rwlock to initialize.
74 */
76
77/**
78 * @brief Acquires a rwlock for reading, blocking until it is available.
79 *
80 * @param lock Pointer to the rwlock to acquire.
81 */
83
84/**
85 * @brief Releases a rwlock from reading.
86 *
87 * @param lock Pointer to the rwlock to release.
88 */
90
91/**
92 * @brief Acquires a rwlock for writing, blocking until it is available.
93 *
94 * @param lock Pointer to the rwlock to acquire.
95 */
97
98/**
99 * @brief Releases a rwlock from writing.
100 *
101 * @param lock Pointer to the rwlock to release.
102 */
104
105static inline void rwlock_read_cleanup(rwlock_t** lock)
106{
108}
109
110static inline void rwlock_write_cleanup(rwlock_t** lock)
111{
113}
114
115/** @} */
void rwlock_write_acquire(rwlock_t *lock)
Acquires a rwlock for writing, blocking until it is available.
Definition rwlock.c:62
static void rwlock_write_cleanup(rwlock_t **lock)
Definition rwlock.h:110
void rwlock_read_acquire(rwlock_t *lock)
Acquires a rwlock for reading, blocking until it is available.
Definition rwlock.c:18
void rwlock_read_release(rwlock_t *lock)
Releases a rwlock from reading.
Definition rwlock.c:54
void rwlock_write_release(rwlock_t *lock)
Releases a rwlock from writing.
Definition rwlock.c:109
static void rwlock_read_cleanup(rwlock_t **lock)
Definition rwlock.h:105
void rwlock_init(rwlock_t *lock)
Initializes a rwlock.
Definition rwlock.c:8
static lock_t lock
Definition io.c:13
Read-Write Ticket Lock structure.
Definition rwlock.h:61
atomic_uint_fast16_t readTicket
Definition rwlock.h:62
atomic_uint_fast16_t writeServe
Definition rwlock.h:65
atomic_uint_fast16_t readServe
Definition rwlock.h:63
atomic_uint_fast16_t activeReaders
Definition rwlock.h:66
atomic_uint_fast16_t writeTicket
Definition rwlock.h:64
atomic_bool activeWriter
Definition rwlock.h:67
Thread of execution structure.
Definition thread.h:56