ia64/xen-unstable

changeset 19362:c05fa254405d

xend: Add lock for xen-api class instances in XendAPIStore.py

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>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 17 10:36:51 2009 +0000 (2009-03-17)
parents 24af58657d8e
children 95a7f97761ed
files tools/python/xen/xend/XendAPIStore.py
line diff
     1.1 --- a/tools/python/xen/xend/XendAPIStore.py	Tue Mar 17 10:36:20 2009 +0000
     1.2 +++ b/tools/python/xen/xend/XendAPIStore.py	Tue Mar 17 10:36:51 2009 +0000
     1.3 @@ -25,36 +25,59 @@ You must register both the uuid and type
     1.4  by type, to ensure safety
     1.5  """
     1.6  
     1.7 +import threading
     1.8 +
     1.9  __classes = {}
    1.10 +__classes_lock = threading.RLock()
    1.11  
    1.12  def register(uuid, type, inst):
    1.13 -    __classes[(uuid, type)] = inst
    1.14 -    return inst
    1.15 +    __classes_lock.acquire()
    1.16 +    try:
    1.17 +        __classes[(uuid, type)] = inst
    1.18 +        return inst
    1.19 +    finally:
    1.20 +        __classes_lock.release()
    1.21  
    1.22  def deregister(uuid, type):
    1.23 -    old = get(uuid, type)
    1.24 -    if old is not None:
    1.25 -        del __classes[(uuid, type)]
    1.26 -    return old
    1.27 +    __classes_lock.acquire()
    1.28 +    try:
    1.29 +        old = get(uuid, type)
    1.30 +        if old is not None:
    1.31 +            del __classes[(uuid, type)]
    1.32 +        return old
    1.33 +    finally:
    1.34 +        __classes_lock.release()
    1.35  
    1.36  def get(uuid, type):
    1.37      """
    1.38      Get the instances by uuid and type
    1.39      """
    1.40 -    return __classes.get((uuid, type), None)
    1.41 +    __classes_lock.acquire()
    1.42 +    try:
    1.43 +        return __classes.get((uuid, type), None)
    1.44 +    finally:
    1.45 +        __classes_lock.release()
    1.46  
    1.47  def get_all(all_type):
    1.48      """
    1.49      Get all instances by type
    1.50      """
    1.51 -    return [inst
    1.52 -            for ((uuid, t), inst) in __classes.items()
    1.53 -            if t == all_type]        
    1.54 +    __classes_lock.acquire()
    1.55 +    try:
    1.56 +        return [inst
    1.57 +                for ((uuid, t), inst) in __classes.items()
    1.58 +                if t == all_type]        
    1.59 +    finally:
    1.60 +        __classes_lock.release()
    1.61  
    1.62  def get_all_uuid(all_type):
    1.63      """
    1.64      Get all uuids by type
    1.65      """
    1.66 -    return [uuid
    1.67 -            for (uuid, t) in __classes.keys()
    1.68 -            if t == all_type]
    1.69 +    __classes_lock.acquire()
    1.70 +    try:
    1.71 +        return [uuid
    1.72 +                for (uuid, t) in __classes.keys()
    1.73 +                if t == all_type]
    1.74 +    finally:
    1.75 +        __classes_lock.release()