Multi-actor execution scenario

Yeah, you're getting a total order because you have a single task. If you had multiple tasks calling C.run, then no, your precondition would not hold reliably.

I suspect that what you're trying to ask is how to guarantee that a linear sequence of events will be handled in order. If actors A and B were serial dispatch queues, and you had a linear sequence of events, and for each event you (1) dispatched something onto A, and then, while still on A, you (2) dispatched something onto B, then you would be guaranteed at each step to handle the events in their original order, because dispatch queues are FIFO. This does not reliably happen with Swift actors: Swift actors are not strictly FIFO, and even if they were, it's not easy to get Swift to guarantee that you'll transition directly from one actor to the next.

If you want this kind of guarantee in Swift, the right way to get it is with a task. Tasks run in a linear order, just like any other code, so if the task receives an event and then passes it off before receiving the next event, the stream of events remains in that linear order. (Of course, each task won't be processing events concurrently, but neither were the dispatch queue in the original example.) You can send events between tasks with an AsyncStream.

8 Likes