* - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
* to specify where the nonce is stored. This will allow people to use
* G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
- * to easily acheive secure RPC via nonce-tcp.
+ * to easily achieve secure RPC via nonce-tcp.
*
* - need to expose an extension point for resolving D-Bus address and
* turning them into GIOStream objects. This will allow us to implement
* an D-Bus client, it is often easier to use the g_bus_own_name(),
* g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
*
+ * As an exception to the usual GLib rule that a particular object must not be
+ * used by two threads at the same time, #GDBusConnection's methods may be
+ * called from any thread<footnote>
+ * <para>
+ * This is so that g_bus_get() and g_bus_get_sync() can safely return the
+ * same #GDBusConnection when called from any thread.
+ * </para>
+ * </footnote>.
+ *
+ * Most of the ways to obtain a #GDBusConnection automatically initialize it
+ * (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
+ * g_bus_get(), and the synchronous versions of those methods, give you an
+ * initialized connection. Language bindings for GIO should use
+ * g_initable_new() or g_async_initable_new_async(), which also initialize the
+ * connection.
+ *
+ * If you construct an uninitialized #GDBusConnection, such as via
+ * g_object_new(), you must initialize it via g_initable_init() or
+ * g_async_initable_init_async() before using its methods or properties.
+ * Calling methods or accessing properties on a #GDBusConnection that has not
+ * completed initialization successfully is considered to be invalid, and leads
+ * to undefined behaviour. In particular, if initialization fails with a
+ * #GError, the only valid thing you can do with that #GDBusConnection is to
+ * free it with g_object_unref().
+ *
* <example id="gdbus-server"><title>D-Bus server example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-server.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
*
* <example id="gdbus-subtree-server"><title>D-Bus subtree example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-subtree.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
G_LOCK_DEFINE_STATIC (message_bus_lock);
-static GDBusConnection *the_session_bus = NULL;
-static GDBusConnection *the_system_bus = NULL;
+static GWeakRef the_session_bus;
+static GWeakRef the_system_bus;
+
+/* Extra pseudo-member of GDBusSendMessageFlags.
+ * Set by initable_init() to indicate that despite not being initialized yet,
+ * enough of the only-valid-after-init members are set that we can send a
+ * message, and we're being called from its thread, so no memory barrier is
+ * required before accessing them.
+ */
+#define SEND_MESSAGE_FLAGS_INITIALIZING (1<<31)
+
+/* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
+#define CALL_FLAGS_INITIALIZING (1<<31)
/* ---------------------------------------------------------------------------------------------------- */
GDestroyNotify callback,
gpointer user_data)
{
+ GSource *idle_source;
+ CallDestroyNotifyData *data;
+
if (callback == NULL)
goto out;
- if (context == g_main_context_get_thread_default ())
- {
- callback (user_data);
- }
- else
- {
- GSource *idle_source;
- CallDestroyNotifyData *data;
-
- data = g_new0 (CallDestroyNotifyData, 1);
- data->callback = callback;
- data->user_data = user_data;
- data->context = context;
- if (data->context != NULL)
- g_main_context_ref (data->context);
+ data = g_new0 (CallDestroyNotifyData, 1);
+ data->callback = callback;
+ data->user_data = user_data;
+ data->context = context;
+ if (data->context != NULL)
+ g_main_context_ref (data->context);
- idle_source = g_idle_source_new ();
- g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
- g_source_set_callback (idle_source,
- call_destroy_notify_data_in_idle,
- data,
- (GDestroyNotify) call_destroy_notify_data_free);
- g_source_attach (idle_source, data->context);
- g_source_unref (idle_source);
- }
+ idle_source = g_idle_source_new ();
+ g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
+ g_source_set_callback (idle_source,
+ call_destroy_notify_data_in_idle,
+ data,
+ (GDestroyNotify) call_destroy_notify_data_free);
+ g_source_attach (idle_source, data->context);
+ g_source_unref (idle_source);
out:
;
#else
// TODO: for some reason this doesn't work on Windows
#define CONNECTION_ENSURE_LOCK(obj) do { \
- if (G_UNLIKELY (g_mutex_trylock((obj)->lock))) \
+ if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
{ \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
"CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
#endif
#define CONNECTION_LOCK(obj) do { \
- g_mutex_lock ((obj)->lock); \
+ g_mutex_lock (&(obj)->lock); \
} while (FALSE)
#define CONNECTION_UNLOCK(obj) do { \
- g_mutex_unlock ((obj)->lock); \
+ g_mutex_unlock (&(obj)->lock); \
} while (FALSE)
+/* Flags in connection->atomic_flags */
+enum {
+ FLAG_INITIALIZED = 1 << 0,
+ FLAG_EXIT_ON_CLOSE = 1 << 1,
+ FLAG_CLOSED = 1 << 2
+};
+
/**
* GDBusConnection:
*
/* -- General object state ------------------------------------------------ */
/* ------------------------------------------------------------------------ */
- /* object-wide lock */
- GMutex *lock;
+ /* General-purpose lock for most fields */
+ GMutex lock;
/* A lock used in the init() method of the GInitable interface - see comments
- * in initable_init() for why a separate lock is needed
+ * in initable_init() for why a separate lock is needed.
+ *
+ * If you need both @lock and @init_lock, you must take @init_lock first.
*/
- GMutex *init_lock;
+ GMutex init_lock;
/* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
- * someone calls org.freedesktop.DBus.GetMachineId()
+ * someone calls org.freedesktop.DBus.GetMachineId(). Protected by @lock.
*/
gchar *machine_id;
- /* The underlying stream used for communication */
+ /* The underlying stream used for communication
+ * Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
+ */
GIOStream *stream;
- /* The object used for authentication (if any) */
+ /* The object used for authentication (if any).
+ * Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
+ */
GDBusAuth *auth;
- /* Set to TRUE if the connection has been closed */
- gboolean closed;
-
- /* Last serial used */
+ /* Last serial used. Protected by @lock. */
guint32 last_serial;
- /* The object used to send/receive message */
+ /* The object used to send/receive messages.
+ * Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
+ */
GDBusWorker *worker;
/* If connected to a message bus, this contains the unique name assigned to
- * us by the bus (e.g. ":1.42")
+ * us by the bus (e.g. ":1.42").
+ * Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
*/
gchar *bus_unique_name;
/* The GUID returned by the other side if we authenticed as a client or
- * the GUID to use if authenticating as a server
+ * the GUID to use if authenticating as a server.
+ * Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
*/
gchar *guid;
- /* set to TRUE exactly when initable_init() has finished running */
- gboolean is_initialized;
+ /* 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.
+ *
+ * FLAG_CLOSED is the closed property. It may be read at any time, but
+ * may only be written while holding @lock.
+ */
+ volatile gint atomic_flags;
- /* If the connection could not be established during initable_init(), this GError will set */
+ /* If the connection could not be established during initable_init(),
+ * this GError will be set.
+ * Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
+ */
GError *initialization_error;
- /* The result of g_main_context_get_thread_default() when the object
+ /* The result of g_main_context_ref_thread_default() when the object
* was created (the GObject _init() function) - this is used for delivery
* of the :closed GObject signal.
+ *
+ * Only set in the GObject init function, so no locks are needed.
*/
GMainContext *main_context_at_construction;
- /* construct properties */
+ /* Read-only construct properties, no locks needed */
gchar *address;
GDBusConnectionFlags flags;
- /* Map used for managing method replies */
+ /* Map used for managing method replies, protected by @lock */
GHashTable *map_method_serial_to_send_message_data; /* guint32 -> SendMessageData* */
- /* Maps used for managing signal subscription */
+ /* Maps used for managing signal subscription, protected by @lock */
GHashTable *map_rule_to_signal_data; /* match rule (gchar*) -> SignalData */
GHashTable *map_id_to_signal_data; /* id (guint) -> SignalData */
GHashTable *map_sender_unique_name_to_signal_data_array; /* unique sender (gchar*) -> GPtrArray* of SignalData */
- /* Maps used for managing exported objects and subtrees */
+ /* Maps used for managing exported objects and subtrees,
+ * protected by @lock
+ */
GHashTable *map_object_path_to_eo; /* gchar* -> ExportedObject* */
GHashTable *map_id_to_ei; /* guint -> ExportedInterface* */
GHashTable *map_object_path_to_es; /* gchar* -> ExportedSubtree* */
GHashTable *map_id_to_es; /* guint -> ExportedSubtree* */
- /* Structure used for message filters */
+ /* Structure used for message filters, protected by @lock */
GPtrArray *filters;
- /* Whether to exit on close */
- gboolean exit_on_close;
-
- /* Capabilities negotiated during authentication */
+ /* Capabilities negotiated during authentication
+ * Read-only after initable_init(), so it may be read without holding a
+ * lock, if you check for initialization first.
+ */
GDBusCapabilityFlags capabilities;
+ /* Protected by @init_lock */
GDBusAuthObserver *authentication_observer;
+
+ /* Read-only after initable_init(), so it may be read if you either
+ * hold @init_lock or check for initialization first.
+ */
GCredentials *credentials;
/* set to TRUE when finalizing */
G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
);
+/*
+ * Check that all members of @connection that can only be accessed after
+ * the connection is initialized can safely be accessed. If not,
+ * log a critical warning. This function is a memory barrier.
+ *
+ * Returns: %TRUE if initialized
+ */
+static gboolean
+check_initialized (GDBusConnection *connection)
+{
+ /* The access to @atomic_flags isn't conditional, so that this function
+ * provides a memory barrier for thread-safety even if checks are disabled.
+ * (If you don't want this stricter guarantee, you can call
+ * g_return_if_fail (check_initialized (c)).)
+ *
+ * This isn't strictly necessary now that we've decided use of an
+ * uninitialized GDBusConnection is undefined behaviour, but it seems
+ * better to be as deterministic as is feasible.
+ *
+ * (Anything that could suffer a crash from seeing undefined values
+ * must have a race condition - thread A initializes the connection while
+ * thread B calls a method without initialization, hoping that thread A will
+ * win the race - so its behaviour is undefined anyway.)
+ */
+ gint flags = g_atomic_int_get (&connection->atomic_flags);
+
+ g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE);
+
+ /* We can safely access this, due to the memory barrier above */
+ g_return_val_if_fail (connection->initialization_error == NULL, FALSE);
+
+ return TRUE;
+}
+
+typedef enum {
+ MAY_BE_UNINITIALIZED = (1<<1)
+} CheckUnclosedFlags;
+
+/*
+ * Check the same thing as check_initialized(), and also that the
+ * connection is not closed. If the connection is uninitialized,
+ * raise a critical warning (it's programmer error); if it's closed,
+ * raise a recoverable GError (it's a runtime error).
+ *
+ * This function is a memory barrier.
+ *
+ * Returns: %TRUE if initialized and not closed
+ */
+static gboolean
+check_unclosed (GDBusConnection *connection,
+ CheckUnclosedFlags check,
+ GError **error)
+{
+ /* check_initialized() is effectively inlined, so we don't waste time
+ * doing two memory barriers
+ */
+ gint flags = g_atomic_int_get (&connection->atomic_flags);
+
+ if (!(check & MAY_BE_UNINITIALIZED))
+ {
+ g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE);
+ g_return_val_if_fail (connection->initialization_error == NULL, FALSE);
+ }
+
+ if (flags & FLAG_CLOSED)
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_CLOSED,
+ _("The connection is closed"));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
static GHashTable *alive_connections = NULL;
static void
GDBusConnection *connection = G_DBUS_CONNECTION (object);
G_LOCK (message_bus_lock);
- if (connection == the_session_bus)
- {
- the_session_bus = NULL;
- }
- else if (connection == the_system_bus)
- {
- the_system_bus = NULL;
- }
CONNECTION_LOCK (connection);
if (connection->worker != NULL)
{
if (connection->stream != NULL)
{
- /* We don't really care if closing the stream succeeds or not */
- g_io_stream_close_async (connection->stream,
- G_PRIORITY_DEFAULT,
- NULL, /* GCancellable */
- NULL, /* GAsyncReadyCallback */
- NULL); /* userdata */
g_object_unref (connection->stream);
connection->stream = NULL;
}
g_hash_table_unref (connection->map_id_to_es);
g_hash_table_unref (connection->map_object_path_to_es);
- if (connection->main_context_at_construction != NULL)
- g_main_context_unref (connection->main_context_at_construction);
+ g_main_context_unref (connection->main_context_at_construction);
g_free (connection->machine_id);
- g_mutex_free (connection->init_lock);
- g_mutex_free (connection->lock);
+ g_mutex_clear (&connection->init_lock);
+ g_mutex_clear (&connection->lock);
G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object);
}
+/* called in any user thread, with the connection's lock not held */
static void
g_dbus_connection_get_property (GObject *object,
guint prop_id,
}
}
+/* called in any user thread, with the connection's lock not held */
static void
g_dbus_connection_set_property (GObject *object,
guint prop_id,
}
}
+/* Base-class implementation of GDBusConnection::closed.
+ *
+ * Called in a user thread, by the main context that was thread-default when
+ * the object was constructed.
+ */
static void
g_dbus_connection_real_closed (GDBusConnection *connection,
gboolean remote_peer_vanished,
GError *error)
{
- if (remote_peer_vanished && connection->exit_on_close)
+ gint flags = g_atomic_int_get (&connection->atomic_flags);
+
+ /* 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 &&
+ (flags & FLAG_EXIT_ON_CLOSE) != 0 &&
+ (flags & FLAG_INITIALIZED) != 0 &&
+ connection->initialization_error == NULL)
{
if (error != NULL)
{
* If this is passed on construction and is a #GSocketConnection,
* then the corresponding #GSocket will be put into non-blocking mode.
*
+ * While the #GDBusConnection is active, it will interact with this
+ * stream from a worker thread, so it is not safe to interact with
+ * the stream directly.
+ *
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
* calling <literal>raise(SIGTERM)</literal>) if the connection
* is closed by the remote peer.
*
+ * Note that #GDBusConnection objects returned by g_bus_get_finish() and
+ * g_bus_get_sync() will (usually) have this property set to %TRUE.
+ *
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
static void
g_dbus_connection_init (GDBusConnection *connection)
{
- connection->lock = g_mutex_new ();
- connection->init_lock = g_mutex_new ();
+ g_mutex_init (&connection->lock);
+ g_mutex_init (&connection->init_lock);
connection->map_method_serial_to_send_message_data = g_hash_table_new (g_direct_hash, g_direct_equal);
connection->map_id_to_es = g_hash_table_new (g_direct_hash,
g_direct_equal);
- connection->main_context_at_construction = g_main_context_get_thread_default ();
- if (connection->main_context_at_construction != NULL)
- g_main_context_ref (connection->main_context_at_construction);
+ connection->main_context_at_construction = g_main_context_ref_thread_default ();
connection->filters = g_ptr_array_new ();
}
*
* Gets the underlying stream used for IO.
*
+ * While the #GDBusConnection is active, it will interact with this
+ * stream from a worker thread, so it is not safe to interact with
+ * the stream directly.
+ *
* Returns: (transfer none): the stream used for IO
*
* Since: 2.26
g_dbus_connection_get_stream (GDBusConnection *connection)
{
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
+
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return NULL;
+
return connection->stream;
}
g_dbus_connection_start_message_processing (GDBusConnection *connection)
{
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
+
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return;
+
+ g_assert (connection->worker != NULL);
_g_dbus_worker_unfreeze (connection->worker);
}
gboolean
g_dbus_connection_is_closed (GDBusConnection *connection)
{
+ gint flags;
+
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
- return connection->closed;
+
+ flags = g_atomic_int_get (&connection->atomic_flags);
+
+ return (flags & FLAG_CLOSED) ? TRUE : FALSE;
}
/**
g_dbus_connection_get_capabilities (GDBusConnection *connection)
{
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CAPABILITY_FLAGS_NONE);
+
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return G_DBUS_CAPABILITY_FLAGS_NONE;
+
return connection->capabilities;
}
/* ---------------------------------------------------------------------------------------------------- */
+/* Called in a temporary thread without holding locks. */
static void
flush_in_thread_func (GSimpleAsyncResult *res,
GObject *object,
callback,
user_data,
g_dbus_connection_flush);
+ g_simple_async_result_set_check_cancellable (simple, cancellable);
g_simple_async_result_run_in_thread (simple,
flush_in_thread_func,
G_PRIORITY_DEFAULT,
ret = FALSE;
- if (connection->closed)
- {
- g_set_error_literal (error,
- G_IO_ERROR,
- G_IO_ERROR_CLOSED,
- _("The connection is closed"));
- goto out;
- }
+ /* This is only a best-effort attempt to see whether the connection is
+ * closed, so it doesn't need the lock. If the connection closes just
+ * after this check, but before scheduling the flush operation, the
+ * result will be more or less the same as if the connection closed while
+ * the flush operation was pending - it'll fail with either CLOSED or
+ * CANCELLED.
+ */
+ if (!check_unclosed (connection, 0, error))
+ goto out;
+
+ g_assert (connection->worker != NULL);
ret = _g_dbus_worker_flush_sync (connection->worker,
cancellable,
g_free (data);
}
+/* Called in a user thread that has acquired the main context that was
+ * thread-default when the object was constructed
+ */
static gboolean
emit_closed_in_idle (gpointer user_data)
{
return FALSE;
}
-/* Can be called from any thread, must hold lock */
+/* Can be called from any thread, must hold lock.
+ * FLAG_CLOSED must already have been set.
+ */
static void
-set_closed_unlocked (GDBusConnection *connection,
- gboolean remote_peer_vanished,
- GError *error)
+schedule_closed_unlocked (GDBusConnection *connection,
+ gboolean remote_peer_vanished,
+ GError *error)
{
GSource *idle_source;
EmitClosedData *data;
CONNECTION_ENSURE_LOCK (connection);
- g_assert (!connection->closed);
-
- connection->closed = TRUE;
-
data = g_new0 (EmitClosedData, 1);
data->connection = g_object_ref (connection);
data->remote_peer_vanished = remote_peer_vanished;
/* ---------------------------------------------------------------------------------------------------- */
-static void
-close_in_thread_func (GSimpleAsyncResult *res,
- GObject *object,
- GCancellable *cancellable)
-{
- GError *error;
-
- error = NULL;
- if (!g_dbus_connection_close_sync (G_DBUS_CONNECTION (object),
- cancellable,
- &error))
- g_simple_async_result_take_error (res, error);
-}
-
/**
* g_dbus_connection_close:
* @connection: A #GDBusConnection.
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return;
+
+ g_assert (connection->worker != NULL);
+
simple = g_simple_async_result_new (G_OBJECT (connection),
callback,
user_data,
g_dbus_connection_close);
- g_simple_async_result_run_in_thread (simple,
- close_in_thread_func,
- G_PRIORITY_DEFAULT,
- cancellable);
+ g_simple_async_result_set_check_cancellable (simple, cancellable);
+ _g_dbus_worker_close (connection->worker, cancellable, simple);
g_object_unref (simple);
}
return ret;
}
+typedef struct {
+ GMainLoop *loop;
+ GAsyncResult *result;
+} SyncCloseData;
+
+/* Can be called by any thread, without the connection lock */
+static void
+sync_close_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ SyncCloseData *data = user_data;
+
+ data->result = g_object_ref (res);
+ g_main_loop_quit (data->loop);
+}
+
/**
* g_dbus_connection_close_sync:
* @connection: A #GDBusConnection.
ret = FALSE;
- CONNECTION_LOCK (connection);
- if (!connection->closed)
+ if (check_unclosed (connection, 0, error))
{
- ret = g_io_stream_close (connection->stream,
- cancellable,
- error);
- if (ret)
- set_closed_unlocked (connection, FALSE, NULL);
- }
- else
- {
- g_set_error_literal (error,
- G_IO_ERROR,
- G_IO_ERROR_CLOSED,
- _("The connection is closed"));
+ GMainContext *context;
+ SyncCloseData data;
+
+ context = g_main_context_new ();
+ g_main_context_push_thread_default (context);
+ data.loop = g_main_loop_new (context, TRUE);
+ data.result = NULL;
+
+ g_dbus_connection_close (connection, cancellable, sync_close_cb, &data);
+ g_main_loop_run (data.loop);
+ ret = g_dbus_connection_close_finish (connection, data.result, error);
+
+ g_object_unref (data.result);
+ g_main_loop_unref (data.loop);
+ g_main_context_pop_thread_default (context);
+ g_main_context_unref (context);
}
- CONNECTION_UNLOCK (connection);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
+/* Can be called by any thread, with the connection lock held */
static gboolean
g_dbus_connection_send_message_unlocked (GDBusConnection *connection,
GDBusMessage *message,
if (out_serial != NULL)
*out_serial = 0;
- if (connection->closed)
- {
- g_set_error_literal (error,
- G_IO_ERROR,
- G_IO_ERROR_CLOSED,
- _("The connection is closed"));
- goto out;
- }
+ /* If we're in initable_init(), don't check for being initialized, to avoid
+ * chicken-and-egg problems. initable_init() is responsible for setting up
+ * our prerequisites (mainly connection->worker), and only calling us
+ * from its own thread (so no memory barrier is needed).
+ */
+ if (!check_unclosed (connection,
+ (flags & SEND_MESSAGE_FLAGS_INITIALIZING) ? MAY_BE_UNINITIALIZED : 0,
+ error))
+ goto out;
blob = g_dbus_message_to_blob (message,
&blob_size,
gboolean delivered;
} SendMessageData;
+/* Can be called from any thread with or without lock held */
static SendMessageData *
send_message_data_ref (SendMessageData *data)
{
return data;
}
+/* Can be called from any thread with or without lock held */
static void
send_message_data_unref (SendMessageData *data)
{
g_object_unref (data->connection);
if (data->cancellable != NULL)
g_object_unref (data->cancellable);
- if (data->main_context != NULL)
- g_main_context_unref (data->main_context);
+ g_main_context_unref (data->main_context);
g_free (data);
}
}
/* can be called from any thread with lock held - caller must have prepared GSimpleAsyncResult already */
static void
-send_message_with_reply_deliver (SendMessageData *data)
+send_message_with_reply_deliver (SendMessageData *data, gboolean remove)
{
CONNECTION_ENSURE_LOCK (data->connection);
data->cancellable_handler_id = 0;
}
- g_warn_if_fail (g_hash_table_remove (data->connection->map_method_serial_to_send_message_data,
- GUINT_TO_POINTER (data->serial)));
+ if (remove)
+ {
+ g_warn_if_fail (g_hash_table_remove (data->connection->map_method_serial_to_send_message_data,
+ GUINT_TO_POINTER (data->serial)));
+ }
send_message_data_unref (data);
}
/* ---------------------------------------------------------------------------------------------------- */
-/* must hold lock */
+/* Can be called from any thread with lock held */
static void
send_message_data_deliver_reply_unlocked (SendMessageData *data,
GDBusMessage *reply)
g_object_ref (reply),
g_object_unref);
- send_message_with_reply_deliver (data);
+ send_message_with_reply_deliver (data, TRUE);
out:
;
/* ---------------------------------------------------------------------------------------------------- */
+/* Called from a user thread, lock is not held */
static gboolean
send_message_with_reply_cancelled_idle_cb (gpointer user_data)
{
G_IO_ERROR_CANCELLED,
_("Operation was cancelled"));
- send_message_with_reply_deliver (data);
+ send_message_with_reply_deliver (data, TRUE);
out:
CONNECTION_UNLOCK (data->connection);
/* ---------------------------------------------------------------------------------------------------- */
+/* Called from a user thread, lock is not held */
static gboolean
send_message_with_reply_timeout_cb (gpointer user_data)
{
G_IO_ERROR_TIMED_OUT,
_("Timeout was reached"));
- send_message_with_reply_deliver (data);
+ send_message_with_reply_deliver (data, TRUE);
out:
CONNECTION_UNLOCK (data->connection);
/* ---------------------------------------------------------------------------------------------------- */
+/* Called from a user thread, connection's lock is held */
static void
g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection *connection,
GDBusMessage *message,
callback,
user_data,
g_dbus_connection_send_message_with_reply);
+ g_simple_async_result_set_check_cancellable (simple, cancellable);
if (g_cancellable_is_cancelled (cancellable))
{
goto out;
}
- if (connection->closed)
- {
- g_simple_async_result_set_error (simple,
- G_IO_ERROR,
- G_IO_ERROR_CLOSED,
- _("The connection is closed"));
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
- goto out;
- }
-
error = NULL;
if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error))
{
data->connection = g_object_ref (connection);
data->simple = simple;
data->serial = *out_serial;
- data->main_context = g_main_context_get_thread_default ();
- if (data->main_context != NULL)
- g_main_context_ref (data->main_context);
+ data->main_context = g_main_context_ref_thread_default ();
if (cancellable != NULL)
{
* Finishes an operation started with g_dbus_connection_send_message_with_reply().
*
* Note that @error is only set if a local in-process error
- * occured. That is to say that the returned #GDBusMessage object may
+ * occurred. That is to say that the returned #GDBusMessage object may
* be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
* g_dbus_message_to_gerror() to transcode this to a #GError.
*
GMainLoop *loop;
} SendMessageSyncData;
+/* Called from a user thread, lock is not held */
static void
send_message_with_reply_sync_cb (GDBusConnection *connection,
GAsyncResult *res,
* the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
*
* Note that @error is only set if a local in-process error
- * occured. That is to say that the returned #GDBusMessage object may
+ * occurred. That is to say that the returned #GDBusMessage object may
* be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
* g_dbus_message_to_gerror() to transcode this to a #GError.
*
GDestroyNotify user_data_free_func;
} FilterData;
-/* Called in worker's thread - we must not block */
+/* Called in GDBusWorker's thread - we must not block - with no lock held */
static void
on_worker_message_received (GDBusWorker *worker,
GDBusMessage *message,
g_free (filters);
}
-/* Called in worker's thread */
+/* Called in GDBusWorker's thread, lock is not held */
static GDBusMessage *
on_worker_message_about_to_be_sent (GDBusWorker *worker,
GDBusMessage *message,
return message;
}
-/* Called in worker's thread - we must not block */
+/* called with connection lock held, in GDBusWorker thread */
+static gboolean
+cancel_method_on_close (gpointer key, gpointer value, gpointer user_data)
+{
+ SendMessageData *data = value;
+
+ if (data->delivered)
+ return FALSE;
+
+ g_simple_async_result_set_error (data->simple,
+ G_IO_ERROR,
+ G_IO_ERROR_CLOSED,
+ _("The connection is closed"));
+
+ /* Ask send_message_with_reply_deliver not to remove the element from the
+ * hash table - we're in the middle of a foreach; that would be unsafe.
+ * Instead, return TRUE from this function so that it gets removed safely.
+ */
+ send_message_with_reply_deliver (data, FALSE);
+ return TRUE;
+}
+
+/* Called in GDBusWorker's thread - we must not block - without lock held */
static void
on_worker_closed (GDBusWorker *worker,
gboolean remote_peer_vanished,
{
GDBusConnection *connection;
gboolean alive;
+ guint old_atomic_flags;
G_LOCK (message_bus_lock);
alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
//g_debug ("in on_worker_closed: %s", error->message);
CONNECTION_LOCK (connection);
- if (!connection->closed)
- set_closed_unlocked (connection, remote_peer_vanished, error);
+ /* Even though this is atomic, we do it inside the lock to avoid breaking
+ * assumptions in remove_match_rule(). We'd need the lock in a moment
+ * anyway, so, no loss.
+ */
+ old_atomic_flags = g_atomic_int_or (&connection->atomic_flags, FLAG_CLOSED);
+
+ if (!(old_atomic_flags & FLAG_CLOSED))
+ {
+ g_hash_table_foreach_remove (connection->map_method_serial_to_send_message_data, cancel_method_on_close, NULL);
+ schedule_closed_unlocked (connection, remote_peer_vanished, error);
+ }
CONNECTION_UNLOCK (connection);
g_object_unref (connection);
/* ---------------------------------------------------------------------------------------------------- */
-/* Determines the biggest set of capabilities we can support on this connection */
+/* Determines the biggest set of capabilities we can support on this
+ * connection.
+ *
+ * Called with the init_lock held.
+ */
static GDBusCapabilityFlags
get_offered_capabilities_max (GDBusConnection *connection)
{
return ret;
}
+/* Called in a user thread, lock is not held */
static gboolean
initable_init (GInitable *initable,
GCancellable *cancellable,
* callbacks above needs the lock during initialization (for message
* bus connections we do a synchronous Hello() call on the bus).
*/
- g_mutex_lock (connection->init_lock);
+ g_mutex_lock (&connection->init_lock);
ret = FALSE;
- /* First, handle the case where the connection already has an
- * initialization error set.
+ /* Make this a no-op if we're already initialized (successfully or
+ * unsuccessfully)
*/
- if (connection->initialization_error != NULL)
- goto out;
-
- /* Also make this a no-op if we're already initialized fine */
- if (connection->is_initialized)
+ if ((g_atomic_int_get (&connection->atomic_flags) & FLAG_INITIALIZED))
{
- ret = TRUE;
+ ret = (connection->initialization_error == NULL);
goto out;
}
- g_assert (connection->initialization_error == NULL && !connection->is_initialized);
+ /* Because of init_lock, we can't get here twice in different threads */
+ g_assert (connection->initialization_error == NULL);
/* The user can pass multiple (but mutally exclusive) construct
* properties:
if ((connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) ||
(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS))
{
- g_set_error_literal (error,
+ g_set_error_literal (&connection->initialization_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Unsupported flags encountered when constructing a client-side connection"));
connection->worker = _g_dbus_worker_new (connection->stream,
connection->capabilities,
- (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING),
+ ((connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) != 0),
on_worker_message_received,
on_worker_message_about_to_be_sent,
on_worker_closed,
"Hello",
NULL, /* parameters */
G_VARIANT_TYPE ("(s)"),
- G_DBUS_CALL_FLAGS_NONE,
+ CALL_FLAGS_INITIALIZING,
-1,
NULL, /* TODO: cancellable */
&connection->initialization_error);
//g_debug ("unique name is `%s'", connection->bus_unique_name);
}
- connection->is_initialized = TRUE;
-
ret = TRUE;
out:
if (!ret)
g_propagate_error (error, g_error_copy (connection->initialization_error));
}
- g_mutex_unlock (connection->init_lock);
+ g_atomic_int_or (&connection->atomic_flags, FLAG_INITIALIZED);
+ g_mutex_unlock (&connection->init_lock);
return ret;
}
* If @stream is a #GSocketConnection, then the corresponding #GSocket
* will be put into non-blocking mode.
*
+ * The D-Bus connection will interact with @stream from a worker thread.
+ * As a result, the caller should not interact with @stream after this
+ * method has been called, except by calling g_object_unref() on it.
+ *
* If @observer is not %NULL it may be used to control the
* authentication process.
*
* If @stream is a #GSocketConnection, then the corresponding #GSocket
* will be put into non-blocking mode.
*
+ * The D-Bus connection will interact with @stream from a worker thread.
+ * As a result, the caller should not interact with @stream after this
+ * method has been called, except by calling g_object_unref() on it.
+ *
* If @observer is not %NULL it may be used to control the
* authentication process.
*
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);
+
}
/**
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;
}
/**
g_dbus_connection_get_unique_name (GDBusConnection *connection)
{
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
+
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return NULL;
+
return connection->bus_unique_name;
}
g_dbus_connection_get_peer_credentials (GDBusConnection *connection)
{
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
+
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return NULL;
+
return connection->credentials;
}
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
g_return_val_if_fail (filter_function != NULL, 0);
+ g_return_val_if_fail (check_initialized (connection), 0);
CONNECTION_LOCK (connection);
data = g_new0 (FilterData, 1);
FilterData *to_destroy;
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
+ g_return_if_fail (check_initialized (connection));
CONNECTION_LOCK (connection);
to_destroy = NULL;
/* ---------------------------------------------------------------------------------------------------- */
-/* must hold lock when calling */
+/* Called in a user thread, lock is held */
static void
add_match_rule (GDBusConnection *connection,
const gchar *match_rule)
/* ---------------------------------------------------------------------------------------------------- */
-/* must hold lock when calling */
+/* Called in a user thread, lock is held */
static void
remove_match_rule (GDBusConnection *connection,
const gchar *match_rule)
NULL,
&error))
{
+ /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to
+ * critical; but we're holding the lock, and our caller checked whether
+ * we were already closed, so we can't get that error.
+ */
g_critical ("Error while sending RemoveMatch() message: %s", error->message);
g_error_free (error);
}
* @flags: Flags describing how to subscribe to the signal (currently unused).
* @callback: Callback to invoke when there is a signal matching the requested data.
* @user_data: User data to pass to @callback.
- * @user_data_free_func: Function to free @user_data with when subscription is removed or %NULL.
+ * @user_data_free_func: (allow-none): Function to free @user_data with when
+ * subscription is removed or %NULL.
*
* Subscribes to signals on @connection and invokes @callback with a
* whenever the signal is received. Note that @callback
g_return_val_if_fail (member == NULL || g_dbus_is_member_name (member), 0);
g_return_val_if_fail (object_path == NULL || g_variant_is_object_path (object_path), 0);
g_return_val_if_fail (callback != NULL, 0);
+ g_return_val_if_fail (check_initialized (connection), 0);
CONNECTION_LOCK (connection);
subscriber.user_data = user_data;
subscriber.user_data_free_func = user_data_free_func;
subscriber.id = _global_subscriber_id++; /* TODO: overflow etc. */
- subscriber.context = g_main_context_get_thread_default ();
- if (subscriber.context != NULL)
- g_main_context_ref (subscriber.context);
+ subscriber.context = g_main_context_ref_thread_default ();
/* see if we've already have this rule */
signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule);
/* ---------------------------------------------------------------------------------------------------- */
+/* called in any thread */
/* must hold lock when calling this (except if connection->finalizing is TRUE) */
static void
unsubscribe_id_internal (GDBusConnection *connection,
}
/* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
- if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
+ if ((connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION) &&
+ !is_signal_data_for_name_lost_or_acquired (signal_data) &&
+ !g_dbus_connection_is_closed (connection) &&
+ !connection->finalizing)
{
- if (!is_signal_data_for_name_lost_or_acquired (signal_data))
- if (!connection->closed && !connection->finalizing)
- remove_match_rule (connection, signal_data->rule);
+ /* The check for g_dbus_connection_is_closed() means that
+ * sending the RemoveMatch message can't fail with
+ * G_IO_ERROR_CLOSED, because we're holding the lock,
+ * so on_worker_closed() can't happen between the check we just
+ * did, and releasing the lock later.
+ */
+ remove_match_rule (connection, signal_data->rule);
}
+
signal_data_free (signal_data);
}
guint n;
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
+ g_return_if_fail (check_initialized (connection));
subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
call_destroy_notify (subscriber->context,
subscriber->user_data_free_func,
subscriber->user_data);
- if (subscriber->context != NULL)
- g_main_context_unref (subscriber->context);
+ g_main_context_unref (subscriber->context);
}
g_array_free (subscribers, TRUE);
g_free (signal_instance);
}
-/* called in message handler thread WITH lock held */
+/* called in GDBusWorker thread WITH lock held */
static void
schedule_callbacks (GDBusConnection *connection,
GPtrArray *signal_data_array,
}
}
-/* called in message handler thread with lock held */
+/* called in GDBusWorker thread with lock held */
static void
distribute_signals (GDBusConnection *connection,
GDBusMessage *message)
call_destroy_notify (subscriber->context,
subscriber->user_data_free_func,
subscriber->user_data);
- if (subscriber->context != NULL)
- g_main_context_unref (subscriber->context);
+ g_main_context_unref (subscriber->context);
}
g_array_free (subscribers, TRUE);
ei->user_data_free_func,
ei->user_data);
- if (ei->context != NULL)
- g_main_context_unref (ei->context);
+ g_main_context_unref (ei->context);
g_free (ei->interface_name);
_g_dbus_interface_vtable_free (ei->vtable);
* @subtree_registration_id (if not zero) has been unregistered. If
* so, returns %TRUE.
*
- * Caller must *not* hold lock.
+ * May be called by any thread. Caller must *not* hold lock.
*/
static gboolean
has_object_been_unregistered (GDBusConnection *connection,
return FALSE;
}
-/* called with lock held */
+/* called in any thread with connection's lock held */
static gboolean
validate_and_maybe_schedule_property_getset (GDBusConnection *connection,
GDBusMessage *message,
return handled;
}
-/* called with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
handle_getset_property (GDBusConnection *connection,
ExportedObject *eo,
return FALSE;
}
-/* called with lock held */
+/* called in any thread with connection's lock held */
static gboolean
validate_and_maybe_schedule_property_get_all (GDBusConnection *connection,
GDBusMessage *message,
return handled;
}
-/* called with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
handle_get_all_properties (GDBusConnection *connection,
ExportedObject *eo,
}
/* TODO: we want a nicer public interface for this */
+/* called in any thread with connection's lock held */
static gchar **
g_dbus_connection_list_registered_unlocked (GDBusConnection *connection,
const gchar *path)
return ret;
}
+/* called in any thread with connection's lock not held */
static gchar **
g_dbus_connection_list_registered (GDBusConnection *connection,
const gchar *path)
return ret;
}
-/* called in message handler thread with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
handle_introspect (GDBusConnection *connection,
ExportedObject *eo,
return FALSE;
}
-/* called in message handler thread with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
validate_and_maybe_schedule_method_call (GDBusConnection *connection,
GDBusMessage *message,
/* ---------------------------------------------------------------------------------------------------- */
-/* called in message handler thread with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
obj_message_func (GDBusConnection *connection,
ExportedObject *eo,
* @object_path: The object path to register at.
* @interface_info: Introspection data for the interface.
* @vtable: (allow-none): A #GDBusInterfaceVTable to call into or %NULL.
- * @user_data: Data to pass to functions in @vtable.
+ * @user_data: (allow-none): Data to pass to functions in @vtable.
* @user_data_free_func: Function to call when the object path is unregistered.
* @error: Return location for error or %NULL.
*
g_return_val_if_fail (interface_info != NULL, 0);
g_return_val_if_fail (g_dbus_is_interface_name (interface_info->name), 0);
g_return_val_if_fail (error == NULL || *error == NULL, 0);
+ g_return_val_if_fail (check_initialized (connection), 0);
ret = 0;
ei->interface_info = g_dbus_interface_info_ref (interface_info);
g_dbus_interface_info_cache_build (ei->interface_info);
ei->interface_name = g_strdup (interface_info->name);
- ei->context = g_main_context_get_thread_default ();
- if (ei->context != NULL)
- g_main_context_ref (ei->context);
+ ei->context = g_main_context_ref_thread_default ();
g_hash_table_insert (eo->map_if_name_to_ei,
(gpointer) ei->interface_name,
gboolean ret;
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
+ g_return_val_if_fail (check_initialized (connection), FALSE);
ret = FALSE;
g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), FALSE);
g_return_val_if_fail (signal_name != NULL && g_dbus_is_member_name (signal_name), FALSE);
g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), FALSE);
+ g_return_val_if_fail (check_initialized (connection), FALSE);
if (G_UNLIKELY (_g_dbus_debug_emission ()))
{
g_slice_free (CallState, state);
}
+/* called in any thread, with the connection's lock not held */
static void
g_dbus_connection_call_done (GObject *source,
GAsyncResult *result,
{
g_simple_async_result_set_op_res_gpointer (state->simple, state, (GDestroyNotify) call_state_free);
g_simple_async_result_complete (state->simple);
- g_object_unref (reply);
}
+ g_clear_object (&reply);
g_object_unref (simple);
}
+/* called in any thread, with the connection's lock not held */
static void
g_dbus_connection_call_internal (GDBusConnection *connection,
const gchar *bus_name,
g_return_if_fail (method_name != NULL && g_dbus_is_member_name (method_name));
g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
+ g_return_if_fail (check_initialized (connection));
#ifdef G_OS_UNIX
g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
#else
state->simple = g_simple_async_result_new (G_OBJECT (connection),
callback, user_data,
g_dbus_connection_call_internal);
+ g_simple_async_result_set_check_cancellable (state->simple, cancellable);
state->method_name = g_strjoin (".", interface_name, method_name, NULL);
if (reply_type == NULL)
g_object_unref (message);
}
+/* called in any thread, with the connection's lock not held */
static GVariant *
g_dbus_connection_call_finish_internal (GDBusConnection *connection,
GUnixFDList **out_fd_list,
return g_variant_ref (state->value);
}
+/* called in any user thread, with the connection's lock not held */
static GVariant *
g_dbus_connection_call_sync_internal (GDBusConnection *connection,
const gchar *bus_name,
GDBusMessage *reply;
GVariant *result;
GError *local_error;
+ GDBusSendMessageFlags send_flags;
message = NULL;
reply = NULL;
#endif
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+ if (!(flags & CALL_FLAGS_INITIALIZING))
+ g_return_val_if_fail (check_initialized (connection), FALSE);
+
if (reply_type == NULL)
reply_type = G_VARIANT_TYPE_ANY;
}
local_error = NULL;
+
+ send_flags = G_DBUS_SEND_MESSAGE_FLAGS_NONE;
+
+ /* translate from one flavour of flags to another... */
+ if (flags & CALL_FLAGS_INITIALIZING)
+ send_flags |= SEND_MESSAGE_FLAGS_INITIALIZING;
+
reply = g_dbus_connection_send_message_with_reply_sync (connection,
message,
- G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+ send_flags,
timeout_msec,
NULL, /* volatile guint32 *out_serial */
cancellable,
* timeout or %G_MAXINT for no timeout.
* @cancellable: A #GCancellable or %NULL.
* @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
- * satisfied or %NULL if you don't * care about the result of the
+ * satisfied or %NULL if you don't care about the result of the
* method invocation.
* @user_data: The data to pass to @callback.
*
GAsyncReadyCallback callback,
gpointer user_data)
{
- return g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, cancellable, callback, user_data);
+ g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, cancellable, callback, user_data);
}
/**
GAsyncReadyCallback callback,
gpointer user_data)
{
- return g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, cancellable, callback, user_data);
+ g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, cancellable, callback, user_data);
}
/**
es->user_data_free_func,
es->user_data);
- if (es->context != NULL)
- g_main_context_unref (es->context);
+ g_main_context_unref (es->context);
_g_dbus_subtree_vtable_free (es->vtable);
g_free (es->object_path);
g_free (es);
}
-/* called without lock held */
+/* called without lock held in the thread where the caller registered
+ * the subtree
+ */
static gboolean
handle_subtree_introspect (GDBusConnection *connection,
ExportedSubtree *es,
return handled;
}
-/* called without lock held */
+/* called without lock held in the thread where the caller registered
+ * the subtree
+ */
static gboolean
handle_subtree_method_invocation (GDBusConnection *connection,
ExportedSubtree *es,
return FALSE;
}
-/* called in message handler thread with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
subtree_message_func (GDBusConnection *connection,
ExportedSubtree *es,
g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
g_return_val_if_fail (vtable != NULL, 0);
g_return_val_if_fail (error == NULL || *error == NULL, 0);
+ g_return_val_if_fail (check_initialized (connection), 0);
ret = 0;
es->id = _global_subtree_registration_id++; /* TODO: overflow etc. */
es->user_data = user_data;
es->user_data_free_func = user_data_free_func;
- es->context = g_main_context_get_thread_default ();
- if (es->context != NULL)
- g_main_context_ref (es->context);
+ es->context = g_main_context_ref_thread_default ();
g_hash_table_insert (connection->map_object_path_to_es, es->object_path, es);
g_hash_table_insert (connection->map_id_to_es,
gboolean ret;
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
+ g_return_val_if_fail (check_initialized (connection), FALSE);
ret = FALSE;
/* ---------------------------------------------------------------------------------------------------- */
-/* must be called with lock held */
+/* may be called in any thread, with connection's lock held */
static void
handle_generic_ping_unlocked (GDBusConnection *connection,
const gchar *object_path,
g_object_unref (reply);
}
-/* must be called with lock held */
+/* may be called in any thread, with connection's lock held */
static void
handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
const gchar *object_path,
g_object_unref (reply);
}
-/* must be called with lock held */
+/* may be called in any thread, with connection's lock held */
static void
handle_generic_introspect_unlocked (GDBusConnection *connection,
const gchar *object_path,
g_string_free (s, TRUE);
}
-/* must be called with lock held */
+/* may be called in any thread, with connection's lock held */
static gboolean
handle_generic_unlocked (GDBusConnection *connection,
GDBusMessage *message)
/* ---------------------------------------------------------------------------------------------------- */
-/* called in message handler thread with lock held */
+/* called in GDBusWorker thread with connection's lock held */
static void
distribute_method_call (GDBusConnection *connection,
GDBusMessage *message)
/* ---------------------------------------------------------------------------------------------------- */
-static GDBusConnection **
+/* Called in any user thread, with the message_bus_lock held. */
+static GWeakRef *
message_bus_get_singleton (GBusType bus_type,
GError **error)
{
- GDBusConnection **ret;
+ GWeakRef *ret;
const gchar *starter_bus;
ret = NULL;
return ret;
}
+/* Called in any user thread, without holding locks. */
static GDBusConnection *
get_uninitialized_connection (GBusType bus_type,
GCancellable *cancellable,
GError **error)
{
- GDBusConnection **singleton;
+ GWeakRef *singleton;
GDBusConnection *ret;
ret = NULL;
if (singleton == NULL)
goto out;
- if (*singleton == NULL)
+ ret = g_weak_ref_get (singleton);
+
+ if (ret == NULL)
{
gchar *address;
address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
if (address == NULL)
goto out;
- ret = *singleton = g_object_new (G_TYPE_DBUS_CONNECTION,
- "address", address,
- "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
- G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
- "exit-on-close", TRUE,
- NULL);
+ ret = g_object_new (G_TYPE_DBUS_CONNECTION,
+ "address", address,
+ "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
+ G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
+ "exit-on-close", TRUE,
+ NULL);
+
+ g_weak_ref_set (singleton, ret);
g_free (address);
}
- else
- {
- ret = g_object_ref (*singleton);
- }
g_assert (ret != NULL);
callback,
user_data,
g_bus_get);
+ g_simple_async_result_set_check_cancellable (simple, cancellable);
error = NULL;
connection = get_uninitialized_connection (bus_type, cancellable, &error);