ia64/xen-unstable

changeset 19363:95a7f97761ed

xend: Add lock for lspci_info in pci.py

Add lspci_info_lock to protect lspci_info.

Signed-off-by: Yosuke Iwamatsu <y-iwamatsu@ab.jp.nec.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 17 10:37:12 2009 +0000 (2009-03-17)
parents c05fa254405d
children 57327f3334b4
files tools/python/xen/util/pci.py
line diff
     1.1 --- a/tools/python/xen/util/pci.py	Tue Mar 17 10:36:51 2009 +0000
     1.2 +++ b/tools/python/xen/util/pci.py	Tue Mar 17 10:37:12 2009 +0000
     1.3 @@ -12,6 +12,7 @@ import re
     1.4  import types
     1.5  import struct
     1.6  import time
     1.7 +import threading
     1.8  from xen.util import utils
     1.9  
    1.10  PROC_PCI_PATH = '/proc/bus/pci/devices'
    1.11 @@ -97,6 +98,7 @@ MSIX_SIZE_MASK = 0x7ff
    1.12  
    1.13  # Global variable to store information from lspci
    1.14  lspci_info = None
    1.15 +lspci_info_lock = threading.RLock()
    1.16  
    1.17  #Calculate PAGE_SHIFT: number of bits to shift an address to get the page number
    1.18  PAGE_SIZE = resource.getpagesize()
    1.19 @@ -174,12 +176,16 @@ def get_all_pci_devices():
    1.20  
    1.21      return pci_devs
    1.22  
    1.23 -def create_lspci_info():
    1.24 +def _create_lspci_info():
    1.25 +    """Execute 'lspci' command and parse the result.
    1.26 +    If the command does not exist, lspci_info will be kept blank ({}).
    1.27 +
    1.28 +    Expects to be protected by lspci_info_lock.
    1.29 +    """
    1.30      global lspci_info
    1.31 +    
    1.32      lspci_info = {}
    1.33  
    1.34 -    # Execute 'lspci' command and parse the result.
    1.35 -    # If the command does not exist, lspci_info will be kept blank ({}).
    1.36      for paragraph in os.popen(LSPCI_CMD + ' -vmm').read().split('\n\n'):
    1.37          device_name = None
    1.38          device_info = {}
    1.39 @@ -195,6 +201,14 @@ def create_lspci_info():
    1.40          if device_name is not None:
    1.41              lspci_info[device_name] = device_info
    1.42  
    1.43 +def create_lspci_info():
    1.44 +    global lspci_info_lock
    1.45 +    lspci_info_lock.acquire()
    1.46 +    try:
    1.47 +        _create_lspci_info()
    1.48 +    finally:
    1.49 +        lspci_info_lock.release()
    1.50 +
    1.51  def save_pci_conf_space(devs_string):
    1.52      pci_list = []
    1.53      cfg_list = []
    1.54 @@ -911,22 +925,27 @@ class PciDevice:
    1.55          Since we cannot obtain these data from sysfs, use 'lspci' command.
    1.56          """
    1.57          global lspci_info
    1.58 +        global lspci_info_lock
    1.59  
    1.60 -        if lspci_info is None:
    1.61 -            create_lspci_info()
    1.62 -
    1.63 +        lspci_info_lock.acquire()
    1.64          try:
    1.65 -            device_info = lspci_info[self.name]
    1.66 -            self.revision = int(device_info['Rev'], 16)
    1.67 -            self.vendorname = device_info['Vendor']
    1.68 -            self.devicename = device_info['Device']
    1.69 -            self.classname = device_info['Class']
    1.70 -            self.subvendorname = device_info['SVendor']
    1.71 -            self.subdevicename = device_info['SDevice']
    1.72 -        except KeyError:
    1.73 -            pass
    1.74 +            if lspci_info is None:
    1.75 +                _create_lspci_info()
    1.76  
    1.77 -        return True
    1.78 +            try:
    1.79 +                device_info = lspci_info[self.name]
    1.80 +                self.revision = int(device_info['Rev'], 16)
    1.81 +                self.vendorname = device_info['Vendor']
    1.82 +                self.devicename = device_info['Device']
    1.83 +                self.classname = device_info['Class']
    1.84 +                self.subvendorname = device_info['SVendor']
    1.85 +                self.subdevicename = device_info['SDevice']
    1.86 +            except KeyError:
    1.87 +                pass
    1.88 +
    1.89 +            return True
    1.90 +        finally:
    1.91 +            lspci_info_lock.release()
    1.92  
    1.93      def __str__(self):
    1.94          str = "PCI Device %s\n" % (self.name)