Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fiber queues for execution context schedulers #15345

Merged

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Jan 14, 2025

Introduces 3 queues that will be used the ExecutionContext schedulers. They derivate from Go's internal queues (q, runq and globrunq).

  1. Fiber::Queue: holds an unbounded singly-linked list of Fiber with a bulk insert operation.

    Note: we may consider renaming Fiber::Queue as Fiber::List and Fiber#schedlink as Fiber#list_next?

  2. ExecutionContext::GlobalQueue: wraps a Fiber::Queue with optional thread-safety and a bulk grab operation. There will be a single global queue per execution context shared among one or more schedulers.

    The point is to have an unbounded space to store as many fibers as needed, or to store cross context enqueues —the runnables queue below only supports a single producer.

  3. ExecutionContext::Runnables: a bounded, lock-free, chase-lev queue (single producer, multiple consumers) with bulk operations. There will be a one runnables queue per execution context scheduler (aka local queue).

    On overflow or underflow it pushes/grabs half the queue size to/from the global queue (locking the mutex once in a while).

    Any scheduler can steal from any runnables queue in the execution context at any time, directly into their own local queue.

    The point is to have a quick list to push/pop and steal from, and to limit the occurrences where we must lock the scheduler mutex to reach to the global queue.

Refs #15342

Simple abstraction on top of a mutex and condition variable to
synchronize the execution of a set of threads.
@BlobCodes
Copy link
Contributor

Fiber::Queue: holds an unbounded singly-linked list of Fiber

Isn't that a Stack / LIFO?

@yxhuvud
Copy link
Contributor

yxhuvud commented Jan 14, 2025

How much of the queues will be public interface that are hard to change later on?

@ysbaddaden
Copy link
Contributor Author

ysbaddaden commented Jan 14, 2025

@BlobCodes Yes, it's a LIFO singly linked list (in contrast to the FIFO doubly linked list of Crystal::PointerLinkedList).

@yxhuvud Good call, they should all be :nodoc: (I missed GlobalQueue) at least for the time being!

@ysbaddaden ysbaddaden force-pushed the feature/execution-context-queues branch from defe37d to 7c67d27 Compare January 20, 2025 17:45
@ysbaddaden
Copy link
Contributor Author

Moved to Fiber::ExecutionContext as per #15350.

src/fiber.cr Outdated Show resolved Hide resolved
src/fiber/queue.cr Outdated Show resolved Hide resolved
src/fiber/execution_context/runnables.cr Outdated Show resolved Hide resolved
src/fiber/execution_context/runnables.cr Outdated Show resolved Hide resolved
@ysbaddaden
Copy link
Contributor Author

The MinGW failure on CI should be fixed by #15404.

@straight-shoota
Copy link
Member

Hm, that'll leave MinGW broken until we merge that fixup then? 🤔

@ysbaddaden
Copy link
Contributor Author

ysbaddaden commented Feb 4, 2025

That's possible, yes.

We might want to split #15404:

@ysbaddaden
Copy link
Contributor Author

Updated to add:

  1. cherry-pick the commit that adds the stack arguments to Fiber#initialize;
  2. use fake stacks in the queue specs.

Let's avoid any further changes 🤞

@ysbaddaden
Copy link
Contributor Author

Following the addition of Fiber::Stack in #15409, the new_fake_fiber helper will need to be updated.

@ysbaddaden ysbaddaden force-pushed the feature/execution-context-queues branch from bfe4756 to edcb13f Compare February 10, 2025 16:59
@ysbaddaden
Copy link
Contributor Author

I replaced the last couple commits to integrate #15434 and the last version of #15409 and a fixup for new_fake_fiber to use Fiber::Stack.

@straight-shoota straight-shoota added this to the 1.16.0 milestone Feb 11, 2025
ysbaddaden added a commit to ysbaddaden/crystal that referenced this pull request Feb 11, 2025
@straight-shoota straight-shoota changed the title RFC 2: Queues Add fiber queues for execution context schedulers Feb 12, 2025
@straight-shoota straight-shoota merged commit 15fc484 into crystal-lang:master Feb 12, 2025
71 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants