xref: /netbsd-src/sys/sys/lock.h (revision 7d42dd44cb944388f40b3ce13eb2b18ee6dd4066)
1 /*	$NetBSD: lock.h,v 1.94 2024/05/12 10:45:13 rillig Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2006, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, by Ross Harvey, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef	_SYS_LOCK_H_
34 #define	_SYS_LOCK_H_
35 
36 #include <sys/stdint.h>
37 #include <sys/mutex.h>
38 
39 #include <machine/lock.h>
40 
41 #ifdef _KERNEL
42 
43 #ifdef _KERNEL_OPT
44 #include "opt_lockdebug.h"
45 #endif
46 
47 /*
48  * From <machine/lock.h>.
49  */
50 #ifndef SPINLOCK_SPIN_HOOK
51 #define	SPINLOCK_SPIN_HOOK
52 #endif
53 #ifndef SPINLOCK_BACKOFF_HOOK
54 #include <sys/systm.h>
55 #define	SPINLOCK_BACKOFF_HOOK		nullop(NULL)
56 #endif
57 #ifndef	SPINLOCK_BACKOFF_MIN
58 #define	SPINLOCK_BACKOFF_MIN	4
59 #endif
60 #ifndef	SPINLOCK_BACKOFF_MAX
61 #define	SPINLOCK_BACKOFF_MAX	128
62 #endif
63 
64 #define	SPINLOCK_BACKOFF(count)					\
65 do {								\
66 	int __i;						\
67 	for (__i = (count); __i != 0; __i--) {			\
68 		SPINLOCK_BACKOFF_HOOK;				\
69 	}							\
70 	if ((count) < SPINLOCK_BACKOFF_MAX)			\
71 		(count) += (count);				\
72 } while (0)
73 
74 #ifdef LOCKDEBUG
75 #define	SPINLOCK_SPINOUT(spins)		((spins)++ > 0x0fffffff)
76 #else
77 #define	SPINLOCK_SPINOUT(spins)		((void)(spins), 0)
78 #endif
79 
80 extern __cpu_simple_lock_t kernel_lock[];
81 
82 #endif /* _KERNEL */
83 
84 #endif /* _SYS_LOCK_H_ */
85