[glib] GDBusConnection: access the exit-on-close flag atomically



commit 8df114f5975ac8819a5a16cb4c5492a179cdfa2e
Author: Simon McVittie <simon mcvittie collabora co uk>
Date:   Mon Oct 17 19:27:23 2011 +0100

    GDBusConnection: access the exit-on-close flag atomically
    
    This isn't strictly necessary, because in every location where it's
    checked, if the reading thread misses an update from another thread,
    it's indistinguishable from the reading thread having been scheduled
    before the writing thread, which is an unavoidable race condition that
    callers need to cope with anyway. On the other hand, merging exit_on_close
    into atomic_flags gives the least astonishing semantics to library users
    and saves 4 bytes of struct, and if you're accessing exit-on-close often
    enough for it to be a performance concern, you're probably doing it wrong.
    
    Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
    Signed-off-by: Simon McVittie <simon mcvittie collabora co uk>
    Reviewed-by: David Zeuthen <davidz redhat com>

 gio/gdbusconnection.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)
---
diff --git a/gio/gdbusconnection.c b/gio/gdbusconnection.c
index 40dc1bd..509e37a 100644
--- a/gio/gdbusconnection.c
+++ b/gio/gdbusconnection.c
@@ -329,7 +329,8 @@ _g_strv_has_string (const gchar* const *haystack,
 
 /* Flags in connection->atomic_flags */
 enum {
-    FLAG_INITIALIZED = 1 << 0
+    FLAG_INITIALIZED = 1 << 0,
+    FLAG_EXIT_ON_CLOSE = 1 << 1
 };
 
 /**
@@ -402,6 +403,8 @@ struct _GDBusConnection
 
   /* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
    * Inspect @initialization_error to see whether it succeeded or failed.
+   *
+   * FLAG_EXIT_ON_CLOSE is the exit-on-close property.
    */
   volatile gint atomic_flags;
 
@@ -439,9 +442,6 @@ struct _GDBusConnection
   /* Structure used for message filters */
   GPtrArray *filters;
 
-  /* Whether to exit on close */
-  gboolean exit_on_close;
-
   /* Capabilities negotiated during authentication
    * Read-only after initable_init(), so it may be read without holding a
    * lock, if you check for initialization first.
@@ -739,7 +739,8 @@ g_dbus_connection_real_closed (GDBusConnection *connection,
   /* Because atomic int access is a memory barrier, we can safely read
    * initialization_error without a lock, as long as we do it afterwards.
    */
-  if (remote_peer_vanished && connection->exit_on_close &&
+  if (remote_peer_vanished &&
+      (flags & FLAG_EXIT_ON_CLOSE) != 0 &&
       (flags & FLAG_INITIALIZED) != 0 &&
       connection->initialization_error == NULL)
     {
@@ -2948,7 +2949,12 @@ g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
                                      gboolean         exit_on_close)
 {
   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
-  connection->exit_on_close = exit_on_close;
+
+  if (exit_on_close)
+    g_atomic_int_or (&connection->atomic_flags, FLAG_EXIT_ON_CLOSE);
+  else
+    g_atomic_int_and (&connection->atomic_flags, ~FLAG_EXIT_ON_CLOSE);
+
 }
 
 /**
@@ -2968,7 +2974,11 @@ gboolean
 g_dbus_connection_get_exit_on_close (GDBusConnection *connection)
 {
   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
-  return connection->exit_on_close;
+
+  if (g_atomic_int_get (&connection->atomic_flags) & FLAG_EXIT_ON_CLOSE)
+    return TRUE;
+  else
+    return FALSE;
 }
 
 /**



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