Ruby Global Interpreter Lock(GIL) - rb_thread_call_without_gvl

时间:2016-05-05 23:16:27

标签: c ruby gil ruby-c-extension

我正在努力将参数传递给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 %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);
}

然后我尝试使用脚本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的初学者,但是我需要创建一个可以使用库而不受单个线程限制的扩展。查看我的other question

当我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

1 个答案:

答案 0 :(得分:1)

如果您还没有看到,the source for rb_thread_call_without_gvl includes some documentation。 (我已链接到您正在使用的版本,但这是pretty old 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 %f\n", 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)
相关问题