Skip to content

Add awakened_from_remote() to bypass remote_ready_queue#1

Draft
Akatsukis wants to merge 14 commits into
categoryfrom
xiaojun/awakened_from_remote
Draft

Add awakened_from_remote() to bypass remote_ready_queue#1
Akatsukis wants to merge 14 commits into
categoryfrom
xiaojun/awakened_from_remote

Conversation

@Akatsukis

Copy link
Copy Markdown
Collaborator

Adds a virtual method algorithm::awakened_from_remote() that allows scheduling algorithms with thread-safe ready-queues to handle cross-thread fiber wakeups directly, avoiding the priority inversion caused by remote_ready_queue_ draining only on the owning thread.

Guarded by BOOST_FIBERS_WORKER_QUEUE_LOCK to protect worker_queue_ for cross-thread detach/attach with zero overhead when not needed.

jhunsaker and others added 2 commits March 27, 2026 18:05
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
These mutex types are unused by the top-level project. Removes source
files, headers, and references from CMakeLists.txt and all.hpp.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new scheduling extension point to allow certain scheduling algorithms to directly handle cross-thread wakeups, aiming to reduce latency/priority inversion caused by draining remote_ready_queue_ only on the owning thread.

Changes:

  • Introduces boost::fibers::algo::algorithm::awakened_from_remote() (default false) as an optional fast-path for remote scheduling.
  • Updates scheduler::schedule_from_remote() to try awakened_from_remote() before falling back to remote_ready_queue_.
  • Adds an optional BOOST_FIBERS_WORKER_QUEUE_LOCK spinlock to protect worker_queue_ for cross-thread attach/detach and adjusts related code paths.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/scheduler.cpp Uses the new awakened_from_remote() fast-path and adds optional locking around worker_queue_ operations.
include/boost/fiber/scheduler.hpp Declares optional worker_splk_ and enforces multi-thread support when BOOST_FIBERS_WORKER_QUEUE_LOCK is enabled.
include/boost/fiber/algo/algorithm.hpp Adds the new virtual awakened_from_remote() API and documents intended usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/scheduler.cpp Outdated
Comment thread src/scheduler.cpp Outdated
Comment thread src/scheduler.cpp Outdated
Comment thread include/boost/fiber/algo/algorithm.hpp
Comment thread include/boost/fiber/algo/algorithm.hpp Outdated
@Akatsukis Akatsukis force-pushed the xiaojun/awakened_from_remote branch 2 times, most recently from 08c6e7b to d4627cd Compare April 7, 2026 13:57
jhunsaker and others added 11 commits April 7, 2026 22:50
Unused by the top-level project. Removes source, header, examples,
and performance benchmarks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project. Removes source, header, examples,
and performance benchmarks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project. Removes header, example, and include
references from fiber.hpp and all.hpp.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project. Removes source, header, dedicated
test files, and dead do_wait() references in fiber tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project. Removes fss.hpp, detail/fss.hpp, test
files, and cleans fss_data_ from context.hpp and context.cpp.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project. Removes header, test files, and
examples that solely used unbuffered_channel.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Unused by the top-level project. Removes rtm.hpp, spinlock_rtm.hpp,
and strips BOOST_USE_TSX branches from spinlock.hpp.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/boost/fiber/scheduler.hpp Outdated
@Akatsukis Akatsukis force-pushed the xiaojun/awakened_from_remote branch 2 times, most recently from 84fb830 to dbaa8a9 Compare April 9, 2026 16:54
@Akatsukis Akatsukis requested a review from Copilot April 9, 2026 17:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/scheduler.cpp Outdated
@Akatsukis Akatsukis force-pushed the xiaojun/awakened_from_remote branch 2 times, most recently from 435a0f3 to 595be01 Compare April 13, 2026 19:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/scheduler.cpp Outdated
Comment on lines +289 to +292
#if defined(BOOST_FIBERS_NO_SLEEP_QUEUE)
BOOST_ASSERT_MSG( false, "timed wait not supported with BOOST_FIBERS_NO_SLEEP_QUEUE");
return false;
#else

