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
58
59#ifndef OPENSWMM_ENGINE_IO_THREAD_HPP
60#define OPENSWMM_ENGINE_IO_THREAD_HPP
61
62#include "WriteTask.hpp"
63
64#include <thread>
65#include <mutex>
66#include <condition_variable>
67#include <queue>
68#include <atomic>
69#include <functional>
70
79
80namespace openswmm {
81
82class PluginFactory;
83
91class IOThread {
92public:
94 static constexpr std::size_t DEFAULT_QUEUE_CAPACITY = 8;
95
104 explicit IOThread(PluginFactory& factory, std::size_t capacity = DEFAULT_QUEUE_CAPACITY);
105
108 ~IOThread();
109
110 // Non-copyable, non-movable (owns a thread)
112 IOThread(const IOThread&) = delete;
114 IOThread& operator=(const IOThread&) = delete;
115
116 // -----------------------------------------------------------------------
117 // Control
118 // -----------------------------------------------------------------------
119
129 void start();
130
142 void post(SimulationSnapshot snap);
143
154 void stop();
155
162 int flush() const;
163
164 // -----------------------------------------------------------------------
165 // Diagnostics
166 // -----------------------------------------------------------------------
167
171 bool running() const noexcept { return running_.load(std::memory_order_relaxed); }
172
176 int tasks_completed() const noexcept {
177 return tasks_completed_.load(std::memory_order_relaxed);
178 }
179
183 int last_error() const noexcept { return last_error_.load(std::memory_order_relaxed); }
184
185private:
190 void run();
191
192 PluginFactory& factory_;
193 const std::size_t capacity_;
194
195 std::thread thread_;
196 std::queue<WriteTask> queue_;
197 mutable std::mutex mutex_;
198 mutable std::condition_variable cv_idle_;
199 bool writing_ = false;
200 std::condition_variable cv_not_full_;
201 std::condition_variable cv_not_empty_;
202 std::atomic<bool> stop_flag_{false};
203 std::atomic<bool> running_{false};
204 std::atomic<int> tasks_completed_{0};
205 std::atomic<int> last_error_{0};
206 int next_sequence_ = 0;
207};
208
209} /* namespace openswmm */
210
211#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:122
int tasks_completed() const noexcept
Number of tasks processed so far.
Definition IOThread.hpp:176
static constexpr std::size_t DEFAULT_QUEUE_CAPACITY
Default maximum queue depth.
Definition IOThread.hpp:94
void start()
Start the IO worker thread.
Definition IOThread.cpp:91
IOThread(PluginFactory &factory, std::size_t capacity=DEFAULT_QUEUE_CAPACITY)
Construct (does NOT start the thread).
Definition IOThread.cpp:80
~IOThread()
Stop/drain and join the native worker before releasing queue storage. Can block for plugin writes; de...
Definition IOThread.cpp:83
bool running() const noexcept
True if the thread is running.
Definition IOThread.hpp:171
void post(SimulationSnapshot snap)
Post a snapshot to the write queue.
Definition IOThread.cpp:102
int last_error() const noexcept
Last error code from a plugin update (0 = no error).
Definition IOThread.hpp:183
IOThread & operator=(const IOThread &)=delete
Disable assignment of thread/queue ownership.
IOThread(const IOThread &)=delete
Disable copying a live thread, queue and borrowed factory association.
int flush() const
Definition IOThread.cpp:136
Manages plugin discovery, loading, and lifecycle for one engine instance.
Definition PluginFactory.hpp:91
Definition ExchangeLaw.hpp:60
Complete simulation state snapshot at one output time step.
Definition SimulationSnapshot.hpp:123