Hand thread into thread_sched_wait_events_timeval

* When we have the thread already, it saves a lookup

* `event_wait`, not `kq`

Clean up the `thread_sched_wait_events_timeval` calls

* By handling the PTHREAD check inside the function, all the other code can become much simpler and just call the function directly without additional checks
This commit is contained in:
JP Camara 2023-12-15 22:39:45 -05:00 committed by Koichi Sasada
parent 8782e02138
commit 256f34ab6b
2 changed files with 23 additions and 31 deletions

View File

@ -4265,27 +4265,27 @@ rb_thread_fd_select(int max, rb_fdset_t * read, rb_fdset_t * write, rb_fdset_t *
return (int)rb_ensure(do_select, (VALUE)&set, select_set_free, (VALUE)&set);
}
#ifdef RUBY_THREAD_PTHREAD_H
static bool
thread_sched_wait_events_timeval(int fd, int events, struct timeval *timeout)
thread_sched_wait_events_timeval(rb_thread_t *th, int fd, int events, struct timeval *timeout)
{
rb_thread_t *th = GET_THREAD();
rb_hrtime_t rel, *prel;
#ifdef RUBY_THREAD_PTHREAD_H
if (!th->nt->dedicated) {
rb_hrtime_t rel, *prel;
if (timeout) {
rel = rb_timeval2hrtime(timeout);
prel = &rel;
}
else {
prel = NULL;
}
if (timeout) {
rel = rb_timeval2hrtime(timeout);
prel = &rel;
}
else {
prel = NULL;
}
return thread_sched_wait_events(TH_SCHED(th), th, fd, waitfd_to_waiting_flag(events), prel);
return thread_sched_wait_events(TH_SCHED(th), th, fd, waitfd_to_waiting_flag(events), prel);
}
#endif // RUBY_THREAD_PTHREAD_H
return 0;
}
#endif
#ifdef USE_POLL
/* The same with linux kernel. TODO: make platform independent definition. */
@ -4314,13 +4314,9 @@ rb_thread_wait_for_single_fd(int fd, int events, struct timeval *timeout)
wfd.fd = fd;
wfd.busy = NULL;
#ifdef RUBY_THREAD_PTHREAD_H
if (!th_has_dedicated_nt(th)) {
if (thread_sched_wait_events_timeval(fd, events, timeout)) {
return 0; // timeout
}
if (thread_sched_wait_events_timeval(th, fd, events, timeout)) {
return 0; // timeout
}
#endif
RB_VM_LOCK_ENTER();
{
@ -4455,15 +4451,11 @@ rb_thread_wait_for_single_fd(int fd, int events, struct timeval *timeout)
struct select_args args;
int r;
VALUE ptr = (VALUE)&args;
#ifdef RUBY_THREAD_PTHREAD_H
rb_thread_t *th = GET_THREAD();
if (!th_has_dedicated_nt(th)) {
if (thread_sched_wait_events_timeval(fd, events, timeout)) {
return 0; // timeout
}
if (thread_sched_wait_events_timeval(th, fd, events, timeout)) {
return 0; // timeout
}
#endif
args.as.fd = fd;
args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
@ -4471,7 +4463,7 @@ rb_thread_wait_for_single_fd(int fd, int events, struct timeval *timeout)
args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
args.tv = timeout;
args.wfd.fd = fd;
args.wfd.th = GET_THREAD();
args.wfd.th = th;
args.wfd.busy = NULL;
RB_VM_LOCK_ENTER();

View File

@ -883,8 +883,8 @@ timer_thread_polling(rb_vm_t *vm)
// simply retry
break;
default:
perror("kq");
rb_bug("kq errno:%d", errno);
perror("event_wait");
rb_bug("event_wait errno:%d", errno);
}
break;