/b.rb Secret
Created
March 1, 2015 13:00
Rubinius - Constant autoloading and thread-safety
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
module A | |
class B | |
autoload :C, 'c' | |
def foo | |
C | |
end | |
end | |
end |
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
module A | |
class B | |
class C | |
end | |
end | |
end | |
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 'monitor' | |
module A | |
autoload :B, 'b' | |
end | |
require 'b' | |
class Foo | |
include MonitorMixin | |
attr_accessor :b | |
def initialize | |
super | |
@cv = new_cond | |
@committed = false | |
end | |
def await_commit | |
synchronize do | |
@cv.wait_until { @committed } | |
end | |
end | |
def commit! | |
synchronize do | |
@committed = true | |
@cv.broadcast | |
end | |
end | |
def process | |
error = nil | |
Thread.new { | |
begin | |
# Line loading the A::B::C constant | |
b.foo | |
rescue => e | |
error = e | |
ensure | |
commit! | |
end | |
} | |
await_commit | |
raise error if error | |
end | |
end | |
foo = Foo.new | |
foo.b = A::B.new | |
foo.process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment