blob: 646136cff12c1bcd5ac3fbc5a0b3e1b19d0f6a97 [file] [log] [blame]
Andrew Scullfbc938a2018-08-20 14:09:28 +01001#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01002
3#include <stdatomic.h>
4
5struct spinlock {
6 atomic_flag v;
7};
8
Andrew Scull4f170f52018-07-19 12:58:20 +01009#define SPINLOCK_INIT \
10 { \
11 .v = ATOMIC_FLAG_INIT \
12 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010013
14static inline void sl_init(struct spinlock *l)
15{
16 *l = (struct spinlock)SPINLOCK_INIT;
17}
18
19static inline void sl_lock(struct spinlock *l)
20{
Andrew Scull7364a8e2018-07-19 15:39:29 +010021 while (atomic_flag_test_and_set_explicit(&l->v, memory_order_acquire)) {
22 /* do nothing */
23 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010024}
25
26static inline void sl_unlock(struct spinlock *l)
27{
28 atomic_flag_clear_explicit(&l->v, memory_order_release);
29}