]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Compensate for the lack of g_task_get_completed on glib < 2.44
authorCarlos Garnacho <carlosg@gnome.org>
Wed, 14 Dec 2016 13:14:23 +0000 (14:14 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 14 Dec 2016 18:02:17 +0000 (19:02 +0100)
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
codegen/valagasyncmodule.vala

index 3a89c7e41ea662138f5184fd12513ea17acf91a5..6d8d3b6edb3427954c85aaeac5f6a2cfb9128487 100644 (file)
@@ -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"));
index 85d6ac5c031e0ca26439322b1d06be36a0c24f3b..d389ae6db589e6f974b68303a782487dde464cff 100644 (file)
@@ -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);