-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic.h
More file actions
68 lines (56 loc) · 1.73 KB
/
Copy pathatomic.h
File metadata and controls
68 lines (56 loc) · 1.73 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef _ATOMIC_H_
#define _ATOMIC_H_
typedef int spinlock_t;
typedef int atomic_t;
/* #define __USE_GCC_BUILTIN */
#define spin_trylock(ptr) (!__sync_lock_test_and_set(ptr, 1))
#define spin_lock(ptr) ({ while (unlikely(!spin_trylock(ptr))) { }})
#define spin_unlock(ptr) __sync_lock_release(ptr)
#define atomic_add_fetch(object, operand) \
__sync_add_and_fetch(object, operand)
#define atomic_sub_fetch(object, operand) \
__sync_sub_and_fetch(object, operand)
#define atomic_load(object) *(object)
#define atomic_store(object, operand) *object = operand
#define likely(expr) __builtin_expect(!!(expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0)
static __inline void cpu_spinwait(void)
{
__asm __volatile("pause");
}
/*
* Atomic compare and set, used by the mutex functions
*
* if (*dst == expect) *dst = src (all 32 bit words)
*
* Returns 0 on failure, non-zero on success
*/
#ifndef __USE_GCC_BUILTIN
/* XXX this performs better than gcc's __sync_bool_compare_and_swap() */
static __inline int
atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src)
{
u_char res;
__asm __volatile(
" lock ; "
" cmpxchgl %3,%1 ; "
" sete %0 ; "
"# atomic_cmpset_int"
: "=q" (res), /* 0 */
"+m" (*dst), /* 1 */
"+a" (expect) /* 2 */
: "r" (src) /* 3 */
: "memory", "cc");
return res;
}
#else
static __inline int
atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src)
{
return __sync_bool_compare_and_swap(dst, expect, src);
}
#endif
#define mb() __asm __volatile("mfence;" : : : "memory")
#define wmb() __asm __volatile("sfence;" : : : "memory")
#define rmb() __asm __volatile("lfence;" : : : "memory")
#endif /* _ATOMIC_H_ */