Package ganeti :: Module locking :: Class LockSet
[hide private]
[frames] | no frames]

Class LockSet

source code

Implements a set of locks.

This abstraction implements a set of shared locks for the same resource type, distinguished by name. The user can lock a subset of the resources and the LockSet will take care of acquiring the locks always in the same order, thus preventing deadlock.

All the locks needed in the same set must be acquired together, though.

Instance Methods [hide private]
 
__init__(self, members=None)
Constructs a new LockSet.
source code
 
_is_owned(self)
Is the current thread a current level owner?
source code
 
_add_owned(self, name=None)
Note the current thread owns the given lock
source code
 
_del_owned(self, name=None)
Note the current thread owns the given lock
source code
 
_list_owned(self)
Get the set of resource names owned by the current thread
source code
 
__names(self)
Return the current set of names.
source code
 
_names(self)
Return a copy of the current set of elements.
source code
 
acquire(self, names, blocking=1, shared=0)
Acquire a set of resource locks.
source code
 
release(self, names=None)
Release a set of resource locks, at the same level.
source code
 
add(self, names, acquired=0, shared=0)
Add a new set of elements to the set
source code
 
remove(self, names, blocking=1)
Remove elements from the lock set.
source code
Method Details [hide private]

__init__(self, members=None)
(Constructor)

source code 

Constructs a new LockSet.

Parameters:
  • members - initial members of the set

__names(self)

source code 

Return the current set of names.

Only call this function while holding __lock and don't iterate on the result after releasing the lock.

_names(self)

source code 

Return a copy of the current set of elements.

Used only for debugging purposes.

acquire(self, names, blocking=1, shared=0)

source code 

Acquire a set of resource locks.

Parameters:
  • names - the names of the locks which shall be acquired (special lock names, or instance/node names)
  • shared - whether to acquire in shared mode; by default an exclusive lock will be acquired
  • blocking - whether to block while trying to acquire or to operate in try-lock mode (this locking mode is not supported yet)
Returns:
True when all the locks are successfully acquired
Raises:
  • errors.LockError - when any lock we try to acquire has been deleted before we succeed. In this case none of the locks requested will be acquired.

release(self, names=None)

source code 

Release a set of resource locks, at the same level.

You must have acquired the locks, either in shared or in exclusive mode, before releasing them.

Parameters:
  • names - the names of the locks which shall be released (defaults to all the locks acquired at that level).

add(self, names, acquired=0, shared=0)

source code 

Add a new set of elements to the set

Parameters:
  • names - names of the new elements to add
  • acquired - pre-acquire the new resource?
  • shared - is the pre-acquisition shared?

remove(self, names, blocking=1)

source code 

Remove elements from the lock set.

You can either not hold anything in the lockset or already hold a superset of the elements you want to delete, exclusively.

Parameters:
  • names - names of the resource to remove.
  • blocking - whether to block while trying to acquire or to operate in try-lock mode (this locking mode is not supported yet unless you are already holding exclusively the locks)
Returns:
: a list of locks which we removed; the list is always equal to the names list if we were holding all the locks exclusively