Unified processing of events and coroutines in embedded program
- Authors: Minin P.V.1
-
Affiliations:
- DORS R&D LLC
- Issue: No 5 (2024)
- Pages: 14-30
- Section: PARALLEL AND DISTRIBUTED SOFTWARE
- URL: https://gynecology.orscience.ru/0132-3474/article/view/683329
- DOI: https://doi.org/10.31857/S0132347424050027
- EDN: https://elibrary.ru/OMADOW
- ID: 683329
Cite item
Abstract
A new architectural approach to real-time embedded programming is described. A bare-metal program is written in C/C++ and combines event-driven technique with concurrency based on co-routines. Events are processed by soft real-time core at the priority level of software generated interrupts. Event is first posted to input queue of the core and then processed by invocation of its event handler. A special case of event is co-routine, its resumable function being a co-routine event handler. The co-routine that either yielded or needs to be resumed is queued for event processing once again. As a result, it is processed multiple times until execution of resumable function comes to the end of its operator sequence. Different levels of processing priority may be assigned to an event. Soft real-time core could be further expanded to run on symmetrical multiprocessor hardware. A combination of co-routines and basic events could easily be used in fork/join model. Concurrency constructs resemble those of Go and occam languages. Virtually all classic types of synchronization primitives could be implemented. The new approach was implemented for various ARM and Blackfin processors in C++ language as portable DORSECC library. This library was further used to program real-time embedded systems for mass-produced banknote sorting machines. One type of systems was used to recognize and validate banknote images by the method of cascade of one-class classifiers. The other system worked as a motion controller and used finite automata to control sensors and actuators. The total number of systems in operation is currently over 20000. The event and co-routine core in these systems provides average event processing time in the range of dozens of microseconds with sub-microsecond overhead time per each event.
Full Text

