ww_mutex.h revision 1.12 1 1.12 riastrad /* $NetBSD: ww_mutex.h,v 1.12 2018/08/27 06:06:41 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #ifndef _ASM_WW_MUTEX_H_
33 1.1 riastrad #define _ASM_WW_MUTEX_H_
34 1.1 riastrad
35 1.10 riastrad #include <sys/types.h>
36 1.10 riastrad #include <sys/condvar.h>
37 1.10 riastrad #include <sys/mutex.h>
38 1.1 riastrad #include <sys/rbtree.h>
39 1.1 riastrad
40 1.12 riastrad #include <linux/mutex.h>
41 1.12 riastrad
42 1.1 riastrad struct ww_class {
43 1.1 riastrad volatile uint64_t wwc_ticket;
44 1.1 riastrad };
45 1.1 riastrad
46 1.1 riastrad #define DEFINE_WW_CLASS(CLASS) \
47 1.1 riastrad struct ww_class CLASS = { \
48 1.1 riastrad .wwc_ticket = 0, \
49 1.1 riastrad }
50 1.1 riastrad
51 1.1 riastrad struct ww_acquire_ctx {
52 1.1 riastrad struct ww_class *wwx_class __diagused;
53 1.7 riastrad struct lwp *wwx_owner __diagused;
54 1.1 riastrad uint64_t wwx_ticket;
55 1.1 riastrad unsigned wwx_acquired;
56 1.1 riastrad bool wwx_acquire_done;
57 1.1 riastrad struct rb_node wwx_rb_node;
58 1.1 riastrad };
59 1.1 riastrad
60 1.1 riastrad struct ww_mutex {
61 1.1 riastrad enum ww_mutex_state {
62 1.7 riastrad WW_UNLOCKED, /* nobody owns it */
63 1.7 riastrad WW_OWNED, /* owned by a lwp without a context */
64 1.7 riastrad WW_CTX, /* owned by a context */
65 1.7 riastrad WW_WANTOWN, /* owned by ctx, waiters w/o ctx waiting */
66 1.1 riastrad } wwm_state;
67 1.1 riastrad union {
68 1.1 riastrad struct lwp *owner;
69 1.1 riastrad struct ww_acquire_ctx *ctx;
70 1.1 riastrad } wwm_u;
71 1.11 riastrad /*
72 1.11 riastrad * XXX wwm_lock must *not* be first, so that the ww_mutex has a
73 1.11 riastrad * different address from the kmutex for LOCKDEBUG purposes.
74 1.11 riastrad */
75 1.11 riastrad kmutex_t wwm_lock;
76 1.1 riastrad struct ww_class *wwm_class;
77 1.1 riastrad struct rb_tree wwm_waiters;
78 1.1 riastrad kcondvar_t wwm_cv;
79 1.11 riastrad #ifdef LOCKDEBUG
80 1.11 riastrad bool wwm_debug;
81 1.11 riastrad #endif
82 1.1 riastrad };
83 1.1 riastrad
84 1.10 riastrad /* XXX Make the nm output a little more greppable... */
85 1.10 riastrad #define ww_acquire_done linux_ww_acquire_done
86 1.10 riastrad #define ww_acquire_fini linux_ww_acquire_fini
87 1.10 riastrad #define ww_acquire_init linux_ww_acquire_init
88 1.10 riastrad #define ww_mutex_destroy linux_ww_mutex_destroy
89 1.10 riastrad #define ww_mutex_init linux_ww_mutex_init
90 1.10 riastrad #define ww_mutex_is_locked linux_ww_mutex_is_locked
91 1.10 riastrad #define ww_mutex_lock linux_ww_mutex_lock
92 1.10 riastrad #define ww_mutex_lock_interruptible linux_ww_mutex_lock_interruptible
93 1.10 riastrad #define ww_mutex_lock_slow linux_ww_mutex_lock_slow
94 1.10 riastrad #define ww_mutex_lock_slow_interruptible linux_ww_mutex_lock_slow_interruptible
95 1.10 riastrad #define ww_mutex_trylock linux_ww_mutex_trylock
96 1.10 riastrad #define ww_mutex_unlock linux_ww_mutex_unlock
97 1.10 riastrad
98 1.10 riastrad void ww_acquire_init(struct ww_acquire_ctx *, struct ww_class *);
99 1.10 riastrad void ww_acquire_done(struct ww_acquire_ctx *);
100 1.10 riastrad void ww_acquire_fini(struct ww_acquire_ctx *);
101 1.10 riastrad
102 1.10 riastrad void ww_mutex_init(struct ww_mutex *, struct ww_class *);
103 1.10 riastrad void ww_mutex_destroy(struct ww_mutex *);
104 1.1 riastrad
105 1.1 riastrad /*
106 1.10 riastrad * WARNING: ww_mutex_is_locked returns true if it is locked by ANYONE.
107 1.10 riastrad * Does NOT mean `Do I hold this lock?' (answering which really
108 1.10 riastrad * requires an acquire context).
109 1.1 riastrad */
110 1.10 riastrad bool ww_mutex_is_locked(struct ww_mutex *);
111 1.1 riastrad
112 1.10 riastrad int ww_mutex_lock(struct ww_mutex *, struct ww_acquire_ctx *);
113 1.10 riastrad int ww_mutex_lock_interruptible(struct ww_mutex *,
114 1.10 riastrad struct ww_acquire_ctx *);
115 1.10 riastrad void ww_mutex_lock_slow(struct ww_mutex *, struct ww_acquire_ctx *);
116 1.10 riastrad int ww_mutex_lock_slow_interruptible(struct ww_mutex *,
117 1.10 riastrad struct ww_acquire_ctx *);
118 1.10 riastrad int ww_mutex_trylock(struct ww_mutex *);
119 1.10 riastrad void ww_mutex_unlock(struct ww_mutex *);
120 1.1 riastrad
121 1.1 riastrad #endif /* _ASM_WW_MUTEX_H_ */
122