2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* completions.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/completion.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/kthread.h>
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/module.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct completion crank_comp;
|
|
|
|
struct completion flywheel_comp;
|
|
|
|
} machine;
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
static int machine_crank_thread(void *arg)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("Turn the crank\n");
|
|
|
|
|
|
|
|
complete_all(&machine.crank_comp);
|
|
|
|
complete_and_exit(&machine.crank_comp, 0);
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
static int machine_flywheel_spinup_thread(void *arg)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2021-07-22 06:58:13 +08:00
|
|
|
struct task_struct *crank_thread;
|
|
|
|
struct task_struct *flywheel_thread;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
pr_info("completions example\n");
|
|
|
|
|
|
|
|
init_completion(&machine.crank_comp);
|
|
|
|
init_completion(&machine.flywheel_comp);
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
crank_thread = kthread_create(machine_crank_thread, NULL, "KThread Crank");
|
2021-07-22 06:35:24 +08:00
|
|
|
if (IS_ERR(crank_thread))
|
|
|
|
goto ERROR_THREAD_1;
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
flywheel_thread = kthread_create(machine_flywheel_spinup_thread, NULL,
|
|
|
|
"KThread Flywheel");
|
2021-07-22 06:35:24 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static void completions_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
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");
|