ysbaddaden / execution_context

5 stars 2 forks source link

Fiber#status #16

Open ysbaddaden opened 3 months ago

ysbaddaden commented 3 months ago

It would valuable to know the current status of a Fiber. Implementing this is easy, but we currently have two booleans to reflect a fiber's status:

It could be interesting to group these into a single variable (enum), and maybe make it an atomic (set once in the target specific context switch assembly) which might be useful for #15.

Note: we could eventually have sub-states in addition to suspended (e.g. sleep or waiting on something: mutex, channel, event-loop, ...). Useful in combination to https://github.com/crystal-lang/perf-tools/pull/18

ysbaddaden commented 3 months ago

We might want to specify a status to swapcontext, at least to set the :dead status, and/or have a loadcontext that would merely load a fiber's context (no need to save the context of a dead fiber + won't override the :dead status).

This in turns means we'd need ExecutionContext.reschedule(Fiber::Status) or ExecutionContext.???() to call loadcontext (how to name that one: exec? replace? abandon? terminate_fiber?). The later would improve control over the dead fiber's stack: we'd know for sure that the current fiber is dead.

ysbaddaden commented 3 months ago

Merging both booleans is theoretically a good idea, but it's not easy in practice, as once the fiber is dead, its status musn't change anymore but #swapcontext will always set the status to suspended, so either we need to update each assembly to check for the state (tedious), or we need a new #loadcontext that won't touch the current context (also tedious: must duplicate the current assembly).

straight-shoota commented 3 months ago

Perhaps we could only flip a single bit in swapcontext?

ysbaddaden commented 3 months ago

Of course, a flags enum!