xref: /netbsd-src/sys/sys/sleeptab.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: sleeptab.h,v 1.2 2020/11/01 21:00:20 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2019, 2020
5  *     The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe and 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_SLEEPTAB_H_
34 #define	_SYS_SLEEPTAB_H_
35 
36 #define	SLEEPTAB_HASH_SHIFT	7
37 #define	SLEEPTAB_HASH_SIZE	(1 << SLEEPTAB_HASH_SHIFT)
38 #define	SLEEPTAB_HASH_MASK	(SLEEPTAB_HASH_SIZE - 1)
39 #define	SLEEPTAB_HASH(wchan)	(((uintptr_t)(wchan) >> 8) & SLEEPTAB_HASH_MASK)
40 
41 LIST_HEAD(sleepq, lwp);
42 
43 typedef struct sleeptab {
44 	sleepq_t	st_queue[SLEEPTAB_HASH_SIZE];
45 } sleeptab_t;
46 
47 void	sleeptab_init(sleeptab_t *);
48 
49 extern sleeptab_t	sleeptab;
50 
51 #ifdef _KERNEL
52 /*
53  * Find the correct sleep queue for the specified wait channel.  This
54  * acquires and holds the per-queue interlock.
55  */
56 static __inline sleepq_t *
57 sleeptab_lookup(sleeptab_t *st, wchan_t wchan, kmutex_t **mp)
58 {
59 	extern sleepqlock_t sleepq_locks[SLEEPTAB_HASH_SIZE];
60 	sleepq_t *sq;
61 	u_int hash;
62 
63 	hash = SLEEPTAB_HASH(wchan);
64 	sq = &st->st_queue[hash];
65 	*mp = &sleepq_locks[hash].lock;
66 	mutex_spin_enter(*mp);
67 	return sq;
68 }
69 
70 static __inline kmutex_t *
71 sleepq_hashlock(wchan_t wchan)
72 {
73 	extern sleepqlock_t sleepq_locks[SLEEPTAB_HASH_SIZE];
74 	kmutex_t *mp;
75 
76 	mp = &sleepq_locks[SLEEPTAB_HASH(wchan)].lock;
77 	mutex_spin_enter(mp);
78 	return mp;
79 }
80 
81 #define sleepq_destroy(a) __nothing
82 
83 #endif
84 
85 /*
86  * Turnstiles, specialized sleep queues for use by kernel locks.
87  */
88 
89 typedef struct turnstile {
90 	LIST_ENTRY(turnstile)	ts_chain;	/* link on hash chain */
91 	struct turnstile	*ts_free;	/* turnstile free list */
92 	wchan_t			ts_obj;		/* lock object */
93 	sleepq_t		ts_sleepq[2];	/* sleep queues */
94 	u_int			ts_waiters[2];	/* count of waiters */
95 
96 	/* priority inheritance */
97 	pri_t			ts_eprio;
98 	lwp_t			*ts_inheritor;
99 	SLIST_ENTRY(turnstile)	ts_pichain;
100 } turnstile_t;
101 
102 LIST_HEAD(tschain, turnstile);
103 
104 typedef struct tschain tschain_t;
105 
106 #define	TS_READER_Q	0		/* reader sleep queue */
107 #define	TS_WRITER_Q	1		/* writer sleep queue */
108 
109 #define	TS_WAITERS(ts, q)						\
110 	(ts)->ts_waiters[(q)]
111 
112 #define	TS_ALL_WAITERS(ts)						\
113 	((ts)->ts_waiters[TS_READER_Q] +				\
114 	 (ts)->ts_waiters[TS_WRITER_Q])
115 
116 #define	TS_FIRST(ts, q)	(LIST_FIRST(&(ts)->ts_sleepq[(q)]))
117 
118 #ifdef	_KERNEL
119 
120 void	turnstile_init(void);
121 turnstile_t	*turnstile_lookup(wchan_t);
122 void	turnstile_ctor(turnstile_t *);
123 void	turnstile_exit(wchan_t);
124 void	turnstile_block(turnstile_t *, int, wchan_t, syncobj_t *);
125 void	turnstile_wakeup(turnstile_t *, int, int, lwp_t *);
126 void	turnstile_print(volatile void *, void (*)(const char *, ...)
127     __printflike(1, 2));
128 void	turnstile_unsleep(lwp_t *, bool);
129 void	turnstile_changepri(lwp_t *, pri_t);
130 
131 extern struct pool turnstile_pool;
132 extern turnstile_t turnstile0;
133 
134 #endif	/* _KERNEL */
135 
136 #endif	/* _SYS_SLEEPTAB_H_ */
137