Class: UnionStationHooks::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/union_station_hooks_core/lock.rb

Overview

A wrapper around a mutex, for use within Connection.

Instance Method Summary collapse

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

#lockObject



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

#synchronizeObject



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

#unlockObject



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