Add new thread creation API
[platform/upstream/glib.git] / gio / gdbusprivate.c
index 8bc6e5b..5584d51 100644 (file)
@@ -302,10 +302,11 @@ _g_dbus_shared_thread_ref (void)
       
       data->context = g_main_context_new ();
       data->loop = g_main_loop_new (data->context, FALSE);
-      data->thread = g_thread_create (gdbus_shared_thread_func,
-                                     data,
-                                     TRUE,
-                                     &error);
+      data->thread = g_thread_new ("gdbus",
+                                   gdbus_shared_thread_func,
+                                   data,
+                                   TRUE,
+                                   &error);
       g_assert_no_error (error);
       /* We can cast between gsize and gpointer safely */
       g_once_init_leave (&shared_thread_data, (gsize) data);
@@ -340,7 +341,8 @@ struct GDBusWorker
 
   SharedThreadData                   *shared_thread_data;
 
-  gboolean                            stopped;
+  /* really a boolean, but GLib 2.28 lacks atomic boolean ops */
+  volatile gint                       stopped;
 
   /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
    * only affects messages received from the other peer (since GDBusServer is the
@@ -472,7 +474,7 @@ _g_dbus_worker_emit_disconnected (GDBusWorker  *worker,
                                   gboolean      remote_peer_vanished,
                                   GError       *error)
 {
-  if (!worker->stopped)
+  if (!g_atomic_int_get (&worker->stopped))
     worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
 }
 
@@ -480,7 +482,7 @@ static void
 _g_dbus_worker_emit_message_received (GDBusWorker  *worker,
                                       GDBusMessage *message)
 {
-  if (!worker->stopped)
+  if (!g_atomic_int_get (&worker->stopped))
     worker->message_received_callback (worker, message, worker->user_data);
 }
 
@@ -489,7 +491,7 @@ _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker  *worker,
                                               GDBusMessage *message)
 {
   GDBusMessage *ret;
-  if (!worker->stopped)
+  if (!g_atomic_int_get (&worker->stopped))
     ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
   else
     ret = message;
@@ -571,7 +573,7 @@ _g_dbus_worker_do_read_cb (GInputStream  *input_stream,
   g_mutex_lock (worker->read_lock);
 
   /* If already stopped, don't even process the reply */
-  if (worker->stopped)
+  if (g_atomic_int_get (&worker->stopped))
     goto out;
 
   error = NULL;
@@ -1589,8 +1591,8 @@ _g_dbus_worker_new (GIOStream                              *stream,
   g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
   g_source_set_callback (idle_source,
                          _g_dbus_worker_do_initial_read,
-                         worker,
-                         NULL);
+                         _g_dbus_worker_ref (worker),
+                         (GDestroyNotify) _g_dbus_worker_unref);
   g_source_attach (idle_source, worker->shared_thread_data->context);
   g_source_unref (idle_source);
 
@@ -1631,7 +1633,7 @@ _g_dbus_worker_close (GDBusWorker         *worker,
 void
 _g_dbus_worker_stop (GDBusWorker *worker)
 {
-  worker->stopped = TRUE;
+  g_atomic_int_set (&worker->stopped, TRUE);
 
   /* Cancel any pending operations and schedule a close of the underlying I/O
    * stream in the worker thread