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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/system.h> |
| 18 | #include <asm/atomic.h> |
| 19 | |
| 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 { |
| 27 | long count; |
| 28 | spinlock_t wait_lock; |
| 29 | struct list_head wait_list; |
| 30 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 31 | struct lockdep_map dep_map; |
| 32 | #endif |
| 33 | }; |
| 34 | |
| 35 | /* Include the arch specific part */ |
| 36 | #include <asm/rwsem.h> |
Thomas Gleixner | 41e5887 | 2011-01-26 20:06:03 +0000 | [diff] [blame^] | 37 | |
| 38 | /* In all implementations count != 0 means locked */ |
| 39 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 40 | { |
| 41 | return sem->count != 0; |
| 42 | } |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Thomas Gleixner | 12249b3 | 2011-01-26 20:06:00 +0000 | [diff] [blame] | 46 | /* Common initializer macros and functions */ |
| 47 | |
| 48 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 49 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } |
| 50 | #else |
| 51 | # define __RWSEM_DEP_MAP_INIT(lockname) |
| 52 | #endif |
| 53 | |
| 54 | #define __RWSEM_INITIALIZER(name) \ |
| 55 | { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED(name.wait_lock), \ |
| 56 | LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) } |
| 57 | |
| 58 | #define DECLARE_RWSEM(name) \ |
| 59 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 60 | |
| 61 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 62 | struct lock_class_key *key); |
| 63 | |
| 64 | #define init_rwsem(sem) \ |
| 65 | do { \ |
| 66 | static struct lock_class_key __key; \ |
| 67 | \ |
| 68 | __init_rwsem((sem), #sem, &__key); \ |
| 69 | } while (0) |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* |
| 72 | * lock for reading |
| 73 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 74 | extern void down_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 78 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 79 | extern int down_read_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * lock for writing |
| 83 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 84 | extern void down_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 88 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 89 | extern int down_write_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * release a read lock |
| 93 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 94 | extern void up_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * release a write lock |
| 98 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 99 | extern void up_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * downgrade write lock to read lock |
| 103 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 104 | extern void downgrade_write(struct rw_semaphore *sem); |
| 105 | |
| 106 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 107 | /* |
Ingo Molnar | 5fca80e | 2006-07-10 04:44:02 -0700 | [diff] [blame] | 108 | * nested locking. NOTE: rwsems are not allowed to recurse |
| 109 | * (which occurs if the same task tries to acquire the same |
| 110 | * lock instance multiple times), but multiple locks of the |
| 111 | * same lock class might be taken, if the order of the locks |
| 112 | * is always the same. This ordering rule can be expressed |
| 113 | * to lockdep via the _nested() APIs, but enumerating the |
| 114 | * subclasses that are used. (If the nesting relationship is |
| 115 | * static then another method for expressing nested locking is |
| 116 | * the explicit definition of lock class keys and the use of |
| 117 | * lockdep_set_class() at lock initialization time. |
| 118 | * See Documentation/lockdep-design.txt for more details.) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 119 | */ |
| 120 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); |
| 121 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); |
| 122 | /* |
Ingo Molnar | 5fca80e | 2006-07-10 04:44:02 -0700 | [diff] [blame] | 123 | * Take/release a lock when not the owner will release it. |
| 124 | * |
| 125 | * [ This API should be avoided as much as possible - the |
| 126 | * proper abstraction for this case is completions. ] |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 127 | */ |
| 128 | extern void down_read_non_owner(struct rw_semaphore *sem); |
| 129 | extern void up_read_non_owner(struct rw_semaphore *sem); |
| 130 | #else |
| 131 | # define down_read_nested(sem, subclass) down_read(sem) |
| 132 | # define down_write_nested(sem, subclass) down_write(sem) |
| 133 | # define down_read_non_owner(sem) down_read(sem) |
| 134 | # define up_read_non_owner(sem) up_read(sem) |
| 135 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #endif /* _LINUX_RWSEM_H */ |