Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR "tightens" the wordings on the requirements for schedulers when implementing the Picos effects.
The words "must" and "should" are now used more intentionally.
The biggest change is that the
Spawn
effect should now be all or nothing — ifspawn
returns normally, the scheduler should guarantee that all the mains spawned will be called and ifspawn
raises no mains should be called. This is how the sample effects based schedulers have worked already (modulo running out of memory) and this requirement has crossed my mind earlier, but for some reason I had failed to spell it out explicitly. This PR also changesPicos_threaded
to provide the same guarantee as much as possible. The reason behind this is that otherwise it becomes very tricky (impossible even) to ensure that all spawned fibers are accounted for and no resources are leaked. OTOH, with this guarantee, it is safe to e.g. increment a counter before attempting spawn and then decrement it either in the fiber or in an exception handler:In general, all of the effects are all or nothing.
Two of the effects are somewhat special when it comes to cancelation:
As specified earlier already, the
Current
effect must not be discontinued. This is becausecurrent
is used by programs to obtain the fiber handle through which cancelation propagation can be controlled and ifcurrent
would raise, it would be much more difficult to control cancelation propagation.The
Await
effect also must not be discontinued. The scheduler must instead continue it with the cancelation exception (with backtrace). This is for performance, convenience, and to make the cancelation visible in the types — I expect calls ofawait
are the places where cancelation requires extra care. Having theAwait
always return a value can be convenient in cases where one must deal with the possibity of cancelation while also juggling atomic state and cancelation propagation.The motivation behind the requirements for schedulers is to make it possible to build non-trivial mechanisms, such as structured concurrency models and synchronization and communication primitives, on top of the effects correctly and efficiently.