Browse Source

spinlocks

master
Mathieu Serandour 6 months ago
parent
commit
ca2cb6489a
  1. 33
      kernel/sync/spinlock.h
  2. 19
      kernel/sync/spinlock.s

33
kernel/sync/spinlock.h

@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
#include "../lib/stacktrace.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;
static inline void spinlock_init(spinlock_t* lock) {
*lock = 0;
}
// should take either a spinlock_t* or a fast_spinlock_t*
void _spinlock_acquire(void *lock);
void _spinlock_release(void *lock);
static inline void spinlock_acquire(void* lock) {
_spinlock_acquire(lock);
}
static inline void spinlock_release(void* lock) {
_spinlock_release(lock);
}

19
kernel/sync/spinlock.s

@ -0,0 +1,19 @@
[global _spinlock_init]
[global _spinlock_acquire]
[global _spinlock_release]
; stolen from https://wiki.osdev.org/Spinlock#Improved_Lock
spin_wait:
pause
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)
jc spin_wait ;Spin if locked ( organize code such that conditional jumps are typically not taken )
ret ;Lock obtained
_spinlock_release:
mov dword [rdi], 0
ret
Loading…
Cancel
Save