Coroutine Explained

Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.

They have been described as "functions whose execution you can pause".[1]

Melvin Conway coined the term coroutine in 1958 when he applied it to the construction of an assembly program. The first published explanation of the coroutine appeared later, in 1963.

Definition and types

There is no single precise definition of coroutine. In 1980 Christopher D. Marlin[2] summarized two widely-acknowledged fundamental characteristics of a coroutine:

  1. the values of data local to a coroutine persist between successive calls;
  2. the execution of a coroutine is suspended as control leaves it, only to carry on where it left off when control re-enters the coroutine at some later stage.

Besides that, a coroutine implementation has 3 features:

  1. the control-transfer mechanism. asymmetric coroutines usually provide keywords like yield and resume. Programmers cannot freely choose which frame to yield to. The runtime only yields to the nearest caller of the current coroutine. On the other hand, in symmetric coroutines, programmers must specify a yield destination.
  2. whether coroutines are provided in the language as first-class objects, which can be freely manipulated by the programmer, or as constrained constructs;
  3. whether a coroutine is able to suspend its execution from within nested function calls. Such a coroutine is a stackful coroutine. One to the contrary is called stackless coroutines, where unless marked as coroutine, a regular function can't use the keyword yield.

The paper "Revisiting Coroutines"[3] published in 2009 proposed term full coroutine to denote one that supports first-class coroutine and is stackful. Full Coroutines deserve their own name in that they have the same expressive power as one-shot continuations and delimited continuations. Full coroutines are either symmetric or asymmetric. Importantly, whether a coroutine is symmetric or asymmetric has no bearing on how expressive it can be as they are equally as expressive, though full coroutines are more expressive than non-full coroutines. While their expressive power is the same, asymmetrical coroutines more closely resemble routine based control structures in the sense that control is always passed back to the invoker, which programmers may find more familiar.

Comparison with

Subroutines

Subroutines are special cases of coroutines. When subroutines are invoked, execution begins at the start, and once a subroutine exits, it is finished; an instance of a subroutine only returns once, and does not hold state between invocations. By contrast, coroutines can exit by calling other coroutines, which may later return to the point where they were invoked in the original coroutine; from the coroutine's point of view, it is not exiting but calling another coroutine. Thus, a coroutine instance holds state, and varies between invocations; there can be multiple instances of a given coroutine at once. The difference between calling another coroutine by means of "yielding" to it and simply calling another routine (which then, also, would return to the original point), is that the relationship between two coroutines which yield to each other is not that of caller-callee, but instead symmetric.

Any subroutine can be translated to a coroutine which does not call yield.

Here is a simple example of how coroutines can be useful. Suppose you have a consumer-producer relationship where one routine creates items and adds them to a queue and another removes items from the queue and uses them. For reasons of efficiency, you want to add and remove several items at once. The code might look like this:

var q := new queue coroutine produce loop while q is not full create some new items add the items to q yield to consume coroutine consume loop while q is not empty remove some items from q use the items yield to produce call produce

The queue is then completely filled or emptied before yielding control to the other coroutine using the yield command. The further coroutines calls are starting right after the yield, in the outer coroutine loop.

Although this example is often used as an introduction to multithreading, two threads are not needed for this: the yield statement can be implemented by a jump directly from one routine into the other.

Threads

Coroutines are very similar to threads. However, coroutines are cooperatively multitasked, whereas threads are typically preemptively multitasked. Coroutines provide concurrency, because they allow tasks to be performed out of order or in a changeable order, without changing the overall outcome, but they do not provide parallelism, because they do not execute multiple tasks simultaneously. The advantages of coroutines over threads are that they may be used in a hard-realtime context (switching between coroutines need not involve any system calls or any blocking calls whatsoever), there is no need for synchronization primitives such as mutexes, semaphores, etc. in order to guard critical sections, and there is no need for support from the operating system.

It is possible to implement coroutines using preemptively-scheduled threads, in a way that will be transparent to the calling code, but some of the advantages (particularly the suitability for hard-realtime operation and relative cheapness of switching between them) will be lost.

Generators

