2 * include/asm-sh/rwsem.h: R/W semaphores for SH using the stuff
6 #ifndef _ASM_SH_RWSEM_H
7 #define _ASM_SH_RWSEM_H
10 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <asm/atomic.h>
17 #include <asm/system.h>
20 * the semaphore definition
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)
31 struct list_head wait_list
;
32 #ifdef CONFIG_DEBUG_LOCK_ALLOC
33 struct lockdep_map dep_map
;
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
38 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
40 # define __RWSEM_DEP_MAP_INIT(lockname)
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) \
61 static struct lock_class_key __key; \
63 __init_rwsem((sem), #sem, &__key); \
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
);
76 static inline void __down_read(struct rw_semaphore
*sem
)
78 if (atomic_inc_return((atomic_t
*)(&sem
->count
)) > 0)
81 rwsem_down_read_failed(sem
);
84 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
88 while ((tmp
= sem
->count
) >= 0) {
89 if (tmp
== cmpxchg(&sem
->count
, tmp
,
90 tmp
+ RWSEM_ACTIVE_READ_BIAS
)) {
101 static inline void __down_write(struct rw_semaphore
*sem
)
105 tmp
= atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS
,
106 (atomic_t
*)(&sem
->count
));
107 if (tmp
== RWSEM_ACTIVE_WRITE_BIAS
)
110 rwsem_down_write_failed(sem
);
113 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
117 tmp
= cmpxchg(&sem
->count
, RWSEM_UNLOCKED_VALUE
,
118 RWSEM_ACTIVE_WRITE_BIAS
);
120 return tmp
== RWSEM_UNLOCKED_VALUE
;
124 * unlock after reading
126 static inline void __up_read(struct rw_semaphore
*sem
)
131 tmp
= atomic_dec_return((atomic_t
*)(&sem
->count
));
132 if (tmp
< -1 && (tmp
& RWSEM_ACTIVE_MASK
) == 0)
137 * unlock after writing
139 static inline void __up_write(struct rw_semaphore
*sem
)
142 if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS
,
143 (atomic_t
*)(&sem
->count
)) < 0)
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
)
163 tmp
= atomic_add_return(-RWSEM_WAITING_BIAS
, (atomic_t
*)(&sem
->count
));
165 rwsem_downgrade_wake(sem
);
168 static inline void __down_write_nested(struct rw_semaphore
*sem
, int subclass
)
174 * implement exchange and add functionality
176 static inline int rwsem_atomic_update(int delta
, struct rw_semaphore
*sem
)
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 */