]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * include/asm-ppc/rwsem.h: R/W semaphores for SH using the stuff | |
3 | * in lib/rwsem.c. | |
4 | */ | |
5 | ||
6 | #ifndef _ASM_SH_RWSEM_H | |
7 | #define _ASM_SH_RWSEM_H | |
8 | ||
9 | #ifdef __KERNEL__ | |
10 | #include <linux/list.h> | |
11 | #include <linux/spinlock.h> | |
12 | #include <asm/atomic.h> | |
13 | #include <asm/system.h> | |
14 | ||
15 | /* | |
16 | * the semaphore definition | |
17 | */ | |
18 | struct rw_semaphore { | |
19 | long count; | |
20 | #define RWSEM_UNLOCKED_VALUE 0x00000000 | |
21 | #define RWSEM_ACTIVE_BIAS 0x00000001 | |
22 | #define RWSEM_ACTIVE_MASK 0x0000ffff | |
23 | #define RWSEM_WAITING_BIAS (-0x00010000) | |
24 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS | |
25 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) | |
26 | spinlock_t wait_lock; | |
27 | struct list_head wait_list; | |
1da177e4 LT |
28 | }; |
29 | ||
1da177e4 LT |
30 | #define __RWSEM_INITIALIZER(name) \ |
31 | { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \ | |
61f4c3d6 | 32 | LIST_HEAD_INIT((name).wait_list) } |
1da177e4 LT |
33 | |
34 | #define DECLARE_RWSEM(name) \ | |
35 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | |
36 | ||
37 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); | |
38 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); | |
39 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem); | |
40 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); | |
41 | ||
42 | static inline void init_rwsem(struct rw_semaphore *sem) | |
43 | { | |
44 | sem->count = RWSEM_UNLOCKED_VALUE; | |
45 | spin_lock_init(&sem->wait_lock); | |
46 | INIT_LIST_HEAD(&sem->wait_list); | |
1da177e4 LT |
47 | } |
48 | ||
49 | /* | |
50 | * lock for reading | |
51 | */ | |
52 | static inline void __down_read(struct rw_semaphore *sem) | |
53 | { | |
54 | if (atomic_inc_return((atomic_t *)(&sem->count)) > 0) | |
55 | smp_wmb(); | |
56 | else | |
57 | rwsem_down_read_failed(sem); | |
58 | } | |
59 | ||
60 | static inline int __down_read_trylock(struct rw_semaphore *sem) | |
61 | { | |
62 | int tmp; | |
63 | ||
64 | while ((tmp = sem->count) >= 0) { | |
65 | if (tmp == cmpxchg(&sem->count, tmp, | |
66 | tmp + RWSEM_ACTIVE_READ_BIAS)) { | |
67 | smp_wmb(); | |
68 | return 1; | |
69 | } | |
70 | } | |
71 | return 0; | |
72 | } | |
73 | ||
74 | /* | |
75 | * lock for writing | |
76 | */ | |
77 | static inline void __down_write(struct rw_semaphore *sem) | |
78 | { | |
79 | int tmp; | |
80 | ||
81 | tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS, | |
82 | (atomic_t *)(&sem->count)); | |
83 | if (tmp == RWSEM_ACTIVE_WRITE_BIAS) | |
84 | smp_wmb(); | |
85 | else | |
86 | rwsem_down_write_failed(sem); | |
87 | } | |
88 | ||
89 | static inline int __down_write_trylock(struct rw_semaphore *sem) | |
90 | { | |
91 | int tmp; | |
92 | ||
93 | tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE, | |
94 | RWSEM_ACTIVE_WRITE_BIAS); | |
95 | smp_wmb(); | |
96 | return tmp == RWSEM_UNLOCKED_VALUE; | |
97 | } | |
98 | ||
99 | /* | |
100 | * unlock after reading | |
101 | */ | |
102 | static inline void __up_read(struct rw_semaphore *sem) | |
103 | { | |
104 | int tmp; | |
105 | ||
106 | smp_wmb(); | |
107 | tmp = atomic_dec_return((atomic_t *)(&sem->count)); | |
108 | if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0) | |
109 | rwsem_wake(sem); | |
110 | } | |
111 | ||
112 | /* | |
113 | * unlock after writing | |
114 | */ | |
115 | static inline void __up_write(struct rw_semaphore *sem) | |
116 | { | |
117 | smp_wmb(); | |
118 | if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS, | |
119 | (atomic_t *)(&sem->count)) < 0) | |
120 | rwsem_wake(sem); | |
121 | } | |
122 | ||
123 | /* | |
124 | * implement atomic add functionality | |
125 | */ | |
126 | static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem) | |
127 | { | |
128 | atomic_add(delta, (atomic_t *)(&sem->count)); | |
129 | } | |
130 | ||
131 | /* | |
132 | * downgrade write lock to read lock | |
133 | */ | |
134 | static inline void __downgrade_write(struct rw_semaphore *sem) | |
135 | { | |
136 | int tmp; | |
137 | ||
138 | smp_wmb(); | |
139 | tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count)); | |
140 | if (tmp < 0) | |
141 | rwsem_downgrade_wake(sem); | |
142 | } | |
143 | ||
144 | /* | |
145 | * implement exchange and add functionality | |
146 | */ | |
147 | static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem) | |
148 | { | |
149 | smp_mb(); | |
150 | return atomic_add_return(delta, (atomic_t *)(&sem->count)); | |
151 | } | |
152 | ||
eb92f4ef RVR |
153 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
154 | { | |
155 | return (sem->count != 0); | |
156 | } | |
157 | ||
1da177e4 LT |
158 | #endif /* __KERNEL__ */ |
159 | #endif /* _ASM_SH_RWSEM_H */ |