mirror of https://gitee.com/openkylin/libvirt.git
Return count of callbacks when registering callbacks
When registering a callback for a particular event some callers need to know how many callbacks already exist for that event. While it is possible to ask for a count, this is not free from race conditions when threaded. Thus the API for registering callbacks should return the count of callbacks. Also rename virDomainEventStateDeregisterAny to virDomainEventStateDeregisterID * src/conf/domain_event.c, src/conf/domain_event.h, src/libvirt_private.syms: Return count of callbacks when registering callbacks * src/libxl/libxl_driver.c, src/libxl/libxl_driver.c, src/qemu/qemu_driver.c, src/remote/remote_driver.c, src/remote/remote_driver.c, src/uml/uml_driver.c, src/vbox/vbox_tmpl.c, src/xen/xen_driver.c: Update for change in APIs
This commit is contained in:
parent
a86bbc6003
commit
d09f6ba5fe
|
@ -357,7 +357,7 @@ virDomainEventCallbackListAdd(virConnectPtr conn,
|
|||
return virDomainEventCallbackListAddID(conn, cbList, NULL,
|
||||
VIR_DOMAIN_EVENT_ID_LIFECYCLE,
|
||||
VIR_DOMAIN_EVENT_CALLBACK(callback),
|
||||
opaque, freecb);
|
||||
opaque, freecb, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -368,6 +368,7 @@ virDomainEventCallbackListAdd(virConnectPtr conn,
|
|||
* @eventID: the event ID
|
||||
* @callback: the callback to add
|
||||
* @opaque: opaque data tio pass to callback
|
||||
* @callbackID: filled with callback ID
|
||||
*
|
||||
* Internal function to add a callback from a virDomainEventCallbackListPtr
|
||||
*/
|
||||
|
@ -378,10 +379,12 @@ virDomainEventCallbackListAddID(virConnectPtr conn,
|
|||
int eventID,
|
||||
virConnectDomainEventGenericCallback callback,
|
||||
void *opaque,
|
||||
virFreeCallback freecb)
|
||||
virFreeCallback freecb,
|
||||
int *callbackID)
|
||||
{
|
||||
virDomainEventCallbackPtr event;
|
||||
int i;
|
||||
int ret = 0;
|
||||
|
||||
/* Check incoming */
|
||||
if ( !cbList ) {
|
||||
|
@ -427,7 +430,17 @@ virDomainEventCallbackListAddID(virConnectPtr conn,
|
|||
|
||||
event->callbackID = cbList->nextID++;
|
||||
|
||||
return event->callbackID;
|
||||
for (i = 0 ; i < cbList->count ; i++) {
|
||||
if (cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
|
||||
cbList->callbacks[i]->conn == conn &&
|
||||
!cbList->callbacks[i]->deleted)
|
||||
ret++;
|
||||
}
|
||||
|
||||
if (callbackID)
|
||||
*callbackID = event->callbackID;
|
||||
|
||||
return ret;
|
||||
|
||||
no_memory:
|
||||
virReportOOMError();
|
||||
|
@ -1358,20 +1371,20 @@ virDomainEventStateDeregister(virConnectPtr conn,
|
|||
|
||||
|
||||
/**
|
||||
* virDomainEventStateDeregisterAny:
|
||||
* virDomainEventStateDeregisterID:
|
||||
* @state: domain event state
|
||||
* @conn: connection to associate with callback
|
||||
* @callbackID: ID of the function to remove from event
|
||||
*
|
||||
* Unregister the function @callbackID with connection @conn,
|
||||
* from @state, for lifecycle events.
|
||||
* from @state, for events.
|
||||
*
|
||||
* Returns: the number of lifecycle callbacks still registered, or -1 on error
|
||||
* Returns: the number of callbacks still registered, or -1 on error
|
||||
*/
|
||||
int
|
||||
virDomainEventStateDeregisterAny(virConnectPtr conn,
|
||||
virDomainEventStatePtr state,
|
||||
int callbackID)
|
||||
virDomainEventStateDeregisterID(virConnectPtr conn,
|
||||
virDomainEventStatePtr state,
|
||||
int callbackID)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
|
|
@ -80,7 +80,8 @@ int virDomainEventCallbackListAddID(virConnectPtr conn,
|
|||
int eventID,
|
||||
virConnectDomainEventGenericCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback freecb)
|
||||
virFreeCallback freecb,
|
||||
int *callbackID)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(5);
|
||||
|
||||
|
||||
|
@ -242,9 +243,9 @@ virDomainEventStateDeregister(virConnectPtr conn,
|
|||
virConnectDomainEventCallback callback)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
|
||||
int
|
||||
virDomainEventStateDeregisterAny(virConnectPtr conn,
|
||||
virDomainEventStatePtr state,
|
||||
int callbackID)
|
||||
virDomainEventStateDeregisterID(virConnectPtr conn,
|
||||
virDomainEventStatePtr state,
|
||||
int callbackID)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -511,7 +511,7 @@ virDomainEventRebootNew;
|
|||
virDomainEventRebootNewFromDom;
|
||||
virDomainEventRebootNewFromObj;
|
||||
virDomainEventStateDeregister;
|
||||
virDomainEventStateDeregisterAny;
|
||||
virDomainEventStateDeregisterID;
|
||||
virDomainEventStateFlush;
|
||||
virDomainEventStateFree;
|
||||
virDomainEventStateNew;
|
||||
|
|
|
@ -3851,10 +3851,11 @@ libxlDomainEventRegisterAny(virConnectPtr conn, virDomainPtr dom, int eventID,
|
|||
int ret;
|
||||
|
||||
libxlDriverLock(driver);
|
||||
ret = virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID, callback, opaque,
|
||||
freecb);
|
||||
if (virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID, callback, opaque,
|
||||
freecb, &ret) < 0)
|
||||
ret = -1;
|
||||
libxlDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
@ -3868,9 +3869,9 @@ libxlDomainEventDeregisterAny(virConnectPtr conn, int callbackID)
|
|||
int ret;
|
||||
|
||||
libxlDriverLock(driver);
|
||||
ret = virDomainEventStateDeregisterAny(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
ret = virDomainEventStateDeregisterID(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
libxlDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -2164,10 +2164,11 @@ lxcDomainEventRegisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
lxcDriverLock(driver);
|
||||
ret = virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb);
|
||||
if (virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb, &ret) < 0)
|
||||
ret = -1;
|
||||
lxcDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
@ -2182,9 +2183,9 @@ lxcDomainEventDeregisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
lxcDriverLock(driver);
|
||||
ret = virDomainEventStateDeregisterAny(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
ret = virDomainEventStateDeregisterID(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
lxcDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -7997,10 +7997,11 @@ qemuDomainEventRegisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
qemuDriverLock(driver);
|
||||
ret = virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb);
|
||||
if (virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb, &ret) < 0)
|
||||
ret = -1;
|
||||
qemuDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
@ -8015,9 +8016,9 @@ qemuDomainEventDeregisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
qemuDriverLock(driver);
|
||||
ret = virDomainEventStateDeregisterAny(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
ret = virDomainEventStateDeregisterID(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
qemuDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -3120,6 +3120,7 @@ static int remoteDomainEventRegister(virConnectPtr conn,
|
|||
{
|
||||
int rv = -1;
|
||||
struct private_data *priv = conn->privateData;
|
||||
int count;
|
||||
|
||||
remoteDriverLock(priv);
|
||||
|
||||
|
@ -3128,15 +3129,13 @@ static int remoteDomainEventRegister(virConnectPtr conn,
|
|||
goto done;
|
||||
}
|
||||
|
||||
if (virDomainEventCallbackListAdd(conn, priv->domainEventState->callbacks,
|
||||
callback, opaque, freecb) < 0) {
|
||||
if ((count = virDomainEventCallbackListAdd(conn, priv->domainEventState->callbacks,
|
||||
callback, opaque, freecb)) < 0) {
|
||||
remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (virDomainEventCallbackListCountID(conn,
|
||||
priv->domainEventState->callbacks,
|
||||
VIR_DOMAIN_EVENT_ID_LIFECYCLE) == 1) {
|
||||
if (count == 1) {
|
||||
/* Tell the server when we are the first callback deregistering */
|
||||
if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_EVENTS_REGISTER,
|
||||
(xdrproc_t) xdr_void, (char *) NULL,
|
||||
|
@ -3760,6 +3759,7 @@ static int remoteDomainEventRegisterAny(virConnectPtr conn,
|
|||
struct private_data *priv = conn->privateData;
|
||||
remote_domain_events_register_any_args args;
|
||||
int callbackID;
|
||||
int count;
|
||||
|
||||
remoteDriverLock(priv);
|
||||
|
||||
|
@ -3768,19 +3768,18 @@ static int remoteDomainEventRegisterAny(virConnectPtr conn,
|
|||
goto done;
|
||||
}
|
||||
|
||||
if ((callbackID = virDomainEventCallbackListAddID(conn,
|
||||
priv->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb)) < 0) {
|
||||
remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
|
||||
goto done;
|
||||
if ((count = virDomainEventCallbackListAddID(conn,
|
||||
priv->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb,
|
||||
&callbackID)) < 0) {
|
||||
remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* If this is the first callback for this eventID, we need to enable
|
||||
* events on the server */
|
||||
if (virDomainEventCallbackListCountID(conn,
|
||||
priv->domainEventState->callbacks,
|
||||
eventID) == 1) {
|
||||
if (count == 1) {
|
||||
args.eventID = eventID;
|
||||
|
||||
if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_EVENTS_REGISTER_ANY,
|
||||
|
@ -3818,9 +3817,9 @@ static int remoteDomainEventDeregisterAny(virConnectPtr conn,
|
|||
goto done;
|
||||
}
|
||||
|
||||
if (virDomainEventStateDeregisterAny(conn,
|
||||
priv->domainEventState,
|
||||
callbackID) < 0)
|
||||
if (virDomainEventStateDeregisterID(conn,
|
||||
priv->domainEventState,
|
||||
callbackID) < 0)
|
||||
goto done;
|
||||
|
||||
/* If that was the last callback for this eventID, we need to disable
|
||||
|
|
|
@ -5442,10 +5442,11 @@ testDomainEventRegisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
testDriverLock(driver);
|
||||
ret = virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb);
|
||||
if (virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb, &ret) < 0)
|
||||
ret = -1;
|
||||
testDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
@ -5459,9 +5460,9 @@ testDomainEventDeregisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
testDriverLock(driver);
|
||||
ret = virDomainEventStateDeregisterAny(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
ret = virDomainEventStateDeregisterID(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
testDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -2483,10 +2483,11 @@ umlDomainEventRegisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
umlDriverLock(driver);
|
||||
ret = virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb);
|
||||
if (virDomainEventCallbackListAddID(conn,
|
||||
driver->domainEventState->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb, &ret) < 0)
|
||||
ret = -1;
|
||||
umlDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
@ -2501,9 +2502,9 @@ umlDomainEventDeregisterAny(virConnectPtr conn,
|
|||
int ret;
|
||||
|
||||
umlDriverLock(driver);
|
||||
ret = virDomainEventStateDeregisterAny(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
ret = virDomainEventStateDeregisterID(conn,
|
||||
driver->domainEventState,
|
||||
callbackID);
|
||||
umlDriverUnlock(driver);
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -7280,13 +7280,14 @@ static int vboxDomainEventRegisterAny(virConnectPtr conn,
|
|||
* later you can iterate over them
|
||||
*/
|
||||
|
||||
ret = virDomainEventCallbackListAddID(conn, data->domainEvents->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb);
|
||||
if (virDomainEventCallbackListAddID(conn, data->domainEvents->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freecb, &ret) < 0)
|
||||
ret = -1;
|
||||
VIR_DEBUG("virDomainEventCallbackListAddID (ret = %d) ( conn: %p, "
|
||||
"data->domainEvents->callbacks: %p, callback: %p, opaque: %p, "
|
||||
"freecb: %p )", ret, conn, data->domainEvents->callbacks, callback,
|
||||
opaque, freecb);
|
||||
"data->domainEvents->callbacks: %p, callback: %p, opaque: %p, "
|
||||
"freecb: %p )", ret, conn, data->domainEvents->callbacks, callback,
|
||||
opaque, freecb);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7312,8 +7313,8 @@ static int vboxDomainEventDeregisterAny(virConnectPtr conn,
|
|||
*/
|
||||
vboxDriverLock(data);
|
||||
|
||||
cnt = virDomainEventStateDeregisterAny(conn, data->domainEvents,
|
||||
callbackID);
|
||||
cnt = virDomainEventStateDeregisterID(conn, data->domainEvents,
|
||||
callbackID);
|
||||
|
||||
if (data->vboxCallback && cnt == 0) {
|
||||
data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback);
|
||||
|
|
|
@ -1897,9 +1897,10 @@ xenUnifiedDomainEventRegisterAny(virConnectPtr conn,
|
|||
return -1;
|
||||
}
|
||||
|
||||
ret = virDomainEventCallbackListAddID(conn, priv->domainEvents->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freefunc);
|
||||
if (virDomainEventCallbackListAddID(conn, priv->domainEvents->callbacks,
|
||||
dom, eventID,
|
||||
callback, opaque, freefunc, &ret) < 0)
|
||||
ret = -1;
|
||||
|
||||
xenUnifiedUnlock(priv);
|
||||
return (ret);
|
||||
|
@ -1919,9 +1920,9 @@ xenUnifiedDomainEventDeregisterAny(virConnectPtr conn,
|
|||
return -1;
|
||||
}
|
||||
|
||||
ret = virDomainEventStateDeregisterAny(conn,
|
||||
priv->domainEvents,
|
||||
callbackID);
|
||||
ret = virDomainEventStateDeregisterID(conn,
|
||||
priv->domainEvents,
|
||||
callbackID);
|
||||
|
||||
xenUnifiedUnlock(priv);
|
||||
return ret;
|
||||
|
|
Loading…
Reference in New Issue