2 * include/asm-sh/spinlock.h
4 * Copyright (C) 2002, 2003 Paul Mundt
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 #ifndef __ASM_SH_SPINLOCK_H
11 #define __ASM_SH_SPINLOCK_H
13 #include <asm/atomic.h>
16 * Your basic SMP spinlocks, allowing only a single CPU anywhere
19 #define __raw_spin_is_locked(x) ((x)->lock != 0)
20 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
21 #define __raw_spin_unlock_wait(x) \
22 do { cpu_relax(); } while (__raw_spin_is_locked(x))
25 * Simple spin lock operations. There are two variants, one clears IRQ's
26 * on the local processor, one does not.
28 * We make no fairness assumptions. They have a cost.
30 static inline void __raw_spin_lock(raw_spinlock_t *lock)
32 __asm__ __volatile__ (
43 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
45 assert_spin_locked(lock);
50 #define __raw_spin_trylock(x) (!test_and_set_bit(0, &(x)->lock))
53 * Read-write spinlocks, allowing multiple readers but only one writer.
55 * NOTE! it is quite common to have readers in interrupts but no interrupt
56 * writers. For those circumstances we can "mix" irq-safe locks - any writer
57 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
61 static inline void __raw_read_lock(raw_rwlock_t *rw)
63 __raw_spin_lock(&rw->lock);
65 atomic_inc(&rw->counter);
67 __raw_spin_unlock(&rw->lock);
70 static inline void __raw_read_unlock(raw_rwlock_t *rw)
72 __raw_spin_lock(&rw->lock);
74 atomic_dec(&rw->counter);
76 __raw_spin_unlock(&rw->lock);
79 static inline void __raw_write_lock(raw_rwlock_t *rw)
81 __raw_spin_lock(&rw->lock);
82 atomic_set(&rw->counter, -1);
85 static inline void __raw_write_unlock(raw_rwlock_t *rw)
87 atomic_set(&rw->counter, 0);
88 __raw_spin_unlock(&rw->lock);
91 #define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
93 static inline int __raw_write_trylock(raw_rwlock_t *rw)
95 if (atomic_sub_and_test(RW_LOCK_BIAS, &rw->counter))
98 atomic_add(RW_LOCK_BIAS, &rw->counter);
103 #endif /* __ASM_SH_SPINLOCK_H */