skip to main content
10.1145/3486607.3486751acmconferencesArticle/Chapter ViewAbstractPublication PagessplashConference Proceedingsconference-collections
research-article

Kotlin coroutines: design and implementation

Published: 17 October 2021 Publication History

Abstract

Asynchronous programming is having its “renaissance” moment in recent years. Created in the 1980s, it was in use for quite some time, but with the advent of multi-core processors, it has been sidestepped by multi-threaded programming, which was (for a long time) the de facto standard of performing concurrent computations. However, since the 2000s, more and more programming languages have begun to include the support for asynchronous programming, some built around asynchronicity from the start, others including it later in their evolution.
In this paper, we explore the design and implementation of asynchronous programming in Kotlin, a multiplatform programming language from JetBrains, which uses coroutines for asynchronicity. Kotlin provides a compact built-in API for coroutine support, thus giving a lot of implementation freedom to the developer; this flexibility allows to transparently support different flavours of asynchronous programming within the same language.
We overview existing approaches to asynchronous programming, zoom in and talk about coroutines in detail, and describe how they are used in Kotlin as the basis for asynchronous computations. Along the way, we show the flexibility of Kotlin coroutines, highlight several existing problems with asynchronicity, how they are fixed or worked-around in Kotlin, and also mention future directions asynchronous programming might explore.

Supplementary Material

Auxiliary Presentation Video (onward21papers-p11-p-video.mp4)
Asynchronous programming is having its “renaissance” moment in recent years. Since the 2000s, more and more programming languages have begun to include the support for asynchronous programming, some built around asynchronicity from the start, others including it later in their evolution. In this paper, we explore the design and implementation of asynchronous programming in Kotlin,which uses coroutines for asynchronicity. Kotlin provides a compact built-in API for coroutine support, giving a lot of implementation freedom to the developer; this flexibility allows to transparently support different flavours of asynchronous programming within the same language. We overview existing approaches to asynchronous programming, talk about coroutines in detail, and describe how they are used in Kotlin as the basis for asynchronous computations.

References

