Browse Source

spinlocks

master
Mathieu Serandour 5 months ago
parent
commit
4acfd0121a
  1. 14
      kernel/sync/spinlock.h
  2. 7
      kernel/sync/spinlock.s

14
kernel/sync/spinlock.h

@ -2,12 +2,13 @@
#include <stdint.h>
#include "../lib/stacktrace.h"
#include "../lib/logging.h"
typedef uint32_t spinlock_t;
// fast spinlocks use the same functions as the normal spinlocks
typedef struct {uint32_t val; uint8_t reserved[60];} __attribute__((aligned(64))) fast_spinlock_t;
typedef union {uint32_t val; uint8_t reserved[128];} __attribute__((aligned(128))) fast_spinlock_t;
static inline void spinlock_init(spinlock_t* lock) {
@ -21,10 +22,21 @@ void _spinlock_release(void *lock);
static inline void spinlock_acquire(void* lock) {
//log_warn("spinlock_acquire(%lx)", (uint64_t)lock);
//stacktrace_print();
//log_warn("---------------------------");
_spinlock_acquire(lock);
}
static inline void spinlock_release(void* lock) {
//log_warn("spinlock_release(%lx)", (uint64_t)lock);
//stacktrace_print();
//log_warn("---------------------------");
_spinlock_release(lock);
}

7
kernel/sync/spinlock.s

@ -3,15 +3,16 @@
[global _spinlock_release]
; stolen from https://wiki.osdev.org/Spinlock#Improved_Lock
;
spin_wait:
pause
test dword [rdi],1 ;Is the lock free?
test dword [rdi],1 ;Is the lock free?
jnz spin_wait ;no, wait
_spinlock_acquire:
lock bts dword [rdi],0 ;Attempt to acquire the lock (in case lock is uncontended)
lock bts dword [rdi],0 ;Attempt to acquire the lock (in case lock is uncontended)
jc spin_wait ;Spin if locked ( organize code such that conditional jumps are typically not taken )
ret ;Lock obtained
ret ;Lock obtained
_spinlock_release:

Loading…
Cancel
Save