<SUBSECTION>
g_main_context_get_thread_default
+g_main_context_ref_thread_default
g_main_context_push_thread_default
g_main_context_pop_thread_default
self.c.write(' g_list_free (skeleton->priv->changed_properties);\n')
self.c.write(' if (skeleton->priv->changed_properties_idle_source != NULL)\n')
self.c.write(' g_source_destroy (skeleton->priv->changed_properties_idle_source);\n')
- self.c.write(' if (skeleton->priv->context != NULL)\n')
- self.c.write(' g_main_context_unref (skeleton->priv->context);\n')
+ self.c.write(' g_main_context_unref (skeleton->priv->context);\n')
self.c.write(' g_mutex_clear (&skeleton->priv->lock);\n')
self.c.write(' G_OBJECT_CLASS (%s_skeleton_parent_class)->finalize (object);\n'
'}\n'
' skeleton->priv = G_TYPE_INSTANCE_GET_PRIVATE (skeleton, %sTYPE_%s_SKELETON, %sSkeletonPrivate);\n'
%(i.name_lower, i.camel_name, i.ns_upper, i.name_upper, i.camel_name))
self.c.write(' g_mutex_init (&skeleton->priv->lock);\n')
- self.c.write(' skeleton->priv->context = g_main_context_get_thread_default ();\n')
- self.c.write(' if (skeleton->priv->context != NULL)\n')
- self.c.write(' g_main_context_ref (skeleton->priv->context);\n')
+ self.c.write(' skeleton->priv->context = g_main_context_ref_thread_default ();\n')
if len(i.properties) > 0:
self.c.write(' skeleton->priv->properties = g_value_array_new (%d);\n'%(len(i.properties)))
n = 0
GDestroyNotify callback,
gpointer user_data)
{
+ GMainContext *current_context;
+
if (callback == NULL)
goto out;
- if (context == g_main_context_get_thread_default ())
+ current_context = g_main_context_get_thread_default ();
+ if ((context == current_context) ||
+ (current_context == NULL && context == g_main_context_default ()))
{
callback (user_data);
}
/* If the connection could not be established during initable_init(), this GError will set */
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.
*/
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);
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 ();
}
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);
}
}
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)
{
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);
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);
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);
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,
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);
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,
{
if (g_atomic_int_dec_and_test (&data->ref_count))
{
- if (data->context != NULL)
- g_main_context_unref (data->context);
+ g_main_context_unref (data->context);
g_free (data);
}
}
data->interface = interface;
data->method_call_func = method_call_func;
data->invocation = invocation;
- data->context = g_main_context_get_thread_default ();
+ data->context = g_main_context_ref_thread_default ();
data->ref_count = 1;
- if (data->context != NULL)
- g_main_context_ref (data->context);
g_io_scheduler_push_job (dispatch_in_thread_func,
data,
(GDestroyNotify) dispatch_data_unref,
g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
g_object_unref (client->connection);
}
- if (client->main_context != NULL)
- g_main_context_unref (client->main_context);
+ g_main_context_unref (client->main_context);
g_free (client->name);
if (client->user_data_free_func != NULL)
client->user_data_free_func (client->user_data);
static void
do_call (Client *client, CallType call_type)
{
+ GMainContext *current_context;
+
/* only schedule in idle if we're not in the right thread */
- if (g_main_context_get_thread_default () != client->main_context)
+ current_context = g_main_context_ref_thread_default ();
+ if (current_context != client->main_context)
schedule_call_in_idle (client, call_type);
else
actually_do_call (client, client->connection, call_type);
+ g_main_context_unref (current_context);
}
static void
client->name_lost_handler = name_lost_handler;
client->user_data = user_data;
client->user_data_free_func = user_data_free_func;
- client->main_context = g_main_context_get_thread_default ();
- if (client->main_context != NULL)
- g_main_context_ref (client->main_context);
+ client->main_context = g_main_context_ref_thread_default ();
client->connection = g_object_ref (connection);
client->name_lost_handler = name_lost_handler;
client->user_data = user_data;
client->user_data_free_func = user_data_free_func;
- client->main_context = g_main_context_get_thread_default ();
- if (client->main_context != NULL)
- g_main_context_ref (client->main_context);
+ client->main_context = g_main_context_ref_thread_default ();
if (map_id_to_client == NULL)
{
}
g_free (client->name);
g_free (client->name_owner);
- if (client->main_context != NULL)
- g_main_context_unref (client->main_context);
+ g_main_context_unref (client->main_context);
if (client->user_data_free_func != NULL)
client->user_data_free_func (client->user_data);
g_free (client);
static void
do_call (Client *client, CallType call_type)
{
+ GMainContext *current_context;
+
/* only schedule in idle if we're not in the right thread */
- if (g_main_context_get_thread_default () != client->main_context)
+ current_context = g_main_context_ref_thread_default ();
+ if (current_context != client->main_context)
schedule_call_in_idle (client, call_type);
else
actually_do_call (client, client->connection, client->name_owner, call_type);
+ g_main_context_unref (current_context);
}
static void
client->name_vanished_handler = name_vanished_handler;
client->user_data = user_data;
client->user_data_free_func = user_data_free_func;
- client->main_context = g_main_context_get_thread_default ();
- if (client->main_context != NULL)
- g_main_context_ref (client->main_context);
+ client->main_context = g_main_context_ref_thread_default ();
if (map_id_to_client == NULL)
{
client->name_vanished_handler = name_vanished_handler;
client->user_data = user_data;
client->user_data_free_func = user_data_free_func;
- client->main_context = g_main_context_get_thread_default ();
- if (client->main_context != NULL)
- g_main_context_ref (client->main_context);
+ client->main_context = g_main_context_ref_thread_default ();
if (map_id_to_client == NULL)
map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
gboolean is_using_listener;
gulong run_signal_handler_id;
- /* 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 :new-connection GObject signal.
*/
*/
g_free (server->nonce_file);
- if (server->main_context_at_construction != NULL)
- g_main_context_unref (server->main_context_at_construction);
+ g_main_context_unref (server->main_context_at_construction);
G_OBJECT_CLASS (g_dbus_server_parent_class)->finalize (object);
}
static void
g_dbus_server_init (GDBusServer *server)
{
- server->main_context_at_construction = g_main_context_get_thread_default ();
- if (server->main_context_at_construction != NULL)
- g_main_context_ref (server->main_context_at_construction);
+ server->main_context_at_construction = g_main_context_ref_thread_default ();
}
static gboolean
g_hash_table_destroy (monitor->priv->rate_limiter);
- if (monitor->priv->context)
- g_main_context_unref (monitor->priv->context);
+ g_main_context_unref (monitor->priv->context);
G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
}
monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
NULL, (GDestroyNotify) rate_limiter_free);
- monitor->priv->context = g_main_context_get_thread_default ();
+ monitor->priv->context = g_main_context_ref_thread_default ();
}
/**
{
if (job->cancellable)
g_object_unref (job->cancellable);
- if (job->context)
- g_main_context_unref (job->context);
+ g_main_context_unref (job->context);
g_free (job);
}
if (cancellable)
job->cancellable = g_object_ref (cancellable);
- job->context = g_main_context_get_thread_default ();
- if (job->context)
- g_main_context_ref (job->context);
+ job->context = g_main_context_ref_thread_default ();
G_LOCK (active_jobs);
active_jobs = g_slist_prepend (active_jobs, job);
G_TYPE_SETTINGS,
GSettingsPrivate);
- settings->priv->main_context = g_main_context_get_thread_default ();
-
- if (settings->priv->main_context == NULL)
- settings->priv->main_context = g_main_context_default ();
-
- g_main_context_ref (settings->priv->main_context);
+ settings->priv->main_context = g_main_context_ref_thread_default ();
}
static void
if (simple->source_object)
g_object_unref (simple->source_object);
- if (simple->context)
- g_main_context_unref (simple->context);
+ g_main_context_unref (simple->context);
clear_op_res (simple);
{
simple->handle_cancellation = TRUE;
- simple->context = g_main_context_get_thread_default ();
- if (simple->context)
- g_main_context_ref (simple->context);
+ simple->context = g_main_context_ref_thread_default ();
}
/**
if (current_source && !g_source_is_destroyed (current_source))
{
current_context = g_source_get_context (current_source);
- if (current_context == g_main_context_default ())
- current_context = NULL;
if (simple->context != current_context)
g_warning ("g_simple_async_result_complete() called from wrong context!");
}
{
interaction->priv = G_TYPE_INSTANCE_GET_PRIVATE (interaction, G_TYPE_TLS_INTERACTION,
GTlsInteractionPrivate);
- interaction->priv->context = g_main_context_get_thread_default ();
- if (interaction->priv->context)
- g_main_context_ref (interaction->priv->context);
+ interaction->priv->context = g_main_context_ref_thread_default ();
}
static void
{
GTlsInteraction *interaction = G_TLS_INTERACTION (object);
- if (interaction->priv->context)
- g_main_context_unref (interaction->priv->context);
+ g_main_context_unref (interaction->priv->context);
G_OBJECT_CLASS (g_tls_interaction_parent_class)->finalize (object);
}
g_main_context_push_thread_default
g_main_context_query
g_main_context_ref
+g_main_context_ref_thread_default
g_main_context_release
g_main_context_remove_poll
g_main_context_set_poll_func
*
* Gets the thread-default #GMainContext for this thread. Asynchronous
* operations that want to be able to be run in contexts other than
- * the default one should call this method to get a #GMainContext to
- * add their #GSource<!-- -->s to. (Note that even in single-threaded
+ * the default one should call this method or
+ * g_main_context_ref_thread_default() to get a #GMainContext to add
+ * their #GSource<!-- -->s to. (Note that even in single-threaded
* programs applications may sometimes want to temporarily push a
* non-default context, so it is not safe to assume that this will
* always return %NULL if you are running in the default thread.)
*
+ * If you need to hold a reference on the context, use
+ * g_main_context_ref_thread_default() instead.
+ *
* Returns: (transfer none): the thread-default #GMainContext, or
* %NULL if the thread-default context is the global default context.
*
return NULL;
}
+/**
+ * g_main_context_ref_thread_default:
+ *
+ * Gets the thread-default #GMainContext for this thread, as with
+ * g_main_context_get_thread_default(), but also adds a reference to
+ * it with g_main_context_ref(). In addition, unlike
+ * g_main_context_get_thread_default(), if the thread-default context
+ * is the global default context, this will return that #GMainContext
+ * (with a ref added to it) rather than returning %NULL.
+ *
+ * Returns: (transfer full): the thread-default #GMainContext. Unref
+ * with g_main_context_unref() when you are done with it.
+ *
+ * Since: 2.32
+ */
+GMainContext *
+g_main_context_ref_thread_default (void)
+{
+ GMainContext *context;
+
+ context = g_main_context_get_thread_default ();
+ if (!context)
+ context = g_main_context_default ();
+ return g_main_context_ref (context);
+}
+
/* Hooks for adding to the main loop */
/**
void g_main_context_push_thread_default (GMainContext *context);
void g_main_context_pop_thread_default (GMainContext *context);
GMainContext *g_main_context_get_thread_default (void);
+GMainContext *g_main_context_ref_thread_default (void);
/* GMainLoop: */