From e2793a908e23dd1dde5eaf553c5bb7ed5e8533c9 Mon Sep 17 00:00:00 2001 From: ko1 Date: Sun, 26 May 2013 21:30:44 +0000 Subject: * 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 --- vm_trace.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'vm_trace.c') 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; + } +} -- cgit v1.2.3