Skip to content

refactor(concurrency): name the while-loop resume markers and add a resume regression test - #23

Merged
zombocoder merged 1 commit into
zombocoder:mainfrom
Craftegy-NextGen:chore/while-resume-markers
Oct 27, 2026
Merged

zombocoder merged 1 commit into
zombocoder:mainfrom
Craftegy-NextGen:chore/while-resume-markers

Conversation

@sashml

@sashml sashml commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A small readability refactor in the green-threads resume path, plus a regression test for it.

WhileStatementNode records push_back(1) on Coroutine::block_resume_stack when a coroutine
suspends inside the loop body, and reads it back with a bare != 0. The meaning of both numbers lived
only in a comment.

Change

  • Name the two positions as file-local constants: kResumeAtCondition = 0 (re-evaluate the condition)
    and kResumeInBody = 1 (re-enter the body, skipping the condition re-check).
  • Same values and the same comparison: behavior is identical.

Tests

  • ConcurrencyTest.resume_logic_multi_statement_body in tests/test_resume_logic.cpp: suspends inside
    a while body that has statements both before and after the suspension point, and asserts each
    pre-suspend statement runs once per iteration and each post-suspend statement once per resume.

Verification

  • Windows 10, MSVC 2022, Release, Ninja, GoogleTest from vcpkg, on current main with this branch merged: build OK, o2l_tests 527/527 passing.
  • clang-format (repository .clang-format) reports nothing on the changed lines. It still flags the include order of ../Runtime/Scheduler.hpp in this file, which is the same on main and left alone here.
  • Linux and macOS: covered by CI.

If you would rather not take a pure refactor, the test alone is still useful as a guard for the resume
logic; happy to split it out.

… index

The green-threads resume mechanism stores a marker on
Coroutine::block_resume_stack when a coroutine suspends inside a
while-loop body. WhileStatementNode hardcoded push_back(1) with a
"// 1 means resuming body" comment, and read it back via a bare `!= 0`
compare: a magic number whose meaning was only discoverable from the
comment.

Replace both literals with named, file-local constexpr markers
(kResumeAtCondition = 0, kResumeInBody = 1) that document the while node
as a two-position sequence (condition vs body). Behavior is identical
(same values, same comparison); this only removes the magic number.

Tests: adds ConcurrencyTest.resume_logic_multi_statement_body, a
regression guard that suspends inside a while body with statements both
before and after the suspension point, asserting each pre-suspend
statement runs once per iteration and each post-suspend statement once
per resume.

Signed-off-by: svaiml <88979468+svaiml@users.noreply.github.com>
@zombocoder
zombocoder merged commit f3b335e into zombocoder:main Oct 27, 2026
6 checks passed
@sashml
sashml deleted the chore/while-resume-markers branch October 27, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants