![]() |
OpenSWMM Engine
6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
|
Producer-consumer IO thread for writing simulation snapshots. More...
#include <IOThread.hpp>
Public Member Functions | |
| IOThread (PluginFactory &factory, std::size_t capacity=DEFAULT_QUEUE_CAPACITY) | |
| Construct (does NOT start the thread). | |
| ~IOThread () | |
| Stop/drain and join the native worker before releasing queue storage. Can block for plugin writes; destruction must not run from the worker itself. | |
| IOThread (const IOThread &)=delete | |
| Disable copying a live thread, queue and borrowed factory association. | |
| IOThread & | operator= (const IOThread &)=delete |
| Disable assignment of thread/queue ownership. | |
| void | start () |
| Start the IO worker thread. | |
| void | post (SimulationSnapshot snap) |
| Post a snapshot to the write queue. | |
| void | stop () |
| Signal the IO thread to finish and join it. | |
| int | flush () const |
| bool | running () const noexcept |
| True if the thread is running. | |
| int | tasks_completed () const noexcept |
| Number of tasks processed so far. | |
| int | last_error () const noexcept |
| Last error code from a plugin update (0 = no error). | |
Static Public Attributes | |
| static constexpr std::size_t | DEFAULT_QUEUE_CAPACITY = 8 |
| Default maximum queue depth. | |
Producer-consumer IO thread for writing simulation snapshots.
One IOThread per SWMMEngine instance. Shared with PluginFactory.
|
explicit |
Construct (does NOT start the thread).
| factory | Borrowed factory required to outlive this object and active worker. Plugins must accept snapshot delivery on the native IO thread (or caller thread for WebAssembly), and callbacks must not throw across the worker boundary. |
| capacity | Positive queued-task budget; zero causes native post() to wait until stop. Capacity excludes the one currently executing task. No validation/clamp occurs. |
| openswmm::IOThread::~IOThread | ( | ) |
Stop/drain and join the native worker before releasing queue storage. Can block for plugin writes; destruction must not run from the worker itself.
|
delete |
Disable copying a live thread, queue and borrowed factory association.
| int openswmm::IOThread::flush | ( | ) | const |
Wait for queued and currently executing writes. Call from the producer.
|
inlinenoexcept |
Last error code from a plugin update (0 = no error).
Disable assignment of thread/queue ownership.
| void openswmm::IOThread::post | ( | SimulationSnapshot | snap | ) |
Post a snapshot to the write queue.
Native producers block at queue capacity; WebAssembly writes synchronously.
| snap | Snapshot taken by value and moved into queue ownership. Its owned vectors isolate ordinary simulation data, but any retained context pointer must satisfy plugin lifetime/access contracts. Native producers wait for space, or discard the request if stop is observed. WebAssembly calls update_all() directly without capacity waiting and requires serialized callers. Errors returned by plugins become sticky last_error(); no exception recovery is installed. |
|
inlinenoexcept |
True if the thread is running.
| void openswmm::IOThread::start | ( | ) |
Start the IO worker thread.
Call once before posting, with lifecycle externally serialized. Native thread sets running asynchronously; repeated immediate start calls can race that flag and must not occur. After stop/join, restarting retains sequence/counters/ sticky error. WebAssembly only changes flags and uses synchronous writes. Native thread creation errors propagate.
| void openswmm::IOThread::stop | ( | ) |
Signal the IO thread to finish and join it.
Waits for all queued tasks to be processed, then joins the thread. Safe to call even if start() was never called.
Discards newly posted work after the stop flag, wakes blocked native producers, drains already queued tasks and joins the worker. Requires lifecycle serialization and a nonworker caller. If never started, an existing queue is not drained; intended usage starts before posting. WebAssembly simply clears running.
|
inlinenoexcept |
Number of tasks processed so far.
|
staticconstexpr |
Default maximum queue depth.