Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rwsem.c
Go to the documentation of this file.
1 /* kernel/rwsem.c: R/W semaphores, public implementation
2  *
3  * Written by David Howells ([email protected]).
4  * Derived from asm-i386/semaphore.h
5  */
6 
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/export.h>
11 #include <linux/rwsem.h>
12 
13 #include <linux/atomic.h>
14 
15 /*
16  * lock for reading
17  */
19 {
20  might_sleep();
21  rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
22 
24 }
25 
27 
28 /*
29  * trylock for reading -- returns 1 if successful, 0 if contention
30  */
32 {
33  int ret = __down_read_trylock(sem);
34 
35  if (ret == 1)
36  rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
37  return ret;
38 }
39 
41 
42 /*
43  * lock for writing
44  */
46 {
47  might_sleep();
48  rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
49 
51 }
52 
54 
55 /*
56  * trylock for writing -- returns 1 if successful, 0 if contention
57  */
59 {
60  int ret = __down_write_trylock(sem);
61 
62  if (ret == 1)
63  rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
64  return ret;
65 }
66 
68 
69 /*
70  * release a read lock
71  */
72 void up_read(struct rw_semaphore *sem)
73 {
74  rwsem_release(&sem->dep_map, 1, _RET_IP_);
75 
76  __up_read(sem);
77 }
78 
80 
81 /*
82  * release a write lock
83  */
84 void up_write(struct rw_semaphore *sem)
85 {
86  rwsem_release(&sem->dep_map, 1, _RET_IP_);
87 
88  __up_write(sem);
89 }
90 
92 
93 /*
94  * downgrade write lock to read lock
95  */
97 {
98  /*
99  * lockdep: a downgraded write will live on as a write
100  * dependency.
101  */
102  __downgrade_write(sem);
103 }
104 
106 
107 #ifdef CONFIG_DEBUG_LOCK_ALLOC
108 
109 void down_read_nested(struct rw_semaphore *sem, int subclass)
110 {
111  might_sleep();
112  rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
113 
115 }
116 
118 
119 void down_write_nested(struct rw_semaphore *sem, int subclass)
120 {
121  might_sleep();
122  rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
123 
125 }
126 
128 
129 #endif
130 
131