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