Last active
December 30, 2015 18:29
-
-
Save SamSaffron/7868096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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