See main article: Generator (computer programming). Generators, also known as semicoroutines,[4] are a subset of coroutines. Specifically, while both can yield multiple times, suspending their execution and allowing re-entry at multiple entry points, they differ in coroutines' ability to control where execution continues immediately after they yield, while generators cannot, instead transferring control back to the generator's caller.[5] That is, since generators are primarily used to simplify the writing of iterators, the yield statement in a generator does not specify a coroutine to jump to, but rather passes a value back to a parent routine.

However, it is still possible to implement coroutines on top of a generator facility, with the aid of a top-level dispatcher routine (a trampoline, essentially) that passes control explicitly to child generators identified by tokens passed back from the generators:

var q := new queue generator produce loop while q is not full create some new items add the items to q yield generator consume loop while q is not empty remove some items from q use the items yield subroutine dispatcher var d := new dictionary(generatoriterator) d[produce] := start produce d[consume] := start consume var current := produce loop call current current := next d[current] call dispatcher

A number of implementations of coroutines for languages with generator support but no native coroutines (e.g. Python before 2.5) use this or a similar model.

Mutual recursion

Using coroutines for state machines or concurrency is similar to using mutual recursion with tail calls, as in both cases the control changes to a different one of a set of routines. However, coroutines are more flexible and generally more efficient. Since coroutines yield rather than return, and then resume execution rather than restarting from the beginning, they are able to hold state, both variables (as in a closure) and execution point, and yields are not limited to being in tail position; mutually recursive subroutines must either use shared variables or pass state as parameters. Further, each mutually recursive call of a subroutine requires a new stack frame (unless tail call elimination is implemented), while passing control between coroutines uses the existing contexts and can be implemented simply by a jump.

Common uses

Coroutines are useful to implement the following:

Native support

Coroutines originated as an assembly language method, but are supported in some high-level programming languages.

Since continuations can be used to implement coroutines, programming languages that support them can also quite easily support coroutines.

Implementations

, many of the most popular programming languages, including C and its derivatives, do not have built-in support for coroutines within the language or their standard libraries. This is, in large part, due to the limitations of stack-based subroutine implementation. An exception is the C++ library Boost.Context, part of boost libraries, which supports context swapping on ARM, MIPS, PowerPC, SPARC and x86 on POSIX, Mac OS X and Windows. Coroutines can be built upon Boost.Context.

In situations where a coroutine would be the natural implementation of a mechanism, but is not available, the typical response is to use a closurea subroutine with state variables (static variables, often boolean flags) to maintain an internal state between calls, and to transfer control to the correct point. Conditionals within the code result in the execution of different code paths on successive calls, based on the values of the state variables. Another typical response is to implement an explicit state machine in the form of a large and complex switch statement or via a goto statement, particularly a computed goto. Such implementations are considered difficult to understand and maintain, and a motivation for coroutine support.

Threads, and to a lesser extent fibers, are an alternative to coroutines in mainstream programming environments today. Threads provide facilities for managing the real-time cooperative interaction of simultaneously executing pieces of code. Threads are widely available in environments that support C (and are supported natively in many other modern languages), are familiar to many programmers, and are usually well-implemented, well-documented and well-supported. However, as they solve a large and difficult problem they include many powerful and complex facilities and have a correspondingly difficult learning curve. As such, when a coroutine is all that is needed, using a thread can be overkill.

One important difference between threads and coroutines is that threads are typically preemptively scheduled while coroutines are not. Because threads can be rescheduled at any instant and can execute concurrently, programs using threads must be careful about locking. In contrast, because coroutines can only be rescheduled at specific points in the program and do not execute concurrently, programs using coroutines can often avoid locking entirely. This property is also cited as a benefit of event-driven or asynchronous programming.

Since fibers are cooperatively scheduled, they provide an ideal base for implementing coroutines above.[18] However, system support for fibers is often lacking compared to that for threads.

C

In order to implement general-purpose coroutines, a second call stack must be obtained, which is a feature not directly supported by the C language. A reliable (albeit platform-specific) way to achieve this is to use a small amount of inline assembly to explicitly manipulate the stack pointer during initial creation of the coroutine. This is the approach recommended by Tom Duff in a discussion on its relative merits vs. the method used by Protothreads.[19] On platforms which provide the POSIX sigaltstack system call, a second call stack can be obtained by calling a springboard function from within a signal handler[20] [21] to achieve the same goal in portable C, at the cost of some extra complexity. C libraries complying to POSIX or the Single Unix Specification (SUSv3) provided such routines as getcontext, setcontext, makecontext and swapcontext, but these functions were declared obsolete in POSIX 1.2008.[22]

