diff options
author | Benoit Daloze <[email protected]> | 2021-04-27 18:42:50 +0200 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2021-04-27 18:42:50 +0200 |
commit | 3a3b19b2bba49e5d6f1cf13764eb6dd701397be9 (patch) | |
tree | da5bb4f720bfe4254b30050ed3dbb74af60457a0 | |
parent | 1c1c91535c4f8ebc2d1ccfb63076632d53604401 (diff) |
Fix Monitor to lock per Fiber, like Mutex [Bug #17827]
-rw-r--r-- | ext/monitor/monitor.c | 10 | ||||
-rw-r--r-- | test/monitor/test_monitor.rb | 7 |
2 files changed, 12 insertions, 5 deletions
diff --git a/ext/monitor/monitor.c b/ext/monitor/monitor.c index 26a564f4c3..43a18f58af 100644 --- a/ext/monitor/monitor.c +++ b/ext/monitor/monitor.c @@ -53,7 +53,7 @@ monitor_ptr(VALUE monitor) static int mc_owner_p(struct rb_monitor *mc) { - return mc->owner == rb_thread_current(); + return mc->owner == rb_fiber_current(); } static VALUE @@ -65,7 +65,7 @@ monitor_try_enter(VALUE monitor) if (!rb_mutex_trylock(mc->mutex)) { return Qfalse; } - RB_OBJ_WRITE(monitor, &mc->owner, rb_thread_current()); + RB_OBJ_WRITE(monitor, &mc->owner, rb_fiber_current()); mc->count = 0; } mc->count += 1; @@ -78,7 +78,7 @@ monitor_enter(VALUE monitor) struct rb_monitor *mc = monitor_ptr(monitor); if (!mc_owner_p(mc)) { rb_mutex_lock(mc->mutex); - RB_OBJ_WRITE(monitor, &mc->owner, rb_thread_current()); + RB_OBJ_WRITE(monitor, &mc->owner, rb_fiber_current()); mc->count = 0; } mc->count++; @@ -90,7 +90,7 @@ monitor_check_owner(VALUE monitor) { struct rb_monitor *mc = monitor_ptr(monitor); if (!mc_owner_p(mc)) { - rb_raise(rb_eThreadError, "current thread not owner"); + rb_raise(rb_eThreadError, "current fiber not owner"); } return Qnil; } @@ -161,7 +161,7 @@ monitor_enter_for_cond(VALUE v) struct wait_for_cond_data *data = (struct wait_for_cond_data *)v; struct rb_monitor *mc = monitor_ptr(data->monitor); - RB_OBJ_WRITE(data->monitor, &mc->owner, rb_thread_current()); + RB_OBJ_WRITE(data->monitor, &mc->owner, rb_fiber_current()); mc->count = NUM2LONG(data->count); return Qnil; } diff --git a/test/monitor/test_monitor.rb b/test/monitor/test_monitor.rb index 734b639d4c..0f17d58f71 100644 --- a/test/monitor/test_monitor.rb +++ b/test/monitor/test_monitor.rb @@ -10,6 +10,13 @@ class TestMonitor < Test::Unit::TestCase @monitor = Monitor.new end + def test_enter_in_different_fibers + @monitor.enter + Fiber.new { + assert_equal false, @monitor.try_enter + }.resume + end + def test_enter ary = [] queue = Queue.new |