logging: Convert backend_thread into an std::jthread

Was getting an unhandled `invalid_argument` [exception](https://en.cppreference.com/w/cpp/thread/thread/join) during
shutdown on my linux machine. This removes the need for a `StopBackendThread` function entirely since `jthread`
[automatically handles both checking if the thread is joinable and stopping the token before attempting to join](https://en.cppreference.com/w/cpp/thread/jthread/~jthread) in the case that `StartBackendThread` was never called.
This commit is contained in:
Wunkolo 2023-06-24 11:43:10 +03:00 committed by GPUCode
parent 197c1adcba
commit ae6fda8638

View file

@ -265,7 +265,8 @@ private:
if (sig <= 0) { if (sig <= 0) {
abort(); abort();
} }
StopBackendThread(); backend_thread.request_stop();
backend_thread.join();
const auto signal_entry = const auto signal_entry =
CreateEntry(Class::Log, Level::Critical, "?", 0, "?", CreateEntry(Class::Log, Level::Critical, "?", 0, "?",
fmt::vformat("Received signal {}", fmt::make_format_args(sig))); fmt::vformat("Received signal {}", fmt::make_format_args(sig)));
@ -308,18 +309,17 @@ private:
SleepForever(); SleepForever();
} }
#endif #endif
StopBackendThread();
} }
void StartBackendThread() { void StartBackendThread() {
backend_thread = std::thread([this] { backend_thread = std::jthread([this](std::stop_token stop_token) {
Common::SetCurrentThreadName("citra:Log"); Common::SetCurrentThreadName("citra:Log");
Entry entry; Entry entry;
const auto write_logs = [this, &entry]() { const auto write_logs = [this, &entry]() {
ForEachBackend([&entry](Backend& backend) { backend.Write(entry); }); ForEachBackend([&entry](Backend& backend) { backend.Write(entry); });
}; };
while (!stop.stop_requested()) { while (!stop_token.stop_requested()) {
entry = message_queue.PopWait(stop.get_token()); entry = message_queue.PopWait(stop_token);
if (entry.filename != nullptr) { if (entry.filename != nullptr) {
write_logs(); write_logs();
} }
@ -333,11 +333,6 @@ private:
}); });
} }
void StopBackendThread() {
stop.request_stop();
backend_thread.join();
}
Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
const char* function, std::string&& message) const { const char* function, std::string&& message) const {
using std::chrono::duration_cast; using std::chrono::duration_cast;
@ -402,8 +397,7 @@ private:
ColorConsoleBackend color_console_backend{}; ColorConsoleBackend color_console_backend{};
FileBackend file_backend; FileBackend file_backend;
std::stop_source stop; std::jthread backend_thread;
std::thread backend_thread;
MPSCQueue<Entry, true> message_queue{}; MPSCQueue<Entry, true> message_queue{};
std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()}; std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};