Subject: [ruby-ffi] Re: Crazy thought (wrapping rb_thread_blocking_region) |
From: v01d |
Date: 1/8/10 7:51 PM |
To: ruby-ffi |
Well I think leaf cases are the usual thing when using FFI, unless you're wrapping some Ruby-aware code which re-enters the interpreter. I'd vote for an "attach_blocking_function" or something like that. On 7 ene, 22:10, Wayne Meissner <wmeiss...@gmail.com> wrote:
<rogerpack2...@gmail.com>:>> several of these calls in parallel native threads) and I just foundSupport 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 rogerdpackout 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