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__
7
typedef
volatile
uint64_t
utils_spinlock
;
8
9
#else
/* if __x86_64__ */
10
typedef
volatile
uint32_t
utils_spinlock
;
11
12
#endif
/* if __x86_64__ */
13
19
static
inline
void
utils_spinlock_init
(
utils_spinlock
*lock)
20
{
21
*lock = 0;
22
}
23
32
static
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
48
static
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 */
arax_assert.h
arax_assert
#define arax_assert(EXPR)
Definition
arax_assert.h:7
utils_spinlock
volatile uint32_t utils_spinlock
Definition
spinlock.h:10
utils_spinlock_lock
#define utils_spinlock_lock(V)
Definition
queue.c:35
utils_spinlock_init
#define utils_spinlock_init(V)
Definition
queue.c:34
utils_spinlock_unlock
#define utils_spinlock_unlock(V)
Definition
queue.c:36
src
utils
spinlock.h
Generated by
1.13.2