Class: OsCtld::Lockable::Lock
- Inherits:
-
Object
- Object
- OsCtld::Lockable::Lock
- Defined in:
- lib/osctld/lockable.rb
Constant Summary collapse
- TIMEOUT =
90
Instance Method Summary collapse
- #acquire_exclusive ⇒ Object
- #acquire_inclusive ⇒ Object
- #exclusively ⇒ Object
- #inclusively ⇒ Object
-
#initialize(object) ⇒ Lock
constructor
A new instance of Lock.
- #release_exclusive ⇒ Object
- #release_inclusive ⇒ Object
Constructor Details
#initialize(object) ⇒ Lock
Returns a new instance of Lock.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/osctld/lockable.rb', line 32 def initialize(object) @mutex = OsCtl::Lib::Mutex.new @in_held = [] @in_queued = [] @ex_queued = [] @ex = nil @allow_inclusive_after_exclusive = false @cond_ex = ConditionVariable.new @cond_in = ConditionVariable.new @object = object end |
Instance Method Details
#acquire_exclusive ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/osctld/lockable.rb', line 118 def acquire_exclusive return if @mutex.owned? && @ex == Thread.current t = Time.now is_timeout = false LockRegistry.register(@object, :exclusive, :waiting) begin @mutex.lock(TIMEOUT) rescue OsCtl::Lib::Mutex::Timeout LockRegistry.register(@object, :exclusive, :timeout) raise OsCtld::DeadlockDetected.new(@object, :exclusive) end if @in_held.empty? @allow_inclusive_after_exclusive = false @ex = Thread.current LockRegistry.register(@object, :exclusive, :locked) elsif @in_held.include?(Thread.current) @mutex.unlock raise 'attempted to acquire exclusive lock while holding inclusive lock' else @ex_queued << Thread.current # Wait for all inclusive blocks to finish LockRegistry.register(@object, :exclusive, :waiting) loop do now = Time.now if (now - t) >= TIMEOUT is_timeout = true break elsif @in_held.empty? && @ex.nil? break end @cond_ex.wait(@mutex, TIMEOUT - (now - t)) end if (!@in_held.empty? || @ex) && is_timeout LockRegistry.register(@object, :exclusive, :timeout) @mutex.unlock raise OsCtld::DeadlockDetected.new(@object, :exclusive) else @allow_inclusive_after_exclusive = false @ex = @ex_queued.shift LockRegistry.register(@object, :exclusive, :locked) end end end |
#acquire_inclusive ⇒ Object
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/osctld/lockable.rb', line 44 def acquire_inclusive t = Time.now is_timeout = false sync do if @ex_queued.any? && !@allow_inclusive_after_exclusive @in_queued << Thread.current # Wait for the exclusive lock to finish, if there is one LockRegistry.register(@object, :inclusive, :waiting) loop do now = Time.now if (now - t) >= TIMEOUT is_timeout = true break elsif @ex.nil? && (@allow_inclusive_after_exclusive || @ex_queued.empty?) break end @cond_in.wait(@mutex, TIMEOUT - (now - t)) end if (@ex || (!@allow_inclusive_after_exclusive && @ex_queued.any?)) && is_timeout LockRegistry.register(@object, :inclusive, :timeout) raise OsCtld::DeadlockDetected.new(@object, :inclusive) else @in_queued.delete(Thread.current) @in_held << Thread.current LockRegistry.register(@object, :inclusive, :locked) end else @in_held << Thread.current LockRegistry.register(@object, :inclusive, :locked) end end end |
#exclusively ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/osctld/lockable.rb', line 197 def exclusively if @mutex.owned? && @ex == Thread.current yield else acquire_exclusive begin yield ensure release_exclusive end end end |
#inclusively ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/osctld/lockable.rb', line 98 def inclusively return yield if @mutex.owned? && @ex == Thread.current held = false sync { held = @in_held.include?(Thread.current) } if held yield else acquire_inclusive begin yield ensure release_inclusive end end end |
#release_exclusive ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/osctld/lockable.rb', line 174 def release_exclusive unless @mutex.owned? raise 'expected to own the mutex, have you called acquire_exclusive first?' end # Leave exlusive block, signal waiting inclusive blocks to continue @ex = nil LockRegistry.register(@object, :exclusive, :unlocked) # Give the first chance to a round of inclusive locks, then exclusive # ones if @in_queued.any? @allow_inclusive_after_exclusive = true @cond_in.broadcast elsif @ex_queued.any? @allow_inclusive_after_exclusive = false @cond_ex.signal end @mutex.unlock end |
#release_inclusive ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/osctld/lockable.rb', line 85 def release_inclusive sync do @in_held.delete(Thread.current) LockRegistry.register(@object, :inclusive, :unlocked) # Start exclusive block, if there is one waiting if @in_held.empty? && @ex_queued.any? @allow_inclusive_after_exclusive = false @cond_ex.signal end end end |