Once a second call stack has been obtained with one of the methods listed above, the setjmp and longjmp functions in the standard C library can then be used to implement the switches between coroutines. These functions save and restore, respectively, the stack pointer, program counter, callee-saved registers, and any other internal state as required by the ABI, such that returning to a coroutine after having yielded restores all the state that would be restored upon returning from a function call. Minimalist implementations, which do not piggyback off the setjmp and longjmp functions, may achieve the same result via a small block of inline assembly which swaps merely the stack pointer and program counter, and clobbers all other registers. This can be significantly faster, as setjmp and longjmp must conservatively store all registers which may be in use according to the ABI, whereas the clobber method allows the compiler to store (by spilling to the stack) only what it knows is actually in use.

Due to the lack of direct language support, many authors have written their own libraries for coroutines which hide the above details. Russ Cox's libtask library[23] is a good example of this genre. It uses the context functions if they are provided by the native C library; otherwise it provides its own implementations for ARM, PowerPC, Sparc, and x86. Other notable implementations include libpcl,[24] coro,[25] lthread,[26] libCoroutine,[27] libconcurrency,[28] libcoro,[29] ribs2,[30] libdill.,[31] libaco,[32] and libco.

In addition to the general approach above, several attempts have been made to approximate coroutines in C with combinations of subroutines and macros. Simon Tatham's contribution,[33] based on Duff's device, is a notable example of the genre, and is the basis for Protothreads and similar implementations.[34] In addition to Duff's objections, Tatham's own comments provide a frank evaluation of the limitations of this approach: "As far as I know, this is the worst piece of C hackery ever seen in serious production code." The main shortcomings of this approximation are that, in not maintaining a separate stack frame for each coroutine, local variables are not preserved across yields from the function, it is not possible to have multiple entries to the function, and control can only be yielded from the top-level routine.

C++

C#

C# 2.0 added semi-coroutine (generator) functionality through the iterator pattern and yield keyword.[39] [40] C# 5.0 includes await syntax support. In addition:

Clojure

Cloroutine is a third-party library providing support for stackless coroutines in Clojure. It's implemented as a macro, statically splitting an arbitrary code block on arbitrary var calls and emitting the coroutine as a stateful function.

D

D implements coroutines as its standard library class Fiber A generator makes it trivial to expose a fiber function as an input range, making any fiber compatible with existing range algorithms.

Go

Go has a built-in concept of "goroutines", which are lightweight, independent processes managed by the Go runtime. A new goroutine can be started using the "go" keyword. Each goroutine has a variable-size stack which can be expanded as needed. Goroutines generally communicate using Go's built-in channels.[41] [42] [43] [44]

Java

There are several implementations for coroutines in Java. Despite the constraints imposed by Java's abstractions, the JVM does not preclude the possibility.[45] There are four general methods used, but two break bytecode portability among standards-compliant JVMs.

JavaScript

Kotlin

Kotlin implements coroutines as part of a first-party library.

Lua

Lua has supported first-class stackful asymmetric coroutines since version 5.0 (2003),[47] in the standard library coroutine.[48] [49]

Modula-2

Modula-2 as defined by Wirth implements coroutines as part of the standard SYSTEM library.

The procedure NEWPROCESS fills in a context given a code block and space for a stack as parameters, and the procedure TRANSFER transfers control to a coroutine given the coroutine's context as its parameter.

Mono

The Mono Common Language Runtime has support for continuations,[50] from which coroutines can be built.

.NET Framework

During the development of the .NET Framework 2.0, Microsoft extended the design of the Common Language Runtime (CLR) hosting APIs to handle fiber-based scheduling with an eye towards its use in fiber-mode for SQL server.[51] Before release, support for the task switching hook ICLRTask::SwitchOut was removed due to time constraints.[52] Consequently, the use of the fiber API to switch tasks is currently not a viable option in the .NET Framework.

OCaml

OCaml supports coroutines through its Thread module.[53] These coroutines provide concurrency without parallelism, and are scheduled preemptively on a single operating system thread. Since OCaml 5.0, green threads are also available; provided by different modules.

Perl

