Whamcloud - gitweb
LU-506 kernel: remove BKL
[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/smp_lock.h>
51 #include <linux/mutex.h>
52
53 /*
54  * IMPORTANT !!!!!!!!
55  *
56  * All locks' declaration are not guaranteed to be initialized,
57  * Althought some of they are initialized in Linux. All locks
58  * declared by CFS_DECL_* should be initialized explicitly.
59  */
60
61 /*
62  * spin_lock "implementation" (use Linux kernel's primitives)
63  *
64  * - spin_lock_init(x)
65  * - spin_lock(x)
66  * - spin_lock_bh(x)
67  * - spin_lock_bh_init(x)
68  * - spin_unlock(x)
69  * - spin_unlock_bh(x)
70  * - spin_trylock(x)
71  * - spin_is_locked(x)
72  *
73  * - spin_lock_irq(x)
74  * - spin_lock_irqsave(x, f)
75  * - spin_unlock_irqrestore(x, f)
76  * - read_lock_irqsave(lock, f)
77  * - write_lock_irqsave(lock, f)
78  * - write_unlock_irqrestore(lock, f)
79  *
80  * - SPIN_LOCK_UNLOCKED
81  */
82
83 /*
84  * spinlock "implementation"
85  */
86
87 typedef spinlock_t cfs_spinlock_t;
88
89 #define cfs_spin_lock_init(lock)             spin_lock_init(lock)
90 #define cfs_spin_lock(lock)                  spin_lock(lock)
91 #define cfs_spin_lock_bh(lock)               spin_lock_bh(lock)
92 #define cfs_spin_lock_bh_init(lock)          spin_lock_bh_init(lock)
93 #define cfs_spin_unlock(lock)                spin_unlock(lock)
94 #define cfs_spin_unlock_bh(lock)             spin_unlock_bh(lock)
95 #define cfs_spin_trylock(lock)               spin_trylock(lock)
96 #define cfs_spin_is_locked(lock)             spin_is_locked(lock)
97
98 #define cfs_spin_lock_irq(lock)              spin_lock_irq(lock)
99 #define cfs_spin_unlock_irq(lock)            spin_unlock_irq(lock)
100 #define cfs_read_lock_irqsave(lock, f)       read_lock_irqsave(lock, f)
101 #define cfs_write_lock_irqsave(lock, f)      write_lock_irqsave(lock, f)
102 #define cfs_write_unlock_irqrestore(lock, f) write_unlock_irqrestore(lock, f)
103 #define cfs_spin_lock_irqsave(lock, f)       spin_lock_irqsave(lock, f)
104 #define cfs_spin_unlock_irqrestore(lock, f)  spin_unlock_irqrestore(lock, f)
105
106 #define CFS_SPIN_LOCK_UNLOCKED               SPIN_LOCK_UNLOCKED
107
108 /*
109  * rw_semaphore "implementation" (use Linux kernel's primitives)
110  *
111  * - sema_init(x)
112  * - init_rwsem(x)
113  * - down_read(x)
114  * - up_read(x)
115  * - down_write(x)
116  * - up_write(x)
117  */
118 typedef struct rw_semaphore cfs_rw_semaphore_t;
119
120 #define cfs_init_rwsem(s)         init_rwsem(s)
121 #define cfs_down_read(s)          down_read(s)
122 #define cfs_down_read_trylock(s)  down_read_trylock(s)
123 #define cfs_up_read(s)            up_read(s)
124 #define cfs_down_write(s)         down_write(s)
125 #define cfs_down_write_trylock(s) down_write_trylock(s)
126 #define cfs_up_write(s)           up_write(s)
127
128 #define cfs_fini_rwsem(s)         do {} while(0)
129
130 #define CFS_DECLARE_RWSEM(name)   DECLARE_RWSEM(name)
131
132 /*
133  * rwlock_t "implementation" (use Linux kernel's primitives)
134  *
135  * - rwlock_init(x)
136  * - read_lock(x)
137  * - read_unlock(x)
138  * - write_lock(x)
139  * - write_unlock(x)
140  * - write_lock_bh(x)
141  * - write_unlock_bh(x)
142  *
143  * - RW_LOCK_UNLOCKED
144  */
145 typedef rwlock_t cfs_rwlock_t;
146
147 #define cfs_rwlock_init(lock)                  rwlock_init(lock)
148 #define cfs_read_lock(lock)                    read_lock(lock)
149 #define cfs_read_unlock(lock)                  read_unlock(lock)
150 #define cfs_read_unlock_irqrestore(lock,flags) \
151         read_unlock_irqrestore(lock, flags)
152 #define cfs_write_lock(lock)                   write_lock(lock)
153 #define cfs_write_unlock(lock)                 write_unlock(lock)
154 #define cfs_write_lock_bh(lock)                write_lock_bh(lock)
155 #define cfs_write_unlock_bh(lock)              write_unlock_bh(lock)
156
157 #define CFS_RW_LOCK_UNLOCKED                   RW_LOCK_UNLOCKED
158
159 /*
160  * completion "implementation" (use Linux kernel's primitives)
161  *
162  * - DECLARE_COMPLETION(work)
163  * - INIT_COMPLETION(c)
164  * - COMPLETION_INITIALIZER(work)
165  * - init_completion(c)
166  * - complete(c)
167  * - wait_for_completion(c)
168  * - wait_for_completion_interruptible(c)
169  * - fini_completion(c)
170  */
171 typedef struct completion cfs_completion_t;
172
173 #define CFS_DECLARE_COMPLETION(work)             DECLARE_COMPLETION(work)
174 #define CFS_INIT_COMPLETION(c)                   INIT_COMPLETION(c)
175 #define CFS_COMPLETION_INITIALIZER(work)         COMPLETION_INITIALIZER(work)
176 #define cfs_init_completion(c)                   init_completion(c)
177 #define cfs_complete(c)                          complete(c)
178 #define cfs_wait_for_completion(c)               wait_for_completion(c)
179 #define cfs_wait_for_completion_interruptible(c) \
180         wait_for_completion_interruptible(c)
181 #define cfs_complete_and_exit(c, code)           complete_and_exit(c, code)
182 #define cfs_fini_completion(c)                   do { } while (0)
183
184 /*
185  * semaphore "implementation" (use Linux kernel's primitives)
186  * - DEFINE_SEMAPHORE(name)
187  * - sema_init(sem, val)
188  * - up(sem)
189  * - down(sem)
190  * - down_interruptible(sem)
191  * - down_trylock(sem)
192  */
193 typedef struct semaphore      cfs_semaphore_t;
194
195 #ifdef DEFINE_SEMAPHORE
196 #define CFS_DEFINE_SEMAPHORE(name)          DEFINE_SEMAPHORE(name)
197 #else
198 #define CFS_DEFINE_SEMAPHORE(name)          DECLARE_MUTEX(name)
199 #endif
200
201 #define cfs_sema_init(sem, val)             sema_init(sem, val)
202 #define cfs_up(x)                           up(x)
203 #define cfs_down(x)                         down(x)
204 #define cfs_down_interruptible(x)           down_interruptible(x)
205 #define cfs_down_trylock(x)                 down_trylock(x)
206
207 /*
208  * mutex "implementation" (use Linux kernel's primitives)
209  *
210  * - DEFINE_MUTEX(name)
211  * - mutex_init(x)
212  * - mutex_lock(x)
213  * - mutex_unlock(x)
214  * - mutex_trylock(x)
215  * - mutex_is_locked(x)
216  * - mutex_destroy(x)
217  */
218 typedef struct mutex cfs_mutex_t;
219
220 #define CFS_DEFINE_MUTEX(name)             DEFINE_MUTEX(name)
221
222 #define cfs_mutex_init(x)                   mutex_init(x)
223 #define cfs_mutex_lock(x)                   mutex_lock(x)
224 #define cfs_mutex_unlock(x)                 mutex_unlock(x)
225 #define cfs_mutex_lock_interruptible(x)     mutex_lock_interruptible(x)
226 #define cfs_mutex_trylock(x)                mutex_trylock(x)
227 #define cfs_mutex_is_locked(x)              mutex_is_locked(x)
228 #define cfs_mutex_destroy(x)                mutex_destroy(x)
229
230 #ifndef lockdep_set_class
231
232 /**************************************************************************
233  *
234  * Lockdep "implementation". Also see liblustre.h
235  *
236  **************************************************************************/
237
238 typedef struct cfs_lock_class_key {
239         ;
240 } cfs_lock_class_key_t;
241
242 #define cfs_lockdep_set_class(lock, key) \
243         do { (void)sizeof (lock);(void)sizeof (key); } while (0)
244 /* This has to be a macro, so that `subclass' can be undefined in kernels that
245  * do not support lockdep. */
246
247
248 static inline void cfs_lockdep_off(void)
249 {
250 }
251
252 static inline void cfs_lockdep_on(void)
253 {
254 }
255 #else
256 typedef struct lock_class_key cfs_lock_class_key_t;
257
258 #define cfs_lockdep_set_class(lock, key) lockdep_set_class(lock, key)
259 #define cfs_lockdep_off()                lockdep_off()
260 #define cfs_lockdep_on()                 lockdep_on()
261 #endif /* lockdep_set_class */
262
263 #ifndef CONFIG_DEBUG_LOCK_ALLOC
264 #ifndef mutex_lock_nested
265 #define cfs_mutex_lock_nested(mutex, subclass) mutex_lock(mutex)
266 #else
267 #define cfs_mutex_lock_nested(mutex, subclass) \
268         mutex_lock_nested(mutex, subclass)
269 #endif
270
271 #ifndef spin_lock_nested
272 #define cfs_spin_lock_nested(lock, subclass) spin_lock(lock)
273 #else
274 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
275 #endif
276
277 #ifndef down_read_nested
278 #define cfs_down_read_nested(lock, subclass) down_read(lock)
279 #else
280 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
281 #endif
282
283 #ifndef down_write_nested
284 #define cfs_down_write_nested(lock, subclass) down_write(lock)
285 #else
286 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
287 #endif
288 #else /* CONFIG_DEBUG_LOCK_ALLOC is defined */
289 #define cfs_mutex_lock_nested(mutex, subclass) \
290         mutex_lock_nested(mutex, subclass)
291 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
292 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
293 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
294 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
295
296
297 #endif /* __LIBCFS_LINUX_CFS_LOCK_H__ */