Skip to contentSkip to conversation

hw/char: use g_clear_handle_id() for GSource cleanup

PATCH New Matthew Penney 1 participant 1 message Apr 8 Arm STM32

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 hw/char: 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.

---
 hw/char/cmsdk-apb-uart.c  |  5 +----
 hw/char/nrf51_uart.c      |  5 +----
 hw/char/serial.c          |  7 ++-----
 hw/char/stm32l4x5_usart.c |  5 +----
 hw/char/terminal3270.c    |  5 +----
 hw/char/virtio-console.c  | 11 +++--------
 6 files changed, 9 insertions(+), 29 deletions(-)
hw/char/cmsdk-apb-uart.c +1 -4
index edb80f6181..0631821a57 100644
@@ -236,10 +236,7 @@ buffer_drained:
 
 static void uart_cancel_transmit(CMSDKAPBUART *s)
 {
-    if (s->watch_tag) {
-        g_source_remove(s->watch_tag);
-        s->watch_tag = 0;
-    }
+    g_clear_handle_id(&s->watch_tag, g_source_remove);
 }
 
 static void uart_write(void *opaque, hwaddr offset, uint64_t value,
hw/char/nrf51_uart.c +1 -4
index 7306923244..96f0d085fd 100644
@@ -104,10 +104,7 @@ buffer_drained:
 
 static void uart_cancel_transmit(NRF51UARTState *s)
 {
-    if (s->watch_tag) {
-        g_source_remove(s->watch_tag);
-        s->watch_tag = 0;
-    }
+    g_clear_handle_id(&s->watch_tag, g_source_remove);
 }
 
 static void uart_write(void *opaque, hwaddr addr,
hw/char/serial.c +2 -5
index 0f3469a1e8..cfb5efed50 100644
@@ -857,10 +857,7 @@ static void serial_reset(void *opaque)
 {
     SerialState *s = opaque;
 
-    if (s->watch_tag > 0) {
-        g_source_remove(s->watch_tag);
-        s->watch_tag = 0;
-    }
+    g_clear_handle_id(&s->watch_tag, g_source_remove);
 
     s->rbr = 0;
     s->ier = 0;
@@ -913,7 +910,7 @@ static int serial_be_change(void *opaque)
     }
 
     if (s->watch_tag > 0) {
-        g_source_remove(s->watch_tag);
+        g_clear_handle_id(&s->watch_tag, g_source_remove);
         s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
                                              serial_watch_cb, s);
     }
hw/char/stm32l4x5_usart.c +1 -4
index 736f1e764e..dd1b099195 100644
@@ -280,10 +280,7 @@ buffer_drained:
 
 static void usart_cancel_transmit(Stm32l4x5UsartBaseState *s)
 {
-    if (s->watch_tag) {
-        g_source_remove(s->watch_tag);
-        s->watch_tag = 0;
-    }
+    g_clear_handle_id(&s->watch_tag, g_source_remove);
 }
 
 static void stm32l4x5_update_params(Stm32l4x5UsartBaseState *s)
hw/char/terminal3270.c +1 -4
index 1d857bad9b..989b6f6ee1 100644
@@ -52,10 +52,7 @@ static int terminal_can_read(void *opaque)
 
 static void terminal_timer_cancel(Terminal3270 *t)
 {
-    if (t->timer_tag) {
-        g_source_remove(t->timer_tag);
-        t->timer_tag = 0;
-    }
+    g_clear_handle_id(&t->timer_tag, g_source_remove);
 }
 
 /*
hw/char/virtio-console.c +3 -8
index efe7cd6772..4de8577c13 100644
@@ -159,10 +159,7 @@ static void chr_event(void *opaque, QEMUChrEvent event)
         virtio_serial_open(port);
         break;
     case CHR_EVENT_CLOSED:
-        if (vcon->watch) {
-            g_source_remove(vcon->watch);
-            vcon->watch = 0;
-        }
+        g_clear_handle_id(&vcon->watch, g_source_remove);
         virtio_serial_close(port);
         break;
     case CHR_EVENT_BREAK:
@@ -188,7 +185,7 @@ static int chr_be_change(void *opaque)
     }
 
     if (vcon->watch) {
-        g_source_remove(vcon->watch);
+        g_clear_handle_id(&vcon->watch, g_source_remove);
         vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
                                             G_IO_OUT | G_IO_HUP,
                                             chr_write_unblocked, vcon);
@@ -256,9 +253,7 @@ static void virtconsole_unrealize(DeviceState *dev)
 {
     VirtConsole *vcon = VIRTIO_CONSOLE(dev);
 
-    if (vcon->watch) {
-        g_clear_handle_id(&vcon->watch, g_source_remove);
-    }
+    g_clear_handle_id(&vcon->watch, g_source_remove);
 }
 
 static void virtconsole_class_init(ObjectClass *klass, const void *data)
Signature · 1 line
2.53.0