diff options
author | bz <bz@FreeBSD.org> | 2010-03-19 19:51:03 +0000 |
---|---|---|
committer | bz <bz@FreeBSD.org> | 2010-03-19 19:51:03 +0000 |
commit | 102a1f8933fc21bc4d59fefc7cf937a79852efd3 (patch) | |
tree | f7449f42c29dbbbb2dfc5e32c28e23f633214e78 /sys/kern | |
parent | 1d097a47c39ac0c2575579a65e78796174fba8d3 (diff) | |
download | FreeBSD-src-102a1f8933fc21bc4d59fefc7cf937a79852efd3.zip FreeBSD-src-102a1f8933fc21bc4d59fefc7cf937a79852efd3.tar.gz |
Split eventhandler_register() into an internal part and a wrapper function
that provides the allocated and setup eventhandler entry.
Add a new wrapper for VIMAGE that allocates extra space to hold the
callback function and argument in addition to an extra wrapper function.
While the wrapper function goes as normal callback function the
argument points to the extra space allocated holding the original func
and arg that the wrapper function can then call.
Provide an iterator function for the virtual network stack (vnet) that
will call the callback function for each network stack.
Provide a new set of macros for VNET that in the non-VIMAGE case will
just call eventhandler_register() while in the VIMAGE case it will use
vimage_eventhandler_register() passing in the extra iterator function
but will only register once rather than per-vnet.
We need a special macro in case we are interested in the tag returned
as we must check for curvnet and can neither simply assign the
return value, nor not change it in the non-vnet0 case without that.
Sponsored by: ISPsystem
Discussed with: jhb
Reviewed by: zec (earlier version), jhb
MFC after: 1 month
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/subr_eventhandler.c | 71 |
1 files changed, 54 insertions, 17 deletions
diff --git a/sys/kern/subr_eventhandler.c b/sys/kern/subr_eventhandler.c index 37c482c..5894099 100644 --- a/sys/kern/subr_eventhandler.c +++ b/sys/kern/subr_eventhandler.c @@ -68,15 +68,15 @@ SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init, * Insertion is O(n) due to the priority scan, but optimises to O(1) * if all priorities are identical. */ -eventhandler_tag -eventhandler_register(struct eventhandler_list *list, const char *name, - void *func, void *arg, int priority) +static eventhandler_tag +eventhandler_register_internal(struct eventhandler_list *list, + const char *name, eventhandler_tag epn) { struct eventhandler_list *new_list; - struct eventhandler_entry_generic *eg; struct eventhandler_entry *ep; KASSERT(eventhandler_lists_initted, ("eventhandler registered too early")); + KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__)); /* lock the eventhandler lists */ mtx_lock(&eventhandler_mutex); @@ -117,31 +117,68 @@ eventhandler_register(struct eventhandler_list *list, const char *name, } mtx_unlock(&eventhandler_mutex); - /* allocate an entry for this handler, populate it */ - eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER, - M_WAITOK | M_ZERO); - eg->func = func; - eg->ee.ee_arg = arg; - eg->ee.ee_priority = priority; - KASSERT(priority != EHE_DEAD_PRIORITY, + KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY, ("%s: handler for %s registered with dead priority", __func__, name)); /* sort it into the list */ - CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, eg, - func, name); + CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn, + ((struct eventhandler_entry_generic *)epn)->func, name); EHL_LOCK(list); TAILQ_FOREACH(ep, &list->el_entries, ee_link) { if (ep->ee_priority != EHE_DEAD_PRIORITY && - eg->ee.ee_priority < ep->ee_priority) { - TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link); + epn->ee_priority < ep->ee_priority) { + TAILQ_INSERT_BEFORE(ep, epn, ee_link); break; } } if (ep == NULL) - TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link); + TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link); EHL_UNLOCK(list); - return(&eg->ee); + return(epn); +} + +eventhandler_tag +eventhandler_register(struct eventhandler_list *list, const char *name, + void *func, void *arg, int priority) +{ + struct eventhandler_entry_generic *eg; + + /* allocate an entry for this handler, populate it */ + eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER, + M_WAITOK | M_ZERO); + eg->func = func; + eg->ee.ee_arg = arg; + eg->ee.ee_priority = priority; + + return (eventhandler_register_internal(list, name, &eg->ee)); +} + +#ifdef VIMAGE +struct eventhandler_entry_generic_vimage +{ + struct eventhandler_entry ee; + vimage_iterator_func_t func; /* Vimage iterator function. */ + struct eventhandler_entry_vimage v_ee; /* Original func, arg. */ +}; + +eventhandler_tag +vimage_eventhandler_register(struct eventhandler_list *list, const char *name, + void *func, void *arg, int priority, vimage_iterator_func_t iterfunc) +{ + struct eventhandler_entry_generic_vimage *eg; + + /* allocate an entry for this handler, populate it */ + eg = malloc(sizeof(struct eventhandler_entry_generic_vimage), + M_EVENTHANDLER, M_WAITOK | M_ZERO); + eg->func = iterfunc; + eg->v_ee.func = func; + eg->v_ee.ee_arg = arg; + eg->ee.ee_arg = &eg->v_ee; + eg->ee.ee_priority = priority; + + return (eventhandler_register_internal(list, name, &eg->ee)); } +#endif void eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) |