From 8c6a30795a0c22826f5d6f6ebc4b49e337e0ae4e Mon Sep 17 00:00:00 2001 From: Aleksei Gusev Date: Tue, 20 Sep 2011 21:54:21 +0300 Subject: [PATCH] Change Guard::Interactor#lock and #unlock methods so they will lock interactor in the right thread and free $stdin [closes #137]. --- lib/guard/interactor.rb | 49 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/guard/interactor.rb b/lib/guard/interactor.rb index 0abbb5a..e660fe6 100644 --- a/lib/guard/interactor.rb +++ b/lib/guard/interactor.rb @@ -1,6 +1,9 @@ module Guard class Interactor + class LockException < Exception; end + class UnlockException < Exception; end + attr_reader :locked def initialize @@ -11,35 +14,43 @@ module Guard return if ENV["GUARD_ENV"] == 'test' @thread = Thread.new do loop do - if (entry = $stdin.gets) && !@locked - entry.gsub! /\n/, '' - case entry - when 'stop', 'quit', 'exit', 's', 'q', 'e' - ::Guard.stop - when 'reload', 'r', 'z' - ::Guard.reload - when 'pause', 'p' - ::Guard.pause - else - ::Guard.run_all + begin + if !@locked && (entry = $stdin.gets) + entry.gsub! /\n/, '' + case entry + when 'stop', 'quit', 'exit', 's', 'q', 'e' + ::Guard.stop + when 'reload', 'r', 'z' + ::Guard.reload + when 'pause', 'p' + ::Guard.pause + else + ::Guard.run_all + end end + rescue LockException + lock + rescue UnlockException + unlock end end end end - def stop - @thread.kill - end - def lock - @locked = true - stop + if @thread == Thread.current + @locked = true + else + @thread.raise(LockException) + end end def unlock - @locked = false - start + if @thread == Thread.current + @locked = false + else + @thread.raise(UnlockException) + end end end