1 /* kernel/rwsem.c: R/W semaphores, public implementation
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from asm-i386/semaphore.h
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>
19 void __sched
down_read(struct rw_semaphore
*sem
)
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
);
38 rwsem_acquire_read(&sem
->dep_map
, 0, 1, _RET_IP_
);
39 rwsem_set_reader_owned(sem
);
44 EXPORT_SYMBOL(down_read_trylock
);
49 void __sched
down_write(struct rw_semaphore
*sem
)
52 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
54 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
58 EXPORT_SYMBOL(down_write
);
63 int __sched
down_write_killable(struct rw_semaphore
*sem
)
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_
);
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
);
87 rwsem_acquire(&sem
->dep_map
, 0, 1, _RET_IP_
);
94 EXPORT_SYMBOL(down_write_trylock
);
99 void up_read(struct rw_semaphore
*sem
)
101 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
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
);
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
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
)
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
)
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
)
167 EXPORT_SYMBOL(down_read_non_owner
);
169 void down_write_nested(struct rw_semaphore
*sem
, int subclass
)
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
)
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_
);
190 rwsem_set_owner(sem
);
194 EXPORT_SYMBOL(down_write_killable_nested
);
196 void up_read_non_owner(struct rw_semaphore
*sem
)
201 EXPORT_SYMBOL(up_read_non_owner
);