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

Class SharedLock

source code


Implements a shared lock.

Multiple threads can acquire the lock in a shared way by calling acquire(shared=1). In order to acquire the lock in an exclusive way threads can call acquire(shared=0).

Notes on data structures: __pending contains a priority queue (heapq) of all pending acquires: [(priority1: prioqueue1), (priority2: prioqueue2), ...]. Each per-priority queue contains a normal in-order list of conditions to be notified when the lock can be acquired. Shared locks are grouped together by priority and the condition for them is stored in __pending_shared if it already exists. __pending_by_prio keeps references for the per-priority queues indexed by priority for faster access.

Nested Classes [hide private]
  __condition_class
Instance Methods [hide private]
 
__init__(self, name, monitor=None, _time_fn=time.time)
Construct a new SharedLock.
source code
 
__repr__(self)
repr(x)
source code
 
GetLockInfo(self, requested)
Retrieves information for querying locks.
source code
 
__check_deleted(self)
Raises an exception if the lock has been deleted.
source code
 
__is_sharer(self)
Is the current thread sharing the lock at this time?
source code
 
__is_exclusive(self)
Is the current thread holding the lock exclusively at this time?
source code
 
__is_owned(self, shared=-1)
Is the current thread somehow owning the lock at this time?
source code
 
is_owned(self, shared=-1)
Is the current thread somehow owning the lock at this time?
source code
 
_is_owned(self, shared=-1)
Necessary to remain compatible with threading.Condition, which tries to retrieve a locks' "_is_owned" attribute
source code
int
_count_pending(self)
Returns the number of pending acquires.
source code
bool
_check_empty(self)
Checks whether there are any pending acquires.
source code
 
__do_acquire(self, shared)
Actually acquire the lock.
source code
 
__can_acquire(self, shared)
Determine whether lock can be acquired.
source code
 
__find_first_pending_queue(self)
Tries to find the topmost queued entry with pending acquires.
source code
 
__is_on_top(self, cond)
Checks whether the passed condition is on top of the queue.
source code
 
__acquire_unlocked(self, shared, timeout, priority)
Acquire a shared lock.
source code
 
acquire(self, shared=0, timeout=None, priority=None, test_notify=None)
Acquire a shared lock.
source code
 
downgrade(self)
Changes the lock mode from exclusive to shared.
source code
 
release(self)
Release a Shared Lock.
source code
 
__notify_topmost(self)
Notifies topmost condition in queue of pending acquires.
source code
 
_notify_topmost(self)
Exported version of __notify_topmost.
source code
 
delete(self, timeout=None, priority=None)
Delete a Shared Lock.
source code
 
_release_save(self) source code
 
_acquire_restore(self, shared) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
string name
the name of the lock
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, name, monitor=None, _time_fn=time.time)
(Constructor)

source code 

Construct a new SharedLock.

Parameters:
  • name - the name of the lock
  • monitor (LockMonitor) - Lock monitor with which to register
Overrides: object.__init__

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

GetLockInfo(self, requested)

source code 

Retrieves information for querying locks.

Parameters:
  • requested (set) - Requested information, see query.LQ_*

__is_owned(self, shared=-1)

source code 

Is the current thread somehow owning the lock at this time?

This is a private version of the function, which presumes you're holding the internal lock.

is_owned(self, shared=-1)

source code 

Is the current thread somehow owning the lock at this time?

Parameters:
  • shared -
    • < 0: check for any type of ownership (default)
    • 0: check for exclusive ownership
    • > 0: check for shared ownership

_is_owned(self, shared=-1)

source code 

Necessary to remain compatible with threading.Condition, which tries to retrieve a locks' "_is_owned" attribute

Parameters:
  • shared -
    • < 0: check for any type of ownership (default)
    • 0: check for exclusive ownership
    • > 0: check for shared ownership

__find_first_pending_queue(self)

source code 

Tries to find the topmost queued entry with pending acquires.

Removes empty entries while going through the list.

__is_on_top(self, cond)

source code 

Checks whether the passed condition is on top of the queue.

The caller must make sure the queue isn't empty.

__acquire_unlocked(self, shared, timeout, priority)

source code 

Acquire a shared lock.

Parameters:
  • shared - whether to acquire in shared mode; by default an exclusive lock will be acquired
  • timeout - maximum waiting time before giving up
  • priority (integer) - Priority for acquiring lock

acquire(self, shared=0, timeout=None, priority=None, test_notify=None)

source code 

Acquire a shared lock.

Parameters:
  • shared (integer (0/1) used as a boolean) - whether to acquire in shared mode; by default an exclusive lock will be acquired
  • timeout (float) - maximum waiting time before giving up
  • priority (integer) - Priority for acquiring lock
  • test_notify (callable or None) - Special callback function for unittesting

downgrade(self)

source code 

Changes the lock mode from exclusive to shared.

Pending acquires in shared mode on the same priority will go ahead.

release(self)

source code 

Release a Shared Lock.

You must have acquired the lock, either in shared or in exclusive mode, before calling this function.

delete(self, timeout=None, priority=None)

source code 

Delete a Shared Lock.

This operation will declare the lock for removal. First the lock will be acquired in exclusive mode if you don't already own it, then the lock will be put in a state where any future and pending acquire() fail.

Parameters:
  • timeout (float) - maximum waiting time before giving up
  • priority (integer) - Priority for acquiring lock