]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/include/linux/mutex.h
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / include / linux / mutex.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef _LINUX_MUTEX_H_
32 #define _LINUX_MUTEX_H_
33
34 #include <sys/param.h>
35 #include <sys/proc.h>
36 #include <sys/lock.h>
37 #include <sys/sx.h>
38
39 #include <linux/spinlock.h>
40 #include <asm/atomic.h>
41
42 typedef struct mutex {
43         struct sx sx;
44 } mutex_t;
45
46 /*
47  * By defining CONFIG_NO_MUTEX_SKIP LinuxKPI mutexes and asserts will
48  * not be skipped during panic().
49  */
50 #ifdef CONFIG_NO_MUTEX_SKIP
51 #define MUTEX_SKIP(void) 0
52 #else
53 #define MUTEX_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active)
54 #endif
55
56 #define mutex_lock(_m) do {                     \
57         if (MUTEX_SKIP())                       \
58                 break;                          \
59         sx_xlock(&(_m)->sx);                    \
60 } while (0)
61
62 #define mutex_lock_nested(_m, _s)       mutex_lock(_m)
63 #define mutex_lock_nest_lock(_m, _s)    mutex_lock(_m)
64
65 #define mutex_lock_interruptible(_m) ({         \
66         MUTEX_SKIP() ? 0 :                      \
67         linux_mutex_lock_interruptible(_m);     \
68 })
69
70 #define mutex_lock_interruptible_nested(m, c)   mutex_lock_interruptible(m)
71
72 /*
73  * Reuse the interruptable method since the SX
74  * lock handles both signals and interrupts:
75  */
76 #define mutex_lock_killable(_m) ({              \
77         MUTEX_SKIP() ? 0 :                      \
78         linux_mutex_lock_interruptible(_m);     \
79 })
80
81 #define mutex_lock_killable_nested(_m, _sub)    \
82         mutex_lock_killable(_m)
83
84 #define mutex_unlock(_m) do {                   \
85         if (MUTEX_SKIP())                       \
86                 break;                          \
87         sx_xunlock(&(_m)->sx);                  \
88 } while (0)
89
90 #define mutex_trylock(_m) ({                    \
91         MUTEX_SKIP() ? 1 :                      \
92         !!sx_try_xlock(&(_m)->sx);              \
93 })
94
95 enum mutex_trylock_recursive_enum {
96         MUTEX_TRYLOCK_FAILED = 0,
97         MUTEX_TRYLOCK_SUCCESS = 1,
98         MUTEX_TRYLOCK_RECURSIVE = 2,
99 };
100
101 static inline __must_check enum mutex_trylock_recursive_enum
102 mutex_trylock_recursive(struct mutex *lock)
103 {
104         if (unlikely(sx_xholder(&lock->sx) == curthread))
105                 return (MUTEX_TRYLOCK_RECURSIVE);
106
107         return (mutex_trylock(lock));
108 }
109
110 #define mutex_init(_m) \
111         linux_mutex_init(_m, mutex_name(#_m), SX_NOWITNESS)
112
113 #define __mutex_init(_m, _n, _l) \
114         linux_mutex_init(_m, _n, SX_NOWITNESS)
115
116 #define mutex_init_witness(_m) \
117         linux_mutex_init(_m, mutex_name(#_m), SX_DUPOK)
118
119 #define mutex_destroy(_m) \
120         linux_mutex_destroy(_m)
121
122 static inline bool
123 mutex_is_locked(mutex_t *m)
124 {
125         return ((struct thread *)SX_OWNER(m->sx.sx_lock) != NULL);
126 }
127
128 static inline bool
129 mutex_is_owned(mutex_t *m)
130 {
131         return (sx_xlocked(&m->sx));
132 }
133
134 static inline int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *m)
135 {
136         if (atomic_dec_and_test(cnt)) {
137                 mutex_lock(m);
138                 return (1);
139         }
140
141         return (0);
142 }
143
144 #ifdef WITNESS_ALL
145 /* NOTE: the maximum WITNESS name is 64 chars */
146 #define __mutex_name(name, file, line)          \
147         (((const char *){file ":" #line "-" name}) +    \
148         (sizeof(file) > 16 ? sizeof(file) - 16 : 0))
149 #else
150 #define __mutex_name(name, file, line)  name
151 #endif
152 #define _mutex_name(...)        __mutex_name(__VA_ARGS__)
153 #define mutex_name(name)        _mutex_name(name, __FILE__, __LINE__)
154
155 #define DEFINE_MUTEX(lock)                                              \
156         mutex_t lock;                                                   \
157         SX_SYSINIT_FLAGS(lock, &(lock).sx, mutex_name(#lock), SX_DUPOK)
158
159 static inline void
160 linux_mutex_init(mutex_t *m, const char *name, int flags)
161 {
162         memset(m, 0, sizeof(*m));
163         sx_init_flags(&m->sx, name, flags);
164 }
165
166 static inline void
167 linux_mutex_destroy(mutex_t *m)
168 {
169         if (mutex_is_owned(m))
170                 mutex_unlock(m);
171         sx_destroy(&m->sx);
172 }
173
174 extern int linux_mutex_lock_interruptible(mutex_t *m);
175
176 #endif                                  /* _LINUX_MUTEX_H_ */