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