首页 > 解决方案 > 添加 DEFINE_WAIT 无法在 linux 模块中编译

问题描述

我通过调用 DEFINE_WAIT 添加了一个等待队列条目,并且代码无法编译。

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>

MODULE_LICENSE("GPL");

DEFINE_WAIT(mywait);

static int __init test_hello_init(void)
{
    return 0;
}

static void __exit test_hello_exit(void)
{
}

module_init(test_hello_init);
module_exit(test_hello_exit);

它失败并出现以下错误,说初始化元素不是常量。

In file included from ./include/linux/thread_info.h:21:0,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/spinlock.h:51,
                 from ./include/linux/seqlock.h:36,
                 from ./include/linux/time.h:6,
                 from ./include/linux/stat.h:19,
                 from ./include/linux/module.h:10,
                 from /home/linuxtrainer/Linux_Device_Drivers/day20/3_waitqueue/2/hello.c:2:
./arch/x86/include/asm/current.h:18:17: error: initializer element is not constant
 #define current get_current()

在 test_hello_init 中添加 DEFINE_WAIT 有效,有什么区别?

标签: linuxlinux-kernelsynchronizationlinux-device-driver

解决方案


DEFINE_WAIT()使用不是编译时常量的初始化程序——它需要在进程上下文中执行——所以它不能在文件范围内使用。

它应该直接在将要进行等待的函数中使用。


推荐阅读