Impl of threading locking APIs

This commit is contained in:
Daniel P. Berrange 2008-12-04 22:00:14 +00:00
parent d48717054c
commit 746f4373e7
9 changed files with 151 additions and 16 deletions

View File

@ -1,3 +1,13 @@
Thu Dec 4 21:49:41 GMT 2008 Daniel P. Berrange <berrange@redhat.com>
Per object locking implementation
* src/domain_conf.c, src/domain_conf.h, src/network_conf.c,
src/network_conf.h, src/node_device_conf.c,
src/node_device_conf.h, src/storage_conf.c
src/storage_conf.h: Add implementation of locking APIs,
and make object lookup / creation methods return locked
objects
Thu Dec 4 21:48:41 GMT 2008 Daniel P. Berrange <berrange@redhat.com> Thu Dec 4 21:48:41 GMT 2008 Daniel P. Berrange <berrange@redhat.com>
* src/libvirt_sym.version.in, src/node_device.c, * src/libvirt_sym.version.in, src/node_device.c,

View File

@ -151,10 +151,13 @@ virDomainObjPtr virDomainFindByID(const virDomainObjListPtr doms,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < doms->count ; i++) for (i = 0 ; i < doms->count ; i++) {
virDomainObjLock(doms->objs[i]);
if (virDomainIsActive(doms->objs[i]) && if (virDomainIsActive(doms->objs[i]) &&
doms->objs[i]->def->id == id) doms->objs[i]->def->id == id)
return doms->objs[i]; return doms->objs[i];
virDomainObjUnlock(doms->objs[i]);
}
return NULL; return NULL;
} }
@ -165,9 +168,12 @@ virDomainObjPtr virDomainFindByUUID(const virDomainObjListPtr doms,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < doms->count ; i++) for (i = 0 ; i < doms->count ; i++) {
virDomainObjLock(doms->objs[i]);
if (!memcmp(doms->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN)) if (!memcmp(doms->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
return doms->objs[i]; return doms->objs[i];
virDomainObjUnlock(doms->objs[i]);
}
return NULL; return NULL;
} }
@ -177,9 +183,12 @@ virDomainObjPtr virDomainFindByName(const virDomainObjListPtr doms,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < doms->count ; i++) for (i = 0 ; i < doms->count ; i++) {
virDomainObjLock(doms->objs[i]);
if (STREQ(doms->objs[i]->def->name, name)) if (STREQ(doms->objs[i]->def->name, name))
return doms->objs[i]; return doms->objs[i];
virDomainObjUnlock(doms->objs[i]);
}
return NULL; return NULL;
} }
@ -455,6 +464,8 @@ virDomainObjPtr virDomainAssignDef(virConnectPtr conn,
return NULL; return NULL;
} }
pthread_mutex_init(&domain->lock, NULL);
virDomainObjLock(domain);
domain->state = VIR_DOMAIN_SHUTOFF; domain->state = VIR_DOMAIN_SHUTOFF;
domain->def = def; domain->def = def;
@ -475,8 +486,12 @@ void virDomainRemoveInactive(virDomainObjListPtr doms,
{ {
unsigned int i; unsigned int i;
virDomainObjUnlock(dom);
for (i = 0 ; i < doms->count ; i++) { for (i = 0 ; i < doms->count ; i++) {
virDomainObjLock(doms->objs[i]);
if (doms->objs[i] == dom) { if (doms->objs[i] == dom) {
virDomainObjUnlock(doms->objs[i]);
virDomainObjFree(doms->objs[i]); virDomainObjFree(doms->objs[i]);
if (i < (doms->count - 1)) if (i < (doms->count - 1))
@ -490,6 +505,7 @@ void virDomainRemoveInactive(virDomainObjListPtr doms,
break; break;
} }
virDomainObjUnlock(doms->objs[i]);
} }
} }
@ -3348,9 +3364,11 @@ int virDomainLoadAllConfigs(virConnectPtr conn,
entry->d_name, entry->d_name,
notify, notify,
opaque); opaque);
if (dom) if (dom) {
virDomainObjUnlock(dom);
dom->persistent = 1; dom->persistent = 1;
} }
}
closedir(dir); closedir(dir);
@ -3470,6 +3488,19 @@ const char *virDomainDefDefaultEmulator(virConnectPtr conn,
} }
#ifdef HAVE_PTHREAD_H
void virDomainObjLock(virDomainObjPtr obj)
{
pthread_mutex_lock(&obj->lock);
}
void virDomainObjUnlock(virDomainObjPtr obj)
{
pthread_mutex_unlock(&obj->lock);
}
#else
void virDomainObjLock(virDomainObjPtr obj ATTRIBUTE_UNUSED) void virDomainObjLock(virDomainObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
@ -3477,5 +3508,6 @@ void virDomainObjLock(virDomainObjPtr obj ATTRIBUTE_UNUSED)
void virDomainObjUnlock(virDomainObjPtr obj ATTRIBUTE_UNUSED) void virDomainObjUnlock(virDomainObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
#endif
#endif /* ! PROXY */ #endif /* ! PROXY */

View File

@ -454,6 +454,8 @@ struct _virDomainDef {
typedef struct _virDomainObj virDomainObj; typedef struct _virDomainObj virDomainObj;
typedef virDomainObj *virDomainObjPtr; typedef virDomainObj *virDomainObjPtr;
struct _virDomainObj { struct _virDomainObj {
PTHREAD_MUTEX_T(lock);
int stdin_fd; int stdin_fd;
int stdout_fd; int stdout_fd;
int stdout_watch; int stdout_watch;

View File

@ -58,9 +58,12 @@ virNetworkObjPtr virNetworkFindByUUID(const virNetworkObjListPtr nets,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < nets->count ; i++) for (i = 0 ; i < nets->count ; i++) {
virNetworkObjLock(nets->objs[i]);
if (!memcmp(nets->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN)) if (!memcmp(nets->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
return nets->objs[i]; return nets->objs[i];
virNetworkObjUnlock(nets->objs[i]);
}
return NULL; return NULL;
} }
@ -70,9 +73,12 @@ virNetworkObjPtr virNetworkFindByName(const virNetworkObjListPtr nets,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < nets->count ; i++) for (i = 0 ; i < nets->count ; i++) {
virNetworkObjLock(nets->objs[i]);
if (STREQ(nets->objs[i]->def->name, name)) if (STREQ(nets->objs[i]->def->name, name))
return nets->objs[i]; return nets->objs[i];
virNetworkObjUnlock(nets->objs[i]);
}
return NULL; return NULL;
} }
@ -157,7 +163,8 @@ virNetworkObjPtr virNetworkAssignDef(virConnectPtr conn,
virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL); virNetworkReportError(conn, VIR_ERR_NO_MEMORY, NULL);
return NULL; return NULL;
} }
pthread_mutex_init(&network->lock, NULL);
virNetworkObjLock(network);
network->def = def; network->def = def;
if (VIR_REALLOC_N(nets->objs, nets->count + 1) < 0) { if (VIR_REALLOC_N(nets->objs, nets->count + 1) < 0) {
@ -178,8 +185,11 @@ void virNetworkRemoveInactive(virNetworkObjListPtr nets,
{ {
unsigned int i; unsigned int i;
virNetworkObjUnlock(net);
for (i = 0 ; i < nets->count ; i++) { for (i = 0 ; i < nets->count ; i++) {
virNetworkObjLock(nets->objs[i]);
if (nets->objs[i] == net) { if (nets->objs[i] == net) {
virNetworkObjUnlock(nets->objs[i]);
virNetworkObjFree(nets->objs[i]); virNetworkObjFree(nets->objs[i]);
if (i < (nets->count - 1)) if (i < (nets->count - 1))
@ -193,6 +203,7 @@ void virNetworkRemoveInactive(virNetworkObjListPtr nets,
break; break;
} }
virNetworkObjUnlock(nets->objs[i]);
} }
} }
@ -770,6 +781,8 @@ int virNetworkLoadAllConfigs(virConnectPtr conn,
} }
while ((entry = readdir(dir))) { while ((entry = readdir(dir))) {
virNetworkObjPtr net;
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
continue; continue;
@ -778,11 +791,13 @@ int virNetworkLoadAllConfigs(virConnectPtr conn,
/* NB: ignoring errors, so one malformed config doesn't /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */ kill the whole process */
virNetworkLoadConfig(conn, net = virNetworkLoadConfig(conn,
nets, nets,
configDir, configDir,
autostartDir, autostartDir,
entry->d_name); entry->d_name);
if (net)
virNetworkObjUnlock(net);
} }
closedir(dir); closedir(dir);
@ -812,6 +827,19 @@ int virNetworkDeleteConfig(virConnectPtr conn,
return 0; return 0;
} }
#ifdef HAVE_PTHREAD_H
void virNetworkObjLock(virNetworkObjPtr obj)
{
pthread_mutex_lock(&obj->lock);
}
void virNetworkObjUnlock(virNetworkObjPtr obj)
{
pthread_mutex_unlock(&obj->lock);
}
#else
void virNetworkObjLock(virNetworkObjPtr obj ATTRIBUTE_UNUSED) void virNetworkObjLock(virNetworkObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
@ -819,3 +847,5 @@ void virNetworkObjLock(virNetworkObjPtr obj ATTRIBUTE_UNUSED)
void virNetworkObjUnlock(virNetworkObjPtr obj ATTRIBUTE_UNUSED) void virNetworkObjUnlock(virNetworkObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
#endif

View File

@ -82,6 +82,8 @@ struct _virNetworkDef {
typedef struct _virNetworkObj virNetworkObj; typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr; typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj { struct _virNetworkObj {
PTHREAD_MUTEX_T(lock);
pid_t dnsmasqPid; pid_t dnsmasqPid;
unsigned int active : 1; unsigned int active : 1;
unsigned int autostart : 1; unsigned int autostart : 1;

View File

@ -59,9 +59,12 @@ virNodeDeviceObjPtr virNodeDeviceFindByName(const virNodeDeviceObjListPtr devs,
{ {
unsigned int i; unsigned int i;
for (i = 0; i < devs->count; i++) for (i = 0; i < devs->count; i++) {
virNodeDeviceObjLock(devs->objs[i]);
if (STREQ(devs->objs[i]->def->name, name)) if (STREQ(devs->objs[i]->def->name, name))
return devs->objs[i]; return devs->objs[i];
virNodeDeviceObjUnlock(devs->objs[i]);
}
return NULL; return NULL;
} }
@ -125,6 +128,8 @@ virNodeDeviceObjPtr virNodeDeviceAssignDef(virConnectPtr conn,
return NULL; return NULL;
} }
pthread_mutex_init(&device->lock, NULL);
virNodeDeviceObjLock(device);
device->def = def; device->def = def;
if (VIR_REALLOC_N(devs->objs, devs->count+1) < 0) { if (VIR_REALLOC_N(devs->objs, devs->count+1) < 0) {
@ -144,8 +149,12 @@ void virNodeDeviceObjRemove(virNodeDeviceObjListPtr devs,
{ {
unsigned int i; unsigned int i;
virNodeDeviceObjUnlock(dev);
for (i = 0; i < devs->count; i++) { for (i = 0; i < devs->count; i++) {
virNodeDeviceObjLock(dev);
if (devs->objs[i] == dev) { if (devs->objs[i] == dev) {
virNodeDeviceObjUnlock(dev);
virNodeDeviceObjFree(devs->objs[i]); virNodeDeviceObjFree(devs->objs[i]);
if (i < (devs->count - 1)) if (i < (devs->count - 1))
@ -159,6 +168,7 @@ void virNodeDeviceObjRemove(virNodeDeviceObjListPtr devs,
break; break;
} }
virNodeDeviceObjUnlock(dev);
} }
} }
@ -398,6 +408,20 @@ void virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps)
} }
#ifdef HAVE_PTHREAD_H
void virNodeDeviceObjLock(virNodeDeviceObjPtr obj)
{
pthread_mutex_lock(&obj->lock);
}
void virNodeDeviceObjUnlock(virNodeDeviceObjPtr obj)
{
pthread_mutex_unlock(&obj->lock);
}
#else
void virNodeDeviceObjLock(virNodeDeviceObjPtr obj ATTRIBUTE_UNUSED) void virNodeDeviceObjLock(virNodeDeviceObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
@ -405,3 +429,5 @@ void virNodeDeviceObjLock(virNodeDeviceObjPtr obj ATTRIBUTE_UNUSED)
void virNodeDeviceObjUnlock(virNodeDeviceObjPtr obj ATTRIBUTE_UNUSED) void virNodeDeviceObjUnlock(virNodeDeviceObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
#endif

View File

@ -142,6 +142,8 @@ struct _virNodeDeviceDef {
typedef struct _virNodeDeviceObj virNodeDeviceObj; typedef struct _virNodeDeviceObj virNodeDeviceObj;
typedef virNodeDeviceObj *virNodeDeviceObjPtr; typedef virNodeDeviceObj *virNodeDeviceObjPtr;
struct _virNodeDeviceObj { struct _virNodeDeviceObj {
PTHREAD_MUTEX_T(lock);
virNodeDeviceDefPtr def; /* device definition */ virNodeDeviceDefPtr def; /* device definition */
void *privateData; /* driver-specific private data */ void *privateData; /* driver-specific private data */
void (*privateFree)(void *data); /* destructor for private data */ void (*privateFree)(void *data); /* destructor for private data */

View File

@ -310,8 +310,12 @@ virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
{ {
unsigned int i; unsigned int i;
virStoragePoolObjUnlock(pool);
for (i = 0 ; i < pools->count ; i++) { for (i = 0 ; i < pools->count ; i++) {
virStoragePoolObjLock(pools->objs[i]);
if (pools->objs[i] == pool) { if (pools->objs[i] == pool) {
virStoragePoolObjUnlock(pools->objs[i]);
virStoragePoolObjFree(pools->objs[i]); virStoragePoolObjFree(pools->objs[i]);
if (i < (pools->count - 1)) if (i < (pools->count - 1))
@ -325,6 +329,7 @@ virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
break; break;
} }
virStoragePoolObjUnlock(pools->objs[i]);
} }
} }
@ -1153,9 +1158,12 @@ virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
const unsigned char *uuid) { const unsigned char *uuid) {
unsigned int i; unsigned int i;
for (i = 0 ; i < pools->count ; i++) for (i = 0 ; i < pools->count ; i++) {
virStoragePoolObjLock(pools->objs[i]);
if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN)) if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
return pools->objs[i]; return pools->objs[i];
virStoragePoolObjUnlock(pools->objs[i]);
}
return NULL; return NULL;
} }
@ -1165,9 +1173,12 @@ virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
const char *name) { const char *name) {
unsigned int i; unsigned int i;
for (i = 0 ; i < pools->count ; i++) for (i = 0 ; i < pools->count ; i++) {
virStoragePoolObjLock(pools->objs[i]);
if (STREQ(pools->objs[i]->def->name, name)) if (STREQ(pools->objs[i]->def->name, name))
return pools->objs[i]; return pools->objs[i];
virStoragePoolObjUnlock(pools->objs[i]);
}
return NULL; return NULL;
} }
@ -1243,6 +1254,8 @@ virStoragePoolObjAssignDef(virConnectPtr conn,
return NULL; return NULL;
} }
pthread_mutex_init(&pool->lock, NULL);
virStoragePoolObjLock(pool);
pool->active = 0; pool->active = 0;
pool->def = def; pool->def = def;
@ -1327,6 +1340,7 @@ virStoragePoolLoadAllConfigs(virConnectPtr conn,
char *xml = NULL; char *xml = NULL;
char path[PATH_MAX]; char path[PATH_MAX];
char autostartLink[PATH_MAX]; char autostartLink[PATH_MAX];
virStoragePoolObjPtr pool;
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
continue; continue;
@ -1351,7 +1365,9 @@ virStoragePoolLoadAllConfigs(virConnectPtr conn,
if (virFileReadAll(path, 8192, &xml) < 0) if (virFileReadAll(path, 8192, &xml) < 0)
continue; continue;
virStoragePoolObjLoad(conn, pools, entry->d_name, path, xml, autostartLink); pool = virStoragePoolObjLoad(conn, pools, entry->d_name, path, xml, autostartLink);
if (pool)
virStoragePoolObjUnlock(pool);
VIR_FREE(xml); VIR_FREE(xml);
} }
@ -1509,6 +1525,18 @@ char *virStoragePoolSourceListFormat(virConnectPtr conn,
} }
#ifdef HAVE_PTHREAD_H
void virStoragePoolObjLock(virStoragePoolObjPtr obj)
{
pthread_mutex_lock(&obj->lock);
}
void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
{
pthread_mutex_unlock(&obj->lock);
}
#else
void virStoragePoolObjLock(virStoragePoolObjPtr obj ATTRIBUTE_UNUSED) void virStoragePoolObjLock(virStoragePoolObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
@ -1516,3 +1544,4 @@ void virStoragePoolObjLock(virStoragePoolObjPtr obj ATTRIBUTE_UNUSED)
void virStoragePoolObjUnlock(virStoragePoolObjPtr obj ATTRIBUTE_UNUSED) void virStoragePoolObjUnlock(virStoragePoolObjPtr obj ATTRIBUTE_UNUSED)
{ {
} }
#endif

View File

@ -223,6 +223,8 @@ typedef struct _virStoragePoolObj virStoragePoolObj;
typedef virStoragePoolObj *virStoragePoolObjPtr; typedef virStoragePoolObj *virStoragePoolObjPtr;
struct _virStoragePoolObj { struct _virStoragePoolObj {
PTHREAD_MUTEX_T(lock);
char *configFile; char *configFile;
char *autostartLink; char *autostartLink;
int active; int active;