ww_mutex.h revision 1.10 1 1.10 riastrad /* $NetBSD: ww_mutex.h,v 1.10 2015/01/08 23:35:47 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.1 riastrad struct ww_class {
41 1.1 riastrad volatile uint64_t wwc_ticket;
42 1.1 riastrad };
43 1.1 riastrad
44 1.1 riastrad #define DEFINE_WW_CLASS(CLASS) \
45 1.1 riastrad struct ww_class CLASS = { \
46 1.1 riastrad .wwc_ticket = 0, \
47 1.1 riastrad }
48 1.1 riastrad
49 1.1 riastrad struct ww_acquire_ctx {
50 1.1 riastrad struct ww_class *wwx_class __diagused;
51 1.7 riastrad struct lwp *wwx_owner __diagused;
52 1.1 riastrad uint64_t wwx_ticket;
53 1.1 riastrad unsigned wwx_acquired;
54 1.1 riastrad bool wwx_acquire_done;
55 1.1 riastrad struct rb_node wwx_rb_node;
56 1.1 riastrad };
57 1.1 riastrad
58 1.1 riastrad struct ww_mutex {
59 1.1 riastrad kmutex_t wwm_lock;
60 1.1 riastrad enum ww_mutex_state {
61 1.7 riastrad WW_UNLOCKED, /* nobody owns it */
62 1.7 riastrad WW_OWNED, /* owned by a lwp without a context */
63 1.7 riastrad WW_CTX, /* owned by a context */
64 1.7 riastrad WW_WANTOWN, /* owned by ctx, waiters w/o ctx waiting */
65 1.1 riastrad } wwm_state;
66 1.1 riastrad union {
67 1.1 riastrad struct lwp *owner;
68 1.1 riastrad struct ww_acquire_ctx *ctx;
69 1.1 riastrad } wwm_u;
70 1.1 riastrad struct ww_class *wwm_class;
71 1.1 riastrad struct rb_tree wwm_waiters;
72 1.1 riastrad kcondvar_t wwm_cv;
73 1.1 riastrad };
74 1.1 riastrad
75 1.10 riastrad /* XXX Make the nm output a little more greppable... */
76 1.10 riastrad #define ww_acquire_done linux_ww_acquire_done
77 1.10 riastrad #define ww_acquire_fini linux_ww_acquire_fini
78 1.10 riastrad #define ww_acquire_init linux_ww_acquire_init
79 1.10 riastrad #define ww_mutex_destroy linux_ww_mutex_destroy
80 1.10 riastrad #define ww_mutex_init linux_ww_mutex_init
81 1.10 riastrad #define ww_mutex_is_locked linux_ww_mutex_is_locked
82 1.10 riastrad #define ww_mutex_lock linux_ww_mutex_lock
83 1.10 riastrad #define ww_mutex_lock_interruptible linux_ww_mutex_lock_interruptible
84 1.10 riastrad #define ww_mutex_lock_slow linux_ww_mutex_lock_slow
85 1.10 riastrad #define ww_mutex_lock_slow_interruptible linux_ww_mutex_lock_slow_interruptible
86 1.10 riastrad #define ww_mutex_trylock linux_ww_mutex_trylock
87 1.10 riastrad #define ww_mutex_unlock linux_ww_mutex_unlock
88 1.10 riastrad
89 1.10 riastrad void ww_acquire_init(struct ww_acquire_ctx *, struct ww_class *);
90 1.10 riastrad void ww_acquire_done(struct ww_acquire_ctx *);
91 1.10 riastrad void ww_acquire_fini(struct ww_acquire_ctx *);
92 1.10 riastrad
93 1.10 riastrad void ww_mutex_init(struct ww_mutex *, struct ww_class *);
94 1.10 riastrad void ww_mutex_destroy(struct ww_mutex *);
95 1.1 riastrad
96 1.1 riastrad /*
97 1.10 riastrad * WARNING: ww_mutex_is_locked returns true if it is locked by ANYONE.
98 1.10 riastrad * Does NOT mean `Do I hold this lock?' (answering which really
99 1.10 riastrad * requires an acquire context).
100 1.1 riastrad */
101 1.10 riastrad bool ww_mutex_is_locked(struct ww_mutex *);
102 1.1 riastrad
103 1.10 riastrad int ww_mutex_lock(struct ww_mutex *, struct ww_acquire_ctx *);
104 1.10 riastrad int ww_mutex_lock_interruptible(struct ww_mutex *,
105 1.10 riastrad struct ww_acquire_ctx *);
106 1.10 riastrad void ww_mutex_lock_slow(struct ww_mutex *, struct ww_acquire_ctx *);
107 1.10 riastrad int ww_mutex_lock_slow_interruptible(struct ww_mutex *,
108 1.10 riastrad struct ww_acquire_ctx *);
109 1.10 riastrad int ww_mutex_trylock(struct ww_mutex *);
110 1.10 riastrad void ww_mutex_unlock(struct ww_mutex *);
111 1.1 riastrad
112 1.1 riastrad #endif /* _ASM_WW_MUTEX_H_ */
113