Unable to make eventfd work with epoll
I’m writing a simple server class based on epoll. To wake up epoll_wait(),
I decided to use an eventfd. It is said that it is better suited for simple event communication, and I agree with that. So I created my event and put a watch :
_epollfd = epoll_create1(0);
if (_epollfd == -1) throw ServerError("epoll_create");
_eventfd = eventfd(0, EFD_NONBLOCK);
epoll_event evnt = {0};
evnt.data.fd = _eventfd;
evnt.events = _events;
if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &evnt) == -1)
throw ServerError("epoll_ctl(add)");
Later in the message wait loop, on a separate thread:
int count = epoll_wait(_epollfd, evnts, EVENTS, -1);
if (count == -1)
{
if (errno != EINTR)
{
perror("epoll_wait");
return;
}
}
for (int i = 0; i < count; ++i)
{
epoll_event & e = evnts[i];
if (e.data.fd == _serverSock)
connectionAccepted();
else if (e.data.fd == _eventfd)
{
eventfd_t val;
eventfd_read(_eventfd, &val);
return;
}
}
Of course, the code to stop the server is:
eventfd_write(_eventfd, 1);
For reasons I can’t
explain, I can’t wake up epoll_wait()
by writing events. Eventually, this worked in a few debugging sessions.
HERE’S MY WORKAROUND: KNOWING THAT EPOLLOUT
FIRES AN EVENT EVERY TIME FD IS AVAILABLE FOR WRITING, I CHANGE THE STOP CODE TO
epoll_event evnt = {0};
evnt.data.fd = _eventfd;
evnt.events = EPOLLOUT;
if (epoll_ctl(_epollfd, EPOLL_CTL_MOD, _eventfd, &evnt) == -1)
throw ServerError("epoll_ctl(mod)");
It works now, but it shouldn’t be.
I don’t think there should be any difficulty with that. What am I doing wrong?
Thanks
Solution
Suits me. As a reference, here is the complete C code: it prints “eventfd_write”, “1”, and “DING: 1”. Tested on Linux 2.6.35-30-generic#56-Ubuntu SMP.
#include <stdio.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <pthread.h>
#include <stdlib.h>
int _epollfd, _eventfd;
int init()
{
_epollfd = epoll_create1(0);
if (_epollfd == -1) abort();
_eventfd = eventfd(0, EFD_NONBLOCK);
struct epoll_event evnt = {0};
evnt.data.fd = _eventfd;
evnt.events = EPOLLIN | EPOLLET;
if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &evnt) == -1)
abort();
}
void *subprocess(void *arg)
{
static const int EVENTS = 20;
struct epoll_event evnts[EVENTS];
while (1) {
int count = epoll_wait(_epollfd, evnts, EVENTS, -1);
printf("%d\n", count);
if (count == -1)
{
if (errno != EINTR)
{
perror("epoll_wait");
return NULL;
}
}
int i;
for (i = 0; i < count; ++i)
{
struct epoll_event *e = evnts + i;
if (e->data.fd == _eventfd)
{
eventfd_t val;
eventfd_read(_eventfd, &val);
printf("DING: %lld\n", (long long)val);
return NULL;
}
}
}
}
int main()
{
pthread_t th;
init();
if (pthread_create(&th, NULL, subprocess, NULL) != 0)
abort();
sleep(2);
printf("eventfd_write\n");
eventfd_write(_eventfd, 1);
sleep(2);
}