Skip to content

Commit

Permalink
Prefer one-shot semantics for kqueue.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 22, 2023
1 parent f673a30 commit d2699af
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
2 changes: 1 addition & 1 deletion ext/io/event/selector/epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ void IO_Event_Selector_EPoll_Descriptor_initialize(void *element)
{
struct IO_Event_Selector_EPoll_Descriptor *epoll_descriptor = element;
IO_Event_List_initialize(&epoll_descriptor->list);
epoll_descriptor->io = 0;
epoll_descriptor->waiting_events = 0;
epoll_descriptor->registered_events = 0;
epoll_descriptor->io = 0;
}

void IO_Event_Selector_EPoll_Descriptor_free(void *element)
Expand Down
33 changes: 5 additions & 28 deletions ext/io/event/selector/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,62 +214,37 @@ enum IO_Event events_from_kevent_filter(int filter)
inline static
int IO_Event_Selector_KQueue_Descriptor_update(struct IO_Event_Selector_KQueue *selector, uintptr_t identifier, struct IO_Event_Selector_KQueue_Descriptor *kqueue_descriptor)
{
// if (kqueue_descriptor->registered_events == kqueue_descriptor->waiting_events) {
// // All the events we are interested in are already registered.
// return 0;
// }

int count = 0;
struct kevent kevents[3] = {0};

if (kqueue_descriptor->waiting_events & IO_EVENT_READABLE) {
kevents[count].ident = identifier;
kevents[count].filter = EVFILT_READ;
kevents[count].flags = EV_ADD | EV_ENABLE;
kevents[count].flags = EV_ADD | EV_ONESHOT;
kevents[count].udata = (void *)kqueue_descriptor;

// #ifdef EV_OOBAND
// if (events & IO_EVENT_PRIORITY) {
// kevents[count].flags |= EV_OOBAND;
// }
// #endif

count++;
} else if (kqueue_descriptor->registered_events & IO_EVENT_READABLE) {
kevents[count].ident = identifier;
kevents[count].filter = EVFILT_READ;
kevents[count].flags = EV_DELETE;
kevents[count].udata = (void *)kqueue_descriptor;
count++;
}

if (kqueue_descriptor->waiting_events & IO_EVENT_WRITABLE) {
kevents[count].ident = identifier;
kevents[count].filter = EVFILT_WRITE;
kevents[count].flags = EV_ADD | EV_ENABLE;
kevents[count].udata = (void *)kqueue_descriptor;
count++;
} else if (kqueue_descriptor->registered_events & IO_EVENT_WRITABLE) {
kevents[count].ident = identifier;
kevents[count].filter = EVFILT_WRITE;
kevents[count].flags = EV_DELETE;
kevents[count].flags = EV_ADD | EV_ONESHOT;
kevents[count].udata = (void *)kqueue_descriptor;
count++;
}

if (kqueue_descriptor->waiting_events & IO_EVENT_EXIT) {
kevents[count].ident = identifier;
kevents[count].filter = EVFILT_PROC;
kevents[count].flags = EV_ADD | EV_ENABLE | EV_ONESHOT;
kevents[count].flags = EV_ADD | EV_ONESHOT;
kevents[count].fflags = NOTE_EXIT;
kevents[count].udata = (void *)kqueue_descriptor;
count++;
} else if (kqueue_descriptor->registered_events & IO_EVENT_EXIT) {
kevents[count].ident = identifier;
kevents[count].filter = EVFILT_PROC;
kevents[count].flags = EV_DELETE;
kevents[count].udata = (void *)kqueue_descriptor;
count++;
}

if (count == 0) {
Expand Down Expand Up @@ -887,6 +862,8 @@ int IO_Event_Selector_KQueue_handle(struct IO_Event_Selector_KQueue *selector, u

if (ready_events) {
kqueue_descriptor->ready_events = 0;
// Since we use one-shot semantics, we need to re-arm the events that are ready if needed:
kqueue_descriptor->registered_events &= ~ready_events;
} else {
return 0;
}
Expand Down

0 comments on commit d2699af

Please sign in to comment.