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 | #ifdef __KERNEL__ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <asm/system.h> |
| 17 | #include <asm/atomic.h> |
| 18 | |
| 19 | struct rw_semaphore; |
| 20 | |
| 21 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK |
| 22 | #include <linux/rwsem-spinlock.h> /* use a generic implementation */ |
| 23 | #else |
| 24 | #include <asm/rwsem.h> /* use an arch-specific implementation */ |
| 25 | #endif |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* |
| 28 | * lock for reading |
| 29 | */ |
| 30 | static inline void down_read(struct rw_semaphore *sem) |
| 31 | { |
| 32 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | __down_read(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* |
| 37 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 38 | */ |
| 39 | static inline int down_read_trylock(struct rw_semaphore *sem) |
| 40 | { |
| 41 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | ret = __down_read_trylock(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * lock for writing |
| 48 | */ |
| 49 | static inline void down_write(struct rw_semaphore *sem) |
| 50 | { |
| 51 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | __down_write(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 57 | */ |
| 58 | static inline int down_write_trylock(struct rw_semaphore *sem) |
| 59 | { |
| 60 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | ret = __down_write_trylock(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * release a read lock |
| 67 | */ |
| 68 | static inline void up_read(struct rw_semaphore *sem) |
| 69 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | __up_read(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * release a write lock |
| 75 | */ |
| 76 | static inline void up_write(struct rw_semaphore *sem) |
| 77 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | __up_write(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
| 82 | * downgrade write lock to read lock |
| 83 | */ |
| 84 | static inline void downgrade_write(struct rw_semaphore *sem) |
| 85 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | __downgrade_write(sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | #endif /* __KERNEL__ */ |
| 90 | #endif /* _LINUX_RWSEM_H */ |