Search code examples
macossocketsfreebsdkqueue

triggering kevent by force


I'm using kqueue for socket synchronization in OS X. I can register an event of interest like the following:

struct kevent change;
EV_SET(&change, connected_socket, EVFILT_READ, EV_ADD, 0, NULL, NULL);
kevent(k_queue_, &change, 1, NULL, 0, NULL);

And the question is, is there a way to trigger this event by force so that the waiting kevent call would return?


Solution

  • Some possibilities aside from natural writing of data to the other side of the socket :)

    • shutdown(2) the read side of that socket - you'll get EV_EOF in flags (silly),
    • Use the timeout argument and call the same handling function,
    • Use the self-pipe trick when you need to break the wait.

    My question though: why do you need this?

    Edit:

    If I understand your comments correctly you are looking for a way to get around edge-triggered behavior (EV_CLEAR) for write events. I believe that the proper way of doing this is to un-register your socket from EVFILT_WRITE when you don't have anything in the outgoing queue, then re-register it again when there's data to send. It's a bit more work, but that's how it works, and you don't need any additional system calls since kevent(2) accepts both changes and results. Take a look into libevent and see how it handles this sort of stuff. And you are using non-blocking sockets, right?