[1]
2018. Project Loom: Fibers and Continuations for the Java Virtual Machine. https://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html [Online; accessed 15/04/2021].
[2]
2018. Structured Concurrency. https://elizarov.medium.com/structured-concurrency-anniversary-f2cc748b2401 [Online; accessed 15/08/2021].
[3]
2019. Stackless-Python 3.7.5 documentation. https://stackless.readthedocs.io/en/v3.7.5-slp/stackless-python.html [Online; accessed 15/08/2021].
[4]
2020. Erlang Reference Manual. http://erlang.org/doc/reference_manual/users_guide.html [Online; accessed 15/08/2021].
[5]
2020. The Go Programming Language Specification. https://golang.org/ref/spec [Online; accessed 15/08/2021].
[6]
2020. Kotlin Coroutines. https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md [Online; accessed 15/04/2021].
[7]
2020. State of Loom. http://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part1.html [Online; accessed 15/08/2021].
[8]
Marat Akhin and Mikhail Belyaev. 2020. Kotlin language specification (version 1.4-rfc+0.3). https://kotlinlang.org/spec [Online; accessed 15/04/2021].
[9]
Andrew W. Appel. 1991. Compiling with Continuations. Cambridge University Press. isbn:9780521416955 lccn:91026651
[10]
Henry C. Baker and Carl Hewitt. 1977. The Incremental Garbage Collection of Processes. SIGPLAN Not., 12, 8 (1977), Aug., 55–59. issn:0362-1340 https://doi.org/10.1145/872734.806932
[11]
Joe Bartel. 2011. Non-preemptive Multitasking. Comput. J., 30 (2011), 37–39.
[12]
Alan Bateman. 2020. Structured Concurrency. https://wiki.openjdk.java.net/display/loom/Structured+Concurrency [Online; accessed 15/08/2021].
[13]
Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and Alan Edelman. 2012. Julia: a Fast Dynamic Language for Technical Computing. arXiv preprint arXiv:1209.5145.
[14]
Graham M. Birtwistle. 1973. Simula Begin. Petrocelli. isbn:9789144062112
[15]
Corrado Böhm and Giuseppe Jacopini. 1966. Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules. Commun. ACM, 9, 5 (1966), May, 366–371. issn:0001-0782 https://doi.org/10.1145/355592.365646
[16]
Carl Bruggeman, Oscar Waddell, and R. Kent Dybvig. 1996. Representing Control in the Presence of One-Shot Continuations. In Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation. Association for Computing Machinery, 99–107. isbn:0897917952 https://doi.org/10.1145/231379.231395
[17]
Koen Claessen. 1999. A Poor Man’s Concurrency Monad. J. Funct. Program., 9, 3 (1999), May, 313–323. issn:0956-7968 https://doi.org/10.1017/S0956796899003342
[18]
Youyou Cong, Leo Osvald, Grégory M. Essertel, and Tiark Rompf. 2019. Compiling with Continuations, or without? Whatever. Proc. ACM Program. Lang., 3, ICFP (2019), Article 79, July, 28 pages. https://doi.org/10.1145/3341643
[19]
Melvin E. Conway. 1963. Design of a Separable Transition-Diagram Compiler. Commun. ACM, 6, 7 (1963), July, 396–408. issn:0001-0782 https://doi.org/10.1145/366663.366704
[20]
Ole-Johan Dahl and C. A. R. Hoare. 1972. Chapter III: Hierarchical Program Structures. Academic Press Ltd., GBR. 175–220. isbn:0122005503
[21]
Ana Lúcia De Moura, Noemi Rodriguez, and Roberto Ierusalimschy. 2004. Coroutines in Lua. Journal of Universal Computer Science, 10, 7 (2004), 910–925.
[22]
ECMA International. 2017. Standard ECMA-334 — C# Language Specification (5 ed.). https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-334.pdf
[23]
David Flanagan and Yukihiro Matsumoto. 2008. The Ruby Programming Language: Everything You Need to Know. "O’Reilly Media, Inc.".
[24]
Daniel P. Friedman and David Stephen Wise. 1976. The Impact of Applicative Programming on Multiprocessing. Indiana University, Computer Science Department.
[25]
Keheliya Gallaba, Ali Mesbah, and Ivan Beschastnikh. 2015. Don’t Call Us, We’ll Call You: Characterizing Callbacks in JavaScript. In 2015 ACM/IEEE International Symposium on Empirical Software Engineering and Measurement. 1–10. https://doi.org/10.1109/ESEM.2015.7321196
[26]
Colin S. Gordon, Werner Dietl, Michael D. Ernst, and Dan Grossman. 2013. Java UI: Effects for Controlling UI Object Access. In European Conference on Object-Oriented Programming. 179–204.
[27]
Christopher T. Haynes, Daniel P. Friedman, and Mitchell Wand. 1984. Continuations and Coroutines. In Proceedings of the 1984 ACM Symposium on LISP and Functional Programming. Association for Computing Machinery, 293–298. isbn:0897911423 https://doi.org/10.1145/800055.802046
[28]
C. A. R. Hoare. 1978. Communicating Sequential Processes. Commun. ACM, 21, 8 (1978), Aug., 666–677. issn:0001-0782 https://doi.org/10.1145/359576.359585
[29]
Roshan P. James and Amr Sabry. 2011. Yield: Mainstream Delimited Continuations. In First International Workshop on the Theory and Practice of Delimited Continuations. 95, 96.
[30]
Andrew Josey. 2004. The Single UNIX Specification Version 3. Open Group.
[31]
Kennedy Kambona, Elisa Gonzalez Boix, and Wolfgang De Meuter. 2013. An Evaluation of Reactive Programming and Promises for Structuring Collaborative Web Applications. In Proceedings of the 7th Workshop on Dynamic Languages and Applications. 1–9.
[32]
INMOS Limited and INMOS International. 1988. OCCAM 2 Reference Manual. Prentice Hall. isbn:9780136293125 lccn:87032839
[33]
Barbara Liskov and Liuba Shrira. 1988. Promises: Linguistic Support for Efficient Asynchronous Procedure Calls in Distributed Systems. ACM SIGPLAN Notices, 23, 7 (1988), 260–267.
[34]
Christopher D. Marlin. 1980. Coroutines: a Programming Methodology, a Language Design and an Implementation. Springer. isbn:9783540102564 lccn:80024852
[35]
Tommi Mikkonen and Antero Taivalsaari. 2007. Web Applications: Spaghetti Code for the 21st Century.
[36]
Robin Milner, Mads Tofte, Robert Harper, and David MacQueen. 1997. The Definition of Standard ML: Revised. MIT Press. isbn:9780262631815 lccn:97000059
[37]
Ana Lúcia De Moura and Roberto Ierusalimschy. 2009. Revisiting Coroutines. ACM Trans. Program. Lang. Syst., 31, 2 (2009), Article 6, Feb., 31 pages. issn:0164-0925 https://doi.org/10.1145/1462166.1462167
[38]
Gregory M Papadopoulos and Kenneth R Traub. 1991. Multithreading: a Revisionist View of Dataflow Architectures. In Proceedings of the 18th annual International Symposium on Computer Architecture. 342–351.
[39]
Tomas Petricek, Dominic Orchard, and Alan Mycroft. 2014. Coeffects: a Calculus of Context-Dependent Computation. ACM SIGPLAN Notices, 49, 9 (2014), 123–135.
[40]
Simon L. Peyton Jones and Philip Wadler. 1993. Imperative Functional Programming. In Proceedings of the 20th ACM SIGPLAN-SIGACT symposium on Principles of Programming Languages. 71–84.
[41]
Laure Philips, Joeri De Koster, Wolfgang De Meuter, and Coen De Roover. 2016. Dependence-Driven Delimited CPS Transformation for JavaScript. In Proceedings of the 2016 ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences. Association for Computing Machinery, 59–69. isbn:9781450344463 https://doi.org/10.1145/2993236.2993243
[42]
Aleksandar Prokopec and Fengyun Liu. 2018. Theory and Practice of Coroutines with Snapshots. In 32nd European Conference on Object-Oriented Programming, Todd D. Millstein (Ed.) (LIPIcs, Vol. 109). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 3:1–3:32. https://doi.org/10.4230/LIPIcs.ECOOP.2018.3
[43]
John H. Reppy. 1991. CML: a Higher-Order Concurrent Language. In Proceedings of the SIGPLAN 1991 Conference on Programming Language Design and Implementation. ACM, 293–305.
[44]
John C. Reynolds. 1993. The Discoveries of Continuations. Lisp Symb. Comput., 6, 3–4 (1993), Nov., 233–248. issn:0892-4635 https://doi.org/10.1007/BF01019459
[45]
Tiark Rompf, Ingo Maier, and Martin Odersky. 2009. Implementing First-Class Polymorphic Delimited Continuations by a Type-Directed Selective CPS-Transform. In Proceedings of the 14th ACM SIGPLAN International Conference on Functional Programming. Association for Computing Machinery, 317–328. isbn:9781605583327 https://doi.org/10.1145/1596550.1596596
[46]
Casey Rosenthal and Nora Jones. 2020. Chaos Engineering. 1005, O’Reilly Media, Incorporated.
[47]
Jaebaek Seo, Daehyeok Kim, Donghyun Cho, Insik Shin, and Taesoo Kim. 2016. FLEXDROID: Enforcing In-App Privilege Separation in Android. In NDSS.
[48]
Richard Smith. 2020. Working draft, standard for programming language C++, document N4868. ISO/IEC JTC1/SC22/WG21, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4868.pdf
[49]
Michael Sperber, R Kent Dybvig, Matthew Flatt, Anton Van Straaten, Robby Findler, and Jacob Matthews. 2009. Revised^6 Report on the Algorithmic Language Scheme. Journal of Functional Programming, 19, S1 (2009), 1–301.
[50]
Martin Sústrik. 2018. Structured Concurrency. https://250bpm.com/blog:71/ [Online; accessed 15/08/2021].
[51]
Don Syme. 2012. The 2.0 Language Specification. https://fsharp.org/specs/language-spec/2.0/FSharpSpec-2.0-April-2012.pdf
[52]
Don Syme, Tomas Petricek, and Dmitry Lomov. 2011. The F# Asynchronous Programming Model. In Practical Aspects of Declarative Languages, Ricardo Rocha and John Launchbury (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 175–189. isbn:978-3-642-18378-2
[53]
Sergey Tepliakov. 2018. Extending the Async Methods in. https://devblogs.microsoft.com/premier-developer/extending-the-async-methods-in-c/ [Online; accessed 15/08/2021].
[54]
E. Dean Tribble, Mark S. Miller, Norm Hardy, and David Krieger. 1995. Joule: Distributed Application Foundations. ADd03.
[55]
Tengfei Tu, Xiaoyu Liu, Linhai Song, and Yiying Zhang. 2019. Understanding Real-World Concurrency Bugs in Go. In Proceedings of the Twenty-Fourth International Conference on Architectural Support for Programming Languages and Operating Systems. Association for Computing Machinery, 865–878. isbn:9781450362405 https://doi.org/10.1145/3297858.3304069
[56]
Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft. 2014. Java 8 in Action. Manning publications.
[57]
Ilmir Usmanov. 2021. The Ultimate Breakdown of Kotlin Coroutines. https://ilmirus.blogspot.com/2021/01/the-ultimate-breakdown-of-kotlin.html [Online; accessed 15/04/2021].
[58]
Adriaan van Wijngaarden. 1966. Recursive Definition of Syntax and Semantics. North Holland Publishing Company.
[59]
Philip Wadler. 1995. Monads for Functional Programming. In International School on Advanced Functional Programming. 24–52.
[60]
Niklaus Wirth. 1956. Programming in Modula-2. Ju Lin. lccn:84026838

