Class: Synapse::IdentifierLock
- Inherits:
-
Object
- Object
- Synapse::IdentifierLock
- Defined in:
- lib/synapse/common/concurrency/identifier_lock.rb
Overview
TODO:
Deadlock detection
Generic implementation of a lock that can be used to lock an identifier for use
Instance Method Summary collapse
- #initialize ⇒ undefined constructor
-
#obtain_lock(identifier) ⇒ undefined
Obtains a lock for the given identifier, blocking until the lock is obtained.
-
#owned?(identifier) ⇒ Boolean
Returns true if the calling thread holds the lock for the given identifier.
-
#release_lock(identifier) ⇒ undefined
Releases a lock for the given identifier.
Constructor Details
#initialize ⇒ undefined
6 7 8 9 |
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 6 def initialize @identifiers = Hash.new @lock = Mutex.new end |
Instance Method Details
#obtain_lock(identifier) ⇒ undefined
Obtains a lock for the given identifier, blocking until the lock is obtained
23 24 25 |
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 23 def obtain_lock(identifier) lock_for(identifier).lock end |
#owned?(identifier) ⇒ Boolean
Returns true if the calling thread holds the lock for the given identifier
15 16 17 |
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 15 def owned?(identifier) lock_available?(identifier) and lock_for(identifier).owned? end |
#release_lock(identifier) ⇒ undefined
Releases a lock for the given identifier
32 33 34 35 36 37 38 |
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 32 def release_lock(identifier) unless lock_available? identifier raise ThreadError, 'No lock for this identifier was ever obtained' end lock_for(identifier).unlock end |