]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/include/linux/ww_mutex.h
Upgrade Unbound to 1.9.2.
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / include / linux / ww_mutex.h
1 /*-
2  * Copyright (c) 2017 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #ifndef _LINUX_WW_MUTEX_H_
29 #define _LINUX_WW_MUTEX_H_
30
31 #include <sys/param.h>
32 #include <sys/proc.h>
33 #include <sys/condvar.h>
34 #include <sys/kernel.h>
35
36 #include <linux/mutex.h>
37
38 struct ww_class {
39         const char *mutex_name;
40 };
41
42 struct ww_acquire_ctx {
43 };
44
45 struct ww_mutex {
46         struct mutex base;
47         struct cv condvar;
48         struct ww_acquire_ctx *ctx;
49 };
50
51 #define DEFINE_WW_CLASS(name)                                   \
52         struct ww_class name = {                                \
53                 .mutex_name = mutex_name(#name "_mutex")        \
54         }
55
56 #define DEFINE_WW_MUTEX(name, ww_class)                                 \
57         struct ww_mutex name;                                           \
58         static void name##_init(void *arg)                              \
59         {                                                               \
60                 ww_mutex_init(&name, &ww_class);                        \
61         }                                                               \
62         SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
63
64 #define ww_mutex_is_locked(_m) \
65         sx_xlocked(&(_m)->base.sx)
66
67 #define ww_mutex_lock_slow(_m, _x) \
68         ww_mutex_lock(_m, _x)
69
70 #define ww_mutex_lock_slow_interruptible(_m, _x) \
71         ww_mutex_lock_interruptible(_m, _x)
72
73 static inline int __must_check
74 ww_mutex_trylock(struct ww_mutex *lock)
75 {
76         return (mutex_trylock(&lock->base));
77 }
78
79 extern int linux_ww_mutex_lock_sub(struct ww_mutex *, int catch_signal);
80
81 static inline int
82 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
83 {
84         if (MUTEX_SKIP())
85                 return (0);
86         else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread)
87                 return (-EALREADY);
88         else
89                 return (linux_ww_mutex_lock_sub(lock, 0));
90 }
91
92 static inline int
93 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
94 {
95         if (MUTEX_SKIP())
96                 return (0);
97         else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread)
98                 return (-EALREADY);
99         else
100                 return (linux_ww_mutex_lock_sub(lock, 1));
101 }
102
103 extern void linux_ww_mutex_unlock_sub(struct ww_mutex *);
104
105 static inline void
106 ww_mutex_unlock(struct ww_mutex *lock)
107 {
108         if (MUTEX_SKIP())
109                 return;
110         else
111                 linux_ww_mutex_unlock_sub(lock);
112 }
113
114 static inline void
115 ww_mutex_destroy(struct ww_mutex *lock)
116 {
117         cv_destroy(&lock->condvar);
118         mutex_destroy(&lock->base);
119 }
120
121 static inline void
122 ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class)
123 {
124 }
125
126 static inline void
127 ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class)
128 {
129         linux_mutex_init(&lock->base, ww_class->mutex_name, SX_NOWITNESS);
130         cv_init(&lock->condvar, "lkpi-ww");
131 }
132
133 static inline void
134 ww_acquire_fini(struct ww_acquire_ctx *ctx)
135 {
136 }
137
138 static inline void
139 ww_acquire_done(struct ww_acquire_ctx *ctx)
140 {
141 }
142
143 #endif                                  /* _LINUX_WW_MUTEX_H_ */