Search code examples
crubygilruby-c-extension

Ruby Global Interpreter Lock (GIL) - rb_thread_call_without_gvl


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.


Solution

  • If you haven’t seen it already, the source for rb_thread_call_without_gvl includes some documentation. (I’ve linked to the version that you’re using, but that’s a pretty old Ruby, you should look at updating if possible. This API is the same in current versions at least up to 2.3.1.)

    The function prototype looks like:

    void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
                 rb_unblock_function_t *ubf, void *data2);
    

    The function that is being called should accept a single void * argument and return void *, i.e. it is a plain C function, not a C function implementing a Ruby method as you have in your example. In fact it can’t implement a Ruby method as doing so would mean accessing structures protected by the GVL.

    To use it you need to move the code you want to execute without the lock into a function with the correct interface, and that doesn’t use any of the Ruby API. Here is an example (based on your own) that creates a Ruby method that doubles the argument passed to it, and does the work without the 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);
    }
    

    You can call it with a script like:

    require './csum'
    obj = MyModule::MyClass.new
    obj.double_no_gvl(3.9)