[PATCH] pcmcia: remove client services version (fix)
[linux-2.6/sactl.git] / include / asm-x86_64 / rwsem.h
blobc002175b6e82f78a3aa7fe89396a97cd4ca2b2fd
1 /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for x86_64+
3 * Written by David Howells (dhowells@redhat.com).
4 * Ported by Andi Kleen <ak@suse.de> to x86-64.
6 * Derived from asm-i386/semaphore.h and asm-i386/rwsem.h
9 * The MSW of the count is the negated number of active writers and waiting
10 * lockers, and the LSW is the total number of active locks
12 * The lock count is initialized to 0 (no active and no waiting lockers).
14 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
15 * uncontended lock. This can be determined because XADD returns the old value.
16 * Readers increment by 1 and see a positive value when uncontended, negative
17 * if there are writers (and maybe) readers waiting (in which case it goes to
18 * sleep).
20 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
21 * be extended to 65534 by manually checking the whole MSW rather than relying
22 * on the S flag.
24 * The value of ACTIVE_BIAS supports up to 65535 active processes.
26 * This should be totally fair - if anything is waiting, a process that wants a
27 * lock will go to the back of the queue. When the currently active lock is
28 * released, if there's a writer at the front of the queue, then that and only
29 * that will be woken up; if there's a bunch of consecutive readers at the
30 * front, then they'll all be woken up, but no other readers will be.
33 #ifndef _X8664_RWSEM_H
34 #define _X8664_RWSEM_H
36 #ifndef _LINUX_RWSEM_H
37 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
38 #endif
40 #ifdef __KERNEL__
42 #include <linux/list.h>
43 #include <linux/spinlock.h>
45 struct rwsem_waiter;
47 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
48 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
49 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
50 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
53 * the semaphore definition
55 struct rw_semaphore {
56 signed int count;
57 #define RWSEM_UNLOCKED_VALUE 0x00000000
58 #define RWSEM_ACTIVE_BIAS 0x00000001
59 #define RWSEM_ACTIVE_MASK 0x0000ffff
60 #define RWSEM_WAITING_BIAS (-0x00010000)
61 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
62 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
63 spinlock_t wait_lock;
64 struct list_head wait_list;
65 #if RWSEM_DEBUG
66 int debug;
67 #endif
71 * initialisation
73 #if RWSEM_DEBUG
74 #define __RWSEM_DEBUG_INIT , 0
75 #else
76 #define __RWSEM_DEBUG_INIT /* */
77 #endif
79 #define __RWSEM_INITIALIZER(name) \
80 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
81 __RWSEM_DEBUG_INIT }
83 #define DECLARE_RWSEM(name) \
84 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
86 static inline void init_rwsem(struct rw_semaphore *sem)
88 sem->count = RWSEM_UNLOCKED_VALUE;
89 spin_lock_init(&sem->wait_lock);
90 INIT_LIST_HEAD(&sem->wait_list);
91 #if RWSEM_DEBUG
92 sem->debug = 0;
93 #endif
97 * lock for reading
99 static inline void __down_read(struct rw_semaphore *sem)
101 __asm__ __volatile__(
102 "# beginning down_read\n\t"
103 LOCK_PREFIX " incl (%%rdi)\n\t" /* adds 0x00000001, returns the old value */
104 " js 2f\n\t" /* jump if we weren't granted the lock */
105 "1:\n\t"
106 LOCK_SECTION_START("") \
107 "2:\n\t"
108 " call rwsem_down_read_failed_thunk\n\t"
109 " jmp 1b\n"
110 LOCK_SECTION_END \
111 "# ending down_read\n\t"
112 : "+m"(sem->count)
113 : "D"(sem)
114 : "memory", "cc");
119 * trylock for reading -- returns 1 if successful, 0 if contention
121 static inline int __down_read_trylock(struct rw_semaphore *sem)
123 __s32 result, tmp;
124 __asm__ __volatile__(
125 "# beginning __down_read_trylock\n\t"
126 " movl %0,%1\n\t"
127 "1:\n\t"
128 " movl %1,%2\n\t"
129 " addl %3,%2\n\t"
130 " jle 2f\n\t"
131 LOCK_PREFIX " cmpxchgl %2,%0\n\t"
132 " jnz 1b\n\t"
133 "2:\n\t"
134 "# ending __down_read_trylock\n\t"
135 : "+m"(sem->count), "=&a"(result), "=&r"(tmp)
136 : "i"(RWSEM_ACTIVE_READ_BIAS)
137 : "memory", "cc");
138 return result>=0 ? 1 : 0;
143 * lock for writing
145 static inline void __down_write(struct rw_semaphore *sem)
147 int tmp;
149 tmp = RWSEM_ACTIVE_WRITE_BIAS;
150 __asm__ __volatile__(
151 "# beginning down_write\n\t"
152 LOCK_PREFIX " xaddl %0,(%%rdi)\n\t" /* subtract 0x0000ffff, returns the old value */
153 " testl %0,%0\n\t" /* was the count 0 before? */
154 " jnz 2f\n\t" /* jump if we weren't granted the lock */
155 "1:\n\t"
156 LOCK_SECTION_START("")
157 "2:\n\t"
158 " call rwsem_down_write_failed_thunk\n\t"
159 " jmp 1b\n"
160 LOCK_SECTION_END
161 "# ending down_write"
162 : "=&r" (tmp)
163 : "0"(tmp), "D"(sem)
164 : "memory", "cc");
168 * trylock for writing -- returns 1 if successful, 0 if contention
170 static inline int __down_write_trylock(struct rw_semaphore *sem)
172 signed long ret = cmpxchg(&sem->count,
173 RWSEM_UNLOCKED_VALUE,
174 RWSEM_ACTIVE_WRITE_BIAS);
175 if (ret == RWSEM_UNLOCKED_VALUE)
176 return 1;
177 return 0;
181 * unlock after reading
183 static inline void __up_read(struct rw_semaphore *sem)
185 __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
186 __asm__ __volatile__(
187 "# beginning __up_read\n\t"
188 LOCK_PREFIX " xaddl %[tmp],(%%rdi)\n\t" /* subtracts 1, returns the old value */
189 " js 2f\n\t" /* jump if the lock is being waited upon */
190 "1:\n\t"
191 LOCK_SECTION_START("")
192 "2:\n\t"
193 " decw %w[tmp]\n\t" /* do nothing if still outstanding active readers */
194 " jnz 1b\n\t"
195 " call rwsem_wake_thunk\n\t"
196 " jmp 1b\n"
197 LOCK_SECTION_END
198 "# ending __up_read\n"
199 : "+m"(sem->count), [tmp] "+r" (tmp)
200 : "D"(sem)
201 : "memory", "cc");
205 * unlock after writing
207 static inline void __up_write(struct rw_semaphore *sem)
209 unsigned tmp;
210 __asm__ __volatile__(
211 "# beginning __up_write\n\t"
212 " movl %[bias],%[tmp]\n\t"
213 LOCK_PREFIX " xaddl %[tmp],(%%rdi)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
214 " jnz 2f\n\t" /* jump if the lock is being waited upon */
215 "1:\n\t"
216 LOCK_SECTION_START("")
217 "2:\n\t"
218 " decw %w[tmp]\n\t" /* did the active count reduce to 0? */
219 " jnz 1b\n\t" /* jump back if not */
220 " call rwsem_wake_thunk\n\t"
221 " jmp 1b\n"
222 LOCK_SECTION_END
223 "# ending __up_write\n"
224 : "+m"(sem->count), [tmp] "=r" (tmp)
225 : "D"(sem), [bias] "i"(-RWSEM_ACTIVE_WRITE_BIAS)
226 : "memory", "cc");
230 * downgrade write lock to read lock
232 static inline void __downgrade_write(struct rw_semaphore *sem)
234 __asm__ __volatile__(
235 "# beginning __downgrade_write\n\t"
236 LOCK_PREFIX " addl %[bias],(%%rdi)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
237 " js 2f\n\t" /* jump if the lock is being waited upon */
238 "1:\n\t"
239 LOCK_SECTION_START("")
240 "2:\n\t"
241 " call rwsem_downgrade_thunk\n"
242 " jmp 1b\n"
243 LOCK_SECTION_END
244 "# ending __downgrade_write\n"
245 : "=m"(sem->count)
246 : "D"(sem), [bias] "i"(-RWSEM_WAITING_BIAS), "m"(sem->count)
247 : "memory", "cc");
251 * implement atomic add functionality
253 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
255 __asm__ __volatile__(
256 LOCK_PREFIX "addl %1,%0"
257 :"=m"(sem->count)
258 :"ir"(delta), "m"(sem->count));
262 * implement exchange and add functionality
264 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
266 int tmp = delta;
268 __asm__ __volatile__(
269 LOCK_PREFIX "xaddl %0,(%2)"
270 : "=r"(tmp), "=m"(sem->count)
271 : "r"(sem), "m"(sem->count), "0" (tmp)
272 : "memory");
274 return tmp+delta;
277 #endif /* __KERNEL__ */
278 #endif /* _X8664_RWSEM_H */