c - Ruby 全局解释器锁 (GIL) - rb_thread_call_without_gvl

标签 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 .

当我制作扩展时,我收到一条警告

warning: incompatible pointer types passing 'VALUE (VALUE, VALUE)' to parameter of type 'void *(*)(void *)'

虽然我明白它说的是什么,但我不知道如何修复它。我应该忽略它吗? 此外,当我运行 client.rb 时,它调用 obj.no_gvl 时出现段错误。

我在 Mac OSX 10.10.5 上,我通过 rbenv 使用 Ruby 2.0.0-p247。

最佳答案

如果你还没有看过,the source for rb_thread_call_without_gvl includes some documentation . (我已经链接到您正在使用的版本,但那是一个 pretty old Ruby ,如果可能,您应该考虑更新。这个 API 在当前版本中是相同的,至少到 2.3.1。)

函数原型(prototype)如下:

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 保护的结构。

要使用它,您需要将要在没有锁定的情况下执行的代码移动到具有正确接口(interface)的函数中,并且不使用任何 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)

关于c - Ruby 全局解释器锁 (GIL) - rb_thread_call_without_gvl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37061787/

相关文章:

python - f2py 函数释放 GIL

c - 将文件名作为 C 的参数传递

ruby-on-rails - 在 Rails 应用程序上运行 ruby​​ 时出现问题

CMake;第386章 :x86-64 architecture of input file (. ) 与 i386 输出不兼容

ruby - rspec 是否有比 target.should < 6 更具体的内容?

ruby-on-rails - Mechanize ruby无法看到linkedin中的所有内容

python - 我会遇到 python 的全局解释器锁的问题吗?

Python 线程和 GIL

c - 为什么标准托管 C 中的 "file"概念如此通用?

有人可以逐行解释这个简短的 C 程序的作用以及如何运行它吗?