blob: cfff1435bdfb2f1e6d8a88d797c2a205f9145daf [file] [log] [blame]
Ingo Molnarc4e05112006-07-03 00:24:29 -07001/* kernel/rwsem.c: R/W semaphores, public implementation
2 *
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from asm-i386/semaphore.h
5 */
6
7#include <linux/types.h>
8#include <linux/kernel.h>
Livio Soaresc7af77b2007-12-18 15:21:13 +01009#include <linux/sched.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070011#include <linux/rwsem.h>
12
Arun Sharma600634972011-07-26 16:09:06 -070013#include <linux/atomic.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070014
15/*
16 * lock for reading
17 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010018void __sched down_read(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070019{
20 might_sleep();
21 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
22
Peter Zijlstra4fe87742007-07-19 01:48:58 -070023 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Ingo Molnarc4e05112006-07-03 00:24:29 -070024}
25
26EXPORT_SYMBOL(down_read);
27
28/*
29 * trylock for reading -- returns 1 if successful, 0 if contention
30 */
31int down_read_trylock(struct rw_semaphore *sem)
32{
33 int ret = __down_read_trylock(sem);
34
35 if (ret == 1)
36 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
37 return ret;
38}
39
40EXPORT_SYMBOL(down_read_trylock);
41
42/*
43 * lock for writing
44 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010045void __sched down_write(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070046{
47 might_sleep();
48 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
49
Peter Zijlstra4fe87742007-07-19 01:48:58 -070050 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Ingo Molnarc4e05112006-07-03 00:24:29 -070051}
52
53EXPORT_SYMBOL(down_write);
54
55/*
56 * trylock for writing -- returns 1 if successful, 0 if contention
57 */
58int down_write_trylock(struct rw_semaphore *sem)
59{
60 int ret = __down_write_trylock(sem);
61
62 if (ret == 1)
Pavel Emelianov428e6ce2007-05-08 00:29:10 -070063 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
Ingo Molnarc4e05112006-07-03 00:24:29 -070064 return ret;
65}
66
67EXPORT_SYMBOL(down_write_trylock);
68
69/*
70 * release a read lock
71 */
72void up_read(struct rw_semaphore *sem)
73{
74 rwsem_release(&sem->dep_map, 1, _RET_IP_);
75
76 __up_read(sem);
77}
78
79EXPORT_SYMBOL(up_read);
80
81/*
82 * release a write lock
83 */
84void up_write(struct rw_semaphore *sem)
85{
86 rwsem_release(&sem->dep_map, 1, _RET_IP_);
87
88 __up_write(sem);
89}
90
91EXPORT_SYMBOL(up_write);
92
93/*
94 * downgrade write lock to read lock
95 */
96void downgrade_write(struct rw_semaphore *sem)
97{
98 /*
99 * lockdep: a downgraded write will live on as a write
100 * dependency.
101 */
102 __downgrade_write(sem);
103}
104
105EXPORT_SYMBOL(downgrade_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700106
107#ifdef CONFIG_DEBUG_LOCK_ALLOC
108
109void down_read_nested(struct rw_semaphore *sem, int subclass)
110{
111 might_sleep();
112 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
113
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700114 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700115}
116
117EXPORT_SYMBOL(down_read_nested);
118
Jiri Kosina1b963c82013-01-11 14:31:56 -0800119void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
120{
121 might_sleep();
122 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
123
124 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
125}
126
127EXPORT_SYMBOL(_down_write_nest_lock);
128
Kent Overstreet84759c62011-09-21 21:43:05 -0700129void down_read_non_owner(struct rw_semaphore *sem)
130{
131 might_sleep();
132
133 __down_read(sem);
134}
135
136EXPORT_SYMBOL(down_read_non_owner);
137
Ingo Molnar4ea21762006-07-03 00:24:53 -0700138void down_write_nested(struct rw_semaphore *sem, int subclass)
139{
140 might_sleep();
141 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
142
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700143 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700144}
145
146EXPORT_SYMBOL(down_write_nested);
147
Kent Overstreet84759c62011-09-21 21:43:05 -0700148void up_read_non_owner(struct rw_semaphore *sem)
149{
150 __up_read(sem);
151}
152
153EXPORT_SYMBOL(up_read_non_owner);
154
Ingo Molnar4ea21762006-07-03 00:24:53 -0700155#endif
156
157