sleepq.c revision 1.13.14.2 1 /* $NetBSD: sleepq.c,v 1.13.14.2 2014/08/20 00:04:41 tls 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/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: sleepq.c,v 1.13.14.2 2014/08/20 00:04:41 tls Exp $");
30
31 #include <sys/param.h>
32 #include <sys/condvar.h>
33 #include <sys/mutex.h>
34 #include <sys/once.h>
35 #include <sys/queue.h>
36 #include <sys/sleepq.h>
37 #include <sys/syncobj.h>
38 #include <sys/atomic.h>
39
40 #include "rump_private.h"
41
42 syncobj_t sleep_syncobj;
43 static kcondvar_t sq_cv;
44
45 static int
46 sqinit1(void)
47 {
48
49 cv_init(&sq_cv, "sleepq");
50
51 return 0;
52 }
53
54 void
55 sleepq_init(sleepq_t *sq)
56 {
57 static ONCE_DECL(sqctl);
58
59 RUN_ONCE(&sqctl, sqinit1);
60
61 TAILQ_INIT(sq);
62 }
63
64 void
65 sleepq_enqueue(sleepq_t *sq, wchan_t wc, const char *wmsg, syncobj_t *sob)
66 {
67 struct lwp *l = curlwp;
68
69 l->l_wchan = wc;
70 l->l_wmesg = wmsg;
71 l->l_sleepq = sq;
72 TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
73 }
74
75 int
76 sleepq_block(int timo, bool catch)
77 {
78 struct lwp *l = curlwp;
79 int error = 0;
80 kmutex_t *mp = l->l_mutex;
81 int biglocks = l->l_biglocks;
82
83 while (l->l_wchan) {
84 l->l_mutex = mp; /* keep sleepq lock until woken up */
85 error = cv_timedwait(&sq_cv, mp, timo);
86 if (error == EWOULDBLOCK || error == EINTR) {
87 if (l->l_wchan) {
88 TAILQ_REMOVE(l->l_sleepq, l, l_sleepchain);
89 l->l_wchan = NULL;
90 l->l_wmesg = NULL;
91 }
92 }
93 }
94 mutex_spin_exit(mp);
95
96 if (biglocks)
97 KERNEL_LOCK(biglocks, curlwp);
98
99 return error;
100 }
101
102 void
103 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
104 {
105 struct lwp *l, *l_next;
106 bool found = false;
107
108 for (l = TAILQ_FIRST(sq); l; l = l_next) {
109 l_next = TAILQ_NEXT(l, l_sleepchain);
110 if (l->l_wchan == wchan) {
111 found = true;
112 l->l_wchan = NULL;
113 l->l_wmesg = NULL;
114 TAILQ_REMOVE(sq, l, l_sleepchain);
115 if (--expected == 0)
116 break;
117 }
118 }
119 if (found)
120 cv_broadcast(&sq_cv);
121
122 mutex_spin_exit(mp);
123 }
124
125 void
126 sleepq_unsleep(struct lwp *l, bool cleanup)
127 {
128
129 l->l_wchan = NULL;
130 l->l_wmesg = NULL;
131 TAILQ_REMOVE(l->l_sleepq, l, l_sleepchain);
132 cv_broadcast(&sq_cv);
133
134 if (cleanup) {
135 mutex_spin_exit(l->l_mutex);
136 }
137 }
138
139 /*
140 * Thread scheduler handles priorities. Therefore no action here.
141 * (maybe do something if we're deperate?)
142 */
143 void
144 sleepq_changepri(struct lwp *l, pri_t pri)
145 {
146
147 }
148
149 void
150 sleepq_lendpri(struct lwp *l, pri_t pri)
151 {
152
153 }
154
155 struct lwp *
156 syncobj_noowner(wchan_t wc)
157 {
158
159 return NULL;
160 }
161
162 void
163 lwp_unlock_to(struct lwp *l, kmutex_t *new)
164 {
165 kmutex_t *old;
166
167 KASSERT(mutex_owned(l->l_mutex));
168
169 old = l->l_mutex;
170 membar_exit();
171 l->l_mutex = new;
172 mutex_spin_exit(old);
173 }
174