1*fac91bbeSriastradh /* $NetBSD: sleeptab.h,v 1.3 2023/10/15 10:27:11 riastradh Exp $ */
29f4e6ae3Schristos
39f4e6ae3Schristos /*-
49f4e6ae3Schristos * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2019, 2020
59f4e6ae3Schristos * The NetBSD Foundation, Inc.
69f4e6ae3Schristos * All rights reserved.
79f4e6ae3Schristos *
89f4e6ae3Schristos * This code is derived from software contributed to The NetBSD Foundation
99f4e6ae3Schristos * by Jason R. Thorpe and Andrew Doran.
109f4e6ae3Schristos *
119f4e6ae3Schristos * Redistribution and use in source and binary forms, with or without
129f4e6ae3Schristos * modification, are permitted provided that the following conditions
139f4e6ae3Schristos * are met:
149f4e6ae3Schristos * 1. Redistributions of source code must retain the above copyright
159f4e6ae3Schristos * notice, this list of conditions and the following disclaimer.
169f4e6ae3Schristos * 2. Redistributions in binary form must reproduce the above copyright
179f4e6ae3Schristos * notice, this list of conditions and the following disclaimer in the
189f4e6ae3Schristos * documentation and/or other materials provided with the distribution.
199f4e6ae3Schristos *
209f4e6ae3Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
219f4e6ae3Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
229f4e6ae3Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
239f4e6ae3Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
249f4e6ae3Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259f4e6ae3Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269f4e6ae3Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279f4e6ae3Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289f4e6ae3Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299f4e6ae3Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309f4e6ae3Schristos * POSSIBILITY OF SUCH DAMAGE.
319f4e6ae3Schristos */
329f4e6ae3Schristos
339f4e6ae3Schristos #ifndef _SYS_SLEEPTAB_H_
349f4e6ae3Schristos #define _SYS_SLEEPTAB_H_
359f4e6ae3Schristos
36*fac91bbeSriastradh #include <sys/wchan.h>
37*fac91bbeSriastradh
38*fac91bbeSriastradh struct syncobj;
39*fac91bbeSriastradh
409f4e6ae3Schristos #define SLEEPTAB_HASH_SHIFT 7
419f4e6ae3Schristos #define SLEEPTAB_HASH_SIZE (1 << SLEEPTAB_HASH_SHIFT)
429f4e6ae3Schristos #define SLEEPTAB_HASH_MASK (SLEEPTAB_HASH_SIZE - 1)
439f4e6ae3Schristos #define SLEEPTAB_HASH(wchan) (((uintptr_t)(wchan) >> 8) & SLEEPTAB_HASH_MASK)
449f4e6ae3Schristos
459f4e6ae3Schristos LIST_HEAD(sleepq, lwp);
469f4e6ae3Schristos
479f4e6ae3Schristos typedef struct sleeptab {
489f4e6ae3Schristos sleepq_t st_queue[SLEEPTAB_HASH_SIZE];
499f4e6ae3Schristos } sleeptab_t;
509f4e6ae3Schristos
519f4e6ae3Schristos void sleeptab_init(sleeptab_t *);
529f4e6ae3Schristos
539f4e6ae3Schristos extern sleeptab_t sleeptab;
549f4e6ae3Schristos
559f4e6ae3Schristos #ifdef _KERNEL
569f4e6ae3Schristos /*
579f4e6ae3Schristos * Find the correct sleep queue for the specified wait channel. This
589f4e6ae3Schristos * acquires and holds the per-queue interlock.
599f4e6ae3Schristos */
609f4e6ae3Schristos static __inline sleepq_t *
sleeptab_lookup(sleeptab_t * st,wchan_t wchan,kmutex_t ** mp)619f4e6ae3Schristos sleeptab_lookup(sleeptab_t *st, wchan_t wchan, kmutex_t **mp)
629f4e6ae3Schristos {
639f4e6ae3Schristos extern sleepqlock_t sleepq_locks[SLEEPTAB_HASH_SIZE];
649f4e6ae3Schristos sleepq_t *sq;
659f4e6ae3Schristos u_int hash;
669f4e6ae3Schristos
679f4e6ae3Schristos hash = SLEEPTAB_HASH(wchan);
689f4e6ae3Schristos sq = &st->st_queue[hash];
699f4e6ae3Schristos *mp = &sleepq_locks[hash].lock;
709f4e6ae3Schristos mutex_spin_enter(*mp);
719f4e6ae3Schristos return sq;
729f4e6ae3Schristos }
739f4e6ae3Schristos
749f4e6ae3Schristos static __inline kmutex_t *
sleepq_hashlock(wchan_t wchan)759f4e6ae3Schristos sleepq_hashlock(wchan_t wchan)
769f4e6ae3Schristos {
779f4e6ae3Schristos extern sleepqlock_t sleepq_locks[SLEEPTAB_HASH_SIZE];
789f4e6ae3Schristos kmutex_t *mp;
799f4e6ae3Schristos
809f4e6ae3Schristos mp = &sleepq_locks[SLEEPTAB_HASH(wchan)].lock;
819f4e6ae3Schristos mutex_spin_enter(mp);
829f4e6ae3Schristos return mp;
839f4e6ae3Schristos }
849f4e6ae3Schristos
853f9be1d9Schristos #define sleepq_destroy(a) __nothing
869f4e6ae3Schristos
879f4e6ae3Schristos #endif
889f4e6ae3Schristos
899f4e6ae3Schristos /*
909f4e6ae3Schristos * Turnstiles, specialized sleep queues for use by kernel locks.
919f4e6ae3Schristos */
929f4e6ae3Schristos
939f4e6ae3Schristos typedef struct turnstile {
949f4e6ae3Schristos LIST_ENTRY(turnstile) ts_chain; /* link on hash chain */
959f4e6ae3Schristos struct turnstile *ts_free; /* turnstile free list */
969f4e6ae3Schristos wchan_t ts_obj; /* lock object */
979f4e6ae3Schristos sleepq_t ts_sleepq[2]; /* sleep queues */
989f4e6ae3Schristos u_int ts_waiters[2]; /* count of waiters */
999f4e6ae3Schristos
1009f4e6ae3Schristos /* priority inheritance */
1019f4e6ae3Schristos pri_t ts_eprio;
1029f4e6ae3Schristos lwp_t *ts_inheritor;
1039f4e6ae3Schristos SLIST_ENTRY(turnstile) ts_pichain;
1049f4e6ae3Schristos } turnstile_t;
1059f4e6ae3Schristos
1069f4e6ae3Schristos LIST_HEAD(tschain, turnstile);
1079f4e6ae3Schristos
1089f4e6ae3Schristos typedef struct tschain tschain_t;
1099f4e6ae3Schristos
1109f4e6ae3Schristos #define TS_READER_Q 0 /* reader sleep queue */
1119f4e6ae3Schristos #define TS_WRITER_Q 1 /* writer sleep queue */
1129f4e6ae3Schristos
1139f4e6ae3Schristos #define TS_WAITERS(ts, q) \
1149f4e6ae3Schristos (ts)->ts_waiters[(q)]
1159f4e6ae3Schristos
1169f4e6ae3Schristos #define TS_ALL_WAITERS(ts) \
1179f4e6ae3Schristos ((ts)->ts_waiters[TS_READER_Q] + \
1189f4e6ae3Schristos (ts)->ts_waiters[TS_WRITER_Q])
1199f4e6ae3Schristos
1209f4e6ae3Schristos #define TS_FIRST(ts, q) (LIST_FIRST(&(ts)->ts_sleepq[(q)]))
1219f4e6ae3Schristos
1229f4e6ae3Schristos #ifdef _KERNEL
1239f4e6ae3Schristos
1249f4e6ae3Schristos void turnstile_init(void);
1259f4e6ae3Schristos turnstile_t *turnstile_lookup(wchan_t);
1269f4e6ae3Schristos void turnstile_ctor(turnstile_t *);
1279f4e6ae3Schristos void turnstile_exit(wchan_t);
128*fac91bbeSriastradh void turnstile_block(turnstile_t *, int, wchan_t, const struct syncobj *);
1299f4e6ae3Schristos void turnstile_wakeup(turnstile_t *, int, int, lwp_t *);
1309f4e6ae3Schristos void turnstile_print(volatile void *, void (*)(const char *, ...)
1319f4e6ae3Schristos __printflike(1, 2));
1329f4e6ae3Schristos void turnstile_unsleep(lwp_t *, bool);
1339f4e6ae3Schristos void turnstile_changepri(lwp_t *, pri_t);
1349f4e6ae3Schristos
1359f4e6ae3Schristos extern struct pool turnstile_pool;
1369f4e6ae3Schristos extern turnstile_t turnstile0;
1379f4e6ae3Schristos
1389f4e6ae3Schristos #endif /* _KERNEL */
1399f4e6ae3Schristos
1409f4e6ae3Schristos #endif /* _SYS_SLEEPTAB_H_ */
141