Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* rwsem.h: R/W semaphores, public interface |
| 2 | * |
| 3 | * Written by David Howells (dhowells@redhat.com). |
| 4 | * Derived from asm-i386/semaphore.h |
| 5 | */ |
| 6 | |
| 7 | #ifndef _LINUX_RWSEM_H |
| 8 | #define _LINUX_RWSEM_H |
| 9 | |
| 10 | #include <linux/linkage.h> |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
Thomas Gleixner | c16a87c | 2011-01-26 20:05:50 +0000 | [diff] [blame] | 14 | #include <linux/list.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 17 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 19 | struct optimistic_spin_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct rw_semaphore; |
| 21 | |
| 22 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK |
| 23 | #include <linux/rwsem-spinlock.h> /* use a generic implementation */ |
| 24 | #else |
Thomas Gleixner | 1c8ed64 | 2011-01-26 20:05:56 +0000 | [diff] [blame] | 25 | /* All arch specific implementations share the same struct */ |
| 26 | struct rw_semaphore { |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 27 | long count; |
| 28 | raw_spinlock_t wait_lock; |
| 29 | struct list_head wait_list; |
| 30 | #ifdef CONFIG_SMP |
| 31 | /* |
| 32 | * Write owner. Used as a speculative check to see |
| 33 | * if the owner is running on the cpu. |
| 34 | */ |
| 35 | struct task_struct *owner; |
| 36 | struct optimistic_spin_queue *osq; /* spinner MCS lock */ |
| 37 | #endif |
Thomas Gleixner | 1c8ed64 | 2011-01-26 20:05:56 +0000 | [diff] [blame] | 38 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 39 | struct lockdep_map dep_map; |
| 40 | #endif |
| 41 | }; |
| 42 | |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 43 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); |
| 44 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); |
| 45 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *); |
| 46 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); |
Thomas Gleixner | aac7227 | 2011-01-26 20:06:06 +0000 | [diff] [blame] | 47 | |
Thomas Gleixner | 1c8ed64 | 2011-01-26 20:05:56 +0000 | [diff] [blame] | 48 | /* Include the arch specific part */ |
| 49 | #include <asm/rwsem.h> |
Thomas Gleixner | 41e5887 | 2011-01-26 20:06:03 +0000 | [diff] [blame] | 50 | |
| 51 | /* In all implementations count != 0 means locked */ |
| 52 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 53 | { |
| 54 | return sem->count != 0; |
| 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #endif |
| 58 | |
Thomas Gleixner | 12249b3 | 2011-01-26 20:06:00 +0000 | [diff] [blame] | 59 | /* Common initializer macros and functions */ |
| 60 | |
| 61 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 62 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } |
| 63 | #else |
| 64 | # define __RWSEM_DEP_MAP_INIT(lockname) |
| 65 | #endif |
| 66 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 67 | #ifdef CONFIG_SMP |
| 68 | #define __RWSEM_INITIALIZER(name) \ |
| 69 | { RWSEM_UNLOCKED_VALUE, \ |
| 70 | __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \ |
| 71 | LIST_HEAD_INIT((name).wait_list), \ |
| 72 | NULL, /* owner */ \ |
| 73 | NULL /* mcs lock */ \ |
| 74 | __RWSEM_DEP_MAP_INIT(name) } |
| 75 | #else |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 76 | #define __RWSEM_INITIALIZER(name) \ |
| 77 | { RWSEM_UNLOCKED_VALUE, \ |
| 78 | __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \ |
| 79 | LIST_HEAD_INIT((name).wait_list) \ |
| 80 | __RWSEM_DEP_MAP_INIT(name) } |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 81 | #endif |
Thomas Gleixner | 12249b3 | 2011-01-26 20:06:00 +0000 | [diff] [blame] | 82 | |
| 83 | #define DECLARE_RWSEM(name) \ |
| 84 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 85 | |
| 86 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 87 | struct lock_class_key *key); |
| 88 | |
| 89 | #define init_rwsem(sem) \ |
| 90 | do { \ |
| 91 | static struct lock_class_key __key; \ |
| 92 | \ |
| 93 | __init_rwsem((sem), #sem, &__key); \ |
| 94 | } while (0) |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
Josef Bacik | 4a444b1 | 2013-08-30 10:05:22 -0400 | [diff] [blame] | 97 | * This is the same regardless of which rwsem implementation that is being used. |
| 98 | * It is just a heuristic meant to be called by somebody alreadying holding the |
| 99 | * rwsem to see if somebody from an incompatible type is wanting access to the |
| 100 | * lock. |
| 101 | */ |
| 102 | static inline int rwsem_is_contended(struct rw_semaphore *sem) |
| 103 | { |
| 104 | return !list_empty(&sem->wait_list); |
| 105 | } |
| 106 | |
| 107 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * lock for reading |
| 109 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 110 | extern void down_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 114 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 115 | extern int down_read_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * lock for writing |
| 119 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 120 | extern void down_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 124 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 125 | extern int down_write_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * release a read lock |
| 129 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 130 | extern void up_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * release a write lock |
| 134 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 135 | extern void up_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * downgrade write lock to read lock |
| 139 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 140 | extern void downgrade_write(struct rw_semaphore *sem); |
| 141 | |
| 142 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 143 | /* |
Ingo Molnar | 5fca80e | 2006-07-10 04:44:02 -0700 | [diff] [blame] | 144 | * nested locking. NOTE: rwsems are not allowed to recurse |
| 145 | * (which occurs if the same task tries to acquire the same |
| 146 | * lock instance multiple times), but multiple locks of the |
| 147 | * same lock class might be taken, if the order of the locks |
| 148 | * is always the same. This ordering rule can be expressed |
| 149 | * to lockdep via the _nested() APIs, but enumerating the |
| 150 | * subclasses that are used. (If the nesting relationship is |
| 151 | * static then another method for expressing nested locking is |
| 152 | * the explicit definition of lock class keys and the use of |
| 153 | * lockdep_set_class() at lock initialization time. |
| 154 | * See Documentation/lockdep-design.txt for more details.) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 155 | */ |
| 156 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); |
| 157 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 158 | extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); |
| 159 | |
| 160 | # define down_write_nest_lock(sem, nest_lock) \ |
| 161 | do { \ |
| 162 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ |
| 163 | _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ |
| 164 | } while (0); |
| 165 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 166 | /* |
| 167 | * Take/release a lock when not the owner will release it. |
| 168 | * |
| 169 | * [ This API should be avoided as much as possible - the |
| 170 | * proper abstraction for this case is completions. ] |
| 171 | */ |
| 172 | extern void down_read_non_owner(struct rw_semaphore *sem); |
| 173 | extern void up_read_non_owner(struct rw_semaphore *sem); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 174 | #else |
| 175 | # define down_read_nested(sem, subclass) down_read(sem) |
Jiri Kosina | e65b9ad | 2013-01-15 20:12:37 +0100 | [diff] [blame] | 176 | # define down_write_nest_lock(sem, nest_lock) down_write(sem) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 177 | # define down_write_nested(sem, subclass) down_write(sem) |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 178 | # define down_read_non_owner(sem) down_read(sem) |
| 179 | # define up_read_non_owner(sem) up_read(sem) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 180 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | #endif /* _LINUX_RWSEM_H */ |