90 lines
2.7 KiB
C
90 lines
2.7 KiB
C
/*
|
|
* completions.c 完成情况示例程序
|
|
*/
|
|
#include <linux/completion.h>
|
|
#include <linux/err.h> /* 引入 IS_ERR() */
|
|
#include <linux/init.h>
|
|
#include <linux/kthread.h>
|
|
#include <linux/module.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/version.h>
|
|
|
|
static struct completion crank_comp; /* 定义完成情况 crank_comp */
|
|
static struct completion flywheel_comp; /* 定义完成情况 flywheel_comp */
|
|
|
|
/* crank线程函数 */
|
|
static int machine_crank_thread(void *arg)
|
|
{
|
|
pr_info("crank转动\n");
|
|
|
|
complete_all(&crank_comp); /* 设置 crank_comp 完成情况 */
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
|
|
kthread_complete_and_exit(&crank_comp, 0); /* 内核版本 >= 5.17,使用 kthread 完成并退出 */
|
|
#else
|
|
complete_and_exit(&crank_comp, 0); /* 否则,使用旧版本接口完成并退出 */
|
|
#endif
|
|
}
|
|
|
|
/* flywheel启动线程函数 */
|
|
static int machine_flywheel_spinup_thread(void *arg)
|
|
{
|
|
wait_for_completion(&crank_comp); /* 等待 crank_comp 完成情况 */
|
|
|
|
pr_info("flywheel启动\n");
|
|
|
|
complete_all(&flywheel_comp); /* 设置 flywheel_comp 完成情况 */
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
|
|
kthread_complete_and_exit(&flywheel_comp, 0); /* 内核版本 >= 5.17,使用 kthread 完成并退出 */
|
|
#else
|
|
complete_and_exit(&flywheel_comp, 0); /* 否则,使用旧版本接口完成并退出 */
|
|
#endif
|
|
}
|
|
|
|
static int __init completions_init(void)
|
|
{
|
|
struct task_struct *crank_thread; /* crank线程 */
|
|
struct task_struct *flywheel_thread; /* flywheel线程 */
|
|
|
|
pr_info("完成情况示例加载\n");
|
|
|
|
init_completion(&crank_comp); /* 初始化 crank_comp 完成情况 */
|
|
init_completion(&flywheel_comp); /* 初始化 flywheel_comp 完成情况 */
|
|
|
|
crank_thread = kthread_create(machine_crank_thread, NULL, "KThread Crank"); /* 创建crank线程 */
|
|
if (IS_ERR(crank_thread))
|
|
goto ERROR_THREAD_1;
|
|
|
|
flywheel_thread = kthread_create(machine_flywheel_spinup_thread, NULL,
|
|
"KThread Flywheel"); /* 创建flywheel线程 */
|
|
if (IS_ERR(flywheel_thread))
|
|
goto ERROR_THREAD_2;
|
|
|
|
wake_up_process(flywheel_thread); /* 启动flywheel线程 */
|
|
wake_up_process(crank_thread); /* 启动crank线程 */
|
|
|
|
return 0;
|
|
|
|
ERROR_THREAD_2:
|
|
kthread_stop(crank_thread); /* 停止crank线程 */
|
|
ERROR_THREAD_1:
|
|
|
|
return -1;
|
|
}
|
|
|
|
static void __exit completions_exit(void)
|
|
{
|
|
wait_for_completion(&crank_comp); /* 等待crank线程完成 */
|
|
wait_for_completion(&flywheel_comp); /* 等待flywheel线程完成 */
|
|
|
|
pr_info("完成情况示例卸载\n");
|
|
}
|
|
|
|
module_init(completions_init);
|
|
module_exit(completions_exit);
|
|
|
|
MODULE_DESCRIPTION("完成情况示例");
|
|
MODULE_LICENSE("GPL");
|
|
|