Thanks for the discussion John!
Yes, good point that the runtime trickery would end up doing the check twice. And that the nonisolated inference will get in the way... I keep forgetting about that interaction and it always ends up messing up those "don't hop" patterns.
I see what you're saying, ultimately all those things runtime may try to do before we run the task will be defeated by the nonisolated inference eventually anyway since it'll try to switch. Instead we need to adjust the inference and the switching behavior. The last example in your writeup indeed is how we'd blow up in this scheme. I was "lucky" while adapting the existing tests towards this new behavior in that the context and closure were both nonisolated or both isolated
Okey, so let's approach this by fixing the initial hop in those closure funclets instead.
I very much agree that fixing the inference rule to not require the explicit capturing here is even more important than in Task.init. Let's do that for this API and maybe if we'd manage to bring it to Task.init that'd be nice, but separate work.
So, there's a few cases now:
- the statically isolated to the same context as caller
- the "new" isolation inference rule would take care of that; funclet just hops to that inferred isolation, notices it already is, we're good.
- the closure passed to
Task.immediate
is isolated to something ELSE than the dynamic caller isolation- this seems like it would just be taken care of by the task_switch as usual, we're on different than expected, so we'll enqueue.
- we won't enqueue the task at first, but try to run it, task_switch would enqueue to target if it has to
- the current context has no executor at all, we're "dynamically nonisolated"
- this is a new special case here; the first task_switch would have to recognize that's fine and just run inline, without hopping off to global pool; but subsequently do hop to that pool... we'd use some "fake" executor to signal this to the runtime probably.
Getting a bit into implementation weeds a bit much here, we can continue elsewhere, but it's been good to bring those up here a bit! Thanks!
I'm sure I missed some cases, but overall I think that'll work out -- and I'm very excited to "just" fix the isolation inference rule. That'll be a much welcomed win for understandability of this API.