Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1 | /* 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 Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 9 | #include <linux/sched.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 11 | #include <linux/rwsem.h> |
| 12 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * lock for reading |
| 17 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 18 | void __sched down_read(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 19 | { |
| 20 | might_sleep(); |
| 21 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
| 22 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 23 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | EXPORT_SYMBOL(down_read); |
| 27 | |
| 28 | /* |
| 29 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 30 | */ |
| 31 | int 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 | |
| 40 | EXPORT_SYMBOL(down_read_trylock); |
| 41 | |
| 42 | /* |
| 43 | * lock for writing |
| 44 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 45 | void __sched down_write(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 46 | { |
| 47 | might_sleep(); |
| 48 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
| 49 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 50 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | EXPORT_SYMBOL(down_write); |
| 54 | |
| 55 | /* |
| 56 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 57 | */ |
| 58 | int down_write_trylock(struct rw_semaphore *sem) |
| 59 | { |
| 60 | int ret = __down_write_trylock(sem); |
| 61 | |
| 62 | if (ret == 1) |
Pavel Emelianov | 428e6ce | 2007-05-08 00:29:10 -0700 | [diff] [blame] | 63 | rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | EXPORT_SYMBOL(down_write_trylock); |
| 68 | |
| 69 | /* |
| 70 | * release a read lock |
| 71 | */ |
| 72 | void up_read(struct rw_semaphore *sem) |
| 73 | { |
| 74 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 75 | |
| 76 | __up_read(sem); |
| 77 | } |
| 78 | |
| 79 | EXPORT_SYMBOL(up_read); |
| 80 | |
| 81 | /* |
| 82 | * release a write lock |
| 83 | */ |
| 84 | void up_write(struct rw_semaphore *sem) |
| 85 | { |
| 86 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 87 | |
| 88 | __up_write(sem); |
| 89 | } |
| 90 | |
| 91 | EXPORT_SYMBOL(up_write); |
| 92 | |
| 93 | /* |
| 94 | * downgrade write lock to read lock |
| 95 | */ |
| 96 | void 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 | |
| 105 | EXPORT_SYMBOL(downgrade_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 106 | |
| 107 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 108 | |
| 109 | void 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 Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 114 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | EXPORT_SYMBOL(down_read_nested); |
| 118 | |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 119 | void _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 | |
| 127 | EXPORT_SYMBOL(_down_write_nest_lock); |
| 128 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame^] | 129 | void down_read_non_owner(struct rw_semaphore *sem) |
| 130 | { |
| 131 | might_sleep(); |
| 132 | |
| 133 | __down_read(sem); |
| 134 | } |
| 135 | |
| 136 | EXPORT_SYMBOL(down_read_non_owner); |
| 137 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 138 | void 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 Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 143 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | EXPORT_SYMBOL(down_write_nested); |
| 147 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame^] | 148 | void up_read_non_owner(struct rw_semaphore *sem) |
| 149 | { |
| 150 | __up_read(sem); |
| 151 | } |
| 152 | |
| 153 | EXPORT_SYMBOL(up_read_non_owner); |
| 154 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | |