<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On Aug 31, 2017, at 11:35 AM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<div><blockquote type="cite" class=""><div class=""><div class="">The coroutine proposal as it stands essentially exposes raw delimited continuations. While this is a flexible and expressive feature in the abstract, for the concrete purpose of representing asynchronous coroutines, it provides weak user-level guarantees about where their code might be running after being resumed from suspension, and puts a lot of pressure on APIs to be well-behaved in this respect. And if we're building toward actors, where async actor methods should be guaranteed to run "in the actor", I think we'll *need* something more than the bare-bones delimited continuation approach to get there. I think the proposal's desire to keep coroutines independent of a specific runtime model is a good idea, but I also think there are a couple possible modifications we could add to the design to make it easier to reason about what context things run in for any runtime model that benefits from async/await:<br class=""><br class=""># Coroutine context<br class=""><br class="">Associating a context value with a coroutine would let us thread useful information through the execution of the coroutine. This is particularly useful for GCD, so you could attach a queue, QoS, and other attributes to the coroutine, since these aren't reliably available from the global environment. It could be a performance improvement even for things like per-pthread queues, since coroutine context should be cheaper to access than pthread_self. <br class=""><br class="">For example, a coroutine-aware `dispatch_async` could spawn a coroutine with the queue object and other interesting attributes as its context:<br class=""><br class="">extension DispatchQueue {<br class=""> func `async`(_ body: () async -&gt; ()) {<br class=""> &nbsp;&nbsp;dispatch_async(self, {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;beginAsync(context: self) { await body() }<br class=""> &nbsp;&nbsp;})<br class=""> }<br class="">}<br class=""></div></div></blockquote><div><br class=""></div><div>I think it makes perfect sense to add a magically available context to async functions, and something like the above is a good way to populate it. &nbsp;Because is is a magic value that is only available in async functions, giving it a keyword like asyncContext might make sense. &nbsp;That said, I don’t understand how (by itself) this helps the queue hopping problem.</div><div><br class=""></div><div><blockquote type="cite" class=""># `onResume` hooks</blockquote></div><blockquote type="cite" class=""><div class=""><div class=""><br class="">Relying on coroutine context alone still leaves responsibility wholly on suspending APIs to pay attention to the coroutine context and schedule the continuation correctly. You'd still have the expression problem when coroutine-spawning APIs from one framework interact with suspending APIs from another framework that doesn't understand the spawning framework's desired scheduling policy. We could provide some defense against this by letting the coroutine control its own resumption with an "onResume" hook, which would run when a suspended continuation is invoked instead of immediately resuming the coroutine. That would let the coroutine-aware dispatch_async example from above do something like this, to ensure the continuation always ends up back on the correct queue:<br class=""></div></div></blockquote><div><br class=""></div><div>Yes, we need something like this, though I’m not sure how your proposal works:</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class="">extension DispatchQueue {<br class=""> func `async`(_ body: () async -&gt; ()) {<br class=""> &nbsp;&nbsp;dispatch_async(self, {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;beginAsync(<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context: self,<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: { await body() },<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onResume: { continuation in<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Defensively hop to the right queue<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dispatch_async(self, continuation)<br class=""></div></div></blockquote><div><br class=""></div><div>If I’m running on a pthread, and use "someQueue.async {…}”, I don’t see how DispatchQueue.async can know how to take me back to a pthread. &nbsp;If I understand your example code above, it looks like the call will run the continuation on someQueue instead.</div><div><br class=""></div><div>That said, I think that your idea of context pretty much covers it: a non-async function cannot have any idea whether it is run on a queue or thread, but there is also no language way to call an async function from a non-async function. &nbsp;I think this means that beginAsync and DispatchQueue.async will have to define some policy: for example, the implementation of DispatchQueue.async could use its own internal data structures to decide if the current task is being run on some dispatch queue (using the maligned “give me the current queue” operation), and ensure that it returns to the originating queue if it can find one.</div><div><br class=""></div><div>Chains of async functions calling each other would maintain their context, so the other transitions we have to worry about are when an async function calls a non-async function (this just drops the context) or when you get to the bottom of the pile of async 🐢’s and want to actually do something on another context. &nbsp;This implies you’d actually want an async form of DispatchQueue.async, something like this:</div><div><br class=""></div><div>extension DispatchQueue {<br class="">func `async`(_ body: () async -&gt; ()) <b class="">async</b> {<br class="">&nbsp; dispatch_async(self) {<br class="">&nbsp; &nbsp; beginAsync(<br class="">&nbsp; &nbsp; &nbsp; context: self,<br class="">&nbsp; &nbsp; &nbsp; body: {&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;await body()&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<b class="">asyncContext.restore()</b></div><div>&nbsp; &nbsp; &nbsp; })</div><div>&nbsp; }}</div><div><div><br class=""></div><div>Going back to the silly example, if you call DispatchQueue.async from an async function on a pthread, the asyncContext would be the pthread’s, and asyncContext.restore() would take you back to it.</div><div><br class=""></div><div>Another nice thing about this is that it gets you back to a single context per async thing.</div><div><br class=""></div><div>-Chris</div><div><br class=""></div></div></div><div class=""><br class=""></div></body></html>