Skip to content

feat(coro): context interface, strand, exact current context - #23

Closed
codethinki wants to merge 1 commit into
masterfrom
feat/strand
Closed

codethinki wants to merge 1 commit into
masterfrom
feat/strand

Conversation

@codethinki

Copy link
Copy Markdown
Owner

A strand for cth_coro: a serial queue on the workers of a scheduler. jvk needs one per Vulkan queue, so submits of one queue never run at the same time, without a thread per queue.

context

  • New abstract cth::co::context: post(), await(handle), await(time point), owns_thread() and current(). Protected non-virtual destructor.
  • scheduler and strand implement it, both final.
  • executor, the payload, the awaiters and the this_coro tag take context const& instead of scheduler const&.
  • Renames: scheduler_payload -> context_payload, payload.scheduler() -> payload.context(), rebind_scheduler() -> rebind_context(), this_coro::scheduler -> this_coro::context, executor::scheduler() -> executor::context().

strand

  • explicit strand(scheduler const&): its jobs run one at a time and in post order, on the workers of the scheduler. The scheduler outlives the strand.
  • await callbacks and timer callbacks run inside the strand.
  • A coroutine holds the strand between two suspension points, not across a co_await.

context::current()

  • The context of the job on the calling thread: the strand inside a strand job, the scheduler in any other worker job, nullptr elsewhere.
  • schedule_awaiter and the spawn final awaiter decide with it. Before, they used owns_thread(), which is also true inside a strand job of the same scheduler, so a coroutine could not leave a strand and a spawn caller could end up inside one.

Shared asio code exists once: src/utility/asio/ops.hpp (post_on, await_on), the macro in asio/macro.hpp.

Tests: 1000 jobs on 4 workers run one at a time and in order, awaits run inside the strand, executor{strand}.schedule(), a coroutine leaves a strand, spawn from and into a strand, current(), a scheduler job runs while a strand job runs. 297/297 on gcc and clang, debug and release. ASan: 0 leaks.

- context: the interface of scheduler and strand (post, await, owns_thread), executor, payloads and awaiters take context const&
- strand: serial queue on the workers of a scheduler, jobs run one at a time in post order
- context::current(): the context of the job on this thread, schedule_awaiter and spawn decide with it
- scheduler -> context renames in the payload, the executor, the awaiters and the this_coro tag
- asio helpers once in src/utility/asio/ops.hpp, the macro in asio/macro.hpp
@codethinki

Copy link
Copy Markdown
Owner Author

Replaced by #25.

@codethinki codethinki closed this Oct 25, 2026
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.

1 participant