Class: MasterLock::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/master_lock/registry.rb

Overview

When MasterLock acquires a lock, it registers it with a global registry. MasterLock will periodically renew all locks that are registered as long as the thread that acquired the lock is still alive and has not explicitly released the lock yet. If there is a failure to renew the lock, MasterLock identifies the lock as having already been released.

Defined Under Namespace

Classes: Registration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



20
21
22
23
# File 'lib/master_lock/registry.rb', line 20

def initialize
  @locks = []
  @locks_mutex = Mutex.new
end

Instance Attribute Details

#locksArray<Registration> (readonly)

Returns currently registered locks.

Returns:



18
19
20
# File 'lib/master_lock/registry.rb', line 18

def locks
  @locks
end

Instance Method Details

#extend_locksObject

Extend all currently registered locks that have been held longer than the extend_interval since they were last acquired/extended. If any locks have expired (should not happen), it will release them.



58
59
60
61
62
63
64
65
# File 'lib/master_lock/registry.rb', line 58

def extend_locks
  # Make a local copy of the locks array to avoid accessing it outside of the mutex.
  locks_copy = @locks_mutex.synchronize { locks.dup }
  locks_copy.each { |registration| extend_lock(registration) }
  @locks_mutex.synchronize do
    locks.delete_if(&:released)
  end
end

#register(lock, extend_interval) ⇒ Registration

Register a lock to be renewed every extend_interval seconds.

Parameters:

  • lock (#extend)

    a currently held lock that can be extended

  • extend_interval (Fixnum)

    the interval in seconds after before the lock is extended

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/master_lock/registry.rb', line 31

def register(lock, extend_interval)
  registration = Registration.new
  registration.lock = lock
  registration.mutex = Mutex.new
  registration.thread = Thread.current
  registration.acquired_at = Time.now
  registration.extend_interval = extend_interval
  registration.released = false
  @locks_mutex.synchronize do
    locks << registration
  end
  registration
end

#unregister(registration) ⇒ Object

Unregister a lock that has been registered.

Parameters:



49
50
51
52
53
# File 'lib/master_lock/registry.rb', line 49

def unregister(registration)
  registration.mutex.synchronize do
    registration.released = true
  end
end