[glib: 1/3] gatomic: Add various casts to use of g_atomic_*()s to fix warnings



commit 55f9c6d2f422a12ecc84ada71dc3a596329b40e3
Author: Philip Withnall <withnall endlessm com>
Date:   Sat Sep 21 10:45:36 2019 +0200

    gatomic: Add various casts to use of g_atomic_*()s to fix warnings
    
    When compiling GLib with `-Wsign-conversion`, we get various warnings
    about the atomic calls. A lot of these were fixed by
    3ad375a629c91a27d0165a31f0ed298fd553de0a, but some remain. Fix them by
    adding appropriate casts at the call sites.
    
    Note that `g_atomic_int_{and,or,xor}()` actually all operate on `guint`s
    rather than `gint`s (which is what the rest of the `g_atomic_int_*()`
    functions operate on). I can’t find any written reasoning for this, but
    assume that it’s because signedness is irrelevant when you’re using an
    integer as a bit field. It’s unfortunate that they’re named a
    `g_atomic_int_*()` rather than `g_atomic_uint_*()` functions.
    
    Tested by compiling GLib as:
    ```
    CFLAGS=-Wsign-conversion jhbuild make -ac |& grep atomic
    ```
    
    I’m not going to add `-Wsign-conversion` to the set of default warnings
    for building GLib, because it mostly produces false positives throughout
    the rest of GLib.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Fixes: #1565

 gio/gdbusconnection.c       |  8 ++++----
 gio/gdbusnamewatching.c     |  4 ++--
 gio/tests/gdbus-threading.c |  2 +-
 glib/gbitlock.c             |  2 +-
 glib/gquark.c               |  2 +-
 glib/gthread-posix.c        |  2 +-
 glib/gthreadpool.c          | 10 +++++-----
 glib/tests/atomic.c         | 20 ++++++++++----------
 8 files changed, 25 insertions(+), 25 deletions(-)
