2009-09-15 12:59:06

by adi hodos

[permalink] [raw]
Subject: Suggested addition to epoll interface - epoll_post_notification()

Hello everyone. Thank you for your time in reading this.
I have been playing with epoll lately and I would like to make a suggestion
regarding the epoll interface.
It would be nice to have a function to wake up a thread/process that is blocked
on an epoll_wait() call and sent it some
user defined data in the form of a struct epoll_event parameter ( something
similar with PostQueuedCompletionStatus() from Windows ).
My suggestion is for a function like this : int epoll_post_notification( int
epoll_descriptor , struct epoll_event* event );
It could be of much help in scenarios like these :

int e_accept;
int e_clients;

typedef enum {
keycode_client_connection ,
keycode_signal ,
keycode_socketio ,
keycode_fileio ,
keycode_event ,
keycode_timer
....
} event_key_type;

struct event_key {
event_key_type keycode;
};

struct eventfd_wrapper {
struct eventkey ek;
int fd_event;
};

struct signalfd_wrapper {
struct eventkey ek;
int fd_signal;
};

struct client {
struct event_key ek;
int sock;
....
};

struct user_posted_event {
struct event_key ek;
union {
void* ptr_data;
int i_data;
uint32_t u32;
uint32_t u64;
} u;
};


// thread A - waits for client connections using an epoll interface
for ( ; ; ) {
epoll_wait( e_accept , ... );

int s = accept();
struct client* new_client = allocate_newclient();
new_client->sock = s;
struct user_posted_event* ue = allocate_newuserevent();
ue->ek.keycode = keycode_client_connection;
ue->u.ptr_data = new_client;

struct epoll_event e;
e.data.ptr = ue;
epoll_post_notification( e_clients , &e );
}

// thread b - serves clients
for( ; ; ) {
struct epoll_event[..] e;
epoll_wait( e_clients , e , ... );

struct event_key* key = ( struct event_key* )( e.data.ptr );
if( key->keycode == keycode_client_connection ) {
// handle new client connection
} else if( key->keycode == keycode_signal ) {
// handle signal
} else if( .... ) {
...
}

Again , thank you for your time in reviewing this.


2009-09-15 18:16:51

by Roland Dreier

[permalink] [raw]
Subject: Re: Suggested addition to epoll interface - epoll_post_notification()


> I have been playing with epoll lately and I would like to make a suggestion
> regarding the epoll interface.
> It would be nice to have a function to wake up a thread/process that is blocked
> on an epoll_wait() call and sent it some
> user defined data in the form of a struct epoll_event parameter ( something
> similar with PostQueuedCompletionStatus() from Windows ).

You might have a look at eventfd() which creates an fd that can be used
to signal from one thread to another (and which can be waited on with epoll).

- R.

2009-09-15 18:48:12

by Davide Libenzi

[permalink] [raw]
Subject: Re: Suggested addition to epoll interface - epoll_post_notification()

On Tue, 15 Sep 2009, adi hodos wrote:

> Hello everyone. Thank you for your time in reading this.
> I have been playing with epoll lately and I would like to make a suggestion
> regarding the epoll interface.
> It would be nice to have a function to wake up a thread/process that is blocked
> on an epoll_wait() call and sent it some
> user defined data in the form of a struct epoll_event parameter ( something
> similar with PostQueuedCompletionStatus() from Windows ).
> My suggestion is for a function like this : int epoll_post_notification( int
> epoll_descriptor , struct epoll_event* event );
> It could be of much help in scenarios like these :
>
> int e_accept;
> int e_clients;
>
> typedef enum {
> keycode_client_connection ,
> keycode_signal ,
> keycode_socketio ,
> keycode_fileio ,
> keycode_event ,
> keycode_timer
> ....
> } event_key_type;
>
> struct event_key {
> event_key_type keycode;
> };
>
> struct eventfd_wrapper {
> struct eventkey ek;
> int fd_event;
> };
>
> struct signalfd_wrapper {
> struct eventkey ek;
> int fd_signal;
> };
>
> struct client {
> struct event_key ek;
> int sock;
> ....
> };
>
> struct user_posted_event {
> struct event_key ek;
> union {
> void* ptr_data;
> int i_data;
> uint32_t u32;
> uint32_t u64;
> } u;
> };
>
>
> // thread A - waits for client connections using an epoll interface
> for ( ; ; ) {
> epoll_wait( e_accept , ... );
>
> int s = accept();
> struct client* new_client = allocate_newclient();
> new_client->sock = s;
> struct user_posted_event* ue = allocate_newuserevent();
> ue->ek.keycode = keycode_client_connection;
> ue->u.ptr_data = new_client;
>
> struct epoll_event e;
> e.data.ptr = ue;
> epoll_post_notification( e_clients , &e );
> }
>
> // thread b - serves clients
> for( ; ; ) {
> struct epoll_event[..] e;
> epoll_wait( e_clients , e , ... );
>
> struct event_key* key = ( struct event_key* )( e.data.ptr );
> if( key->keycode == keycode_client_connection ) {
> // handle new client connection
> } else if( key->keycode == keycode_signal ) {
> // handle signal
> } else if( .... ) {
> ...
> }
>
> Again , thank you for your time in reviewing this.

Just use a pipe(2) to send data from A to B.
The read-side of the pipe will be in thread B epoll set, thread A would
prepare and write(2) a "message" (binary struct really), and thread B
would be wake up and read(2) the message.
no need to add anything.
Do you really have a dedicated thread for accepts though?


- Davide