forked from sysprog21/lkmpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completions.c
76 lines (57 loc) · 1.67 KB
/
completions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* completions.c
*/
#include <linux/completion.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
static struct {
struct completion crank_comp;
struct completion flywheel_comp;
} machine;
static int machine_crank_thread(void *arg)
{
pr_info("Turn the crank\n");
complete_all(&machine.crank_comp);
complete_and_exit(&machine.crank_comp, 0);
}
static int machine_flywheel_spinup_thread(void *arg)
{
wait_for_completion(&machine.crank_comp);
pr_info("Flywheel spins up\n");
complete_all(&machine.flywheel_comp);
complete_and_exit(&machine.flywheel_comp, 0);
}
static int completions_init(void)
{
struct task_struct *crank_thread;
struct task_struct *flywheel_thread;
pr_info("completions example\n");
init_completion(&machine.crank_comp);
init_completion(&machine.flywheel_comp);
crank_thread = kthread_create(machine_crank_thread, NULL, "KThread Crank");
if (IS_ERR(crank_thread))
goto ERROR_THREAD_1;
flywheel_thread = kthread_create(machine_flywheel_spinup_thread, NULL,
"KThread Flywheel");
if (IS_ERR(flywheel_thread))
goto ERROR_THREAD_2;
wake_up_process(flywheel_thread);
wake_up_process(crank_thread);
return 0;
ERROR_THREAD_2:
kthread_stop(crank_thread);
ERROR_THREAD_1:
return -1;
}
static void completions_exit(void)
{
wait_for_completion(&machine.crank_comp);
wait_for_completion(&machine.flywheel_comp);
pr_info("completions exit\n");
}
module_init(completions_init);
module_exit(completions_exit);
MODULE_DESCRIPTION("Completions example");
MODULE_LICENSE("GPL");