sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / kernel / locking / rwsem.c
blob45ba475d4be344b76b1b35e6a174d8f8085788ce
1 /* kernel/rwsem.c: R/W semaphores, public implementation
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from asm-i386/semaphore.h
5 */
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/export.h>
11 #include <linux/rwsem.h>
12 #include <linux/atomic.h>
14 #include "rwsem.h"
17 * lock for reading
19 void __sched down_read(struct rw_semaphore *sem)
21 might_sleep();
22 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
24 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
25 rwsem_set_reader_owned(sem);
28 EXPORT_SYMBOL(down_read);
31 * trylock for reading -- returns 1 if successful, 0 if contention
33 int down_read_trylock(struct rw_semaphore *sem)
35 int ret = __down_read_trylock(sem);
37 if (ret == 1) {
38 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
39 rwsem_set_reader_owned(sem);
41 return ret;
44 EXPORT_SYMBOL(down_read_trylock);
47 * lock for writing
49 void __sched down_write(struct rw_semaphore *sem)
51 might_sleep();
52 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
54 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
55 rwsem_set_owner(sem);
58 EXPORT_SYMBOL(down_write);
61 * lock for writing
63 int __sched down_write_killable(struct rw_semaphore *sem)
65 might_sleep();
66 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
68 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
69 rwsem_release(&sem->dep_map, 1, _RET_IP_);
70 return -EINTR;
73 rwsem_set_owner(sem);
74 return 0;
77 EXPORT_SYMBOL(down_write_killable);
80 * trylock for writing -- returns 1 if successful, 0 if contention
82 int down_write_trylock(struct rw_semaphore *sem)
84 int ret = __down_write_trylock(sem);
86 if (ret == 1) {
87 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
88 rwsem_set_owner(sem);
91 return ret;
94 EXPORT_SYMBOL(down_write_trylock);
97 * release a read lock
99 void up_read(struct rw_semaphore *sem)
101 rwsem_release(&sem->dep_map, 1, _RET_IP_);
103 __up_read(sem);
106 EXPORT_SYMBOL(up_read);
109 * release a write lock
111 void up_write(struct rw_semaphore *sem)
113 rwsem_release(&sem->dep_map, 1, _RET_IP_);
115 rwsem_clear_owner(sem);
116 __up_write(sem);
119 EXPORT_SYMBOL(up_write);
122 * downgrade write lock to read lock
124 void downgrade_write(struct rw_semaphore *sem)
127 * lockdep: a downgraded write will live on as a write
128 * dependency.
130 rwsem_set_reader_owned(sem);
131 __downgrade_write(sem);
134 EXPORT_SYMBOL(downgrade_write);
136 #ifdef CONFIG_DEBUG_LOCK_ALLOC
138 void down_read_nested(struct rw_semaphore *sem, int subclass)
140 might_sleep();
141 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
143 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
144 rwsem_set_reader_owned(sem);
147 EXPORT_SYMBOL(down_read_nested);
149 void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
151 might_sleep();
152 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
154 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
155 rwsem_set_owner(sem);
158 EXPORT_SYMBOL(_down_write_nest_lock);
160 void down_read_non_owner(struct rw_semaphore *sem)
162 might_sleep();
164 __down_read(sem);
167 EXPORT_SYMBOL(down_read_non_owner);
169 void down_write_nested(struct rw_semaphore *sem, int subclass)
171 might_sleep();
172 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
174 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
175 rwsem_set_owner(sem);
178 EXPORT_SYMBOL(down_write_nested);
180 int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
182 might_sleep();
183 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
185 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
186 rwsem_release(&sem->dep_map, 1, _RET_IP_);
187 return -EINTR;
190 rwsem_set_owner(sem);
191 return 0;
194 EXPORT_SYMBOL(down_write_killable_nested);
196 void up_read_non_owner(struct rw_semaphore *sem)
198 __up_read(sem);
201 EXPORT_SYMBOL(up_read_non_owner);
203 #endif