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