| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: linux_kthread.c,v 1.3 2021/12/19 12:29:39 riastradh Exp $ */ | | 1 | /* $NetBSD: linux_kthread.c,v 1.4 2021/12/19 12:38:56 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2021 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2021 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Taylor R. Campbell. | | 8 | * by Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -20,27 +20,27 @@ | | | @@ -20,27 +20,27 @@ |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | __KERNEL_RCSID(0, "$NetBSD: linux_kthread.c,v 1.3 2021/12/19 12:29:39 riastradh Exp $"); | | 33 | __KERNEL_RCSID(0, "$NetBSD: linux_kthread.c,v 1.4 2021/12/19 12:38:56 riastradh Exp $"); |
34 | | | 34 | |
35 | #include <sys/types.h> | | 35 | #include <sys/types.h> |
36 | | | 36 | |
37 | #include <sys/condvar.h> | | 37 | #include <sys/condvar.h> |
38 | #include <sys/kmem.h> | | 38 | #include <sys/kmem.h> |
39 | #include <sys/kthread.h> | | 39 | #include <sys/kthread.h> |
40 | #include <sys/lwp.h> | | 40 | #include <sys/lwp.h> |
41 | #include <sys/mutex.h> | | 41 | #include <sys/mutex.h> |
42 | #include <sys/specificdata.h> | | 42 | #include <sys/specificdata.h> |
43 | | | 43 | |
44 | #include <linux/kthread.h> | | 44 | #include <linux/kthread.h> |
45 | | | 45 | |
46 | struct task_struct { | | 46 | struct task_struct { |
| @@ -174,33 +174,66 @@ kthread_should_stop(void) | | | @@ -174,33 +174,66 @@ kthread_should_stop(void) |
174 | struct task_struct *T = linux_kthread(); | | 174 | struct task_struct *T = linux_kthread(); |
175 | bool shouldstop; | | 175 | bool shouldstop; |
176 | | | 176 | |
177 | mutex_enter(&T->kt_lock); | | 177 | mutex_enter(&T->kt_lock); |
178 | shouldstop = T->kt_shouldstop; | | 178 | shouldstop = T->kt_shouldstop; |
179 | mutex_exit(&T->kt_lock); | | 179 | mutex_exit(&T->kt_lock); |
180 | | | 180 | |
181 | return shouldstop; | | 181 | return shouldstop; |
182 | } | | 182 | } |
183 | | | 183 | |
184 | void | | 184 | void |
185 | kthread_park(struct task_struct *T) | | 185 | kthread_park(struct task_struct *T) |
186 | { | | 186 | { |
| | | 187 | struct lwp *l; |
187 | | | 188 | |
188 | mutex_enter(&T->kt_lock); | | 189 | mutex_enter(&T->kt_lock); |
| | | 190 | |
| | | 191 | /* Caller must not ask to park if they've already asked to stop. */ |
189 | KASSERT(!T->kt_shouldstop); | | 192 | KASSERT(!T->kt_shouldstop); |
| | | 193 | |
| | | 194 | /* Ask the thread to park. */ |
190 | T->kt_shouldpark = true; | | 195 | T->kt_shouldpark = true; |
| | | 196 | |
| | | 197 | /* Don't wait for ourselves -- Linux allows this semantics. */ |
| | | 198 | if ((l = T->kt_lwp) == curlwp) |
| | | 199 | goto out; |
| | | 200 | |
| | | 201 | /* |
| | | 202 | * If the thread is asleep for any reason, give it a spurious |
| | | 203 | * wakeup. The thread is responsible for checking |
| | | 204 | * kthread_shouldpark before sleeping. This logic is like |
| | | 205 | * sleepq_timeout, but without setting LW_STIMO. |
| | | 206 | */ |
| | | 207 | lwp_lock(l); |
| | | 208 | if (l->l_wchan == NULL) { |
| | | 209 | /* |
| | | 210 | * Not sleeping, so no need to wake up -- the thread |
| | | 211 | * will eventually check kthread_shouldpark. |
| | | 212 | */ |
| | | 213 | lwp_unlock(l); |
| | | 214 | } else { |
| | | 215 | /* |
| | | 216 | * Sleeping, so wake it up. lwp_unsleep has the side |
| | | 217 | * effect of unlocking l when we pass unlock=true. |
| | | 218 | */ |
| | | 219 | lwp_unsleep(l, /*unlock*/true); |
| | | 220 | } |
| | | 221 | |
| | | 222 | /* Wait until the thread has issued kthread_parkme. */ |
191 | while (!T->kt_parked) | | 223 | while (!T->kt_parked) |
192 | cv_wait(&T->kt_cv, &T->kt_lock); | | 224 | cv_wait(&T->kt_cv, &T->kt_lock); |
193 | mutex_exit(&T->kt_lock); | | 225 | |
| | | 226 | out: mutex_exit(&T->kt_lock); |
194 | } | | 227 | } |
195 | | | 228 | |
196 | void | | 229 | void |
197 | kthread_unpark(struct task_struct *T) | | 230 | kthread_unpark(struct task_struct *T) |
198 | { | | 231 | { |
199 | | | 232 | |
200 | mutex_enter(&T->kt_lock); | | 233 | mutex_enter(&T->kt_lock); |
201 | T->kt_shouldpark = false; | | 234 | T->kt_shouldpark = false; |
202 | cv_broadcast(&T->kt_cv); | | 235 | cv_broadcast(&T->kt_cv); |
203 | mutex_exit(&T->kt_lock); | | 236 | mutex_exit(&T->kt_lock); |
204 | } | | 237 | } |
205 | | | 238 | |
206 | int | | 239 | int |