Class: UnionStationHooks::Lock
- Inherits:
-
Object
- Object
- UnionStationHooks::Lock
- Defined in:
- lib/union_station_hooks_core/lock.rb
Overview
A wrapper around a mutex, for use within Connection.
Instance Method Summary collapse
-
#initialize(mutex) ⇒ Lock
constructor
A new instance of Lock.
- #lock ⇒ Object
- #reset(mutex, lock_now = true) ⇒ Object
- #synchronize ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(mutex) ⇒ Lock
Returns a new instance of Lock.
30 31 32 33 |
# File 'lib/union_station_hooks_core/lock.rb', line 30 def initialize(mutex) @mutex = mutex @locked = false end |
Instance Method Details
#lock ⇒ Object
50 51 52 53 54 |
# File 'lib/union_station_hooks_core/lock.rb', line 50 def lock raise if @locked @mutex.lock @locked = true end |
#reset(mutex, lock_now = true) ⇒ Object
35 36 37 38 39 |
# File 'lib/union_station_hooks_core/lock.rb', line 35 def reset(mutex, lock_now = true) unlock if @locked @mutex = mutex lock if lock_now end |
#synchronize ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/union_station_hooks_core/lock.rb', line 41 def synchronize lock if !@locked begin yield(self) ensure unlock if @locked end end |
#unlock ⇒ Object
56 57 58 59 60 |
# File 'lib/union_station_hooks_core/lock.rb', line 56 def unlock raise if !@locked @mutex.unlock @locked = false end |