ipg: remove commented out code
[linux-2.6/kmemtrace.git] / include / asm-sh / rwsem.h
blob1987f3ea7f1b1422d8ae5ad02003d6f6c29a770a
1 /*
2 * include/asm-sh/rwsem.h: R/W semaphores for SH using the stuff
3 * in lib/rwsem.c.
4 */
6 #ifndef _ASM_SH_RWSEM_H
7 #define _ASM_SH_RWSEM_H
9 #ifndef _LINUX_RWSEM_H
10 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
11 #endif
13 #ifdef __KERNEL__
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <asm/atomic.h>
17 #include <asm/system.h>
20 * the semaphore definition
22 struct rw_semaphore {
23 long count;
24 #define RWSEM_UNLOCKED_VALUE 0x00000000
25 #define RWSEM_ACTIVE_BIAS 0x00000001
26 #define RWSEM_ACTIVE_MASK 0x0000ffff
27 #define RWSEM_WAITING_BIAS (-0x00010000)
28 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
29 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
30 spinlock_t wait_lock;
31 struct list_head wait_list;
32 #ifdef CONFIG_DEBUG_LOCK_ALLOC
33 struct lockdep_map dep_map;
34 #endif
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
38 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
39 #else
40 # define __RWSEM_DEP_MAP_INIT(lockname)
41 #endif
43 #define __RWSEM_INITIALIZER(name) \
44 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
45 LIST_HEAD_INIT((name).wait_list) \
46 __RWSEM_DEP_MAP_INIT(name) }
48 #define DECLARE_RWSEM(name) \
49 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
51 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
52 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
53 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
54 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
56 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
57 struct lock_class_key *key);
59 #define init_rwsem(sem) \
60 do { \
61 static struct lock_class_key __key; \
63 __init_rwsem((sem), #sem, &__key); \
64 } while (0)
66 static inline void init_rwsem(struct rw_semaphore *sem)
68 sem->count = RWSEM_UNLOCKED_VALUE;
69 spin_lock_init(&sem->wait_lock);
70 INIT_LIST_HEAD(&sem->wait_list);
74 * lock for reading
76 static inline void __down_read(struct rw_semaphore *sem)
78 if (atomic_inc_return((atomic_t *)(&sem->count)) > 0)
79 smp_wmb();
80 else
81 rwsem_down_read_failed(sem);
84 static inline int __down_read_trylock(struct rw_semaphore *sem)
86 int tmp;
88 while ((tmp = sem->count) >= 0) {
89 if (tmp == cmpxchg(&sem->count, tmp,
90 tmp + RWSEM_ACTIVE_READ_BIAS)) {
91 smp_wmb();
92 return 1;
95 return 0;
99 * lock for writing
101 static inline void __down_write(struct rw_semaphore *sem)
103 int tmp;
105 tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
106 (atomic_t *)(&sem->count));
107 if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
108 smp_wmb();
109 else
110 rwsem_down_write_failed(sem);
113 static inline int __down_write_trylock(struct rw_semaphore *sem)
115 int tmp;
117 tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
118 RWSEM_ACTIVE_WRITE_BIAS);
119 smp_wmb();
120 return tmp == RWSEM_UNLOCKED_VALUE;
124 * unlock after reading
126 static inline void __up_read(struct rw_semaphore *sem)
128 int tmp;
130 smp_wmb();
131 tmp = atomic_dec_return((atomic_t *)(&sem->count));
132 if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
133 rwsem_wake(sem);
137 * unlock after writing
139 static inline void __up_write(struct rw_semaphore *sem)
141 smp_wmb();
142 if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
143 (atomic_t *)(&sem->count)) < 0)
144 rwsem_wake(sem);
148 * implement atomic add functionality
150 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
152 atomic_add(delta, (atomic_t *)(&sem->count));
156 * downgrade write lock to read lock
158 static inline void __downgrade_write(struct rw_semaphore *sem)
160 int tmp;
162 smp_wmb();
163 tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count));
164 if (tmp < 0)
165 rwsem_downgrade_wake(sem);
168 static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
170 __down_write(sem);
174 * implement exchange and add functionality
176 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
178 smp_mb();
179 return atomic_add_return(delta, (atomic_t *)(&sem->count));
182 static inline int rwsem_is_locked(struct rw_semaphore *sem)
184 return (sem->count != 0);
187 #endif /* __KERNEL__ */
188 #endif /* _ASM_SH_RWSEM_H */