Coroutines are natively implemented in all Raku backends.[54]

PHP

Python

Racket

Racket provides native continuations, with a trivial implementation of coroutines provided in the official package catalog. Implementation by S. De Gabrielle

Ruby

Scheme

Since Scheme provides full support for continuations, implementing coroutines is nearly trivial, requiring only that a queue of continuations be maintained.

Smalltalk

Since, in most Smalltalk environments, the execution stack is a first-class citizen, coroutines can be implemented without additional library or VM support.

Tool Command Language (Tcl)

Since version 8.6, the Tool Command Language supports coroutines in the core language.[57]

Vala

Vala implements native support for coroutines. They are designed to be used with a Gtk Main Loop, but can be used alone if care is taken to ensure that the end callback will never have to be called before doing, at least, one yield.

Assembly languages

Machine-dependent assembly languages often provide direct methods for coroutine execution. For example, in MACRO-11, the assembly language of the PDP-11 family of minicomputers, the "classic" coroutine switch is effected by the instruction "JSR PC,@(SP)+", which jumps to the address popped from the stack and pushes the current (i.e that of the next) instruction address onto the stack. On VAXen (in VAX MACRO) the comparable instruction is "JSB @(SP)+". Even on a Motorola 6809 there is the instruction "JSR [,S++]"; note the "++", as 2 bytes (of address) are popped from the stack. This instruction is much used in the (standard) 'monitor' Assist 09.

See also

Further reading

External links

