Class: Synapse::IdentifierLockManager
- Inherits:
-
Object
- Object
- Synapse::IdentifierLockManager
- Defined in:
- lib/synapse/common/concurrency/identifier_lock_manager.rb
Overview
Generic lock manager that can be used to lock identifiers for exclusive access
Class Method Summary collapse
- .add(instance) ⇒ undefined private
- .instances ⇒ Array private
- .waiters_for_locks_owned_by(thread) ⇒ Set private
Instance Method Summary collapse
- #initialize ⇒ undefined constructor
- #internal_locks ⇒ Array private
-
#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
69 70 71 72 73 74 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 69 def initialize @locks = Hash.new @mutex = Mutex.new IdentifierLockManager.add self end |
Class Method Details
.add(instance) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
19 20 21 22 23 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 19 def add(instance) @mutex.synchronize do @instances[instance] = true end end |
.instances ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 13 14 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 10 def instances @mutex.synchronize do @instances.keys end end |
.waiters_for_locks_owned_by(thread) ⇒ Set
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
28 29 30 31 32 33 34 35 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 28 def waiters_for_locks_owned_by(thread) stack = Array.new waiters = Set.new find_waiters thread, instances, waiters, stack waiters end |
Instance Method Details
#internal_locks ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
116 117 118 119 120 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 116 def internal_locks @mutex.synchronize do @locks.values end end |
#obtain_lock(identifier) ⇒ undefined
Obtains a lock for the given identifier, blocking until the lock is obtained
89 90 91 92 93 94 95 96 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 89 def obtain_lock(identifier) loop do lock = lock_for identifier return if lock.lock remove_lock identifier, lock end end |
#owned?(identifier) ⇒ Boolean
Returns true if the calling thread holds the lock for the given identifier
80 81 82 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 80 def owned?(identifier) lock_available?(identifier) && lock_for(identifier).owned? end |
#release_lock(identifier) ⇒ undefined
Releases a lock for the given identifier
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 103 def release_lock(identifier) unless lock_available? identifier raise RuntimeError end lock = lock_for identifier lock.unlock try_dispose identifier, lock end |