-
Notifications
You must be signed in to change notification settings - Fork 197
/
kthread_sample.c
53 lines (46 loc) · 1.01 KB
/
kthread_sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
#include <linux/sched/signal.h>
#endif
static DECLARE_COMPLETION(completion);
static int thread_sample(void *data)
{
int ret = 0;
allow_signal(SIGINT);
while (!kthread_should_stop()) {
ret = wait_for_completion_interruptible(&completion);
if (ret == -ERESTARTSYS) {
pr_debug("interrupted\n");
return -EINTR;
}
/*
perform here a useful work in scheduler context
*/
}
return ret;
}
static struct task_struct *thread;
static int thread_sample_init(void)
{
int ret = 0;
thread = kthread_run(thread_sample, NULL, "%s", KBUILD_MODNAME);
if (IS_ERR(thread)) {
ret = PTR_ERR(thread);
goto exit;
}
complete(&completion);
exit:
return ret;
}
static void thread_sample_exit(void)
{
if (!IS_ERR_OR_NULL(thread)) {
send_sig(SIGINT, thread, 1);
kthread_stop(thread);
}
}
module_init(thread_sample_init);
module_exit(thread_sample_exit);
MODULE_LICENSE("Dual BSD/GPL");