Event-driven Programming

In event-drivent programming, the flow of a program is determined by events triggered through a defined interaction (e.g., user interaction, exceeding threshold limit, etc. ).
Usually, a main loop listens for events, and then triggers a callback function when an event is detected.

Overall, Legato components are written using an event-driven model. Callback functions
are attached to specific events, and when events occur, the thread’s event loop triggers the
appropriate callbacks. Legato components can also start other threads that run their own Legato event loops.

Every component implements an initialization function called at process start-up by the
main thread. That function does whatever initialization is required for that component
(e.g., initializes data structures, registers callbacks, starts threads, etc.) and returns.
After all initialization functions have run to completion and returned, the main thread
enters the Legato event loop and begins reacting to events and calling registered callbacks.

This allows multiple components to be deployed to the same process and even share a single thread within that process, without having to know anything about each other. This reduces the risk of multi-threaded race conditions and saves stack memory space.

Beware one drawback is that if a component blocks a shared thread, it blocks every component sharing that thread.

Sometimes, the @a judicious use of threads can considerably simplify a program, so
multi-threading is also supported by Legato along with the usual thread synchronization constructs (mutexes, semaphores, etc.).

– Legato-AF

Leave a Comment

Your email address will not be published. Required fields are marked *