Implement lock IRQ nesting via stack variables/contexts
All checks were successful
Build documentation / build-and-deploy (push) Successful in 21s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 21s
This commit is contained in:
@@ -2,14 +2,17 @@
|
||||
#include <libk/std.h>
|
||||
#include <sync/rw_spin_lock.h>
|
||||
#include <sys/debug.h>
|
||||
#include <sys/irq.h>
|
||||
#include <sys/spin_lock.h>
|
||||
|
||||
#define WRITER_WAIT (1U << 31)
|
||||
#define READER_MASK (~WRITER_WAIT)
|
||||
|
||||
void rw_spin_read_lock (rw_spin_lock_t* rw) {
|
||||
void rw_spin_read_lock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
|
||||
uint32_t value;
|
||||
|
||||
irq_save (ctx);
|
||||
|
||||
for (;;) {
|
||||
value = atomic_load_explicit (rw, memory_order_relaxed);
|
||||
|
||||
@@ -24,14 +27,17 @@ void rw_spin_read_lock (rw_spin_lock_t* rw) {
|
||||
}
|
||||
}
|
||||
|
||||
void rw_spin_read_unlock (rw_spin_lock_t* rw) {
|
||||
void rw_spin_read_unlock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
|
||||
uint32_t old = atomic_fetch_sub_explicit (rw, 1, memory_order_release);
|
||||
assert ((old & READER_MASK) > 0);
|
||||
irq_restore (ctx);
|
||||
}
|
||||
|
||||
void rw_spin_write_lock (rw_spin_lock_t* rw) {
|
||||
void rw_spin_write_lock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
|
||||
uint32_t value;
|
||||
|
||||
irq_save (ctx);
|
||||
|
||||
/* announce writer */
|
||||
for (;;) {
|
||||
value = atomic_load_explicit (rw, memory_order_relaxed);
|
||||
@@ -40,8 +46,9 @@ void rw_spin_write_lock (rw_spin_lock_t* rw) {
|
||||
if (atomic_compare_exchange_weak_explicit (rw, &value, (value | WRITER_WAIT),
|
||||
memory_order_acquire, memory_order_relaxed))
|
||||
break;
|
||||
} else
|
||||
} else {
|
||||
spin_lock_relax ();
|
||||
}
|
||||
}
|
||||
|
||||
/* wait for readers */
|
||||
@@ -54,6 +61,7 @@ void rw_spin_write_lock (rw_spin_lock_t* rw) {
|
||||
}
|
||||
}
|
||||
|
||||
void rw_spin_write_unlock (rw_spin_lock_t* rw) {
|
||||
void rw_spin_write_unlock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
|
||||
atomic_store_explicit (rw, 0, memory_order_release);
|
||||
irq_restore (ctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user