summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoichi Sasada <[email protected]>2024-02-20 19:09:23 +0900
committerKoichi Sasada <[email protected]>2024-02-21 15:38:29 +0900
commitd578684989fd2d75f086a259719e3eb0fe57ccb2 ()
tree182d720a6c0a63a7bcc1f6c48c276d082d074c05
parent91cb30353178004ba57acacf1c39d9c02beff86e (diff)
`rb_thread_lock_native_thread()`
Introduce `rb_thread_lock_native_thread()` to allocate dedicated native thread to the current Ruby thread for M:N threads. This C API is similar to Go's `runtime.LockOSThread()`. Accepted at https://.com/ruby/dev-meeting-log/blob/master/2023/DevMeeting-2023-08-24.md (and missed to implement on Ruby 3.3.0)
-rw-r--r--ext/-test-/thread/lock_native_thread/extconf.rb2
-rw-r--r--ext/-test-/thread/lock_native_thread/lock_native_thread.c49
-rw-r--r--include/ruby/thread.h13
-rw-r--r--test/-ext-/thread/test_lock_native_thread.rb50
-rw-r--r--thread_none.c6
-rw-r--r--thread_pthread.c17
-rw-r--r--thread_pthread_mn.c34
-rw-r--r--thread_win32.c6
8 files changed, 164 insertions, 13 deletions
@@ -0,0 +1,2 @@
@@ -0,0 +1,49 @@
@@ -191,6 +191,19 @@ void *rb_nogvl(void *(*func)(void *), void *data1,
#define RUBY_CALL_WO_GVL_FLAG_SKIP_CHECK_INTS_
/**
* Triggered when a new thread is started.
*
* @note The callback will be called *without* the GVL held.
@@ -0,0 +1,50 @@
@@ -318,4 +318,10 @@ rb_thread_sched_mark_zombies(rb_vm_t *vm)
// do nothing
}
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
@@ -2304,6 +2304,11 @@ nt_start(void *ptr)
// timeout -> deleted.
break;
}
}
}
@@ -3422,4 +3427,16 @@ rb_thread_execute_hooks(rb_event_flag_t event, rb_thread_t *th)
}
}
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
@@ -439,27 +439,35 @@ co_start(struct coroutine_context *from, struct coroutine_context *self)
// Thread is terminated
- VM_ASSERT(!th_has_dedicated_nt(th));
-
- rb_vm_t *vm = th->vm;
- bool has_ready_ractor = vm->ractor.sched.grq_cnt > 0; // at least this ractor is not queued
-
- rb_thread_t *next_th = sched->running;
struct rb_native_thread *nt = th->nt;
native_thread_assign(NULL, th);
rb_ractor_set_current_ec(th->ractor, NULL);
- if (!has_ready_ractor && next_th && !next_th->nt) {
- // switch to the next thread
- thread_sched_set_lock_owner(sched, NULL);
- thread_sched_switch0(th->sched.context, next_th, nt, true);
th->sched.finished = true;
}
else {
- // switch to the next Ractor
- th->sched.finished = true;
- coroutine_transfer0(self, nt->nt_context, true);
}
rb_bug("unreachable");
}
@@ -1003,4 +1003,10 @@ rb_ractor_sched_barrier_join(rb_vm_t *vm, rb_ractor_t *cr)
vm->ractor.sync.lock_owner = NULL;
}
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */