EPOLLONESHOT 是否阻止在对 epoll_wait() 的单次调用中返回单个描述符上的多个事件?

Does EPOLLONESHOT prevent multiple events on a single descriptor from being returned in a single call to epoll_wait()?

epoll_ctl(2) 上的手册页有关于 EPOLLONESHOT 标志的说明:

Sets the one-shot behavior for the associated file descriptor.
This means that after an event is pulled out with
epoll_wait(2) the associated file descriptor is internally
disabled and no other events will be reported by the epoll
interface.  The user must call epoll_ctl() with EPOLL_CTL_MOD
to rearm the file descriptor with a new event mask.

但是,尚不清楚事件是在 epoll_wait 中在插入事件数组之后还是在返回所有事件之后被禁用。

EPOLLONESHOT 的行为是在成功调用报告指定文件描述符的 epoll_wait(2) 后,epoll_wait(2) 不会在同一文件上报告新事件描述符,直到您使用 epoll_ctl(2) 显式重新激活它。您可以将其视为一种在 epoll_wait(2).

返回后临时禁用文件描述符的机制

它不会阻止 epoll_wait(2) 在同一文件描述符的同一调用中返回多个事件 - 事实上,如果在调用时有多个事件可用,它们将全部组合成struct epoll_eventevents 字段,无论 EPOLLONESHOT 是否对该文件描述符有效。

换句话说,EPOLLONESHOT 控制在调用 epoll_wait(2) 报告 文件描述符的条件;它不参与事件聚合和检测。

一个例子

下面的代码创建一对连接的套接字并写入一端。 sv[1] 将可用于读写。如您所见,添加或删除 EPOLLONESHOTEPOLLINEPOLLOUT 合并为一个事件这一事实没有影响。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(void) {
    int efd = epoll_create(1);
    if (efd < 0) {
        perror("epoll_create(2) error");
        exit(EXIT_FAILURE);
    }

    int sv[2];
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
        perror("socketpair(2) error");
        exit(EXIT_FAILURE);
    }

    const char str[] = "Hello, world!";
    size_t to_write = sizeof(str)-1;
    ssize_t written = write(sv[0], str, to_write);

    if (written != to_write) {
        if (written < 0) {
            perror("write(2) error");
        } else {
            fprintf(stderr, "short write detected: %zd/%zu\n", written, to_write);
        }
        exit(EXIT_FAILURE);
    }

    struct epoll_event epevent;
    epevent.events = EPOLLIN | EPOLLOUT | EPOLLONESHOT; // Try to remove EPOLLONESHOT here
    epevent.data.fd = sv[1];

    if (epoll_ctl(efd, EPOLL_CTL_ADD, sv[1], &epevent) < 0) {
        perror("epoll_ctl(2) error");
        exit(EXIT_FAILURE);
    }

    struct epoll_event events_arr[16];
    int events = epoll_wait(efd, events_arr, sizeof(events_arr)/sizeof(*events_arr), -1);
    if (events < 0) {
        perror("epoll_wait(2) error");
        exit(EXIT_FAILURE);
    }

    int i;
    for (i = 0; i < events; i++) {
        printf("Event %d: ", i);
        if (events_arr[i].events & EPOLLIN)
            printf("EPOLLIN ");
        if (events_arr[i].events & EPOLLOUT)
            printf("EPOLLOUT ");
        printf("\n");
    }

    return 0;
}