Notes and References

  1. Web site: 2016-02-11 . How the heck does async/await work in Python 3.5? . 2023-01-10 . Tall, Snarky Canadian . en-ca.
  2. Book: Marlin . Christopher . Coroutines: A Programming Methodology, a Language Design and an Implementation . 1980 . Springer . 3-540-10256-6.
  3. 10.1.1.58.4017. Ana Lucia de Moura. Roberto Ierusalimschy. Revisiting Coroutines. ACM Transactions on Programming Languages and Systems. 31. 2. 1–31. 2009. 10.1145/1462166.1462167. 9918449.
  4. Book: Anthony Ralston. Encyclopedia of computer science. 11 May 2013. 2000. Nature Pub. Group. 978-1-56159-248-7.
  5. See for example The Python Language Reference"https://docs.python.org/reference/expressions.html#yieldexpr 5.2.10. Yield expressions
  6. Web site: Coroutine: Type-safe coroutines using lightweight session types.
  7. Web site: Co-routines in Haskell.
  8. Web site: The Coroutines Module (coroutines.hhf). HLA Standard Library Manual.
  9. Web site: New in JavaScript 1.7. 2018-06-18. 2009-03-08. https://web.archive.org/web/20090308111529/https://developer.mozilla.org/en//docs//New_in_JavaScript_1.7.
  10. Web site: Julia Manual - Control Flow - Tasks (aka Coroutines).
  11. Web site: What's New in Kotlin 1.1.
  12. Web site: Lua 5.2 Reference Manual. www.lua.org.
  13. Web site: Python async/await Tutorial. December 17, 2015. Stack Abuse.
  14. Web site: 8. Compound statements — Python 3.8.0 documentation. docs.python.org.
  15. Web site: Gather and/or Coroutines. 2012-12-19.
  16. Book: Structured Programming. O.J.. Dahl. C.A.R.. Hoare. Academic Press. 1972. 978-0-12-200550-3. London, UK. 175–220. Hierarchical Program Structures.
  17. McCartney, J. "Rethinking the Computer Music Programming Language: SuperCollider". Computer Music Journal, 26(4):61-68. MIT Press, 2002.
  18. http://msdn.microsoft.com/msdnmag/issues/03/09/CoroutinesinNET/default.aspx Implementing Coroutines for .NET by Wrapping the Unmanaged Fiber API
  19. Web site: Coroutines in C – brainwagon. 5 March 2005.
  20. Portable Multithreading – The Signal Stack Trick For User-Space Thread Creation. PS. USENIX Annual Technical Conference. 18–23 June 2000. San Diego, USA. Ralf S. Engelschall.
  21. Web site: libco. code.byuu.org.
  22. Web site: getcontext(3) - Linux manual page. man7.org.
  23. http://swtch.com/libtask/ - Russ Cox's libtask coroutine library for FreeBSD, Linux, Mac OS X, and SunOS
  24. http://xmailserver.org/libpcl.html Portable Coroutine Library
  25. http://www.goron.de/~froese/coro/ - Edgar Toernig's coro library for x86, Linux & FreeBSD
  26. https://github.com/halayli/lthread - lthread is a multicore/multithread coroutine library written in C
  27. Web site: libcoroutine: A portable coroutine implementation. 2013-09-06. 2019-11-12. https://web.archive.org/web/20191112231845/http://dekorte.com/projects/opensource/libcoroutine/. for FreeBSD, Linux, OS X PPC and x86, SunOS, Symbian and others
  28. Web site: libconcurrency - A scalable concurrency library for C. a simple C library for portable stack-switching coroutines
  29. Web site: libcoro: C-library that implements coroutines (cooperative multitasking) in a portable fashion. used as the basis for the Coro perl module.
  30. Web site: RIBS (Robust Infrastructure for Backend Systems) version 2: aolarchive/ribs2. August 13, 2019. GitHub.
  31. Web site: libdill. libdill.org. 2019-10-21. 2019-12-02. https://web.archive.org/web/20191202174632/http://libdill.org/.
  32. Web site: A blazing fast and lightweight C asymmetric coroutine library ⛅⛅: hnes/libaco. October 21, 2019. GitHub.
  33. Web site: Coroutines in C. Simon Tatham. 2000.
  34. Web site: Stackless coroutine implementation in C and C++: jsseldenthuis/coroutine. March 18, 2019. GitHub.
  35. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4680.pdf - Technical specification for coroutines
  36. https://en.cppreference.com/w/cpp/compiler_support#cpp20 - Current compiler support for standard coroutines
  37. http://mozy.com/blog/announcements/open-source-and-mozy-the-debut-of-mozy-code/ - Open Source and Mozy: The Debut of Mozy Code
  38. https://twitter.com/eric01/status/867473461836263424 - EricWF: Coroutines are now in Clang Trunk! Working on the Libc++ implementation now.
  39. Web site: Wagner . Bill . Iterators . C# documentation . . 11 November 2021 . Microsoft Learn.
  40. Web site: Wagner . Bill . The history of C# . C# documentation . . 13 February 2023 . Microsoft Learn . C# version 2.0.
  41. Web site: Goroutines - Effective Go . 2022-11-28 . go.dev . en.
  42. Web site: Go statements - The Go Specification . 2022-11-28 . go.dev . en.
  43. Web site: Goroutines - A Tour of Go . 2022-11-28 . go.dev.
  44. Web site: Frequently Asked Questions (FAQ) - The Go Programming Language. go.dev.
  45. Web site: JVM Continuations. Lukas Stadler. JVM Language Summit. 2009.
  46. Web site: Holy crap: JVM has coroutine/continuation/fiber etc.. Remi Forax. https://web.archive.org/web/20150319052055/http://weblogs.java.net/blog/forax/archive/2009/11/19/holy-crap-jvm-has-coroutinecontinuationfiber-etc. 19 March 2015. 19 November 2009.
  47. Web site: Lua version history . Lua.org .
  48. Web site: de Moura . Ana Lúcia . Rodriguez . Noemi . Ierusalimschy . Roberto . Coroutines in Lua . Lua.org . 24 April 2023.
  49. de Moura . Ana Lúcia . Rodriguez . Noemi . Ierusalimschy . Roberto . Coroutines in Lua . Journal of Universal Computer Science . 2004 . 10 . 7 . 901--924.
  50. http://www.mono-project.com/Continuations Mono Continuations
  51. http://blogs.msdn.com/cbrumme/archive/2004/02/21/77595.aspx, Chris Brumme, cbrumme's WebLog
  52. Web site: kexugit. Fiber mode is gone.... 2021-06-08. docs.microsoft.com. 15 September 2005 . en-us.
  53. Web site: The threads library .
  54. Web site: RFC #31 .
  55. Web site: What's New in Python 3.7 . 10 September 2021.
  56. Web site: semi-coroutines . October 24, 2007 . en . https://web.archive.org/web/20071024123936/http://www.ruby-forum.com/topic/126011 .
  57. Web site: coroutine manual page - Tcl Built-In Commands . Tcl.tk . 2016-06-27.