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