Merge master.kernel.org:/pub/scm/linux/kernel/git/bart/ide-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / rwsem.c
blob9a87886b022eb42ab486cd4749a5da1043a91c36
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/module.h>
10 #include <linux/rwsem.h>
12 #include <asm/system.h>
13 #include <asm/atomic.h>
16 * lock for reading
18 void down_read(struct rw_semaphore *sem)
20 might_sleep();
21 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
23 __down_read(sem);
26 EXPORT_SYMBOL(down_read);
29 * trylock for reading -- returns 1 if successful, 0 if contention
31 int down_read_trylock(struct rw_semaphore *sem)
33 int ret = __down_read_trylock(sem);
35 if (ret == 1)
36 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
37 return ret;
40 EXPORT_SYMBOL(down_read_trylock);
43 * lock for writing
45 void down_write(struct rw_semaphore *sem)
47 might_sleep();
48 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
50 __down_write(sem);
53 EXPORT_SYMBOL(down_write);
56 * trylock for writing -- returns 1 if successful, 0 if contention
58 int down_write_trylock(struct rw_semaphore *sem)
60 int ret = __down_write_trylock(sem);
62 if (ret == 1)
63 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
64 return ret;
67 EXPORT_SYMBOL(down_write_trylock);
70 * release a read lock
72 void up_read(struct rw_semaphore *sem)
74 rwsem_release(&sem->dep_map, 1, _RET_IP_);
76 __up_read(sem);
79 EXPORT_SYMBOL(up_read);
82 * release a write lock
84 void up_write(struct rw_semaphore *sem)
86 rwsem_release(&sem->dep_map, 1, _RET_IP_);
88 __up_write(sem);
91 EXPORT_SYMBOL(up_write);
94 * downgrade write lock to read lock
96 void downgrade_write(struct rw_semaphore *sem)
99 * lockdep: a downgraded write will live on as a write
100 * dependency.
102 __downgrade_write(sem);
105 EXPORT_SYMBOL(downgrade_write);
107 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109 void down_read_nested(struct rw_semaphore *sem, int subclass)
111 might_sleep();
112 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
114 __down_read(sem);
117 EXPORT_SYMBOL(down_read_nested);
119 void down_read_non_owner(struct rw_semaphore *sem)
121 might_sleep();
123 __down_read(sem);
126 EXPORT_SYMBOL(down_read_non_owner);
128 void down_write_nested(struct rw_semaphore *sem, int subclass)
130 might_sleep();
131 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
133 __down_write_nested(sem, subclass);
136 EXPORT_SYMBOL(down_write_nested);
138 void up_read_non_owner(struct rw_semaphore *sem)
140 __up_read(sem);
143 EXPORT_SYMBOL(up_read_non_owner);
145 #endif