Cited By

View all

Recommendations

Comments

Information & Contributors

Information

Published In

cover image ACM Conferences
Onward! 2021: Proceedings of the 2021 ACM SIGPLAN International Symposium on New Ideas, New Paradigms, and Reflections on Programming and Software
October 2021
148 pages
ISBN:9781450391108
DOI:10.1145/3486607
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected].

Sponsors

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 17 October 2021

Permissions

Request permissions for this article.

Check for updates

Author Tags

  1. Kotlin
  2. asynchronous programming
  3. continuations
  4. coroutines
  5. language design

Qualifiers

  • Research-article

Conference

SPLASH '21
Sponsor:
SPLASH '21: Software for Humanity
October 20 - 22, 2021
IL, Chicago, USA

Acceptance Rates

Overall Acceptance Rate 40 of 105 submissions, 38%

Upcoming Conference

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)182
  • Downloads (Last 6 weeks)11
Reflects downloads up to 04 Oct 2024

Other Metrics

Citations

Cited By

View all
  • (2024)Real-Time Energy-Efficient Sensor Libraries for Wearable DevicesIEEE Access10.1109/ACCESS.2024.343004912(126006-126018)Online publication date: 2024
  • (2023)On Composing Asynchronous OperationsStudia Universitatis Babeș-Bolyai Informatica10.24193/subbi.2023.2.0368:2(41-52)Online publication date: 22-Dec-2023
  • (2023)Coroutines Comunications. Design and Implementation Issues in C++20Studia Universitatis Babeș-Bolyai Informatica10.24193/subbi.2022.2.0367:2(37-48)Online publication date: 2-Jun-2023
  • (2023)SensorLib: an Energy-efficient Sensor-collection Library for Wear OSProceedings of the 4th European Symposium on Software Engineering10.1145/3651640.3651641(83-88)Online publication date: 1-Dec-2023
  • (2023)Typing Composable Coroutines2023 12th International Conference on Computer Technologies and Development (TechDev)10.1109/TechDev61156.2023.00011(16-23)Online publication date: 14-Oct-2023
  • (2022)Comparison of Structured Concurrency Constructs in Java and Kotlin – Virtual Threads and Coroutines2022 45th Jubilee International Convention on Information, Communication and Electronic Technology (MIPRO)10.23919/MIPRO55190.2022.9803765(1466-1471)Online publication date: 23-May-2022
  • (2022)CAAT: consistency as a theoryProceedings of the ACM on Programming Languages10.1145/35632926:OOPSLA2(114-144)Online publication date: 31-Oct-2022

View Options

Get Access

Login options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media