xref: /openbsd/sys/arch/m88k/include/mutex.h (revision 52ac02e4)
1 /*	$OpenBSD: mutex.h,v 1.9 2024/05/16 09:30:03 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, Miodrag Vallat.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _M88K_MUTEX_H_
29 #define _M88K_MUTEX_H_
30 
31 #include <sys/_lock.h>
32 
33 struct mutex {
34 	volatile int mtx_lock;
35 	int mtx_wantipl;
36 	int mtx_oldipl;
37 	volatile void *mtx_owner;
38 #ifdef WITNESS
39 	struct lock_object mtx_lock_obj;
40 #endif
41 };
42 
43 #ifdef WITNESS
44 #define	MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \
45 	{ 0, __MUTEX_IPL((ipl)), IPL_NONE, NULL, \
46 	  MTX_LO_INITIALIZER(name, flags) }
47 #else
48 #define	MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \
49 	{ 0, __MUTEX_IPL((ipl)), IPL_NONE, NULL }
50 #endif
51 
52 void __mtx_init(struct mutex *, int);
53 #define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl)))
54 
55 #ifdef DIAGNOSTIC
56 
57 #define MUTEX_ASSERT_LOCKED(mtx) do {					\
58 	if (((mtx)->mtx_owner != curcpu()) && !(panicstr || db_active))	\
59 		panic("mutex %p not held in %s", (mtx), __func__);	\
60 } while (0)
61 
62 #define MUTEX_ASSERT_UNLOCKED(mtx) do {					\
63 	if (((mtx)->mtx_owner == curcpu()) && !(panicstr || db_active))	\
64 		panic("mutex %p held in %s", (mtx), __func__);		\
65 } while (0)
66 
67 #else
68 
69 #define	MUTEX_ASSERT_LOCKED(mtx)	do { /* nothing */ } while (0)
70 #define	MUTEX_ASSERT_UNLOCKED(mtx)	do { /* nothing */ } while (0)
71 
72 #endif
73 
74 #define MUTEX_LOCK_OBJECT(mtx)	(&(mtx)->mtx_lock_obj)
75 #define MUTEX_OLDIPL(mtx)	(mtx)->mtx_oldipl
76 
77 #endif	/* _M88K_MUTEX_H_ */
78