Class: Synapse::IdentifierLock

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

Overview

TODO:

Deadlock detection

Generic implementation of a lock that can be used to lock an identifier for use

Instance Method Summary collapse

Constructor Details

#initializeundefined



6
7
8
9
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 6

def initialize
  @identifiers = Hash.new
  @lock = Mutex.new
end

Instance Method Details

#obtain_lock(identifier) ⇒ undefined

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

Parameters:

  • identifier (Object)

Returns:

  • (undefined)


23
24
25
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 23

def obtain_lock(identifier)
  lock_for(identifier).lock
end

#owned?(identifier) ⇒ Boolean

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

Parameters:

  • identifier (Object)

Returns:

  • (Boolean)


15
16
17
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 15

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

#release_lock(identifier) ⇒ undefined

Releases a lock for the given identifier

Parameters:

  • identifier (Object)

Returns:

  • (undefined)

Raises:

  • (ThreadError)

    If no lock was ever obtained for the identifier



32
33
34
35
36
37
38
# File 'lib/synapse/common/concurrency/identifier_lock.rb', line 32

def release_lock(identifier)
  unless lock_available? identifier
    raise ThreadError, 'No lock for this identifier was ever obtained'
  end

  lock_for(identifier).unlock
end