I have trouble to understand, how to use kqueue for user space events.
I look for 2 use cases.
Use case 1: Manual Reset event
Use case 2: Auto Reset event
I think I understand, how to use kqueue() and kevent(), yet I am unclear on how the events passed to kevent() look for the related operations:
Let there be a struct kevent variable named "event".
Let us assume, we have no problem finding a new event id which is not colliding with other event ids for that kqueue instance, named "eventId".
- Create user event: EV_SET(&event, eventId, EVFILT_USER, EV_ADD, NOTE_FFNOP, 0, NULL)
- Destroy user event: EV_SET(&event, eventId, EVFILT_USER, EV_DESTROY, NOTE_FFNOP, 0, NULL)
- Set the user event: EV_SET(&event, eventId, EVFILT_USER, ?????, NOTE_FFNOP, 0, NULL)
- Reset user event: EV_SET(&event, eventId, EVFILT_USER, ??EV_CLEAR???, NOTE_FFNOP, 0, NULL )
- Pulse user event: EV_SET(&event, eventId, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL )
- In the wait loop, I think the snipped would be along: if( event.filter == EVFILT_USER && event.ident == eventId ) { // this is my event! Do something! }
See the ???? in the above EV_SET() calls to see where I need help.
For use case 1 (Manual reset event), operation (1) Create might look different compared to use case 2 (auto reset event).
Operations (3) and (4) I am completely in the dark. Might I need EV_ENABLE/EV_DISABLE for those? Where is EV_CLEAR fitting in?
So far I assume that I do not need to do anything in operation (6) beyond the "dispatching".
I am pretty positive, that operation (5) could work as I gave it above.
I spent now the better of a day trying to find documentation or samples showing how it is done. I found in apple codebase a kqueue test program but I doubt, it is doing it right. Also, it only sends 1 event in the test and that event terminates the loop of the receiving thread. So it is not helping me to understand the details for my 2 use cases.
I plan to use it under FreeBsd 9.1 on a x86 machine...for now.