Convert virPCIDeviceList and virUSBDeviceList into virObjectLockable

To allow modifications to the lists to be synchronized, convert
virPCIDeviceList and virUSBDeviceList into virObjectLockable
classes. The locking, however, will not be self-contained. The
users of these classes will have to call virObjectLock/Unlock
in the critical regions.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2013-01-16 11:49:54 +00:00
parent 77c3015f9c
commit 0f9ef55814
9 changed files with 77 additions and 46 deletions

View File

@ -1008,7 +1008,6 @@ virPCIDeviceListCount;
virPCIDeviceListDel; virPCIDeviceListDel;
virPCIDeviceListFind; virPCIDeviceListFind;
virPCIDeviceListFindIndex; virPCIDeviceListFindIndex;
virPCIDeviceListFree;
virPCIDeviceListGet; virPCIDeviceListGet;
virPCIDeviceListNew; virPCIDeviceListNew;
virPCIDeviceListSteal; virPCIDeviceListSteal;
@ -1234,7 +1233,6 @@ virUSBDeviceListAdd;
virUSBDeviceListCount; virUSBDeviceListCount;
virUSBDeviceListDel; virUSBDeviceListDel;
virUSBDeviceListFind; virUSBDeviceListFind;
virUSBDeviceListFree;
virUSBDeviceListGet; virUSBDeviceListGet;
virUSBDeviceListNew; virUSBDeviceListNew;
virUSBDeviceListSteal; virUSBDeviceListSteal;

View File

