Skip to content

Instantly share code, notes, and snippets.

@robin850

robin850/b.rb Secret

Created March 1, 2015 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin850/3eeaed2538f50a9887c2 to your computer and use it in GitHub Desktop.
Save robin850/3eeaed2538f50a9887c2 to your computer and use it in GitHub Desktop.
Rubinius - Constant autoloading and thread-safety
module A
class B
autoload :C, 'c'
def foo
C
end
end
end
module A
class B
class C
end
end
end
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