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