2 * Copyright (c) 2015 Michael Neumann <mneumann@ntecs.de>
4 * Copyright (c) 2003-2011 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Michael Neumann <mneumann@ntecs.de> and
8 * Matthew Dillon <dillon@backplane.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _LINUX_WW_MUTEX_H_
33 #define _LINUX_WW_MUTEX_H_
35 #include <linux/mutex.h>
38 * A basic, unoptimized implementation of wound/wait mutexes for DragonFly
39 * modelled after the Linux API [1].
41 * [1]: http://lxr.free-electrons.com/source/include/linux/ww_mutex.h
44 #include <sys/errno.h>
45 #include <sys/types.h>
46 #include <machine/atomic.h>
49 volatile u_long stamp;
53 struct ww_acquire_ctx {
57 struct ww_class *ww_class;
62 struct ww_acquire_ctx *ctx;
63 u_long stamp; /* heuristic */
68 #define DEFINE_WW_CLASS(classname) \
69 struct ww_class classname = { \
74 extern void ww_acquire_init(struct ww_acquire_ctx *ctx,
75 struct ww_class *ww_class);
76 extern void ww_acquire_done(struct ww_acquire_ctx *ctx);
77 extern void ww_acquire_fini(struct ww_acquire_ctx *ctx);
78 extern void ww_mutex_init(struct ww_mutex *ww, struct ww_class *ww_class);
79 extern int ww_mutex_lock(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
80 extern int ww_mutex_lock_slow(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
81 extern int ww_mutex_lock_interruptible(struct ww_mutex *ww,
82 struct ww_acquire_ctx *ctx);
83 extern int ww_mutex_lock_slow_interruptible(struct ww_mutex *ww,
84 struct ww_acquire_ctx *ctx);
85 extern void ww_mutex_unlock(struct ww_mutex *ww);
86 extern void ww_mutex_destroy(struct ww_mutex *ww);
89 * Returns 1 if locked, 0 otherwise
92 ww_mutex_is_locked(struct ww_mutex *ww)
94 return (lockstatus(&ww->base, NULL) != 0);
98 * Returns 1 on success, 0 if contended.
100 * This call has no context accounting.
103 ww_mutex_trylock(struct ww_mutex *ww)
105 return (lockmgr(&ww->base, LK_EXCLUSIVE|LK_NOWAIT) == 0);
108 #endif /* _LINUX_WW_MUTEX_H_ */