Is it possible to use epoll
in one-shot level-triggered mode?
I couldn't find any information on it when I searched; it seems everyone uses edge-triggered mode.
When the EPOLLONESHOT
flag is selected and you have pulled an event for a socket, then the socket won't get removed from epoll as many think but its events get disabled. You can enable them again using epoll_ctl
/ EPOLL_CTL_MOD
.
An example case when the EPOLLONESHOT
behavior comes handy is when you've read the available data from a socket into a buffer. That buffer would be emptied independently, but until it isn't empty, you have to disable the socket events, even if the socket has additional data. Then after the buffer got used and emptied, you can re-enable the socket.
The difference between the edge- and level-triggered "one shot" behaviors come only when you re-enable the socket. An example:
epoll_ctl
/ EPOLL_CTL_MOD
.Level-triggered EPOLLONESHOT:
Edge-triggered EPOLLONESHOT:
EAGAIN
/ EWOULDBLOCK
.If you want epoll to stop listening on a socket the you should use EPOLLONESHOT. If you do use EPOLLONESHOT then you will have to add the socket back to epoll after epoll signals on that socket. Thus EPOLLONESHOT is not EPOLLET. You can use EPOLLONESHOT without EPOLLET, but it might not be efficient. If you use both the flags, then you will have to make use of non blocking sockets and add a socket to epoll only once recv and send return with an EAGAIN error. Please refer to man page for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With