-
-
Save larskanis/7872229 to your computer and use it in GitHub Desktop.
This file contains hidden or 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); | |
extern void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1); | |
volatile int halt = 0; | |
static void *int_func(void *data){ | |
printf("please unblock: %d\n", ruby_native_thread_p()); | |
return NULL; | |
} | |
static void ubf_func(void *data) | |
{ | |
halt = 1; | |
} | |
static void* halt_func(void *data) | |
{ | |
while(1==1) { | |
if(halt) { | |
printf("before rb_thread_call_with_gvl\n"); | |
rb_thread_call_with_gvl(int_func, NULL); | |
printf("after rb_thread_call_with_gvl\n"); | |
halt = 0; | |
} | |
} | |
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 hidden or 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') | |
`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