Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Nuanced en masse cancellation  #2

@nasser

Description

@nasser

When restarting a game it is useful to be able to get rid of any background coroutines while preserving the "main" coroutine. It's important to do this in a way that does not enshrine the "main" coroutine in any way.

One possible approach is to take advantage of the ordered nature of schedules. If coroutine B is farther in the schedule than coroutine A then B guaranteed to have started later than A. If we want to get rid of any coroutines started after a coroutine, we could expose a removeAllAfter(i) method, where i is the offset into the schedule after which to remove coroutines.

SCHED.add(function* () { // "main" coroutine
  const depthAtStart = SCHED.size  // or similar
  while(true) {
    // ... coroutine body
    // when resetting
    SCHED.removeAllAfter(depthAtStart)
  }
})

removeAllAfter should call return() on the relevant coroutines and allow them to be cleaned up in later ticks.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions