I am struggling to pass arguments to rb_thread_call_without_gvl
. This is the simple code I am using.
#include <stdio.h>
#include <ruby.h>
#include <ruby/thread.h>
VALUE summa(VALUE self, VALUE x)
{
double result;
result = NUM2DBL(x) + NUM2DBL(x);
printf("The sum in C is %f\n", result);
return DBL2NUM(result);
}
VALUE no_gvl(VALUE self)
{
double arg = 3.0;
double *ptr = &arg;
rb_thread_call_without_gvl(summa, ptr, NULL, NULL);
return Qnil;
}
void Init_csum()
{
VALUE myModule = rb_define_module("MyModule");
VALUE myClass = rb_define_class_under(myModule, "MyClass", rb_cObject);
rb_define_method(myClass, "summa", summa, 1);
rb_define_method(myClass, "no_gvl", no_gvl, 0);
}
I then try to call the extension from Ruby with the script client.rb
:
require './csum'
obj = MyModule::MyClass.new # MyClass is defined in C
puts "The sum in R is " + obj.summa(7.0).to_s
puts obj.no_gvl
And finally my extconf.rb
:
require 'mkmf'
extension_name = 'csum'
create_makefile(extension_name)
I am a beginner in C, but I need to create an extension that can use a library without the limitation of one single thread. See my other question.
When I make
the extension I receive a warning saying
warning: incompatible pointer types passing 'VALUE (VALUE, VALUE)' to parameter of type 'void *(*)(void *)'
Although I understand what it says, I cannot see how to fix it. Should I just ignore it?
Also when I run client.rb
I have a segmentation fault when it calls obj.no_gvl
.
I am on Mac OSX 10.10.5 and I am using Ruby 2.0.0-p247 through rbenv
.