I wonder if it is possible to set up a dispatcher that ensures that coroutines inside it cannot ever be run simultaneously on different threads. That is, the dispatcher is not necessarily pinned to a specific thread, but if for example the dispatcher runs coroutine 1 on thread A and coroutine 2 on thread B, then 1 and 2 cannot ever run at the same time. In other words, such a dispatcher would ensure that all coroutines that are dispatched by it always run in sequence.
This is useful in cases where you use coroutines not for distributing workload but rather for disentangling state machines. For example, if you write code for controlling some peripheral device, the handshake, command sequences etc. typically are either implemented as a state machine that is tied to a reactor pattern (very often found in POSIX IO based C/C++ code) or a separate dedicated thread that runs all of that stuff. In such code, you usually do not want to run parts of those sequences in parallel. Such code isn’t computationally expensive, but writing it as coroutines greatly simplifies it.
So, any ideas? I know that you can get a single thread executor if you use the JVM, but I wonder if there is a variant that works across platforms.