xref: /freebsd/lib/libthr/thread/thr_sleepq.c (revision 9a2ae72421cd75c741984f63b8c9ee89346a188d)
15e53a4f9SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
40126aea6SDavid Xu  * Copyright (c) 2010 David Xu <davidxu@freebsd.org>
50126aea6SDavid Xu  * All rights reserved.
60126aea6SDavid Xu  *
70126aea6SDavid Xu  * Redistribution and use in source and binary forms, with or without
80126aea6SDavid Xu  * modification, are permitted provided that the following conditions
90126aea6SDavid Xu  * are met:
100126aea6SDavid Xu  * 1. Redistributions of source code must retain the above copyright
110126aea6SDavid Xu  *    notice unmodified, this list of conditions, and the following
120126aea6SDavid Xu  *    disclaimer.
130126aea6SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
140126aea6SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
150126aea6SDavid Xu  *    documentation and/or other materials provided with the distribution.
160126aea6SDavid Xu  *
170126aea6SDavid Xu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
180126aea6SDavid Xu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
190126aea6SDavid Xu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200126aea6SDavid Xu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210126aea6SDavid Xu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
220126aea6SDavid Xu  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230126aea6SDavid Xu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
240126aea6SDavid Xu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250126aea6SDavid Xu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
260126aea6SDavid Xu  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270126aea6SDavid Xu  */
280126aea6SDavid Xu 
290126aea6SDavid Xu #include <stdlib.h>
300126aea6SDavid Xu #include "thr_private.h"
310126aea6SDavid Xu 
320126aea6SDavid Xu #define HASHSHIFT	9
330126aea6SDavid Xu #define HASHSIZE	(1 << HASHSHIFT)
340126aea6SDavid Xu #define SC_HASH(wchan) ((unsigned)				\
350126aea6SDavid Xu 	((((uintptr_t)(wchan) >> 3)				\
360126aea6SDavid Xu 	^ ((uintptr_t)(wchan) >> (HASHSHIFT + 3)))		\
370126aea6SDavid Xu 	& (HASHSIZE - 1)))
380126aea6SDavid Xu #define SC_LOOKUP(wc)	&sc_table[SC_HASH(wc)]
390126aea6SDavid Xu 
400126aea6SDavid Xu struct sleepqueue_chain {
410126aea6SDavid Xu 	struct umutex		sc_lock;
4284ac0fb8SDavid Xu 	int			sc_enqcnt;
430126aea6SDavid Xu 	LIST_HEAD(, sleepqueue) sc_queues;
440126aea6SDavid Xu 	int			sc_type;
450126aea6SDavid Xu };
460126aea6SDavid Xu 
470126aea6SDavid Xu static struct sleepqueue_chain  sc_table[HASHSIZE];
480126aea6SDavid Xu 
490126aea6SDavid Xu void
_sleepq_init(void)500126aea6SDavid Xu _sleepq_init(void)
510126aea6SDavid Xu {
520126aea6SDavid Xu 	int	i;
530126aea6SDavid Xu 
540126aea6SDavid Xu 	for (i = 0; i < HASHSIZE; ++i) {
550126aea6SDavid Xu 		LIST_INIT(&sc_table[i].sc_queues);
560126aea6SDavid Xu 		_thr_umutex_init(&sc_table[i].sc_lock);
570126aea6SDavid Xu 	}
580126aea6SDavid Xu }
590126aea6SDavid Xu 
600126aea6SDavid Xu struct sleepqueue *
_sleepq_alloc(void)610126aea6SDavid Xu _sleepq_alloc(void)
620126aea6SDavid Xu {
630126aea6SDavid Xu 	struct sleepqueue *sq;
640126aea6SDavid Xu 
65*9a2ae724SKonstantin Belousov 	sq = __thr_calloc(1, sizeof(struct sleepqueue));
660126aea6SDavid Xu 	TAILQ_INIT(&sq->sq_blocked);
670126aea6SDavid Xu 	SLIST_INIT(&sq->sq_freeq);
680126aea6SDavid Xu 	return (sq);
690126aea6SDavid Xu }
700126aea6SDavid Xu 
710126aea6SDavid Xu void
_sleepq_free(struct sleepqueue * sq)720126aea6SDavid Xu _sleepq_free(struct sleepqueue *sq)
730126aea6SDavid Xu {
74*9a2ae724SKonstantin Belousov 	__thr_free(sq);
750126aea6SDavid Xu }
760126aea6SDavid Xu 
770126aea6SDavid Xu void
_sleepq_lock(void * wchan)780126aea6SDavid Xu _sleepq_lock(void *wchan)
790126aea6SDavid Xu {
800126aea6SDavid Xu 	struct pthread *curthread = _get_curthread();
810126aea6SDavid Xu 	struct sleepqueue_chain *sc;
820126aea6SDavid Xu 
830126aea6SDavid Xu 	sc = SC_LOOKUP(wchan);
840126aea6SDavid Xu 	THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock);
850126aea6SDavid Xu }
860126aea6SDavid Xu 
870126aea6SDavid Xu void
_sleepq_unlock(void * wchan)880126aea6SDavid Xu _sleepq_unlock(void *wchan)
890126aea6SDavid Xu {
900126aea6SDavid Xu 	struct sleepqueue_chain *sc;
910126aea6SDavid Xu 	struct pthread *curthread = _get_curthread();
920126aea6SDavid Xu 
930126aea6SDavid Xu 	sc = SC_LOOKUP(wchan);
940126aea6SDavid Xu 	THR_LOCK_RELEASE(curthread, &sc->sc_lock);
950126aea6SDavid Xu }
960126aea6SDavid Xu 
97fa782a26SDavid Xu static inline struct sleepqueue *
lookup(struct sleepqueue_chain * sc,void * wchan)98fa782a26SDavid Xu lookup(struct sleepqueue_chain *sc, void *wchan)
990126aea6SDavid Xu {
1000126aea6SDavid Xu 	struct sleepqueue *sq;
1010126aea6SDavid Xu 
1020126aea6SDavid Xu 	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
1030126aea6SDavid Xu 		if (sq->sq_wchan == wchan)
1040126aea6SDavid Xu 			return (sq);
1050126aea6SDavid Xu 	return (NULL);
1060126aea6SDavid Xu }
1070126aea6SDavid Xu 
108fa782a26SDavid Xu struct sleepqueue *
_sleepq_lookup(void * wchan)109fa782a26SDavid Xu _sleepq_lookup(void *wchan)
110fa782a26SDavid Xu {
111fa782a26SDavid Xu 	return (lookup(SC_LOOKUP(wchan), wchan));
112fa782a26SDavid Xu }
113fa782a26SDavid Xu 
1140126aea6SDavid Xu void
_sleepq_add(void * wchan,struct pthread * td)1150126aea6SDavid Xu _sleepq_add(void *wchan, struct pthread *td)
1160126aea6SDavid Xu {
1170126aea6SDavid Xu 	struct sleepqueue_chain *sc;
1180126aea6SDavid Xu 	struct sleepqueue *sq;
1190126aea6SDavid Xu 
120173943acSDavid Xu 	sc = SC_LOOKUP(wchan);
121fa782a26SDavid Xu 	sq = lookup(sc, wchan);
1220126aea6SDavid Xu 	if (sq != NULL) {
1230126aea6SDavid Xu 		SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink);
1240126aea6SDavid Xu 	} else {
1250126aea6SDavid Xu 		sq = td->sleepqueue;
1260126aea6SDavid Xu 		LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
1270126aea6SDavid Xu 		sq->sq_wchan = wchan;
1280126aea6SDavid Xu 		/* sq->sq_type = type; */
1290126aea6SDavid Xu 	}
1300126aea6SDavid Xu 	td->sleepqueue = NULL;
1310126aea6SDavid Xu 	td->wchan = wchan;
13284ac0fb8SDavid Xu 	if (((++sc->sc_enqcnt << _thr_queuefifo) & 0xff) != 0)
13384ac0fb8SDavid Xu 		TAILQ_INSERT_HEAD(&sq->sq_blocked, td, wle);
13484ac0fb8SDavid Xu 	else
1350126aea6SDavid Xu 		TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle);
1360126aea6SDavid Xu }
1370126aea6SDavid Xu 
1380126aea6SDavid Xu int
_sleepq_remove(struct sleepqueue * sq,struct pthread * td)1390126aea6SDavid Xu _sleepq_remove(struct sleepqueue *sq, struct pthread *td)
1400126aea6SDavid Xu {
1410126aea6SDavid Xu 	int rc;
1420126aea6SDavid Xu 
1430126aea6SDavid Xu 	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
1440126aea6SDavid Xu 	if (TAILQ_EMPTY(&sq->sq_blocked)) {
1450126aea6SDavid Xu 		LIST_REMOVE(sq, sq_hash);
1460126aea6SDavid Xu 		td->sleepqueue = sq;
1470126aea6SDavid Xu 		rc = 0;
1480126aea6SDavid Xu 	} else {
1490126aea6SDavid Xu 		td->sleepqueue = SLIST_FIRST(&sq->sq_freeq);
1500126aea6SDavid Xu 		SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink);
1510126aea6SDavid Xu 		rc = 1;
1520126aea6SDavid Xu 	}
1530126aea6SDavid Xu 	td->wchan = NULL;
1540126aea6SDavid Xu 	return (rc);
1550126aea6SDavid Xu }
1560126aea6SDavid Xu 
1570126aea6SDavid Xu void
_sleepq_drop(struct sleepqueue * sq,void (* cb)(struct pthread *,void * arg),void * arg)1580126aea6SDavid Xu _sleepq_drop(struct sleepqueue *sq,
1590126aea6SDavid Xu 	void (*cb)(struct pthread *, void *arg), void *arg)
1600126aea6SDavid Xu {
1610126aea6SDavid Xu 	struct pthread *td;
1620126aea6SDavid Xu 	struct sleepqueue *sq2;
1630126aea6SDavid Xu 
1640126aea6SDavid Xu 	td = TAILQ_FIRST(&sq->sq_blocked);
1650126aea6SDavid Xu 	if (td == NULL)
1660126aea6SDavid Xu 		return;
1670126aea6SDavid Xu 	LIST_REMOVE(sq, sq_hash);
1680126aea6SDavid Xu 	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
1690126aea6SDavid Xu 	if (cb != NULL)
1700126aea6SDavid Xu 		cb(td, arg);
1710126aea6SDavid Xu 	td->sleepqueue = sq;
1720126aea6SDavid Xu 	td->wchan = NULL;
1730126aea6SDavid Xu 	sq2 = SLIST_FIRST(&sq->sq_freeq);
1740126aea6SDavid Xu 	TAILQ_FOREACH(td, &sq->sq_blocked, wle) {
1750126aea6SDavid Xu 		if (cb != NULL)
1760126aea6SDavid Xu 			cb(td, arg);
1770126aea6SDavid Xu 		td->sleepqueue = sq2;
1780126aea6SDavid Xu 		td->wchan = NULL;
1790126aea6SDavid Xu 		sq2 = SLIST_NEXT(sq2, sq_flink);
1800126aea6SDavid Xu 	}
1810126aea6SDavid Xu 	TAILQ_INIT(&sq->sq_blocked);
1820126aea6SDavid Xu 	SLIST_INIT(&sq->sq_freeq);
1830126aea6SDavid Xu }
184