Subject:
Re: [ruby-ffi] Re: Crazy thought (wrapping rb_thread_blocking_region)
From:
Wayne Meissner
Date:
1/7/10 7:10 PM
To:
ruby-ffi@googlegroups.com

Support for this is technically there, however it is not expressable
using attach_function.

e.g.

lib = DynamicLibrary.open("libc.so", DynamicLibrary.RTLD_LAZY|
DynamicLibrary.RTLD_GLOBAL)
addr = lib.find_function("sleep")

# :blocking => true tells FFI the function will block, so it releases the GIL
fn = Function.new(:int, [ :int ], addr, { :blocking => true })
module Sleep
  fn.attach(self, "libc_sleep")
end

Or something like that.  There is a spec that tests the functionality,
which you might want to look at in case I got something wrong above.

I'm not sure I want to make this easier.  On CRuby, it is really easy
to do nasty things if you're not careful with this, e.g. if a blocking
function calls a ruby callback, very bad things will happen.  So you
really only want to use it for leaf functions.

The real solution of course, is to upgrade to JRuby - it handles all
this blocking stuff automagically.


2010/1/8 rogerdpack <rogerpack2005@gmail.com>:
>> several of these calls in parallel native threads) and I just found
>> out about Ruby's "rb_thread_blocking_region" C call.
>
> It might be nice if FFI would optionally wrap its calls in an
> rb_thread_blocking_region (just on 1.9, I guess).
> -r
>