@@ -50,10 +50,10 @@ monitor_ptr(VALUE monitor)
5050 return mc ;
5151}
5252
53- static int
54- mc_owner_p (struct rb_monitor * mc )
53+ static bool
54+ mc_owner_p (struct rb_monitor * mc , VALUE current_fiber )
5555{
56- return mc -> owner == rb_fiber_current () ;
56+ return mc -> owner == current_fiber ;
5757}
5858
5959/*
@@ -67,17 +67,44 @@ monitor_try_enter(VALUE monitor)
6767{
6868 struct rb_monitor * mc = monitor_ptr (monitor );
6969
70- if (!mc_owner_p (mc )) {
70+ VALUE current_fiber = rb_fiber_current ();
71+ if (!mc_owner_p (mc , current_fiber )) {
7172 if (!rb_mutex_trylock (mc -> mutex )) {
7273 return Qfalse ;
7374 }
74- RB_OBJ_WRITE (monitor , & mc -> owner , rb_fiber_current () );
75+ RB_OBJ_WRITE (monitor , & mc -> owner , current_fiber );
7576 mc -> count = 0 ;
7677 }
7778 mc -> count += 1 ;
7879 return Qtrue ;
7980}
8081
82+
83+ struct monitor_args {
84+ VALUE monitor ;
85+ struct rb_monitor * mc ;
86+ VALUE current_fiber ;
87+ };
88+
89+ static inline void
90+ monitor_args_init (struct monitor_args * args , VALUE monitor )
91+ {
92+ args -> monitor = monitor ;
93+ args -> mc = monitor_ptr (monitor );
94+ args -> current_fiber = rb_fiber_current ();
95+ }
96+
97+ static void
98+ monitor_enter0 (struct monitor_args * args )
99+ {
100+ if (!mc_owner_p (args -> mc , args -> current_fiber )) {
101+ rb_mutex_lock (args -> mc -> mutex );
102+ RB_OBJ_WRITE (args -> monitor , & args -> mc -> owner , args -> current_fiber );
103+ args -> mc -> count = 0 ;
104+ }
105+ args -> mc -> count ++ ;
106+ }
107+
81108/*
82109 * call-seq:
83110 * enter -> nil
@@ -87,27 +114,44 @@ monitor_try_enter(VALUE monitor)
87114static VALUE
88115monitor_enter (VALUE monitor )
89116{
90- struct rb_monitor * mc = monitor_ptr (monitor );
91- if (!mc_owner_p (mc )) {
92- rb_mutex_lock (mc -> mutex );
93- RB_OBJ_WRITE (monitor , & mc -> owner , rb_fiber_current ());
94- mc -> count = 0 ;
95- }
96- mc -> count ++ ;
117+ struct monitor_args args ;
118+ monitor_args_init (& args , monitor );
119+ monitor_enter0 (& args );
97120 return Qnil ;
98121}
99122
123+ static inline void
124+ monitor_check_owner0 (struct monitor_args * args )
125+ {
126+ if (!mc_owner_p (args -> mc , args -> current_fiber )) {
127+ rb_raise (rb_eThreadError , "current fiber not owner" );
128+ }
129+ }
130+
100131/* :nodoc: */
101132static VALUE
102133monitor_check_owner (VALUE monitor )
103134{
104- struct rb_monitor * mc = monitor_ptr (monitor );
105- if (!mc_owner_p (mc )) {
106- rb_raise (rb_eThreadError , "current fiber not owner" );
107- }
135+ struct monitor_args args ;
136+ monitor_args_init (& args , monitor );
137+ monitor_check_owner0 (& args );
108138 return Qnil ;
109139}
110140
141+ static void
142+ monitor_exit0 (struct monitor_args * args )
143+ {
144+ monitor_check_owner (args -> monitor );
145+
146+ if (args -> mc -> count <= 0 ) rb_bug ("monitor_exit: count:%d" , (int )args -> mc -> count );
147+ args -> mc -> count -- ;
148+
149+ if (args -> mc -> count == 0 ) {
150+ RB_OBJ_WRITE (args -> monitor , & args -> mc -> owner , Qnil );
151+ rb_mutex_unlock (args -> mc -> mutex );
152+ }
153+ }
154+
111155/*
112156 * call-seq:
113157 * exit -> nil
@@ -117,17 +161,9 @@ monitor_check_owner(VALUE monitor)
117161static VALUE
118162monitor_exit (VALUE monitor )
119163{
120- monitor_check_owner (monitor );
121-
122- struct rb_monitor * mc = monitor_ptr (monitor );
123-
124- if (mc -> count <= 0 ) rb_bug ("monitor_exit: count:%d" , (int )mc -> count );
125- mc -> count -- ;
126-
127- if (mc -> count == 0 ) {
128- RB_OBJ_WRITE (monitor , & mc -> owner , Qnil );
129- rb_mutex_unlock (mc -> mutex );
130- }
164+ struct monitor_args args ;
165+ monitor_args_init (& args , monitor );
166+ monitor_exit0 (& args );
131167 return Qnil ;
132168}
133169
@@ -144,7 +180,7 @@ static VALUE
144180monitor_owned_p (VALUE monitor )
145181{
146182 struct rb_monitor * mc = monitor_ptr (monitor );
147- return ( rb_mutex_locked_p (mc -> mutex ) && mc_owner_p (mc )) ? Qtrue : Qfalse ;
183+ return rb_mutex_locked_p (mc -> mutex ) && mc_owner_p (mc , rb_fiber_current ( )) ? Qtrue : Qfalse ;
148184}
149185
150186static VALUE
@@ -210,9 +246,10 @@ monitor_sync_body(VALUE monitor)
210246}
211247
212248static VALUE
213- monitor_sync_ensure (VALUE monitor )
249+ monitor_sync_ensure (VALUE v_args )
214250{
215- return monitor_exit (monitor );
251+ monitor_exit0 ((struct monitor_args * )v_args );
252+ return Qnil ;
216253}
217254
218255/*
@@ -226,8 +263,10 @@ monitor_sync_ensure(VALUE monitor)
226263static VALUE
227264monitor_synchronize (VALUE monitor )
228265{
229- monitor_enter (monitor );
230- return rb_ensure (monitor_sync_body , monitor , monitor_sync_ensure , monitor );
266+ struct monitor_args args ;
267+ monitor_args_init (& args , monitor );
268+ monitor_enter0 (& args );
269+ return rb_ensure (monitor_sync_body , (VALUE )& args , monitor_sync_ensure , (VALUE )& args );
231270}
232271
233272void
0 commit comments