]>
git.proxmox.com Git - mirror_spl.git/blob - include/sys/mutex.h
ca76d6ea9b942f522ccc8aaf14739b266c6f4238
8 #include <linux/module.h>
9 #include <linux/hardirq.h>
10 #include <sys/types.h>
12 /* See the "Big Theory Statement" in solaris mutex.c.
14 * Spin mutexes apparently aren't needed by zfs so we assert
17 * Our impementation of adaptive mutexes aren't really adaptive.
18 * They go to sleep every time.
21 #define MUTEX_DEFAULT 0
22 #define MUTEX_HELD(x) (mutex_owned(x))
24 #define KM_MAGIC 0x42424242
25 #define KM_POISON 0x84
30 struct task_struct
*km_owner
;
31 struct semaphore km_sem
;
35 static __inline__
void
36 mutex_init(kmutex_t
*mp
, char *name
, int type
, void *ibc
)
38 BUG_ON(ibc
!= NULL
); /* XXX - Spin mutexes not needed? */
39 BUG_ON(type
!= MUTEX_DEFAULT
); /* XXX - Only default type supported? */
41 mp
->km_magic
= KM_MAGIC
;
42 sema_init(&mp
->km_sem
, 1);
47 mp
->km_name
= kmalloc(strlen(name
) + 1, GFP_KERNEL
);
49 strcpy(mp
->km_name
, name
);
54 static __inline__
void
55 mutex_destroy(kmutex_t
*mp
)
57 BUG_ON(mp
->km_magic
!= KM_MAGIC
);
62 memset(mp
, KM_POISON
, sizeof(*mp
));
65 static __inline__
void
66 mutex_enter(kmutex_t
*mp
)
68 BUG_ON(mp
->km_magic
!= KM_MAGIC
);
70 if (unlikely(in_atomic() && !current
->exit_state
)) {
72 printk("Scheduling while atomic: %s/0x%08x/%d\n",
73 current
->comm
, preempt_count(), current
->pid
);
77 down(&mp
->km_sem
); /* Will check in_atomic() for us */
78 BUG_ON(mp
->km_owner
!= NULL
);
79 mp
->km_owner
= current
;
82 /* Return 1 if we acquired the mutex, else zero.
85 mutex_tryenter(kmutex_t
*mp
)
89 BUG_ON(mp
->km_magic
!= KM_MAGIC
);
91 if (unlikely(in_atomic() && !current
->exit_state
)) {
93 printk("Scheduling while atomic: %s/0x%08x/%d\n",
94 current
->comm
, preempt_count(), current
->pid
);
98 result
= down_trylock(&mp
->km_sem
); /* returns 0 if acquired */
100 BUG_ON(mp
->km_owner
!= NULL
);
101 mp
->km_owner
= current
;
107 static __inline__
void
108 mutex_exit(kmutex_t
*mp
)
110 BUG_ON(mp
->km_magic
!= KM_MAGIC
);
111 BUG_ON(mp
->km_owner
!= current
);
116 /* Return 1 if mutex is held by current process, else zero.
118 static __inline__
int
119 mutex_owned(kmutex_t
*mp
)
121 BUG_ON(mp
->km_magic
!= KM_MAGIC
);
122 return (mp
->km_owner
== current
);
125 /* Return owner if mutex is owned, else NULL.
127 static __inline__ kthread_t
*
128 mutex_owner(kmutex_t
*mp
)
130 BUG_ON(mp
->km_magic
!= KM_MAGIC
);
138 #endif /* _SPL_MUTEX_H */