Class: Synapse::DisposableLock Private
- Inherits:
-
Object
- Object
- Synapse::DisposableLock
- Defined in:
- lib/synapse/common/concurrency/disposable_lock.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
- #closed ⇒ Boolean (also: #closed?) readonly private
- #owner ⇒ Thread readonly private
- #waiters ⇒ Array readonly private
Instance Method Summary collapse
- #initialize ⇒ undefined constructor private
-
#lock ⇒ Boolean
private
False if lock has been closed.
- #locked? ⇒ Boolean private
- #owned? ⇒ Boolean private
- #owned_by?(thread) ⇒ Boolean private
-
#try_close ⇒ Boolean
private
True if lock was closed.
- #try_lock ⇒ Boolean private
- #unlock ⇒ undefined private
Constructor Details
#initialize ⇒ 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.
16 17 18 19 20 21 22 23 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 16 def initialize @mutex = Mutex.new @closed = false @hold_count = 0 @owner = nil @waiters = Array.new end |
Instance Attribute Details
#closed ⇒ Boolean (readonly) Also known as: closed?
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.
5 6 7 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 5 def closed @closed end |
#owner ⇒ Thread (readonly)
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 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 10 def owner @owner end |
#waiters ⇒ Array (readonly)
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.
13 14 15 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 13 def waiters @waiters end |
Instance Method Details
#lock ⇒ Boolean
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.
Returns False if lock has been closed.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 42 def lock @mutex.synchronize do unless owned? if @hold_count == 0 @owner = Thread.current else @waiters.push Thread.current begin wait_for_lock ensure @waiters.delete Thread.current end return unless owned? end end @hold_count += 1 end if closed? unlock return false end return true end |
#locked? ⇒ Boolean
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.
37 38 39 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 37 def locked? @hold_count > 0 end |
#owned? ⇒ Boolean
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.
26 27 28 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 26 def owned? @owner == Thread.current end |
#owned_by?(thread) ⇒ Boolean
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.
32 33 34 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 32 def owned_by?(thread) @owner == thread end |
#try_close ⇒ Boolean
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.
Returns True if lock was closed.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 103 def try_close return false unless try_lock begin if @hold_count == 1 @closed = true return true end return false ensure unlock end end |
#try_lock ⇒ Boolean
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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 72 def try_lock @mutex.synchronize do unless owned? if @hold_count == 0 @owner = Thread.current else return false end end @hold_count += 1 return true end end |
#unlock ⇒ 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.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 89 def unlock @mutex.synchronize do raise RuntimeError unless owned? @hold_count -= 1 if @hold_count == 0 @owner = nil wakeup_next_waiter end end end |