From 9c6cb4d1229d925ed8c805e9c0a55692edc71a89 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Thu, 12 Sep 2024 10:41:29 -0400 Subject: [PATCH] Expose ruby_thread_has_gvl_p in thread.h --- error.c | 2 +- ext/fiddle/closure.c | 2 -- gc/default.c | 2 -- include/ruby/thread.h | 14 ++++++++++++++ internal/thread.h | 3 --- thread.c | 5 ----- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/error.c b/error.c index ef646c55c7f996..b7dd34d34bc367 100644 --- a/error.c +++ b/error.c @@ -42,10 +42,10 @@ #include "internal/process.h" #include "internal/string.h" #include "internal/symbol.h" -#include "internal/thread.h" #include "internal/variable.h" #include "ruby/encoding.h" #include "ruby/st.h" +#include "ruby/thread.h" #include "ruby/util.h" #include "ruby_assert.h" #include "vm_core.h" diff --git a/ext/fiddle/closure.c b/ext/fiddle/closure.c index 7aa9407619c71b..11aae2e818f256 100644 --- a/ext/fiddle/closure.c +++ b/ext/fiddle/closure.c @@ -2,8 +2,6 @@ #include #include -int ruby_thread_has_gvl_p(void); /* from internal.h */ - VALUE cFiddleClosure; typedef struct { diff --git a/gc/default.c b/gc/default.c index 0c66dca8b47b49..dfd12107fe3a99 100644 --- a/gc/default.c +++ b/gc/default.c @@ -6782,8 +6782,6 @@ gc_with_gvl(void *ptr) return (void *)(VALUE)garbage_collect(oar->objspace, oar->reason); } -int ruby_thread_has_gvl_p(void); - static int garbage_collect_with_gvl(rb_objspace_t *objspace, unsigned int reason) { diff --git a/include/ruby/thread.h b/include/ruby/thread.h index 337f477fd0d52c..53a6f1d56523c2 100644 --- a/include/ruby/thread.h +++ b/include/ruby/thread.h @@ -63,6 +63,20 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() +/** + * Check if current native thread has GVL. + * @return 1 if the current thread has acquired the GVL. + * + * Usage: + * if (ruby_thread_has_gvl_p()) { + * method_call(&context); + * } + * else { + * rb_thread_call_with_gvl(method_call, &context); + * } + */ +int ruby_thread_has_gvl_p(void); + RBIMPL_ATTR_NONNULL((1)) /** * (Re-)acquires the GVL. This manoeuvre makes it possible for an out-of-GVL diff --git a/internal/thread.h b/internal/thread.h index a2926febc3c20b..d22f24c45bee8a 100644 --- a/internal/thread.h +++ b/internal/thread.h @@ -73,9 +73,6 @@ void *rb_thread_prevent_fork(void *(*func)(void *), void *data); /* for ext/sock VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd); VALUE rb_thread_io_blocking_call(rb_blocking_function_t *func, void *data1, int fd, int events); -/* thread.c (export) */ -int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */ - RUBY_SYMBOL_EXPORT_END int rb_threadptr_execute_interrupts(struct rb_thread_struct *th, int blocking_timing); diff --git a/thread.c b/thread.c index 5a792007859096..6b9e81b896fe0e 100644 --- a/thread.c +++ b/thread.c @@ -1907,11 +1907,6 @@ rb_thread_call_with_gvl(void *(*func)(void *), void *data1) /* * ruby_thread_has_gvl_p - check if current native thread has GVL. - * - *** - *** This API is EXPERIMENTAL! - *** We do not guarantee that this API remains in ruby 1.9.2 or later. - *** */ int