Hobo.busy refactor and mutex fix

This commit is contained in:
John Bender 2010-02-08 22:54:21 -08:00
parent 3ccdaf9182
commit 63fab09ea0
2 changed files with 17 additions and 12 deletions

View File

@ -4,21 +4,12 @@ module Hobo
end
def self.busy(&block)
Mutex.new.synchronize do
Busy.busy = true
yield
Busy.busy = false
end
# In the case were an exception is thrown by the wrapped code
# make sure to set busy to sane state and reraise the error
rescue Exception => e
Busy.busy = false
raise
Busy.busy(&block)
end
class Busy
@@busy = false
@@mutex = Mutex.new
class << self
def busy?
@@busy
@ -27,6 +18,20 @@ module Hobo
def busy=(val)
@@busy = val
end
def busy(&block)
@@mutex.synchronize do
Busy.busy = true
yield
Busy.busy = false
end
# In the case were an exception is thrown by the wrapped code
# make sure to set busy to sane state and reraise the error
rescue Exception => e
Busy.busy = false
raise
end
end
end
end

View File

@ -26,7 +26,7 @@ class BusyTest < Test::Unit::TestCase
should "report busy to the outside world regardless of thread" do
Thread.new do
Hobo.busy do
sleep(10)
sleep(1)
end
end