Hobo.busy refactor and mutex fix
This commit is contained in:
parent
3ccdaf9182
commit
63fab09ea0
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue