Class: Synapse::IdentifierLockManager

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/common/concurrency/identifier_lock_manager.rb

Overview

Generic lock manager that can be used to lock identifiers for exclusive access

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeundefined



69
70
71
72
73
74
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 69

def initialize
  @locks = Hash.new
  @mutex = Mutex.new

  IdentifierLockManager.add self
end

Class Method Details

.add(instance) ⇒ 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.

Parameters:

Returns:

  • (undefined)


19
20
21
22
23
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 19

def add(instance)
  @mutex.synchronize do
    @instances[instance] = true
  end
end

.instancesArray

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:

  • (Array)


10
11
12
13
14
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 10

def instances
  @mutex.synchronize do
    @instances.keys
  end
end

.waiters_for_locks_owned_by(thread) ⇒ Set

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.

Parameters:

  • thread (Thread)

Returns:

  • (Set)


28
29
30
31
32
33
34
35
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 28

def waiters_for_locks_owned_by(thread)
  stack = Array.new
  waiters = Set.new

  find_waiters thread, instances, waiters, stack

  waiters
end

Instance Method Details

#internal_locksArray

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:

  • (Array)


116
117
118
119
120
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 116

def internal_locks
  @mutex.synchronize do
    @locks.values
  end
end

#obtain_lock(identifier) ⇒ undefined

Obtains a lock for the given identifier, blocking until the lock is obtained

Parameters:

  • identifier (Object)

Returns:

  • (undefined)


89
90
91
92
93
94
95
96
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 89

def obtain_lock(identifier)
  loop do
    lock = lock_for identifier

    return if lock.lock
    remove_lock identifier, lock
  end
end

#owned?(identifier) ⇒ Boolean

Returns true if the calling thread holds the lock for the given identifier

Parameters:

  • identifier (Object)

Returns:

  • (Boolean)


80
81
82
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 80

def owned?(identifier)
  lock_available?(identifier) && lock_for(identifier).owned?
end

#release_lock(identifier) ⇒ undefined

Releases a lock for the given identifier

Parameters:

  • identifier (Object)

Returns:

  • (undefined)

Raises:

  • (RuntimeError)

    If no lock was ever obtained for the identifier



103
104
105
106
107
108
109
110
111
112
# File 'lib/synapse/common/concurrency/identifier_lock_manager.rb', line 103

def release_lock(identifier)
  unless lock_available? identifier
    raise RuntimeError
  end

  lock = lock_for identifier
  lock.unlock

  try_dispose identifier, lock
end