]> git.proxmox.com Git - mirror_spl-debian.git/blob - include/linux-mutex.h
More build fixes, I have the kernel module almost building and its
[mirror_spl-debian.git] / include / linux-mutex.h
1 #ifndef _LINUX_MUTEX_H
2 #define _LINUX_MUTEX_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 /* See the "Big Theory Statement" in solaris mutex.c.
9 *
10 * Spin mutexes apparently aren't needed by zfs so we assert
11 * if ibc is non-zero.
12 *
13 * Our impementation of adaptive mutexes aren't really adaptive.
14 * They go to sleep every time.
15 */
16
17 #define MUTEX_DEFAULT 0
18 #define MUTEX_HELD(x) (mutex_owned(x))
19
20 #define KM_MAGIC 0x42424242
21 #define KM_POISON 0x84
22
23 typedef struct {
24 int km_magic;
25 char *km_name;
26 struct task_struct *km_owner;
27 struct semaphore km_sem;
28 } kmutex_t;
29
30 #undef mutex_init
31 static __inline__ void
32 mutex_init(kmutex_t *mp, char *name, int type, void *ibc)
33 {
34 BUG_ON(ibc != NULL); /* XXX - Spin mutexes not needed? */
35 BUG_ON(type != MUTEX_DEFAULT); /* XXX - Only default type supported? */
36
37 mp->km_magic = KM_MAGIC;
38 sema_init(&mp->km_sem, 1);
39 mp->km_owner = NULL;
40 mp->km_name = NULL;
41
42 if (name) {
43 mp->km_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
44 if (mp->km_name)
45 strcpy(mp->km_name, name);
46 }
47 }
48
49 #undef mutex_destroy
50 static __inline__ void
51 mutex_destroy(kmutex_t *mp)
52 {
53 BUG_ON(mp->km_magic != KM_MAGIC);
54
55 if (mp->km_name)
56 kfree(mp->km_name);
57
58 memset(mp, KM_POISON, sizeof(*mp));
59 }
60
61 static __inline__ void
62 mutex_enter(kmutex_t *mp)
63 {
64 BUG_ON(mp->km_magic != KM_MAGIC);
65 down(&mp->km_sem); /* Will check in_atomic() for us */
66 BUG_ON(mp->km_owner != NULL);
67 mp->km_owner = current;
68 }
69
70 /* Return 1 if we acquired the mutex, else zero.
71 */
72 static __inline__ int
73 mutex_tryenter(kmutex_t *mp)
74 {
75 int result;
76
77 BUG_ON(mp->km_magic != KM_MAGIC);
78 result = down_trylock(&mp->km_sem); /* returns 0 if acquired */
79 if (result == 0) {
80 BUG_ON(mp->km_owner != NULL);
81 mp->km_owner = current;
82 return 1;
83 }
84 return 0;
85 }
86
87 static __inline__ void
88 mutex_exit(kmutex_t *mp)
89 {
90 BUG_ON(mp->km_magic != KM_MAGIC);
91 BUG_ON(mp->km_owner != current);
92 mp->km_owner = NULL;
93 up(&mp->km_sem);
94 }
95
96 /* Return 1 if mutex is held by current process, else zero.
97 */
98 static __inline__ int
99 mutex_owned(kmutex_t *mp)
100 {
101 BUG_ON(mp->km_magic != KM_MAGIC);
102 return (mp->km_owner == current);
103 }
104
105 /* Return owner if mutex is owned, else NULL.
106 */
107 static __inline__ kthread_t *
108 mutex_owner(kmutex_t *mp)
109 {
110 BUG_ON(mp->km_magic != KM_MAGIC);
111 return mp->km_owner;
112 }
113
114 #ifdef __cplusplus
115 }
116 #endif
117
118 #endif /* _LINUX_MUTEX_H */