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