blob: e077ddc268c3ff17db3408a327d4319eaae9b7de (
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
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
|
/* Intel ia32 assembly.
* Don't include directly, use asm.h instead.
*
* Most of this code comes straight out of the Linux kernel.
*
* The terms of the GPL v2 apply.
*/
static inline void barrier(void)
{
__asm__ __volatile__("sfence": : :"memory");
}
static __inline__ void cpu_relax(void)
{
__asm__ __volatile("pause");
}
typedef struct { int counter; } atomic_t;
#define ATOMIC_INIT(i) { (i) }
/**
* atomic_read - read atomic variable
* @v: pointer of type atomic_t
*
* Atomically reads the value of @v.
*/
#define atomic_read(v) ((v)->counter)
/**
* atomic_set - set atomic variable
* @v: pointer of type atomic_t
* @i: required value
*
* Atomically sets the value of @v to @i.
*/
#define atomic_set(v,i) (((v)->counter) = (i))
static __inline__ void atomic_add(int i, atomic_t *v)
{
__asm__ __volatile__(
"lock; addl %1,%0"
:"+m" (v->counter)
:"ir" (i));
}
/**
* atomic_add_return - add and return
* @v: pointer of type atomic_t
* @i: integer value to add
*
* Atomically adds @i to @v and returns @i + @v
*/
static __inline__ int atomic_add_return(int i, atomic_t *v)
{
int __i;
__i = i;
__asm__ __volatile__(
"lock; xaddl %0, %1"
:"+r" (i), "+m" (v->counter)
: : "memory");
return i + __i;
}
#define atomic_inc_return(v) (atomic_add_return(1,v))
|