1/*
2 * Copyright (c) 2010 David Xu <davidxu@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <stdlib.h>
30#include "thr_private.h"
31
32#define HASHSHIFT	9
33#define HASHSIZE	(1 << HASHSHIFT)
34#define SC_HASH(wchan) ((unsigned)				\
35	((((uintptr_t)(wchan) >> 3)				\
36	^ ((uintptr_t)(wchan) >> (HASHSHIFT + 3)))		\
37	& (HASHSIZE - 1)))
38#define SC_LOOKUP(wc)	&sc_table[SC_HASH(wc)]
39
40struct sleepqueue_chain {
41	struct umutex		sc_lock;
42	LIST_HEAD(, sleepqueue) sc_queues;
43	int			sc_type;
44};
45
46static struct sleepqueue_chain  sc_table[HASHSIZE];
47
48void
49_sleepq_init(void)
50{
51	int	i;
52
53	for (i = 0; i < HASHSIZE; ++i) {
54		LIST_INIT(&sc_table[i].sc_queues);
55		_thr_umutex_init(&sc_table[i].sc_lock);
56	}
57}
58
59struct sleepqueue *
60_sleepq_alloc(void)
61{
62	struct sleepqueue *sq;
63
64	sq = calloc(1, sizeof(struct sleepqueue));
65	TAILQ_INIT(&sq->sq_blocked);
66	SLIST_INIT(&sq->sq_freeq);
67	return (sq);
68}
69
70void
71_sleepq_free(struct sleepqueue *sq)
72{
73	free(sq);
74}
75
76void
77_sleepq_lock(void *wchan)
78{
79	struct pthread *curthread = _get_curthread();
80	struct sleepqueue_chain *sc;
81
82	sc = SC_LOOKUP(wchan);
83	THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock);
84}
85
86void
87_sleepq_unlock(void *wchan)
88{
89	struct sleepqueue_chain *sc;
90	struct pthread *curthread = _get_curthread();
91
92	sc = SC_LOOKUP(wchan);
93	THR_LOCK_RELEASE(curthread, &sc->sc_lock);
94}
95
96struct sleepqueue *
97_sleepq_lookup(void *wchan)
98{
99	struct sleepqueue_chain *sc;
100	struct sleepqueue *sq;
101
102	sc = SC_LOOKUP(wchan);
103	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
104		if (sq->sq_wchan == wchan)
105			return (sq);
106	return (NULL);
107}
108
109void
110_sleepq_add(void *wchan, struct pthread *td)
111{
112	struct sleepqueue_chain *sc;
113	struct sleepqueue *sq;
114
115	sq = _sleepq_lookup(wchan);
116	if (sq != NULL) {
117		SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink);
118	} else {
119		sc = SC_LOOKUP(wchan);
120		sq = td->sleepqueue;
121		LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
122		sq->sq_wchan = wchan;
123		/* sq->sq_type = type; */
124	}
125	td->sleepqueue = NULL;
126	td->wchan = wchan;
127	TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle);
128}
129
130int
131_sleepq_remove(struct sleepqueue *sq, struct pthread *td)
132{
133	int rc;
134
135	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
136	if (TAILQ_EMPTY(&sq->sq_blocked)) {
137		LIST_REMOVE(sq, sq_hash);
138		td->sleepqueue = sq;
139		rc = 0;
140	} else {
141		td->sleepqueue = SLIST_FIRST(&sq->sq_freeq);
142		SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink);
143		rc = 1;
144	}
145	td->wchan = NULL;
146	return (rc);
147}
148
149void
150_sleepq_drop(struct sleepqueue *sq,
151	void (*cb)(struct pthread *, void *arg), void *arg)
152{
153	struct pthread *td;
154	struct sleepqueue *sq2;
155
156	td = TAILQ_FIRST(&sq->sq_blocked);
157	if (td == NULL)
158		return;
159	LIST_REMOVE(sq, sq_hash);
160	TAILQ_REMOVE(&sq->sq_blocked, td, wle);
161	if (cb != NULL)
162		cb(td, arg);
163	td->sleepqueue = sq;
164	td->wchan = NULL;
165	sq2 = SLIST_FIRST(&sq->sq_freeq);
166	TAILQ_FOREACH(td, &sq->sq_blocked, wle) {
167		if (cb != NULL)
168			cb(td, arg);
169		td->sleepqueue = sq2;
170		td->wchan = NULL;
171		sq2 = SLIST_NEXT(sq2, sq_flink);
172	}
173	TAILQ_INIT(&sq->sq_blocked);
174	SLIST_INIT(&sq->sq_freeq);
175}
176