Why GIL is not released on ruby/dl external function call

Hello.

if i run following test code:

require ‘dl/import’
module User32
extend DL::Importer
dlload ‘user32’
extern ‘int MessageBoxA(int, char*, char*, int)’
end
Thread.new { User32.MessageBoxA( 0, “Text”, “Caption”, 0 ) }
loop { sleep 1; puts( “loop running” ) }

I will see no “loop running” messages while message box is visible. So
i can suggest that Ruby is not releasing GIL while calling external
function via Ruby\dl. Why such weird architectural decision? This
prevents me from calling blocking functions in background thread :(.

That’s kind of the point of the GIL.

On Tue, Feb 14, 2012 at 2:11 PM, Grigory P. [email protected]
wrote:

Thread.new { User32.MessageBoxA( 0, “Text”, “Caption”, 0 ) }
loop { sleep 1; puts( “loop running” ) }

I will see no “loop running” messages while message box is visible. So
i can suggest that Ruby is not releasing GIL while calling external
function via Ruby\dl. Why such weird architectural decision? This
prevents me from calling blocking functions in background thread :(.

Since Ruby does not know what your external C function does (it could
manipulate Ruby objects!) it takes a conservative approach and locks.

Cheers

robert

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/10252

Thanks!

Any way to unlock GIL with on external code call? Or do i need to use
some external gems like FFI? (external gems are not very good since
majority of end users don’t have enough qualification to install them,
and AFAIK ruby can’t auto-install gem dependency on script run).

On Tue, Feb 14, 2012 at 8:08 PM, Robert K.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs