Arax -8d09c51940345c86062e8ef2427c705ae66e5926
A Runtime Framework for Decoupling Applications from Heterogeneous Accelerators
Loading...
Searching...
No Matches
spinlock.h
Go to the documentation of this file.
1#ifndef UTILS_SPINLOCK_HEADER
2#define UTILS_SPINLOCK_HEADER
3#include <stdint.h>
4#include "utils/arax_assert.h"
5
6#if __x86_64__
7typedef volatile uint64_t utils_spinlock;
8
9#else /* if __x86_64__ */
10typedef volatile uint32_t utils_spinlock;
11
12#endif /* if __x86_64__ */
13
19static inline void utils_spinlock_init(utils_spinlock *lock)
20{
21 *lock = 0;
22}
23
32static inline void utils_spinlock_lock(utils_spinlock *lock)
33{
34 do {
35 while (*lock)
36 ; /* Maybe add relax()? */
37 if (__sync_bool_compare_and_swap(lock, 0, 1) )
38 break; /* We got it */
39 } while (1); /* Try again */
40}
41
48static inline void utils_spinlock_unlock(utils_spinlock *lock)
49{
50 arax_assert(*lock); /* Attempting to unlock twice */
51 __sync_fetch_and_and(lock, 0);
52}
53
54#endif /* ifndef UTILS_SPINLOCK_HEADER */
#define arax_assert(EXPR)
Definition arax_assert.h:7
volatile uint32_t utils_spinlock
Definition spinlock.h:10
#define utils_spinlock_lock(V)
Definition queue.c:35
#define utils_spinlock_init(V)
Definition queue.c:34
#define utils_spinlock_unlock(V)
Definition queue.c:36