How to pass a callback function pointer to epoll_event structure in C++

Asked
Active3 hr before
Viewed126 times

4 Answers

eventfunction
90%

I have been searching an answer for this question and I came across the functions timerfd_create and epollin Linux. In a tutorial it was said that epoll_ctl() has an epoll_data_t union member of the epoll_event structure which can be used to execute a callback function on a timerfd event firing. But I am not sure on how to do it. Can anyone please help.,You can't put a callback function pointer into epoll_event because it can't fit in any of these slots:, I want to create a timer which on timeout should execute a function. I thought of executing this function as a callback function. – Jackzz Feb 11 '15 at 4:42 , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

You can't put a callback function pointer into epoll_event because it can't fit in any of these slots:

typedef union epoll_data {
   void * ptr;
   int fd;
   uint32_t u32;
   uint64_t u64;
}
epoll_data_t;
load more v
88%

great explanation. i had been struggling reading a piece of code I had with callbacks in C. This tutorial cleaned my thought and made life much easier. Thanks!,please give one more example for calling a callback function asynchronously, return &a; // we are returning the address of a(which is an integer), chrisy December 7, 2012 At 10:22 PM great explanation. i had been struggling reading a piece of code I had with callbacks in C. This tutorial cleaned my thought and made life much easier. Thanks! Reply

But can we declare a non-constant pointer to a function? Yes, we can — just like we declare a non-constant pointer to a variable:

int( * ptrFunc)();
load more v
72%

_ 1. fd: Each poll FD structure specifies a monitored file descriptor that can pass multiple structures to indicate that poll() monitors multiple file descriptors.,Wtefds: A collection of writable descriptors monitored.,fd: File descriptors that need to be monitored,readfd: A collection of monitored readable descriptors that are stored as long as the file descriptor reading operation is ready.

#include <sys /select.h>
load more v
65%

ep = f.file->private_data; obtain event_poll object ;, Distribute eventpoll Structure and initialization operation ;,eventpoll Structure :, The last call is added fd Of fileoperation->poll function ( In the end, it will call poll_wait operation ) Used to complete the registration operation , take epitem Add structure to red black tree .

eventpoll Structure :

/*
 *  This structure is stored in file->private_data in 
 */
/*
    eventpoll The structure is epoll There is a lot of information stored in the core of , It mainly includes 
    1. struct rb_root rbr; This is the root node of a red black tree , It represents a red and black tree ,
     Hanging under the red black tree is what we are interested in socket Events , When we call epoll_ctl towards 
    epoll Add interesting socket When an event is , The system encapsulates our information into 
    struct epitem Structure , Then hang it on the corresponding node of the red black tree 
    2.struct list_head rdllist; This is a two-way list , This two-way linked list stores 
     Is the ready event when we call epoll_wait These events will be returned to the user 
    3.struct file *file; File structure pointer , Point to epoll file 
    */
struct eventpoll {
   //  spinlocks , stay kernel The inside is locked with a spin lock , You can have multiple lines at the same time ( Into the ) Program operates on this structure 
   //  Mainly to protect ready_list
   spinlock_t lock;
   //  This mutex is to ensure that the eventloop When using the corresponding file descriptor , The file descriptor will not be removed 
   struct mutex mtx;
   // epoll_wait Waiting queue used , It's about process wakening 
   wait_queue_head_t wq;
   // file->poll Waiting queue used , It's about process wakening 
   wait_queue_head_t poll_wait;
   //  Ready descriptor queue , Double linked list 
   struct list_head rdllist;
   //  Organize the current through red and black trees epoll File descriptors of interest 
   struct rb_root rbr;
   //  When transmitting ready events to user space , Link the file descriptors of simultaneous events into this list 
   struct epitem * ovflist;
   //  Corresponding user
   struct user_struct * user;
   //  The corresponding file descriptor 
   struct file * file;
   //  The next two are optimizations for loop detection 
   int visited;
   struct list_head visited_list_link;
};
load more v

Other "event-function" queries related to "How to pass a callback function pointer to epoll_event structure in C++"