Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significance of parameters in epoll_event structure (epoll)

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".

like image 391
SPSN Avatar asked Jul 08 '14 02:07

SPSN


2 Answers

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.

like image 106
Mudzot Avatar answered Oct 25 '22 19:10

Mudzot


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.)

like image 45
CL. Avatar answered Oct 25 '22 19:10

CL.