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
);
27 EXPORT_SYMBOL(down_read
);
30 * trylock for reading -- returns 1 if successful, 0 if contention
32 int down_read_trylock(struct rw_semaphore
*sem
)
34 int ret
= __down_read_trylock(sem
);
37 rwsem_acquire_read(&sem
->dep_map
, 0, 1, _RET_IP_
);
41 EXPORT_SYMBOL(down_read_trylock
);
46 void __sched
down_write(struct rw_semaphore
*sem
)
49 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
51 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
55 EXPORT_SYMBOL(down_write
);
58 * trylock for writing -- returns 1 if successful, 0 if contention
60 int down_write_trylock(struct rw_semaphore
*sem
)
62 int ret
= __down_write_trylock(sem
);
65 rwsem_acquire(&sem
->dep_map
, 0, 1, _RET_IP_
);
72 EXPORT_SYMBOL(down_write_trylock
);
77 void up_read(struct rw_semaphore
*sem
)
79 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
84 EXPORT_SYMBOL(up_read
);
87 * release a write lock
89 void up_write(struct rw_semaphore
*sem
)
91 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
93 rwsem_clear_owner(sem
);
97 EXPORT_SYMBOL(up_write
);
100 * downgrade write lock to read lock
102 void downgrade_write(struct rw_semaphore
*sem
)
105 * lockdep: a downgraded write will live on as a write
108 rwsem_clear_owner(sem
);
109 __downgrade_write(sem
);
112 EXPORT_SYMBOL(downgrade_write
);
114 #ifdef CONFIG_DEBUG_LOCK_ALLOC
116 void down_read_nested(struct rw_semaphore
*sem
, int subclass
)
119 rwsem_acquire_read(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
121 LOCK_CONTENDED(sem
, __down_read_trylock
, __down_read
);
124 EXPORT_SYMBOL(down_read_nested
);
126 void _down_write_nest_lock(struct rw_semaphore
*sem
, struct lockdep_map
*nest
)
129 rwsem_acquire_nest(&sem
->dep_map
, 0, 0, nest
, _RET_IP_
);
131 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
132 rwsem_set_owner(sem
);
135 EXPORT_SYMBOL(_down_write_nest_lock
);
137 void down_read_non_owner(struct rw_semaphore
*sem
)
144 EXPORT_SYMBOL(down_read_non_owner
);
146 void down_write_nested(struct rw_semaphore
*sem
, int subclass
)
149 rwsem_acquire(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
151 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
152 rwsem_set_owner(sem
);
155 EXPORT_SYMBOL(down_write_nested
);
157 void up_read_non_owner(struct rw_semaphore
*sem
)
162 EXPORT_SYMBOL(up_read_non_owner
);