Skip to content

Commit

Permalink
Hand thread into thread_sched_wait_events_timeval
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jpcamara committed Dec 17, 2023
1 parent a3e1135 commit b71c0cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
50 changes: 21 additions & 29 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -4254,27 +4254,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. */
Expand Down Expand Up @@ -4303,13 +4303,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();
{
Expand Down Expand Up @@ -4444,23 +4440,19 @@ 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;
args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
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();
Expand Down
4 changes: 2 additions & 2 deletions thread_pthread_mn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit b71c0cd

Please sign in to comment.