Last active
February 25, 2019 19:03
-
-
Save tenderlove/5733632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fiddle' | |
class GVL | |
handle = Fiddle::Handle::DEFAULT | |
address = handle['rb_thread_blocking_region'] | |
func = Fiddle::Function.new address, [Fiddle::TYPE_VOIDP, | |
Fiddle::TYPE_VOIDP, | |
Fiddle::TYPE_VOIDP, | |
Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP | |
define_method(:unlocked) do |&block| | |
closure = Class.new(Fiddle::Closure) { | |
define_method(:call) { |ptr| | |
Fiddle.dlwrap block.call | |
} | |
}.new(Fiddle::TYPE_VOIDP, [Fiddle::TYPE_VOIDP]) | |
func.call(closure, nil, nil, nil).to_value | |
end | |
end | |
def fib n | |
if n < 3 | |
1 | |
else | |
fib(n-1) + fib(n-2) | |
end | |
end | |
gvl = GVL.new | |
p gvl.unlocked { fib(34) } |
Now, rb_thread_blocking_region is obsolete.
it's replaced with rb_thread_call_without_gvl
according to @nobu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@luikore,
rb_thread_blocking_region
callsrb_thread_call_without_gvl
but with some extra args (one i.e. is a signal in case Ruby needs to kill the thread) and returns aVALUE
.