eventfd 入门介绍

2018-08-19  本文已影响971人  斐然成章

什么是 eventfd ?

eventfd 是 Linux 的一个系统调用,创建一个文件描述符用于事件通知,自 Linux 2.6.22 以后开始支持。

接口及参数介绍

#include <sys/eventfd.h>
int eventfd(unsigned int initval, int flags);

eventfd() 创建一个 eventfd 对象,可以由用户空间应用程序实现事件等待/通知机制,或由内核通知用户空间应用程序事件。
该对象包含了由内核维护的无符号64位整数计数器 count 。使用参数 initval 初始化此计数器。

struct eventfd_ctx {
    struct kref kref;
    wait_queue_head_t wqh;
    /*
     * Every time that a write(2) is performed on an eventfd, the
     * value of the __u64 being written is added to "count" and a
     * wakeup is performed on "wqh". A read(2) will return the "count"
     * value to userspace, and will reset "count" to zero. The kernel
     * side eventfd_signal() also, adds to the "count" counter and
     * issue a wakeup.
     */
    __u64 count;
    unsigned int flags;
};

flags 可以是以下值的 OR 运算结果,用以改变 eventfd 的行为。

在 Linux 2.6.26 版本之前,没有使用参数 flags,必须指定为 0。

操作方法

一切皆为文件是 Linux 内核设计的一种高度抽象,eventfd 的实现也不例外,我们可以使用操作文件的方法操作 eventfd。

使用场景

在 pipe 仅用于发出事件信号的所有情况下,都可以使用 eventfd 取而代之。

参考资料

http://man7.org/linux/man-pages/man2/eventfd2.2.html

上一篇 下一篇

猜你喜欢

热点阅读