5 * Written by Ivan Kokshaysky <ink@jurassic.park.msu.ru>, 2001.
6 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
10 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
15 #include <linux/compiler.h>
16 #include <linux/list.h>
17 #include <linux/spinlock.h>
21 extern struct rw_semaphore
*rwsem_down_read_failed(struct rw_semaphore
*sem
);
22 extern struct rw_semaphore
*rwsem_down_write_failed(struct rw_semaphore
*sem
);
23 extern struct rw_semaphore
*rwsem_wake(struct rw_semaphore
*);
24 extern struct rw_semaphore
*rwsem_downgrade_wake(struct rw_semaphore
*sem
);
27 * the semaphore definition
31 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
32 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
33 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
34 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
35 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
36 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
38 struct list_head wait_list
;
45 #define __RWSEM_DEBUG_INIT , 0
47 #define __RWSEM_DEBUG_INIT /* */
50 #define __RWSEM_INITIALIZER(name) \
51 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
52 LIST_HEAD_INIT((name).wait_list) __RWSEM_DEBUG_INIT }
54 #define DECLARE_RWSEM(name) \
55 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
57 static inline void init_rwsem(struct rw_semaphore
*sem
)
59 sem
->count
= RWSEM_UNLOCKED_VALUE
;
60 spin_lock_init(&sem
->wait_lock
);
61 INIT_LIST_HEAD(&sem
->wait_list
);
67 static inline void __down_read(struct rw_semaphore
*sem
)
71 oldcount
= sem
->count
;
72 sem
->count
+= RWSEM_ACTIVE_READ_BIAS
;
84 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
85 :"Ir" (RWSEM_ACTIVE_READ_BIAS
), "m" (sem
->count
) : "memory");
87 if (unlikely(oldcount
< 0))
88 rwsem_down_read_failed(sem
);
92 * trylock for reading -- returns 1 if successful, 0 if contention
94 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
100 new = res
+ RWSEM_ACTIVE_READ_BIAS
;
104 res
= cmpxchg(&sem
->count
, old
, new);
105 } while (res
!= old
);
106 return res
>= 0 ? 1 : 0;
109 static inline void __down_write(struct rw_semaphore
*sem
)
113 oldcount
= sem
->count
;
114 sem
->count
+= RWSEM_ACTIVE_WRITE_BIAS
;
117 __asm__
__volatile__(
126 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
127 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS
), "m" (sem
->count
) : "memory");
129 if (unlikely(oldcount
))
130 rwsem_down_write_failed(sem
);
134 * trylock for writing -- returns 1 if successful, 0 if contention
136 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
138 long ret
= cmpxchg(&sem
->count
, RWSEM_UNLOCKED_VALUE
,
139 RWSEM_ACTIVE_WRITE_BIAS
);
140 if (ret
== RWSEM_UNLOCKED_VALUE
)
145 static inline void __up_read(struct rw_semaphore
*sem
)
149 oldcount
= sem
->count
;
150 sem
->count
-= RWSEM_ACTIVE_READ_BIAS
;
153 __asm__
__volatile__(
162 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
163 :"Ir" (RWSEM_ACTIVE_READ_BIAS
), "m" (sem
->count
) : "memory");
165 if (unlikely(oldcount
< 0))
166 if ((int)oldcount
- RWSEM_ACTIVE_READ_BIAS
== 0)
170 static inline void __up_write(struct rw_semaphore
*sem
)
174 sem
->count
-= RWSEM_ACTIVE_WRITE_BIAS
;
178 __asm__
__volatile__(
188 :"=&r" (count
), "=m" (sem
->count
), "=&r" (temp
)
189 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS
), "m" (sem
->count
) : "memory");
197 * downgrade write lock to read lock
199 static inline void __downgrade_write(struct rw_semaphore
*sem
)
203 oldcount
= sem
->count
;
204 sem
->count
-= RWSEM_WAITING_BIAS
;
207 __asm__
__volatile__(
216 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
217 :"Ir" (-RWSEM_WAITING_BIAS
), "m" (sem
->count
) : "memory");
219 if (unlikely(oldcount
< 0))
220 rwsem_downgrade_wake(sem
);
223 static inline void rwsem_atomic_add(long val
, struct rw_semaphore
*sem
)
229 __asm__
__volatile__(
237 :"=&r" (temp
), "=m" (sem
->count
)
238 :"Ir" (val
), "m" (sem
->count
));
242 static inline long rwsem_atomic_update(long val
, struct rw_semaphore
*sem
)
249 __asm__
__volatile__(
258 :"=&r" (ret
), "=m" (sem
->count
), "=&r" (temp
)
259 :"Ir" (val
), "m" (sem
->count
));
265 static inline int rwsem_is_locked(struct rw_semaphore
*sem
)
267 return (sem
->count
!= 0);
270 #endif /* __KERNEL__ */
271 #endif /* _ALPHA_RWSEM_H */