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.
37 #ifndef __LIBCFS_DARWIN_CFS_LOCK_H__
38 #define __LIBCFS_DARWIN_CFS_LOCK_H__
40 #ifndef __LIBCFS_LIBCFS_H__
41 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
45 #include <mach/sync_policy.h>
46 #include <mach/task.h>
47 #include <mach/semaphore.h>
48 #include <kern/assert.h>
49 #include <kern/thread.h>
51 #include <libcfs/darwin/darwin-types.h>
52 #include <libcfs/darwin/darwin-sync.h>
55 * spin_lock (use Linux kernel's primitives)
62 * - spin_lock_irqsave(x, f)
63 * - spin_unlock_irqrestore(x, f)
69 typedef struct spin_lock spinlock_t;
71 static inline void spin_lock_init(spinlock_t *lock)
73 kspin_init(&lock->spin);
76 static inline void spin_lock(spinlock_t *lock)
78 kspin_lock(&lock->spin);
81 static inline void spin_unlock(spinlock_t *lock)
83 kspin_unlock(&lock->spin);
86 static inline int spin_trylock(spinlock_t *lock)
88 return kspin_trylock(&lock->spin);
91 static inline void spin_lock_done(spinlock_t *lock)
93 kspin_done(&lock->spin);
96 #error "does this lock out timer callbacks?"
97 #define spin_lock_bh(x) spin_lock(x)
98 #define spin_unlock_bh(x) spin_unlock(x)
99 #define spin_lock_bh_init(x) spin_lock_init(x)
101 extern boolean_t ml_set_interrupts_enabled(boolean_t enable);
102 #define __disable_irq() ml_set_interrupts_enabled(FALSE)
103 #define __enable_irq(x) (void) ml_set_interrupts_enabled(x)
105 #define spin_lock_irqsave(s, f) do{ \
106 f = __disable_irq(); \
107 spin_lock(s); }while(0)
109 #define spin_unlock_irqrestore(s, f) do{ \
111 __enable_irq(f);}while(0)
124 static inline void sema_init(struct semaphore *s, int val)
126 ksem_init(&s->sem, val);
129 static inline void __down(struct semaphore *s)
131 ksem_down(&s->sem, 1);
134 static inline void __up(struct semaphore *s)
143 * - init_mutex_locked(x)
148 #define mutex_up(s) __up(s)
149 #define mutex_down(s) __down(s)
151 #define init_mutex(x) sema_init(x, 1)
152 #define init_mutex_locked(x) sema_init(x, 0)
157 * - init_completion(c)
159 * - wait_for_completion(c)
163 * Emulate completion by semaphore for now.
165 * XXX nikita: this is not safe if completion is used to synchronize
166 * exit from kernel daemon thread and kext unloading. In this case
167 * some core function (a la complete_and_exit()) is needed.
172 static inline void init_completion(struct completion *c)
174 ksem_init(&c->sem, 0);
177 static inline void complete(struct completion *c)
182 static inline void wait_for_completion(struct completion *c)
184 ksem_down(&c->sem, 1);
197 struct rw_semaphore {
201 static inline void init_rwsem(struct rw_semaphore *s)
206 static inline void fini_rwsem(struct rw_semaphore *s)
211 static inline void down_read(struct rw_semaphore *s)
213 krw_sem_down_r(&s->s);
216 static inline int down_read_trylock(struct rw_semaphore *s)
218 int ret = krw_sem_down_r_try(&s->s);
222 static inline void down_write(struct rw_semaphore *s)
224 krw_sem_down_w(&s->s);
227 static inline int down_write_trylock(struct rw_semaphore *s)
229 int ret = krw_sem_down_w_try(&s->s);
233 static inline void up_read(struct rw_semaphore *s)
238 static inline void up_write(struct rw_semaphore *s)
244 * read-write lock : Need to be investigated more!!
246 * - DECLARE_RWLOCK(l)
253 typedef struct krw_spin rwlock_t;
255 #define rwlock_init(pl) krw_spin_init(pl)
257 #define read_lock(l) krw_spin_down_r(l)
258 #define read_unlock(l) krw_spin_up_r(l)
259 #define write_lock(l) krw_spin_down_w(l)
260 #define write_unlock(l) krw_spin_up_w(l)
262 #define write_lock_irqsave(l, f) do{ \
263 f = __disable_irq(); \
264 write_lock(l); }while(0)
266 #define write_unlock_irqrestore(l, f) do{ \
268 __enable_irq(f);}while(0)
270 #define read_lock_irqsave(l, f) do{ \
271 f = __disable_irq(); \
272 read_lock(l); }while(0)
274 #define read_unlock_irqrestore(l, f) do{ \
276 __enable_irq(f);}while(0)
284 #define CFS_DECL_FUNNEL_DATA
285 #define CFS_DECL_CONE_DATA DECLARE_FUNNEL_DATA
286 #define CFS_DECL_NET_DATA DECLARE_FUNNEL_DATA
287 #define CFS_CONE_IN do {} while(0)
288 #define CFS_CONE_EX do {} while(0)
290 #define CFS_NET_IN do {} while(0)
291 #define CFS_NET_EX do {} while(0)
295 #define CFS_DECL_FUNNEL_DATA \
296 boolean_t __funnel_state = FALSE; \
298 #define CFS_DECL_CONE_DATA CFS_DECL_FUNNEL_DATA
299 #define CFS_DECL_NET_DATA CFS_DECL_FUNNEL_DATA
301 void lustre_cone_in(boolean_t *state, funnel_t **cone);
302 void lustre_cone_ex(boolean_t state, funnel_t *cone);
304 #define CFS_CONE_IN lustre_cone_in(&__funnel_state, &__funnel)
305 #define CFS_CONE_EX lustre_cone_ex(__funnel_state, __funnel)
307 void lustre_net_in(boolean_t *state, funnel_t **cone);
308 void lustre_net_ex(boolean_t state, funnel_t *cone);
310 #define CFS_NET_IN lustre_net_in(&__funnel_state, &__funnel)
311 #define CFS_NET_EX lustre_net_ex(__funnel_state, __funnel)
316 #include <libcfs/user-lock.h>
317 #endif /* __KERNEL__ */
319 /* __XNU_CFS_LOCK_H */