From 4f5326c31576219d5931ae9785dfec990704476c Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Tue, 13 Dec 2011 23:38:54 +0000 Subject: [PATCH] Add APIs to allow management of callbacks purely with virDomainEventState While virDomainEventState has APIs for managing removal of callbacks, while locked, adding callbacks in the first place requires direct access to the virDomainEventCallbackList structure. This is not threadsafe since it is bypassing the virDomainEventState locks * src/conf/domain_event.c, src/conf/domain_event.h, src/libvirt_private.syms: Add APIs for managing callbacks via virDomainEventState. --- src/conf/domain_event.c | 114 ++++++++++++++++++++++++++++++++++++++- src/conf/domain_event.h | 24 +++++++++ src/libvirt_private.syms | 4 ++ 3 files changed, 140 insertions(+), 2 deletions(-) diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c index 00c5dbfe6f..856bc32879 100644 --- a/src/conf/domain_event.c +++ b/src/conf/domain_event.c @@ -1342,9 +1342,71 @@ virDomainEventStateFlush(virDomainEventStatePtr state, /** - * virDomainEventStateDeregister: + * virDomainEventStateRegister: + * @conn: connection to associate with callback * @state: domain event state + * @callback: function to remove from event + * @opaque: data blob to pass to callback + * @freecb: callback to free @opaque + * + * Register the function @callback with connection @conn, + * from @state, for lifecycle events. + * + * Returns: the number of lifecycle callbacks now registered, or -1 on error + */ +int virDomainEventStateRegister(virConnectPtr conn, + virDomainEventStatePtr state, + virConnectDomainEventCallback callback, + void *opaque, + virFreeCallback freecb) +{ + int ret; + virDomainEventStateLock(state); + ret = virDomainEventCallbackListAdd(conn, state->callbacks, + callback, opaque, freecb); + virDomainEventStateUnlock(state); + return ret; +} + + +/** + * virDomainEventStateRegisterID: + * @conn: connection to associate with callback + * @state: domain event state + * @eventID: ID of the event type to register for + * @cb: function to remove from event + * @opaque: data blob to pass to callback + * @freecb: callback to free @opaque + * @callbackID: filled with callback ID + * + * Register the function @callbackID with connection @conn, + * from @state, for events of type @eventID. + * + * Returns: the number of callbacks now registered, or -1 on error + */ +int virDomainEventStateRegisterID(virConnectPtr conn, + virDomainEventStatePtr state, + virDomainPtr dom, + int eventID, + virConnectDomainEventGenericCallback cb, + void *opaque, + virFreeCallback freecb, + int *callbackID) +{ + int ret; + virDomainEventStateLock(state); + ret = virDomainEventCallbackListAddID(conn, state->callbacks, + dom, eventID, cb, opaque, freecb, + callbackID); + virDomainEventStateUnlock(state); + return ret; +} + + +/** + * virDomainEventStateDeregister: * @conn: connection to associate with callback + * @state: domain event state * @callback: function to remove from event * * Unregister the function @callback with connection @conn, @@ -1372,8 +1434,8 @@ virDomainEventStateDeregister(virConnectPtr conn, /** * virDomainEventStateDeregisterID: - * @state: domain event state * @conn: connection to associate with callback + * @state: domain event state * @callbackID: ID of the function to remove from event * * Unregister the function @callbackID with connection @conn, @@ -1398,3 +1460,51 @@ virDomainEventStateDeregisterID(virConnectPtr conn, virDomainEventStateUnlock(state); return ret; } + + +/** + * virDomainEventStateDeregisterConn: + * @conn: connection to associate with callbacks + * @state: domain event state + * + * Remove all callbacks from @state associated with the + * connection @conn + * + * Returns 0 on success, -1 on error + */ +int +virDomainEventStateDeregisterConn(virConnectPtr conn, + virDomainEventStatePtr state) +{ + int ret; + virDomainEventStateLock(state); + ret = virDomainEventCallbackListRemoveConn(conn, state->callbacks); + virDomainEventStateUnlock(state); + return ret; +} + + +/** + * virDomainEventStateEventID: + * @conn: connection associated with the callback + * @state: domain event state + * @callbackID: the callback to query + * + * Query what event ID type is associated with the + * callback @callbackID for connection @conn + * + * Returns 0 on success, -1 on error + */ +int +virDomainEventStateEventID(virConnectPtr conn, + virDomainEventStatePtr state, + int callbackID) +{ + int ret; + + virDomainEventStateLock(state); + ret = virDomainEventCallbackListEventID(conn, + state->callbacks, callbackID); + virDomainEventStateUnlock(state); + return ret; +} diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h index 83656e61ab..7eefadb329 100644 --- a/src/conf/domain_event.h +++ b/src/conf/domain_event.h @@ -237,6 +237,21 @@ virDomainEventStateFlush(virDomainEventStatePtr state, virDomainEventDispatchFunc dispatchFunc, void *opaque) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +int virDomainEventStateRegister(virConnectPtr conn, + virDomainEventStatePtr state, + virConnectDomainEventCallback callback, + void *opaque, + virFreeCallback freecb) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +int virDomainEventStateRegisterID(virConnectPtr conn, + virDomainEventStatePtr state, + virDomainPtr dom, + int eventID, + virConnectDomainEventGenericCallback cb, + void *opaque, + virFreeCallback freecb, + int *callbackID) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5); int virDomainEventStateDeregister(virConnectPtr conn, virDomainEventStatePtr state, @@ -247,5 +262,14 @@ virDomainEventStateDeregisterID(virConnectPtr conn, virDomainEventStatePtr state, int callbackID) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +int +virDomainEventStateDeregisterConn(virConnectPtr conn, + virDomainEventStatePtr state) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +int +virDomainEventStateEventID(virConnectPtr conn, + virDomainEventStatePtr state, + int callbackID) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); #endif diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index de74568714..ab1231ec6e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -512,6 +512,10 @@ virDomainEventRebootNewFromDom; virDomainEventRebootNewFromObj; virDomainEventStateDeregister; virDomainEventStateDeregisterID; +virDomainEventStateDeregisterConn; +virDomainEventStateEventID; +virDomainEventStateRegister; +virDomainEventStateRegisterID; virDomainEventStateFlush; virDomainEventStateFree; virDomainEventStateNew; -- GitLab