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 | |
| 13 | #include <asm/system.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 14 | #include <linux/atomic.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * lock for reading |
| 18 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 19 | void __sched down_read(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 20 | { |
| 21 | might_sleep(); |
| 22 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
| 23 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 24 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | EXPORT_SYMBOL(down_read); |
| 28 | |
| 29 | /* |
| 30 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 31 | */ |
| 32 | int down_read_trylock(struct rw_semaphore *sem) |
| 33 | { |
| 34 | int ret = __down_read_trylock(sem); |
| 35 | |
| 36 | if (ret == 1) |
| 37 | rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | EXPORT_SYMBOL(down_read_trylock); |
| 42 | |
| 43 | /* |
| 44 | * lock for writing |
| 45 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 46 | void __sched down_write(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 47 | { |
| 48 | might_sleep(); |
| 49 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
| 50 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 51 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | EXPORT_SYMBOL(down_write); |
| 55 | |
| 56 | /* |
| 57 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 58 | */ |
| 59 | int down_write_trylock(struct rw_semaphore *sem) |
| 60 | { |
| 61 | int ret = __down_write_trylock(sem); |
| 62 | |
| 63 | if (ret == 1) |
Pavel Emelianov | 428e6ce | 2007-05-08 00:29:10 -0700 | [diff] [blame] | 64 | rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | EXPORT_SYMBOL(down_write_trylock); |
| 69 | |
| 70 | /* |
| 71 | * release a read lock |
| 72 | */ |
| 73 | void up_read(struct rw_semaphore *sem) |
| 74 | { |
| 75 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 76 | |
| 77 | __up_read(sem); |
| 78 | } |
| 79 | |
| 80 | EXPORT_SYMBOL(up_read); |
| 81 | |
| 82 | /* |
| 83 | * release a write lock |
| 84 | */ |
| 85 | void up_write(struct rw_semaphore *sem) |
| 86 | { |
| 87 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 88 | |
| 89 | __up_write(sem); |
| 90 | } |
| 91 | |
| 92 | EXPORT_SYMBOL(up_write); |
| 93 | |
| 94 | /* |
| 95 | * downgrade write lock to read lock |
| 96 | */ |
| 97 | void downgrade_write(struct rw_semaphore *sem) |
| 98 | { |
| 99 | /* |
| 100 | * lockdep: a downgraded write will live on as a write |
| 101 | * dependency. |
| 102 | */ |
| 103 | __downgrade_write(sem); |
| 104 | } |
| 105 | |
| 106 | EXPORT_SYMBOL(downgrade_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 107 | |
| 108 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 109 | |
| 110 | void down_read_nested(struct rw_semaphore *sem, int subclass) |
| 111 | { |
| 112 | might_sleep(); |
| 113 | rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_); |
| 114 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 115 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | EXPORT_SYMBOL(down_read_nested); |
| 119 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 120 | void down_write_nested(struct rw_semaphore *sem, int subclass) |
| 121 | { |
| 122 | might_sleep(); |
| 123 | rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_); |
| 124 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 125 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | EXPORT_SYMBOL(down_write_nested); |
| 129 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 130 | #endif |
| 131 | |
| 132 | |