xref: /dragonfly/sys/dev/drm/include/linux/ww_mutex.h (revision ec5b6af45b44f70d488e8226966f076d4270aec1)
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 
48 struct ww_class {
49           volatile u_long               stamp;
50           const char                    *name;
51 };
52 
53 struct ww_acquire_ctx {
54           u_long                        stamp;
55           int                           acquired;
56           int                           unused01;
57           struct ww_class               *ww_class;
58 };
59 
60 struct ww_mutex {
61           struct lock                   base;
62           struct ww_acquire_ctx         *ctx;
63           u_long                        stamp;    /* heuristic */
64           int                           blocked;
65           int                           unused01;
66 };
67 
68 #define DEFINE_WW_CLASS(classname)      \
69           struct ww_class classname = { \
70                     .stamp = 0,                   \
71                     .name = #classname  \
72           }
73 
74 extern void ww_acquire_init(struct ww_acquire_ctx *ctx,
75                               struct ww_class *ww_class);
76 extern void ww_acquire_done(struct ww_acquire_ctx *ctx);
77 extern void ww_acquire_fini(struct ww_acquire_ctx *ctx);
78 extern void ww_mutex_init(struct ww_mutex *ww, struct ww_class *ww_class);
79 extern int ww_mutex_lock(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
80 extern int ww_mutex_lock_slow(struct ww_mutex *ww, struct ww_acquire_ctx *ctx);
81 extern int ww_mutex_lock_interruptible(struct ww_mutex *ww,
82                               struct ww_acquire_ctx *ctx);
83 extern int ww_mutex_lock_slow_interruptible(struct ww_mutex *ww,
84                               struct ww_acquire_ctx *ctx);
85 extern void ww_mutex_unlock(struct ww_mutex *ww);
86 extern void ww_mutex_destroy(struct ww_mutex *ww);
87 
88 /*
89  * Returns 1 if locked, 0 otherwise
90  */
91 static inline bool
ww_mutex_is_locked(struct ww_mutex * ww)92 ww_mutex_is_locked(struct ww_mutex *ww)
93 {
94           return (lockstatus(&ww->base, NULL) != 0);
95 }
96 
97 /*
98  * Returns 1 on success, 0 if contended.
99  *
100  * This call has no context accounting.
101  */
102 static inline int
ww_mutex_trylock(struct ww_mutex * ww)103 ww_mutex_trylock(struct ww_mutex *ww)
104 {
105           return (lockmgr(&ww->base, LK_EXCLUSIVE|LK_NOWAIT) == 0);
106 }
107 
108 #endif    /* _LINUX_WW_MUTEX_H_ */
109