-
-
Save DAddYE/5733699 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' | |
require 'fiddle/import' | |
module GVL | |
include Fiddle | |
extend Importer | |
dlload Handle::DEFAULT | |
T = import_function 'rb_thread_call_without_gvl', TYPE_VOIDP, | |
[TYPE_VOIDP, TYPE_VOIDP, TYPE_VOIDP, TYPE_VOIDP] | |
def self.without_blocking(&block) | |
cl = Closure::BlockCaller.new(TYPE_VOIDP, [TYPE_VOIDP]){ Fiddle.dlwrap(block.call) } | |
T.call(cl, nil, nil, nil).to_value | |
end | |
# Just for tests | |
def self.blocking(&block) | |
block.call | |
end | |
end | |
if $0 == __FILE__ | |
require 'benchmark' | |
def fib(n) | |
Handler.fib | |
n < 2 ? n : fib(n-1) + fib(n-2) | |
end | |
t = Benchmark.realtime do | |
t1 = Thread.new { sleep 1; loop { STDOUT.write "."; }} | |
t2 = Thread.new { sleep 1; loop { STDOUT.write "x"; }} | |
t3 = Thread.new { GVL.blocking { fib(36) } } # <<<<<<<<<<< switch GVL.blocking -> GVL.without_blocking | |
t3.join | |
[t1, t2].each(&:kill) | |
end | |
puts "in #{t}" | |
end |
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
# Blocking others threads, there is few time for scheduling | |
experiments $ ruby gvl.rb | |
......................................................................x.........xxx.x.xxxxxxxxxxxxx | |
in 2.798758 | |
# Using a no blocking version you'll see that there is more time for other threads to run: | |
................................................................................................... | |
.x....xxxxxxx.xxxxxxxxxxxxxx..x.x..xxxx............xxxx........x.............xx.xxxxxxxxxxxxx....xx | |
xxxxxxxxxxxxxxxxxxx....x.x.......................xxxx..xxxxxxxxxxxxxxxxxxxxx....xx...xxxx......xxx. | |
xx.x.....x.xx..x..............xx.......x...xx.x.xx.........xxx.x....x.............................. | |
............xxxx.x....xxx......x........xxxxxxxxxxxxxxxxxx...xxx.....xxx....xx...x.x...xxxxxxx.xxxx | |
xxxxxxxxx......xx...........xxx.................x...xx.................x.x.xxx........x.xxxxxxx.x.. | |
xxx.xx....x...x..xx.........xxx...x.........x...................xxxx.....xxx.....x......xx.xxxxxxxx | |
xxxxxxxxxxxxxx..xxxxxxx..x............................xxx.x......xxxxxxxxxxxxxx........xxxxxxxxxxx. | |
...........x............xxxxxxxxxxxx.xx.x.........x......xxx..xx.xx...x......xxxxxxxxxxx.xx...xxx.x | |
x.....xxxxxxxxxx......xxx.........x.x...x.xxxxxxx.x.....xxxx....xx..xxx.xxx.xxxxxxxxx.......xxxxxx. | |
xxxxxxxx...............x.x.xxxxxx.xxxxxxxxxx..x.x.xx..xxxxxx.........xxx.x....x.xx..x.xxxx..x.x.xxx | |
.xxx................xxxx.x...x....xx.................xxxxxxxx.x.....x..xxxxxxx.....xxxxxxxxxxxxxxxx | |
x...x.x...xxxxxxx.xxxxxxxx..x....x..xxx...xxxx.x.xxx.x.xx...xxxxxx.xxxxxx..xxxx....xxxx.xxxxxx.xxxx | |
x...xxxx................x....x................xxx....xxxxxxxxxxxx.xxxxx..xxx.x.xxxxxxxxxxxxxxxxxxxx | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxx...xxxxxxxx..................xxx.xxxxxxxxxxxxxxxxxxxx.xxx.xxxx......... | |
in 2.6518364 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment