4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
35 #ifndef __LIBCFS_DARWIN_CFS_LOCK_H__
36 #define __LIBCFS_DARWIN_CFS_LOCK_H__
38 #ifndef __LIBCFS_LIBCFS_H__
39 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
43 #include <mach/sync_policy.h>
44 #include <mach/task.h>
45 #include <mach/semaphore.h>
46 #include <kern/assert.h>
47 #include <kern/thread.h>
49 #include <libcfs/darwin/darwin-types.h>
50 #include <libcfs/darwin/darwin-sync.h>
53 * spin_lock (use Linux kernel's primitives)
60 * - spin_lock_irqsave(x, f)
61 * - spin_unlock_irqrestore(x, f)
67 typedef struct spin_lock spinlock_t;
69 static inline void spin_lock_init(spinlock_t *lock)
71 kspin_init(&lock->spin);
74 static inline void spin_lock(spinlock_t *lock)
76 kspin_lock(&lock->spin);
79 static inline void spin_unlock(spinlock_t *lock)
81 kspin_unlock(&lock->spin);
84 static inline int spin_trylock(spinlock_t *lock)
86 return kspin_trylock(&lock->spin);
89 static inline void spin_lock_done(spinlock_t *lock)
91 kspin_done(&lock->spin);
94 #error "does this lock out timer callbacks?"
95 #define spin_lock_bh(x) spin_lock(x)
96 #define spin_unlock_bh(x) spin_unlock(x)
97 #define spin_lock_bh_init(x) spin_lock_init(x)
99 extern boolean_t ml_set_interrupts_enabled(boolean_t enable);
100 #define __disable_irq() ml_set_interrupts_enabled(FALSE)
101 #define __enable_irq(x) (void) ml_set_interrupts_enabled(x)
103 #define spin_lock_irqsave(s, f) do{ \
104 f = __disable_irq(); \
105 spin_lock(s); }while(0)
107 #define spin_unlock_irqrestore(s, f) do{ \
109 __enable_irq(f);}while(0)
122 static inline void sema_init(struct semaphore *s, int val)
124 ksem_init(&s->sem, val);
127 static inline void __down(struct semaphore *s)
129 ksem_down(&s->sem, 1);
132 static inline void __up(struct semaphore *s)
141 * - init_mutex_locked(x)
146 #define mutex_up(s) __up(s)
147 #define mutex_down(s) __down(s)
149 #define init_mutex(x) sema_init(x, 1)
150 #define init_mutex_locked(x) sema_init(x, 0)
155 * - init_completion(c)
157 * - wait_for_completion(c)
161 * Emulate completion by semaphore for now.
163 * XXX nikita: this is not safe if completion is used to synchronize
164 * exit from kernel daemon thread and kext unloading. In this case
165 * some core function (a la complete_and_exit()) is needed.
170 static inline void init_completion(struct completion *c)
172 ksem_init(&c->sem, 0);
175 static inline void complete(struct completion *c)
180 static inline void wait_for_completion(struct completion *c)
182 ksem_down(&c->sem, 1);
195 struct rw_semaphore {
199 static inline void init_rwsem(struct rw_semaphore *s)
204 static inline void fini_rwsem(struct rw_semaphore *s)
209 static inline void down_read(struct rw_semaphore *s)
211 krw_sem_down_r(&s->s);
214 static inline int down_read_trylock(struct rw_semaphore *s)
216 int ret = krw_sem_down_r_try(&s->s);
220 static inline void down_write(struct rw_semaphore *s)
222 krw_sem_down_w(&s->s);
225 static inline int down_write_trylock(struct rw_semaphore *s)
227 int ret = krw_sem_down_w_try(&s->s);
231 static inline void up_read(struct rw_semaphore *s)
236 static inline void up_write(struct rw_semaphore *s)
242 * read-write lock : Need to be investigated more!!
244 * - DECLARE_RWLOCK(l)
251 typedef struct krw_spin rwlock_t;
253 #define rwlock_init(pl) krw_spin_init(pl)
255 #define read_lock(l) krw_spin_down_r(l)
256 #define read_unlock(l) krw_spin_up_r(l)
257 #define write_lock(l) krw_spin_down_w(l)
258 #define write_unlock(l) krw_spin_up_w(l)
260 #define write_lock_irqsave(l, f) do{ \
261 f = __disable_irq(); \
262 write_lock(l); }while(0)
264 #define write_unlock_irqrestore(l, f) do{ \
266 __enable_irq(f);}while(0)
268 #define read_lock_irqsave(l, f) do{ \
269 f = __disable_irq(); \
270 read_lock(l); }while(0)
272 #define read_unlock_irqrestore(l, f) do{ \
274 __enable_irq(f);}while(0)
282 #define CFS_DECL_FUNNEL_DATA
283 #define CFS_DECL_CONE_DATA DECLARE_FUNNEL_DATA
284 #define CFS_DECL_NET_DATA DECLARE_FUNNEL_DATA
285 #define CFS_CONE_IN do {} while(0)
286 #define CFS_CONE_EX do {} while(0)
288 #define CFS_NET_IN do {} while(0)
289 #define CFS_NET_EX do {} while(0)
293 #define CFS_DECL_FUNNEL_DATA \
294 boolean_t __funnel_state = FALSE; \
296 #define CFS_DECL_CONE_DATA CFS_DECL_FUNNEL_DATA
297 #define CFS_DECL_NET_DATA CFS_DECL_FUNNEL_DATA
299 void lustre_cone_in(boolean_t *state, funnel_t **cone);
300 void lustre_cone_ex(boolean_t state, funnel_t *cone);
302 #define CFS_CONE_IN lustre_cone_in(&__funnel_state, &__funnel)
303 #define CFS_CONE_EX lustre_cone_ex(__funnel_state, __funnel)
305 void lustre_net_in(boolean_t *state, funnel_t **cone);
306 void lustre_net_ex(boolean_t state, funnel_t *cone);
308 #define CFS_NET_IN lustre_net_in(&__funnel_state, &__funnel)
309 #define CFS_NET_EX lustre_net_ex(__funnel_state, __funnel)
314 #include <libcfs/user-lock.h>
315 #endif /* __KERNEL__ */
317 /* __XNU_CFS_LOCK_H */