From dbb11e8b7e14ec3028966a2479d42447bec153b2 Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Fri, 25 Jun 2010 18:09:19 +0200 Subject: [PATCH] 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: Guido Trotter <ultrotter@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/locking.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/locking.py b/lib/locking.py index e7e16ebad..0b419c71a 100644 --- a/lib/locking.py +++ b/lib/locking.py @@ -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) -- GitLab