From 63fab09ea005b8243d1c2d3967dc39f57a00d355 Mon Sep 17 00:00:00 2001 From: John Bender Date: Mon, 8 Feb 2010 22:54:21 -0800 Subject: [PATCH] Hobo.busy refactor and mutex fix --- lib/hobo/busy.rb | 27 ++++++++++++++++----------- test/hobo/busy_test.rb | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/hobo/busy.rb b/lib/hobo/busy.rb index c36fb7478..309dbb4e6 100644 --- a/lib/hobo/busy.rb +++ b/lib/hobo/busy.rb @@ -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 diff --git a/test/hobo/busy_test.rb b/test/hobo/busy_test.rb index 25be668a0..fc7f2a3a0 100644 --- a/test/hobo/busy_test.rb +++ b/test/hobo/busy_test.rb @@ -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