xref: /openbsd/sys/arch/m88k/include/mutex.h (revision a6445c1d)
1 #ifndef _M88K_MUTEX_H_
2 #define _M88K_MUTEX_H_
3 /*	$OpenBSD: mutex.h,v 1.2 2014/02/02 20:31:10 kettenis Exp $	*/
4 
5 /*
6  * Copyright (c) 2005, Miodrag Vallat.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 struct mutex {
31 	volatile int mtx_lock;	/* mutex.S relies upon this field being first */
32 	int mtx_wantipl;
33 	int mtx_oldipl;
34 	void *mtx_cpu;
35 };
36 
37 /*
38  * To prevent lock ordering problems with the kernel lock, we need to
39  * make sure we block all interrupts that can grab the kernel lock.
40  * The simplest way to achieve this is to make sure mutexes always
41  * raise the interrupt priority level to the highest level that has
42  * interrupts that grab the kernel lock.
43  */
44 #ifdef MULTIPROCESSOR
45 #define __MUTEX_IPL(ipl) \
46     (((ipl) > IPL_NONE && (ipl) < IPL_TTY) ? IPL_TTY : (ipl))
47 #else
48 #define __MUTEX_IPL(ipl) (ipl)
49 #endif
50 
51 #define	MUTEX_INITIALIZER(ipl)	{ 0, __MUTEX_IPL((ipl)), IPL_NONE, NULL }
52 
53 void __mtx_init(struct mutex *, int);
54 #define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl)))
55 
56 #ifdef DIAGNOSTIC
57 
58 #define	MUTEX_ASSERT_LOCKED(mtx)					\
59 do {									\
60 	if ((mtx)->mtx_lock == 0 || (mtx)->mtx_cpu != curcpu())		\
61 		panic("mutex %p not held in %s", (mtx), __func__);	\
62 } while (0)
63 
64 #define	MUTEX_ASSERT_UNLOCKED(mtx)					\
65 do {									\
66 	if ((mtx)->mtx_lock != 0)					\
67 		panic("mutex %p held in %s", (mtx), __func__);		\
68 } while (0)
69 
70 #else
71 
72 #define	MUTEX_ASSERT_LOCKED(mtx)	do { /* nothing */ } while (0)
73 #define	MUTEX_ASSERT_UNLOCKED(mtx)	do { /* nothing */ } while (0)
74 
75 #endif
76 
77 #define MUTEX_OLDIPL(mtx)	(mtx)->mtx_oldipl
78 
79 #endif	/* _M88K_MUTEX_H_ */
80