我很难将参数传递给rb_thread_call_without_gvl
。这是我正在使用的简单代码。
#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 %fn", 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);
}
然后,我尝试使用脚本client.rb
:从Ruby调用扩展
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
最后是我的extconf.rb
:
require 'mkmf'
extension_name = 'csum'
create_makefile(extension_name)
我是C语言的初学者,但我需要创建一个扩展,它可以在不受单个线程限制的情况下使用库。看看我的另一个问题。
当我make
分机时,我收到一个警告,说
warning: incompatible pointer types passing 'VALUE (VALUE, VALUE)' to parameter of type 'void *(*)(void *)'
虽然我理解它所说的,但我不知道如何解决它。我应该忽略它吗?此外,当我运行client.rb
时,当它调用obj.no_gvl
时,我有一个分段错误。
我使用的是Mac OSX 10.10.5,使用的是Ruby 2.0.0-p247到rbenv
。
如果您还没有看到它,rb_thread_call_without_gvl
的源代码包括一些文档。(我已经链接到您正在使用的版本,但这是一个相当旧的Ruby,如果可能的话,您应该考虑更新。这个API在当前版本中是相同的,至少在2.3.1之前。)
功能原型看起来像:
void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
rb_unblock_function_t *ubf, void *data2);
被调用的函数应该接受一个void *
参数并返回void *
,即它是一个普通的C函数,而不是实现Ruby方法的C函数。事实上,它不能实现Ruby方法,因为这样做意味着访问受GVL保护的结构。
要使用它,您需要将想要在没有锁的情况下执行的代码移动到具有正确接口的函数中,而该函数不使用任何Ruby API。这里有一个例子(基于您自己的例子),它创建了一个Ruby方法,该方法将传递给它的参数加倍,并且在没有GVL:的情况下完成工作
#include <stdio.h>
#include <ruby.h>
#include <ruby/thread.h>
// The function that does the work, accepts void* and returns void*
void* doubleArg(void* x) {
// Unpack the arg and cast back to double.
double val = *(double*)x;
double result = val + val;
printf("The sum in C is %fn", result);
// If you wanted you could wrap up some data here to return to
// Ruby land.
return NULL;
}
// The function implementing the Ruby method
VALUE double_no_gvl(VALUE self, VALUE arg) {
// First wrap up the input as a pointer.
// You'll probably want to do some checking on the type of the
// argument here too.
double argAsDouble = NUM2DBL(arg);
double *ptr = &argAsDouble;
// Now call the function without the GVL.
// It might be worth looking into providing
// an ubf function here too.
rb_thread_call_without_gvl(doubleArg, 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, "double_no_gvl", double_no_gvl, 1);
}
你可以用这样的脚本来调用它:
require './csum'
obj = MyModule::MyClass.new
obj.double_no_gvl(3.9)