diff mbox series

[4/9] util: Replaced qemu_mutex_lock with QEMU_LOCK_GUARDs

Message ID 20210311031538.5325-5-ma.mandourr@gmail.com (mailing list archive)
State New, archived
Headers show
Series Changing qemu_mutex_locks to lock guard macros | expand

Commit Message

Mahmoud Abumandour March 11, 2021, 3:15 a.m. UTC
Removed various qemu_mutex_lock calls and their respective
qemu_mutex_unlock calls and used QEMU_LOCK_GUARD. This simplifies
the code by eliminating various goto paths and removes
qemu_mutex_unlock calls.

Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
---
 util/filemonitor-inotify.c | 24 ++++++++----------------
 util/vfio-helpers.c        | 23 ++++++++++-------------
 2 files changed, 18 insertions(+), 29 deletions(-)
diff mbox series

Patch

diff --git a/util/filemonitor-inotify.c b/util/filemonitor-inotify.c
index 2c45f7f176..0e1a196088 100644
--- a/util/filemonitor-inotify.c
+++ b/util/filemonitor-inotify.c
@@ -59,10 +59,9 @@  static void qemu_file_monitor_watch(void *arg)
     int used = 0;
     int len;
 
-    qemu_mutex_lock(&mon->lock);
+    QEMU_LOCK_GUARD(&mon->lock);
 
     if (mon->fd == -1) {
-        qemu_mutex_unlock(&mon->lock);
         return;
     }
 
@@ -72,11 +71,11 @@  static void qemu_file_monitor_watch(void *arg)
         if (errno != EAGAIN) {
             error_report("Failure monitoring inotify FD '%s',"
                          "disabling events", strerror(errno));
-            goto cleanup;
+            return;
         }
 
         /* no more events right now */
-        goto cleanup;
+        return;
     }
 
     /* Loop over all events in the buffer */
@@ -142,9 +141,6 @@  static void qemu_file_monitor_watch(void *arg)
             }
         }
     }
-
- cleanup:
-    qemu_mutex_unlock(&mon->lock);
 }
 
 
@@ -250,7 +246,8 @@  qemu_file_monitor_add_watch(QFileMonitor *mon,
     QFileMonitorWatch watch;
     int64_t ret = -1;
 
-    qemu_mutex_lock(&mon->lock);
+    QEMU_LOCK_GUARD(&mon->lock);
+
     dir = g_hash_table_lookup(mon->dirs, dirpath);
     if (!dir) {
         int rv = inotify_add_watch(mon->fd, dirpath,
@@ -259,7 +256,7 @@  qemu_file_monitor_add_watch(QFileMonitor *mon,
 
         if (rv < 0) {
             error_setg_errno(errp, errno, "Unable to watch '%s'", dirpath);
-            goto cleanup;
+            return ret;
         }
 
         trace_qemu_file_monitor_enable_watch(mon, dirpath, rv);
@@ -290,8 +287,6 @@  qemu_file_monitor_add_watch(QFileMonitor *mon,
 
     ret = watch.id;
 
- cleanup:
-    qemu_mutex_unlock(&mon->lock);
     return ret;
 }
 
@@ -303,13 +298,13 @@  void qemu_file_monitor_remove_watch(QFileMonitor *mon,
     QFileMonitorDir *dir;
     gsize i;
 
-    qemu_mutex_lock(&mon->lock);
+    QEMU_LOCK_GUARD(&mon->lock);
 
     trace_qemu_file_monitor_remove_watch(mon, dirpath, id);
 
     dir = g_hash_table_lookup(mon->dirs, dirpath);
     if (!dir) {
-        goto cleanup;
+        return;
     }
 
     for (i = 0; i < dir->watches->len; i++) {
@@ -333,7 +328,4 @@  void qemu_file_monitor_remove_watch(QFileMonitor *mon,
             qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
         }
     }
-
- cleanup:
-    qemu_mutex_unlock(&mon->lock);
 }
diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c
index 97dfa3fd57..dc05755ef1 100644
--- a/util/vfio-helpers.c
+++ b/util/vfio-helpers.c
@@ -748,41 +748,41 @@  int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
     assert(QEMU_PTR_IS_ALIGNED(host, qemu_real_host_page_size));
     assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size));
     trace_qemu_vfio_dma_map(s, host, size, temporary, iova);
-    qemu_mutex_lock(&s->lock);
+    QEMU_LOCK_GUARD(&s->lock);
     mapping = qemu_vfio_find_mapping(s, host, &index);
     if (mapping) {
         iova0 = mapping->iova + ((uint8_t *)host - (uint8_t *)mapping->host);
     } else {
         if (s->high_water_mark - s->low_water_mark + 1 < size) {
             ret = -ENOMEM;
-            goto out;
+            return ret;
         }
         if (!temporary) {
             if (qemu_vfio_find_fixed_iova(s, size, &iova0)) {
                 ret = -ENOMEM;
-                goto out;
+                return ret;
             }
 
             mapping = qemu_vfio_add_mapping(s, host, size, index + 1, iova0);
             if (!mapping) {
                 ret = -ENOMEM;
-                goto out;
+                return ret;
             }
             assert(qemu_vfio_verify_mappings(s));
             ret = qemu_vfio_do_mapping(s, host, size, iova0);
             if (ret) {
                 qemu_vfio_undo_mapping(s, mapping, NULL);
-                goto out;
+                return ret;
             }
             qemu_vfio_dump_mappings(s);
         } else {
             if (qemu_vfio_find_temp_iova(s, size, &iova0)) {
                 ret = -ENOMEM;
-                goto out;
+                return ret;
             }
             ret = qemu_vfio_do_mapping(s, host, size, iova0);
             if (ret) {
-                goto out;
+                return ret;
             }
         }
     }
@@ -790,8 +790,7 @@  int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
     if (iova) {
         *iova = iova0;
     }
-out:
-    qemu_mutex_unlock(&s->lock);
+
     return ret;
 }
 
@@ -826,14 +825,12 @@  void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host)
     }
 
     trace_qemu_vfio_dma_unmap(s, host);
-    qemu_mutex_lock(&s->lock);
+    QEMU_LOCK_GUARD(&s->lock);
     m = qemu_vfio_find_mapping(s, host, &index);
     if (!m) {
-        goto out;
+        return;
     }
     qemu_vfio_undo_mapping(s, m, NULL);
-out:
-    qemu_mutex_unlock(&s->lock);
 }
 
 static void qemu_vfio_reset(QEMUVFIOState *s)