+/*
+ * 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;
+}
+