PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
rwlock.c
Go to the documentation of this file.
2
3#ifndef NDEBUG
4#include <kernel/log/panic.h>
6#endif
7
9{
10 atomic_init(&lock->readTicket, 0);
11 atomic_init(&lock->readServe, 0);
12 atomic_init(&lock->writeTicket, 0);
13 atomic_init(&lock->writeServe, 0);
14 atomic_init(&lock->activeReaders, 0);
15 atomic_init(&lock->activeWriter, false);
16}
17
19{
21
22#ifndef NDEBUG
23 uint64_t iterations = 0;
24#endif
25
27
28 while (atomic_load_explicit(&lock->readServe, memory_order_acquire) != ticket)
29 {
30 asm volatile("pause");
31#ifndef NDEBUG
32 if (++iterations >= RWLOCK_DEADLOCK_ITERATIONS)
33 {
34 panic(NULL, "Deadlock in rwlock_read_acquire detected");
35 }
36#endif
37 }
38
39 while (atomic_load_explicit(&lock->writeServe, memory_order_relaxed) !=
41 {
42#ifndef NDEBUG
43 if (++iterations >= RWLOCK_DEADLOCK_ITERATIONS)
44 {
45 panic(NULL, "Deadlock in rwlock_read_acquire detected");
46 }
47#endif
48 asm volatile("pause");
49 }
50
52}
53
61
63{
65
66#ifndef NDEBUG
67 uint64_t iterations = 0;
68#endif
69
71
72 while (atomic_load_explicit(&lock->writeServe, memory_order_acquire) != ticket)
73 {
74#ifndef NDEBUG
75 if (++iterations >= RWLOCK_DEADLOCK_ITERATIONS)
76 {
77 panic(NULL, "Deadlock in rwlock_write_acquire detected");
78 }
79#endif
80 asm volatile("pause");
81 }
82
83 while (atomic_load_explicit(&lock->activeReaders, memory_order_acquire) > 0)
84 {
85#ifndef NDEBUG
86 if (++iterations >= RWLOCK_DEADLOCK_ITERATIONS)
87 {
88 panic(NULL, "Deadlock in rwlock_write_acquire detected");
89 }
90#endif
91 asm volatile("pause");
92 }
93
94 bool expected = false;
95 while (!atomic_compare_exchange_weak_explicit(&lock->activeWriter, &expected, true, memory_order_acquire,
97 {
98#ifndef NDEBUG
99 if (++iterations >= RWLOCK_DEADLOCK_ITERATIONS)
100 {
101 panic(NULL, "Deadlock in rwlock_write_acquire detected");
102 }
103#endif
104 expected = false;
105 asm volatile("pause");
106 }
107}
108
void interrupt_disable(void)
Disable interrupts and increment the disableDepth.
Definition interrupt.c:25
void interrupt_enable(void)
Decrement the CLI depth and enable interrupts if depth reaches zero and interrupts were previously en...
Definition interrupt.c:37
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:266
void rwlock_write_acquire(rwlock_t *lock)
Acquires a rwlock for writing, blocking until it is available.
Definition rwlock.c:62
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
#define RWLOCK_DEADLOCK_ITERATIONS
Number of iterations before we consider a deadlock to have occurred in a rwlock operation....
Definition rwlock.h:22
void rwlock_init(rwlock_t *lock)
Initializes a rwlock.
Definition rwlock.c:8
#define NULL
Pointer error value.
Definition NULL.h:23
static lock_t lock
Definition io.c:13
@ memory_order_release
Definition stdatomic.h:119
@ memory_order_relaxed
Definition stdatomic.h:116
@ memory_order_acquire
Definition stdatomic.h:118
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:259
#define atomic_load_explicit(object, order)
Definition stdatomic.h:264
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:265
#define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure)
Definition stdatomic.h:240
#define atomic_init(obj, value)
Definition stdatomic.h:75
#define atomic_fetch_sub_explicit(object, operand, order)
Definition stdatomic.h:262
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT_FAST16_TYPE__ uint_fast16_t
Definition stdint.h:37
Read-Write Ticket Lock structure.
Definition rwlock.h:61