@samitouri / QOSamiQemu / commits / 19d7f093f7

util/filemonitor-inotify: Use QEMU_LOCK_GUARD()

Replace manual qemu_mutex_(un)lock() calls with QEMU_LOCK_GUARD() to remove 'goto cleanup' code Signed-off-by: Evgeny Kolmakov <randomjack94dev@gmail.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Evgeny Kolmakov committed Jun 22, 2026 at 10:36 UTC 19d7f093f77f592b5aadaba7c6ccfdf52abf4c77
1 file changed +8 -20
util/filemonitor-inotify.c
+8 -20
@@ -21,6 +21,7 @@
21 #include "qemu/osdep.h"
22 #include "qemu/filemonitor.h"
23 #include "qemu/main-loop.h"
24 +#include "qemu/lockable.h"
25 #include "qemu/error-report.h"
26 #include "qapi/error.h"
27 #include "trace.h"
@@ -59,10 +60,9 @@ static void qemu_file_monitor_watch(void *arg)
60 int used = 0;
61 int len;
62
62 - qemu_mutex_lock(&mon->lock);
63 + QEMU_LOCK_GUARD(&mon->lock);
64
65 if (mon->fd == -1) {
65 - qemu_mutex_unlock(&mon->lock);
66 return;
67 }
68
@@ -72,11 +72,10 @@ static void qemu_file_monitor_watch(void *arg)
72 if (errno != EAGAIN) {
73 error_report("Failure monitoring inotify FD '%s',"
74 "disabling events", strerror(errno));
75 - goto cleanup;
75 }
76
77 /* no more events right now */
79 - goto cleanup;
78 + return;
79 }
80
81 /* Loop over all events in the buffer */
@@ -151,9 +150,6 @@ static void qemu_file_monitor_watch(void *arg)
150 }
151 }
152 }
154 -
155 - cleanup:
156 - qemu_mutex_unlock(&mon->lock);
153 }
154
155
@@ -257,9 +253,8 @@ qemu_file_monitor_add_watch(QFileMonitor *mon,
253 {
254 QFileMonitorDir *dir;
255 QFileMonitorWatch watch;
260 - int64_t ret = -1;
256
262 - qemu_mutex_lock(&mon->lock);
257 + QEMU_LOCK_GUARD(&mon->lock);
258 dir = g_hash_table_lookup(mon->dirs, dirpath);
259 if (!dir) {
260 int rv = inotify_add_watch(mon->fd, dirpath,
@@ -268,7 +263,7 @@ qemu_file_monitor_add_watch(QFileMonitor *mon,
263
264 if (rv < 0) {
265 error_setg_errno(errp, errno, "Unable to watch '%s'", dirpath);
271 - goto cleanup;
266 + return -1;
267 }
268
269 trace_qemu_file_monitor_enable_watch(mon, dirpath, rv);
@@ -297,11 +292,7 @@ qemu_file_monitor_add_watch(QFileMonitor *mon,
292 filename ? filename : "<none>",
293 cb, opaque, watch.id);
294
300 - ret = watch.id;
301 -
302 - cleanup:
303 - qemu_mutex_unlock(&mon->lock);
304 - return ret;
295 + return watch.id;
296 }
297
298
@@ -312,13 +303,13 @@ void qemu_file_monitor_remove_watch(QFileMonitor *mon,
303 QFileMonitorDir *dir;
304 gsize i;
305
315 - qemu_mutex_lock(&mon->lock);
306 + QEMU_LOCK_GUARD(&mon->lock);
307
308 trace_qemu_file_monitor_remove_watch(mon, dirpath, id);
309
310 dir = g_hash_table_lookup(mon->dirs, dirpath);
311 if (!dir) {
321 - goto cleanup;
312 + return;
313 }
314
315 for (i = 0; i < dir->watches->len; i++) {
@@ -342,7 +333,4 @@ void qemu_file_monitor_remove_watch(QFileMonitor *mon,
333 qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
334 }
335 }
345 -
346 - cleanup:
347 - qemu_mutex_unlock(&mon->lock);
336 }