Commit dbb11e8b authored by Guido Trotter's avatar Guido Trotter
Browse files

ssynchronized: act on a class member



The ssynchronized decorator takes the lock to act on in input.
With this change we allow a string to be passed, and if so we assume the
function it protects is a class method, and we act on the member of the
class itself named as the string we got.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 3c0d60d0
......@@ -36,16 +36,25 @@ from ganeti import utils
from ganeti import compat
def ssynchronized(lock, shared=0):
def ssynchronized(mylock, shared=0):
"""Shared Synchronization decorator.
Calls the function holding the given lock, either in exclusive or shared
mode. It requires the passed lock to be a SharedLock (or support its
semantics).
@type mylock: lockable object or string
@param mylock: lock to acquire or class member name of the lock to acquire
"""
def wrap(fn):
def sync_function(*args, **kwargs):
if isinstance(mylock, basestring):
assert args, "cannot ssynchronize on non-class method: self not found"
# args[0] is "self"
lock = getattr(args[0], mylock)
else:
lock = mylock
lock.acquire(shared=shared)
try:
return fn(*args, **kwargs)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment