Last active
February 25, 2019 19:03
-
-
Save tenderlove/5733632 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's replaced with
rb_thread_call_without_gvl
according to @nobu