Subject:
Re: [ruby-ffi] native thread callback woes
From:
Wayne Meissner
Date:
3/19/10 4:17 PM
To:
ruby-ffi@googlegroups.com

On 20 March 2010 03:36, Shawn Anderson <shawn42@gmail.com> wrote:
> I'm trying to get allegro-ffi to work with the latest 4.9 branch of allegro
> in SVN.  Allegro has recently changed to run everything in a native
> callback.
> ie..
> ... setup code ...
> al_run_main argc, argv, &method(:run)
> puts "this code will never be called"
> running this in ruby 1.9.1 I see the error:
>
> examples/simple_window.rb:13: [BUG] object allocation during garbage
> collection phase
>
> ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
> full trace here:
> https://gist.github.com/95d3ccc14fd8fdc1febf
>
>
> Allegro spins off a new native thread (os x only) and calls the callback
> from that native thread.  Apparently os x needs the main thread for its
> event stuff...
> Is there anyway to let this new non-ruby native thread call back into ruby
> code?

I asked around, and the consensus is "don't do threading with MRI".

The functions for doing this are a horrible rats nest of
quasi-documented functions marked as "experimental", "use at own risk"
and "may not be supported in 1.9.2".

The good news is that JRuby should work ok (but you'll want to check
that), because it has sane threading.

There's also an additional problem - al_run_main() won't be releasing
the GIL while it waits for events.  There is support in FFI for doing
this, but its not exposed in the attach_function interface (look at
spec/ffi/function_spec.rb and search for blocking).

I have ideas on how to make the async callbacks work on MRI (1.9.x
only - 1.8.x is just not possible), but it won't be this weekend.  Put
it in a github issue so its not forgotten.

To unsubscribe from this group, send email to ruby-ffi+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.