[PATCH v1 05/12] bhyve: monitor: refactor register/unregister

Ryan Moeller posted 12 patches 5 years, 11 months ago
[PATCH v1 05/12] bhyve: monitor: refactor register/unregister
Posted by Ryan Moeller 5 years, 11 months ago
Pull the code for registering and unregistering a bhyve monitor object
into separate functions to improve code clarity.

Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
---
 src/bhyve/bhyve_monitor.c | 50 ++++++++++++++++++++++++++-------------
 1 file changed, 33 insertions(+), 17 deletions(-)

diff --git a/src/bhyve/bhyve_monitor.c b/src/bhyve/bhyve_monitor.c
index 58d40e1f70..a1b1a21a6f 100644
--- a/src/bhyve/bhyve_monitor.c
+++ b/src/bhyve/bhyve_monitor.c
@@ -69,6 +69,37 @@ bhyveMonitorOnceInit(void)
 
 VIR_ONCE_GLOBAL_INIT(bhyveMonitor);
 
+static void bhyveMonitorIO(int, int, int, void *);
+
+static bool
+bhyveMonitorRegister(bhyveMonitorPtr mon)
+{
+    virObjectRef(mon);
+    mon->watch = virEventAddHandle(mon->kq,
+                                   VIR_EVENT_HANDLE_READABLE |
+                                   VIR_EVENT_HANDLE_ERROR |
+                                   VIR_EVENT_HANDLE_HANGUP,
+                                   bhyveMonitorIO,
+                                   mon,
+                                   virObjectFreeCallback);
+    if (mon->watch < 0) {
+        VIR_DEBUG("failed to add event handle for mon %p", mon);
+        virObjectUnref(mon);
+        return false;
+    }
+    return true;
+}
+
+static void
+bhyveMonitorUnregister(bhyveMonitorPtr mon)
+{
+    if (mon->watch < 0)
+        return;
+
+    virEventRemoveHandle(mon->watch);
+    mon->watch = -1;
+}
+
 static void
 bhyveMonitorIO(int watch, int kq, int events G_GNUC_UNUSED, void *opaque)
 {
@@ -166,17 +197,7 @@ bhyveMonitorOpenImpl(virDomainObjPtr vm, bhyveConnPtr driver)
         goto cleanup;
     }
 
-    virObjectRef(mon);
-    mon->watch = virEventAddHandle(mon->kq,
-                                   VIR_EVENT_HANDLE_READABLE |
-                                   VIR_EVENT_HANDLE_ERROR |
-                                   VIR_EVENT_HANDLE_HANGUP,
-                                   bhyveMonitorIO,
-                                   mon,
-                                   virObjectFreeCallback);
-    if (mon->watch < 0) {
-        VIR_DEBUG("failed to add event handle for mon %p", mon);
-        virObjectUnref(mon);
+    if (!bhyveMonitorRegister(mon)) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to register monitor events"));
         goto cleanup;
@@ -209,11 +230,6 @@ bhyveMonitorClose(bhyveMonitorPtr mon)
 
     VIR_DEBUG("cleaning up bhyveMonitor %p", mon);
 
-    if (mon->watch < 0)
-        return;
-
-    virEventRemoveHandle(mon->watch);
-    mon->watch = -1;
-
+    bhyveMonitorUnregister(mon);
     virObjectUnref(mon);
 }
-- 
2.24.1


Re: [PATCH v1 05/12] bhyve: monitor: refactor register/unregister
Posted by Daniel P. Berrangé 5 years, 11 months ago
On Mon, Feb 24, 2020 at 01:46:17AM -0500, Ryan Moeller wrote:
> Pull the code for registering and unregistering a bhyve monitor object
> into separate functions to improve code clarity.
> 
> Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
> ---
>  src/bhyve/bhyve_monitor.c | 50 ++++++++++++++++++++++++++-------------
>  1 file changed, 33 insertions(+), 17 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|