1

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.

Rojj
  • 1,170
  • 1
  • 12
  • 32

1 Answers1

1

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)
matt
  • 78,533
  • 8
  • 163
  • 197
  • Very clear thanks. Re: Ruby version. I need to use this version because it is embedded in the software (SketchUp) for which I am writing a plugin. Also, I think I better understand the issue in [this](http://stackoverflow.com/questions/36245878/releasing-the-global-vm-lock-in-a-c-extension-without-using-another-function) question. I am now trying to return the result to Ruby. If my understanding is correct I need to return a `void *` and then cast it back to `double` in `double_no_gvl`. – Rojj May 06 '16 at 08:18
  • 1
    @Rojj yes, but be careful about allocation. You shouldn’t return a pointer to a local variable to a calling function, as in the calling function the pointer will point to somewhere off the stack (and will likely be overwritten the next time you call a function). It _might_ work if you unpack it immediatley, but would be risky. Instead you’ll need to `malloc` some storage (and `free` it in the calling function), or possibly pass a pointer to a struct that contains the arguments you need and allows you to set the result in it (rather than returning the result). – matt May 06 '16 at 15:47