[前][次][番号順一覧][スレッド一覧]

ruby-changes:65992

From: Benoit <ko1@a...>
Date: Wed, 28 Apr 2021 01:47:13 +0900 (JST)
Subject: [ruby-changes:65992] 3a3b19b2bb (master): Fix Monitor to lock per Fiber, like Mutex [Bug #17827]

https://git.ruby-lang.org/ruby.git/commit/?id=3a3b19b2bb

From 3a3b19b2bba49e5d6f1cf13764eb6dd701397be9 Mon Sep 17 00:00:00 2001
From: Benoit Daloze <eregontp@g...>
Date: Tue, 27 Apr 2021 18:42:50 +0200
Subject: Fix Monitor to lock per Fiber, like Mutex [Bug #17827]

---
 ext/monitor/monitor.c        | 10 +++++-----
 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 26a564f..43a18f5 100644
--- a/ext/monitor/monitor.c
+++ b/ext/monitor/monitor.c
@@ -53,7 +53,7 @@ monitor_ptr(VALUE monitor) https://github.com/ruby/ruby/blob/trunk/ext/monitor/monitor.c#L53
 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) https://github.com/ruby/ruby/blob/trunk/ext/monitor/monitor.c#L65
         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) https://github.com/ruby/ruby/blob/trunk/ext/monitor/monitor.c#L78
     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) https://github.com/ruby/ruby/blob/trunk/ext/monitor/monitor.c#L90
 {
     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) https://github.com/ruby/ruby/blob/trunk/ext/monitor/monitor.c#L161
 
     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 734b639..0f17d58 100644
--- a/test/monitor/test_monitor.rb
+++ b/test/monitor/test_monitor.rb
@@ -10,6 +10,13 @@ class TestMonitor < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/monitor/test_monitor.rb#L10
     @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
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]