lib/zk/locker/shared_locker.rb in zk-1.8.0 vs lib/zk/locker/shared_locker.rb in zk-1.9.0

- old
+ new

@@ -21,100 +21,29 @@ # def assert! super end - # (see LockerBase#locked?) - def locked? - false|@locked - end - # (see LockerBase#acquirable?) def acquirable? return true if locked? - !lock_children.any? { |n| n.start_with?(EXCLUSIVE_LOCK_PREFIX) } + blocking_locks.empty? rescue Exceptions::NoNode true end # @private - def lock_number - lock_path and digit_from(lock_path) - end - - # returns the sequence number of the next lowest write lock node - # - # raises NoWriteLockFoundException when there are no write nodes with a - # sequence less than ours - # - # @private - def next_lowest_write_lock_num - digit_from(next_lowest_write_lock_name) - end - - # the next lowest write lock number to ours - # - # so if we're "read010" and the children of the lock node are: - # - # %w[write008 write009 read010 read011] - # - # then this method will return write009 - # - # raises NoWriteLockFoundException if there were no write nodes with an - # index lower than ours - # - # @private - def next_lowest_write_lock_name - ary = ordered_lock_children() - my_idx = ary.index(lock_basename) # our idx would be 2 - - ary[0..my_idx].reverse.find { |n| n.start_with?(EXCLUSIVE_LOCK_PREFIX) }.tap do |rv| - raise NoWriteLockFoundException if rv.nil? + def lower_write_lock_names + lower_lock_names.select do |lock| + lock.start_with?(EXCLUSIVE_LOCK_PREFIX) end end + alias :blocking_locks :lower_write_lock_names # @private - def got_read_lock? - false if next_lowest_write_lock_num - rescue NoWriteLockFoundException - true + def lock_prefix + SHARED_LOCK_PREFIX end - alias got_lock? got_read_lock? - private - def lock_with_opts_hash(opts) - create_lock_path!(SHARED_LOCK_PREFIX) - - lock_opts = LockOptions.new(opts) - - if got_read_lock? - @mutex.synchronize { @locked = true } - elsif lock_opts.blocking? - block_until_read_lock!(:timeout => lock_opts.timeout) - else - false - end - ensure - cleanup_lock_path! unless @mutex.synchronize { @locked } - end - - def block_until_read_lock!(opts={}) - begin - path = "#{root_lock_path}/#{next_lowest_write_lock_name}" - logger.debug { "SharedLocker#block_until_read_lock! path=#{path.inspect}" } - - @mutex.synchronize do - @node_deletion_watcher = NodeDeletionWatcher.new(zk, path) - @cond.broadcast - end - - @node_deletion_watcher.block_until_deleted(opts) - rescue NoWriteLockFoundException - # next_lowest_write_lock_name may raise NoWriteLockFoundException, - # which means we should not block as we have the lock (there is nothing to wait for) - end - - @mutex.synchronize { @locked = true } - end end # SharedLocker end # Locker end # ZK