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