OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
IOThread.hpp
Go to the documentation of this file.
1
41#ifndef OPENSWMM_ENGINE_IO_THREAD_HPP
42#define OPENSWMM_ENGINE_IO_THREAD_HPP
43
44#include "WriteTask.hpp"
45
46#include <thread>
47#include <mutex>
48#include <condition_variable>
49#include <queue>
50#include <atomic>
51#include <functional>
52
53namespace openswmm {
54
55class PluginFactory;
56
64class IOThread {
65public:
67 static constexpr std::size_t DEFAULT_QUEUE_CAPACITY = 8;
68
74 explicit IOThread(PluginFactory& factory,
75 std::size_t capacity = DEFAULT_QUEUE_CAPACITY);
76
77 ~IOThread();
78
79 // Non-copyable, non-movable (owns a thread)
80 IOThread(const IOThread&) = delete;
81 IOThread& operator=(const IOThread&) = delete;
82
83 // -----------------------------------------------------------------------
84 // Control
85 // -----------------------------------------------------------------------
86
91 void start();
92
99 void post(SimulationSnapshot snap);
100
107 void stop();
108
109 // -----------------------------------------------------------------------
110 // Diagnostics
111 // -----------------------------------------------------------------------
112
114 bool running() const noexcept { return running_.load(std::memory_order_relaxed); }
115
117 int tasks_completed() const noexcept { return tasks_completed_.load(std::memory_order_relaxed); }
118
120 int last_error() const noexcept { return last_error_.load(std::memory_order_relaxed); }
121
122private:
123 void run();
124
125 PluginFactory& factory_;
126 const std::size_t capacity_;
127
128 std::thread thread_;
129 std::queue<WriteTask> queue_;
130 std::mutex mutex_;
131 std::condition_variable cv_not_full_;
132 std::condition_variable cv_not_empty_;
133 std::atomic<bool> stop_flag_ {false};
134 std::atomic<bool> running_ {false};
135 std::atomic<int> tasks_completed_{0};
136 std::atomic<int> last_error_ {0};
137 int next_sequence_ = 0;
138};
139
140} /* namespace openswmm */
141
142#endif /* OPENSWMM_ENGINE_IO_THREAD_HPP */
Ring-buffer task descriptor for the IO thread (Phase 5, R17).
Producer-consumer IO thread for writing simulation snapshots.
Definition IOThread.hpp:64
void stop()
Signal the IO thread to finish and join it.
Definition IOThread.cpp:65
int tasks_completed() const noexcept
Number of tasks processed so far.
Definition IOThread.hpp:117
static constexpr std::size_t DEFAULT_QUEUE_CAPACITY
Default maximum queue depth.
Definition IOThread.hpp:67
void start()
Start the IO worker thread.
Definition IOThread.cpp:35
~IOThread()
Definition IOThread.cpp:27
bool running() const noexcept
True if the thread is running.
Definition IOThread.hpp:114
void post(SimulationSnapshot snap)
Post a snapshot to the write queue.
Definition IOThread.cpp:45
int last_error() const noexcept
Last error code from a plugin update (0 = no error).
Definition IOThread.hpp:120
IOThread & operator=(const IOThread &)=delete
IOThread(const IOThread &)=delete
Manages all dynamically loaded plugins for one engine instance.
Definition PluginFactory.hpp:72
Definition Controls.cpp:24
Complete simulation state snapshot at one output time step.
Definition SimulationSnapshot.hpp:90