[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] epoll_wait-like timeout with libevent (tick generation)




On Oct 16, 2012, at 1:35 PM, Programmist Setevik wrote:

Portability issues aside, is there a way to ask event_base_* functions
to enforce epoll_wait()-like timeout ?

I need to have an ability to generate "ticks" - say once every 100
msec or thereabouts and run some callbacks whenever ticks occur.

I understand libevent offers its own way to generate these, but is
there a way to ask libevent to pass the TO value to epoll_wait ?

The legacy code I have, does something like this (pseudo):


##################################################################

add FDs of interest to the epoll_fd

epoll_wait (...., 100 ); // Wait for events, but no longer than for 100 msec

if (IO events reported)
 process events;

if (time_now > time_prev_run + 100)
 // more than 100 ms passed since the last run,
time_prev_rum = time_now
run_100ms_tick_logic()

This will do exactly what you want:

struct event *tick_event;

void tick(int fd, short events, void *arg)
{
	// More then 100 msec passed
}

int main()
{
	...

	struct timeval timeout = {0, 100*1000};

	tick_event = event_new(event_base, -1, EVPERSIST, tick, NULL);
	event_add(tick_event, &timeout);

	...
}


Then the event loop does something like this:

add your timeout event to a timer heap, sorted by the next deadline.

while there are events:
    get next deadline from timers heap
    wait for events until next deadline (in your case, about 100 msec)
process events (in your case there are no events to process, only the timer)
    process timers (here your tick will run if its deadline has passed)


***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.