]> xenbits.xensource.com Git - xen.git/commitdiff
xend: Add lock for xen-api class instances in XendAPIStore.py
authorKeir Fraser <keir.fraser@citrix.com>
Tue, 17 Mar 2009 10:36:51 +0000 (10:36 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Tue, 17 Mar 2009 10:36:51 +0000 (10:36 +0000)
Add __classes_lock to protect __classes, since it can be modified by
the udev listener thread.

Signed-off-by: Yosuke Iwamatsu <y-iwamatsu@ab.jp.nec.com>
tools/python/xen/xend/XendAPIStore.py

index dc313717dd66f43fa4c5bb89313c26c1e9c75298..6876f2e6fb9e5005e6a617b0e83c30d4411c7561 100644 (file)
@@ -25,36 +25,59 @@ You must register both the uuid and type, and get objects
 by type, to ensure safety
 """
 
+import threading
+
 __classes = {}
+__classes_lock = threading.RLock()
 
 def register(uuid, type, inst):
-    __classes[(uuid, type)] = inst
-    return inst
+    __classes_lock.acquire()
+    try:
+        __classes[(uuid, type)] = inst
+        return inst
+    finally:
+        __classes_lock.release()
 
 def deregister(uuid, type):
-    old = get(uuid, type)
-    if old is not None:
-        del __classes[(uuid, type)]
-    return old
+    __classes_lock.acquire()
+    try:
+        old = get(uuid, type)
+        if old is not None:
+            del __classes[(uuid, type)]
+        return old
+    finally:
+        __classes_lock.release()
 
 def get(uuid, type):
     """
     Get the instances by uuid and type
     """
-    return __classes.get((uuid, type), None)
+    __classes_lock.acquire()
+    try:
+        return __classes.get((uuid, type), None)
+    finally:
+        __classes_lock.release()
 
 def get_all(all_type):
     """
     Get all instances by type
     """
-    return [inst
-            for ((uuid, t), inst) in __classes.items()
-            if t == all_type]        
+    __classes_lock.acquire()
+    try:
+        return [inst
+                for ((uuid, t), inst) in __classes.items()
+                if t == all_type]        
+    finally:
+        __classes_lock.release()
 
 def get_all_uuid(all_type):
     """
     Get all uuids by type
     """
-    return [uuid
-            for (uuid, t) in __classes.keys()
-            if t == all_type]
+    __classes_lock.acquire()
+    try:
+        return [uuid
+                for (uuid, t) in __classes.keys()
+                if t == all_type]
+    finally:
+        __classes_lock.release()