1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
ko1 2013-05-26 21:30:44 +00:00
parent 767debf92e
commit e2793a908e
10 changed files with 179 additions and 13 deletions

View file

@ -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

View file

@ -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) \

View file

@ -0,0 +1 @@
create_makefile('-test-/postponed_job')

View file

@ -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);
}

20
gc.c
View file

@ -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();
}
}

View file

@ -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 {

View file

@ -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

View 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) {

View file

@ -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 */

View file

@ -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;
}
}