xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 8/9] libxl: Introduce libxl__ev_devstate
Date: Fri, 13 Jan 2012 19:25:27 +0000	[thread overview]
Message-ID: <1326482728-10733-9-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1326482728-10733-1-git-send-email-ian.jackson@eu.citrix.com>

Provide a new-style asynchronous facility for waiting for device
states on xenbus.  This will replace libxl__wait_for_device_state,
after the callers have been updated in later patches.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
 tools/libxl/libxl_event.c    |   75 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   41 +++++++++++++++++++++++
 2 files changed, 116 insertions(+), 0 deletions(-)

diff --git a/tools/libxl/libxl_event.c b/tools/libxl/libxl_event.c
index b99049a..1d271b8 100644
--- a/tools/libxl/libxl_event.c
+++ b/tools/libxl/libxl_event.c
@@ -507,6 +507,81 @@ void libxl__ev_xswatch_deregister(libxl__gc *gc, libxl__ev_xswatch *w)
 }
 
 /*
+ * waiting for device state
+ */
+
+static void devstate_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,
+                                const char *watch_path, const char *event_path)
+{
+    EGC_GC;
+    libxl__ev_devstate *ds = CONTAINER_OF(watch, *ds, watch);
+    int rc;
+
+    char *sstate = libxl__xs_read(gc, XBT_NULL, watch_path);
+    if (!sstate) {
+        if (errno == ENOENT) {
+            LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d"
+                       " but it was removed", watch_path, ds->wanted);
+            rc = ERROR_INVAL;
+        } else {
+            LIBXL__LOG_ERRNO(CTX, LIBXL__LOG_ERROR, "backend %s wanted state"
+                             " %d but read failed", watch_path, ds->wanted);
+            rc = ERROR_FAIL;
+        }
+    } else {
+        int got = atoi(sstate);
+        if (got == ds->wanted) {
+            LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d ok",
+                       watch_path, ds->wanted);
+            rc = 0;
+        } else {
+            LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d"
+                       " still waiting state %d", watch_path, ds->wanted, got);
+            return;
+        }
+    }
+    libxl__ev_devstate_cancel(gc, ds);
+    ds->callback(egc, ds, rc);
+}
+
+static void devstate_timeout(libxl__egc *egc, libxl__ev_time *ev,
+                             const struct timeval *requested_abs)
+{
+    EGC_GC;
+    libxl__ev_devstate *ds = CONTAINER_OF(ev, *ds, timeout);
+    LIBXL__LOG(CTX, LIBXL__LOG_DEBUG, "backend %s wanted state %d "
+               " timed out", ds->watch.path, ds->wanted);
+    libxl__ev_devstate_cancel(gc, ds);
+    ds->callback(egc, ds, ERROR_TIMEDOUT);
+}
+
+int libxl__ev_devstate_wait(libxl__gc *gc, libxl__ev_devstate *ds,
+                            libxl__ev_devstate_callback cb,
+                            const char *state_path, int state, int milliseconds)
+{
+    int rc;
+
+    libxl__ev_time_init(&ds->timeout);
+    libxl__ev_xswatch_init(&ds->watch);
+    ds->wanted = state;
+    ds->callback = cb;
+
+    rc = libxl__ev_time_register_rel(gc, &ds->timeout, devstate_timeout,
+                                     milliseconds);
+    if (rc) goto out;
+
+    rc = libxl__ev_xswatch_register(gc, &ds->watch, devstate_watch_callback,
+                                    state_path);
+    if (rc) goto out;
+
+    return 0;
+
+ out:
+    libxl__ev_devstate_cancel(gc, ds);
+    return rc;
+}
+
+/*
  * osevent poll
  */
 
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 213b5f9..b7f0f54 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -684,6 +684,47 @@ _hidden int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv,
                                          libxl__device_state_handler handler);
 
 /*
+ * libxl__ev_devstate - waits a given time for a device to
+ * reach a given state.  Follows the libxl_ev_* conventions.
+ * Will generate only one event, and after that is automatically
+ * cancelled.
+ */
+typedef struct libxl__ev_devstate libxl__ev_devstate;
+typedef void libxl__ev_devstate_callback(libxl__egc *egc, libxl__ev_devstate*,
+                                         int rc);
+  /* rc will be 0, ERROR_TIMEDOUT, ERROR_INVAL (meaning path was removed),
+   * or ERROR_FAIL if other stuff went wrong (in which latter case, logged) */
+
+struct libxl__ev_devstate {
+    /* read-only for caller, who may read only when waiting: */
+    int wanted;
+    libxl__ev_devstate_callback *callback;
+    /* as for the remainder, read-only public parts may also be
+     * read by the caller (notably, watch.path), but only when waiting: */
+    libxl__ev_xswatch watch;
+    libxl__ev_time timeout;
+};
+
+static inline void libxl__ev_devstate_init(libxl__ev_devstate *ds)
+{
+    libxl__ev_time_init(&ds->timeout);
+    libxl__ev_xswatch_init(&ds->watch);
+}
+
+static inline void libxl__ev_devstate_cancel(libxl__gc *gc,
+                                             libxl__ev_devstate *ds)
+{
+    libxl__ev_time_deregister(gc,&ds->timeout);
+    libxl__ev_xswatch_deregister(gc,&ds->watch);
+}
+
+_hidden int libxl__ev_devstate_wait(libxl__gc *gc, libxl__ev_devstate *ds,
+                                    libxl__ev_devstate_callback cb,
+                                    const char *state_path,
+                                    int state, int milliseconds);
+
+
+/*
  * libxl__try_phy_backend - Check if there's support for the passed
  * type of file using the PHY backend
  * st_mode: mode_t of the file, as returned by stat function
-- 
1.7.2.5

  parent reply	other threads:[~2012-01-13 19:25 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-13 19:25 [PATCH v7 0/9] libxl: New event API Ian Jackson
2012-01-13 19:25 ` [PATCH 1/9] libxl: New API for providing OS events to libxl Ian Jackson
2012-01-18 16:35   ` Ian Campbell
2012-01-18 17:06     ` Ian Jackson
2012-01-13 19:25 ` [PATCH 2/9] ocaml, libxl: support "private" fields Ian Jackson
2012-01-18 14:03   ` Ian Campbell
2012-01-13 19:25 ` [PATCH 3/9] libxl: New event generation API Ian Jackson
2012-01-18 17:33   ` Ian Campbell
2012-01-24 16:23     ` Ian Jackson
2012-01-24 16:38       ` Ian Campbell
2012-01-24 18:43         ` Ian Jackson
2012-01-13 19:25 ` [PATCH 4/9] libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec Ian Jackson
2012-01-13 19:25 ` [PATCH 5/9] libxl: Permit multithreaded event waiting Ian Jackson
2012-01-19 10:01   ` Ian Campbell
2012-01-24 16:34     ` Ian Jackson
2012-01-13 19:25 ` [PATCH 6/9] libxl: Asynchronous/long-running operation infrastructure Ian Jackson
2012-01-19 10:44   ` Ian Campbell
2012-01-24 17:27     ` Ian Jackson
2012-01-25 10:48       ` Ian Campbell
2012-01-25 14:45         ` Ian Jackson
2012-01-25 16:56           ` Ian Jackson
2012-01-13 19:25 ` [PATCH 7/9] libxl: New convenience macro CONTAINER_OF Ian Jackson
2012-01-18 14:04   ` Ian Campbell
2012-01-13 19:25 ` Ian Jackson [this message]
2012-01-19 10:54   ` [PATCH 8/9] libxl: Introduce libxl__ev_devstate Ian Campbell
2012-01-24 17:33     ` Ian Jackson
2012-01-25 10:57       ` Ian Campbell
2012-01-25 14:49         ` Ian Jackson
2012-01-13 19:25 ` [PATCH 9/9] libxl: Convert to asynchronous: device removal Ian Jackson
2012-01-19 11:55   ` Ian Campbell
2012-01-24 17:39     ` Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1326482728-10733-9-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).