Class: Clasp::LockManager
- Inherits:
-
Object
- Object
- Clasp::LockManager
- Defined in:
- lib/clasp/lock_manager.rb
Instance Method Summary collapse
- #initialize ⇒ undefined constructor
-
#lock(identifier) ⇒ undefined
Obtains the lock for the given identifier.
- #locks ⇒ Enumerable private
-
#owned?(identifier) ⇒ Boolean
Returns true if the calling thread holds the lock for the given identifier.
-
#unlock(identifier) ⇒ undefined
Releases the lock for the given identifier.
Constructor Details
#initialize ⇒ undefined
4 5 6 7 |
# File 'lib/clasp/lock_manager.rb', line 4 def initialize @locks = ThreadSafe::Cache.new LockManagerTracker.register(self) end |
Instance Method Details
#lock(identifier) ⇒ undefined
Obtains the lock for the given identifier
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/clasp/lock_manager.rb', line 15 def lock(identifier) obtained = false until obtained lock = @locks.compute_if_absent(identifier) { DisposableLock.new(self, identifier) } obtained = lock.lock unless obtained @locks.delete_pair(identifier, lock) end end end |
#locks ⇒ Enumerable
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.
58 59 60 |
# File 'lib/clasp/lock_manager.rb', line 58 def locks @locks.values end |
#owned?(identifier) ⇒ Boolean
Returns true if the calling thread holds the lock for the given identifier
32 33 34 35 |
# File 'lib/clasp/lock_manager.rb', line 32 def owned?(identifier) lock = @locks.get(identifier) lock && lock.owned? end |
#unlock(identifier) ⇒ undefined
Releases the lock for the given identifier
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/clasp/lock_manager.rb', line 43 def unlock(identifier) unless @locks.key?(identifier) raise IllegalLockUsageError, "Unknown lock #{identifier}" end lock = @locks.get(identifier) lock.unlock if lock.closed? @locks.delete_pair(identifier, lock) end end |