Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Last active December 30, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SamSaffron/7868096 to your computer and use it in GitHub Desktop.
Save SamSaffron/7868096 to your computer and use it in GitHub Desktop.
#include <ruby.h>
#include <ruby/io.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
extern void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
rb_unblock_function_t *ubf, void *data2);
volatile int halt = 1;
static void ubf_func(void *data)
{
VALUE thread = (VALUE)data;
if(rb_thread_interrupted(thread)){
halt = 0;
}
}
static void* halt_func(void *data)
{
while(1==1) {
if(!halt) {
return NULL;
}
}
return NULL;
}
static VALUE
rb_test_halt(VALUE module)
{
VALUE thread = rb_thread_current();
rb_thread_call_without_gvl(halt_func, NULL, ubf_func, (void*)thread);
return Qnil;
}
void Init_test( void )
{
VALUE rb_mTest = rb_define_module("Test");
rb_define_module_function(rb_mTest, "halt", rb_test_halt, 0);
}
require 'mkmf'
create_header
create_makefile('test')
`ruby extconf.rb`
`make`
puts Process.pid
$:.unshift(".")
require "test"
Thread.new do
sleep 1
# this kills the process but should not ...
`kill -s USR1 #{Process.pid}`
end
trap :USR1 do
p "GOT USR1"
end
Test.halt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment