summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
authorJP Camara <jp@jpcamara.com>2023-12-15 22:39:45 -0500
committerKoichi Sasada <ko1@atdot.net>2023-12-20 16:23:38 +0900
commit256f34ab6b221b630e5e4ec8d39e3808e82a3296 (patch)
tree7f7960f59806e559c89c5f5108337403f5a10a86 /thread.c
parent8782e02138e6fe18b6c0dcc29bb877d6cdae57e5 (diff)
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
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c50
1 files changed, 21 insertions, 29 deletions
diff --git a/thread.c b/thread.c
index 7c53f958fe..1a857a12ca 100644
--- a/thread.c
+++ b/thread.c
@@ -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();