summaryrefslogtreecommitdiffstats
path: root/lib/spinlock.h
blob: 75af28aac4af3dcae9ca9bd94e32bfbff75df391 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef JOVE_LIB_SPINLOCK_H
#define JOVE_LIB_SPINLOCK_H 1

#include <stdatomic.h>

typedef struct Spinlock
{
    atomic_flag flg;
    
    const char *locker_file;
    const char *locker_func;
    int locker_line;
} spinlock_t;

#define spinlock_acquire(lock) \
    while(atomic_flag_test_and_set_explicit(&lock.flg, memory_order_acquire)) \
        __builtin_ia32_pause(); \
    lock.locker_file = __FILE__; \
    lock.locker_func = __FUNCTION__; \
    lock.locker_line = __LINE__

#define spinlock_release(lock) \
    atomic_flag_clear_explicit(&lock.flg, memory_order_release); \
    lock.locker_file = lock.locker_func = NULL; \
    lock.locker_line = -1

#endif