1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/linux/linux-lock.h
38 * Basic library routines.
41 #ifndef __LIBCFS_LINUX_CFS_LOCK_H__
42 #define __LIBCFS_LINUX_CFS_LOCK_H__
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
49 #error This include is only for kernel use.
52 #include <linux/smp_lock.h>
53 #include <linux/mutex.h>
58 * All locks' declaration are not guaranteed to be initialized,
59 * Althought some of they are initialized in Linux. All locks
60 * declared by CFS_DECL_* should be initialized explicitly.
64 * spin_lock "implementation" (use Linux kernel's primitives)
69 * - spin_lock_bh_init(x)
76 * - spin_lock_irqsave(x, f)
77 * - spin_unlock_irqrestore(x, f)
78 * - read_lock_irqsave(lock, f)
79 * - write_lock_irqsave(lock, f)
80 * - write_unlock_irqrestore(lock, f)
82 * - SPIN_LOCK_UNLOCKED
86 * spinlock "implementation"
89 typedef spinlock_t cfs_spinlock_t;
91 #define cfs_spin_lock_init(lock) spin_lock_init(lock)
92 #define cfs_spin_lock(lock) spin_lock(lock)
93 #define cfs_spin_lock_bh(lock) spin_lock_bh(lock)
94 #define cfs_spin_lock_bh_init(lock) spin_lock_bh_init(lock)
95 #define cfs_spin_unlock(lock) spin_unlock(lock)
96 #define cfs_spin_unlock_bh(lock) spin_unlock_bh(lock)
97 #define cfs_spin_trylock(lock) spin_trylock(lock)
98 #define cfs_spin_is_locked(lock) spin_is_locked(lock)
100 #define cfs_spin_lock_irq(lock) spin_lock_irq(lock)
101 #define cfs_spin_unlock_irq(lock) spin_unlock_irq(lock)
102 #define cfs_read_lock_irqsave(lock, f) read_lock_irqsave(lock, f)
103 #define cfs_write_lock_irqsave(lock, f) write_lock_irqsave(lock, f)
104 #define cfs_write_unlock_irqrestore(lock, f) write_unlock_irqrestore(lock, f)
105 #define cfs_spin_lock_irqsave(lock, f) spin_lock_irqsave(lock, f)
106 #define cfs_spin_unlock_irqrestore(lock, f) spin_unlock_irqrestore(lock, f)
108 #define CFS_SPIN_LOCK_UNLOCKED SPIN_LOCK_UNLOCKED
111 * rw_semaphore "implementation" (use Linux kernel's primitives)
120 typedef struct rw_semaphore cfs_rw_semaphore_t;
122 #define cfs_init_rwsem(s) init_rwsem(s)
123 #define cfs_down_read(s) down_read(s)
124 #define cfs_down_read_trylock(s) down_read_trylock(s)
125 #define cfs_up_read(s) up_read(s)
126 #define cfs_down_write(s) down_write(s)
127 #define cfs_down_write_trylock(s) down_write_trylock(s)
128 #define cfs_up_write(s) up_write(s)
130 #define cfs_fini_rwsem(s) do {} while(0)
132 #define CFS_DECLARE_RWSEM(name) DECLARE_RWSEM(name)
135 * rwlock_t "implementation" (use Linux kernel's primitives)
143 * - write_unlock_bh(x)
147 typedef rwlock_t cfs_rwlock_t;
149 #define cfs_rwlock_init(lock) rwlock_init(lock)
150 #define cfs_read_lock(lock) read_lock(lock)
151 #define cfs_read_unlock(lock) read_unlock(lock)
152 #define cfs_read_unlock_irqrestore(lock,flags) \
153 read_unlock_irqrestore(lock, flags)
154 #define cfs_write_lock(lock) write_lock(lock)
155 #define cfs_write_unlock(lock) write_unlock(lock)
156 #define cfs_write_lock_bh(lock) write_lock_bh(lock)
157 #define cfs_write_unlock_bh(lock) write_unlock_bh(lock)
159 #define CFS_RW_LOCK_UNLOCKED RW_LOCK_UNLOCKED
162 * completion "implementation" (use Linux kernel's primitives)
164 * - DECLARE_COMPLETION(work)
165 * - INIT_COMPLETION(c)
166 * - COMPLETION_INITIALIZER(work)
167 * - init_completion(c)
169 * - wait_for_completion(c)
170 * - wait_for_completion_interruptible(c)
171 * - fini_completion(c)
173 typedef struct completion cfs_completion_t;
175 #define CFS_DECLARE_COMPLETION(work) DECLARE_COMPLETION(work)
176 #define CFS_INIT_COMPLETION(c) INIT_COMPLETION(c)
177 #define CFS_COMPLETION_INITIALIZER(work) COMPLETION_INITIALIZER(work)
178 #define cfs_init_completion(c) init_completion(c)
179 #define cfs_complete(c) complete(c)
180 #define cfs_wait_for_completion(c) wait_for_completion(c)
181 #define cfs_wait_for_completion_interruptible(c) \
182 wait_for_completion_interruptible(c)
183 #define cfs_complete_and_exit(c, code) complete_and_exit(c, code)
184 #define cfs_fini_completion(c) do { } while (0)
187 * semaphore "implementation" (use Linux kernel's primitives)
188 * - DEFINE_SEMAPHORE(name)
189 * - sema_init(sem, val)
192 * - down_interruptible(sem)
193 * - down_trylock(sem)
195 typedef struct semaphore cfs_semaphore_t;
197 #ifdef DEFINE_SEMAPHORE
198 #define CFS_DEFINE_SEMAPHORE(name) DEFINE_SEMAPHORE(name)
200 #define CFS_DEFINE_SEMAPHORE(name) DECLARE_MUTEX(name)
203 #define cfs_sema_init(sem, val) sema_init(sem, val)
204 #define cfs_up(x) up(x)
205 #define cfs_down(x) down(x)
206 #define cfs_down_interruptible(x) down_interruptible(x)
207 #define cfs_down_trylock(x) down_trylock(x)
210 * mutex "implementation" (use Linux kernel's primitives)
212 * - DEFINE_MUTEX(name)
217 * - mutex_is_locked(x)
220 typedef struct mutex cfs_mutex_t;
222 #define CFS_DEFINE_MUTEX(name) DEFINE_MUTEX(name)
224 #define cfs_mutex_init(x) mutex_init(x)
225 #define cfs_mutex_lock(x) mutex_lock(x)
226 #define cfs_mutex_unlock(x) mutex_unlock(x)
227 #define cfs_mutex_lock_interruptible(x) mutex_lock_interruptible(x)
228 #define cfs_mutex_trylock(x) mutex_trylock(x)
229 #define cfs_mutex_is_locked(x) mutex_is_locked(x)
230 #define cfs_mutex_destroy(x) mutex_destroy(x)
233 * Kernel locking primitives
238 #define cfs_lock_kernel() lock_kernel()
239 #define cfs_unlock_kernel() unlock_kernel()
241 #ifndef lockdep_set_class
243 /**************************************************************************
245 * Lockdep "implementation". Also see liblustre.h
247 **************************************************************************/
249 typedef struct cfs_lock_class_key {
251 } cfs_lock_class_key_t;
253 #define cfs_lockdep_set_class(lock, key) \
254 do { (void)sizeof (lock);(void)sizeof (key); } while (0)
255 /* This has to be a macro, so that `subclass' can be undefined in kernels that
256 * do not support lockdep. */
259 static inline void cfs_lockdep_off(void)
263 static inline void cfs_lockdep_on(void)
267 typedef struct lock_class_key cfs_lock_class_key_t;
269 #define cfs_lockdep_set_class(lock, key) lockdep_set_class(lock, key)
270 #define cfs_lockdep_off() lockdep_off()
271 #define cfs_lockdep_on() lockdep_on()
272 #endif /* lockdep_set_class */
274 #ifndef CONFIG_DEBUG_LOCK_ALLOC
275 #ifndef mutex_lock_nested
276 #define cfs_mutex_lock_nested(mutex, subclass) mutex_lock(mutex)
278 #define cfs_mutex_lock_nested(mutex, subclass) \
279 mutex_lock_nested(mutex, subclass)
282 #ifndef spin_lock_nested
283 #define cfs_spin_lock_nested(lock, subclass) spin_lock(lock)
285 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
288 #ifndef down_read_nested
289 #define cfs_down_read_nested(lock, subclass) down_read(lock)
291 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
294 #ifndef down_write_nested
295 #define cfs_down_write_nested(lock, subclass) down_write(lock)
297 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
299 #else /* CONFIG_DEBUG_LOCK_ALLOC is defined */
300 #define cfs_mutex_lock_nested(mutex, subclass) \
301 mutex_lock_nested(mutex, subclass)
302 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
303 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
304 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
305 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
308 #endif /* __LIBCFS_LINUX_CFS_LOCK_H__ */