xref: /dragonfly/sys/sys/mutex.h (revision 65cc0652)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef	_SYS_MUTEX_H_
36 #define	_SYS_MUTEX_H_
37 
38 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
39 
40 #ifndef _SYS_TYPES_H_
41 #include <sys/types.h>
42 #endif
43 #include <machine/atomic.h>
44 #include <machine/cpufunc.h>
45 
46 /*
47  * The general mutex structure provides recursive shared and exclusive
48  * locks, downgrade, a non-blocking upgrade, and various other functions.
49  *
50  * The structure is 16-byte aligned and either 16 or 32 bytes, designed
51  * for 32 or 64 bit cpus.
52  */
53 struct thread;
54 
55 struct mtx_link {
56 	struct mtx_link	*next;
57 	struct mtx_link	*prev;
58 	struct thread	*owner;
59 	int		state;
60 	void		(*callback)(struct mtx_link *, void *arg, int error);
61 	void		*arg;
62 };
63 
64 typedef struct mtx_link	mtx_link_t;
65 
66 struct mtx {
67 	volatile u_int	mtx_lock;
68 	uint32_t	mtx_flags;
69 	struct thread	*mtx_owner;
70 	mtx_link_t	*mtx_exlink;
71 	mtx_link_t	*mtx_shlink;
72 	const char	*mtx_ident;
73 } __cachealign;
74 
75 #define MTXF_NOCOLLSTATS	0x00000001	/* v_lock_coll not applicable */
76 
77 typedef struct mtx	mtx_t;
78 typedef u_int		mtx_state_t;
79 
80 #define MTX_INITIALIZER(ident)	{ .mtx_lock = 0, .mtx_flags = 0,	\
81 				  .mtx_owner = NULL,			\
82 			  	  .mtx_exlink = NULL, .mtx_shlink = NULL, \
83 				  .mtx_ident = ident }
84 
85 #define MTX_EXCLUSIVE	0x80000000
86 #define MTX_SHWANTED	0x40000000
87 #define MTX_EXWANTED	0x20000000
88 #define MTX_LINKSPIN	0x10000000
89 #define MTX_MASK	0x0FFFFFFF
90 
91 #define MTX_OWNER_NONE	NULL
92 #define MTX_OWNER_ANON	((struct thread *)-2)
93 
94 #define MTX_LINK_IDLE		0
95 #define MTX_LINK_LINKED_SH	(MTX_LINK_LINKED | 1)
96 #define MTX_LINK_LINKED_EX	(MTX_LINK_LINKED | 2)
97 #define MTX_LINK_ACQUIRED	3
98 #define MTX_LINK_CALLEDBACK	4
99 #define MTX_LINK_ABORTED	5
100 
101 #define MTX_LINK_LINKED		0x1000
102 
103 #endif
104 
105 /*
106  * See also sys/mutex2.h
107  */
108 #ifdef _KERNEL
109 
110 int	_mtx_lock_ex_link(mtx_t *mtx, mtx_link_t *link, int flags, int to);
111 int	_mtx_lock_ex(mtx_t *mtx, int flags, int to);
112 int	_mtx_lock_sh_link(mtx_t *mtx, mtx_link_t *link, int flags, int to);
113 int	_mtx_lock_sh(mtx_t *mtx, int flags, int to);
114 int	_mtx_lock_ex_quick(mtx_t *mtx);
115 int	_mtx_lock_sh_quick(mtx_t *mtx);
116 void	_mtx_spinlock(mtx_t *mtx);
117 int	_mtx_spinlock_try(mtx_t *mtx);
118 int	_mtx_lock_ex_try(mtx_t *mtx);
119 int	_mtx_lock_sh_try(mtx_t *mtx);
120 void	_mtx_downgrade(mtx_t *mtx);
121 int	_mtx_upgrade_try(mtx_t *mtx);
122 void	_mtx_unlock(mtx_t *mtx);
123 void	mtx_abort_link(mtx_t *mtx, mtx_link_t *link);
124 int	mtx_wait_link(mtx_t *mtx, mtx_link_t *link, int flags, int to);
125 
126 #endif
127 
128 #endif
129