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/sched/debug.h>
11 #include <linux/export.h>
12 #include <linux/rwsem.h>
13 #include <linux/atomic.h>
20 void __sched
down_read(struct rw_semaphore
*sem
)
23 rwsem_acquire_read(&sem
->dep_map
, 0, 0, _RET_IP_
);
25 LOCK_CONTENDED(sem
, __down_read_trylock
, __down_read
);
26 rwsem_set_reader_owned(sem
);
29 EXPORT_SYMBOL(down_read
);
32 * trylock for reading -- returns 1 if successful, 0 if contention
34 int down_read_trylock(struct rw_semaphore
*sem
)
36 int ret
= __down_read_trylock(sem
);
39 rwsem_acquire_read(&sem
->dep_map
, 0, 1, _RET_IP_
);
40 rwsem_set_reader_owned(sem
);
45 EXPORT_SYMBOL(down_read_trylock
);
50 void __sched
down_write(struct rw_semaphore
*sem
)
53 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
55 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
59 EXPORT_SYMBOL(down_write
);
64 int __sched
down_write_killable(struct rw_semaphore
*sem
)
67 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
69 if (LOCK_CONTENDED_RETURN(sem
, __down_write_trylock
, __down_write_killable
)) {
70 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
78 EXPORT_SYMBOL(down_write_killable
);
81 * trylock for writing -- returns 1 if successful, 0 if contention
83 int down_write_trylock(struct rw_semaphore
*sem
)
85 int ret
= __down_write_trylock(sem
);
88 rwsem_acquire(&sem
->dep_map
, 0, 1, _RET_IP_
);
95 EXPORT_SYMBOL(down_write_trylock
);
100 void up_read(struct rw_semaphore
*sem
)
102 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
107 EXPORT_SYMBOL(up_read
);
110 * release a write lock
112 void up_write(struct rw_semaphore
*sem
)
114 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
116 rwsem_clear_owner(sem
);
120 EXPORT_SYMBOL(up_write
);
123 * downgrade write lock to read lock
125 void downgrade_write(struct rw_semaphore
*sem
)
128 * lockdep: a downgraded write will live on as a write
131 rwsem_set_reader_owned(sem
);
132 __downgrade_write(sem
);
135 EXPORT_SYMBOL(downgrade_write
);
137 #ifdef CONFIG_DEBUG_LOCK_ALLOC
139 void down_read_nested(struct rw_semaphore
*sem
, int subclass
)
142 rwsem_acquire_read(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
144 LOCK_CONTENDED(sem
, __down_read_trylock
, __down_read
);
145 rwsem_set_reader_owned(sem
);
148 EXPORT_SYMBOL(down_read_nested
);
150 void _down_write_nest_lock(struct rw_semaphore
*sem
, struct lockdep_map
*nest
)
153 rwsem_acquire_nest(&sem
->dep_map
, 0, 0, nest
, _RET_IP_
);
155 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
156 rwsem_set_owner(sem
);
159 EXPORT_SYMBOL(_down_write_nest_lock
);
161 void down_read_non_owner(struct rw_semaphore
*sem
)
168 EXPORT_SYMBOL(down_read_non_owner
);
170 void down_write_nested(struct rw_semaphore
*sem
, int subclass
)
173 rwsem_acquire(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
175 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
176 rwsem_set_owner(sem
);
179 EXPORT_SYMBOL(down_write_nested
);
181 int __sched
down_write_killable_nested(struct rw_semaphore
*sem
, int subclass
)
184 rwsem_acquire(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
186 if (LOCK_CONTENDED_RETURN(sem
, __down_write_trylock
, __down_write_killable
)) {
187 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
191 rwsem_set_owner(sem
);
195 EXPORT_SYMBOL(down_write_killable_nested
);
197 void up_read_non_owner(struct rw_semaphore
*sem
)
202 EXPORT_SYMBOL(up_read_non_owner
);