From: Charles Oliver Nutter Date: 2010-12-21T22:17:20+09:00 Subject: Re: Ruby and science ? On Tue, Dec 21, 2010 at 3:58 AM, Martin DeMello wrote: > How do FFI wrappers handle concurrent running? (As in, how do they > differ from C extensions in that respect?) At least in JRuby, FFI calls are not prevented from firing in parallel. That requires you to ensure the library you're using is concurrency-safe (most mature C libraries are, usually by passing state around), but it's a far better situation than you have with C extensions where concurrent mutation is often the norm. It might be possible in the future to have a API or contract by which a C extension can be "promoted" to "threadsafe mode", but at the moment, all implementations of the C extension API prevent extension code from running concurrently (and therefore prevent concurrent use of the libraries they bind). This is a very large limitation for any Ruby implementations that support thread-level concurrency, like JRuby, and so we generally discourage C extension use if an alternative is available. It's also worth noting that C extensions usually maintain global state that prevents them from being used with multiple Ruby instances in a single process. JRuby has always had the ability to spin up extra JRuby instances inside a single JVM, but we can't do this when loading C extensions because they usually hold hard global references to symbols, classes, etc that are instance-specific. This also afflicts MRI, and the "multi-VM" work required adding a special new extension entry point whereby extensions "promise" they're MVM-friendly. So C extensions prevent not just concurrency but also MVM. FFI on the other hand only requires that it can load the library. The bindings happen in user code, so the same library can be loaded and called from multiple JRuby instances. - Charlie