ruby-changes:28888
From: ko1 <ko1@a...>
Date: Mon, 27 May 2013 06:31:01 +0900 (JST)
Subject: [ruby-changes:28888] ko1:r40940 (trunk): * include/ruby/debug.h, vm_trace.c: add rb_postponed_job API.
ko1 2013-05-27 06:30:44 +0900 (Mon, 27 May 2013) New Revision: 40940 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40940 Log: * include/ruby/debug.h, vm_trace.c: add rb_postponed_job API. Postponed jobs are registered with this API. Registered jobs are invoked at `ruby-running-safe-point' as soon as possible. This timing is completely same as finalizer timing. There are two APIs: * rb_postponed_job_register(flags, func, data): register a postponed job with data. flags are reserved. * rb_postponed_job_register_one(flags, func, data): same as `rb_postponed_job_register', but only one `func' job is registered (skip if `func' is already registered). This change is mostly written by Aman Gupta (tmm1). https://bugs.ruby-lang.org/issues/8107#note-15 [Feature #8107] * gc.c: use postponed job API for finalizer. * common.mk: add dependency from vm_trace.c to debug.h. * ext/-test-/postponed_job/extconf.rb, postponed_job.c, test/-ext-/postponed_job/test_postponed_job.rb: add a test. * thread.c: implement postponed API. * vm_core.h: ditto. Added directories: trunk/ext/-test-/postponed_job/ trunk/test/-ext-/postponed_job/ Added files: trunk/ext/-test-/postponed_job/extconf.rb trunk/ext/-test-/postponed_job/postponed_job.c trunk/test/-ext-/postponed_job/test_postponed_job.rb Modified files: trunk/ChangeLog trunk/common.mk trunk/gc.c trunk/include/ruby/debug.h trunk/thread.c trunk/vm_core.h trunk/vm_trace.c Index: include/ruby/debug.h =================================================================== --- include/ruby/debug.h (revision 40939) +++ include/ruby/debug.h (revision 40940) @@ -66,6 +66,11 @@ VALUE rb_tracearg_self(rb_trace_arg_t *t https://github.com/ruby/ruby/blob/trunk/include/ruby/debug.h#L66 VALUE rb_tracearg_return_value(rb_trace_arg_t *trace_arg); VALUE rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg); +/* Postponed Job API */ +typedef void (*rb_postponed_job_func_t)(void *arg); +int rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data); +int rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data); + /* undocumented advanced tracing APIs */ typedef enum { Index: ChangeLog =================================================================== --- ChangeLog (revision 40939) +++ ChangeLog (revision 40940) @@ -1,3 +1,32 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon May 27 06:22:41 2013 Koichi Sasada <ko1@a...> + + * include/ruby/debug.h, vm_trace.c: add rb_postponed_job API. + Postponed jobs are registered with this API. Registered jobs + are invoked at `ruby-running-safe-point' as soon as possible. + This timing is completely same as finalizer timing. + + There are two APIs: + * rb_postponed_job_register(flags, func, data): register a + postponed job with data. flags are reserved. + * rb_postponed_job_register_one(flags, func, data): same as + `rb_postponed_job_register', but only one `func' job is + registered (skip if `func' is already registered). + + This change is mostly written by Aman Gupta (tmm1). + https://bugs.ruby-lang.org/issues/8107#note-15 + [Feature #8107] + + * gc.c: use postponed job API for finalizer. + + * common.mk: add dependency from vm_trace.c to debug.h. + + * ext/-test-/postponed_job/extconf.rb, postponed_job.c, + test/-ext-/postponed_job/test_postponed_job.rb: add a test. + + * thread.c: implement postponed API. + + * vm_core.h: ditto. + Mon May 27 02:26:02 2013 Koichi Sasada <ko1@a...> * gc.c (gc_stat): collect promote_operation_count and Index: vm_core.h =================================================================== --- vm_core.h (revision 40939) +++ vm_core.h (revision 40940) @@ -379,6 +379,8 @@ typedef struct rb_vm_struct { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L379 /* hook */ rb_hook_list_t event_hooks; + struct rb_postponed_job_struct *postponed_job; + int src_encoding_index; VALUE verbose, debug, progname; @@ -902,15 +904,15 @@ GET_THREAD(void) https://github.com/ruby/ruby/blob/trunk/vm_core.h#L904 #endif enum { - TIMER_INTERRUPT_MASK = 0x01, - PENDING_INTERRUPT_MASK = 0x02, - FINALIZER_INTERRUPT_MASK = 0x04, - TRAP_INTERRUPT_MASK = 0x08 + TIMER_INTERRUPT_MASK = 0x01, + PENDING_INTERRUPT_MASK = 0x02, + POSTPONED_JOB_INTERRUPT_MASK = 0x04, + TRAP_INTERRUPT_MASK = 0x08 }; #define RUBY_VM_SET_TIMER_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, TIMER_INTERRUPT_MASK) #define RUBY_VM_SET_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, PENDING_INTERRUPT_MASK) -#define RUBY_VM_SET_FINALIZER_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, FINALIZER_INTERRUPT_MASK) +#define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK) #define RUBY_VM_SET_TRAP_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, TRAP_INTERRUPT_MASK) #define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & ~(th)->interrupt_mask & (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK)) #define RUBY_VM_INTERRUPTED_ANY(th) ((th)->interrupt_flag & ~(th)->interrupt_mask) @@ -1000,6 +1002,8 @@ extern VALUE rb_get_coverages(void); https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1002 extern void rb_set_coverages(VALUE); extern void rb_reset_coverages(void); +void rb_postponed_job_flush(rb_vm_t *vm); + RUBY_SYMBOL_EXPORT_END #endif /* RUBY_VM_CORE_H */ Index: thread.c =================================================================== --- thread.c (revision 40939) +++ thread.c (revision 40940) @@ -1925,7 +1925,7 @@ rb_threadptr_execute_interrupts(rb_threa https://github.com/ruby/ruby/blob/trunk/thread.c#L1925 int sig; int timer_interrupt; int pending_interrupt; - int finalizer_interrupt; + int postponed_job_interrupt; int trap_interrupt; do { @@ -1939,7 +1939,7 @@ rb_threadptr_execute_interrupts(rb_threa https://github.com/ruby/ruby/blob/trunk/thread.c#L1939 timer_interrupt = interrupt & TIMER_INTERRUPT_MASK; pending_interrupt = interrupt & PENDING_INTERRUPT_MASK; - finalizer_interrupt = interrupt & FINALIZER_INTERRUPT_MASK; + postponed_job_interrupt = interrupt & POSTPONED_JOB_INTERRUPT_MASK; trap_interrupt = interrupt & TRAP_INTERRUPT_MASK; /* signal handling */ @@ -1974,8 +1974,8 @@ rb_threadptr_execute_interrupts(rb_threa https://github.com/ruby/ruby/blob/trunk/thread.c#L1974 } } - if (finalizer_interrupt) { - rb_gc_finalize_deferred(); + if (postponed_job_interrupt) { + rb_postponed_job_flush(th->vm); } if (timer_interrupt) { Index: common.mk =================================================================== --- common.mk (revision 40939) +++ common.mk (revision 40940) @@ -682,7 +682,7 @@ gc.$(OBJEXT): {$(VPATH)}gc.c $(RUBY_H_IN https://github.com/ruby/ruby/blob/trunk/common.mk#L682 {$(VPATH)}regex.h $(ENCODING_H_INCLUDES) $(VM_CORE_H_INCLUDES) \ {$(VPATH)}gc.h {$(VPATH)}io.h {$(VPATH)}eval_intern.h {$(VPATH)}util.h \ {$(VPATH)}internal.h {$(VPATH)}constant.h \ - {$(VPATH)}thread.h $(PROBES_H_INCLUDES) {$(VPATH)}vm_opts.h + {$(VPATH)}thread.h $(PROBES_H_INCLUDES) {$(VPATH)}vm_opts.h {$(VPATH)}debug.h hash.$(OBJEXT): {$(VPATH)}hash.c $(RUBY_H_INCLUDES) {$(VPATH)}util.h \ $(ENCODING_H_INCLUDES) {$(VPATH)}internal.h $(PROBES_H_INCLUDES) {$(VPATH)}vm_opts.h inits.$(OBJEXT): {$(VPATH)}inits.c $(RUBY_H_INCLUDES) \ Index: gc.c =================================================================== --- gc.c (revision 40939) +++ gc.c (revision 40940) @@ -17,6 +17,7 @@ https://github.com/ruby/ruby/blob/trunk/gc.c#L17 #include "ruby/io.h" #include "ruby/thread.h" #include "ruby/util.h" +#include "ruby/debug.h" #include "eval_intern.h" #include "vm_core.h" #include "internal.h" @@ -1644,8 +1645,8 @@ finalize_deferred(rb_objspace_t *objspac https://github.com/ruby/ruby/blob/trunk/gc.c#L1645 } } -void -rb_gc_finalize_deferred(void) +static void +gc_finalize_deferred(void *dmy) { rb_objspace_t *objspace = &rb_objspace; if (ATOMIC_EXCHANGE(finalizing, 1)) return; @@ -1653,6 +1654,19 @@ rb_gc_finalize_deferred(void) https://github.com/ruby/ruby/blob/trunk/gc.c#L1654 ATOMIC_SET(finalizing, 0); } +/* TODO: to keep compatibility, maybe unused. */ +void +rb_gc_finalize_deferred(void) +{ + gc_finalize_deferred(0); +} + +static void +gc_finalize_deferred_register() +{ + rb_postponed_job_register_one(0, gc_finalize_deferred, 0); +} + struct force_finalize_list { VALUE obj; VALUE table; @@ -2179,7 +2193,7 @@ slot_sweep_body(rb_objspace_t *objspace, https://github.com/ruby/ruby/blob/trunk/gc.c#L2193 if (deferred_final_list && !finalizing) { rb_thread_t *th = GET_THREAD(); if (th) { - RUBY_VM_SET_FINALIZER_INTERRUPT(th); + gc_finalize_deferred_register(); } } Index: ext/-test-/postponed_job/postponed_job.c =================================================================== --- ext/-test-/postponed_job/postponed_job.c (revision 0) +++ ext/-test-/postponed_job/postponed_job.c (revision 40940) @@ -0,0 +1,32 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/postponed_job/postponed_job.c#L1 +#include "ruby.h" +#include "ruby/debug.h" + +static void +pjob_callback(void *data) +{ + VALUE ary = (VALUE)data; + Check_Type(ary, T_ARRAY); + + rb_ary_replace(ary, rb_funcall(Qnil, rb_intern("caller"), 0)); +} + +static VALUE +pjob_register(VALUE self, VALUE obj) +{ + rb_postponed_job_register(0, pjob_callback, (void *)obj); +} + +static VALUE +pjob_call_direct(VALUE self, VALUE obj) +{ + pjob_callback((void *)obj); +} + +void +Init_task(VALUE self) +{ + VALUE mBug = rb_define_module("Bug"); + rb_define_module_function(mBug, "postponed_job_register", pjob_register, 1); + rb_define_module_function(mBug, "postponed_job_call_direct", pjob_call_direct, 1); +} + Index: ext/-test-/postponed_job/extconf.rb =================================================================== --- ext/-test-/postponed_job/extconf.rb (revision 0) +++ ext/-test-/postponed_job/extconf.rb (revision 40940) @@ -0,0 +1 @@ +create_makefile('-test-/postponed_job') Index: vm_trace.c =================================================================== --- vm_trace.c (revision 40939) +++ vm_trace.c (revision 40940) @@ -1352,3 +1352,59 @@ Init_vm_trace(void) https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L1352 rb_define_method(rb_cTracePoint, "raised_exception", tracepoint_attr_raised_exception, 0); } +typedef struct rb_postponed_job_struct { + unsigned long flags; /* reserve */ + rb_thread_t *th; /* created therad, reserve */ + rb_postponed_job_func_t func; + void *data; + struct rb_postponed_job_struct *next; +} rb_postponed_job_t; + +int +rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data) +{ + rb_thread_t *th = GET_THREAD(); + rb_vm_t *vm = th->vm; + rb_postponed_job_t *pjob = (rb_postponed_job_t *)malloc(sizeof(rb_postponed_job_t)); /* postponed_job should be separated with Ruby's GC */ + if (pjob == NULL) return 0; /* failed */ + + pjob->flags = flags; + pjob->th = th; + pjob->func = func; + pjob->data = data; + + pjob->next = vm->postponed_job; + vm->postponed_job = pjob; + + RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(th); + return 1; +} + +int +rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data) +{ + rb_vm_t *vm = GET_VM(); + rb_postponed_job_t *pjob = vm->postponed_job; + + while (pjob) { + if (pjob->func == func) { + return 2; + } + } + + return rb_postponed_job_register(flags, func, data); +} + +void +rb_postponed_job_flush(rb_vm_t *vm) +{ + rb_postponed_job_t *pjob = vm->postponed_job, *next_pjob; + vm->postponed_job = 0; + + while (pjob) { + next_pjob = pjob->next; + pjob->func(pjob->data); + free(pjob); /* postponed_job should be separated with Ruby's GC */ + pjob = next_pjob; + } +} Index: test/-ext-/postponed_job/test_postponed_job.rb =================================================================== --- test/-ext-/postponed_job/test_postponed_job.rb (revision 0) +++ test/-ext-/postponed_job/test_postponed_job.rb (revision 40940) @@ -0,0 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/postponed_job/test_postponed_job.rb#L1 +require 'test/unit' +require 'thread' +require '-test-/postponed_job' + +module Bug + def self.postponed_job_call_direct_wrapper(*args) + postponed_job_call_direct(*arg) + end + + def self.postponed_job_register_wrapper(*args) + postponed_job_register(*args) + end +end + +class TestTask < Test::Unit::TestCase + def test_register + direct, registered = [], [] + + Bug.postponed_job_call_direct_wrapper(direct) + Bug.postponed_job_register_wrapper(registered) + + assert_match /postponed_job_call_direct_wrapper/, direct.join + assert_not_match /postponed_job_register_wrapper/, registered.join + end +end \ No newline at end of file -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/