Significance of parameters in epoll_event structure (epoll)
Asked Answered
U

2

9

I am using epoll_ctl() and epoll_wait() system calls.

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);

struct epoll_event {
    uint32_t events; /* epoll events (bit mask) */
    epoll_data_t data; /* User data */
};


typedef union epoll_data {
     enter code here`void *ptr; /* Pointer to user-defined data */  
     int fd; /* File descriptor */  
     uint32_t u32; /* 32-bit integer */  
     uint64_t u64; /* 64-bit integer */  
} epoll_data_t;

When using epoll_ctl, I can use the union epoll_data to specify the fd. One way is to specify it in "fd" member. Other way is to specify it in my own structure, "ptr" member will point to the structure.

What is the use of "u32" and "u64" ?

I went through the kernel system call implementation and found the following:
1. epoll_ctl initializes the epoll_event and stores it (in some RB tree format)
2. when fd is ready, epoll_wait returns epoll_event that was filled in epoll_ctl. After this I can identify the fd which becomes ready. I don't understand the purpose of "u32" and "u64".

Uralian answered 8/7, 2014 at 2:56 Comment(0)
R
7

The predefined union is for convinient usages and you will usually use only one of them:

  • use ptr if you want to store a pointer
  • use fd if you want to store socket descriptor
  • use u32/u64 if you want to store a general/opaque 32/64 bit number

Actually epoll_data is 64bit data associated with a socket event for you to store anything to find event handler as epoll_wait returns just 2 things: the event & associated epoll_data.

Radioactive answered 16/7, 2014 at 8:35 Comment(1)
A similar idea is implemented in the io_uring mechanism in the latest kernel __u64 user_data;Pham
S
5

The fields of epoll_data do not have any predefined meaning; your application assigns a meaning.

Any of the fields can be used to stored the information needed by your application to identify the event. (u32 or u64 might be useful for something like an array index.)

Slacken answered 8/7, 2014 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.