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