2 * Copyright (c) 2017 Hans Petter Selasky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <linux/compat.h>
31 #include <linux/kthread.h>
32 #include <linux/sched.h>
33 #include <linux/wait.h>
36 #include <sys/interrupt.h>
37 #include <sys/priority.h>
40 KTHREAD_SHOULD_STOP_MASK = (1 << 0),
41 KTHREAD_SHOULD_PARK_MASK = (1 << 1),
42 KTHREAD_IS_PARKED_MASK = (1 << 2),
46 linux_kthread_should_stop_task(struct task_struct *task)
49 return (atomic_read(&task->kthread_flags) & KTHREAD_SHOULD_STOP_MASK);
53 linux_kthread_should_stop(void)
56 return (atomic_read(¤t->kthread_flags) & KTHREAD_SHOULD_STOP_MASK);
60 linux_kthread_stop(struct task_struct *task)
65 * Assume task is still alive else caller should not call
68 atomic_or(KTHREAD_SHOULD_STOP_MASK, &task->kthread_flags);
70 wake_up_process(task);
71 wait_for_completion(&task->exited);
74 * Get return code and free task structure:
76 retval = task->task_ret;
77 put_task_struct(task);
83 linux_kthread_park(struct task_struct *task)
86 atomic_or(KTHREAD_SHOULD_PARK_MASK, &task->kthread_flags);
87 wake_up_process(task);
88 wait_for_completion(&task->parked);
93 linux_kthread_parkme(void)
95 struct task_struct *task;
98 set_task_state(task, TASK_PARKED | TASK_UNINTERRUPTIBLE);
99 while (linux_kthread_should_park()) {
100 while ((atomic_fetch_or(KTHREAD_IS_PARKED_MASK,
101 &task->kthread_flags) & KTHREAD_IS_PARKED_MASK) == 0)
102 complete(&task->parked);
104 set_task_state(task, TASK_PARKED | TASK_UNINTERRUPTIBLE);
106 atomic_andnot(KTHREAD_IS_PARKED_MASK, &task->kthread_flags);
107 set_task_state(task, TASK_RUNNING);
111 linux_kthread_should_park(void)
113 struct task_struct *task;
116 return (atomic_read(&task->kthread_flags) & KTHREAD_SHOULD_PARK_MASK);
120 linux_kthread_unpark(struct task_struct *task)
123 atomic_andnot(KTHREAD_SHOULD_PARK_MASK, &task->kthread_flags);
124 if ((atomic_fetch_andnot(KTHREAD_IS_PARKED_MASK, &task->kthread_flags) &
125 KTHREAD_IS_PARKED_MASK) != 0)
126 wake_up_state(task, TASK_PARKED);
130 linux_kthread_setup_and_run(struct thread *td, linux_task_fn_t *task_fn, void *arg)
132 struct task_struct *task;
134 linux_set_current(td);
136 task = td->td_lkpi_task;
137 task->task_fn = task_fn;
138 task->task_data = arg;
141 /* make sure the scheduler priority is raised */
142 sched_prio(td, PI_SWI(SWI_NET));
143 /* put thread into run-queue */
144 sched_add(td, SRQ_BORING);
151 linux_kthread_fn(void *arg __unused)
153 struct task_struct *task = current;
155 if (linux_kthread_should_stop_task(task) == 0)
156 task->task_ret = task->task_fn(task->task_data);
158 if (linux_kthread_should_stop_task(task) != 0) {
159 struct thread *td = curthread;
161 /* let kthread_stop() free data */
162 td->td_lkpi_task = NULL;
164 /* wakeup kthread_stop() */
165 complete(&task->exited);