2013-05-27 14:16:05 -04:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/debug.h"
|
|
|
|
|
2013-10-11 05:13:18 -04:00
|
|
|
static int invoking; /* TODO: should not be global variable */
|
|
|
|
|
|
|
|
static VALUE
|
2019-08-26 02:20:15 -04:00
|
|
|
invoke_proc_ensure(VALUE _)
|
2013-10-11 05:13:18 -04:00
|
|
|
{
|
|
|
|
invoking = 0;
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
invoke_proc_begin(VALUE proc)
|
|
|
|
{
|
|
|
|
return rb_proc_call(proc, rb_ary_new());
|
|
|
|
}
|
|
|
|
|
2013-05-27 14:16:05 -04:00
|
|
|
static void
|
|
|
|
invoke_proc(void *data)
|
|
|
|
{
|
|
|
|
VALUE proc = (VALUE)data;
|
2013-10-11 05:13:18 -04:00
|
|
|
invoking += 1;
|
|
|
|
rb_ensure(invoke_proc_begin, proc, invoke_proc_ensure, 0);
|
2013-05-27 14:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_start_end_i(VALUE tpval, void *data)
|
|
|
|
{
|
2013-10-11 05:13:18 -04:00
|
|
|
if (0) {
|
|
|
|
rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
|
|
|
|
fprintf(stderr, "trace: %s\n", rb_tracearg_event_flag(tparg) == RUBY_INTERNAL_EVENT_GC_START ? "gc_start" : "gc_end");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invoking == 0) {
|
|
|
|
rb_postponed_job_register(0, invoke_proc, data);
|
|
|
|
}
|
2013-05-27 14:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2013-12-05 04:24:02 -05:00
|
|
|
set_gc_hook(VALUE module, VALUE proc, rb_event_flag_t event, const char *tp_str, const char *proc_str)
|
2013-05-27 14:16:05 -04:00
|
|
|
{
|
|
|
|
VALUE tpval;
|
|
|
|
ID tp_key = rb_intern(tp_str);
|
|
|
|
|
2013-10-11 05:13:18 -04:00
|
|
|
/* disable previous keys */
|
2013-12-05 04:24:02 -05:00
|
|
|
if (rb_ivar_defined(module, tp_key) != 0 &&
|
|
|
|
RTEST(tpval = rb_ivar_get(module, tp_key))) {
|
2013-05-27 14:16:05 -04:00
|
|
|
rb_tracepoint_disable(tpval);
|
2013-12-05 04:24:02 -05:00
|
|
|
rb_ivar_set(module, tp_key, Qnil);
|
2013-05-27 14:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (RTEST(proc)) {
|
|
|
|
if (!rb_obj_is_proc(proc)) {
|
|
|
|
rb_raise(rb_eTypeError, "trace_func needs to be Proc");
|
|
|
|
}
|
|
|
|
|
|
|
|
tpval = rb_tracepoint_new(0, event, gc_start_end_i, (void *)proc);
|
2013-12-05 04:24:02 -05:00
|
|
|
rb_ivar_set(module, tp_key, tpval);
|
2013-05-27 14:16:05 -04:00
|
|
|
rb_tracepoint_enable(tpval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2013-12-05 04:24:02 -05:00
|
|
|
set_after_gc_start(VALUE module, VALUE proc)
|
2013-05-27 14:16:05 -04:00
|
|
|
{
|
2013-12-05 04:24:02 -05:00
|
|
|
return set_gc_hook(module, proc, RUBY_INTERNAL_EVENT_GC_START,
|
2013-05-27 14:16:05 -04:00
|
|
|
"__set_after_gc_start_tpval__", "__set_after_gc_start_proc__");
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:37:14 -04:00
|
|
|
static VALUE
|
|
|
|
start_after_gc_exit(VALUE module, VALUE proc)
|
|
|
|
{
|
|
|
|
return set_gc_hook(module, proc, RUBY_INTERNAL_EVENT_GC_EXIT,
|
|
|
|
"__set_after_gc_exit_tpval__", "__set_after_gc_exit_proc__");
|
|
|
|
}
|
|
|
|
|
2013-05-27 14:16:05 -04:00
|
|
|
void
|
2013-12-05 04:24:02 -05:00
|
|
|
Init_gc_hook(VALUE module)
|
2013-05-27 14:16:05 -04:00
|
|
|
{
|
2013-12-05 04:24:02 -05:00
|
|
|
rb_define_module_function(module, "after_gc_start_hook=", set_after_gc_start, 1);
|
2020-03-11 10:37:14 -04:00
|
|
|
rb_define_module_function(module, "after_gc_exit_hook=", start_after_gc_exit, 1);
|
2013-05-27 14:16:05 -04:00
|
|
|
}
|