blob: 846322d4c35d489daf86e337518dbee79f5c738f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/asm-sh/spinlock.h
3 *
4 * Copyright (C) 2002, 2003 Paul Mundt
5 *
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
8 * for more details.
9 */
10#ifndef __ASM_SH_SPINLOCK_H
11#define __ASM_SH_SPINLOCK_H
12
13#include <asm/atomic.h>
14
15/*
16 * Your basic SMP spinlocks, allowing only a single CPU anywhere
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070019#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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Simple spin lock operations. There are two variants, one clears IRQ's
26 * on the local processor, one does not.
27 *
28 * We make no fairness assumptions. They have a cost.
29 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070030static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 __asm__ __volatile__ (
33 "1:\n\t"
34 "tas.b @%0\n\t"
35 "bf/s 1b\n\t"
36 "nop\n\t"
37 : "=r" (lock->lock)
38 : "r" (&lock->lock)
39 : "t", "memory"
40 );
41}
42
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070043static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 assert_spin_locked(lock);
46
47 lock->lock = 0;
48}
49
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070050#define __raw_spin_trylock(x) (!test_and_set_bit(0, &(x)->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * Read-write spinlocks, allowing multiple readers but only one writer.
54 *
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
58 * read-locks.
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070061static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070063 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 atomic_inc(&rw->counter);
66
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070067 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070070static inline void __raw_read_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070072 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 atomic_dec(&rw->counter);
75
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070076 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070079static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070081 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 atomic_set(&rw->counter, -1);
83}
84
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070085static inline void __raw_write_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 atomic_set(&rw->counter, 0);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070088 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070091#define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070093static inline int __raw_write_trylock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 if (atomic_sub_and_test(RW_LOCK_BIAS, &rw->counter))
96 return 1;
97
98 atomic_add(RW_LOCK_BIAS, &rw->counter);
99
100 return 0;
101}
102
103#endif /* __ASM_SH_SPINLOCK_H */