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

Nuanced en masse cancellation #2

Open
nasser opened this issue Dec 12, 2021 · 0 comments
Open

Nuanced en masse cancellation #2

nasser opened this issue Dec 12, 2021 · 0 comments
Labels
coroutines enhancement New feature or request

Comments

@nasser
Copy link
Owner

nasser commented Dec 12, 2021

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.

@nasser nasser added enhancement New feature or request coroutines labels Dec 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
coroutines enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant