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$
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;
42216642Sdavidxu	LIST_HEAD(, sleepqueue) sc_queues;
43216642Sdavidxu	int			sc_type;
44216642Sdavidxu};
45216642Sdavidxu
46216642Sdavidxustatic struct sleepqueue_chain  sc_table[HASHSIZE];
47216642Sdavidxu
48216642Sdavidxuvoid
49216642Sdavidxu_sleepq_init(void)
50216642Sdavidxu{
51216642Sdavidxu	int	i;
52216642Sdavidxu
53216642Sdavidxu	for (i = 0; i < HASHSIZE; ++i) {
54216642Sdavidxu		LIST_INIT(&sc_table[i].sc_queues);
55216642Sdavidxu		_thr_umutex_init(&sc_table[i].sc_lock);
56216642Sdavidxu	}
57216642Sdavidxu}
58216642Sdavidxu
59216642Sdavidxustruct sleepqueue *
60216642Sdavidxu_sleepq_alloc(void)
61216642Sdavidxu{
62216642Sdavidxu	struct sleepqueue *sq;
63216642Sdavidxu
64216642Sdavidxu	sq = calloc(1, sizeof(struct sleepqueue));
65216642Sdavidxu	TAILQ_INIT(&sq->sq_blocked);
66216642Sdavidxu	SLIST_INIT(&sq->sq_freeq);
67216642Sdavidxu	return (sq);
68216642Sdavidxu}
69216642Sdavidxu
70216642Sdavidxuvoid
71216642Sdavidxu_sleepq_free(struct sleepqueue *sq)
72216642Sdavidxu{
73216642Sdavidxu	free(sq);
74216642Sdavidxu}
75216642Sdavidxu
76216642Sdavidxuvoid
77216642Sdavidxu_sleepq_lock(void *wchan)
78216642Sdavidxu{
79216642Sdavidxu	struct pthread *curthread = _get_curthread();
80216642Sdavidxu	struct sleepqueue_chain *sc;
81216642Sdavidxu
82216642Sdavidxu	sc = SC_LOOKUP(wchan);
83216642Sdavidxu	THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock);
84216642Sdavidxu}
85216642Sdavidxu
86216642Sdavidxuvoid
87216642Sdavidxu_sleepq_unlock(void *wchan)
88216642Sdavidxu{
89216642Sdavidxu	struct sleepqueue_chain *sc;
90216642Sdavidxu	struct pthread *curthread = _get_curthread();
91216642Sdavidxu
92216642Sdavidxu	sc = SC_LOOKUP(wchan);
93216642Sdavidxu	THR_LOCK_RELEASE(curthread, &sc->sc_lock);
94216642Sdavidxu}
95216642Sdavidxu
96216642Sdavidxustruct sleepqueue *
97216642Sdavidxu_sleepq_lookup(void *wchan)
98216642Sdavidxu{
99216642Sdavidxu	struct sleepqueue_chain *sc;
100216642Sdavidxu	struct sleepqueue *sq;
101216642Sdavidxu
102216642Sdavidxu	sc = SC_LOOKUP(wchan);
103216642Sdavidxu	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
104216642Sdavidxu		if (sq->sq_wchan == wchan)
105216642Sdavidxu			return (sq);
106216642Sdavidxu	return (NULL);
107216642Sdavidxu}
108216642Sdavidxu
109216642Sdavidxuvoid
110216642Sdavidxu_sleepq_add(void *wchan, struct pthread *td)
111216642Sdavidxu{
112216642Sdavidxu	struct sleepqueue_chain *sc;
113216642Sdavidxu	struct sleepqueue *sq;
114216642Sdavidxu
115216642Sdavidxu	sq = _sleepq_lookup(wchan);
116216642Sdavidxu	if (sq != NULL) {
117216642Sdavidxu		SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink);
118216642Sdavidxu	} else {
119216642Sdavidxu		sc = SC_LOOKUP(wchan);
120216642Sdavidxu		sq = td->sleepqueue;
121216642Sdavidxu		LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
122216642Sdavidxu		sq->sq_wchan = wchan;
123216642Sdavidxu		/* sq->sq_type = type; */
124216642Sdavidxu	}
125216642Sdavidxu	td->sleepqueue = NULL;
126216642Sdavidxu	td->wchan = wchan;
127216642Sdavidxu	TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle);
128216642Sdavidxu}
129216642Sdavidxu
130216642Sdavidxuint
131216642Sdavidxu_sleepq_remove(struct sleepqueue *sq, struct pthread *td)
132216642Sdavidxu{
133216642Sdavidxu	int rc;
134216642Sdavidxu
135216642Sdavidxu	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
136216642Sdavidxu	if (TAILQ_EMPTY(&sq->sq_blocked)) {
137216642Sdavidxu		LIST_REMOVE(sq, sq_hash);
138216642Sdavidxu		td->sleepqueue = sq;
139216642Sdavidxu		rc = 0;
140216642Sdavidxu	} else {
141216642Sdavidxu		td->sleepqueue = SLIST_FIRST(&sq->sq_freeq);
142216642Sdavidxu		SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink);
143216642Sdavidxu		rc = 1;
144216642Sdavidxu	}
145216642Sdavidxu	td->wchan = NULL;
146216642Sdavidxu	return (rc);
147216642Sdavidxu}
148216642Sdavidxu
149216642Sdavidxuvoid
150216642Sdavidxu_sleepq_drop(struct sleepqueue *sq,
151216642Sdavidxu	void (*cb)(struct pthread *, void *arg), void *arg)
152216642Sdavidxu{
153216642Sdavidxu	struct pthread *td;
154216642Sdavidxu	struct sleepqueue *sq2;
155216642Sdavidxu
156216642Sdavidxu	td = TAILQ_FIRST(&sq->sq_blocked);
157216642Sdavidxu	if (td == NULL)
158216642Sdavidxu		return;
159216642Sdavidxu	LIST_REMOVE(sq, sq_hash);
160216642Sdavidxu	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
161216642Sdavidxu	if (cb != NULL)
162216642Sdavidxu		cb(td, arg);
163216642Sdavidxu	td->sleepqueue = sq;
164216642Sdavidxu	td->wchan = NULL;
165216642Sdavidxu	sq2 = SLIST_FIRST(&sq->sq_freeq);
166216642Sdavidxu	TAILQ_FOREACH(td, &sq->sq_blocked, wle) {
167216642Sdavidxu		if (cb != NULL)
168216642Sdavidxu			cb(td, arg);
169216642Sdavidxu		td->sleepqueue = sq2;
170216642Sdavidxu		td->wchan = NULL;
171216642Sdavidxu		sq2 = SLIST_NEXT(sq2, sq_flink);
172216642Sdavidxu	}
173216642Sdavidxu	TAILQ_INIT(&sq->sq_blocked);
174216642Sdavidxu	SLIST_INIT(&sq->sq_freeq);
175216642Sdavidxu}
176