2013-05-26 17:30:44 -04:00
|
|
|
#include "ruby.h"
|
|
|
|
#include "ruby/debug.h"
|
|
|
|
|
2019-11-29 03:39:06 -05:00
|
|
|
static int counter;
|
|
|
|
|
2013-05-26 17:30:44 -04:00
|
|
|
static void
|
|
|
|
pjob_callback(void *data)
|
|
|
|
{
|
|
|
|
VALUE ary = (VALUE)data;
|
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
|
2019-11-29 03:39:06 -05:00
|
|
|
rb_ary_push(ary, INT2FIX(counter));
|
2013-05-26 17:30:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
pjob_register(VALUE self, VALUE obj)
|
|
|
|
{
|
2019-11-29 03:39:06 -05:00
|
|
|
counter = 0;
|
2013-05-26 17:30:44 -04:00
|
|
|
rb_postponed_job_register(0, pjob_callback, (void *)obj);
|
2019-11-29 03:39:06 -05:00
|
|
|
rb_gc_start();
|
|
|
|
counter++;
|
|
|
|
rb_gc_start();
|
|
|
|
counter++;
|
|
|
|
rb_gc_start();
|
|
|
|
counter++;
|
2013-05-27 12:48:15 -04:00
|
|
|
return self;
|
2013-05-26 17:30:44 -04:00
|
|
|
}
|
|
|
|
|
2013-05-27 13:08:25 -04:00
|
|
|
static void
|
|
|
|
pjob_one_callback(void *data)
|
|
|
|
{
|
|
|
|
VALUE ary = (VALUE)data;
|
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
|
|
|
|
rb_ary_push(ary, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
pjob_register_one(VALUE self, VALUE obj)
|
|
|
|
{
|
|
|
|
rb_postponed_job_register_one(0, pjob_one_callback, (void *)obj);
|
|
|
|
rb_postponed_job_register_one(0, pjob_one_callback, (void *)obj);
|
|
|
|
rb_postponed_job_register_one(0, pjob_one_callback, (void *)obj);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-05-26 17:30:44 -04:00
|
|
|
static VALUE
|
|
|
|
pjob_call_direct(VALUE self, VALUE obj)
|
|
|
|
{
|
2019-11-29 03:39:06 -05:00
|
|
|
counter = 0;
|
2013-05-26 17:30:44 -04:00
|
|
|
pjob_callback((void *)obj);
|
2019-11-29 03:39:06 -05:00
|
|
|
rb_gc_start();
|
|
|
|
counter++;
|
|
|
|
rb_gc_start();
|
|
|
|
counter++;
|
|
|
|
rb_gc_start();
|
|
|
|
counter++;
|
2013-05-27 12:48:15 -04:00
|
|
|
return self;
|
2013-05-26 17:30:44 -04:00
|
|
|
}
|
|
|
|
|
2021-11-22 19:29:29 -05:00
|
|
|
#ifdef HAVE_PTHREAD_H
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
static void *
|
|
|
|
pjob_register_in_c_thread_i(void *obj)
|
|
|
|
{
|
|
|
|
rb_postponed_job_register_one(0, pjob_one_callback, (void *)obj);
|
|
|
|
rb_postponed_job_register_one(0, pjob_one_callback, (void *)obj);
|
|
|
|
rb_postponed_job_register_one(0, pjob_one_callback, (void *)obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
pjob_register_in_c_thread(VALUE self, VALUE obj)
|
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
if (pthread_create(&thread, NULL, pjob_register_in_c_thread_i, (void *)obj)) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_join(thread, NULL)) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-26 17:30:44 -04:00
|
|
|
void
|
2013-05-26 19:40:01 -04:00
|
|
|
Init_postponed_job(VALUE self)
|
2013-05-26 17:30:44 -04:00
|
|
|
{
|
|
|
|
VALUE mBug = rb_define_module("Bug");
|
|
|
|
rb_define_module_function(mBug, "postponed_job_register", pjob_register, 1);
|
2013-05-27 13:08:25 -04:00
|
|
|
rb_define_module_function(mBug, "postponed_job_register_one", pjob_register_one, 1);
|
2013-05-26 17:30:44 -04:00
|
|
|
rb_define_module_function(mBug, "postponed_job_call_direct", pjob_call_direct, 1);
|
2021-11-22 19:29:29 -05:00
|
|
|
#ifdef HAVE_PTHREAD_H
|
|
|
|
rb_define_module_function(mBug, "postponed_job_register_in_c_thread", pjob_register_in_c_thread, 1);
|
|
|
|
#endif
|
2013-05-26 17:30:44 -04:00
|
|
|
}
|
|
|
|
|