diff options
author | Benoit Daloze <eregontp@gmail.com> | 2020-09-05 16:26:24 +1200 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2020-09-14 16:44:09 +1200 |
commit | 178c1b0922dc727897d81d7cfe9c97d5ffa97fd9 (patch) | |
tree | 113600e7e6a196b779bcac7529535597858f78a7 /scheduler.c | |
parent | 9e0a48c7a31ecd39be0596d0517b9d521ae75282 (diff) |
Make Mutex per-Fiber instead of per-Thread
* Enables Mutex to be used as synchronization between multiple Fibers
of the same Thread.
* With a Fiber scheduler we can yield to another Fiber on contended
Mutex#lock instead of blocking the entire thread.
* This also makes the behavior of Mutex consistent across CRuby, JRuby and TruffleRuby.
* [Feature #16792]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3434
Diffstat (limited to 'scheduler.c')
-rw-r--r-- | scheduler.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/scheduler.c b/scheduler.c index 9821d07636..9ecc40cf6c 100644 --- a/scheduler.c +++ b/scheduler.c @@ -12,6 +12,8 @@ #include "ruby/io.h" static ID id_kernel_sleep; +static ID id_mutex_lock; +static ID id_mutex_unlock; static ID id_io_read; static ID id_io_write; static ID id_io_wait; @@ -20,6 +22,8 @@ void Init_Scheduler(void) { id_kernel_sleep = rb_intern_const("kernel_sleep"); + id_mutex_lock = rb_intern_const("mutex_lock"); + id_mutex_unlock = rb_intern_const("mutex_unlock"); id_io_read = rb_intern_const("io_read"); id_io_write = rb_intern_const("io_write"); id_io_wait = rb_intern_const("io_wait"); @@ -44,6 +48,16 @@ VALUE rb_scheduler_kernel_sleepv(VALUE scheduler, int argc, VALUE * argv) return rb_funcallv(scheduler, id_kernel_sleep, argc, argv); } +VALUE rb_scheduler_mutex_lock(VALUE scheduler, VALUE mutex) +{ + return rb_funcall(scheduler, id_mutex_lock, 1, mutex); +} + +VALUE rb_scheduler_mutex_unlock(VALUE scheduler, VALUE mutex, VALUE fiber) +{ + return rb_funcall(scheduler, id_mutex_unlock, 2, mutex, fiber); +} + VALUE rb_scheduler_io_wait(VALUE scheduler, VALUE io, VALUE events, VALUE timeout) { return rb_funcall(scheduler, id_io_wait, 3, io, events, timeout); |