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,8 +10,14 @@ 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 bool spinlock_try(spinlock_t *lock) {
|
||||||
|
return __sync_bool_compare_and_swap(lock, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
static inline void spinlock_acquire(spinlock_t *lock) {
|
static inline void spinlock_acquire(spinlock_t *lock) {
|
||||||
while(!__sync_bool_compare_and_swap(lock, 0, 1)) asm("pause");
|
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) {
|
static inline bool spinlock_acquire_irq_clear(spinlock_t *lock) {
|
||||||
@@ -20,10 +26,6 @@ static inline bool spinlock_acquire_irq_clear(spinlock_t *lock){
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool spinlock_try(spinlock_t *lock){
|
|
||||||
return __sync_bool_compare_and_swap(lock, 0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void spinlock_release(spinlock_t *lock) {
|
static inline void spinlock_release(spinlock_t *lock) {
|
||||||
*lock = 0;
|
*lock = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user