Class: ZK::Locker::ExclusiveLocker
- Inherits:
-
LockerBase
- Object
- LockerBase
- ZK::Locker::ExclusiveLocker
- Defined in:
- lib/z_k/locker.rb
Overview
An exclusive lock implementation
Instance Attribute Summary
Attributes inherited from LockerBase
#lock_path, #root_lock_path, #zk
Instance Method Summary collapse
- #block_until_write_lock! ⇒ Object protected
- #got_write_lock? ⇒ Boolean protected
- #lock!(blocking = false) ⇒ Object
-
#next_lowest_node ⇒ Object
protected
the node that is next-lowest in sequence number to ours, the one we watch for updates to.
Methods inherited from LockerBase
#cleanup_lock_path!, #create_lock_path!, #create_root_path!, #digit_from, digit_from_lock_path, #in_waiting_status, #initialize, #lock_basename, #lock_children, #locked?, #ordered_lock_children, #unlock!, #waiting?, #with_lock
Constructor Details
This class inherits a constructor from ZK::Locker::LockerBase
Instance Method Details
#block_until_write_lock! ⇒ Object (protected)
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/z_k/locker.rb', line 241 def block_until_write_lock! begin path = [root_lock_path, next_lowest_node].join('/') logger.debug { "SharedLocker#block_until_write_lock! path=#{path.inspect}" } @zk.block_until_node_deleted(path) rescue WeAreTheLowestLockNumberException end @locked = true end |
#got_write_lock? ⇒ Boolean (protected)
237 238 239 |
# File 'lib/z_k/locker.rb', line 237 def got_write_lock? ordered_lock_children.first == lock_basename end |
#lock!(blocking = false) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/z_k/locker.rb', line 209 def lock!(blocking=false) return true if @locked create_lock_path!(EXCLUSIVE_LOCK_PREFIX) if got_write_lock? @locked = true elsif blocking in_waiting_status do block_until_write_lock! end else cleanup_lock_path! false end end |
#next_lowest_node ⇒ Object (protected)
the node that is next-lowest in sequence number to ours, the one we watch for updates to
228 229 230 231 232 233 234 235 |
# File 'lib/z_k/locker.rb', line 228 def next_lowest_node ary = ordered_lock_children() my_idx = ary.index(lock_basename) raise WeAreTheLowestLockNumberException if my_idx == 0 ary[(my_idx - 1)] end |