Updated Threading (markdown)

Mikayla 2023-05-24 09:55:20 -04:00
parent 0e525fd04f
commit 21d657ea92

@ -2,6 +2,10 @@
The PLC and RTUs 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 allows the auxiliary co-routines to run commands that would consume the event queue, namely Mekanism API calls. Bringing those functions out of the main co-routine means that it will always catch all the incoming events, rather than some of them being consumed by Mekanism functions waiting for `task_complete` events. The PLC and RTUs 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 allows the auxiliary co-routines to run commands that would consume the event queue, namely Mekanism API calls. Bringing those functions out of the main co-routine means that it will always catch all the incoming events, rather than some of them being consumed by Mekanism functions waiting for `task_complete` events.
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.
Note that terminate events are sometimes caught while doing peripheral related commands, due to receiving a terminate before the command has completed with `task_complete`. The [PPM](https://github.com/MikaylaFischler/cc-mek-scada/wiki/PPM) records this with a flag as it catches that error, then the main thread checks if the PPM reports that they system should terminate.
## Reactor PLC ## Reactor PLC
The reactor PLC has 5 co-routines running via the ComputerCraft `parallel` API: The reactor PLC has 5 co-routines running via the ComputerCraft `parallel` API:
@ -11,3 +15,7 @@ The reactor PLC has 5 co-routines running via the ComputerCraft `parallel` API:
- Communications Tx (Transmit) Thread - Communications Tx (Transmit) Thread
- Communications Rx (Receive) Thread - Communications Rx (Receive) Thread
- Setpoint Control Thread - Setpoint Control Thread
### Main Thread
The PLC main thread is responsible for timer events, modem messages, peripheral connections and disconnections, mouse events, and terminate events. To instruct other threads, it sends information across command and data queues to prevent blocking the main thread. For example, it can trip a timeout RPS trip due to a watchdog event via `smem.q.mq_rps.push_command(MQ__RPS_CMD.TRIP_TIMEOUT)`, which passes an RPS command across the message queue object that the RPS thread can look for. It also pushes parsed packets to the communications Rx queue as it receives modem messages, and updates device status and peripheral references on peripheral connect/disconnect events.