lib/zk/locker/locker_base.rb in zk-1.5.3 vs lib/zk/locker/locker_base.rb in zk-1.6.0

- old
+ new

@@ -127,15 +127,17 @@ # @note There is more than one way you might not "own the lock" # see [issue #34](https://github.com/slyphon/zk/issues/34) # def unlock rval = false - synchronize do + @mutex.synchronize do if @locked + logger.debug { "unlocking" } rval = cleanup_lock_path! @locked = false @node_deletion_watcher = nil + @cond.broadcast end end rval end @@ -174,21 +176,31 @@ # returns true if this locker is waiting to acquire lock # this should be used in tests only. # # @private def waiting? - synchronize do + @mutex.synchronize do !!(@node_deletion_watcher and @node_deletion_watcher.blocked?) end end # blocks the caller until this lock is blocked # @private def wait_until_blocked(timeout=nil) - synchronize do - @cond.wait_until { @node_deletion_watcher } + time_to_stop = timeout ? (Time.now + timeout) : nil + + @mutex.synchronize do + if @node_deletion_watcher + logger.debug { "@node_deletion_watcher already assigned, not waiting" } + else + logger.debug { "going to wait up to #{timeout} sec for a @node_deletion_watcher to be assigned" } + + @cond.wait(timeout) + raise "Timeout waiting for @node_deletion_watcher" unless @node_deletion_watcher + end end + logger.debug { "ok, @node_deletion_watcher: #{@node_deletion_watcher}, going to call wait_until_blocked" } @node_deletion_watcher.wait_until_blocked(timeout) end # This is for users who wish to check that the assumption is correct @@ -218,11 +230,11 @@ # puts "hah! he thinks we're workin!" # sleep(60) # end # def assert! - synchronize do + @mutex.synchronize do raise LockAssertionFailedError, "have not obtained the lock yet" unless locked? raise LockAssertionFailedError, "not connected" unless zk.connected? raise LockAssertionFailedError, "lock_path was #{lock_path.inspect}" unless lock_path raise LockAssertionFailedError, "the lock path #{lock_path} did not exist!" unless zk.exists?(lock_path) raise LockAssertionFailedError, "the parent node was replaced!" unless root_lock_path_same? @@ -237,15 +249,10 @@ def digit_from(path) self.class.digit_from_lock_path(path) end - # possibly lighter weight check to see if the lock path has any children - # (using stat, rather than getting the list of children). - def any_lock_children? - end - def lock_children(watch=false) zk.children(root_lock_path, :watch => watch) end def ordered_lock_children(watch=false) @@ -273,11 +280,11 @@ # this method also saves the stat of root_lock_path at the time of creation # to ensure we don't accidentally remove a lock we don't own. see # [rule #34](https://github.com/slyphon/zk/issues/34)...er, *issue* #34. # def create_lock_path!(prefix='lock') - synchronize do + @mutex.synchronize do @lock_path = @zk.create("#{root_lock_path}/#{prefix}", :mode => :ephemeral_sequential) @parent_stat = @zk.stat(root_lock_path) end logger.debug { "got lock path #{@lock_path}" } @@ -292,11 +299,11 @@ # that we actually own the lock_path # # see [issue #34](https://github.com/slyphon/zk/issues/34) # def root_lock_path_same? - synchronize do + @mutex.synchronize do return false unless @parent_stat cur_stat = zk.stat(root_lock_path) cur_stat.exists? and (cur_stat.ctime == @parent_stat.ctime) end @@ -309,10 +316,10 @@ # later and get to it # def cleanup_lock_path! rval = false - synchronize do + @mutex.synchronize do if root_lock_path_same? logger.debug { "removing lock path #{@lock_path}" } zk.delete(@lock_path, :ignore => :no_node) zk.delete(root_lock_path, :ignore => [:not_empty, :no_node])