Home | History | Annotate | Line # | Download | only in linux
ww_mutex.h revision 1.4.4.4
      1  1.4.4.4       snj /*	$NetBSD: ww_mutex.h,v 1.4.4.4 2015/07/30 15:50:15 snj 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.4.4.3       snj #include <sys/types.h>
     36  1.4.4.3       snj #include <sys/condvar.h>
     37  1.4.4.3       snj #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.4.4.2    martin 	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 	enum ww_mutex_state {
     60  1.4.4.2    martin 		WW_UNLOCKED,	/* nobody owns it */
     61  1.4.4.2    martin 		WW_OWNED,	/* owned by a lwp without a context */
     62  1.4.4.2    martin 		WW_CTX,		/* owned by a context */
     63  1.4.4.2    martin 		WW_WANTOWN,	/* owned by ctx, waiters w/o ctx waiting */
     64      1.1  riastrad 	}			wwm_state;
     65      1.1  riastrad 	union {
     66      1.1  riastrad 		struct lwp		*owner;
     67      1.1  riastrad 		struct ww_acquire_ctx	*ctx;
     68      1.1  riastrad 	}			wwm_u;
     69  1.4.4.4       snj 	/*
     70  1.4.4.4       snj 	 * XXX wwm_lock must *not* be first, so that the ww_mutex has a
     71  1.4.4.4       snj 	 * different address from the kmutex for LOCKDEBUG purposes.
     72  1.4.4.4       snj 	 */
     73  1.4.4.4       snj 	kmutex_t		wwm_lock;
     74      1.1  riastrad 	struct ww_class		*wwm_class;
     75      1.1  riastrad 	struct rb_tree		wwm_waiters;
     76      1.1  riastrad 	kcondvar_t		wwm_cv;
     77  1.4.4.4       snj #ifdef LOCKDEBUG
     78  1.4.4.4       snj 	bool			wwm_debug;
     79  1.4.4.4       snj #endif
     80      1.1  riastrad };
     81      1.1  riastrad 
     82  1.4.4.3       snj /* XXX Make the nm output a little more greppable...  */
     83  1.4.4.3       snj #define	ww_acquire_done		linux_ww_acquire_done
     84  1.4.4.3       snj #define	ww_acquire_fini		linux_ww_acquire_fini
     85  1.4.4.3       snj #define	ww_acquire_init		linux_ww_acquire_init
     86  1.4.4.3       snj #define	ww_mutex_destroy	linux_ww_mutex_destroy
     87  1.4.4.3       snj #define	ww_mutex_init		linux_ww_mutex_init
     88  1.4.4.3       snj #define	ww_mutex_is_locked	linux_ww_mutex_is_locked
     89  1.4.4.3       snj #define	ww_mutex_lock		linux_ww_mutex_lock
     90  1.4.4.3       snj #define	ww_mutex_lock_interruptible linux_ww_mutex_lock_interruptible
     91  1.4.4.3       snj #define	ww_mutex_lock_slow	linux_ww_mutex_lock_slow
     92  1.4.4.3       snj #define	ww_mutex_lock_slow_interruptible linux_ww_mutex_lock_slow_interruptible
     93  1.4.4.3       snj #define	ww_mutex_trylock	linux_ww_mutex_trylock
     94  1.4.4.3       snj #define	ww_mutex_unlock		linux_ww_mutex_unlock
     95  1.4.4.3       snj 
     96  1.4.4.3       snj void	ww_acquire_init(struct ww_acquire_ctx *, struct ww_class *);
     97  1.4.4.3       snj void	ww_acquire_done(struct ww_acquire_ctx *);
     98  1.4.4.3       snj void	ww_acquire_fini(struct ww_acquire_ctx *);
     99  1.4.4.3       snj 
    100  1.4.4.3       snj void	ww_mutex_init(struct ww_mutex *, struct ww_class *);
    101  1.4.4.3       snj void	ww_mutex_destroy(struct ww_mutex *);
    102      1.1  riastrad 
    103      1.1  riastrad /*
    104  1.4.4.3       snj  * WARNING: ww_mutex_is_locked returns true if it is locked by ANYONE.
    105  1.4.4.3       snj  * Does NOT mean `Do I hold this lock?' (answering which really
    106  1.4.4.3       snj  * requires an acquire context).
    107      1.1  riastrad  */
    108  1.4.4.3       snj bool	ww_mutex_is_locked(struct ww_mutex *);
    109      1.1  riastrad 
    110  1.4.4.3       snj int	ww_mutex_lock(struct ww_mutex *, struct ww_acquire_ctx *);
    111  1.4.4.3       snj int	ww_mutex_lock_interruptible(struct ww_mutex *,
    112  1.4.4.3       snj 	    struct ww_acquire_ctx *);
    113  1.4.4.3       snj void	ww_mutex_lock_slow(struct ww_mutex *, struct ww_acquire_ctx *);
    114  1.4.4.3       snj int	ww_mutex_lock_slow_interruptible(struct ww_mutex *,
    115  1.4.4.3       snj 	    struct ww_acquire_ctx *);
    116  1.4.4.3       snj int	ww_mutex_trylock(struct ww_mutex *);
    117  1.4.4.3       snj void	ww_mutex_unlock(struct ww_mutex *);
    118      1.1  riastrad 
    119      1.1  riastrad #endif  /* _ASM_WW_MUTEX_H_ */
    120