mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 23:18:21 +00:00
spinlock: spin non atomically while waiting for lock to be released
This commit is contained in:
@@ -10,25 +10,27 @@ typedef int spinlock_t;
|
|||||||
#define SPINLOCK_INIT(x) x = SPINLOCK_INIT_VALUE
|
#define SPINLOCK_INIT(x) x = SPINLOCK_INIT_VALUE
|
||||||
#define SPINLOCK_DEFINE(x) spinlock_t x = SPINLOCK_INIT_VALUE
|
#define SPINLOCK_DEFINE(x) spinlock_t x = SPINLOCK_INIT_VALUE
|
||||||
|
|
||||||
static inline void spinlock_acquire(spinlock_t *lock){
|
static inline bool spinlock_try(spinlock_t *lock) {
|
||||||
while(!__sync_bool_compare_and_swap(lock, 0, 1)) asm("pause");
|
return __sync_bool_compare_and_swap(lock, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool spinlock_acquire_irq_clear(spinlock_t *lock){
|
static inline void spinlock_acquire(spinlock_t *lock) {
|
||||||
|
while (!__sync_bool_compare_and_swap(lock, 0, 1)) {
|
||||||
|
while (*lock) asm volatile ("pause" : : : "memory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool spinlock_acquire_irq_clear(spinlock_t *lock) {
|
||||||
bool ret = interrupt_set(false);
|
bool ret = interrupt_set(false);
|
||||||
spinlock_acquire(lock);
|
spinlock_acquire(lock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool spinlock_try(spinlock_t *lock){
|
static inline void spinlock_release(spinlock_t *lock) {
|
||||||
return __sync_bool_compare_and_swap(lock, 0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void spinlock_release(spinlock_t *lock){
|
|
||||||
*lock = 0;
|
*lock = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void spinlock_release_irq_restore(spinlock_t *lock, bool irqstate){
|
static inline void spinlock_release_irq_restore(spinlock_t *lock, bool irqstate) {
|
||||||
spinlock_release(lock);
|
spinlock_release(lock);
|
||||||
interrupt_set(irqstate);
|
interrupt_set(irqstate);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user