1 #ifndef __LIBCFS_DARWIN_CFS_LOCK_H__
2 #define __LIBCFS_DARWIN_CFS_LOCK_H__
4 #ifndef __LIBCFS_LIBCFS_H__
5 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
9 #include <mach/sync_policy.h>
10 #include <mach/task.h>
11 #include <mach/semaphore.h>
12 #include <kern/assert.h>
13 #include <kern/thread.h>
15 #include <libcfs/darwin/darwin-types.h>
16 #include <libcfs/darwin/darwin-sync.h>
19 * spin_lock (use Linux kernel's primitives)
26 * - spin_lock_irqsave(x, f)
27 * - spin_unlock_irqrestore(x, f)
33 typedef struct spin_lock spinlock_t;
35 static inline void spin_lock_init(spinlock_t *lock)
37 kspin_init(&lock->spin);
40 static inline void spin_lock(spinlock_t *lock)
42 kspin_lock(&lock->spin);
45 static inline void spin_unlock(spinlock_t *lock)
47 kspin_unlock(&lock->spin);
50 static inline int spin_trylock(spinlock_t *lock)
52 return kspin_trylock(&lock->spin);
55 static inline void spin_lock_done(spinlock_t *lock)
57 kspin_done(&lock->spin);
60 #error "does this lock out timer callbacks?"
61 #define spin_lock_bh(x) spin_lock(x)
62 #define spin_unlock_bh(x) spin_unlock(x)
63 #define spin_lock_bh_init(x) spin_lock_init(x)
65 extern boolean_t ml_set_interrupts_enabled(boolean_t enable);
66 #define __disable_irq() ml_set_interrupts_enabled(FALSE)
67 #define __enable_irq(x) (void) ml_set_interrupts_enabled(x)
69 #define spin_lock_irqsave(s, f) do{ \
70 f = __disable_irq(); \
71 spin_lock(s); }while(0)
73 #define spin_unlock_irqrestore(s, f) do{ \
75 __enable_irq(f);}while(0)
88 static inline void sema_init(struct semaphore *s, int val)
90 ksem_init(&s->sem, val);
93 static inline void __down(struct semaphore *s)
95 ksem_down(&s->sem, 1);
98 static inline void __up(struct semaphore *s)
107 * - init_mutex_locked(x)
112 #define mutex_up(s) __up(s)
113 #define mutex_down(s) __down(s)
115 #define init_mutex(x) sema_init(x, 1)
116 #define init_mutex_locked(x) sema_init(x, 0)
121 * - init_completion(c)
123 * - wait_for_completion(c)
127 * Emulate completion by semaphore for now.
129 * XXX nikita: this is not safe if completion is used to synchronize
130 * exit from kernel daemon thread and kext unloading. In this case
131 * some core function (a la complete_and_exit()) is needed.
136 static inline void init_completion(struct completion *c)
138 ksem_init(&c->sem, 0);
141 static inline void complete(struct completion *c)
146 static inline void wait_for_completion(struct completion *c)
148 ksem_down(&c->sem, 1);
161 struct rw_semaphore {
165 static inline void init_rwsem(struct rw_semaphore *s)
170 static inline void fini_rwsem(struct rw_semaphore *s)
175 static inline void down_read(struct rw_semaphore *s)
177 krw_sem_down_r(&s->s);
180 static inline int down_read_trylock(struct rw_semaphore *s)
182 int ret = krw_sem_down_r_try(&s->s);
186 static inline void down_write(struct rw_semaphore *s)
188 krw_sem_down_w(&s->s);
191 static inline int down_write_trylock(struct rw_semaphore *s)
193 int ret = krw_sem_down_w_try(&s->s);
197 static inline void up_read(struct rw_semaphore *s)
202 static inline void up_write(struct rw_semaphore *s)
208 * read-write lock : Need to be investigated more!!
210 * - DECLARE_RWLOCK(l)
217 typedef struct krw_spin rwlock_t;
219 #define rwlock_init(pl) krw_spin_init(pl)
221 #define read_lock(l) krw_spin_down_r(l)
222 #define read_unlock(l) krw_spin_up_r(l)
223 #define write_lock(l) krw_spin_down_w(l)
224 #define write_unlock(l) krw_spin_up_w(l)
226 #define write_lock_irqsave(l, f) do{ \
227 f = __disable_irq(); \
228 write_lock(l); }while(0)
230 #define write_unlock_irqrestore(l, f) do{ \
232 __enable_irq(f);}while(0)
234 #define read_lock_irqsave(l, f) do{ \
235 f = __disable_irq(); \
236 read_lock(l); }while(0)
238 #define read_unlock_irqrestore(l, f) do{ \
240 __enable_irq(f);}while(0)
248 #define CFS_DECL_FUNNEL_DATA
249 #define CFS_DECL_CONE_DATA DECLARE_FUNNEL_DATA
250 #define CFS_DECL_NET_DATA DECLARE_FUNNEL_DATA
251 #define CFS_CONE_IN do {} while(0)
252 #define CFS_CONE_EX do {} while(0)
254 #define CFS_NET_IN do {} while(0)
255 #define CFS_NET_EX do {} while(0)
259 #define CFS_DECL_FUNNEL_DATA \
260 boolean_t __funnel_state = FALSE; \
262 #define CFS_DECL_CONE_DATA CFS_DECL_FUNNEL_DATA
263 #define CFS_DECL_NET_DATA CFS_DECL_FUNNEL_DATA
265 void lustre_cone_in(boolean_t *state, funnel_t **cone);
266 void lustre_cone_ex(boolean_t state, funnel_t *cone);
268 #define CFS_CONE_IN lustre_cone_in(&__funnel_state, &__funnel)
269 #define CFS_CONE_EX lustre_cone_ex(__funnel_state, __funnel)
271 void lustre_net_in(boolean_t *state, funnel_t **cone);
272 void lustre_net_ex(boolean_t state, funnel_t *cone);
274 #define CFS_NET_IN lustre_net_in(&__funnel_state, &__funnel)
275 #define CFS_NET_EX lustre_net_ex(__funnel_state, __funnel)
280 #include <libcfs/user-lock.h>
281 #endif /* __KERNEL__ */
283 /* __XNU_CFS_LOCK_H */