[libvirt PATCH 09/10] ch_monitor: Use automatic mutex management

Tim Wiederhake twiederh at redhat.com
Fri Mar 25 15:02:21 UTC 2022


Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/ch/ch_monitor.c | 55 ++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 31 deletions(-)

diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c
index 84085b7991..2c6b83a1b5 100644
--- a/src/ch/ch_monitor.c
+++ b/src/ch/ch_monitor.c
@@ -648,14 +648,13 @@ virCHMonitorCurlPerform(CURL *handle)
 int
 virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
 {
+    VIR_LOCK_GUARD lock = virObjectLockGuard(mon);
     g_autofree char *url = NULL;
     int responseCode = 0;
     int ret = -1;
 
     url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
 
-    virObjectLock(mon);
-
     /* reset all options of a libcurl session handle at first */
     curl_easy_reset(mon->handle);
 
@@ -666,8 +665,6 @@ virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
 
     responseCode = virCHMonitorCurlPerform(mon->handle);
 
-    virObjectUnlock(mon);
-
     if (responseCode == 200 || responseCode == 204)
         ret = 0;
 
@@ -707,26 +704,24 @@ virCHMonitorGet(virCHMonitor *mon, const char *endpoint, virJSONValue **response
 
     url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
 
-    virObjectLock(mon);
+    VIR_WITH_OBJECT_LOCK_GUARD(mon) {
+        /* reset all options of a libcurl session handle at first */
+        curl_easy_reset(mon->handle);
 
-    /* reset all options of a libcurl session handle at first */
-    curl_easy_reset(mon->handle);
+        curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
+        curl_easy_setopt(mon->handle, CURLOPT_URL, url);
 
-    curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
-    curl_easy_setopt(mon->handle, CURLOPT_URL, url);
+        if (response) {
+            headers = curl_slist_append(headers, "Accept: application/json");
+            headers = curl_slist_append(headers, "Content-Type: application/json");
+            curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
+            curl_easy_setopt(mon->handle, CURLOPT_WRITEFUNCTION, curl_callback);
+            curl_easy_setopt(mon->handle, CURLOPT_WRITEDATA, (void *)&data);
+        }
 
-    if (response) {
-        headers = curl_slist_append(headers, "Accept: application/json");
-        headers = curl_slist_append(headers, "Content-Type: application/json");
-        curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
-        curl_easy_setopt(mon->handle, CURLOPT_WRITEFUNCTION, curl_callback);
-        curl_easy_setopt(mon->handle, CURLOPT_WRITEDATA, (void *)&data);
+        responseCode = virCHMonitorCurlPerform(mon->handle);
     }
 
-    responseCode = virCHMonitorCurlPerform(mon->handle);
-
-    virObjectUnlock(mon);
-
     if (responseCode == 200 || responseCode == 204) {
         if (response) {
             data.content = g_realloc(data.content, data.size + 1);
@@ -863,20 +858,18 @@ virCHMonitorCreateVM(virCHMonitor *mon,
                                 nnicindexes, nicindexes) != 0)
         return -1;
 
-    virObjectLock(mon);
-
-    /* reset all options of a libcurl session handle at first */
-    curl_easy_reset(mon->handle);
-
-    curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
-    curl_easy_setopt(mon->handle, CURLOPT_URL, url);
-    curl_easy_setopt(mon->handle, CURLOPT_CUSTOMREQUEST, "PUT");
-    curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
-    curl_easy_setopt(mon->handle, CURLOPT_POSTFIELDS, payload);
+    VIR_WITH_OBJECT_LOCK_GUARD(mon) {
+        /* reset all options of a libcurl session handle at first */
+        curl_easy_reset(mon->handle);
 
-    responseCode = virCHMonitorCurlPerform(mon->handle);
+        curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
+        curl_easy_setopt(mon->handle, CURLOPT_URL, url);
+        curl_easy_setopt(mon->handle, CURLOPT_CUSTOMREQUEST, "PUT");
+        curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
+        curl_easy_setopt(mon->handle, CURLOPT_POSTFIELDS, payload);
 
-    virObjectUnlock(mon);
+        responseCode = virCHMonitorCurlPerform(mon->handle);
+    }
 
     if (responseCode == 200 || responseCode == 204)
         ret = 0;
-- 
2.31.1



More information about the libvir-list mailing list