From 7996a1358184552db6ab4f69c127336b2b794d47 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 10 Jul 2008 12:17:52 +0000
Subject: [PATCH] Add a simple decorator for instance methods

This is just a simple, hardcoded decorator for object methods needing
synchronization on the _lock instance attribute.

Reviewed-by: ultrotter
---
 lib/jqueue.py |  1 +
 lib/utils.py  | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/lib/jqueue.py b/lib/jqueue.py
index 6bc02a270..d96094ea8 100644
--- a/lib/jqueue.py
+++ b/lib/jqueue.py
@@ -28,6 +28,7 @@ from ganeti import constants
 from ganeti import workerpool
 from ganeti import errors
 from ganeti import mcpu
+from ganeti import utils
 
 
 JOBQUEUE_THREADS = 5
diff --git a/lib/utils.py b/lib/utils.py
index 5e4233a05..33bf83f3b 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1063,3 +1063,22 @@ def CheckVolumeGroupSize(vglist, vgname, minsize):
     return ("volume group '%s' too small (%s MiB required, %d MiB found)" %
             (vgname, minsize, vgsize))
   return None
+
+
+def LockedMethod(fn):
+  """Synchronized object access decorator.
+
+  This decorator is intended to protect access to an object using the
+  object's own lock which is hardcoded to '_lock'.
+
+  """
+  def wrapper(self, *args, **kwargs):
+    assert hasattr(self, '_lock')
+    lock = self._lock
+    lock.acquire()
+    try:
+      result = fn(self, *args, **kwargs)
+    finally:
+      lock.release()
+    return result
+  return wrapper
-- 
GitLab