2 * include/asm-xtensa/spinlock.h
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 #ifndef _XTENSA_SPINLOCK_H
12 #define _XTENSA_SPINLOCK_H
14 #include <asm/barrier.h>
15 #include <asm/processor.h>
20 * There is at most one owner of a spinlock. There are not different
21 * types of spinlock owners like there are for rwlocks (see below).
23 * When trying to obtain a spinlock, the function "spins" forever, or busy-
24 * waits, until the lock is obtained. When spinning, presumably some other
25 * owner will soon give up the spinlock making it available to others. Use
26 * the trylock functions to avoid spinning forever.
30 * 0 nobody owns the spinlock
31 * 1 somebody owns the spinlock
34 #define arch_spin_is_locked(x) ((x)->slock != 0)
36 static inline void arch_spin_lock(arch_spinlock_t *lock)
42 " wsr %0, scompare1\n"
51 /* Returns 1 if the lock is obtained, 0 otherwise. */
53 static inline int arch_spin_trylock(arch_spinlock_t *lock)
59 " wsr %0, scompare1\n"
66 return tmp == 0 ? 1 : 0;
69 static inline void arch_spin_unlock(arch_spinlock_t *lock)
84 * Read-write locks are really a more flexible spinlock. They allow
85 * multiple readers but only one writer. Write ownership is exclusive
86 * (i.e., all other readers and writers are blocked from ownership while
87 * there is a write owner). These rwlocks are unfair to writers. Writers
88 * can be starved for an indefinite time by readers.
92 * 0 nobody owns the rwlock
93 * >0 one or more readers own the rwlock
94 * (the positive value is the actual number of readers)
95 * 0x80000000 one writer owns the rwlock, no other writers, no readers
98 static inline void arch_write_lock(arch_rwlock_t *rw)
102 __asm__ __volatile__(
104 " wsr %0, scompare1\n"
107 " s32c1i %0, %1, 0\n"
114 /* Returns 1 if the lock is obtained, 0 otherwise. */
116 static inline int arch_write_trylock(arch_rwlock_t *rw)
120 __asm__ __volatile__(
122 " wsr %0, scompare1\n"
125 " s32c1i %0, %1, 0\n"
130 return tmp == 0 ? 1 : 0;
133 static inline void arch_write_unlock(arch_rwlock_t *rw)
137 __asm__ __volatile__(
145 static inline void arch_read_lock(arch_rwlock_t *rw)
148 unsigned long result;
150 __asm__ __volatile__(
151 "1: l32i %1, %2, 0\n"
153 " wsr %1, scompare1\n"
155 " s32c1i %0, %2, 0\n"
157 : "=&a" (result), "=&a" (tmp)
162 /* Returns 1 if the lock is obtained, 0 otherwise. */
164 static inline int arch_read_trylock(arch_rwlock_t *rw)
166 unsigned long result;
169 __asm__ __volatile__(
173 " wsr %1, scompare1\n"
174 " s32c1i %0, %2, 0\n"
177 : "=&a" (result), "=&a" (tmp)
184 static inline void arch_read_unlock(arch_rwlock_t *rw)
186 unsigned long tmp1, tmp2;
188 __asm__ __volatile__(
189 "1: l32i %1, %2, 0\n"
191 " wsr %1, scompare1\n"
192 " s32c1i %0, %2, 0\n"
194 : "=&a" (tmp1), "=&a" (tmp2)
199 #endif /* _XTENSA_SPINLOCK_H */