Nothing Special   »   [go: up one dir, main page]

skip to main content
research-article
Open access

Effect Handlers for C via Coroutines

Published: 08 October 2024 Publication History

Abstract

Effect handlers provide a structured means for implementing user-defined, composable, and customisable computational effects, ranging from exceptions to generators to lightweight threads. We introduce libseff, a novel effect handlers library for C, based on coroutines. Whereas prior effect handler libraries for C are intended primarily as compilation targets, libseff is intended to be used directly from C programs. As such, the design of libseff parts ways from traditional effect handler implementations, both by using mutable coroutines as the main representation of pending computations, and by avoiding closures as handlers by way of reified effects. We show that the performance of libseff is competitive across a range of platforms and benchmarks.

Supplemental Material

PDF File - Effect Handlers for C via Coroutines - Extended
Extended version of Effect Handlers for C via Coroutines

References

[1]
Mario Alvarez-Picallo, Teodoro Freund, Dan R. Ghica, and Sam Lindley. 2024. Effect Handlers for C via Coroutines - Artifact. https://doi.org/10.5281/zenodo.13485897
[2]
Mario Alvarez-Picallo, Teodoro Freund, Dan R. Ghica, and Sam Lindley. 2024. Effect Handlers for C via Coroutines - Artifact. https://doi.org/10.5281/zenodo.13619817
[3]
Lewis Baker. 2024. cppcoro. https://github.com/lewissbaker/cppcoro
[4]
Effect Handlers Community. 2024. Effect handlers benchmarks suite. https://github.com/effect-handlers/effect-handlers-bench
[5]
Lukas Convent, Sam Lindley, Conor McBride, and Craig McLaughlin. 2020. Doo bee doo bee doo. J. Funct. Program., 30 (2020), e9. https://doi.org/10.1017/S0956796820000039
[6]
Alex Crichton. 2021. Wasmtime Fiber API. https://docs.wasmtime.dev/api/wasmtime_fiber/index.html Accessed 2023-11-15
[7]
Ana Lúcia de Moura and Roberto Ierusalimschy. 2009. Revisiting coroutines. ACM Trans. Program. Lang. Syst., 31, 2 (2009), 6:1–6:31. https://doi.org/10.1145/1462166.1462167
[8]
KC Sivaramakrishnan Deepali Ande, Sudha Parimala. 2023. Effectively Composing Concurrency Libraries. Draft. https://kcsrk.info/papers/composable_concurrency.pdf
[9]
Stephen Dolan, Spiros Eliopoulos, Daniel Hillerström, Anil Madhavapeddy, KC Sivaramakrishnan, and Leo White. 2017. Concurrent System Programming with Effect Handlers. In TFP (Lecture Notes in Computer Science, Vol. 10788). Springer, 98–117. https://doi.org/10.1007/978-3-319-89719-6_6
[10]
Matthias Felleisen and Daniel P. Friedman. 1986. Control Operators, the SECD-machine, and the λ -Calculus. In Formal Description of Programming Concepts III. Elsevier, 193–217.
[11]
Dan R. Ghica, Sam Lindley, Marcos Maroñas Bravo, and Maciej Piróg. 2022. High-level effect handlers in C++. Proc. ACM Program. Lang., 6, OOPSLA2 (2022), 1639–1667. https://doi.org/10.1145/3563445
[12]
Daniel Hillerström, Sam Lindley, and Robert Atkey. 2020. Effect handlers via generalised continuations. J. Funct. Program., 30 (2020), e5. https://doi.org/10.1017/S0956796820000040
[13]
Daniel Hillerström. 2021. Foundations for Programming and Implementing Effect Handlers. Ph. D. Dissertation. School of Informatics, The University of Edinburgh, Scotland, UK.
[14]
Christopher Jonathan, Umar Farooq Minhas, James Hunter, Justin J. Levandoski, and Gor V. Nishanov. 2018. Exploiting Coroutines to Attack the "Killer Nanoseconds". Proc. VLDB Endow., 11, 11 (2018), 1702–1714. https://doi.org/10.14778/3236187.3236216
[15]
Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in action. In ICFP. ACM, 145–158. https://doi.org/10.1145/2500365.2500590
[16]
Georgios Karachalias, Filip Koprivec, Matija Pretnar, and Tom Schrijvers. 2021. Efficient compilation of algebraic effect handlers. Proc. ACM Program. Lang., 5, OOPSLA (2021), 1–28. https://doi.org/10.1145/3485479
[17]
Satoru Kawahara and Yukiyoshi Kameyama. 2020. One-Shot Algebraic Effects as Coroutines. In TFP (Lecture Notes in Computer Science, Vol. 12222). Springer, 159–179. https://doi.org/10.1007/978-3-030-57761-2_8
[18]
Oleg Kiselyov and Hiromi Ishii. 2015. Freer monads, more extensible effects. In Haskell. ACM, 94–105. https://doi.org/10.1145/2804302.2804319
[19]
Daan Leijen. 2017. Implementing Algebraic Effects in C — "Monads for Free in C". In APLAS (Lecture Notes in Computer Science, Vol. 10695). Springer, 339–363. https://doi.org/10.1007/978-3-319-71237-6_17
[20]
Daan Leijen. 2019. libhandler. https://github.com/koka-lang/libhandler
[21]
Daan Leijen and KC Sivamarakrishnan. 2023. libmprompt and libmpeff. https://github.com/koka-lang/libmprompt
[22]
Paul Blain Levy, John Power, and Hayo Thielecke. 2003. Modelling environments in call-by-value programming languages. Inf. Comput., 185, 2 (2003), 182–210. https://doi.org/10.1016/S0890-5401(03)00088-9
[23]
Zhiyao Ma and Lin Zhong. 2023. Bringing Segmented Stacks to Embedded Systems. In Proceedings of the 24th International Workshop on Mobile Computing Systems and Applications, HotMobile 2023, Newport Beach, California, February 22-23, 2023. ACM, 117–123. https://doi.org/10.1145/3572864.3580344
[24]
Daniel Morsing. 2014. How Stacks are Handled in Go. https://blog.cloudflare.com/how-stacks-are-handled-in-go/
[25]
Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase, and Shigeo Mitsunari. 2024. PicoHTTPParser. https://github.com/h2o/picohttpparser
[26]
Luna Phipps-Costin, Andreas Rossberg, Arjun Guha, Daan Leijen, Daniel Hillerström, KC Sivaramakrishnan, Matija Pretnar, and Sam Lindley. 2023. Continuing WebAssembly with Effect Handlers. Proc. ACM Program. Lang., 7, OOPSLA2 (2023), 460–485. https://doi.org/10.1145/3622814
[27]
Gordon D. Plotkin and Matija Pretnar. 2013. Handling Algebraic Effects. Logical Methods in Computer Science, 9, 4 (2013), https://doi.org/10.2168/LMCS-9(4:23)2013
[28]
Aleksandar Prokopec and Fengyun Liu. 2018. Theory and Practice of Coroutines with Snapshots. https://doi.org/10.4230/LIPIcs.ECOOP.2018.3
[29]
Philipp Schuster, Jonathan Immanuel Brachthäuser, and Klaus Ostermann. 2020. Compiling effect handlers in capability-passing style. Proc. ACM Program. Lang., 4, ICFP (2020), 93:1–93:28. https://doi.org/10.1145/3408975
[30]
KC Sivaramakrishnan, Stephen Dolan, Leo White, Tom Kelly, Sadiq Jaffer, and Anil Madhavapeddy. 2021. Retrofitting effect handlers onto OCaml. In PLDI. ACM, 206–221. https://doi.org/10.1145/3453483.3454039
[31]
Martin Sustrik. 2021. libmill. https://github.com/sustrik/libmill
[32]
Boost team. 2021. Boost.coroutine library. https://www.boost.org/doc/libs/1_83_0/libs/coroutine/doc/html/index.html
[33]
Boost team. 2024. Boost.context library. https://github.com/boostorg/context
[34]
Eio team. 2024. Eio. https://github.com/ocaml-multicore/eio
[35]
Mirage team. 2024. Mirage. https://github.com/mirage/ocaml-cohttp
[36]
OCaml Multicore team. 2021. Multicore OCaml HTTP benchmarks. https://github.com/ocaml-multicore/retro-httpaf-bench
[37]
TechEmpower. 2023. Web Framework Benchmarks. https://www.techempower.com/benchmarks
[38]
Tencent. 2020. libco. https://github.com/Tencent/libco
[39]
wrk2 team. 2024. wrk2. https://github.com/giltene/wrk2
[40]
Nicolas Wu and Tom Schrijvers. 2015. Fusion for Free - Efficient Algebraic Effect Handlers. In MPC (Lecture Notes in Computer Science, Vol. 9129). Springer, 302–322.
[41]
Ningning Xie and Daan Leijen. 2021. Generalized evidence passing for effect handlers: efficient compilation of effect handlers to C. Proc. ACM Program. Lang., 5, ICFP (2021), 1–30. https://doi.org/10.1145/3473576

Recommendations

Comments

Please enable JavaScript to view thecomments powered by Disqus.

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 8, Issue OOPSLA2
October 2024
2691 pages
EISSN:2475-1421
DOI:10.1145/3554319
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution-NoDerivs International 4.0 License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 08 October 2024
Published in PACMPL Volume 8, Issue OOPSLA2

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. C
  2. Coroutines
  3. Effect Handlers

Qualifiers

  • Research-article

Funding Sources

  • UKRI

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • 0
    Total Citations
  • 251
    Total Downloads
  • Downloads (Last 12 months)251
  • Downloads (Last 6 weeks)56
Reflects downloads up to 16 Feb 2025

Other Metrics

Citations

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Login options

Full Access

Figures

Tables

Media

Share

Share

Share this Publication link

Share on social media