1913ced85SMichael Neumann /*
2913ced85SMichael Neumann * Copyright (c) 2015 Michael Neumann <mneumann@ntecs.de>
3913ced85SMichael Neumann * All rights reserved.
4*6cd4d95dSMatthew Dillon * Copyright (c) 2003-2011 The DragonFly Project. All rights reserved.
5*6cd4d95dSMatthew Dillon *
6*6cd4d95dSMatthew Dillon * This code is derived from software contributed to The DragonFly Project
7*6cd4d95dSMatthew Dillon * by Michael Neumann <mneumann@ntecs.de> and
8*6cd4d95dSMatthew Dillon * Matthew Dillon <dillon@backplane.com>
9913ced85SMichael Neumann *
10913ced85SMichael Neumann * Redistribution and use in source and binary forms, with or without
11913ced85SMichael Neumann * modification, are permitted provided that the following conditions
12913ced85SMichael Neumann * are met:
13913ced85SMichael Neumann * 1. Redistributions of source code must retain the above copyright
14913ced85SMichael Neumann * notice unmodified, this list of conditions, and the following
15913ced85SMichael Neumann * disclaimer.
16913ced85SMichael Neumann * 2. Redistributions in binary form must reproduce the above copyright
17913ced85SMichael Neumann * notice, this list of conditions and the following disclaimer in the
18913ced85SMichael Neumann * documentation and/or other materials provided with the distribution.
19913ced85SMichael Neumann *
20913ced85SMichael Neumann * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21913ced85SMichael Neumann * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22913ced85SMichael Neumann * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23913ced85SMichael Neumann * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24913ced85SMichael Neumann * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25913ced85SMichael Neumann * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26913ced85SMichael Neumann * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27913ced85SMichael Neumann * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28913ced85SMichael Neumann * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29913ced85SMichael Neumann * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30913ced85SMichael Neumann */
31913ced85SMichael Neumann
32913ced85SMichael Neumann #ifndef _LINUX_WW_MUTEX_H_
33913ced85SMichael Neumann #define _LINUX_WW_MUTEX_H_
34913ced85SMichael Neumann
35d6aa1cc5SFrançois Tigeot #include <linux/mutex.h>
36d6aa1cc5SFrançois Tigeot
37913ced85SMichael Neumann /*
38913ced85SMichael Neumann * A basic, unoptimized implementation of wound/wait mutexes for DragonFly
39913ced85SMichael Neumann * modelled after the Linux API [1].
40913ced85SMichael Neumann *
41913ced85SMichael Neumann * [1]: http://lxr.free-electrons.com/source/include/linux/ww_mutex.h
42913ced85SMichael Neumann */
43913ced85SMichael Neumann
44913ced85SMichael Neumann #include <sys/errno.h>
45913ced85SMichael Neumann #include <sys/types.h>
46913ced85SMichael Neumann #include <machine/atomic.h>
47913ced85SMichael Neumann
48913ced85SMichael Neumann struct ww_class {
49913ced85SMichael Neumann volatile u_long stamp;
50913ced85SMichael Neumann const char *name;
51913ced85SMichael Neumann };
52913ced85SMichael Neumann
53913ced85SMichael Neumann struct ww_acquire_ctx {
54913ced85SMichael Neumann u_long stamp;
55*6cd4d95dSMatthew Dillon int acquired;
56*6cd4d95dSMatthew Dillon int unused01;
57913ced85SMichael Neumann struct ww_class *ww_class;
58913ced85SMichael Neumann };
59913ced85SMichael Neumann
60913ced85SMichael Neumann struct ww_mutex {
61*6cd4d95dSMatthew Dillon struct lock base;
62*6cd4d95dSMatthew Dillon struct ww_acquire_ctx *ctx;
63*6cd4d95dSMatthew Dillon u_long stamp; /* heuristic */
64*6cd4d95dSMatthew Dillon int blocked;
65*6cd4d95dSMatthew Dillon int unused01;
66913ced85SMichael Neumann };
67913ced85SMichael Neumann
68913ced85SMichael Neumann #define DEFINE_WW_CLASS(classname) \
69ba55f2f5SFrançois Tigeot struct ww_class classname = { \
70913ced85SMichael Neumann .stamp = 0, \
71913ced85SMichael Neumann .name = #classname \
72913ced85SMichael Neumann }
73913ced85SMichael Neumann
74*6cd4d95dSMatthew Dillon extern void ww_acquire_init(struct ww_acquire_ctx *ctx,
75*6cd4d95dSMatthew Dillon struct ww_class *ww_class);
76*6cd4d95dSMatthew Dillon extern void ww_acquire_done(struct ww_acquire_ctx *ctx);
77*6cd4d95dSMatthew Dillon extern void ww_acquire_fini(struct ww_acquire_ctx *ctx);
78*6cd4d95dSMatthew Dillon extern void ww_mutex_init(struct ww_mutex *ww, struct ww_class *ww_class);
79*6cd4d95dSMatthew Dillon extern int ww_mutex_lock(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
80*6cd4d95dSMatthew Dillon extern int ww_mutex_lock_slow(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
81*6cd4d95dSMatthew Dillon extern int ww_mutex_lock_interruptible(struct ww_mutex *ww,
82*6cd4d95dSMatthew Dillon struct ww_acquire_ctx *ctx);
83*6cd4d95dSMatthew Dillon extern int ww_mutex_lock_slow_interruptible(struct ww_mutex *ww,
84*6cd4d95dSMatthew Dillon struct ww_acquire_ctx *ctx);
85*6cd4d95dSMatthew Dillon extern void ww_mutex_unlock(struct ww_mutex *ww);
86*6cd4d95dSMatthew Dillon extern void ww_mutex_destroy(struct ww_mutex *ww);
87913ced85SMichael Neumann
88*6cd4d95dSMatthew Dillon /*
89*6cd4d95dSMatthew Dillon * Returns 1 if locked, 0 otherwise
90*6cd4d95dSMatthew Dillon */
91913ced85SMichael Neumann static inline bool
ww_mutex_is_locked(struct ww_mutex * ww)92*6cd4d95dSMatthew Dillon ww_mutex_is_locked(struct ww_mutex *ww)
93*6cd4d95dSMatthew Dillon {
94*6cd4d95dSMatthew Dillon return (lockstatus(&ww->base, NULL) != 0);
95913ced85SMichael Neumann }
96913ced85SMichael Neumann
97913ced85SMichael Neumann /*
98*6cd4d95dSMatthew Dillon * Returns 1 on success, 0 if contended.
99*6cd4d95dSMatthew Dillon *
100*6cd4d95dSMatthew Dillon * This call has no context accounting.
101913ced85SMichael Neumann */
102913ced85SMichael Neumann static inline int
ww_mutex_trylock(struct ww_mutex * ww)103*6cd4d95dSMatthew Dillon ww_mutex_trylock(struct ww_mutex *ww)
104*6cd4d95dSMatthew Dillon {
105*6cd4d95dSMatthew Dillon return (lockmgr(&ww->base, LK_EXCLUSIVE|LK_NOWAIT) == 0);
106913ced85SMichael Neumann }
107913ced85SMichael Neumann
108913ced85SMichael Neumann #endif /* _LINUX_WW_MUTEX_H_ */
109