blob: 93581534b915129b36eb1369471334628103e209 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Torvalds1da177e2005-04-16 15:20:36 -070012#ifdef __KERNEL__
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
16#include <asm/system.h>
17#include <asm/atomic.h>
18
19struct 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 Torvalds1da177e2005-04-16 15:20:36 -070027/*
28 * lock for reading
29 */
30static inline void down_read(struct rw_semaphore *sem)
31{
32 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 __down_read(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36/*
37 * trylock for reading -- returns 1 if successful, 0 if contention
38 */
39static inline int down_read_trylock(struct rw_semaphore *sem)
40{
41 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 ret = __down_read_trylock(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return ret;
44}
45
46/*
47 * lock for writing
48 */
49static inline void down_write(struct rw_semaphore *sem)
50{
51 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 __down_write(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55/*
56 * trylock for writing -- returns 1 if successful, 0 if contention
57 */
58static inline int down_write_trylock(struct rw_semaphore *sem)
59{
60 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ret = __down_write_trylock(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return ret;
63}
64
65/*
66 * release a read lock
67 */
68static inline void up_read(struct rw_semaphore *sem)
69{
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 __up_read(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73/*
74 * release a write lock
75 */
76static inline void up_write(struct rw_semaphore *sem)
77{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 __up_write(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81/*
82 * downgrade write lock to read lock
83 */
84static inline void downgrade_write(struct rw_semaphore *sem)
85{
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 __downgrade_write(sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89#endif /* __KERNEL__ */
90#endif /* _LINUX_RWSEM_H */