8000 Replicate EEXIST epoll_ctl behavior in kqueue · github/ruby@a2ebf9c · GitHub
[go: up one dir, main page]

Skip to content

Commit a2ebf9c

Browse files
jpcamarako1
authored andcommitted
Replicate EEXIST epoll_ctl behavior in kqueue
* In the epoll implementation, you get an EEXIST if you try to register the same event for the same fd more than once for a particular epoll instance * Otherwise kevent will just override the previous event registration, and if multiple threads listen on the same fd only the last one to register will ever finish, the others are stuck * This approach will lead to native threads getting created, similar to the epoll implementation. This is not ideal, but it fixes certain test cases for now, like test/socket/test_tcp.rb#test_accept_multithread
1 parent fadda88 commit a2ebf9c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

thread_pthread_mn.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,21 @@ kqueue_unregister_waiting(int fd, enum thread_sched_waiting_flag flags)
616616
}
617617
}
618618

619+
static bool
620+
kqueue_already_registered(int fd)
621+
{
622+
rb_thread_t *wth, *found_wth = NULL;
623+
ccan_list_for_each(&timer_th.waiting, wth, sched.waiting_reason.node) {
624+
// Similar to EEXIST in epoll_ctl, but more strict because it checks fd rather than flags
625+
// for simplicity
626+
if (wth->sched.waiting_reason.flags && wth->sched.waiting_reason.data.fd == fd) {
627+
found_wth = wth;
628+
break;
629+
}
630+
}
631+
return found_wth != NULL;
632+
}
633+
619634
#endif // HAVE_SYS_EVENT_H
620635

621636
// return false if the fd is not waitable or not need to wait.
@@ -645,6 +660,10 @@ timer_thread_register_waiting(rb_thread_t *th, int fd, enum thread_sched_waiting
645660
#if HAVE_SYS_EVENT_H
646661
struct kevent ke[2];
647662
int num_events = 0;
663+
664+
if (kqueue_already_registered(fd)) {
665+
return false;
666+
}
648667
#else
649668
uint32_t epoll_events = 0;
650669
#endif

0 commit comments

Comments
 (0)
0