Whamcloud - gitweb
cc2ca51b8c2d734fca788770c9c3dfc4cb4b6dd8
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-lock.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/linux/linux-lock.h
35  *
36  * Basic library routines.
37  */
38
39 #ifndef __LIBCFS_LINUX_CFS_LOCK_H__
40 #define __LIBCFS_LINUX_CFS_LOCK_H__
41
42 #ifndef __LIBCFS_LIBCFS_H__
43 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
44 #endif
45
46 #ifndef __KERNEL__
47 #error This include is only for kernel use.
48 #endif
49
50 #include <linux/mutex.h>
51
52 /*
53  * IMPORTANT !!!!!!!!
54  *
55  * All locks' declaration are not guaranteed to be initialized,
56  * Althought some of they are initialized in Linux. All locks
57  * declared by CFS_DECL_* should be initialized explicitly.
58  */
59
60 /*
61  * spin_lock "implementation" (use Linux kernel's primitives)
62  *
63  * - spin_lock_init(x)
64  * - spin_lock(x)
65  * - spin_lock_bh(x)
66  * - spin_lock_bh_init(x)
67  * - spin_unlock(x)
68  * - spin_unlock_bh(x)
69  * - spin_trylock(x)
70  * - spin_is_locked(x)
71  *
72  * - spin_lock_irq(x)
73  * - spin_lock_irqsave(x, f)
74  * - spin_unlock_irqrestore(x, f)
75  * - read_lock_irqsave(lock, f)
76  * - write_lock_irqsave(lock, f)
77  * - write_unlock_irqrestore(lock, f)
78  */
79
80 /*
81  * spinlock "implementation"
82  */
83
84 typedef spinlock_t cfs_spinlock_t;
85
86 #define cfs_spin_lock_init(lock)             spin_lock_init(lock)
87 #define cfs_spin_lock(lock)                  spin_lock(lock)
88 #define cfs_spin_lock_bh(lock)               spin_lock_bh(lock)
89 #define cfs_spin_lock_bh_init(lock)          spin_lock_bh_init(lock)
90 #define cfs_spin_unlock(lock)                spin_unlock(lock)
91 #define cfs_spin_unlock_bh(lock)             spin_unlock_bh(lock)
92 #define cfs_spin_trylock(lock)               spin_trylock(lock)
93 #define cfs_spin_is_locked(lock)             spin_is_locked(lock)
94
95 #define cfs_spin_lock_irq(lock)              spin_lock_irq(lock)
96 #define cfs_spin_unlock_irq(lock)            spin_unlock_irq(lock)
97 #define cfs_read_lock_irqsave(lock, f)       read_lock_irqsave(lock, f)
98 #define cfs_write_lock_irqsave(lock, f)      write_lock_irqsave(lock, f)
99 #define cfs_write_unlock_irqrestore(lock, f) write_unlock_irqrestore(lock, f)
100 #define cfs_spin_lock_irqsave(lock, f)       spin_lock_irqsave(lock, f)
101 #define cfs_spin_unlock_irqrestore(lock, f)  spin_unlock_irqrestore(lock, f)
102
103 /*
104  * rw_semaphore "implementation" (use Linux kernel's primitives)
105  *
106  * - sema_init(x)
107  * - init_rwsem(x)
108  * - down_read(x)
109  * - up_read(x)
110  * - down_write(x)
111  * - up_write(x)
112  */
113 typedef struct rw_semaphore cfs_rw_semaphore_t;
114
115 #define cfs_init_rwsem(s)         init_rwsem(s)
116 #define cfs_down_read(s)          down_read(s)
117 #define cfs_down_read_trylock(s)  down_read_trylock(s)
118 #define cfs_up_read(s)            up_read(s)
119 #define cfs_down_write(s)         down_write(s)
120 #define cfs_down_write_trylock(s) down_write_trylock(s)
121 #define cfs_up_write(s)           up_write(s)
122
123 #define cfs_fini_rwsem(s)         do {} while(0)
124
125 #define CFS_DECLARE_RWSEM(name)   DECLARE_RWSEM(name)
126
127 /*
128  * rwlock_t "implementation" (use Linux kernel's primitives)
129  *
130  * - rwlock_init(x)
131  * - read_lock(x)
132  * - read_unlock(x)
133  * - write_lock(x)
134  * - write_unlock(x)
135  * - write_lock_bh(x)
136  * - write_unlock_bh(x)
137  *
138  * - RW_LOCK_UNLOCKED
139  */
140 typedef rwlock_t cfs_rwlock_t;
141
142 #define cfs_rwlock_init(lock)                  rwlock_init(lock)
143 #define cfs_read_lock(lock)                    read_lock(lock)
144 #define cfs_read_unlock(lock)                  read_unlock(lock)
145 #define cfs_read_unlock_irqrestore(lock,flags) \
146         read_unlock_irqrestore(lock, flags)
147 #define cfs_write_lock(lock)                   write_lock(lock)
148 #define cfs_write_unlock(lock)                 write_unlock(lock)
149 #define cfs_write_lock_bh(lock)                write_lock_bh(lock)
150 #define cfs_write_unlock_bh(lock)              write_unlock_bh(lock)
151
152 #ifndef DEFINE_RWLOCK
153 #define DEFINE_RWLOCK(lock)     rwlock_t lock = __RW_LOCK_UNLOCKED(lock)
154 #endif
155
156 /*
157  * completion "implementation" (use Linux kernel's primitives)
158  *
159  * - DECLARE_COMPLETION(work)
160  * - INIT_COMPLETION(c)
161  * - COMPLETION_INITIALIZER(work)
162  * - init_completion(c)
163  * - complete(c)
164  * - wait_for_completion(c)
165  * - wait_for_completion_interruptible(c)
166  * - fini_completion(c)
167  */
168 typedef struct completion cfs_completion_t;
169
170 #define CFS_DECLARE_COMPLETION(work)             DECLARE_COMPLETION(work)
171 #define CFS_INIT_COMPLETION(c)                   INIT_COMPLETION(c)
172 #define CFS_COMPLETION_INITIALIZER(work)         COMPLETION_INITIALIZER(work)
173 #define cfs_init_completion(c)                   init_completion(c)
174 #define cfs_complete(c)                          complete(c)
175 #define cfs_wait_for_completion(c)               wait_for_completion(c)
176 #define cfs_wait_for_completion_interruptible(c) \
177         wait_for_completion_interruptible(c)
178 #define cfs_complete_and_exit(c, code)           complete_and_exit(c, code)
179 #define cfs_fini_completion(c)                   do { } while (0)
180
181 /*
182  * semaphore "implementation" (use Linux kernel's primitives)
183  * - DEFINE_SEMAPHORE(name)
184  * - sema_init(sem, val)
185  * - up(sem)
186  * - down(sem)
187  * - down_interruptible(sem)
188  * - down_trylock(sem)
189  */
190 typedef struct semaphore      cfs_semaphore_t;
191
192 #ifdef DEFINE_SEMAPHORE
193 #define CFS_DEFINE_SEMAPHORE(name)          DEFINE_SEMAPHORE(name)
194 #else
195 #define CFS_DEFINE_SEMAPHORE(name)          DECLARE_MUTEX(name)
196 #endif
197
198 #define cfs_sema_init(sem, val)             sema_init(sem, val)
199 #define cfs_up(x)                           up(x)
200 #define cfs_down(x)                         down(x)
201 #define cfs_down_interruptible(x)           down_interruptible(x)
202 #define cfs_down_trylock(x)                 down_trylock(x)
203
204 /*
205  * mutex "implementation" (use Linux kernel's primitives)
206  *
207  * - DEFINE_MUTEX(name)
208  * - mutex_init(x)
209  * - mutex_lock(x)
210  * - mutex_unlock(x)
211  * - mutex_trylock(x)
212  * - mutex_is_locked(x)
213  * - mutex_destroy(x)
214  */
215 typedef struct mutex cfs_mutex_t;
216
217 #define CFS_DEFINE_MUTEX(name)             DEFINE_MUTEX(name)
218
219 #define cfs_mutex_init(x)                   mutex_init(x)
220 #define cfs_mutex_lock(x)                   mutex_lock(x)
221 #define cfs_mutex_unlock(x)                 mutex_unlock(x)
222 #define cfs_mutex_lock_interruptible(x)     mutex_lock_interruptible(x)
223 #define cfs_mutex_trylock(x)                mutex_trylock(x)
224 #define cfs_mutex_is_locked(x)              mutex_is_locked(x)
225 #define cfs_mutex_destroy(x)                mutex_destroy(x)
226
227 #ifndef lockdep_set_class
228
229 /**************************************************************************
230  *
231  * Lockdep "implementation". Also see liblustre.h
232  *
233  **************************************************************************/
234
235 typedef struct cfs_lock_class_key {
236         ;
237 } cfs_lock_class_key_t;
238
239 #define cfs_lockdep_set_class(lock, key) \
240         do { (void)sizeof (lock);(void)sizeof (key); } while (0)
241 /* This has to be a macro, so that `subclass' can be undefined in kernels that
242  * do not support lockdep. */
243
244
245 static inline void cfs_lockdep_off(void)
246 {
247 }
248
249 static inline void cfs_lockdep_on(void)
250 {
251 }
252 #else
253 typedef struct lock_class_key cfs_lock_class_key_t;
254
255 #define cfs_lockdep_set_class(lock, key) lockdep_set_class(lock, key)
256 #define cfs_lockdep_off()                lockdep_off()
257 #define cfs_lockdep_on()                 lockdep_on()
258 #endif /* lockdep_set_class */
259
260 #ifndef CONFIG_DEBUG_LOCK_ALLOC
261 #ifndef mutex_lock_nested
262 #define cfs_mutex_lock_nested(mutex, subclass) mutex_lock(mutex)
263 #else
264 #define cfs_mutex_lock_nested(mutex, subclass) \
265         mutex_lock_nested(mutex, subclass)
266 #endif
267
268 #ifndef spin_lock_nested
269 #define cfs_spin_lock_nested(lock, subclass) spin_lock(lock)
270 #else
271 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
272 #endif
273
274 #ifndef down_read_nested
275 #define cfs_down_read_nested(lock, subclass) down_read(lock)
276 #else
277 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
278 #endif
279
280 #ifndef down_write_nested
281 #define cfs_down_write_nested(lock, subclass) down_write(lock)
282 #else
283 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
284 #endif
285 #else /* CONFIG_DEBUG_LOCK_ALLOC is defined */
286 #define cfs_mutex_lock_nested(mutex, subclass) \
287         mutex_lock_nested(mutex, subclass)
288 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
289 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
290 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
291 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
292
293
294 #endif /* __LIBCFS_LINUX_CFS_LOCK_H__ */