<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class="">
-Pierre
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Sep 2, 2017, at 1:58 AM, Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">On Aug 31, 2017, at 11:35 AM, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""># Coroutine context<br class=""><br class=""># `onResume` hooks<br class=""></blockquote><br class="">`onResume` hooks seem like a really good way to, essentially, allow arbitrary concurrency primitives to be passed into `async` functions. My main question is, if we have it, why do we need coroutine contexts? It seems to me that anything the coroutine might do with the context could either be passed into its parameters, or encapsulated in its `onResume` hook.<br class=""></div></div></blockquote><div><br class=""></div>No it's not the same. Arbitrary code is this: arbitrary code and data.</div><div><br class=""></div><div>Please read the few mails I sent recently about this, but to recap here quickly:</div><div><br class=""></div><div>It is needed for the runtime (in a broad sense, from language to the operating system) to be able to introspect these:</div><div>- priority attributes</div><div>- dependencies</div><div>- execution contexts (thread/queue/runloop/...)</div><div>- ownership</div><div><br class=""></div><div>Without this info, the scheduling of these coroutines will essentially be random, subject to priority inversions and other similar issues.</div><div><br class=""></div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" class="">and also prevents writing APIs that intentionally change context across an `await`, like a theoretical "goToMainThread()" function<br class=""></blockquote><br class="">You say that like it's a bad thing. :^)<br class=""><br class="">(Seriously, I feel like that makes it *way* too easy to jump to another thread in the middle of a function using what looks like an ordinary line of code. A `let result = await someQueue.async { … }` syntax is a lot clearer that you're hopping to another thread and about what code will run on it, but can get around our current "pyramid of doom" problem. If the async syntax guarantees that the code before and after the `await` runs in the same "environment"—whatever environment is set up by the onResume hook—then that makes async functions much safer and easier for programmers to reason about, while only restricting their power slightly.)<br class=""></div></div></blockquote><div><br class=""></div><div>I do agree that an explicit syntax that spells out the context when it changes is really preferable, and if no context is provided it should mean "the current one".</div><div>The context doesn't necessarily need to be a dispatch queue.</div><div><br class=""></div><div>Ideally API surface that provide similar things could be annotated for Swift to reason about, e.g. to take a realatively obvious example:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>void xpc_connection_send_message_with_reply(xpc_connection_t conn, xpc_object_t msg<b class="">, dispatch_queue_t queue, void (^handler)(xpc_object_t reply)</b>)</div></div><div class=""><br class=""></div><div class="">Could provided sufficient overlay, could be understood by Swift and provide something like this (completely random syntax, I barely do any Swift, so bear with me):</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let reply = await(queue) connection.sendQuery(message: msg)</div><div class=""><br class=""></div><div class="">Because the API could be annotated with `queue` in that function is the execution context for the coroutine, and `handler` is the said coroutine.</div><div class=""><br class=""></div><br class=""><div class="">-Pierre</div></body></html>