From 41c12e92031284e357bab04fe6e08b45c6dd3ba8 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 27 Oct 2023 21:21:03 +0800 Subject: feat: improve thread system and gathering gnupg options info to rt --- src/core/thread/Task.cpp | 67 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 10 deletions(-) (limited to 'src/core/thread/Task.cpp') diff --git a/src/core/thread/Task.cpp b/src/core/thread/Task.cpp index ad3d3321..0e0f63ac 100644 --- a/src/core/thread/Task.cpp +++ b/src/core/thread/Task.cpp @@ -29,6 +29,7 @@ #include "core/thread/Task.h" #include +#include #include #include @@ -36,6 +37,8 @@ #include #include +#include "spdlog/spdlog.h" + namespace GpgFrontend::Thread { class Task::Impl : public QObject { @@ -186,37 +189,81 @@ class Task::Impl : public QObject { try { if (callback_) { + SPDLOG_DEBUG("task {} has a callback function", GetFullID()); if (callback_thread_ == QThread::currentThread()) { SPDLOG_DEBUG("for task {}, the callback thread is the same thread", GetFullID(), callback_thread_->currentThreadId()); + callback_(rtn, data_object_); + + // raise signal, announcing this task comes to an end + SPDLOG_DEBUG( + "for task {}, its life comes to an end in the same thread after " + "its callback executed.", + parent_->GetFullID()); + emit parent_->SignalTaskEnd(); } else { SPDLOG_DEBUG("for task {}, callback thread is a different thread: {}", GetFullID(), callback_thread_->currentThreadId()); - if (!QMetaObject::invokeMethod(callback_thread_, - [callback = callback_, rtn = rtn_, - data_object = data_object_]() { - callback(rtn, data_object); - })) { - SPDLOG_ERROR("task {} had failed to invoke callback", GetFullID()); + if (!QMetaObject::invokeMethod( + callback_thread_, + [callback = callback_, rtn = rtn_, data_object = data_object_, + parent_ = this->parent_]() { + SPDLOG_DEBUG("calling callback of task {}", + parent_->GetFullID()); + try { + callback(rtn, data_object); + } catch (...) { + SPDLOG_ERROR( + "unknown exception was caught when execute " + "callback of task {}", + parent_->GetFullID()); + } + // raise signal, announcing this task comes to an end + SPDLOG_DEBUG( + "for task {}, its life comes to an end whether its " + "callback function fails or not.", + parent_->GetFullID()); + emit parent_->SignalTaskEnd(); + })) { + SPDLOG_ERROR( + "task {} had failed to invoke the callback function to target " + "thread", + GetFullID()); + SPDLOG_DEBUG( + "for task {}, its life must come to an end now, although it " + "has something not done yet.", + GetFullID()); + emit parent_->SignalTaskEnd(); } } + } else { + // raise signal, announcing this task comes to an end + SPDLOG_DEBUG( + "for task {}, its life comes to an end without callback " + "peacefully.", + GetFullID()); + emit parent_->SignalTaskEnd(); } } catch (std::exception &e) { SPDLOG_ERROR("exception was caught at task callback: {}", e.what()); SPDLOG_ERROR( "stacktrace of the exception: {}", boost::stacktrace::to_string(boost::stacktrace::stacktrace())); + // raise signal, announcing this task comes to an end + SPDLOG_DEBUG("for task {}, its life comes to an end at chaos.", + GetFullID()); + emit parent_->SignalTaskEnd(); } catch (...) { SPDLOG_ERROR("unknown exception was caught"); SPDLOG_ERROR( "stacktrace of the exception: {}", boost::stacktrace::to_string(boost::stacktrace::stacktrace())); + // raise signal, announcing this task comes to an end + SPDLOG_DEBUG("for task {}, its life comes to an end at unknown chaos.", + GetFullID()); + emit parent_->SignalTaskEnd(); } - - // raise signal, announcing this task come to an end - SPDLOG_DEBUG("for task {}, its life comes to an end.", GetFullID()); - emit parent_->SignalTaskEnd(); } }; -- cgit v1.2.3