Class: Clasp::LockManager

Inherits:
Object
  • Object
show all
Defined in:
lib/clasp/lock_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeundefined



4
5
6
7
# File 'lib/clasp/lock_manager.rb', line 4

def initialize
  @locks = ThreadSafe::Cache.new
  LockManagerTracker.register(self)
end

Instance Method Details

#lock(identifier) ⇒ undefined

Obtains the lock for the given identifier

Parameters:

  • identifier (Object)

Returns:

  • (undefined)

Raises:

  • (LockAcquisitonError)

    If the lock could not be acquired, usually due to deadlock



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/clasp/lock_manager.rb', line 15

def lock(identifier)
  obtained = false
  until obtained
    lock = @locks.compute_if_absent(identifier) {
      DisposableLock.new(self, identifier)
    }
    obtained = lock.lock
    unless obtained
      @locks.delete_pair(identifier, lock)
    end
  end
end

#locksEnumerable

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:

  • (Enumerable)


58
59
60
# File 'lib/clasp/lock_manager.rb', line 58

def locks
  @locks.values
end

#owned?(identifier) ⇒ Boolean

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

Parameters:

  • identifier (Object)

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/clasp/lock_manager.rb', line 32

def owned?(identifier)
  lock = @locks.get(identifier)
  lock && lock.owned?
end

#unlock(identifier) ⇒ undefined

Releases the lock for the given identifier

Parameters:

  • identifier (Object)

Returns:

  • (undefined)

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/clasp/lock_manager.rb', line 43

def unlock(identifier)
  unless @locks.key?(identifier)
    raise IllegalLockUsageError, "Unknown lock #{identifier}"
  end

  lock = @locks.get(identifier)
  lock.unlock

  if lock.closed?
    @locks.delete_pair(identifier, lock)
  end
end