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