Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame^] | 1 | #ifndef _SPINLOCK_H |
| 2 | #define _SPINLOCK_H |
| 3 | |
| 4 | #include <stdatomic.h> |
| 5 | |
| 6 | struct spinlock { |
| 7 | atomic_flag v; |
| 8 | }; |
| 9 | |
| 10 | #define SPINLOCK_INIT {.v = ATOMIC_FLAG_INIT} |
| 11 | |
| 12 | static inline void sl_init(struct spinlock *l) |
| 13 | { |
| 14 | *l = (struct spinlock)SPINLOCK_INIT; |
| 15 | } |
| 16 | |
| 17 | static inline void sl_lock(struct spinlock *l) |
| 18 | { |
| 19 | while (atomic_flag_test_and_set_explicit(&l->v, memory_order_acquire)); |
| 20 | } |
| 21 | |
| 22 | static inline void sl_unlock(struct spinlock *l) |
| 23 | { |
| 24 | atomic_flag_clear_explicit(&l->v, memory_order_release); |
| 25 | } |
| 26 | |
| 27 | #endif /* _SPINLOCK_H */ |