thr_sleepq.c revision 234947
1216642Sdavidxu/*
2216642Sdavidxu * Copyright (c) 2010 David Xu <davidxu@freebsd.org>
3216642Sdavidxu * All rights reserved.
4216642Sdavidxu *
5216642Sdavidxu * Redistribution and use in source and binary forms, with or without
6216642Sdavidxu * modification, are permitted provided that the following conditions
7216642Sdavidxu * are met:
8216642Sdavidxu * 1. Redistributions of source code must retain the above copyright
9216642Sdavidxu *    notice unmodified, this list of conditions, and the following
10216642Sdavidxu *    disclaimer.
11216642Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
12216642Sdavidxu *    notice, this list of conditions and the following disclaimer in the
13216642Sdavidxu *    documentation and/or other materials provided with the distribution.
14216642Sdavidxu *
15216642Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16216642Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17216642Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18216642Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19216642Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20216642Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21216642Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22216642Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23216642Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24216642Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25216642Sdavidxu *
26216642Sdavidxu * $FreeBSD: head/lib/libthr/thread/thr_sleepq.c 234947 2012-05-03 09:17:31Z davidxu $
27216642Sdavidxu */
28216642Sdavidxu
29216642Sdavidxu#include <stdlib.h>
30216642Sdavidxu#include "thr_private.h"
31216642Sdavidxu
32216642Sdavidxu#define HASHSHIFT	9
33216642Sdavidxu#define HASHSIZE	(1 << HASHSHIFT)
34216642Sdavidxu#define SC_HASH(wchan) ((unsigned)				\
35216642Sdavidxu	((((uintptr_t)(wchan) >> 3)				\
36216642Sdavidxu	^ ((uintptr_t)(wchan) >> (HASHSHIFT + 3)))		\
37216642Sdavidxu	& (HASHSIZE - 1)))
38216642Sdavidxu#define SC_LOOKUP(wc)	&sc_table[SC_HASH(wc)]
39216642Sdavidxu
40216642Sdavidxustruct sleepqueue_chain {
41216642Sdavidxu	struct umutex		sc_lock;
42234947Sdavidxu	int			sc_enqcnt;
43216642Sdavidxu	LIST_HEAD(, sleepqueue) sc_queues;
44216642Sdavidxu	int			sc_type;
45216642Sdavidxu};
46216642Sdavidxu
47216642Sdavidxustatic struct sleepqueue_chain  sc_table[HASHSIZE];
48216642Sdavidxu
49216642Sdavidxuvoid
50216642Sdavidxu_sleepq_init(void)
51216642Sdavidxu{
52216642Sdavidxu	int	i;
53216642Sdavidxu
54216642Sdavidxu	for (i = 0; i < HASHSIZE; ++i) {
55216642Sdavidxu		LIST_INIT(&sc_table[i].sc_queues);
56216642Sdavidxu		_thr_umutex_init(&sc_table[i].sc_lock);
57216642Sdavidxu	}
58216642Sdavidxu}
59216642Sdavidxu
60216642Sdavidxustruct sleepqueue *
61216642Sdavidxu_sleepq_alloc(void)
62216642Sdavidxu{
63216642Sdavidxu	struct sleepqueue *sq;
64216642Sdavidxu
65216642Sdavidxu	sq = calloc(1, sizeof(struct sleepqueue));
66216642Sdavidxu	TAILQ_INIT(&sq->sq_blocked);
67216642Sdavidxu	SLIST_INIT(&sq->sq_freeq);
68216642Sdavidxu	return (sq);
69216642Sdavidxu}
70216642Sdavidxu
71216642Sdavidxuvoid
72216642Sdavidxu_sleepq_free(struct sleepqueue *sq)
73216642Sdavidxu{
74216642Sdavidxu	free(sq);
75216642Sdavidxu}
76216642Sdavidxu
77216642Sdavidxuvoid
78216642Sdavidxu_sleepq_lock(void *wchan)
79216642Sdavidxu{
80216642Sdavidxu	struct pthread *curthread = _get_curthread();
81216642Sdavidxu	struct sleepqueue_chain *sc;
82216642Sdavidxu
83216642Sdavidxu	sc = SC_LOOKUP(wchan);
84216642Sdavidxu	THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock);
85216642Sdavidxu}
86216642Sdavidxu
87216642Sdavidxuvoid
88216642Sdavidxu_sleepq_unlock(void *wchan)
89216642Sdavidxu{
90216642Sdavidxu	struct sleepqueue_chain *sc;
91216642Sdavidxu	struct pthread *curthread = _get_curthread();
92216642Sdavidxu
93216642Sdavidxu	sc = SC_LOOKUP(wchan);
94216642Sdavidxu	THR_LOCK_RELEASE(curthread, &sc->sc_lock);
95216642Sdavidxu}
96216642Sdavidxu
97216642Sdavidxustruct sleepqueue *
98216642Sdavidxu_sleepq_lookup(void *wchan)
99216642Sdavidxu{
100216642Sdavidxu	struct sleepqueue_chain *sc;
101216642Sdavidxu	struct sleepqueue *sq;
102216642Sdavidxu
103216642Sdavidxu	sc = SC_LOOKUP(wchan);
104216642Sdavidxu	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
105216642Sdavidxu		if (sq->sq_wchan == wchan)
106216642Sdavidxu			return (sq);
107216642Sdavidxu	return (NULL);
108216642Sdavidxu}
109216642Sdavidxu
110216642Sdavidxuvoid
111216642Sdavidxu_sleepq_add(void *wchan, struct pthread *td)
112216642Sdavidxu{
113216642Sdavidxu	struct sleepqueue_chain *sc;
114216642Sdavidxu	struct sleepqueue *sq;
115216642Sdavidxu
116216642Sdavidxu	sq = _sleepq_lookup(wchan);
117216642Sdavidxu	if (sq != NULL) {
118216642Sdavidxu		SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink);
119216642Sdavidxu	} else {
120216642Sdavidxu		sc = SC_LOOKUP(wchan);
121216642Sdavidxu		sq = td->sleepqueue;
122216642Sdavidxu		LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
123216642Sdavidxu		sq->sq_wchan = wchan;
124216642Sdavidxu		/* sq->sq_type = type; */
125216642Sdavidxu	}
126216642Sdavidxu	td->sleepqueue = NULL;
127216642Sdavidxu	td->wchan = wchan;
128234947Sdavidxu	if (((++sc->sc_enqcnt << _thr_queuefifo) & 0xff) != 0)
129234947Sdavidxu		TAILQ_INSERT_HEAD(&sq->sq_blocked, td, wle);
130234947Sdavidxu	else
131234947Sdavidxu		TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle);
132216642Sdavidxu}
133216642Sdavidxu
134216642Sdavidxuint
135216642Sdavidxu_sleepq_remove(struct sleepqueue *sq, struct pthread *td)
136216642Sdavidxu{
137216642Sdavidxu	int rc;
138216642Sdavidxu
139216642Sdavidxu	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
140216642Sdavidxu	if (TAILQ_EMPTY(&sq->sq_blocked)) {
141216642Sdavidxu		LIST_REMOVE(sq, sq_hash);
142216642Sdavidxu		td->sleepqueue = sq;
143216642Sdavidxu		rc = 0;
144216642Sdavidxu	} else {
145216642Sdavidxu		td->sleepqueue = SLIST_FIRST(&sq->sq_freeq);
146216642Sdavidxu		SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink);
147216642Sdavidxu		rc = 1;
148216642Sdavidxu	}
149216642Sdavidxu	td->wchan = NULL;
150216642Sdavidxu	return (rc);
151216642Sdavidxu}
152216642Sdavidxu
153216642Sdavidxuvoid
154216642Sdavidxu_sleepq_drop(struct sleepqueue *sq,
155216642Sdavidxu	void (*cb)(struct pthread *, void *arg), void *arg)
156216642Sdavidxu{
157216642Sdavidxu	struct pthread *td;
158216642Sdavidxu	struct sleepqueue *sq2;
159216642Sdavidxu
160216642Sdavidxu	td = TAILQ_FIRST(&sq->sq_blocked);
161216642Sdavidxu	if (td == NULL)
162216642Sdavidxu		return;
163216642Sdavidxu	LIST_REMOVE(sq, sq_hash);
164216642Sdavidxu	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
165216642Sdavidxu	if (cb != NULL)
166216642Sdavidxu		cb(td, arg);
167216642Sdavidxu	td->sleepqueue = sq;
168216642Sdavidxu	td->wchan = NULL;
169216642Sdavidxu	sq2 = SLIST_FIRST(&sq->sq_freeq);
170216642Sdavidxu	TAILQ_FOREACH(td, &sq->sq_blocked, wle) {
171216642Sdavidxu		if (cb != NULL)
172216642Sdavidxu			cb(td, arg);
173216642Sdavidxu		td->sleepqueue = sq2;
174216642Sdavidxu		td->wchan = NULL;
175216642Sdavidxu		sq2 = SLIST_NEXT(sq2, sq_flink);
176216642Sdavidxu	}
177216642Sdavidxu	TAILQ_INIT(&sq->sq_blocked);
178216642Sdavidxu	SLIST_INIT(&sq->sq_freeq);
179216642Sdavidxu}
180