18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 22:04:59 +01:00

fix(physicslog): every stop asked wakes the writer, the one from itself too; no psyche for a driver of no vehicle

This commit is contained in:
maj00r
2026-11-16 02:12:01 +01:00
parent 94b7c7f01b
commit 423d49b42c
2 changed files with 21 additions and 16 deletions

View File

@@ -40,10 +40,10 @@ void keep_last(std::string &Text, std::size_t const Most) {
physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : m_folder{std::move(Folder)}, m_interval{Interval} {} physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : m_folder{std::move(Folder)}, m_interval{Interval} {}
physicslog_writer::~physicslog_writer() { physicslog_writer::~physicslog_writer() {
// at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse
try { try {
stop(); stop();
} catch (...) { } catch (...) {
// at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse
} }
} }
@@ -81,28 +81,25 @@ bool physicslog_writer::flush_for(std::chrono::milliseconds const Timeout) {
void physicslog_writer::stop() { void physicslog_writer::stop() {
std::jthread thread; std::jthread thread;
bool itself;
{ {
std::scoped_lock lock{m_mutex}; std::scoped_lock lock{m_mutex};
m_stopped = true; m_stopped = true;
// stopped from the writer itself, by the end of a run which failed in it: the files may be held by it, and
// another thread may be joining it already
if (m_writer == std::this_thread::get_id()) {
if (m_thread.joinable()) {
m_thread.request_stop();
m_thread.detach();
}
return;
}
// taken by the first to stop, so it's joined once // taken by the first to stop, so it's joined once
thread = std::move(m_thread); thread = std::move(m_thread);
itself = m_writer == std::this_thread::get_id();
} }
if (thread.joinable()) { // the stop asked outside the lock, which waking the writer takes
if (thread.joinable())
thread.request_stop(); thread.request_stop();
// the writer waits under the lock: taken once, it's either about to wait, seeing the stop, or waiting already // stopped from the writer itself, by the end of a run which failed in it: the files may be held by it
{ std::scoped_lock lock{m_mutex}; } if (itself) {
m_wake.notify_all(); if (thread.joinable())
thread.join(); thread.detach();
return;
} }
if (thread.joinable())
thread.join();
flush(); flush();
} }
@@ -114,6 +111,12 @@ void physicslog_writer::start() {
} }
void physicslog_writer::run(std::stop_token const &Stop) { void physicslog_writer::run(std::stop_token const &Stop) {
// woken by every stop asked, by stop() or by the end of the thread itself. the lock taken once first: the writer is
// either about to wait, and sees the stop, or waiting already
std::stop_callback const wake{Stop, [this] {
{ std::scoped_lock lock{m_mutex}; }
m_wake.notify_all();
}};
while (false == Stop.stop_requested()) { while (false == Stop.stop_requested()) {
{ {
std::unique_lock lock{m_mutex}; std::unique_lock lock{m_mutex};

View File

@@ -2334,7 +2334,9 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche,
iDrivigFlags |= movePrimary; // aktywnie prowadzące pojazd iDrivigFlags |= movePrimary; // aktywnie prowadzące pojazd
} }
SetDriverPsyche(); // na końcu, bo wymaga ustawienia zmiennych if( mvOccupied != nullptr ) {
SetDriverPsyche(); // na końcu, bo wymaga ustawienia zmiennych
}
TableClear(); TableClear();
// the logs of all, begun at once as they always were. one of a train only at its first line (update_logs): every // the logs of all, begun at once as they always were. one of a train only at its first line (update_logs): every