xref: /dragonfly/sys/dev/drm/include/linux/ww_mutex.h (revision 6e316fcd)
1 /*
2  * Copyright (c) 2015 Michael Neumann <mneumann@ntecs.de>
3  * All rights reserved.
4  * Copyright (c) 2003-2011 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Michael Neumann <mneumann@ntecs.de> and
8  *    Matthew Dillon <dillon@backplane.com>
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 unmodified, this list of conditions, and the following
15  *    disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_WW_MUTEX_H_
33 #define _LINUX_WW_MUTEX_H_
34 
35 #include <linux/mutex.h>
36 
37 /*
38  * A basic, unoptimized implementation of wound/wait mutexes for DragonFly
39  * modelled after the Linux API [1].
40  *
41  * [1]: http://lxr.free-electrons.com/source/include/linux/ww_mutex.h
42  */
43 
44 #include <sys/errno.h>
45 #include <sys/types.h>
46 #include <machine/atomic.h>
47 #include <sys/spinlock.h>
48 #include <sys/spinlock2.h>
49 
50 struct ww_class {
51 	volatile u_long		stamp;
52 	const char		*name;
53 };
54 
55 struct ww_acquire_ctx {
56 	u_long			stamp;
57 	int			acquired;
58 	int			unused01;
59 	struct ww_class		*ww_class;
60 };
61 
62 struct ww_mutex {
63 	struct lock		base;
64 	struct ww_acquire_ctx	*ctx;
65 	u_long			stamp;	/* heuristic */
66 	int			blocked;
67 	int			unused01;
68 };
69 
70 #define DEFINE_WW_CLASS(classname)	\
71 	struct ww_class classname = {	\
72 		.stamp = 0,		\
73 		.name = #classname	\
74 	}
75 
76 extern void ww_acquire_init(struct ww_acquire_ctx *ctx,
77 			struct ww_class *ww_class);
78 extern void ww_acquire_done(struct ww_acquire_ctx *ctx);
79 extern void ww_acquire_fini(struct ww_acquire_ctx *ctx);
80 extern void ww_mutex_init(struct ww_mutex *ww, struct ww_class *ww_class);
81 extern int ww_mutex_lock(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
82 extern int ww_mutex_lock_slow(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
83 extern int ww_mutex_lock_interruptible(struct ww_mutex *ww,
84 			struct ww_acquire_ctx *ctx);
85 extern int ww_mutex_lock_slow_interruptible(struct ww_mutex *ww,
86 			struct ww_acquire_ctx *ctx);
87 extern void ww_mutex_unlock(struct ww_mutex *ww);
88 extern void ww_mutex_destroy(struct ww_mutex *ww);
89 
90 /*
91  * Returns 1 if locked, 0 otherwise
92  */
93 static inline bool
94 ww_mutex_is_locked(struct ww_mutex *ww)
95 {
96 	return (lockstatus(&ww->base, NULL) != 0);
97 }
98 
99 /*
100  * Returns 1 on success, 0 if contended.
101  *
102  * This call has no context accounting.
103  */
104 static inline int
105 ww_mutex_trylock(struct ww_mutex *ww)
106 {
107 	return (lockmgr(&ww->base, LK_EXCLUSIVE|LK_NOWAIT) == 0);
108 }
109 
110 #endif	/* _LINUX_WW_MUTEX_H_ */
111