[vala/staging] codegen: Compensate for the lack of g_task_get_completed on glib < 2.44



commit a27219c5da0f622e148a3e6927a3ea8cefa337d8
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Dec 14 14:14:23 2016 +0100

    codegen: Compensate for the lack of g_task_get_completed on glib < 2.44
    
    If a target glib between 2.36 and 2.44 is used, store a boolean var
    in the async task data, which is initialized to FALSE and set to
    TRUE in the async task callback, so async-forced-to-sync like the
    async generator example can block until the task is complete.
    
    There is one special case, if the async task receives no callback
    to execute, the boolean flag is set to TRUE right away, as it
    will be "finished" by the first time it's checked.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763345

 codegen/valaccodemethodmodule.vala |   13 ++++++++++---
 codegen/valagasyncmodule.vala      |   22 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)
---
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 3a89c7e..6d8d3b6 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -140,9 +140,16 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        var state_is_not_zero = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, 
state, zero);
                        ccode.open_if (state_is_not_zero);
 
-                       var task_complete = new CCodeFunctionCall (new CCodeIdentifier 
("g_task_get_completed"));
-                       task_complete.add_argument (async_result_expr);
-                       var task_is_complete = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, 
task_complete, new CCodeConstant ("TRUE"));
+                       CCodeBinaryExpression task_is_complete;
+
+                       if (context.require_glib_version (2, 44)) {
+                               var task_complete = new CCodeFunctionCall (new CCodeIdentifier 
("g_task_get_completed"));
+                               task_complete.add_argument (async_result_expr);
+                               task_is_complete = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, 
task_complete, new CCodeConstant ("TRUE"));
+                       } else {
+                               var task_complete = new CCodeMemberAccess.pointer (new CCodeIdentifier 
("_data_"), "_task_complete_");
+                               task_is_complete = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, 
task_complete, new CCodeConstant ("TRUE"));
+                       }
 
                        ccode.open_while (task_is_complete);
                        var task_context = new CCodeFunctionCall (new CCodeIdentifier ("g_task_get_context"));
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index 85d6ac5..d389ae6 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -33,6 +33,9 @@ public class Vala.GAsyncModule : GtkModule {
 
                if (context.require_glib_version (2, 36)) {
                        data.add_field ("GTask*", "_async_result");
+                       if (!context.require_glib_version (2, 44)) {
+                               data.add_field ("gboolean", "_task_complete_");
+                       }
                } else {
                        data.add_field ("GSimpleAsyncResult*", "_async_result");
                }
@@ -262,6 +265,16 @@ public class Vala.GAsyncModule : GtkModule {
                if (!context.require_glib_version (2, 36)) {
                        attach_data_call = new CCodeFunctionCall (new CCodeIdentifier 
("g_simple_async_result_set_op_res_gpointer"));
                } else {
+                       if (!context.require_glib_version (2, 44)) {
+                               var task_completed_var = new CCodeMemberAccess.pointer (data_var, 
"_task_complete_");
+                               var callback = new CCodeIdentifier ("_callback_");
+                               var callback_is_null = new CCodeBinaryExpression 
(CCodeBinaryOperator.EQUALITY, callback, new CCodeConstant ("NULL"));
+
+                               ccode.open_if (callback_is_null);
+                               ccode.add_assignment (task_completed_var, new CCodeConstant ("TRUE"));
+                               ccode.close ();
+                       }
+
                        attach_data_call = new CCodeFunctionCall (new CCodeIdentifier 
("g_task_set_task_data"));
                }
 
@@ -593,6 +606,11 @@ public class Vala.GAsyncModule : GtkModule {
                                return_default_value (return_type);
                                ccode.close ();
                        }
+
+                       if (!context.require_glib_version (2, 44)) {
+                               var task_completed_var = new CCodeMemberAccess.pointer (data_var, 
"_task_complete_");
+                               ccode.add_assignment (task_completed_var, new CCodeConstant ("TRUE"));
+                       }
                } else {
                        var simple_async_result_cast = new CCodeFunctionCall (new CCodeIdentifier 
("G_SIMPLE_ASYNC_RESULT"));
                        simple_async_result_cast.add_argument (new CCodeIdentifier ("_res_"));
@@ -683,6 +701,10 @@ public class Vala.GAsyncModule : GtkModule {
                ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), 
"_source_object_"), new CCodeIdentifier ("source_object"));
                ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), 
"_res_"), new CCodeIdentifier ("_res_"));
 
+               if (context.require_glib_version (2, 36) && !context.require_glib_version (2, 44)) {
+                       ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), 
"_task_complete_"), new CCodeConstant ("TRUE"));
+               }
+
                var ccall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_real_name (m) + "_co"));
                ccall.add_argument (new CCodeIdentifier ("_data_"));
                ccode.add_expression (ccall);


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