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