I'm writing a coroutine wrapper for sockets as a demo for coroutine use cases and I'm struggling a bit with how to use epoll safely (without introducing race conditions).
I have already figured out that I have to use the edge mode EPOLLET with EPOLLONESHOT. But now I'm not sure when I should be rearming the socket.
Should I rearm before calling a non-blocking operation or after? I want to make sure that I neither miss the event nor receive a phantom one.
// epoll & socket setup
int ret;
ret = epoll_ctl(epoll_, EPOLL_CTL_MOD, ...); // rearm here
//...
ret = read(...);
//...
ret = epoll_ctl(epoll_, EPOL_CTL_MOD, ...); // or here?
int ret = epoll_wait(...);