Skip to content

Commit 4e970c5

Browse files
committed
Expose ruby_thread_has_gvl_p.
1 parent 22667fc commit 4e970c5

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

include/ruby/thread.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ void *rb_internal_thread_specific_get(VALUE thread_val, rb_internal_thread_speci
333333
*/
334334
void rb_internal_thread_specific_set(VALUE thread_val, rb_internal_thread_specific_key_t key, void *data);
335335

336+
/**
337+
* Whether the current thread is holding the GVL.
338+
*
339+
* @return true if the current thread is holding the GVL, false otherwise.
340+
*/
341+
int ruby_thread_has_gvl_p(void);
342+
336343
RBIMPL_SYMBOL_EXPORT_END()
337344

338345
#endif /* RUBY_THREAD_H */

spec/ruby/optional/capi/ext/thread_spec.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ static VALUE thread_spec_rb_thread_wait_for(VALUE self, VALUE s, VALUE ms) {
118118
return Qnil;
119119
}
120120

121-
122121
VALUE thread_spec_call_proc(void *arg_ptr) {
123122
VALUE arg_array = (VALUE)arg_ptr;
124123
VALUE arg = rb_ary_pop(arg_array);
@@ -167,6 +166,12 @@ static VALUE thread_spec_ruby_native_thread_p_new_thread(VALUE self) {
167166
#endif
168167
}
169168

169+
#ifdef RUBY_VERSION_IS_3_5
170+
static VALUE thread_spec_ruby_thread_has_gvl_p(VALUE self) {
171+
return ruby_thread_has_gvl_p() ? Qtrue : Qfalse;
172+
}
173+
#endif
174+
170175
void Init_thread_spec(void) {
171176
VALUE cls = rb_define_class("CApiThreadSpecs", rb_cObject);
172177
rb_define_method(cls, "rb_thread_alone", thread_spec_rb_thread_alone, 0);
@@ -180,6 +185,9 @@ void Init_thread_spec(void) {
180185
rb_define_method(cls, "rb_thread_create", thread_spec_rb_thread_create, 2);
181186
rb_define_method(cls, "ruby_native_thread_p", thread_spec_ruby_native_thread_p, 0);
182187
rb_define_method(cls, "ruby_native_thread_p_new_thread", thread_spec_ruby_native_thread_p_new_thread, 0);
188+
#ifdef RUBY_VERSION_IS_3_5
189+
rb_define_method(cls, "ruby_thread_has_gvl_p", thread_spec_ruby_thread_has_gvl_p, 0);
190+
#endif
183191
}
184192

185193
#ifdef __cplusplus

spec/ruby/optional/capi/thread_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,12 @@ def call_capi_rb_thread_wakeup
184184
thr.value.should be_true
185185
end
186186
end
187+
188+
ruby_version_is "3.5" do
189+
describe "ruby_thread_has_gvl_p" do
190+
it "returns true if the current thread has the GVL" do
191+
@t.ruby_thread_has_gvl_p.should be_true
192+
end
193+
end
194+
end
187195
end

thread.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,11 +1937,6 @@ rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
19371937

19381938
/*
19391939
* ruby_thread_has_gvl_p - check if current native thread has GVL.
1940-
*
1941-
***
1942-
*** This API is EXPERIMENTAL!
1943-
*** We do not guarantee that this API remains in ruby 1.9.2 or later.
1944-
***
19451940
*/
19461941

19471942
int

0 commit comments

Comments
 (0)