2 * Copyright (c) 2017 Mellanox Technologies, Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
28 #ifndef _LINUX_WW_MUTEX_H_
29 #define _LINUX_WW_MUTEX_H_
31 #include <sys/param.h>
33 #include <sys/condvar.h>
34 #include <sys/kernel.h>
36 #include <linux/mutex.h>
39 const char *mutex_name;
42 struct ww_acquire_ctx {
50 #define DEFINE_WW_CLASS(name) \
51 struct ww_class name = { \
52 .mutex_name = mutex_name(#name "_mutex") \
55 #define DEFINE_WW_MUTEX(name, ww_class) \
56 struct ww_mutex name; \
57 static void name##_init(void *arg) \
59 ww_mutex_init(&name, &ww_class); \
61 SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
63 #define ww_mutex_is_locked(_m) \
64 sx_xlocked(&(_m)->base.sx)
66 #define ww_mutex_lock_slow(_m, _x) \
69 #define ww_mutex_lock_slow_interruptible(_m, _x) \
70 ww_mutex_lock_interruptible(_m, _x)
72 static inline int __must_check
73 ww_mutex_trylock(struct ww_mutex *lock)
75 return (mutex_trylock(&lock->base));
78 extern int linux_ww_mutex_lock_sub(struct ww_mutex *, int catch_signal);
81 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
85 else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread)
88 return (linux_ww_mutex_lock_sub(lock, 0));
92 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
96 else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread)
99 return (linux_ww_mutex_lock_sub(lock, 1));
102 extern void linux_ww_mutex_unlock_sub(struct ww_mutex *);
105 ww_mutex_unlock(struct ww_mutex *lock)
110 linux_ww_mutex_unlock_sub(lock);
114 ww_mutex_destroy(struct ww_mutex *lock)
116 cv_destroy(&lock->condvar);
117 mutex_destroy(&lock->base);
121 ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class)
126 ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class)
128 linux_mutex_init(&lock->base, ww_class->mutex_name, SX_NOWITNESS);
129 cv_init(&lock->condvar, "lkpi-ww");
133 ww_acquire_fini(struct ww_acquire_ctx *ctx)
138 ww_acquire_done(struct ww_acquire_ctx *ctx)
142 #endif /* _LINUX_WW_MUTEX_H_ */