ext/gyro/thread.c in polyphony-0.39 vs ext/gyro/thread.c in polyphony-0.40

- old
+ new

@@ -38,15 +38,18 @@ return rb_funcall(selector_proc, ID_call, 1, thread); } static VALUE Thread_setup_fiber_scheduling(VALUE self) { + VALUE queue; + VALUE selector; + rb_ivar_set(self, ID_ivar_main_fiber, rb_fiber_current()); rb_ivar_set(self, ID_fiber_ref_count, INT2NUM(0)); - VALUE queue = rb_ary_new(); + queue = rb_ary_new(); rb_ivar_set(self, ID_run_queue, queue); - VALUE selector = rb_funcall(rb_cThread, ID_create_event_selector, 1, self); + selector = rb_funcall(rb_cThread, ID_create_event_selector, 1, self); rb_ivar_set(self, ID_ivar_event_selector, selector); return self; } @@ -99,33 +102,34 @@ static VALUE Thread_fiber_scheduling_stats(VALUE self) { VALUE stats = rb_hash_new(); VALUE queue = rb_ivar_get(self, ID_run_queue); VALUE selector = rb_ivar_get(self, ID_ivar_event_selector); - + long pending_count; + long scheduled_count = RARRAY_LEN(queue); rb_hash_aset(stats, SYM_scheduled_fibers, INT2NUM(scheduled_count)); - long pending_count = Gyro_Selector_pending_count(selector); + pending_count = Gyro_Selector_pending_count(selector); rb_hash_aset(stats, SYM_pending_watchers, INT2NUM(pending_count)); return stats; } VALUE Thread_schedule_fiber(VALUE self, VALUE fiber, VALUE value) { - if (rb_fiber_alive_p(fiber) != Qtrue) { - return self; - } + VALUE queue; + if (rb_fiber_alive_p(fiber) != Qtrue) return self; + FIBER_TRACE(3, SYM_fiber_schedule, fiber, value); // if fiber is already scheduled, just set the scheduled value, then return rb_ivar_set(fiber, ID_runnable_value, value); if (rb_ivar_get(fiber, ID_runnable) != Qnil) { return self; } - VALUE queue = rb_ivar_get(self, ID_run_queue); + queue = rb_ivar_get(self, ID_run_queue); rb_ary_push(queue, fiber); rb_ivar_set(fiber, ID_runnable, Qtrue); if (rb_thread_current() != self) { // if the fiber scheduling is done across threads, we need to make sure the @@ -140,17 +144,18 @@ } return self; } VALUE Thread_schedule_fiber_with_priority(VALUE self, VALUE fiber, VALUE value) { - if (rb_fiber_alive_p(fiber) != Qtrue) { - return self; - } + VALUE queue; + + if (rb_fiber_alive_p(fiber) != Qtrue) return self; + FIBER_TRACE(3, SYM_fiber_schedule, fiber, value); rb_ivar_set(fiber, ID_runnable_value, value); - VALUE queue = rb_ivar_get(self, ID_run_queue); + queue = rb_ivar_get(self, ID_run_queue); // if fiber is already scheduled, remove it from the run queue if (rb_ivar_get(fiber, ID_runnable) != Qnil) { rb_ary_delete(queue, fiber); } else { @@ -174,26 +179,29 @@ return self; } VALUE Thread_switch_fiber(VALUE self) { VALUE current_fiber = rb_fiber_current(); + VALUE queue = rb_ivar_get(self, ID_run_queue); + VALUE selector = rb_ivar_get(self, ID_ivar_event_selector); + VALUE next_fiber; + VALUE value; + int ref_count; + if (__tracing_enabled__) { if (rb_ivar_get(current_fiber, ID_ivar_running) != Qfalse) { rb_funcall(rb_cObject, ID_fiber_trace, 2, SYM_fiber_switchpoint, current_fiber); } } - VALUE queue = rb_ivar_get(self, ID_run_queue); - VALUE selector = rb_ivar_get(self, ID_ivar_event_selector); - VALUE next_fiber; while (1) { next_fiber = rb_ary_shift(queue); // if (break_flag != 0) { // return Qnil; // } - int ref_count = Thread_fiber_ref_count(self); + ref_count = Thread_fiber_ref_count(self); if (next_fiber != Qnil) { if (ref_count > 0) { // this mechanism prevents event starvation in case the run queue never // empties Gyro_Selector_run_no_wait(selector, current_fiber, RARRAY_LEN(queue)); @@ -210,11 +218,11 @@ if (next_fiber == Qnil) { return Qnil; } // run next fiber - VALUE value = rb_ivar_get(next_fiber, ID_runnable_value); + value = rb_ivar_get(next_fiber, ID_runnable_value); FIBER_TRACE(3, SYM_fiber_run, next_fiber, value); rb_ivar_set(next_fiber, ID_runnable, Qnil); RB_GC_GUARD(next_fiber); RB_GC_GUARD(value); @@ -234,12 +242,13 @@ return self; } VALUE Gyro_switchpoint() { + VALUE ret; VALUE thread = rb_thread_current(); Thread_ref(thread); - VALUE ret = Thread_switch_fiber(thread); + ret = Thread_switch_fiber(thread); Thread_unref(thread); RB_GC_GUARD(ret); return ret; }