About the authors
P. V. Minin
DORS R&D LLC
Author for correspondence.
Email: p.minin@dors.ru
Russian Federation, Moscow
References
- Knuth D. The Art of Computer Programming: Fundamental Algorithms. Addison-Wesley, Reading, Massachusetts, the USA. 1997. P. 193–200.
- Dunkels A., Schmidt O., Voigt T., Ali M. Protothreads: Simplifying Event-Driven Programming of MemoryConstrained Embedded Systems. Proceedings of the Fourth ACM Conference on Embedded Networked Sensor Systems (SenSys 2006), Boulder, Colorado, the USA, November 2006.
- Libtask: a Coroutine Library for C and Unix. https://swtch.com/libtask/. Accessed 21.03.2023.
- The Boost C++ Libraries. Chapter 51: Boost.Coroutine. https://theboostcpplibraries.com/boost.coroutine. Accessed 21.03.2023.
- The Boost C++ Libraries. Chapter 32: Boost.Asio. https://theboostcpplibraries.com/boost.asio. Accessed 21.03.2023.
- CO2 Coroutine. https://github.com/jamboree/co2. Accessed 21.03.2023.
- Goodspeed N. Stackful Coroutines and Stackless Resumable Functions. The C++ Standards Committee – ISOCPP Document N4232. December 13, 2014. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4232.pdf.
- Belson B., Holdsworth J., Xiang W., Philippa B. A Survey of Asynchronous Programming Using Coroutines in the Internet of Things and Embedded Systems. ACM Transactions on Embedded Computer Systems. April 2019. V. 18. № 3. Article 21. https://doi.org/10.1145/3319618.
- Moskała M. Kotlin Coroutines: Deep Dive. Kt. Academy, Warsaw, Poland. 2022.
- FreeRTOS Kernel. Co-routines. https://www.freertos.org/croutine.html. Accessed 01.11.2023.
- Shalyto, A.A. Paradigm for automata based programming, in Nauchno-tekhnicheskii vestnik ITMO (ScientificTechnical Herald of ITMO Univ.). Issue 53: Avtomatnoe programmirovanie (Automata Based Programming), St. Petersburg, 2008.
- Ousterhout J. Why Threads are a Bad Idea (for most purposes). January 1996. USENIX Winter Technical Conference, San Diego, California, the USA.
- Lee E. The Problem with Threads. Computer. 2006. V. 39. P. 33–42.
- Samek M. Who Moved My State? Dr. Dobb’s (online). April 1, 2003. https://drdobbs.com/who-moved-my-state/184401643
- Samek M. Practical Statecharts in C/C++: Quantum Programming for Embedded Systems. CMP Books, San-Francisco, the USA. 2002.
- Gamma E., Helm R., Johnson R., Vlissides J. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, Reading, Massachusetts, the USA. 1995. P. 293.
- Kernel-Mode Driver Architecture Design Guide: Introduction to DPC Objects. https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/introduction-to-dpc-objects. Accessed 21.03.2023.
- Deferred Work – The Linux Kernel Documentation. https://linux-kernel-labs.github.io/refs/heads/master/labs/deferred_work.html. Accessed 21.03.2023.
- Nicholl R.A. A Specification of Modula-2 Process (Coroutine) Management. Journal of Pascal, Ada & Modula-2. 1988. V. 7. № 5. P. 16–22.
- Tatham S. Coroutines in C (online). https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html. 2000.
- Dunkels A., Grönvall B., Voigt T. Contiki – A Lightweight and Flexible Operating System for Tiny Networked Sensors. Proceedings of the Conference on Local Computer Networks. 2004. P. 455–462. https://doi.org/10.1109/LCN.2004.38
- Barnett D., Massa A. Inside the uIP Stack. Dr Dobbs Journal (online). February 1, 2005. https://www.drdobbs.com/inside-the-uip-stack/184405971
- Dunkels A. Programming Memory-Constrained Networked Embedded Systems. Swedish Institute of Computer Science Doctoral Thesis (online). SICS Dissertation Series 47. February 2007. http://www.diva-potal.org/smash/get/diva2:1041306/FULLTEXT01.pdf
- Perlis A. Epigrams on programming. ACM SIGPLAN Notices. September 1982. V. 17. № 9. P. 7–13. https://doi.org/10.1145/947955.1083808
- McCool M., Reinders J., Robison A. Structured Parallel Programming: Patterns for Efficient Computation. Morgan Kaufmann, Burlington, Massachusetts, the USA. 2012. P. 209–252.
- Cox-Buday K. Concurrency in Go. O’Reily Media, Sebastopol, California, the USA. 2017.
- Roscoe A., Hoare C.A.R. The Laws of Occam Programming. Theoretical Computer Science. 1988. V. 60. P. 177–229. https://doi.org/10.1016/0304-3975(88)90049-7
- Hoare C.A.R. Communicating sequential processes. Prentice-Hall, Englewood Cliffs, New Jersey, the USA. 1985.
- Lampson B.W., Redell D.D. Experience with processes and monitors in Mesa. Comm. of the ACM. 1980. V. 23. № 2. P. 105–117.
- Love R. CPU Affinity. Linux Journal (online). July 1, 2003. https://www.linuxjournal.com/article/6799
- Gujarati A., Cerqueira F., Brandenburg B. Multiprocessor real-time scheduling with arbitrary processor affinities: from practice to theory. Real-Time Systems. 2015. V. 51. P. 440–483. https://doi.org/10.1007/s11241-014-9205-9
- Hoberock J., Garland M., Kohloff C. et al. A Unified Executors Proposal for C++. The C++ Standards Committee – ISOCPP Document P0443R14. September 15, 2020. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0443r14.html
- Bell J. Threaded code. Communications of the ACM. 1973. V. 16. № 6. P. 370–372. https://doi.org/10.1145/362248.362270.
- Rather E., Colburn D., Moore C. The evolution of Forth. In: History of programming languages – II. ACM Other Books. 1 January 1996. P. 625–670. https://doi.org/10.1145/234286.1057832 ISBN 9780201895025
- Reid G. Thinking in PostScript. Addison-Wesley, Reading, Massachusets, the USA. 1990. P. 105–118.
Supplementary files
