blob: 1061fb3e57c868706fea2eee972bd636a8445228 [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
Andrew Scull4f170f52018-07-19 12:58:20 +010010#define SPINLOCK_INIT \
11 { \
12 .v = ATOMIC_FLAG_INIT \
13 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010014
15static inline void sl_init(struct spinlock *l)
16{
17 *l = (struct spinlock)SPINLOCK_INIT;
18}
19
20static inline void sl_lock(struct spinlock *l)
21{
Andrew Scull7364a8e2018-07-19 15:39:29 +010022 while (atomic_flag_test_and_set_explicit(&l->v, memory_order_acquire)) {
23 /* do nothing */
24 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010025}
26
27static inline void sl_unlock(struct spinlock *l)
28{
29 atomic_flag_clear_explicit(&l->v, memory_order_release);
30}
31
Andrew Scull4f170f52018-07-19 12:58:20 +010032#endif /* _SPINLOCK_H */