[vala/staging: 3/3] codegen: Make every co-routine state its own CodeBlock and bump state early
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging: 3/3] codegen: Make every co-routine state its own CodeBlock and bump state early
- Date: Wed, 29 Nov 2017 09:00:47 +0000 (UTC)
commit 249ff05c4a71623b21f756bea87c34e63fcb1bc6
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sat Oct 21 11:08:08 2017 +0200
codegen: Make every co-routine state its own CodeBlock and bump state early
https://bugzilla.gnome.org/show_bug.cgi?id=789249
ccode/valaccodefunction.vala | 23 ++++++++++++++++++++++-
codegen/valaccodebasemodule.vala | 10 +++++++++-
codegen/valaccodemethodcallmodule.vala | 9 ++++++++-
codegen/valaccodemethodmodule.vala | 2 ++
codegen/valagasyncmodule.vala | 10 +++++++++-
codegen/valagdbusclientmodule.vala | 6 ++++++
tests/Makefile.am | 1 +
tests/asynchronous/bug789249.vala | 17 +++++++++++++++++
8 files changed, 74 insertions(+), 4 deletions(-)
---
diff --git a/ccode/valaccodefunction.vala b/ccode/valaccodefunction.vala
index 8ef10bb..786ab9d 100644
--- a/ccode/valaccodefunction.vala
+++ b/ccode/valaccodefunction.vala
@@ -322,10 +322,31 @@ public class Vala.CCodeFunction : CCodeNode {
add_statement (stmt);
}
- public void close () {
+ public void prepend_statement (CCodeNode stmt) {
+ stmt.line = current_line;
+ current_block.prepend_statement (stmt);
+ }
+
+ public void prepend_expression (CCodeExpression expression) {
+ prepend_statement (new CCodeExpressionStatement (expression));
+ }
+
+ public void prepend_assignment (CCodeExpression left, CCodeExpression right) {
+ prepend_expression (new CCodeAssignment (left, right));
+ }
+
+ public void close (CCodeBlock? block = null) {
+ //FIXME Don't ignore this invalid close without further action
+ if (block != null && statement_stack.index_of (block) >= 0) {
+ return;
+ }
+
do {
var top = statement_stack.remove_at (statement_stack.size - 1);
current_block = top as CCodeBlock;
+ if (current_block == block) {
+ current_block = null;
+ }
} while (current_block == null);
}
}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 439c54e..22bb51f 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -42,6 +42,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
public Map<string,string> variable_name_map = new HashMap<string,string> (str_hash,
str_equal);
public Map<string,int> closure_variable_count_map = new HashMap<string,int> (str_hash,
str_equal);
public Map<LocalVariable,int> closure_variable_clash_map = new HashMap<LocalVariable,int> ();
+ public CCodeBlock? current_co_state_block;
public EmitContext (Symbol? symbol = null) {
current_symbol = symbol;
@@ -4797,10 +4798,17 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
// set state before calling async function to support immediate callbacks
int state = emit_context.next_coroutine_state++;
- ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier
("_data_"), "_state_"), new CCodeConstant (state.to_string ()));
+ ccode.prepend_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier
("_data_"), "_state_"), new CCodeConstant (state.to_string ()));
ccode.add_expression (async_call);
ccode.add_return (new CCodeConstant ("FALSE"));
+
+ if (state > 0) {
+ ccode.close (emit_context.current_co_state_block);
+ emit_context.current_co_state_block = null;
+ }
ccode.add_label ("_state_%d".printf (state));
+ ccode.open_block ();
+ emit_context.current_co_state_block = ccode.current_block;
}
creation_expr = creation_call;
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index 82a0574..af8bf44 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -708,10 +708,17 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
// set state before calling async function to support immediate callbacks
int state = emit_context.next_coroutine_state++;
- ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"),
"_state_"), new CCodeConstant (state.to_string ()));
+ ccode.prepend_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier
("_data_"), "_state_"), new CCodeConstant (state.to_string ()));
ccode.add_expression (async_call);
ccode.add_return (new CCodeConstant ("FALSE"));
+
+ if (state > 0) {
+ ccode.close (emit_context.current_co_state_block);
+ emit_context.current_co_state_block = null;
+ }
ccode.add_label ("_state_%d".printf (state));
+ ccode.open_block ();
+ emit_context.current_co_state_block = ccode.current_block;
}
if (expr.is_assert) {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 4efd04b..45ae58e 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -485,6 +485,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
// coroutine body
ccode.add_label ("_state_0");
+ ccode.open_block ();
+ emit_context.current_co_state_block = ccode.current_block;
}
if (m.closure) {
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index dd8e916..7a75eaa 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -775,11 +775,19 @@ public class Vala.GAsyncModule : GtkModule {
}
if (stmt.yield_expression == null) {
+ // set state early to support immediate callbacks
int state = emit_context.next_coroutine_state++;
- ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"),
"_state_"), new CCodeConstant (state.to_string ()));
+ ccode.prepend_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier
("_data_"), "_state_"), new CCodeConstant (state.to_string ()));
ccode.add_return (new CCodeConstant ("FALSE"));
+
+ if (state > 0) {
+ ccode.close (emit_context.current_co_state_block);
+ emit_context.current_co_state_block = null;
+ }
ccode.add_label ("_state_%d".printf (state));
+ ccode.open_block ();
+ emit_context.current_co_state_block = ccode.current_block;
ccode.add_statement (new CCodeEmptyStatement ());
return;
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 4d9caa1..514297c 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -412,7 +412,13 @@ public class Vala.GDBusClientModule : GDBusModule {
ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier
("_data_"), "_state_"), new CCodeConstant (state.to_string ()));
ccode.add_expression (ccall);
ccode.add_return (new CCodeConstant ("FALSE"));
+ if (state > 0) {
+ ccode.close (emit_context.current_co_state_block);
+ emit_context.current_co_state_block = null;
+ }
ccode.add_label ("_state_%d".printf (state));
+ ccode.open_block ();
+ emit_context.current_co_state_block = ccode.current_block;
ccall = new CCodeFunctionCall (new CCodeIdentifier
("g_async_initable_new_finish"));
ccall.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier
("_data_"), "_source_object_"));
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b062029..4ebd109 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -285,6 +285,7 @@ TESTS = \
asynchronous/bug762819.vala \
asynchronous/bug777242.vala \
asynchronous/bug783543.vala \
+ asynchronous/bug789249.vala \
asynchronous/closures.vala \
asynchronous/generator.vala \
asynchronous/yield.vala \
diff --git a/tests/asynchronous/bug789249.vala b/tests/asynchronous/bug789249.vala
new file mode 100644
index 0000000..0b4bb12
--- /dev/null
+++ b/tests/asynchronous/bug789249.vala
@@ -0,0 +1,17 @@
+static int counter = 0;
+
+static async void foo () {
+ counter++;
+ assert (counter <= 1);
+
+ // This is the simplest way to trigger the issue,
+ // it may happen due to GTask/ThreadPool/threads
+ // getting to call the callback before yield, too.
+ foo.callback ();
+ yield;
+}
+
+void main () {
+ foo.begin ();
+ yield;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]