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> |
| 14 | #include <asm/system.h> |
| 15 | #include <asm/atomic.h> |
| 16 | |
| 17 | struct rw_semaphore; |
| 18 | |
| 19 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK |
| 20 | #include <linux/rwsem-spinlock.h> /* use a generic implementation */ |
| 21 | #else |
| 22 | #include <asm/rwsem.h> /* use an arch-specific implementation */ |
| 23 | #endif |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | /* |
| 26 | * lock for reading |
| 27 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 28 | extern void down_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 32 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 33 | extern int down_read_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * lock for writing |
| 37 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 38 | extern void down_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 42 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 43 | extern int down_write_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * release a read lock |
| 47 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 48 | extern void up_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * release a write lock |
| 52 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 53 | extern void up_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * downgrade write lock to read lock |
| 57 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 58 | extern void downgrade_write(struct rw_semaphore *sem); |
| 59 | |
| 60 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 61 | /* |
Ingo Molnar | 5fca80e | 2006-07-10 04:44:02 -0700 | [diff] [blame] | 62 | * nested locking. NOTE: rwsems are not allowed to recurse |
| 63 | * (which occurs if the same task tries to acquire the same |
| 64 | * lock instance multiple times), but multiple locks of the |
| 65 | * same lock class might be taken, if the order of the locks |
| 66 | * is always the same. This ordering rule can be expressed |
| 67 | * to lockdep via the _nested() APIs, but enumerating the |
| 68 | * subclasses that are used. (If the nesting relationship is |
| 69 | * static then another method for expressing nested locking is |
| 70 | * the explicit definition of lock class keys and the use of |
| 71 | * lockdep_set_class() at lock initialization time. |
| 72 | * See Documentation/lockdep-design.txt for more details.) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 73 | */ |
| 74 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); |
| 75 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); |
| 76 | /* |
Ingo Molnar | 5fca80e | 2006-07-10 04:44:02 -0700 | [diff] [blame] | 77 | * Take/release a lock when not the owner will release it. |
| 78 | * |
| 79 | * [ This API should be avoided as much as possible - the |
| 80 | * proper abstraction for this case is completions. ] |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 81 | */ |
| 82 | extern void down_read_non_owner(struct rw_semaphore *sem); |
| 83 | extern void up_read_non_owner(struct rw_semaphore *sem); |
| 84 | #else |
| 85 | # define down_read_nested(sem, subclass) down_read(sem) |
| 86 | # define down_write_nested(sem, subclass) down_write(sem) |
| 87 | # define down_read_non_owner(sem) down_read(sem) |
| 88 | # define up_read_non_owner(sem) up_read(sem) |
| 89 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | #endif /* _LINUX_RWSEM_H */ |