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 <mach/mach_traps.h>
14 /* spin lock types and operations */
15 #include <kern/simple_lock.h>
16 #include <kern/assert.h>
17 #include <kern/thread.h>
19 #include <libcfs/darwin/darwin-types.h>
20 #include <libcfs/darwin/darwin-sync.h>
23 * spin_lock (use Linux kernel's primitives)
30 * - spin_lock_irqsave(x, f)
31 * - spin_unlock_irqrestore(x, f)
37 typedef struct spin_lock spinlock_t;
39 static inline void spin_lock_init(spinlock_t *lock)
41 kspin_init(&lock->spin);
44 static inline void spin_lock(spinlock_t *lock)
46 kspin_lock(&lock->spin);
49 static inline void spin_unlock(spinlock_t *lock)
51 kspin_unlock(&lock->spin);
54 static inline int spin_trylock(spinlock_t *lock)
56 return kspin_trylock(&lock->spin);
59 #define spin_lock_bh(x) spin_lock(x)
60 #define spin_unlock_bh(x) spin_unlock(x)
61 #define spin_lock_bh_init(x) spin_lock_init(x)
63 extern boolean_t ml_set_interrupts_enabled(boolean_t enable);
64 #define __disable_irq() (spl_t) ml_set_interrupts_enabled(FALSE)
65 #define __enable_irq(x) (void) ml_set_interrupts_enabled(x)
67 #define spin_lock_irqsave(s, f) do{ \
68 f = __disable_irq(); \
69 spin_lock(s); }while(0)
71 #define spin_unlock_irqrestore(s, f) do{ \
73 __enable_irq(f);}while(0)
86 static inline void sema_init(struct semaphore *s, int val)
88 ksem_init(&s->sem, val);
91 static inline void __down(struct semaphore *s)
93 ksem_down(&s->sem, 1);
96 static inline void __up(struct semaphore *s)
105 * - init_mutex_locked(x)
110 #define mutex_up(s) __up(s)
111 #define mutex_down(s) __down(s)
113 #define init_mutex(x) sema_init(x, 1)
114 #define init_mutex_locked(x) sema_init(x, 0)
119 * - init_completion(c)
121 * - wait_for_completion(c)
125 * Emulate completion by semaphore for now.
127 * XXX nikita: this is not safe if completion is used to synchronize
128 * exit from kernel daemon thread and kext unloading. In this case
129 * some core function (a la complete_and_exit()) is needed.
134 static inline void init_completion(struct completion *c)
136 ksem_init(&c->sem, 0);
139 static inline void complete(struct completion *c)
144 static inline void wait_for_completion(struct completion *c)
146 ksem_down(&c->sem, 1);
159 struct rw_semaphore {
163 static inline void init_rwsem(struct rw_semaphore *s)
168 static inline void down_read(struct rw_semaphore *s)
170 krw_sem_down_r(&s->s);
173 static inline int down_read_trylock(struct rw_semaphore *s)
175 int ret = krw_sem_down_r_try(&s->s);
176 return ret == 0? 1: 0;
179 static inline void down_write(struct rw_semaphore *s)
181 krw_sem_down_w(&s->s);
184 static inline int down_write_trylock(struct rw_semaphore *s)
186 int ret = krw_sem_down_w_try(&s->s);
187 return ret == 0? 1: 0;
190 static inline void up_read(struct rw_semaphore *s)
195 static inline void up_write(struct rw_semaphore *s)
201 * read-write lock : Need to be investigated more!!
202 * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
204 * - DECLARE_RWLOCK(l)
211 typedef struct rw_semaphore rwlock_t;
213 #define rwlock_init(pl) init_rwsem(pl)
215 #define read_lock(l) down_read(l)
216 #define read_unlock(l) up_read(l)
217 #define write_lock(l) down_write(l)
218 #define write_unlock(l) up_write(l)
220 #define write_lock_irqsave(l, f) do{ \
221 f = __disable_irq(); \
222 write_lock(l); }while(0)
224 #define write_unlock_irqrestore(l, f) do{ \
226 __enable_irq(f);}while(0)
228 #define read_lock_irqsave(l, f) do{ \
229 f = __disable_irq(); \
230 read_lock(l); }while(0)
232 #define read_unlock_irqrestore(l, f) do{ \
234 __enable_irq(f);}while(0)
242 #define CFS_DECL_FUNNEL_DATA \
243 boolean_t __funnel_state = FALSE; \
245 #define CFS_DECL_CONE_DATA CFS_DECL_FUNNEL_DATA
246 #define CFS_DECL_NET_DATA CFS_DECL_FUNNEL_DATA
248 void lustre_cone_in(boolean_t *state, funnel_t **cone);
249 void lustre_cone_ex(boolean_t state, funnel_t *cone);
251 #define CFS_CONE_IN lustre_cone_in(&__funnel_state, &__funnel)
252 #define CFS_CONE_EX lustre_cone_ex(__funnel_state, __funnel)
254 void lustre_net_in(boolean_t *state, funnel_t **cone);
255 void lustre_net_ex(boolean_t state, funnel_t *cone);
257 #define CFS_NET_IN lustre_net_in(&__funnel_state, &__funnel)
258 #define CFS_NET_EX lustre_net_ex(__funnel_state, __funnel)
263 /* __XNU_CFS_LOCK_H */