Skip to content

Conversation

@kammce
Copy link
Member

@kammce kammce commented Jan 4, 2026

This PR adds a proxy system to the async context, introduces an unsafe cancellation routine for contexts, refactors a number of coroutine helpers and updates the test suite accordingly. The changes also include new benchmarks, build system tweaks, and minor clean‑ups.

Proxy provides a means for a coroutine to use its context to supervise other async operations without losing control over being the active coroutine.


Key Functional Changes

Feature What changed
Proxy contexts context::borrow_proxy() now returns a proxy context that shares the same stack space as its parent but has its own active coroutine handle. The proxy owns a proxy_info struct that tracks the original “origin” and the parent. The proxy is created via a private constructor context(proxy_tag, context&).
Proxy state The original m_proxy member was a scheduler_t; it is now a proxy_state variant (`proxy_info
Unsafe cancellation context::unsafe_cancel() walks the coroutine chain starting from m_active_handle and destroys every coroutine up to the top‑level one. The stack is reset but the public future object that was bound to this context is not marked cancelled – hence “unsafe”.
Memory accounting memory_used(), capacity(), and memory_remaining() were updated to use the new layout.
State handling The context’s state() method now returns a blocked_by (defaulting to nothing) and the transition logic uses the proxy’s scheduler.
Future refactor future now stores its state in a variant future_state<T> (`handle
Cancellation handling future::cancel() destroys the coroutine and marks its state as cancelled_state. Helper operation_cancelled exception is thrown when awaiting a cancelled future.
Future construction promise::get_return_object() now creates the future from the promise’s coroutine handle and records the frame size.
Future for void future<void> has its own awaiter that throws if the future was cancelled or had an exception.
Context constructor context(scheduler_t const&, usize) now initializes the proxy field. The old m_scheduler member is removed; scheduler is stored in proxy_state.
Proxy creation guard borrow_proxy() is deleted for temporaries (context&&) to avoid dangling references.

Test Additions

  • Cancellation test – verifies that unsafe_cancel() correctly destroys all coroutines and resets the stack.
  • Exception propagation test – checks that a thrown exception inside a coroutine propagates to the caller.
  • Proxy coroutine tests – exercise proxies that run coroutines in a shared stack and handle timeouts.
  • Memory accounting assertions – confirm that memory usage is zero after cancellation.

Benchmark Additions

  • Added a new benchmarks directory with the original benchmark file (benchmark.cpp) moved from benchmark.
  • Updated CMake to link against Google Benchmark.
  • Benchmarks cover:
    • Virtual calls
    • Futures that return synchronously or via coroutines
    • Mixed coroutine/sync patterns
    • Void‑return coroutines
    • Variant return type virtual calls

Build System & Packaging

  • CMakeLists.txt now:
    • Adds benchmarks/* to the source list.
    • Builds a separate async_benchmark executable only when not cross‑compiling.
  • conanfile.py
    • Adds benchmarks to the exported sources.
    • Only adds test dependencies (boost-ext-ut, benchmark) when building for non‑baremetal OSes.

@kammce kammce changed the title ✨ Add proxy context and much more ✨ Add proxy capability to context & add cancellation support Jan 4, 2026
@kammce kammce merged commit 891dc61 into main Jan 4, 2026
5 checks passed
@kammce kammce deleted the proxy-context branch January 4, 2026 16:43
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.

2 participants