[vala/staging: 3/4] codegen: Force sync behavior of GTasks



commit d919989b812895c408eae6c346e606a2baa0ec0b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Sep 29 21:56:44 2016 +0200

    codegen: Force sync behavior of GTasks
    
    There's code out there relying on immediate return here when state is
    !=0. As GTask always defers the finalization to an idle in its main
    context, ensure the source is dispatched and the task completed before
    returning.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763345

 codegen/valaccodemethodmodule.vala |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
---
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index fc3ef34..b428091 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -132,6 +132,28 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        finish_call.add_argument (new CCodeIdentifier ("_data_"));
                        finish_call.add_argument (new CCodeConstant ("NULL"));
                        ccode.add_expression (finish_call);
+
+                       // Preserve the "complete now" behavior if state != 0, do so by
+                       //  iterating the GTask's main context till the task is complete.
+                       var state = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_state_");
+                       var zero = new CCodeConstant ("0");
+                       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"));
+
+                       ccode.open_while (task_is_complete);
+                       var task_context = new CCodeFunctionCall (new CCodeIdentifier ("g_task_get_context"));
+                       task_context.add_argument (async_result_expr);
+                       var iterate_context = new CCodeFunctionCall (new CCodeIdentifier 
("g_main_context_iteration"));
+                       iterate_context.add_argument (task_context);
+                       iterate_context.add_argument (new CCodeConstant ("TRUE"));
+                       ccode.add_expression (iterate_context);
+                       ccode.close();
+
+                       ccode.close ();
                } else {
                        var state = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_state_");
                        var zero = new CCodeConstant ("0");


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