Merge branch vendor/AWK into master.
[dragonfly.git] / sys / dev / drm / linux_kthread.c
1 /*
2  * Copyright (c) 2019 François Tigeot <ftigeot@wolfpond.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    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 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.
25  */
26
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30
31 #include <sys/kthread.h>
32
33 /*
34    All Linux threads/processes have an associated task_struct
35    a kthread is a pure kernel thread without userland context
36 */
37
38 static void
39 linux_ktfn_wrapper(void *arg)
40 {
41         struct task_struct *task = arg;
42
43         task->kt_exitvalue = task->kt_fn(task->kt_fndata);
44 }
45
46 struct task_struct *
47 kthread_run(int (*lfn)(void *), void *data, const char *namefmt, ...)
48 {
49         struct task_struct *task;
50         struct thread *td;
51         __va_list args;
52         int ret;
53
54         task = kzalloc(sizeof(*task), GFP_KERNEL);
55
56         __va_start(args, namefmt);
57         ret = kthread_alloc(linux_ktfn_wrapper, task, &td, namefmt, args);
58         __va_end(args);
59         if (ret) {
60                 kfree(task);
61                 return ERR_PTR(-ENOMEM);
62         }
63
64         task->dfly_td = td;
65         td->td_linux_task = task;
66
67         task->mm = NULL;        /* kthreads have no userland address space */
68
69         task->kt_fn = lfn;
70         task->kt_fndata = data;
71
72         /* Start the thread here */
73         lwkt_schedule(td);
74
75         return task;
76 }
77
78 #define KTHREAD_SHOULD_STOP 1
79
80 bool
81 kthread_should_stop(void)
82 {
83         return test_bit(KTHREAD_SHOULD_STOP, &current->kt_flags);
84 }
85
86 int
87 kthread_stop(struct task_struct *ts)
88 {
89         set_bit(KTHREAD_SHOULD_STOP, &ts->kt_flags);
90
91         wake_up_process(ts);
92         wait_for_completion(&ts->kt_exited);
93
94         return ts->kt_exitvalue;
95 }