Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-2.6] / include / linux / rwsem.h
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
12 #define RWSEM_DEBUG 0
13
14 #ifdef __KERNEL__
15
16 #include <linux/config.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <asm/system.h>
20 #include <asm/atomic.h>
21
22 struct rw_semaphore;
23
24 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
25 #include <linux/rwsem-spinlock.h> /* use a generic implementation */
26 #else
27 #include <asm/rwsem.h> /* use an arch-specific implementation */
28 #endif
29
30 #ifndef rwsemtrace
31 #if RWSEM_DEBUG
32 extern void FASTCALL(rwsemtrace(struct rw_semaphore *sem, const char *str));
33 #else
34 #define rwsemtrace(SEM,FMT)
35 #endif
36 #endif
37
38 /*
39  * lock for reading
40  */
41 static inline void down_read(struct rw_semaphore *sem)
42 {
43         might_sleep();
44         rwsemtrace(sem,"Entering down_read");
45         __down_read(sem);
46         rwsemtrace(sem,"Leaving down_read");
47 }
48
49 /*
50  * trylock for reading -- returns 1 if successful, 0 if contention
51  */
52 static inline int down_read_trylock(struct rw_semaphore *sem)
53 {
54         int ret;
55         rwsemtrace(sem,"Entering down_read_trylock");
56         ret = __down_read_trylock(sem);
57         rwsemtrace(sem,"Leaving down_read_trylock");
58         return ret;
59 }
60
61 /*
62  * lock for writing
63  */
64 static inline void down_write(struct rw_semaphore *sem)
65 {
66         might_sleep();
67         rwsemtrace(sem,"Entering down_write");
68         __down_write(sem);
69         rwsemtrace(sem,"Leaving down_write");
70 }
71
72 /*
73  * trylock for writing -- returns 1 if successful, 0 if contention
74  */
75 static inline int down_write_trylock(struct rw_semaphore *sem)
76 {
77         int ret;
78         rwsemtrace(sem,"Entering down_write_trylock");
79         ret = __down_write_trylock(sem);
80         rwsemtrace(sem,"Leaving down_write_trylock");
81         return ret;
82 }
83
84 /*
85  * release a read lock
86  */
87 static inline void up_read(struct rw_semaphore *sem)
88 {
89         rwsemtrace(sem,"Entering up_read");
90         __up_read(sem);
91         rwsemtrace(sem,"Leaving up_read");
92 }
93
94 /*
95  * release a write lock
96  */
97 static inline void up_write(struct rw_semaphore *sem)
98 {
99         rwsemtrace(sem,"Entering up_write");
100         __up_write(sem);
101         rwsemtrace(sem,"Leaving up_write");
102 }
103
104 /*
105  * downgrade write lock to read lock
106  */
107 static inline void downgrade_write(struct rw_semaphore *sem)
108 {
109         rwsemtrace(sem,"Entering downgrade_write");
110         __downgrade_write(sem);
111         rwsemtrace(sem,"Leaving downgrade_write");
112 }
113
114 #endif /* __KERNEL__ */
115 #endif /* _LINUX_RWSEM_H */