Interrupt epoll_wait with a non-IO event, no signals

The current scenario is epoll_wait through a couple of fds and a queue of possible incoming messages, I would like the loop below epoll_wait to be executed in an I / O event or in a new message.
The ways I know:

  • Use time msec timeout and check the first queue in the loop
  • Use the do-it -yourself trick from the queue code when messages appear
  • Abort a system call with a standard signal
  • Use epoll_pwait and refine the previous point.

None of the above questions satisfy me, and I was wondering if there are other methods that I did not find. Causes:

  • Signals avoid multi-threaded code and are not very reliable
  • Timeout one removes part of the epoll advantage, only waking up with events
  • The do-it-yourself pipe trick looks best at the moment, but there are still too many templates

ideas?

+7
linux asynchronous io networking
source share
2 answers

You can use eventfd , which is actually the same as a trick with your own pipe, except for fewer file descriptors and fewer templates (glibc has convenient eventfd_read/write functions, for example).

+17
source share

You have listed events that may wake up in the epic, so the question really becomes: "How to reduce the template for a stunt with your own pipe?"

The answer to this question really depends on your code, language, and what you are trying to do. I assume that you have a stream that handles I / O, and you want to do other work on that stream until there is no I / O. In the code that controls the epoll loop, it can have an internal descriptor that is exposed to other parts of the system, like the wake-up function or the send job function.

There are libraries that do this, such as boost.asio for C ++. However, it’s easy to write your own if you are just targeting epoll, and the amount of actual template code should be minimal if you have a class / module / anything related to the epoll loop.

+1
source share

All Articles