OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
IOThread.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
56
57#ifndef OPENSWMM_ENGINE_IO_THREAD_HPP
58#define OPENSWMM_ENGINE_IO_THREAD_HPP
59
60#include "WriteTask.hpp"
61
62#include <thread>
63#include <mutex>
64#include <condition_variable>
65#include <queue>
66#include <atomic>
67#include <functional>
68
69namespace openswmm {
70
71class PluginFactory;
72
80class IOThread {
81public:
83 static constexpr std::size_t DEFAULT_QUEUE_CAPACITY = 8;
84
90 explicit IOThread(PluginFactory& factory,
91 std::size_t capacity = DEFAULT_QUEUE_CAPACITY);
92
93 ~IOThread();
94
95 // Non-copyable, non-movable (owns a thread)
96 IOThread(const IOThread&) = delete;
97 IOThread& operator=(const IOThread&) = delete;
98
99 // -----------------------------------------------------------------------
100 // Control
101 // -----------------------------------------------------------------------
102
107 void start();
108
115 void post(SimulationSnapshot snap);
116
123 void stop();
124
125 // -----------------------------------------------------------------------
126 // Diagnostics
127 // -----------------------------------------------------------------------
128
130 bool running() const noexcept { return running_.load(std::memory_order_relaxed); }
131
133 int tasks_completed() const noexcept { return tasks_completed_.load(std::memory_order_relaxed); }
134
136 int last_error() const noexcept { return last_error_.load(std::memory_order_relaxed); }
137
138private:
139 void run();
140
141 PluginFactory& factory_;
142 const std::size_t capacity_;
143
144 std::thread thread_;
145 std::queue<WriteTask> queue_;
146 std::mutex mutex_;
147 std::condition_variable cv_not_full_;
148 std::condition_variable cv_not_empty_;
149 std::atomic<bool> stop_flag_ {false};
150 std::atomic<bool> running_ {false};
151 std::atomic<int> tasks_completed_{0};
152 std::atomic<int> last_error_ {0};
153 int next_sequence_ = 0;
154};
155
156} /* namespace openswmm */
157
158#endif /* OPENSWMM_ENGINE_IO_THREAD_HPP */
Ring-buffer task descriptor for the IO thread (Phase 5, R17).
void stop()
Signal the IO thread to finish and join it.
Definition IOThread.cpp:120
int tasks_completed() const noexcept
Number of tasks processed so far.
Definition IOThread.hpp:133
static constexpr std::size_t DEFAULT_QUEUE_CAPACITY
Default maximum queue depth.
Definition IOThread.hpp:83
void start()
Start the IO worker thread.
Definition IOThread.cpp:90
IOThread(PluginFactory &factory, std::size_t capacity=DEFAULT_QUEUE_CAPACITY)
Construct (does NOT start the thread).
Definition IOThread.cpp:77
~IOThread()
Definition IOThread.cpp:82
bool running() const noexcept
True if the thread is running.
Definition IOThread.hpp:130
void post(SimulationSnapshot snap)
Post a snapshot to the write queue.
Definition IOThread.cpp:100
int last_error() const noexcept
Last error code from a plugin update (0 = no error).
Definition IOThread.hpp:136
IOThread & operator=(const IOThread &)=delete
IOThread(const IOThread &)=delete
Manages plugin discovery, loading, and lifecycle for one engine instance.
Definition PluginFactory.hpp:88
Definition NodeCoupling.cpp:16
Complete simulation state snapshot at one output time step.
Definition SimulationSnapshot.hpp:117