[evolution-data-server] Introduce e_async_closure_new_with_context()



commit a9a1cd878ff6ab4ee11e61de4a3f96a6c95074bb
Author: Milan Crha <mcrha redhat com>
Date:   Thu Nov 5 19:47:54 2020 +0100

    Introduce e_async_closure_new_with_context()
    
    It allows to override what main context should be used while waiting
    for the asynchronous result.

 src/libedataserver/e-data-server-util.c | 39 ++++++++++++++++++++++++++++++---
 src/libedataserver/e-data-server-util.h |  1 +
 2 files changed, 37 insertions(+), 3 deletions(-)
---
diff --git a/src/libedataserver/e-data-server-util.c b/src/libedataserver/e-data-server-util.c
index 88aa696d4..ee9851707 100644
--- a/src/libedataserver/e-data-server-util.c
+++ b/src/libedataserver/e-data-server-util.c
@@ -1727,6 +1727,7 @@ struct _EAsyncClosure {
        GMainContext *context;
        GAsyncResult *result;
        gboolean finished;
+       gboolean pop_thread_default;
        GMutex lock;
 };
 
@@ -1743,14 +1744,45 @@ EAsyncClosure *
 e_async_closure_new (void)
 {
        EAsyncClosure *closure;
+       GMainContext *context;
+
+       context = g_main_context_new ();
+       closure = e_async_closure_new_with_context (context);
+       g_main_context_unref (context);
+
+       return closure;
+}
+
+/**
+ * e_async_closure_new_with_context: (skip)
+ * @context: (nullable): a #GMainContext to use, or %NULL to use the default context
+ *
+ * Creates a new #EAsyncClosure for use with asynchronous functions
+ * using the @context as the main context.
+ *
+ * Returns: a new #EAsyncClosure
+ *
+ * Since: 3.40
+ **/
+EAsyncClosure *
+e_async_closure_new_with_context (GMainContext *context)
+{
+       EAsyncClosure *closure;
+
+       if (!context)
+               context = g_main_context_get_thread_default ();
+       if (!context)
+               context = g_main_context_default ();
 
        closure = g_slice_new0 (EAsyncClosure);
-       closure->context = g_main_context_new ();
+       closure->context = g_main_context_ref (context);
        closure->loop = g_main_loop_new (closure->context, FALSE);
        closure->finished = FALSE;
+       closure->pop_thread_default = context != g_main_context_get_thread_default () && context != 
g_main_context_default ();
        g_mutex_init (&closure->lock);
 
-       g_main_context_push_thread_default (closure->context);
+       if (closure->pop_thread_default)
+               g_main_context_push_thread_default (closure->context);
 
        return closure;
 }
@@ -1820,7 +1852,8 @@ e_async_closure_free (EAsyncClosure *closure)
 {
        g_return_if_fail (closure != NULL);
 
-       g_main_context_pop_thread_default (closure->context);
+       if (closure->pop_thread_default)
+               g_main_context_pop_thread_default (closure->context);
 
        g_main_loop_unref (closure->loop);
        g_main_context_unref (closure->context);
diff --git a/src/libedataserver/e-data-server-util.h b/src/libedataserver/e-data-server-util.h
index 7811ba335..bfddb2399 100644
--- a/src/libedataserver/e-data-server-util.h
+++ b/src/libedataserver/e-data-server-util.h
@@ -146,6 +146,7 @@ const gchar *       e_enum_to_string                (GType enum_type,
 typedef struct _EAsyncClosure EAsyncClosure;
 
 EAsyncClosure *        e_async_closure_new             (void);
+EAsyncClosure *        e_async_closure_new_with_context(GMainContext *context);
 GAsyncResult * e_async_closure_wait            (EAsyncClosure *closure);
 void           e_async_closure_free            (EAsyncClosure *closure);
 void           e_async_closure_callback        (GObject *object,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]