Merge branch 'upstream' into tizen
[platform/upstream/glib.git] / gio / gdbusnamewatching.c
index 7ca294d..ef6481f 100755 (executable)
@@ -40,7 +40,7 @@
  * Convenience API for watching bus names.
  *
  * A simple example for watching a name can be found in
- * [gdbus-example-watch-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-name.c)
+ * [gdbus-example-watch-name.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-watch-name.c)
  */
 
 G_LOCK_DEFINE_STATIC (lock);
@@ -56,7 +56,7 @@ typedef enum
 
 typedef struct
 {
-  volatile gint             ref_count;
+  gint                      ref_count;  /* (atomic) */
   guint                     id;
   gchar                    *name;
   GBusNameWatcherFlags      flags;
@@ -78,7 +78,7 @@ typedef struct
 } Client;
 
 /* Must be accessed atomically. */
-static volatile guint next_global_id = 1;
+static guint next_global_id = 1;  /* (atomic) */
 
 /* Must be accessed with @lock held. */
 static GHashTable *map_id_to_client = NULL;
@@ -90,6 +90,13 @@ client_ref (Client *client)
   return client;
 }
 
+static gboolean
+free_user_data_cb (gpointer user_data)
+{
+  /* The user data is actually freed by the GDestroyNotify for the idle source */
+  return G_SOURCE_REMOVE;
+}
+
 static void
 client_unref (Client *client)
 {
@@ -105,9 +112,26 @@ client_unref (Client *client)
         }
       g_free (client->name);
       g_free (client->name_owner);
-      g_main_context_unref (client->main_context);
+
       if (client->user_data_free_func != NULL)
-        client->user_data_free_func (client->user_data);
+        {
+          /* Ensure client->user_data_free_func() is called from the right thread */
+          if (client->main_context != g_main_context_get_thread_default ())
+            {
+              GSource *idle_source = g_idle_source_new ();
+              g_source_set_callback (idle_source, free_user_data_cb,
+                                     client->user_data,
+                                     client->user_data_free_func);
+              g_source_set_name (idle_source, "[gio, gdbusnamewatching.c] free_user_data_cb");
+              g_source_attach (idle_source, client->main_context);
+              g_source_unref (idle_source);
+            }
+          else
+            client->user_data_free_func (client->user_data);
+        }
+
+      g_main_context_unref (client->main_context);
+
       g_free (client);
     }
 }
@@ -148,6 +172,11 @@ call_handler_data_free (CallHandlerData *data)
 static void
 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
 {
+  /* The client might have been cancelled (g_bus_unwatch_name()) while we were
+   * sitting in the #GMainContext dispatch queue. */
+  if (client->cancelled)
+    return;
+
   switch (call_type)
     {
     case CALL_TYPE_NAME_APPEARED:
@@ -201,7 +230,7 @@ schedule_call_in_idle (Client *client, CallType call_type)
                          call_in_idle_cb,
                          data,
                          (GDestroyNotify) call_handler_data_free);
-  g_source_set_name (idle_source, "[gio, gdbusnamewatching.c] call_in_idle_cb");
+  g_source_set_static_name (idle_source, "[gio, gdbusnamewatching.c] call_in_idle_cb");
   g_source_attach (idle_source, client->main_context);
   g_source_unref (idle_source);
 }
@@ -234,13 +263,12 @@ call_appeared_handler (Client *client)
 }
 
 static void
-call_vanished_handler (Client  *client,
-                       gboolean ignore_cancelled)
+call_vanished_handler (Client *client)
 {
   if (client->previous_call != PREVIOUS_CALL_VANISHED)
     {
       client->previous_call = PREVIOUS_CALL_VANISHED;
-      if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
+      if (!client->cancelled && client->name_vanished_handler != NULL)
         {
           do_call (client, CALL_TYPE_NAME_VANISHED);
         }
@@ -296,7 +324,7 @@ on_connection_disconnected (GDBusConnection *connection,
   client->name_owner_changed_subscription_id = 0;
   client->connection = NULL;
 
-  call_vanished_handler (client, FALSE);
+  call_vanished_handler (client);
 
   client_unref (client);
 }
@@ -342,7 +370,7 @@ on_name_owner_changed (GDBusConnection *connection,
     {
       g_free (client->name_owner);
       client->name_owner = NULL;
-      call_vanished_handler (client, FALSE);
+      call_vanished_handler (client);
     }
 
   if (new_owner != NULL && strlen (new_owner) > 0)
@@ -400,7 +428,7 @@ get_name_owner_cb (GObject      *source_object,
     }
   else
     {
-      call_vanished_handler (client, FALSE);
+      call_vanished_handler (client);
     }
 
  out:
@@ -460,7 +488,8 @@ start_service_by_name_cb (GObject      *source_object,
       else
         {
           g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
-          call_vanished_handler (client, FALSE);
+          call_vanished_handler (client);
+          client->initialized = TRUE;
         }
     }
   else
@@ -538,7 +567,7 @@ connection_get_cb (GObject      *source_object,
   client->connection = g_bus_get_finish (res, NULL);
   if (client->connection == NULL)
     {
-      call_vanished_handler (client, FALSE);
+      call_vanished_handler (client);
       goto out;
     }
 
@@ -612,7 +641,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;
@@ -674,7 +703,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;
@@ -863,6 +892,13 @@ guint g_bus_watch_name_on_connection_with_closures (
  *
  * Stops watching a name.
  *
+ * Note that there may still be D-Bus traffic to process (relating to watching
+ * and unwatching the name) in the current thread-default #GMainContext after
+ * this function has returned. You should continue to iterate the #GMainContext
+ * until the #GDestroyNotify function passed to g_bus_watch_name() is called, in
+ * order to avoid memory leaks through callbacks queued on the #GMainContext
+ * after it’s stopped being iterated.
+ *
  * Since: 2.26
  */
 void