On Thu, 2013-12-12 at 18:04 +0000, Daniel P. Berrange wrote: > From: "Daniel P. Berrange" <berrange@xxxxxxxxxx> > > Instead of having the object event code have to know about each > type of event and their dispatch functions, associate a dispatch > function with the object instance. The dispatch code can thus be > significantly simplified. > > Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx> > --- > src/conf/domain_event.c | 16 ++++++--- > src/conf/domain_event.h | 7 ---- > src/conf/network_event.c | 8 ++--- > src/conf/network_event.h | 6 ---- > src/conf/object_event.c | 72 +++++++++++------------------------------ > src/conf/object_event_private.h | 9 +++++- > 6 files changed, 42 insertions(+), 76 deletions(-) > > diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c > index 64035f7..a9c499e 100644 > --- a/src/conf/domain_event.c > +++ b/src/conf/domain_event.c > @@ -61,6 +61,11 @@ static void virDomainEventTrayChangeDispose(void *obj); > static void virDomainEventBalloonChangeDispose(void *obj); > static void virDomainEventDeviceRemovedDispose(void *obj); > > +static void > +virDomainEventDispatchDefaultFunc(virConnectPtr conn, > + virObjectEventPtr event, > + virConnectObjectEventGenericCallback cb, > + void *cbopaque); > > struct _virDomainEvent { > virObjectEvent parent; > @@ -462,7 +467,9 @@ virDomainEventNew(virClassPtr klass, > return NULL; > } > > - if (!(event = virObjectEventNew(klass, eventID, > + if (!(event = virObjectEventNew(klass, > + virDomainEventDispatchDefaultFunc, > + eventID, > id, name, uuid))) > return NULL; > > @@ -1160,12 +1167,11 @@ virDomainEventDeviceRemovedNewFromDom(virDomainPtr dom, > } > > > -void > +static void > virDomainEventDispatchDefaultFunc(virConnectPtr conn, > virObjectEventPtr event, > - virConnectDomainEventGenericCallback cb, > - void *cbopaque, > - void *opaque ATTRIBUTE_UNUSED) > + virConnectObjectEventGenericCallback cb, > + void *cbopaque) > { > virDomainPtr dom = virGetDomain(conn, event->meta.name, event->meta.uuid); > int eventID = virObjectEventGetEventID(event); > diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h > index 30156ed..b2ea580 100644 > --- a/src/conf/domain_event.h > +++ b/src/conf/domain_event.h > @@ -197,11 +197,4 @@ virDomainEventStateDeregister(virConnectPtr conn, > virConnectDomainEventCallback callback) > ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); > > -void > -virDomainEventDispatchDefaultFunc(virConnectPtr conn, > - virObjectEventPtr event, > - virConnectDomainEventGenericCallback cb, > - void *cbopaque, > - void *opaque); > - > #endif > diff --git a/src/conf/network_event.c b/src/conf/network_event.c > index b1312b7..3173e68 100644 > --- a/src/conf/network_event.c > +++ b/src/conf/network_event.c > @@ -87,12 +87,11 @@ virNetworkEventLifecycleDispose(void *obj) > } > > > -void > +static void > virNetworkEventDispatchDefaultFunc(virConnectPtr conn, > virObjectEventPtr event, > - virConnectNetworkEventGenericCallback cb ATTRIBUTE_UNUSED, > - void *cbopaque ATTRIBUTE_UNUSED, > - void *opaque ATTRIBUTE_UNUSED) > + virConnectObjectEventGenericCallback cb, > + void *cbopaque) > { > virNetworkPtr net = virGetNetwork(conn, event->meta.name, event->meta.uuid); > if (!net) > @@ -171,6 +170,7 @@ virNetworkEventLifecycleNew(const char *name, > return NULL; > > if (!(event = virObjectEventNew(virNetworkEventLifecycleClass, > + virNetworkEventDispatchDefaultFunc, > eventId, > 0, name, uuid))) > return NULL; > diff --git a/src/conf/network_event.h b/src/conf/network_event.h > index 1eef771..a1afbc5 100644 > --- a/src/conf/network_event.h > +++ b/src/conf/network_event.h > @@ -43,10 +43,4 @@ virNetworkEventLifecycleNew(const char *name, > int type, > int detail); > > -void > -virNetworkEventDispatchDefaultFunc(virConnectPtr conn, > - virObjectEventPtr event, > - virConnectNetworkEventGenericCallback cb, > - void *cbopaque, > - void *opaque); > #endif > diff --git a/src/conf/object_event.c b/src/conf/object_event.c > index ec00aaf..96c719c 100644 > --- a/src/conf/object_event.c > +++ b/src/conf/object_event.c > @@ -466,6 +466,7 @@ error: > > void * > virObjectEventNew(virClassPtr klass, > + virObjectEventDispatchFunc dispatcher, > int eventID, > int id, > const char *name, > @@ -486,6 +487,7 @@ virObjectEventNew(virClassPtr klass, > if (!(event = virObjectNew(klass))) > return NULL; > > + event->dispatch = dispatcher; > event->eventID = eventID; > > if (VIR_STRDUP(event->meta.name, name) < 0) { > @@ -527,13 +529,6 @@ virObjectEventQueuePush(virObjectEventQueuePtr evtQueue, > } > > > -typedef void (*virObjectEventDispatchFunc)(virConnectPtr conn, > - virObjectEventPtr event, > - virConnectObjectEventGenericCallback cb, > - void *cbopaque, > - void *opaque); > - > - > static int > virObjectEventDispatchMatchCallback(virObjectEventPtr event, > virObjectEventCallbackPtr cb) > @@ -563,10 +558,9 @@ virObjectEventDispatchMatchCallback(virObjectEventPtr event, > > > static void > -virObjectEventDispatch(virObjectEventPtr event, > - virObjectEventCallbackListPtr callbacks, > - virObjectEventDispatchFunc dispatch, > - void *opaque) > +virObjectEventStateDispatchCallbacks(virObjectEventStatePtr state, > + virObjectEventPtr event, > + virObjectEventCallbackListPtr callbacks) > { > size_t i; > /* Cache this now, since we may be dropping the lock, > @@ -578,25 +572,26 @@ virObjectEventDispatch(virObjectEventPtr event, > if (!virObjectEventDispatchMatchCallback(event, callbacks->callbacks[i])) > continue; > > - (*dispatch)(callbacks->callbacks[i]->conn, > - event, > - callbacks->callbacks[i]->cb, > - callbacks->callbacks[i]->opaque, > - opaque); > + /* Drop the lock whle dispatching, for sake of re-entrancy */ > + virObjectEventStateUnlock(state); > + event->dispatch(callbacks->callbacks[i]->conn, > + event, > + callbacks->callbacks[i]->cb, > + callbacks->callbacks[i]->opaque); > + virObjectEventStateLock(state); > } > } > > > static void > -virObjectEventQueueDispatch(virObjectEventQueuePtr queue, > - virObjectEventCallbackListPtr callbacks, > - virObjectEventDispatchFunc dispatch, > - void *opaque) > +virObjectEventStateQueueDispatch(virObjectEventStatePtr state, > + virObjectEventQueuePtr queue, > + virObjectEventCallbackListPtr callbacks) > { > size_t i; > > for (i = 0; i < queue->count; i++) { > - virObjectEventDispatch(queue->events[i], callbacks, dispatch, opaque); > + virObjectEventStateDispatchCallbacks(state, queue->events[i], callbacks); > virObjectUnref(queue->events[i]); > } > VIR_FREE(queue->events); > @@ -626,34 +621,6 @@ virObjectEventStateQueue(virObjectEventStatePtr state, > > > static void > -virObjectEventStateDispatchFunc(virConnectPtr conn, > - virObjectEventPtr event, > - virConnectObjectEventGenericCallback cb, > - void *cbopaque, > - void *opaque) > -{ > - virObjectEventStatePtr state = opaque; > - virEventNamespaceID namespace = (event->eventID & 0xFF00) >> 8; > - > - /* Drop the lock whle dispatching, for sake of re-entrancy */ > - virObjectEventStateUnlock(state); > - switch (namespace) { > - case VIR_EVENT_NAMESPACE_DOMAIN: > - virDomainEventDispatchDefaultFunc(conn, event, > - VIR_DOMAIN_EVENT_CALLBACK(cb), cbopaque, NULL); > - break; > - case VIR_EVENT_NAMESPACE_NETWORK: > - virNetworkEventDispatchDefaultFunc(conn, event, > - VIR_NETWORK_EVENT_CALLBACK(cb), cbopaque, NULL); > - break; > - default: > - VIR_ERROR(_("Unknown event namespace to dispatch")); > - } > - virObjectEventStateLock(state); > -} > - > - > -static void > virObjectEventStateFlush(virObjectEventStatePtr state) > { > virObjectEventQueue tempQueue; > @@ -669,10 +636,9 @@ virObjectEventStateFlush(virObjectEventStatePtr state) > state->queue->events = NULL; > virEventUpdateTimeout(state->timer, -1); > > - virObjectEventQueueDispatch(&tempQueue, > - state->callbacks, > - virObjectEventStateDispatchFunc, > - state); > + virObjectEventStateQueueDispatch(state, > + &tempQueue, > + state->callbacks); > > /* Purge any deleted callbacks */ > virObjectEventCallbackListPurgeMarked(state->callbacks); > diff --git a/src/conf/object_event_private.h b/src/conf/object_event_private.h > index f41f432..f18277a 100644 > --- a/src/conf/object_event_private.h > +++ b/src/conf/object_event_private.h > @@ -69,10 +69,17 @@ struct _virObjectEventCallback { > int deleted; > }; > > +typedef void > +(*virObjectEventDispatchFunc)(virConnectPtr conn, > + virObjectEventPtr event, > + virConnectObjectEventGenericCallback cb, > + void *cbopaque); > + > struct _virObjectEvent { > virObject parent; > int eventID; > virObjectMeta meta; > + virObjectEventDispatchFunc dispatch; > }; > > virClassPtr > @@ -108,10 +115,10 @@ virObjectEventTimer(int timer, > > void * > virObjectEventNew(virClassPtr klass, > + virObjectEventDispatchFunc dispatcher, > int eventID, > int id, > const char *name, > const unsigned char *uuid); > > - > #endif ACK. A clever way to get rid of the initialization problem. -- Cedric -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list