Copilot AI Apr 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With BOOST_FIBERS_NO_SLEEP_QUEUE defined, these wait_until() overloads assert and then return false. In release builds (asserts compiled out) this will silently report an immediate timeout, which can break higher-level primitives (e.g., condition_variable_any timed waits) in a hard-to-diagnose way. Consider making this a hard failure even in release (terminate/abort) or removing/ifdef’ing out timed-wait APIs when BOOST_FIBERS_NO_SLEEP_QUEUE is enabled so misuse can’t silently change semantics.

Copilot uses AI. Check for mistakes.
Comment thread src/scheduler.cpp Outdated
Comment on lines +318 to +321
#if defined(BOOST_FIBERS_NO_SLEEP_QUEUE)
BOOST_ASSERT_MSG( false, "timed wait not supported with BOOST_FIBERS_NO_SLEEP_QUEUE");
return false;
#else

Copilot AI Apr 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as the other wait_until() overload: under BOOST_FIBERS_NO_SLEEP_QUEUE this will silently behave as an immediate timeout in non-assert builds. Consider enforcing this at compile time or failing hard in all build modes to prevent subtle logic errors in timed-wait callers.

Copilot uses AI. Check for mistakes.
Comment thread include/boost/fiber/scheduler.hpp
@Akatsukis Akatsukis marked this pull request as ready for review April 14, 2026 16:03
@Akatsukis Akatsukis force-pushed the xiaojun/awakened_from_remote branch from 595be01 to f8c75be Compare April 23, 2026 17:28
@Akatsukis Akatsukis requested a review from Copilot April 23, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/boost/fiber/scheduler.hpp
Comment thread src/scheduler.cpp Outdated
Comment thread src/scheduler.cpp
Comment thread include/boost/fiber/context.hpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/boost/fiber/scheduler.hpp
Comment thread src/scheduler.cpp
Comment on lines +226 to +239
#if defined(BOOST_FIBERS_AWAKENED_FROM_REMOTE)
// Let the algorithm handle cross-thread scheduling directly if it
// supports a thread-safe ready-queue (e.g. a shared priority queue).
// Gated by BOOST_FIBERS_AWAKENED_FROM_REMOTE because this path requires
// the atomic sleep_linked_ mirror (so sleep_is_linked() is safe to
// read remotely) and the worker_splk_ spinlock (so the subsequent
// ctx->detach() inside awakened_from_remote does not race the owning
// thread's attach of another fiber).
if ( ! ctx->is_context( type::pinned_context) && ! ctx->sleep_is_linked() ) {
if ( algo_->awakened_from_remote( ctx) ) {
algo_->notify();
return;
}
}

Copilot AI Apr 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new cross-thread fast-path (awakened_from_remote() + atomic sleep_linked_ mirror) introduces new concurrency behavior but there are no tests exercising (1) that awakened_from_remote() is invoked for eligible remote wakeups, (2) that sleeping/pinned contexts correctly fall back to remote_ready_queue_, and (3) that no duplicate scheduling occurs under races. Given the existing multi-threaded test suite, it would be good to add a focused regression test with a custom algorithm overriding awakened_from_remote() and counters/assertions for these cases.

Copilot uses AI. Check for mistakes.
Extends algorithm::awakened_from_remote() so algorithms with a
thread-safe ready queue (e.g. a shared priority queue) can bypass the
per-thread remote_ready_queue_ FIFO on cross-thread wakeups. When
defined, BOOST_FIBERS_AWAKENED_FROM_REMOTE also enables:

  - a spinlock around attach/detach_worker_context, so ctx->detach()
    from a remote thread (as performed inside awakened_from_remote)
    does not race the owning thread's attach for a different fiber.

  - an atomic mirror of the sleep-queue link state on context, so
    sleep_is_linked() can be queried from a remote thread without
    touching the non-atomic intrusive sleep_hook_.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jhunsaker jhunsaker force-pushed the xiaojun/awakened_from_remote branch from 04b5548 to 49a83fe Compare April 28, 2026 14:11
@Akatsukis Akatsukis marked this pull request as draft May 13, 2026 15:42
@jhunsaker jhunsaker force-pushed the category branch 2 times, most recently from 133ac78 to a7c0273 Compare May 29, 2026 05:45
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