Linux 3.11-rc3
[cris-mirror.git] / arch / arc / include / asm / spinlock.h
blobf158197ac5b04432ac6beb37c9175629361c0901
1 /*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #ifndef __ASM_SPINLOCK_H
10 #define __ASM_SPINLOCK_H
12 #include <asm/spinlock_types.h>
13 #include <asm/processor.h>
14 #include <asm/barrier.h>
16 #define arch_spin_is_locked(x) ((x)->slock != __ARCH_SPIN_LOCK_UNLOCKED__)
17 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
18 #define arch_spin_unlock_wait(x) \
19 do { while (arch_spin_is_locked(x)) cpu_relax(); } while (0)
21 static inline void arch_spin_lock(arch_spinlock_t *lock)
23 unsigned int tmp = __ARCH_SPIN_LOCK_LOCKED__;
25 __asm__ __volatile__(
26 "1: ex %0, [%1] \n"
27 " breq %0, %2, 1b \n"
28 : "+&r" (tmp)
29 : "r"(&(lock->slock)), "ir"(__ARCH_SPIN_LOCK_LOCKED__)
30 : "memory");
33 static inline int arch_spin_trylock(arch_spinlock_t *lock)
35 unsigned int tmp = __ARCH_SPIN_LOCK_LOCKED__;
37 __asm__ __volatile__(
38 "1: ex %0, [%1] \n"
39 : "+r" (tmp)
40 : "r"(&(lock->slock))
41 : "memory");
43 return (tmp == __ARCH_SPIN_LOCK_UNLOCKED__);
46 static inline void arch_spin_unlock(arch_spinlock_t *lock)
48 lock->slock = __ARCH_SPIN_LOCK_UNLOCKED__;
49 smp_mb();
53 * Read-write spinlocks, allowing multiple readers but only one writer.
55 * The spinlock itself is contained in @counter and access to it is
56 * serialized with @lock_mutex.
58 * Unfair locking as Writers could be starved indefinitely by Reader(s)
61 /* Would read_trylock() succeed? */
62 #define arch_read_can_lock(x) ((x)->counter > 0)
64 /* Would write_trylock() succeed? */
65 #define arch_write_can_lock(x) ((x)->counter == __ARCH_RW_LOCK_UNLOCKED__)
67 /* 1 - lock taken successfully */
68 static inline int arch_read_trylock(arch_rwlock_t *rw)
70 int ret = 0;
72 arch_spin_lock(&(rw->lock_mutex));
75 * zero means writer holds the lock exclusively, deny Reader.
76 * Otherwise grant lock to first/subseq reader
78 if (rw->counter > 0) {
79 rw->counter--;
80 ret = 1;
83 arch_spin_unlock(&(rw->lock_mutex));
85 smp_mb();
86 return ret;
89 /* 1 - lock taken successfully */
90 static inline int arch_write_trylock(arch_rwlock_t *rw)
92 int ret = 0;
94 arch_spin_lock(&(rw->lock_mutex));
97 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
98 * deny writer. Otherwise if unlocked grant to writer
99 * Hence the claim that Linux rwlocks are unfair to writers.
100 * (can be starved for an indefinite time by readers).
102 if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
103 rw->counter = 0;
104 ret = 1;
106 arch_spin_unlock(&(rw->lock_mutex));
108 return ret;
111 static inline void arch_read_lock(arch_rwlock_t *rw)
113 while (!arch_read_trylock(rw))
114 cpu_relax();
117 static inline void arch_write_lock(arch_rwlock_t *rw)
119 while (!arch_write_trylock(rw))
120 cpu_relax();
123 static inline void arch_read_unlock(arch_rwlock_t *rw)
125 arch_spin_lock(&(rw->lock_mutex));
126 rw->counter++;
127 arch_spin_unlock(&(rw->lock_mutex));
130 static inline void arch_write_unlock(arch_rwlock_t *rw)
132 arch_spin_lock(&(rw->lock_mutex));
133 rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
134 arch_spin_unlock(&(rw->lock_mutex));
137 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
138 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
140 #define arch_spin_relax(lock) cpu_relax()
141 #define arch_read_relax(lock) cpu_relax()
142 #define arch_write_relax(lock) cpu_relax()
144 #endif /* __ASM_SPINLOCK_H */