PatchworkOS
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
17
19{
21
22#ifndef NDEBUG
24#endif
25
27
28 while (atomic_load(&lock->readServe) != ticket)
29 {
30 asm volatile("pause");
31#ifndef NDEBUG
32 clock_t now = timer_uptime();
33 if (start != 0 && now - start > RWLOCK_DEADLOCK_TIMEOUT)
34 {
35 panic(NULL, "Deadlock in rwlock_read_acquire detected");
36 }
37#endif
38 }
39
41 {
42#ifndef NDEBUG
43 clock_t now = timer_uptime();
44 if (start != 0 && now - start > RWLOCK_DEADLOCK_TIMEOUT)
45 {
46 panic(NULL, "Deadlock in rwlock_read_acquire detected");
47 }
48#endif
49 asm volatile("pause");
50 }
51
53
55}
56
64
66{
68
69#ifndef NDEBUG
71#endif
72
74
75 while (atomic_load(&lock->writeServe) != ticket)
76 {
77#ifndef NDEBUG
78 clock_t now = timer_uptime();
79 if (start != 0 && now - start > RWLOCK_DEADLOCK_TIMEOUT)
80 {
81 panic(NULL, "Deadlock in rwlock_write_acquire detected");
82 }
83#endif
84 asm volatile("pause");
85 }
86
87 while (atomic_load(&lock->activeReaders) > 0)
88 {
89#ifndef NDEBUG
90 clock_t now = timer_uptime();
91 if (start != 0 && now - start > RWLOCK_DEADLOCK_TIMEOUT)
92 {
93 panic(NULL, "Deadlock in rwlock_write_acquire detected");
94 }
95#endif
96 asm volatile("pause");
97 }
98
99 bool expected = false;
100 while (!atomic_compare_exchange_weak(&lock->activeWriter, &expected, true))
101 {
102#ifndef NDEBUG
103 clock_t now = timer_uptime();
104 if (start != 0 && now - start > RWLOCK_DEADLOCK_TIMEOUT)
105 {
106 panic(NULL, "Deadlock in rwlock_write_acquire detected");
107 }
108#endif
109 expected = false;
110 asm volatile("pause");
111 }
112
114}
115
void interrupt_disable(void)
Disable interrupts and increment the disableDepth.
Definition interrupt.c:26
void interrupt_enable(void)
Decrement the CLI depth and enable interrupts if depth reaches zero and interrupts were previously en...
Definition interrupt.c:38
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
void rwlock_write_acquire(rwlock_t *lock)
Acquires a rwlock for writing, blocking until it is available.
Definition rwlock.c:65
void rwlock_read_acquire(rwlock_t *lock)
Acquires a rwlock for reading, blocking until it is available.
Definition rwlock.c:18
#define RWLOCK_DEADLOCK_TIMEOUT
Maximum time before we consider a deadlock to have occurred.
Definition rwlock.h:20
void rwlock_read_release(rwlock_t *lock)
Releases a rwlock from reading.
Definition rwlock.c:57
void rwlock_write_release(rwlock_t *lock)
Releases a rwlock from writing.
Definition rwlock.c:116
void rwlock_init(rwlock_t *lock)
Initializes a rwlock.
Definition rwlock.c:8
clock_t timer_uptime(void)
Time since boot.
Definition timer.c:73
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static rwlock_t lock
Definition irq.c:10
static void start()
Definition main.c:542
#define atomic_store(object, desired)
Definition stdatomic.h:289
#define atomic_compare_exchange_weak(object, expected, desired)
Definition stdatomic.h:280
@ memory_order_seq_cst
Definition stdatomic.h:121
#define atomic_fetch_sub(object, operand)
Definition stdatomic.h:286
#define atomic_thread_fence(order)
Definition stdatomic.h:135
#define atomic_load(object)
Definition stdatomic.h:288
#define atomic_fetch_add(object, operand)
Definition stdatomic.h:283
#define atomic_init(obj, value)
Definition stdatomic.h:75
__UINT_FAST16_TYPE__ uint_fast16_t
Definition stdint.h:37
Read-Write Ticket Lock structure.
Definition rwlock.h:59
atomic_uint_fast16_t readTicket
Definition rwlock.h:60
atomic_uint_fast16_t writeServe
Definition rwlock.h:63
atomic_uint_fast16_t readServe
Definition rwlock.h:61
atomic_uint_fast16_t activeReaders
Definition rwlock.h:64
atomic_uint_fast16_t writeTicket
Definition rwlock.h:62
atomic_bool activeWriter
Definition rwlock.h:65