aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrea Bastoni <bastoni@cs.unc.edu>2010-04-19 19:28:07 -0400
committerAndrea Bastoni <bastoni@cs.unc.edu>2010-04-19 19:28:07 -0400
commit09740315d4b914203c25c56e8f8909b4fae7b32d (patch)
tree608d52c4b9c0ec6eacbbb6f6a77b8fd3ce672e8f
parent60dd9a5ff55f32cf34041218818307a4c7b5d78a (diff)
Add atomic operations on x86_642010.1
-rw-r--r--include/asm_x86.h66
1 files changed, 64 insertions, 2 deletions
diff --git a/include/asm_x86.h b/include/asm_x86.h
index 6636abb..ccc6cce 100644
--- a/include/asm_x86.h
+++ b/include/asm_x86.h
@@ -27,10 +27,10 @@ static inline void sti(void) {
27 asm volatile("sti": : :"memory"); 27 asm volatile("sti": : :"memory");
28} 28}
29 29
30#ifdef __i386__
31
32typedef struct { int counter; } atomic_t; 30typedef struct { int counter; } atomic_t;
33 31
32#ifdef __i386__
33
34#define ATOMIC_INIT(i) { (i) } 34#define ATOMIC_INIT(i) { (i) }
35 35
36/** 36/**
@@ -78,5 +78,67 @@ static __inline__ int atomic_add_return(int i, atomic_t *v)
78 78
79#define atomic_inc_return(v) (atomic_add_return(1,v)) 79#define atomic_inc_return(v) (atomic_add_return(1,v))
80 80
81#elif defined(__x86_64__)
82
83/* almost the same as i386, but extra care must be taken when
84 * specifying clobbered registers
85 */
86
87#define ATOMIC_INIT(i) { (i) }
88
89/**
90 * atomic_read - read atomic variable
91 * @v: pointer of type atomic_t
92 *
93 * Atomically reads the value of @v.
94 */
95static inline int atomic_read(const atomic_t *v)
96{
97 return v->counter;
98}
99
100/**
101 * atomic_set - set atomic variable
102 * @v: pointer of type atomic_t
103 * @i: required value
104 *
105 * Atomically sets the value of @v to @i.
106 */
107static inline void atomic_set(atomic_t *v, int i)
108{
109 v->counter = i;
110}
111
112/**
113 * atomic_add - add integer to atomic variable
114 * @i: integer value to add
115 * @v: pointer of type atomic_t
116 *
117 * Atomically adds @i to @v.
118 */
119static inline void atomic_add(int i, atomic_t *v)
120{
121 asm volatile("lock; addl %1,%0"
122 : "=m" (v->counter)
123 : "ir" (i), "m" (v->counter));
124}
125
126/**
127 * atomic_add_return - add and return
128 * @i: integer value to add
129 * @v: pointer of type atomic_t
130 *
131 * Atomically adds @i to @v and returns @i + @v
132 */
133static inline int atomic_add_return(int i, atomic_t *v)
134{
135 int __i = i;
136 asm volatile("lock; xaddl %0, %1"
137 : "+r" (i), "+m" (v->counter)
138 : : "memory");
139 return i + __i;
140}
141
142#define atomic_inc_return(v) (atomic_add_return(1, v))
81 143
82#endif 144#endif