2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "thr_private.h"
36 #define HASHSIZE (1 << HASHSHIFT)
37 #define SC_HASH(wchan) ((unsigned) \
38 ((((uintptr_t)(wchan) >> 3) \
39 ^ ((uintptr_t)(wchan) >> (HASHSHIFT + 3))) \
41 #define SC_LOOKUP(wc) &sc_table[SC_HASH(wc)]
43 struct sleepqueue_chain {
44 struct umutex sc_lock;
46 LIST_HEAD(, sleepqueue) sc_queues;
50 static struct sleepqueue_chain sc_table[HASHSIZE];
57 for (i = 0; i < HASHSIZE; ++i) {
58 LIST_INIT(&sc_table[i].sc_queues);
59 _thr_umutex_init(&sc_table[i].sc_lock);
66 struct sleepqueue *sq;
68 sq = calloc(1, sizeof(struct sleepqueue));
69 TAILQ_INIT(&sq->sq_blocked);
70 SLIST_INIT(&sq->sq_freeq);
75 _sleepq_free(struct sleepqueue *sq)
81 _sleepq_lock(void *wchan)
83 struct pthread *curthread = _get_curthread();
84 struct sleepqueue_chain *sc;
86 sc = SC_LOOKUP(wchan);
87 THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock);
91 _sleepq_unlock(void *wchan)
93 struct sleepqueue_chain *sc;
94 struct pthread *curthread = _get_curthread();
96 sc = SC_LOOKUP(wchan);
97 THR_LOCK_RELEASE(curthread, &sc->sc_lock);
100 static inline struct sleepqueue *
101 lookup(struct sleepqueue_chain *sc, void *wchan)
103 struct sleepqueue *sq;
105 LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
106 if (sq->sq_wchan == wchan)
112 _sleepq_lookup(void *wchan)
114 return (lookup(SC_LOOKUP(wchan), wchan));
118 _sleepq_add(void *wchan, struct pthread *td)
120 struct sleepqueue_chain *sc;
121 struct sleepqueue *sq;
123 sc = SC_LOOKUP(wchan);
124 sq = lookup(sc, wchan);
126 SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink);
129 LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
130 sq->sq_wchan = wchan;
131 /* sq->sq_type = type; */
133 td->sleepqueue = NULL;
135 if (((++sc->sc_enqcnt << _thr_queuefifo) & 0xff) != 0)
136 TAILQ_INSERT_HEAD(&sq->sq_blocked, td, wle);
138 TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle);
142 _sleepq_remove(struct sleepqueue *sq, struct pthread *td)
146 TAILQ_REMOVE(&sq->sq_blocked, td, wle);
147 if (TAILQ_EMPTY(&sq->sq_blocked)) {
148 LIST_REMOVE(sq, sq_hash);
152 td->sleepqueue = SLIST_FIRST(&sq->sq_freeq);
153 SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink);
161 _sleepq_drop(struct sleepqueue *sq,
162 void (*cb)(struct pthread *, void *arg), void *arg)
165 struct sleepqueue *sq2;
167 td = TAILQ_FIRST(&sq->sq_blocked);
170 LIST_REMOVE(sq, sq_hash);
171 TAILQ_REMOVE(&sq->sq_blocked, td, wle);
176 sq2 = SLIST_FIRST(&sq->sq_freeq);
177 TAILQ_FOREACH(td, &sq->sq_blocked, wle) {
180 td->sleepqueue = sq2;
182 sq2 = SLIST_NEXT(sq2, sq_flink);
184 TAILQ_INIT(&sq->sq_blocked);
185 SLIST_INIT(&sq->sq_freeq);