[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH 4 of 9 v2] libxl: introduce libxl__wait_for_device_state
On Fri, 2011-11-18 at 11:59 +0000, Roger Pau Monne wrote: > # HG changeset patch > # User Roger Pau Monne <roger.pau@xxxxxxxxxxxxx> > # Date 1321609740 -3600 > # Node ID a77bba3717402fdf24a72d028ae0f3d482e6f427 > # Parent ec94a7e4983ad6338db20fa0777a85507b582607 > libxl: introduce libxl__wait_for_device_state > > This is a generic function, that waits for xs watches to reach a > certain state and then executes the passed helper function. Removed > wait_for_dev_destroy and used this new function instead. This > function will also be used by future patches that need to wait for > the initialization of devices before executing hotplug scripts. > > Signed-off-by: Roger Pau Monne <roger.pau@xxxxxxxxxxxxx> Acked-by: Ian Campbell <ian.campbell@xxxxxxxxxx> > diff -r ec94a7e4983a -r a77bba371740 tools/libxl/libxl_device.c > --- a/tools/libxl/libxl_device.c Fri Nov 18 10:46:54 2011 +0100 > +++ b/tools/libxl/libxl_device.c Fri Nov 18 10:49:00 2011 +0100 > @@ -370,7 +370,9 @@ int libxl__device_disk_dev_number(const > * Returns 0 if a device is removed, ERROR_* if an error > * or timeout occurred. > */ > -static int wait_for_dev_destroy(libxl__gc *gc, struct timeval *tv) > +int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv, > + int state, > + libxl__device_state_handler handler) > { > libxl_ctx *ctx = libxl__gc_owner(gc); > int nfds, rc; > @@ -395,17 +397,14 @@ start: > default: > l1 = xs_read_watch(ctx->xsh, &n); > if (l1 != NULL) { > - char *state = libxl__xs_read(gc, XBT_NULL, > + char *sstate = libxl__xs_read(gc, XBT_NULL, > l1[XS_WATCH_PATH]); > - if (!state || atoi(state) == 6) { > - xs_unwatch(ctx->xsh, l1[0], l1[1]); > - xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]); > - LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, > - "Destroyed device backend at %s", > - l1[XS_WATCH_TOKEN]); > - rc = 0; > + if (!sstate || atoi(sstate) == state) { > + /* Call handler function if present */ > + if (handler) > + rc = handler(gc, l1, sstate); > } else { > - /* State is not "disconnected", continue waiting... */ > + /* State is different than expected, continue waiting... > */ > goto start; > } > free(l1); > @@ -418,6 +417,23 @@ start: > } > > /* > + * Handler function for device destruction to be passed to > + * libxl__wait_for_device_state > + */ > +static int destroy_device(libxl__gc *gc, char **l1, char *state) > +{ > + libxl_ctx *ctx = libxl__gc_owner(gc); > + > + xs_unwatch(ctx->xsh, l1[0], l1[1]); > + xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]); > + LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, > + "Destroyed device backend at %s", > + l1[XS_WATCH_TOKEN]); > + > + return 0; > +} > + > +/* > * Returns 0 (device already destroyed) or 1 (caller must > * wait_for_dev_destroy) on success, ERROR_* on fail. > */ > @@ -458,7 +474,7 @@ retry_transaction: > struct timeval tv; > tv.tv_sec = LIBXL_DESTROY_TIMEOUT; > tv.tv_usec = 0; > - rc = wait_for_dev_destroy(gc, &tv); > + rc = libxl__wait_for_device_state(gc, &tv, 6, destroy_device); > if (rc < 0) /* an error or timeout occurred, clear watches */ > xs_unwatch(ctx->xsh, state_path, be_path); > xs_rm(ctx->xsh, XBT_NULL, libxl__device_frontend_path(gc, dev)); > @@ -566,7 +582,7 @@ int libxl__devices_destroy(libxl__gc *gc > tv.tv_sec = LIBXL_DESTROY_TIMEOUT; > tv.tv_usec = 0; > while (n_watches > 0) { > - if (wait_for_dev_destroy(gc, &tv) < 0) { > + if (libxl__wait_for_device_state(gc, &tv, 6, destroy_device) < > 0) { > /* function returned ERROR_* */ > break; > } else { > diff -r ec94a7e4983a -r a77bba371740 tools/libxl/libxl_internal.h > --- a/tools/libxl/libxl_internal.h Fri Nov 18 10:46:54 2011 +0100 > +++ b/tools/libxl/libxl_internal.h Fri Nov 18 10:49:00 2011 +0100 > @@ -20,6 +20,7 @@ > #include <stdint.h> > #include <stdarg.h> > #include <stdlib.h> > +#include <sys/time.h> > > #include <xs.h> > #include <xenctrl.h> > @@ -252,6 +253,31 @@ _hidden int libxl__device_destroy(libxl_ > _hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force); > _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char > *state); > > +/* Handler for the libxl__wait_for_device_state callback */ > +/* > + * libxl__device_state_handler - Handler for the libxl__wait_for_device_state > + * gc: allocation pool > + * l1: array containing the path and token > + * state: string that contains the state of the device > + * > + * Returns 0 on success, and < 0 on error. > + */ > +typedef int libxl__device_state_handler(libxl__gc *gc, char **l1, char > *state); > + > +/* > + * libxl__wait_for_device_state - waits a given time for a device to > + * reach a given state > + * gc: allocation pool > + * tv: timeval struct containing the maximum time to wait > + * state: state to wait for > + * handler: callback function to execute when state is reached > + * > + * Returns 0 on success, and < 0 on error. > + */ > +_hidden int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv, > + int state, > + libxl__device_state_handler > handler); > + > /* > * libxl__try_phy_backend - Check if there's support for the passed > * type of file using the PHY backend > > _______________________________________________ > Xen-devel mailing list > Xen-devel@xxxxxxxxxxxxxxxxxxx > http://lists.xensource.com/xen-devel _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |