deallocate tasks when dropping a scheduler
hawkw opened this issue · 3 comments
dropping an Arc
ed scheduler should drop any tasks that have been spawned on that scheduler.
this is easy when the tasks have been woken, as we can simply drain the run queue. however, we don't have a way to "find" tasks bound to the scheduler that are not in the run queue. we probably need a linked list of all tasks bound to a scheduler, so that they can all be found even if they have not yet been woken.
the StaticScheduler
and LocalStaticScheduler
types cannot be dropped. therefore, they will never need to drop all their owned tasks. we should avoid introducing any new overhead for handling task dropping for those scheduler types.
i don't want to add a linked list of all owned tasks that requires locking a Mutex
on spawn to bind the task... we should consider adding a linked list variant where pushes don't need a &mut
borrow, so tasks can be registered concurrently with a read lock.
this could also be useful for optimizing some synchronization primitives.
OTTOH since an owned-task list is per-scheduler, real contention only occurs in the face of external spawns from other cores...
on the other hand, an owned task list would be potentially be contended in the face of work-stealing.
on the third hand, random-access removals from an owned-tasks list only occur on task completion, which is always happening from "inside" the scheduler; this is quite different from sync primitive waitlists, where a cancelled future can remove itself at any time. and, because newly-spawned tasks are in a run queue, scheduler shutdown can drop them when it drains the run queue, so tasks don't need to be added to an owned task list when they're spawned. instead, they only need to be added to owned tasks the first time they yield. so, the owned tasks list can be owned exclusively by the scheduler itself.