@ -167,7 +167,7 @@ virLXCFindHostdevUSBDevice(virDomainHostdevDefPtr hostdev,
*usb = virUSBDeviceListGet(devs, 0); *usb = virUSBDeviceListGet(devs, 0);
virUSBDeviceListSteal(devs, *usb); virUSBDeviceListSteal(devs, *usb);
} }
virUSBDeviceListFree(devs); virObjectUnref(devs);
if (rc == 0) { if (rc == 0) {
goto out; goto out;
@ -273,7 +273,7 @@ virLXCPrepareHostUSBDevices(virLXCDriverPtr driver,
ret = 0; ret = 0;
cleanup: cleanup:
virUSBDeviceListFree(list); virObjectUnref(list);
return ret; return ret;
} }

View File

@ -1047,9 +1047,9 @@ qemuShutdown(void) {
qemuDriverLock(qemu_driver); qemuDriverLock(qemu_driver);
virNWFilterUnRegisterCallbackDriver(&qemuCallbackDriver); virNWFilterUnRegisterCallbackDriver(&qemuCallbackDriver);
virPCIDeviceListFree(qemu_driver->activePciHostdevs); virObjectUnref(qemu_driver->activePciHostdevs);
virPCIDeviceListFree(qemu_driver->inactivePciHostdevs); virObjectUnref(qemu_driver->inactivePciHostdevs);
virUSBDeviceListFree(qemu_driver->activeUsbHostdevs); virObjectUnref(qemu_driver->activeUsbHostdevs);
virHashFree(qemu_driver->sharedDisks); virHashFree(qemu_driver->sharedDisks);
virCapabilitiesFree(qemu_driver->caps); virCapabilitiesFree(qemu_driver->caps);
qemuCapsCacheFree(qemu_driver->capsCache); qemuCapsCacheFree(qemu_driver->capsCache);

View File

@ -56,13 +56,13 @@ qemuGetPciHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
hostdev->source.subsys.u.pci.slot, hostdev->source.subsys.u.pci.slot,
hostdev->source.subsys.u.pci.function); hostdev->source.subsys.u.pci.function);
if (!dev) { if (!dev) {
virPCIDeviceListFree(list); virObjectUnref(list);
return NULL; return NULL;
} }
if (virPCIDeviceListAdd(list, dev) < 0) { if (virPCIDeviceListAdd(list, dev) < 0) {
virPCIDeviceFree(dev); virPCIDeviceFree(dev);
virPCIDeviceListFree(list); virObjectUnref(list);
return NULL; return NULL;
} }
@ -98,14 +98,14 @@ qemuGetActivePciHostDeviceList(virQEMUDriverPtr driver,
hostdev->source.subsys.u.pci.slot, hostdev->source.subsys.u.pci.slot,
hostdev->source.subsys.u.pci.function); hostdev->source.subsys.u.pci.function);
if (!dev) { if (!dev) {
virPCIDeviceListFree(list); virObjectUnref(list);
return NULL; return NULL;
} }
if ((activeDev = virPCIDeviceListFind(driver->activePciHostdevs, dev))) { if ((activeDev = virPCIDeviceListFind(driver->activePciHostdevs, dev))) {
if (virPCIDeviceListAdd(list, activeDev) < 0) { if (virPCIDeviceListAdd(list, activeDev) < 0) {
virPCIDeviceFree(dev); virPCIDeviceFree(dev);
virPCIDeviceListFree(list); virObjectUnref(list);
return NULL; return NULL;
} }
} }
@ -557,7 +557,7 @@ int qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver,
inactivedevs: inactivedevs:
/* Only steal all the devices from driver->activePciHostdevs. We will /* Only steal all the devices from driver->activePciHostdevs. We will
* free them in virPCIDeviceListFree(). * free them in virObjectUnref().
*/ */
while (virPCIDeviceListCount(pcidevs) > 0) { while (virPCIDeviceListCount(pcidevs) > 0) {
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, 0); virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, 0);
@ -580,7 +580,7 @@ reattachdevs:
} }
cleanup: cleanup:
virPCIDeviceListFree(pcidevs); virObjectUnref(pcidevs);
virObjectUnref(cfg); virObjectUnref(cfg);
return ret; return ret;
} }
@ -686,7 +686,7 @@ qemuFindHostdevUSBDevice(virDomainHostdevDefPtr hostdev,
*usb = virUSBDeviceListGet(devs, 0); *usb = virUSBDeviceListGet(devs, 0);
virUSBDeviceListSteal(devs, *usb); virUSBDeviceListSteal(devs, *usb);
} }
virUSBDeviceListFree(devs); virObjectUnref(devs);
if (rc == 0) { if (rc == 0) {
goto out; goto out;
@ -792,7 +792,7 @@ qemuPrepareHostUSBDevices(virQEMUDriverPtr driver,
ret = 0; ret = 0;
cleanup: cleanup:
virUSBDeviceListFree(list); virObjectUnref(list);
return ret; return ret;
} }
@ -880,7 +880,7 @@ void qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver,
continue; continue;
} }
/* virPCIDeviceListFree() will take care of freeing the dev. */ /* virObjectUnref() will take care of freeing the dev. */
virPCIDeviceListSteal(driver->activePciHostdevs, dev); virPCIDeviceListSteal(driver->activePciHostdevs, dev);
} }
@ -916,7 +916,7 @@ void qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver,
qemuReattachPciDevice(dev, driver); qemuReattachPciDevice(dev, driver);
} }
virPCIDeviceListFree(pcidevs); virObjectUnref(pcidevs);
cleanup: cleanup:
virObjectUnref(cfg); virObjectUnref(cfg);
} }

View File

@ -1221,7 +1221,7 @@ int qemuDomainAttachHostDevice(virQEMUDriverPtr driver,
goto error; goto error;
} }
virUSBDeviceListFree(list); virObjectUnref(list);
return 0; return 0;
error: error:
@ -1230,7 +1230,7 @@ error:
VIR_WARN("Unable to restore host device labelling on hotplug fail"); VIR_WARN("Unable to restore host device labelling on hotplug fail");
cleanup: cleanup:
virUSBDeviceListFree(list); virObjectUnref(list);
if (usb) if (usb)
virUSBDeviceListSteal(driver->activeUsbHostdevs, usb); virUSBDeviceListSteal(driver->activeUsbHostdevs, usb);
return -1; return -1;

View File

@ -74,6 +74,8 @@ struct _virPCIDevice {
}; };
struct _virPCIDeviceList { struct _virPCIDeviceList {
virObjectLockable parent;
unsigned count; unsigned count;
virPCIDevicePtr *devs; virPCIDevicePtr *devs;
}; };
@ -165,6 +167,23 @@ struct _virPCIDeviceList {
PCI_EXT_CAP_ACS_CR | \ PCI_EXT_CAP_ACS_CR | \
PCI_EXT_CAP_ACS_UF) PCI_EXT_CAP_ACS_UF)
static virClassPtr virPCIDeviceListClass;
static void virPCIDeviceListDispose(void *obj);
static int virPCIOnceInit(void)
{
if (!(virPCIDeviceListClass = virClassNew(virClassForObjectLockable(),
"virPCIDeviceList",
sizeof(virPCIDeviceList),
virPCIDeviceListDispose)))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virPCI)
static int static int
virPCIDeviceConfigOpen(virPCIDevicePtr dev, bool fatal) virPCIDeviceConfigOpen(virPCIDevicePtr dev, bool fatal)
{ {
@ -1505,22 +1524,21 @@ virPCIDeviceListNew(void)
{ {
virPCIDeviceListPtr list; virPCIDeviceListPtr list;
if (VIR_ALLOC(list) < 0) { if (virPCIInitialize() < 0)
virReportOOMError(); return NULL;
if (!(list = virObjectLockableNew(virPCIDeviceListClass)))
return NULL; return NULL;
}
return list; return list;
} }
void static void
virPCIDeviceListFree(virPCIDeviceListPtr list) virPCIDeviceListDispose(void *obj)
{ {
virPCIDeviceListPtr list = obj;
int i; int i;
if (!list)
return;
for (i = 0; i < list->count; i++) { for (i = 0; i < list->count; i++) {
virPCIDeviceFree(list->devs[i]); virPCIDeviceFree(list->devs[i]);
list->devs[i] = NULL; list->devs[i] = NULL;
@ -1528,7 +1546,6 @@ virPCIDeviceListFree(virPCIDeviceListPtr list)
list->count = 0; list->count = 0;
VIR_FREE(list->devs); VIR_FREE(list->devs);
VIR_FREE(list);
} }
int int

View File

@ -25,6 +25,7 @@
# define __VIR_PCI_H__ # define __VIR_PCI_H__
# include "internal.h" # include "internal.h"
# include "virobject.h"
typedef struct _virPCIDevice virPCIDevice; typedef struct _virPCIDevice virPCIDevice;
typedef virPCIDevice *virPCIDevicePtr; typedef virPCIDevice *virPCIDevicePtr;
@ -78,7 +79,6 @@ void virPCIDeviceReattachInit(virPCIDevice *dev);
virPCIDeviceListPtr virPCIDeviceListNew(void); virPCIDeviceListPtr virPCIDeviceListNew(void);
void virPCIDeviceListFree(virPCIDeviceListPtr list);
int virPCIDeviceListAdd(virPCIDeviceListPtr list, int virPCIDeviceListAdd(virPCIDeviceListPtr list,
virPCIDevicePtr dev); virPCIDevicePtr dev);
virPCIDevicePtr virPCIDeviceListGet(virPCIDeviceListPtr list, virPCIDevicePtr virPCIDeviceListGet(virPCIDeviceListPtr list,

View File

@ -57,6 +57,7 @@ struct _virUSBDevice {
}; };
struct _virUSBDeviceList { struct _virUSBDeviceList {
virObjectLockable parent;
unsigned int count; unsigned int count;
virUSBDevicePtr *devs; virUSBDevicePtr *devs;
}; };
@ -67,6 +68,23 @@ typedef enum {
USB_DEVICE_FIND_BY_BUS = 1 << 1, USB_DEVICE_FIND_BY_BUS = 1 << 1,
} virUSBDeviceFindFlags; } virUSBDeviceFindFlags;
static virClassPtr virUSBDeviceListClass;
static void virUSBDeviceListDispose(void *obj);
static int virUSBOnceInit(void)
{
if (!(virUSBDeviceListClass = virClassNew(virClassForObjectLockable(),
"virUSBDeviceList",
sizeof(virUSBDeviceList),
virUSBDeviceListDispose)))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virUSB)
static int virUSBSysReadFile(const char *f_name, const char *d_name, static int virUSBSysReadFile(const char *f_name, const char *d_name,
int base, unsigned int *value) int base, unsigned int *value)
{ {
@ -184,7 +202,7 @@ cleanup:
} }
if (!ret) if (!ret)
virUSBDeviceListFree(list); virObjectUnref(list);
return ret; return ret;
} }
@ -204,7 +222,7 @@ virUSBDeviceFindByVendor(unsigned int vendor,
return -1; return -1;
if (list->count == 0) { if (list->count == 0) {
virUSBDeviceListFree(list); virObjectUnref(list);
if (!mandatory) { if (!mandatory) {
VIR_DEBUG("Did not find USB device %x:%x", VIR_DEBUG("Did not find USB device %x:%x",
vendor, product); vendor, product);
@ -222,7 +240,7 @@ virUSBDeviceFindByVendor(unsigned int vendor,
if (devices) if (devices)
*devices = list; *devices = list;
else else
virUSBDeviceListFree(list); virObjectUnref(list);
return count; return count;
} }
@ -242,7 +260,7 @@ virUSBDeviceFindByBus(unsigned int bus,
return -1; return -1;
if (list->count == 0) { if (list->count == 0) {
virUSBDeviceListFree(list); virObjectUnref(list);
if (!mandatory) { if (!mandatory) {
VIR_DEBUG("Did not find USB device bus:%u device:%u", VIR_DEBUG("Did not find USB device bus:%u device:%u",
bus, devno); bus, devno);
@ -261,7 +279,7 @@ virUSBDeviceFindByBus(unsigned int bus,
*usb = virUSBDeviceListGet(list, 0); *usb = virUSBDeviceListGet(list, 0);
virUSBDeviceListSteal(list, *usb); virUSBDeviceListSteal(list, *usb);
} }
virUSBDeviceListFree(list); virObjectUnref(list);
return 0; return 0;
} }
@ -283,7 +301,7 @@ virUSBDeviceFind(unsigned int vendor,
return -1; return -1;
if (list->count == 0) { if (list->count == 0) {
virUSBDeviceListFree(list); virObjectUnref(list);
if (!mandatory) { if (!mandatory) {
VIR_DEBUG("Did not find USB device %x:%x bus:%u device:%u", VIR_DEBUG("Did not find USB device %x:%x bus:%u device:%u",
vendor, product, bus, devno); vendor, product, bus, devno);
@ -302,7 +320,7 @@ virUSBDeviceFind(unsigned int vendor,
*usb = virUSBDeviceListGet(list, 0); *usb = virUSBDeviceListGet(list, 0);
virUSBDeviceListSteal(list, *usb); virUSBDeviceListSteal(list, *usb);
} }
virUSBDeviceListFree(list); virObjectUnref(list);
return 0; return 0;
} }
@ -404,27 +422,25 @@ virUSBDeviceListNew(void)
{ {
virUSBDeviceListPtr list; virUSBDeviceListPtr list;
if (VIR_ALLOC(list) < 0) { if (virUSBInitialize() < 0)
virReportOOMError(); return NULL;
if (!(list = virObjectLockableNew(virUSBDeviceListClass)))
return NULL; return NULL;
}
return list; return list;
} }
void static void
virUSBDeviceListFree(virUSBDeviceListPtr list) virUSBDeviceListDispose(void *obj)
{ {
virUSBDeviceListPtr list = obj;
int i; int i;
if (!list)
return;
for (i = 0; i < list->count; i++) for (i = 0; i < list->count; i++)
virUSBDeviceFree(list->devs[i]); virUSBDeviceFree(list->devs[i]);
VIR_FREE(list->devs); VIR_FREE(list->devs);
VIR_FREE(list);
} }
int int

View File

@ -26,6 +26,7 @@
# define __VIR_USB_H__ # define __VIR_USB_H__
# include "internal.h" # include "internal.h"
# include "virobject.h"
# define USB_DEVFS "/dev/bus/usb/" # define USB_DEVFS "/dev/bus/usb/"
@ -81,7 +82,6 @@ int virUSBDeviceFileIterate(virUSBDevicePtr dev,
void *opaque); void *opaque);
virUSBDeviceListPtr virUSBDeviceListNew(void); virUSBDeviceListPtr virUSBDeviceListNew(void);
void virUSBDeviceListFree(virUSBDeviceListPtr list);
int virUSBDeviceListAdd(virUSBDeviceListPtr list, int virUSBDeviceListAdd(virUSBDeviceListPtr list,
virUSBDevicePtr dev); virUSBDevicePtr dev);
virUSBDevicePtr virUSBDeviceListGet(virUSBDeviceListPtr list, virUSBDevicePtr virUSBDeviceListGet(virUSBDeviceListPtr list,