xref: /netbsd-src/sys/external/bsd/drm2/include/linux/ww_mutex.h (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: ww_mutex.h,v 1.10 2015/01/08 23:35:47 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 	kmutex_t		wwm_lock;
60 	enum ww_mutex_state {
61 		WW_UNLOCKED,	/* nobody owns it */
62 		WW_OWNED,	/* owned by a lwp without a context */
63 		WW_CTX,		/* owned by a context */
64 		WW_WANTOWN,	/* owned by ctx, waiters w/o ctx waiting */
65 	}			wwm_state;
66 	union {
67 		struct lwp		*owner;
68 		struct ww_acquire_ctx	*ctx;
69 	}			wwm_u;
70 	struct ww_class		*wwm_class;
71 	struct rb_tree		wwm_waiters;
72 	kcondvar_t		wwm_cv;
73 };
74 
75 /* XXX Make the nm output a little more greppable...  */
76 #define	ww_acquire_done		linux_ww_acquire_done
77 #define	ww_acquire_fini		linux_ww_acquire_fini
78 #define	ww_acquire_init		linux_ww_acquire_init
79 #define	ww_mutex_destroy	linux_ww_mutex_destroy
80 #define	ww_mutex_init		linux_ww_mutex_init
81 #define	ww_mutex_is_locked	linux_ww_mutex_is_locked
82 #define	ww_mutex_lock		linux_ww_mutex_lock
83 #define	ww_mutex_lock_interruptible linux_ww_mutex_lock_interruptible
84 #define	ww_mutex_lock_slow	linux_ww_mutex_lock_slow
85 #define	ww_mutex_lock_slow_interruptible linux_ww_mutex_lock_slow_interruptible
86 #define	ww_mutex_trylock	linux_ww_mutex_trylock
87 #define	ww_mutex_unlock		linux_ww_mutex_unlock
88 
89 void	ww_acquire_init(struct ww_acquire_ctx *, struct ww_class *);
90 void	ww_acquire_done(struct ww_acquire_ctx *);
91 void	ww_acquire_fini(struct ww_acquire_ctx *);
92 
93 void	ww_mutex_init(struct ww_mutex *, struct ww_class *);
94 void	ww_mutex_destroy(struct ww_mutex *);
95 
96 /*
97  * WARNING: ww_mutex_is_locked returns true if it is locked by ANYONE.
98  * Does NOT mean `Do I hold this lock?' (answering which really
99  * requires an acquire context).
100  */
101 bool	ww_mutex_is_locked(struct ww_mutex *);
102 
103 int	ww_mutex_lock(struct ww_mutex *, struct ww_acquire_ctx *);
104 int	ww_mutex_lock_interruptible(struct ww_mutex *,
105 	    struct ww_acquire_ctx *);
106 void	ww_mutex_lock_slow(struct ww_mutex *, struct ww_acquire_ctx *);
107 int	ww_mutex_lock_slow_interruptible(struct ww_mutex *,
108 	    struct ww_acquire_ctx *);
109 int	ww_mutex_trylock(struct ww_mutex *);
110 void	ww_mutex_unlock(struct ww_mutex *);
111 
112 #endif  /* _ASM_WW_MUTEX_H_ */
113