Skip to content

Co‐routine Threading

Mikayla edited this page Dec 23, 2025 · 6 revisions

Application Pseudo-Threading

All applications except for the Supervisor utilize co-routines to act as a multi-threading system. There is one "main" co-routine on each of those devices that is responsible for handling the OS event queue, and then auxiliary co-routines for handling various tasks.

This setup provides two key benefits to the different sub-groups of threaded devices, those that run Mekanism API calls and those that perform long-running graphics render tasks.

Main "Thread"

Main threads are always responsible for handling peripheral disconnect/reconnects, receiving modem messages, handling mouse events, stopping the system on terminate events, and handling watchdog, loop clock, and other timer events. OS pullEvent or pullEventRaw consumes all events in a co-routine, so having a single thread perform this central loop management while others can block on peripheral calls prevents the loss of events.

The main thread communicates with the others via "shared memory" that includes flags, objects, and message queues. These message queues can contain simple integer alerts, communications frames or packets, or other arbitrary data.

Mekanism Co-routine Advantages

For the first group, the co-routine threading allows auxiliary co-routines to run commands that would consume the event queue, usually the Mekanism API calls. Bringing those functions out of the main co-routine means that it can always catch all the incoming events, rather than some of them being consumed by Mekanism functions waiting for task_complete events.

Additionally, in any given peripheral query, all functions are called in parallel co-routines to try to resolve all checks in a single server tick. The RPS functions this way, along with all the Mekanism RTUs, to quickly check status values rather than taking entire seconds to perform a single update query.

Graphics Render Advantages

For the second group, the co-routine threading allows long-running graphics render tasks that yield often to run in a separate co-routine, allowing the main event handler to continue to process timer events and communications messages, preventing the system from locking up relative to the other devices in the network while it completes a render. This is used by the Coordinator to launch the initial multi-display render task from blank, and by the Pocket computer to launch apps fresh.

Application Termination

Note that terminate events (triggered by ctrl + T or the terminate button) are sometimes caught while doing peripheral related commands, due to receiving a terminate before the command has completed with task_complete. The PPM records this with a flag as it catches that error, then the main thread checks if the PPM detected that they system should terminate.

Application Implementations

Review the threads.lua files in any of the four applications using threading for how that particular devices uses it. Shared memory is always defined in startup.lua near the start of the main function entry point.

Homepage

  1. User Manual
    1. System Setup Guide
      1. Device Layout
        1. Coordinator Displays
      2. Connecting to Mekanism Machines
      3. Application Installation
        1. Space Requirements
        2. Installation
        3. Updating
        4. Uninstalling
      4. SCADA Network
        1. Network Channels
        2. Network Connectivity
      5. Setup & Configuration
        1. Supervisor Setup
        2. Coordinator Setup
        3. Reactor PLC Setup
        4. RTU Gateway Setup
      6. Initial Startup
    2. Configurator Tools
      1. Supervisor Configurator
      2. Coordinator Configurator
      3. Reactor PLC Configurator
      4. RTU Gateway Configurator
      5. Pocket Configurator
    3. Redstone Waste Setup Guide
    4. TL;DR Quickstart
    5. FAQ and Common Problems
    6. Colorblind Accessibility
    7. Annunciator Panels
    8. Alarms
    9. System Views
      1. Main View Screen
      2. Valve & Flow Screen
      3. Unit View Screens
    10. Operating Procedure
    11. Status Messages
    12. Glossary
  2. Computer Applications
    1. Reactor PLC
      1. Front Panel
      2. Non-Networked Mode
      3. RPS
      4. PLC Emergency Coolant
    2. RTU Gateway
      1. Front Panel
    3. Supervisory Computer
      1. Front Panel
    4. Coordinator Computer
      1. Front Panel
    5. Pocket
      1. Home and Navigation
      2. Connectivity
      3. Main Applications
      4. System Details and Testing
  3. Notable System Components
    1. Co-routine Threading
    2. Project File Layout/Overview
    3. PPM - Protected Peripherals Manager
    4. RSIO - Redstone I/O
    5. Utility Classes
    6. Networking
  4. Notes
    1. Known Issues
    2. Process Closed Loop Controller
    3. Project Files and Layout
    4. Alternative Installation Strategies
  5. Investigations
    1. Startup Rate High
    2. Reactor Temperature Calculation
  6. References & Resources
    1. CC: Tweaked Character Set
    2. Mekanism API Notes
    3. Design & Regulatory References
  7. Legacy
    1. coord.settings File
    2. config.lua Files
      1. Supervisor config.lua
      2. Coordinator config.lua
      3. Reactor PLC config.lua
      4. RTU Gateway config.lua

Clone this wiki locally