I don't understand how the EVLOOP_NO_EXIT_ON_EMPTY flag is supposed to work in version 2.1.x of libevent.
If I don't add any events to my event_base the
event_base_loop(my_base, EVLOOP_NO_EXIT_ON_EMPTY);
call returns immediately which is not at all what I think it's supposed to do.
If I add an event it loops with that pending event until it get's active but then the loop exits which I hoped would not happen.
Goal:
Have a named pipe open and libevent listening for a read. Whenever I
echo "something" > pipe
the registered callback should be called. If the callback has finished the event get's back to pending and the loop waits for another echo.
Here's what I got so far: (error checking omitted)
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <event.h>
#include <unistd.h>
#define PIPE "/tmp/ev_pipe"
void cb_func(evutil_socket_t fd, short what, void *arg)
{
printf("foo\n");
}
int main(void)
{
/* create & open named pipe */
mkfifo(PIPE, 0666);
int socket = open(PIPE, O_RDONLY | O_NONBLOCK);
/* libevent specific stuff */
struct event_base *ev_base = event_base_new();
struct event *ev = event_new(ev_base, (evutil_socket_t) socket, EV_READ, cb_func, NULL);
event_add(ev, NULL);
/* loop forever */
event_base_loop(ev_base, EVLOOP_NO_EXIT_ON_EMPTY);
printf("a\n");
/* clean up */
unlink(PIPE);
event_base_free(ev_base);
close(socket);
return 0;
}
What am I missing? The event loop exits after the first write to the queue :/
Thanks for any help!