@samitouri / QOSamiQemu / commits / 11a64a4b7c

io: use g_clear_handle_id() for GSource cleanup

Use g_clear_handle_id() instead of g_source_remove() with manual ID checking and zeroing. This simplifies the code and ensures consistent handling of GSource IDs, since g_clear_handle_id() checks for a non-zero ID before calling the cleanup function and zeros it afterwards. No functional change intended. Mechanical change using the following Coccinelle spatch script: @@ expression TAG; @@ - if (TAG > 0) { + if (TAG) { g_source_remove(TAG); <... when != TAG TAG = 0; ...> } @@ expression TAG; @@ - g_source_remove(TAG); - TAG = 0; + g_clear_handle_id(&TAG, g_source_remove); @@ expression TAG; @@ - if (TAG) { g_clear_handle_id(&TAG, g_source_remove); - } Inspired-by: Matthew Penney <matt@matthewpenney.net> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Matthew Penney <matt@matthewpenney.net> Message-Id: <20260408100605.66795-3-philmd@linaro.org>

Philippe Mathieu-Daudé committed Apr 8, 2026 at 11:52 UTC 11a64a4b7c5460dd7ff94b8bedadb636e3338473
16 files changed +25 -95
hw/char/cmsdk-apb-uart.c
+1 -4
@@ -236,10 +236,7 @@ buffer_drained:
236
237 static void uart_cancel_transmit(CMSDKAPBUART *s)
238 {
239 - if (s->watch_tag) {
240 - g_source_remove(s->watch_tag);
241 - s->watch_tag = 0;
242 - }
239 + g_clear_handle_id(&s->watch_tag, g_source_remove);
240 }
241
242 static void uart_write(void *opaque, hwaddr offset, uint64_t value,
hw/char/nrf51_uart.c
+1 -4
@@ -104,10 +104,7 @@ buffer_drained:
104
105 static void uart_cancel_transmit(NRF51UARTState *s)
106 {
107 - if (s->watch_tag) {
108 - g_source_remove(s->watch_tag);
109 - s->watch_tag = 0;
110 - }
107 + g_clear_handle_id(&s->watch_tag, g_source_remove);
108 }
109
110 static void uart_write(void *opaque, hwaddr addr,
hw/char/serial.c
+1 -5
@@ -856,11 +856,7 @@ const VMStateDescription vmstate_serial = {
856 static void serial_reset(void *opaque)
857 {
858 SerialState *s = opaque;
859 -
860 - if (s->watch_tag > 0) {
861 - g_source_remove(s->watch_tag);
862 - s->watch_tag = 0;
863 - }
859 + g_clear_handle_id(&s->watch_tag, g_source_remove);
860
861 s->rbr = 0;
862 s->ier = 0;
hw/char/stm32l4x5_usart.c
+1 -4
@@ -280,10 +280,7 @@ buffer_drained:
280
281 static void usart_cancel_transmit(Stm32l4x5UsartBaseState *s)
282 {
283 - if (s->watch_tag) {
284 - g_source_remove(s->watch_tag);
285 - s->watch_tag = 0;
286 - }
283 + g_clear_handle_id(&s->watch_tag, g_source_remove);
284 }
285
286 static void stm32l4x5_update_params(Stm32l4x5UsartBaseState *s)
hw/char/terminal3270.c
+1 -4
@@ -52,10 +52,7 @@ static int terminal_can_read(void *opaque)
52
53 static void terminal_timer_cancel(Terminal3270 *t)
54 {
55 - if (t->timer_tag) {
56 - g_source_remove(t->timer_tag);
57 - t->timer_tag = 0;
58 - }
55 + g_clear_handle_id(&t->timer_tag, g_source_remove);
56 }
57
58 /*
hw/char/virtio-console.c
+2 -8
@@ -159,10 +159,7 @@ static void chr_event(void *opaque, QEMUChrEvent event)
159 virtio_serial_open(port);
160 break;
161 case CHR_EVENT_CLOSED:
162 - if (vcon->watch) {
163 - g_source_remove(vcon->watch);
164 - vcon->watch = 0;
165 - }
162 + g_clear_handle_id(&vcon->watch, g_source_remove);
163 virtio_serial_close(port);
164 break;
165 case CHR_EVENT_BREAK:
@@ -255,10 +252,7 @@ static void virtconsole_realize(DeviceState *dev, Error **errp)
252 static void virtconsole_unrealize(DeviceState *dev)
253 {
254 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
258 -
259 - if (vcon->watch) {
260 - g_clear_handle_id(&vcon->watch, g_source_remove);
261 - }
255 + g_clear_handle_id(&vcon->watch, g_source_remove);
256 }
257
258 static void virtconsole_class_init(ObjectClass *klass, const void *data)
hw/usb/redirect.c
+1 -4
@@ -1226,10 +1226,7 @@ static void usbredir_chardev_close_bh(void *opaque)
1226 usbredirparser_destroy(dev->parser);
1227 dev->parser = NULL;
1228 }
1229 - if (dev->watch) {
1230 - g_source_remove(dev->watch);
1231 - dev->watch = 0;
1232 - }
1229 + g_clear_handle_id(&dev->watch, g_source_remove);
1230 }
1231
1232 static void usbredir_create_parser(USBRedirDevice *dev)
io/channel-websock.c
+3 -10
@@ -1066,10 +1066,7 @@ static gboolean qio_channel_websock_flush(QIOChannel *ioc,
1066
1067 static void qio_channel_websock_unset_watch(QIOChannelWebsock *ioc)
1068 {
1069 - if (ioc->io_tag) {
1070 - g_source_remove(ioc->io_tag);
1071 - ioc->io_tag = 0;
1072 - }
1069 + g_clear_handle_id(&ioc->io_tag, g_source_remove);
1070 }
1071
1072 static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc)
@@ -1246,12 +1243,8 @@ static int qio_channel_websock_close(QIOChannel *ioc,
1243 buffer_free(&wioc->encinput);
1244 buffer_free(&wioc->encoutput);
1245 buffer_free(&wioc->rawinput);
1249 - if (wioc->hs_io_tag) {
1250 - g_clear_handle_id(&wioc->hs_io_tag, g_source_remove);
1251 - }
1252 - if (wioc->io_tag) {
1253 - g_clear_handle_id(&wioc->io_tag, g_source_remove);
1254 - }
1246 + g_clear_handle_id(&wioc->hs_io_tag, g_source_remove);
1247 + g_clear_handle_id(&wioc->io_tag, g_source_remove);
1248 if (wioc->io_err) {
1249 g_clear_pointer(&wioc->io_err, error_free);
1250 }
net/passt.c
+2 -6
@@ -90,10 +90,7 @@ static void net_passt_cleanup(NetClientState *nc)
90 g_free(s->vhost_net);
91 s->vhost_net = NULL;
92 }
93 - if (s->vhost_watch) {
94 - g_source_remove(s->vhost_watch);
95 - s->vhost_watch = 0;
96 - }
93 + g_clear_handle_id(&s->vhost_watch, g_source_remove);
94 qemu_chr_fe_deinit(&s->vhost_chr, true);
95 if (s->vhost_user) {
96 vhost_user_cleanup(s->vhost_user);
@@ -421,8 +418,7 @@ static void passt_vhost_user_event(void *opaque, QEMUChrEvent event)
418 if (s->vhost_watch) {
419 AioContext *ctx = qemu_get_current_aio_context();
420
424 - g_source_remove(s->vhost_watch);
425 - s->vhost_watch = 0;
421 + g_clear_handle_id(&s->vhost_watch, g_source_remove);
422 qemu_chr_fe_set_handlers(&s->vhost_chr, NULL, NULL, NULL, NULL,
423 NULL, NULL, false);
424
net/stream.c
+3 -12
@@ -71,24 +71,15 @@ static gboolean net_stream_send(QIOChannel *ioc,
71 static void net_stream_cleanup(NetClientState *nc)
72 {
73 NetStreamState *s = DO_UPCAST(NetStreamState, data.nc, nc);
74 - if (s->timer_tag) {
75 - g_source_remove(s->timer_tag);
76 - s->timer_tag = 0;
77 - }
74 + g_clear_handle_id(&s->timer_tag, g_source_remove);
75 if (s->addr) {
76 qapi_free_SocketAddress(s->addr);
77 s->addr = NULL;
78 }
79 if (s->data.ioc) {
80 if (QIO_CHANNEL_SOCKET(s->data.ioc)->fd != -1) {
84 - if (s->data.ioc_read_tag) {
85 - g_source_remove(s->data.ioc_read_tag);
86 - s->data.ioc_read_tag = 0;
87 - }
88 - if (s->data.ioc_write_tag) {
89 - g_source_remove(s->data.ioc_write_tag);
90 - s->data.ioc_write_tag = 0;
91 - }
81 + g_clear_handle_id(&s->data.ioc_read_tag, g_source_remove);
82 + g_clear_handle_id(&s->data.ioc_write_tag, g_source_remove);
83 }
84 object_unref(OBJECT(s->data.ioc));
85 s->data.ioc = NULL;
net/stream_data.c
+1 -4
@@ -84,10 +84,7 @@ void net_stream_data_rs_finalize(SocketReadState *rs)
84 if (qemu_send_packet_async(&d->nc, rs->buf,
85 rs->packet_len,
86 net_stream_data_send_completed) == 0) {
87 - if (d->ioc_read_tag) {
88 - g_source_remove(d->ioc_read_tag);
89 - d->ioc_read_tag = 0;
90 - }
87 + g_clear_handle_id(&d->ioc_read_tag, g_source_remove);
88 }
89 }
90
net/vhost-user.c
+2 -6
@@ -215,10 +215,7 @@ static void net_vhost_user_cleanup(NetClientState *nc)
215 s->vhost_net = NULL;
216 }
217 if (nc->queue_index == 0) {
218 - if (s->watch) {
219 - g_source_remove(s->watch);
220 - s->watch = 0;
221 - }
218 + g_clear_handle_id(&s->watch, g_source_remove);
219 qemu_chr_fe_deinit(&s->chr, true);
220 if (s->vhost_user) {
221 vhost_user_cleanup(s->vhost_user);
@@ -356,8 +353,7 @@ static void net_vhost_user_event(void *opaque, QEMUChrEvent event)
353 if (s->watch) {
354 AioContext *ctx = qemu_get_current_aio_context();
355
359 - g_source_remove(s->watch);
360 - s->watch = 0;
356 + g_clear_handle_id(&s->watch, g_source_remove);
357 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
358 NULL, NULL, false);
359
ui/dbus-clipboard.c
+2 -4
@@ -80,8 +80,7 @@ dbus_clipboard_update_info(DBusDisplay *dpy, QemuClipboardInfo *info)
80 if (req->invocation && info->types[req->type].data) {
81 dbus_clipboard_complete_request(dpy, req->invocation, info, req->type);
82 g_clear_object(&req->invocation);
83 - g_source_remove(req->timeout_id);
84 - req->timeout_id = 0;
83 + g_clear_handle_id(&req->timeout_id, g_source_remove);
84 return;
85 }
86
@@ -183,8 +182,7 @@ dbus_clipboard_request_cancelled(DBusClipboardRequest *req)
182 "Cancelled clipboard request");
183
184 g_clear_object(&req->invocation);
186 - g_source_remove(req->timeout_id);
187 - req->timeout_id = 0;
185 + g_clear_handle_id(&req->timeout_id, g_source_remove);
186 }
187
188 static void
ui/input-barrier.c
+1 -5
@@ -518,11 +518,7 @@ static void input_barrier_complete(UserCreatable *uc, Error **errp)
518 static void input_barrier_instance_finalize(Object *obj)
519 {
520 InputBarrier *ib = INPUT_BARRIER(obj);
521 -
522 - if (ib->ioc_tag) {
523 - g_source_remove(ib->ioc_tag);
524 - ib->ioc_tag = 0;
525 - }
521 + g_clear_handle_id(&ib->ioc_tag, g_source_remove);
522
523 if (ib->sioc) {
524 qio_channel_close(QIO_CHANNEL(ib->sioc), NULL);
ui/vnc-auth-vencrypt.c
+1 -5
@@ -101,11 +101,7 @@ static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len
101 QIOChannelTLS *tls;
102 vnc_write_u8(vs, 1); /* Accept auth */
103 vnc_flush(vs);
104 -
105 - if (vs->ioc_tag) {
106 - g_source_remove(vs->ioc_tag);
107 - vs->ioc_tag = 0;
108 - }
104 + g_clear_handle_id(&vs->ioc_tag, g_source_remove);
105
106 tls = qio_channel_tls_new_server(
107 vs->ioc,
ui/vnc-ws.c
+2 -10
@@ -54,11 +54,7 @@ gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
54 VncState *vs = opaque;
55 QIOChannelTLS *tls;
56 Error *err = NULL;
57 -
58 - if (vs->ioc_tag) {
59 - g_source_remove(vs->ioc_tag);
60 - vs->ioc_tag = 0;
61 - }
57 + g_clear_handle_id(&vs->ioc_tag, g_source_remove);
58
59 if (condition & (G_IO_HUP | G_IO_ERR)) {
60 vnc_client_error(vs);
@@ -123,11 +119,7 @@ gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
119 {
120 VncState *vs = opaque;
121 QIOChannelWebsock *wioc;
126 -
127 - if (vs->ioc_tag) {
128 - g_source_remove(vs->ioc_tag);
129 - vs->ioc_tag = 0;
130 - }
122 + g_clear_handle_id(&vs->ioc_tag, g_source_remove);
123
124 if (condition & (G_IO_HUP | G_IO_ERR)) {
125 vnc_client_error(vs);