18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 06:55:02 +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() {
// at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse
try {
stop();
} 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() {
std::jthread thread;
bool itself;
{
std::scoped_lock lock{m_mutex};
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
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();
// the writer waits under the lock: taken once, it's either about to wait, seeing the stop, or waiting already
{ std::scoped_lock lock{m_mutex}; }
m_wake.notify_all();
thread.join();
// stopped from the writer itself, by the end of a run which failed in it: the files may be held by it
if (itself) {
if (thread.joinable())
thread.detach();
return;
}
if (thread.joinable())
thread.join();
flush();
}
@@ -114,6 +111,12 @@ void physicslog_writer::start() {
}
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()) {
{
std::unique_lock lock{m_mutex};