diff options
author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-05-26 21:30:44 +0000 |
---|---|---|
committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-05-26 21:30:44 +0000 |
commit | e2793a908e23dd1dde5eaf553c5bb7ed5e8533c9 (patch) | |
tree | 2a84a99f5145481d7781121cb7836b82881953f9 | |
parent | 767debf92e7f21f48631ebb4764ba16cfaf1d7f0 (diff) | |
download | ruby-e2793a908e23dd1dde5eaf553c5bb7ed5e8533c9.tar.gz |
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40940 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 29 | ||||
-rw-r--r-- | common.mk | 2 | ||||
-rw-r--r-- | ext/-test-/postponed_job/extconf.rb | 1 | ||||
-rw-r--r-- | ext/-test-/postponed_job/postponed_job.c | 32 | ||||
-rw-r--r-- | gc.c | 20 | ||||
-rw-r--r-- | include/ruby/debug.h | 5 | ||||
-rw-r--r-- | test/-ext-/postponed_job/test_postponed_job.rb | 25 | ||||
-rw-r--r-- | thread.c | 8 | ||||
-rw-r--r-- | vm_core.h | 14 | ||||
-rw-r--r-- | vm_trace.c | 56 |
10 files changed, 179 insertions, 13 deletions
@@ -1,3 +1,32 @@ +Mon May 27 06:22:41 2013 Koichi Sasada <ko1@atdot.net> + + * 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@atdot.net> * gc.c (gc_stat): collect promote_operation_count and @@ -682,7 +682,7 @@ gc.$(OBJEXT): {$(VPATH)}gc.c $(RUBY_H_INCLUDES) {$(VPATH)}re.h \ {$(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) \ diff --git a/ext/-test-/postponed_job/extconf.rb b/ext/-test-/postponed_job/extconf.rb new file mode 100644 index 0000000000..aa29b593f4 --- /dev/null +++ b/ext/-test-/postponed_job/extconf.rb @@ -0,0 +1 @@ +create_makefile('-test-/postponed_job') diff --git a/ext/-test-/postponed_job/postponed_job.c b/ext/-test-/postponed_job/postponed_job.c new file mode 100644 index 0000000000..a5bb8892b5 --- /dev/null +++ b/ext/-test-/postponed_job/postponed_job.c @@ -0,0 +1,32 @@ +#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); +} + @@ -17,6 +17,7 @@ #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 *objspace) } } -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) 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, struct heaps_slot *sweep_slot, const in if (deferred_final_list && !finalizing) { rb_thread_t *th = GET_THREAD(); if (th) { - RUBY_VM_SET_FINALIZER_INTERRUPT(th); + gc_finalize_deferred_register(); } } diff --git a/include/ruby/debug.h b/include/ruby/debug.h index 67e15a8d2f..5663448b6c 100644 --- a/include/ruby/debug.h +++ b/include/ruby/debug.h @@ -66,6 +66,11 @@ VALUE rb_tracearg_self(rb_trace_arg_t *trace_arg); 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 { diff --git a/test/-ext-/postponed_job/test_postponed_job.rb b/test/-ext-/postponed_job/test_postponed_job.rb new file mode 100644 index 0000000000..a554936b3d --- /dev/null +++ b/test/-ext-/postponed_job/test_postponed_job.rb @@ -0,0 +1,25 @@ +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 @@ -1925,7 +1925,7 @@ rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing) 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_thread_t *th, int blocking_timing) 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_thread_t *th, int blocking_timing) } } - if (finalizer_interrupt) { - rb_gc_finalize_deferred(); + if (postponed_job_interrupt) { + rb_postponed_job_flush(th->vm); } if (timer_interrupt) { @@ -379,6 +379,8 @@ typedef struct rb_vm_struct { /* 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) #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); 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 */ diff --git a/vm_trace.c b/vm_trace.c index 2a40578bce..0c54426368 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -1352,3 +1352,59 @@ Init_vm_trace(void) 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; + } +} |