sleepq.c revision 1.1 1 /* $NetBSD: sleepq.c,v 1.1 2008/10/10 13:14:41 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following 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
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/condvar.h>
30 #include <sys/mutex.h>
31 #include <sys/queue.h>
32 #include <sys/sleepq.h>
33 #include <sys/syncobj.h>
34
35 /*
36 * Flimsy and minimalistic sleepq implementation. This is implemented
37 * only for the use of callouts in kern_timeout.c. locking etc is
38 * completely incorrect, horrible, etc etc etc.
39 */
40
41 syncobj_t sleep_syncobj;
42 static kcondvar_t sq_cv;
43 static kmutex_t sq_mtx;
44
45 void
46 sleepq_init(sleepq_t *sq)
47 {
48
49 TAILQ_INIT(sq);
50
51 cv_init(&sq_cv, "sleepq"); /* XXX */
52 mutex_init(&sq_mtx, MUTEX_DEFAULT, IPL_NONE); /* multi-XXX */
53 }
54
55 void
56 sleepq_enqueue(sleepq_t *sq, wchan_t wc, const char *wmsg, syncobj_t *sob)
57 {
58 struct lwp *l = curlwp;
59
60 if (__predict_false(sob != &sleep_syncobj || strcmp(wmsg, "callout"))) {
61 panic("sleepq: unsupported enqueue");
62 }
63
64 l->l_wchan = wc;
65 TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
66 }
67
68 int
69 sleepq_block(int timo, bool hatch)
70 {
71 struct lwp *l = curlwp;
72
73 KASSERT(timo == 0 && !hatch);
74
75 mutex_enter(&sq_mtx);
76 while (l->l_wchan)
77 cv_wait(&sq_cv, &sq_mtx);
78 mutex_exit(&sq_mtx);
79
80 return 0;
81 }
82
83 lwp_t *
84 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
85 {
86 struct lwp *l;
87 bool found = false;
88
89 TAILQ_FOREACH(l, sq, l_sleepchain) {
90 if (l->l_wchan == wchan) {
91 found = true;
92 l->l_wchan = NULL;
93 }
94 }
95 if (found)
96 cv_broadcast(&sq_cv);
97
98 mutex_spin_exit(mp);
99 return NULL;
100 }
101
102 /*
103 * XXX: used only by callout, therefore here
104 *
105 * We don't fudge around with the lwp mutex at all, therefore
106 * this is enough.
107 */
108 kmutex_t *
109 lwp_lock_retry(struct lwp *l, kmutex_t *old)
110 {
111
112 return old;
113 }
114