File: atomic.h

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (143 lines) | stat: -rw-r--r-- 3,022 bytes parent folder | download | duplicates (3)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef __CR_ATOMIC_H__
#define __CR_ATOMIC_H__

#include <linux/types.h>
#include "common/compiler.h"
#include "common/arch/mips/asm/utils.h"
#include "common/arch/mips/asm/cmpxchg.h"

/*
 * atomic_read - read atomic variable
 * @v: pointer of type atomic_t
 *
 * Atomically reads the value of @v.
 */
#define atomic_read(v) (*(volatile int *)&(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))
/*
 * atomic_add - add integer to atomic variable
 * @i: integer value to add
 * @v: pointer of type atomic_t
 *
 * Atomically adds @i to @v.
 */

static __inline__ void atomic_add(int i, atomic_t *v)
{
	int temp;

	do {
		__asm__ __volatile__("	.set	mips3				\n"
				     "	ll	%0, %1		# atomic_add	\n"
				     "	addu	%0, %2				\n"
				     "	sc	%0, %1				\n"
				     "	.set	mips0				\n"
				     : "=&r"(temp), "+m"(v->counter)
				     : "Ir"(i));
	} while (unlikely(!temp));
}

/*
 * atomic_sub - subtract the atomic variable
 * @i: integer value to subtract
 * @v: pointer of type atomic_t
 *
 * Atomically subtracts @i from @v.
 */
static __inline__ void atomic_sub(int i, atomic_t *v)
{
	int temp;

	do {
		__asm__ __volatile__("	.set	mips3				\n"
				     "	ll	%0, %1		# atomic_sub	\n"
				     "	subu	%0, %2				\n"
				     "	sc	%0, %1				\n"
				     "	.set	mips0				\n"
				     : "=&r"(temp), "+m"(v->counter)
				     : "Ir"(i));
	} while (unlikely(!temp));
}

/*
 * Same as above, but return the result value
 */
static __inline__ int atomic_add_return(int i, atomic_t *v)
{
	int result;

	smp_mb__before_llsc();

	int temp;

	do {
		__asm__ __volatile__("	.set	mips3				\n"
				     "	ll	%1, %2	# atomic_add_return	\n"
				     "	addu	%0, %1, %3			\n"
				     "	sc	%0, %2				\n"
				     "	.set	mips0				\n"
				     : "=&r"(result), "=&r"(temp), "+m"(v->counter)
				     : "Ir"(i));
	} while (unlikely(!result));

	result = temp + i;

	smp_llsc_mb();

	return result;
}

static __inline__ int atomic_sub_return(int i, atomic_t *v)
{
	int result;

	smp_mb__before_llsc();

	int temp;

	do {
		__asm__ __volatile__("	.set	mips3				\n"
				     "	ll	%1, %2	# atomic_sub_return	\n"
				     "	subu	%0, %1, %3			\n"
				     "	sc	%0, %2				\n"
				     "	.set	mips0				\n"
				     : "=&r"(result), "=&r"(temp), "+m"(v->counter)
				     : "Ir"(i));
	} while (unlikely(!result));

	result = temp - i;

	smp_llsc_mb();

	return result;
}

#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
#define atomic_dec_return(v)	atomic_sub_return(1, (v))
#define atomic_inc_return(v)	atomic_add_return(1, (v))

/*
 * atomic_inc - increment atomic variable
 * @v: pointer of type atomic_t
 *
 * Atomically increments @v by 1.
 */
#define atomic_inc(v) atomic_add(1, (v))

/*
 * atomic_dec - decrement and test
 * @v: pointer of type atomic_t
 *
 * Atomically decrements @v by 1.
 */
#define atomic_dec(v) atomic_sub(1, (v))

#endif /* __CR_ATOMIC_H__ */