diff mbox series

[v2,35/35] libxl_pci: Extract common part of *qemu_trad_watch_state_cb

Message ID 20190919171656.899649-36-anthony.perard@citrix.com (mailing list archive)
State New, archived
Headers show
Series libxl refactoring to use ev_qmp (with API changes) | expand

Commit Message

Anthony PERARD Sept. 19, 2019, 5:16 p.m. UTC
Functions pci_add_qemu_trad_watch_state_cb and
pci_remove_qemu_trad_watch_state_cb are similar so the common part is
extracted in a different function.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---

Notes:
    v2:
    - new patch which attempt to combine the "basically-identical" functions
      pci_{add,remove}_qemu_trad_watch_state_cb

 tools/libxl/libxl_pci.c | 51 +++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 22 deletions(-)

Comments

Ian Jackson Sept. 19, 2019, 7:18 p.m. UTC | #1
Anthony PERARD writes ("[PATCH v2 35/35] libxl_pci: Extract common part of *qemu_trad_watch_state_cb"):
> Functions pci_add_qemu_trad_watch_state_cb and
> pci_remove_qemu_trad_watch_state_cb are similar so the common part is
> extracted in a different function.

OK (though not quite how I would have done it; maybe simpler than what
I had in mind).

Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
diff mbox series

Patch

diff --git a/tools/libxl/libxl_pci.c b/tools/libxl/libxl_pci.c
index 2c4e2e5cff5e..47258177bc71 100644
--- a/tools/libxl/libxl_pci.c
+++ b/tools/libxl/libxl_pci.c
@@ -984,6 +984,27 @@  static int qemu_pci_add_xenstore(libxl__gc *gc, uint32_t domid,
     return rc;
 }
 
+static int check_qemu_running(libxl__gc *gc,
+                              libxl_domid domid,
+                              libxl__xswait_state *xswa,
+                              int rc,
+                              const char *state)
+{
+    if (rc) {
+        if (rc == ERROR_TIMEDOUT) {
+            LOGD(ERROR, domid, "%s not ready", xswa->what);
+        }
+        goto out;
+    }
+
+    if (!state || strcmp(state, "running"))
+        return ERROR_NOT_READY;
+
+out:
+    libxl__xswait_stop(gc, xswa);
+    return rc;
+}
+
 typedef struct pci_add_state {
     /* filled by user of do_pci_add */
     libxl__ao_device *aodev;
@@ -1071,21 +1092,14 @@  static void pci_add_qemu_trad_watch_state_cb(libxl__egc *egc,
     libxl_domid domid = pas->domid;
     libxl_device_pci *pcidev = pas->pcidev;
 
-    if (rc) {
-        if (rc == ERROR_TIMEDOUT) {
-            LOGD(ERROR, domid, "%s not ready", xswa->what);
-        }
-        goto out;
-    }
-
-    if (!state)
-        return;
-    if (strcmp(state, "running"))
+    rc = check_qemu_running(gc, domid, xswa, rc, state);
+    if (rc == ERROR_NOT_READY)
         return;
+    if (rc)
+        goto out;
 
     rc = qemu_pci_add_xenstore(gc, domid, pcidev);
 out:
-    libxl__xswait_stop(gc, xswa);
     pci_add_dm_done(egc, pas, rc); /* must be last */
 }
 
@@ -1893,22 +1907,15 @@  static void pci_remove_qemu_trad_watch_state_cb(libxl__egc *egc,
     libxl_domid domid = prs->domid;
     libxl_device_pci *const pcidev = prs->pcidev;
 
-    if (rc) {
-        if (rc == ERROR_TIMEDOUT) {
-            LOGD(ERROR, domid, "%s not ready", xswa->what);
-        }
-        goto out;
-    }
-
-    if (!state)
-        return;
-    if (strcmp(state, "running"))
+    rc = check_qemu_running(gc, domid, xswa, rc, state);
+    if (rc == ERROR_NOT_READY)
         return;
+    if (rc)
+        goto out;
 
     rc = qemu_pci_remove_xenstore(gc, domid, pcidev, prs->force);
 
 out:
-    libxl__xswait_stop(gc, xswa);
     pci_remove_detatched(egc, prs, rc);
 }