public sealed class CoroutineSchedulerWhere suspended coroutines wait, and the thing that decides when they carry on.
No guide page documents this yet — the page shows what the code says about itself.
Remarks
It is a queue per resume point, drained on the loop thread, and nothing else. There is no timer, no thread pool and no synchronisation context anywhere in a coroutine's life: a continuation goes into a list, a drain at a known frame point walks that list in the order things were added, and whatever is ready runs. That is what makes a coroutine as deterministic as a system — the same sequence of frame deltas produces the same sequence of resumptions, which the determinism exit criterion of this phase requires and which nothing built on Task and the thread pool could promise.
Nothing resumes in the frame it suspended in. Every wait records the tick it was made on and becomes eligible on the next one, so await Seconds(0f) costs a frame and while (true) await NextFrame(); is a loop rather than a hang. The tick is the frame for three of the resume points and the fixed step for FixedStep, which is why a frame owing three steps resumes a step-waiting coroutine three times.
Steady state allocates nothing. The waiting entries are structs in a reused list; the continuation delegate is the one the pooled state machine box caches; the state machines themselves come from PoolingAsyncValueTaskMethodBuilder's pool; and the bookkeeping object behind a running coroutine comes from a free list here. A behaviour that starts a coroutine every frame for an hour allocates for the first few and then stops. That is measured, not asserted — see CoroutineAllocationTests.
Fields and properties (6)
public long FrameHow many frames have begun.
public long StepHow many fixed steps have begun.
public GameTime TimeThe clock as of the last BeginFrame.
public ResumePoint CurrentPointThe resume point currently being drained, and Update when none is. This is what an await NextFrame() with no explicit point means.
public int RunningCountHow many coroutines have been started and not yet finished.
public Action<Exception>? UnhandledExceptionWhat to do with an exception that escapes a coroutine. rethrows it out of the drain that observed it.
Methods (11)
public void BeginFrame(in GameTime time)Starts a frame. Call once per frame, before anything drains.
public void BeginStep()Starts a fixed step. Call once per step, before FixedStep drains.
public CoroutineHandle Run(Coroutine coroutine)Starts a coroutine and watches it to the end.
public void Drain(ResumePoint point)Resumes everything waiting on a resume point that is ready to go.
public int WaitingCount(ResumePoint point)How many coroutines are waiting on a resume point.
public CoroutineAwaitable NextFrame(ResumePoint? point = null, ICoroutineOwner? owner = null)Waits for the next occurrence of a resume point.
public CoroutineAwaitable Seconds(float seconds, ResumePoint? point = null, ICoroutineOwner? owner = null)Waits an amount of scaled game time. A paused game does not advance it.
public CoroutineAwaitable UnscaledSeconds(float seconds, ResumePoint? point = null, ICoroutineOwner? owner = null)Waits an amount of unscaled time, which a pause does not stop.
public CoroutineAwaitable Until(Func<bool> predicate, ResumePoint? point = null, ICoroutineOwner? owner = null)Waits until a predicate is true, testing it once per occurrence of the resume point.
public CoroutineAwaitable While(Func<bool> predicate, ResumePoint? point = null, ICoroutineOwner? owner = null)Waits while a predicate is true.
public CoroutineAwaitable ResumeOnLoop(ResumePoint point = Update, ICoroutineOwner? owner = null)Comes back to the loop thread from wherever the coroutine currently is.
Used by (14)
- BehaviorVixen.Engine
- BehaviorStoreVixen.Engine
- ClockVixen.Engine.Tests
- CoroutineAllocationTestsVixen.Engine.Tests
- CoroutineAwaitableVixen.Engine
- CoroutineEndOfFrameSystemVixen.Engine
- CoroutineFixedStepSystemVixen.Engine
- CoroutineHandleVixen.Engine
- CoroutineLateUpdateSystemVixen.Engine
- CoroutineTestsVixen.Engine.Tests
- CoroutineUpdateSystemVixen.Engine
- EngineLoopVixen.Engine
- RunningCoroutineVixen.Engine
- TypeRegistrationVixen.Editor.App.Tests