---
diff --git a/gio/gdbusconnection.c b/gio/gdbusconnection.c
index f1f0921d4..3411033f3 100644
--- a/gio/gdbusconnection.c
+++ b/gio/gdbusconnection.c
@@ -3148,7 +3148,7 @@ g_dbus_connection_add_filter (GDBusConnection            *connection,
 
   CONNECTION_LOCK (connection);
   data = g_new0 (FilterData, 1);
-  data->id = g_atomic_int_add (&_global_filter_id, 1); /* TODO: overflow etc. */
+  data->id = (guint) g_atomic_int_add (&_global_filter_id, 1); /* TODO: overflow etc. */
   data->ref_count = 1;
   data->filter_function = filter_function;
   data->user_data = user_data;
@@ -3508,7 +3508,7 @@ g_dbus_connection_signal_subscribe (GDBusConnection     *connection,
   subscriber.callback = callback;
   subscriber.user_data = user_data;
   subscriber.user_data_free_func = user_data_free_func;
-  subscriber.id = g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */
+  subscriber.id = (guint) g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */
   subscriber.context = g_main_context_ref_thread_default ();
 
   /* see if we've already have this rule */
@@ -5198,7 +5198,7 @@ g_dbus_connection_register_object (GDBusConnection             *connection,
     }
 
   ei = g_new0 (ExportedInterface, 1);
-  ei->id = g_atomic_int_add (&_global_registration_id, 1); /* TODO: overflow etc. */
+  ei->id = (guint) g_atomic_int_add (&_global_registration_id, 1); /* TODO: overflow etc. */
   ei->eo = eo;
   ei->user_data = user_data;
   ei->user_data_free_func = user_data_free_func;
@@ -6858,7 +6858,7 @@ g_dbus_connection_register_subtree (GDBusConnection           *connection,
 
   es->vtable = _g_dbus_subtree_vtable_copy (vtable);
   es->flags = flags;
-  es->id = g_atomic_int_add (&_global_subtree_registration_id, 1); /* TODO: overflow etc. */
+  es->id = (guint) g_atomic_int_add (&_global_subtree_registration_id, 1); /* TODO: overflow etc. */
   es->user_data = user_data;
   es->user_data_free_func = user_data_free_func;
   es->context = g_main_context_ref_thread_default ();
diff --git a/gio/gdbusnamewatching.c b/gio/gdbusnamewatching.c
index 3a97c50ee..f74952a19 100644
--- a/gio/gdbusnamewatching.c
+++ b/gio/gdbusnamewatching.c
@@ -603,7 +603,7 @@ g_bus_watch_name (GBusType                  bus_type,
 
   client = g_new0 (Client, 1);
   client->ref_count = 1;
-  client->id = g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
+  client->id = (guint) g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
   client->name = g_strdup (name);
   client->flags = flags;
   client->name_appeared_handler = name_appeared_handler;
@@ -665,7 +665,7 @@ guint g_bus_watch_name_on_connection (GDBusConnection          *connection,
 
   client = g_new0 (Client, 1);
   client->ref_count = 1;
-  client->id = g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
+  client->id = (guint) g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
   client->name = g_strdup (name);
   client->flags = flags;
   client->name_appeared_handler = name_appeared_handler;
diff --git a/gio/tests/gdbus-threading.c b/gio/tests/gdbus-threading.c
index ffca6f317..dd20530ff 100644
--- a/gio/tests/gdbus-threading.c
+++ b/gio/tests/gdbus-threading.c
@@ -512,7 +512,7 @@ test_threaded_singleton (void)
       /* We want to be the last ref, so let it finish setting up */
       for (j = 0; j < 100; j++)
         {
-          guint r = g_atomic_int_get (&G_OBJECT (c)->ref_count);
+          guint r = (guint) g_atomic_int_get (&G_OBJECT (c)->ref_count);
 
           if (r == 1)
             break;
diff --git a/glib/gbitlock.c b/glib/gbitlock.c
index 46e5f7d06..23024d08c 100644
--- a/glib/gbitlock.c
+++ b/glib/gbitlock.c
@@ -224,7 +224,7 @@ g_bit_lock (volatile gint *address,
     guint mask = 1u << lock_bit;
     guint v;
 
-    v = g_atomic_int_get (address);
+    v = (guint) g_atomic_int_get (address);
     if (v & mask)
       {
         guint class = ((gsize) address) % G_N_ELEMENTS (g_bit_lock_contended);
diff --git a/glib/gquark.c b/glib/gquark.c
index 2a0861094..2799b7d57 100644
--- a/glib/gquark.c
+++ b/glib/gquark.c
@@ -273,7 +273,7 @@ g_quark_to_string (GQuark quark)
   gchar **strings;
   guint seq_id;
 
-  seq_id = g_atomic_int_get (&quark_seq_id);
+  seq_id = (guint) g_atomic_int_get (&quark_seq_id);
   strings = g_atomic_pointer_get (&quarks);
 
   if (quark < seq_id)
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index 5a30b6d80..32314cc30 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -1416,7 +1416,7 @@ void
 g_cond_wait (GCond  *cond,
              GMutex *mutex)
 {
-  guint sampled = g_atomic_int_get (&cond->i[0]);
+  guint sampled = (guint) g_atomic_int_get (&cond->i[0]);
 
   g_mutex_unlock (mutex);
   syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, NULL);
diff --git a/glib/gthreadpool.c b/glib/gthreadpool.c
index 0a5f7518a..222209caa 100644
--- a/glib/gthreadpool.c
+++ b/glib/gthreadpool.c
@@ -146,7 +146,7 @@ g_thread_pool_wait_for_new_pool (void)
   gint last_wakeup_thread_serial;
   gboolean have_relayed_thread_marker = FALSE;
 
-  local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
+  local_max_unused_threads = (guint) g_atomic_int_get (&max_unused_threads);
   local_max_idle_time = g_atomic_int_get (&max_idle_time);
   last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
 
@@ -210,7 +210,7 @@ g_thread_pool_wait_for_new_pool (void)
               DEBUG_MSG (("thread %p updating to new limits.",
                           g_thread_self ()));
 
-              local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
+              local_max_unused_threads = (guint) g_atomic_int_get (&max_unused_threads);
               local_max_idle_time = g_atomic_int_get (&max_idle_time);
               last_wakeup_thread_serial = local_wakeup_thread_serial;
 
@@ -899,7 +899,7 @@ g_thread_pool_get_max_unused_threads (void)
 guint
 g_thread_pool_get_num_unused_threads (void)
 {
-  return g_atomic_int_get (&unused_threads);
+  return (guint) g_atomic_int_get (&unused_threads);
 }
 
 /**
@@ -1022,7 +1022,7 @@ g_thread_pool_set_max_idle_time (guint interval)
 
   g_atomic_int_set (&max_idle_time, interval);
 
-  i = g_atomic_int_get (&unused_threads);
+  i = (guint) g_atomic_int_get (&unused_threads);
   if (i > 0)
     {
       g_atomic_int_inc (&wakeup_thread_serial);
@@ -1058,5 +1058,5 @@ g_thread_pool_set_max_idle_time (guint interval)
 guint
 g_thread_pool_get_max_idle_time (void)
 {
-  return g_atomic_int_get (&max_idle_time);
+  return (guint) g_atomic_int_get (&max_idle_time);
 }
diff --git a/glib/tests/atomic.c b/glib/tests/atomic.c
index 84c13fb21..e6cc45159 100644
--- a/glib/tests/atomic.c
+++ b/glib/tests/atomic.c
@@ -32,7 +32,7 @@ test_types (void)
   cspp = &csp;
 
   g_atomic_int_set (&u, 5);
-  u2 = g_atomic_int_get (&u);
+  u2 = (guint) g_atomic_int_get (&u);
   g_assert_cmpint (u2, ==, 5);
   res = g_atomic_int_compare_and_exchange (&u, 6, 7);
   g_assert (!res);
@@ -67,13 +67,13 @@ test_types (void)
   res = g_atomic_int_dec_and_test (&s);
   g_assert (!res);
   g_assert_cmpint (s, ==, 6);
-  s2 = g_atomic_int_and (&s, 5);
+  s2 = (gint) g_atomic_int_and (&s, 5);
   g_assert_cmpint (s2, ==, 6);
   g_assert_cmpint (s, ==, 4);
-  s2 = g_atomic_int_or (&s, 8);
+  s2 = (gint) g_atomic_int_or (&s, 8);
   g_assert_cmpint (s2, ==, 4);
   g_assert_cmpint (s, ==, 12);
-  s2 = g_atomic_int_xor (&s, 4);
+  s2 = (gint) g_atomic_int_xor (&s, 4);
   g_assert_cmpint (s2, ==, 12);
   g_assert_cmpint (s, ==, 8);
 
@@ -98,7 +98,7 @@ test_types (void)
   res = g_atomic_pointer_compare_and_exchange (&gs, 0, 0);
   g_assert (res);
   g_assert (gs == 0);
-  gs2 = g_atomic_pointer_add (&gs, 5);
+  gs2 = (gsize) g_atomic_pointer_add (&gs, 5);
   g_assert (gs2 == 0);
   g_assert (gs == 5);
   gs2 = g_atomic_pointer_and (&gs, 6);
@@ -133,7 +133,7 @@ test_types (void)
 #undef g_atomic_pointer_xor
 
   g_atomic_int_set ((gint*)&u, 5);
-  u2 = g_atomic_int_get ((gint*)&u);
+  u2 = (guint) g_atomic_int_get ((gint*)&u);
   g_assert_cmpint (u2, ==, 5);
   res = g_atomic_int_compare_and_exchange ((gint*)&u, 6, 7);
   g_assert (!res);
@@ -167,13 +167,13 @@ test_types (void)
   res = g_atomic_int_dec_and_test (&s);
   g_assert (!res);
   g_assert_cmpint (s, ==, 6);
-  s2 = g_atomic_int_and ((guint*)&s, 5);
+  s2 = (gint) g_atomic_int_and ((guint*)&s, 5);
   g_assert_cmpint (s2, ==, 6);
   g_assert_cmpint (s, ==, 4);
-  s2 = g_atomic_int_or ((guint*)&s, 8);
+  s2 = (gint) g_atomic_int_or ((guint*)&s, 8);
   g_assert_cmpint (s2, ==, 4);
   g_assert_cmpint (s, ==, 12);
-  s2 = g_atomic_int_xor ((guint*)&s, 4);
+  s2 = (gint) g_atomic_int_xor ((guint*)&s, 4);
   g_assert_cmpint (s2, ==, 12);
   g_assert_cmpint (s, ==, 8);
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
@@ -203,7 +203,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
   res = g_atomic_pointer_compare_and_exchange (&gs, 0, 0);
   g_assert (res);
   g_assert (gs == 0);
-  gs2 = g_atomic_pointer_add (&gs, 5);
+  gs2 = (gsize) g_atomic_pointer_add (&gs, 5);
   g_assert (gs2 == 0);
   g_assert (gs == 5);
   gs2 = g_atomic_pointer_and (&gs, 6);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]