Skip to content

Commit 2b05785

Browse files
kbrockeregon
andcommitted
Allow rb_thread_call_with_gvl() to work when thread already has GVL
[Feature #20750] Co-authored-by: Benoit Daloze <eregontp@gmail.com>
1 parent 8c4f79d commit 2b05785

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ The following bundled gems are updated.
342342
`IO` objects share the same file descriptor, closing one does not affect
343343
the other. [[Feature #18455]]
344344
345+
* GVL
346+
347+
* `rb_thread_call_with_gvl` now works with or without the GVL.
348+
This allows gems to avoid checking `ruby_thread_has_gvl_p`.
349+
Please still be diligent about the GVL. [[Feature #20750]]
350+
345351
* Set
346352
347353
* A C API for `Set` has been added. The following methods are supported:
@@ -402,6 +408,7 @@ A lot of work has gone into making Ractors more stable, performant, and usable.
402408
[Feature #19908]: https://bugs.ruby-lang.org/issues/19908
403409
[Feature #20610]: https://bugs.ruby-lang.org/issues/20610
404410
[Feature #20724]: https://bugs.ruby-lang.org/issues/20724
411+
[Feature #20750]: https://bugs.ruby-lang.org/issues/20750
405412
[Feature #20884]: https://bugs.ruby-lang.org/issues/20884
406413
[Feature #20925]: https://bugs.ruby-lang.org/issues/20925
407414
[Feature #20971]: https://bugs.ruby-lang.org/issues/20971

gc.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5063,11 +5063,7 @@ gc_raise(VALUE exc, const char *fmt, ...)
50635063
exc, fmt, &ap,
50645064
};
50655065

5066-
if (ruby_thread_has_gvl_p()) {
5067-
gc_vraise(&argv);
5068-
UNREACHABLE;
5069-
}
5070-
else if (ruby_native_thread_p()) {
5066+
if (ruby_native_thread_p()) {
50715067
rb_thread_call_with_gvl(gc_vraise, &argv);
50725068
UNREACHABLE;
50735069
}

thread.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,9 @@ rb_thread_io_blocking_region(struct rb_io *io, rb_blocking_function_t *func, voi
20422042
* created as Ruby thread (created by Thread.new or so). In other
20432043
* words, this function *DOES NOT* associate or convert a NON-Ruby
20442044
* thread to a Ruby thread.
2045+
*
2046+
* NOTE: If this thread has already acquired the GVL, then the method call
2047+
* is performed without acquiring or releasing the GVL (from Ruby 4.0).
20452048
*/
20462049
void *
20472050
rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
@@ -2065,7 +2068,8 @@ rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
20652068
prev_unblock = th->unblock;
20662069

20672070
if (brb == 0) {
2068-
rb_bug("rb_thread_call_with_gvl: called by a thread which has GVL.");
2071+
/* the GVL is already acquired, call method directly */
2072+
return (*func)(data1);
20692073
}
20702074

20712075
blocking_region_end(th, brb);

0 commit comments

Comments
 (0)