diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/GpgContext.cpp | 14 | ||||
-rw-r--r-- | src/core/function/gpg/GpgCommandExecutor.cpp | 1 | ||||
-rw-r--r-- | src/core/thread/FileReadTask.cpp | 5 | ||||
-rw-r--r-- | src/core/thread/Task.cpp | 29 | ||||
-rw-r--r-- | src/core/thread/Task.h | 15 | ||||
-rw-r--r-- | src/core/thread/TaskRunner.cpp | 26 | ||||
-rw-r--r-- | src/module/integrated/version_checking_module/VersionCheckTask.cpp | 9 | ||||
-rw-r--r-- | src/ui/thread/KeyServerImportTask.cpp | 8 | ||||
-rw-r--r-- | src/ui/thread/KeyServerSearchTask.cpp | 8 | ||||
-rw-r--r-- | src/ui/thread/ListedKeyServerTestTask.cpp | 5 | ||||
-rw-r--r-- | src/ui/thread/ProxyConnectionTestTask.cpp | 8 | ||||
-rw-r--r-- | src/ui/thread/VersionCheckTask.cpp | 6 | ||||
-rw-r--r-- | src/ui/widgets/PlainTextEditorPage.cpp | 4 |
13 files changed, 64 insertions, 74 deletions
diff --git a/src/core/GpgContext.cpp b/src/core/GpgContext.cpp index e6c4d322..059a5179 100644 --- a/src/core/GpgContext.cpp +++ b/src/core/GpgContext.cpp @@ -353,7 +353,8 @@ const GpgInfo &GpgContext::GetInfo(bool refresh) { // get all components GpgCommandExecutor::GetInstance().Execute( info_.GpgConfPath, {"--list-components"}, - [=](int exit_code, const std::string &p_out, const std::string &p_err) { + [this](int exit_code, const std::string &p_out, + const std::string &p_err) { SPDLOG_DEBUG( "gpgconf components exit_code: {} process stdout size: {}", exit_code, p_out.size()); @@ -437,7 +438,8 @@ const GpgInfo &GpgContext::GetInfo(bool refresh) { GpgCommandExecutor::GetInstance().ExecuteConcurrently( info_.GpgConfPath, {"--list-dirs"}, - [=](int exit_code, const std::string &p_out, const std::string &p_err) { + [this](int exit_code, const std::string &p_out, + const std::string &p_err) { SPDLOG_DEBUG( "gpgconf configurations exit_code: {} process stdout size: {}", exit_code, p_out.size()); @@ -495,8 +497,8 @@ const GpgInfo &GpgContext::GetInfo(bool refresh) { GpgCommandExecutor::GetInstance().ExecuteConcurrently( info_.GpgConfPath, {"--check-options", component.first}, - [=](int exit_code, const std::string &p_out, - const std::string &p_err) { + [this, component](int exit_code, const std::string &p_out, + const std::string &p_err) { SPDLOG_DEBUG( "gpgconf {} options exit_code: {} process stdout " "size: {} ", @@ -552,8 +554,8 @@ const GpgInfo &GpgContext::GetInfo(bool refresh) { GpgCommandExecutor::GetInstance().ExecuteConcurrently( info_.GpgConfPath, {"--list-options", component.first}, - [=](int exit_code, const std::string &p_out, - const std::string &p_err) { + [this, component](int exit_code, const std::string &p_out, + const std::string &p_err) { SPDLOG_DEBUG( "gpgconf {} avaliable options exit_code: {} process stdout " "size: {} ", diff --git a/src/core/function/gpg/GpgCommandExecutor.cpp b/src/core/function/gpg/GpgCommandExecutor.cpp index 8c04f41a..2fee2be6 100644 --- a/src/core/function/gpg/GpgCommandExecutor.cpp +++ b/src/core/function/gpg/GpgCommandExecutor.cpp @@ -259,6 +259,7 @@ void GpgFrontend::GpgCommandExecutor::ExecuteConcurrently( auto *process_task = new GpgFrontend::Thread::Task( std::move(runner), fmt::format("ExecuteConcurrently/{}", cmd), data_object, std::move(result_callback), false); + process_task->HoldOnLifeCycle(true); GpgFrontend::Thread::TaskRunnerGetter::GetInstance() .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_External_Process) diff --git a/src/core/thread/FileReadTask.cpp b/src/core/thread/FileReadTask.cpp index e0ef2f16..52ec9db9 100644 --- a/src/core/thread/FileReadTask.cpp +++ b/src/core/thread/FileReadTask.cpp @@ -31,6 +31,7 @@ namespace GpgFrontend::UI { FileReadTask::FileReadTask(std::string path) : Task("file_read_task") { + HoldOnLifeCycle(true); connect(this, &FileReadTask::SignalFileBytesReadNext, this, &FileReadTask::read_bytes); @@ -45,8 +46,6 @@ FileReadTask::FileReadTask(std::string path) : Task("file_read_task") { } void FileReadTask::Run() { - HoldOnLifeCycle(true); - if (is_regular_file(read_file_path_)) { SPDLOG_DEBUG("read open file: {}", read_file_path_.u8string()); @@ -76,7 +75,7 @@ void FileReadTask::read_bytes() { SPDLOG_DEBUG("read bytes end"); emit SignalFileBytesReadEnd(); // announce finish task - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } } diff --git a/src/core/thread/Task.cpp b/src/core/thread/Task.cpp index 031dc4c7..bda64564 100644 --- a/src/core/thread/Task.cpp +++ b/src/core/thread/Task.cpp @@ -109,9 +109,7 @@ class Task::Impl : public QObject { * * @param finish_after_run */ - void HoldOnLifeCycle(bool hold_on) { - this->run_callback_after_runnable_finished_ = !hold_on; - } + void HoldOnLifeCycle(bool hold_on) { parent_->setAutoDelete(!hold_on); } /** * @brief @@ -139,28 +137,29 @@ class Task::Impl : public QObject { boost::stacktrace::to_string(boost::stacktrace::stacktrace())); } // raise signal to anounce after runnable returned - if (run_callback_after_runnable_finished_) - emit parent_->SignalTaskRunnableEnd(rtn_); + if (parent_->autoDelete()) slot_task_run_callback(rtn_); } private: Task *const parent_; const std::string uuid_; const std::string name_; - const bool sequency_ = true; ///< must run in the same thread - TaskCallback callback_; ///< - TaskRunnable runnable_; ///< - bool run_callback_after_runnable_finished_ = true; ///< - int rtn_ = 0; ///< - QThread *callback_thread_ = nullptr; ///< - DataObjectPtr data_object_ = nullptr; ///< + const bool sequency_ = true; ///< must run in the same thread + TaskCallback callback_; ///< + TaskRunnable runnable_; ///< + int rtn_ = 0; ///< + QThread *callback_thread_ = nullptr; ///< + DataObjectPtr data_object_ = nullptr; ///< void init() { connect(parent_, &Task::SignalRun, this, &Task::Impl::slot_run); - // after runnable finished, running callback - connect(parent_, &Task::SignalTaskRunnableEnd, this, + // + connect(parent_, &Task::SignalTaskShouldEnd, this, &Impl::slot_task_run_callback); + + // + connect(parent_, &Task::SignalTaskShouldEnd, parent_, &Task::deleteLater); } /** @@ -253,7 +252,7 @@ void Task::SlotRun() { emit SignalRun(); } void Task::Run() { p_->Run(); } -void Task::run() { emit SignalRun(); } +void Task::run() { this->SlotRun(); } } // namespace GpgFrontend::Thread diff --git a/src/core/thread/Task.h b/src/core/thread/Task.h index 388b5a35..769fafd2 100644 --- a/src/core/thread/Task.h +++ b/src/core/thread/Task.h @@ -104,6 +104,8 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable { */ bool GetSequency() const; + void HoldOnLifeCycle(bool hold_on); + public slots: /** @@ -117,26 +119,19 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable { void SignalRun(); /** - * @brief announce runnable finished + * @brief task should finish * */ - void SignalTaskRunnableEnd(int rtn); + void SignalTaskShouldEnd(int rtn); /** - * @brief runnable and callabck all finished + * @brief task is finished * */ void SignalTaskEnd(); protected: /** - * @brief Set the Finish After Run object - * - * @param finish_after_run - */ - void HoldOnLifeCycle(bool hold_on); - - /** * @brief * * @param rtn diff --git a/src/core/thread/TaskRunner.cpp b/src/core/thread/TaskRunner.cpp index 6eed7a89..494e356c 100644 --- a/src/core/thread/TaskRunner.cpp +++ b/src/core/thread/TaskRunner.cpp @@ -30,6 +30,7 @@ #include <qobjectdefs.h> #include <qthread.h> +#include <qthreadpool.h> #include <memory> @@ -47,27 +48,19 @@ class TaskRunner::Impl : public QThread { } task->setParent(nullptr); - connect(task, &Task::SignalTaskEnd, task, &Task::deleteLater); if (task->GetSequency()) { SPDLOG_TRACE("post task: {}, sequency mode: {}", task->GetFullID(), task->GetSequency()); task->moveToThread(this); } else { - // if it need to run concurrently, we should create a new thread to - // run it. - auto* concurrent_thread = new QThread(this); - - connect(task, &Task::SignalTaskEnd, concurrent_thread, &QThread::quit); - // concurrent thread is responsible for deleting the task - connect(concurrent_thread, &QThread::finished, task, &Task::deleteLater); - // concurrent thread is responsible for self deleting - connect(concurrent_thread, &QThread::finished, concurrent_thread, - &QThread::deleteLater); - - // start thread - concurrent_thread->start(); - task->moveToThread(concurrent_thread); + if (pool_.tryStart(task)) { + SPDLOG_TRACE("runner's pool starts concurrent task {} immediately", + task->GetFullID()); + } else { + SPDLOG_TRACE("runner's pool will start concurrent task {} later", + task->GetFullID()); + } } emit task->SignalRun(); } @@ -76,6 +69,9 @@ class TaskRunner::Impl : public QThread { if (task == nullptr) return; // TODO } + + private: + QThreadPool pool_; }; GpgFrontend::Thread::TaskRunner::TaskRunner() : p_(std::make_unique<Impl>()) {} diff --git a/src/module/integrated/version_checking_module/VersionCheckTask.cpp b/src/module/integrated/version_checking_module/VersionCheckTask.cpp index b1e8eca8..a2a329f6 100644 --- a/src/module/integrated/version_checking_module/VersionCheckTask.cpp +++ b/src/module/integrated/version_checking_module/VersionCheckTask.cpp @@ -42,13 +42,12 @@ VersionCheckTask::VersionCheckTask() current_version_(std::string("v") + std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR) + "." + std::to_string(VERSION_PATCH)) { + HoldOnLifeCycle(true); qRegisterMetaType<SoftwareVersion>("SoftwareVersion"); version_.current_version = current_version_; } void VersionCheckTask::Run() { - HoldOnLifeCycle(true); - try { using namespace nlohmann; SPDLOG_DEBUG("current version: {}", current_version_); @@ -66,7 +65,7 @@ void VersionCheckTask::Run() { } catch (...) { SPDLOG_ERROR("unknown error occurred"); - emit SignalTaskRunnableEnd(-1); + emit SignalTaskShouldEnd(-1); } } @@ -130,7 +129,7 @@ void VersionCheckTask::slot_parse_latest_version_info() { &VersionCheckTask::slot_parse_current_version_info); } catch (...) { SPDLOG_ERROR("current version request create error"); - emit SignalTaskRunnableEnd(-1); + emit SignalTaskShouldEnd(-1); } } @@ -173,7 +172,7 @@ void VersionCheckTask::slot_parse_current_version_info() { } emit SignalUpgradeVersion(version_); - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } } // namespace GpgFrontend::Module::Integrated::VersionCheckingModule diff --git a/src/ui/thread/KeyServerImportTask.cpp b/src/ui/thread/KeyServerImportTask.cpp index 7b8c8984..99e662af 100644 --- a/src/ui/thread/KeyServerImportTask.cpp +++ b/src/ui/thread/KeyServerImportTask.cpp @@ -35,11 +35,11 @@ GpgFrontend::UI::KeyServerImportTask::KeyServerImportTask( : Task("key_server_import_task"), keyserver_url_(std::move(keyserver_url)), keyids_(std::move(keyids)), - manager_(new QNetworkAccessManager(this)) {} - -void GpgFrontend::UI::KeyServerImportTask::run() { + manager_(new QNetworkAccessManager(this)) { HoldOnLifeCycle(true); +} +void GpgFrontend::UI::KeyServerImportTask::run() { QUrl keyserver_url = QUrl(keyserver_url_.c_str()); for (const auto& key_id : keyids_) { QUrl req_url(keyserver_url.scheme() + "://" + keyserver_url.host() + @@ -62,6 +62,6 @@ void GpgFrontend::UI::KeyServerImportTask::dealing_reply_from_server() { emit SignalKeyServerImportResult(network_reply, buffer); if (result_count_++ == keyids_.size() - 1) { - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } }
\ No newline at end of file diff --git a/src/ui/thread/KeyServerSearchTask.cpp b/src/ui/thread/KeyServerSearchTask.cpp index 5da50c6e..2aaa95f0 100644 --- a/src/ui/thread/KeyServerSearchTask.cpp +++ b/src/ui/thread/KeyServerSearchTask.cpp @@ -35,11 +35,11 @@ GpgFrontend::UI::KeyServerSearchTask::KeyServerSearchTask( : Task("key_server_search_task"), keyserver_url_(std::move(keyserver_url)), search_string_(std::move(search_string)), - manager_(new QNetworkAccessManager(this)) {} - -void GpgFrontend::UI::KeyServerSearchTask::run() { + manager_(new QNetworkAccessManager(this)) { HoldOnLifeCycle(true); +} +void GpgFrontend::UI::KeyServerSearchTask::run() { QUrl url_from_remote = QString::fromStdString(keyserver_url_) + "/pks/lookup?search=" + QString::fromStdString(search_string_) + @@ -58,5 +58,5 @@ void GpgFrontend::UI::KeyServerSearchTask::dealing_reply_from_server() { buffer = reply_->readAll(); } emit SignalKeyServerSearchResult(network_reply, buffer); - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } diff --git a/src/ui/thread/ListedKeyServerTestTask.cpp b/src/ui/thread/ListedKeyServerTestTask.cpp index f02d3ce1..38495f55 100644 --- a/src/ui/thread/ListedKeyServerTestTask.cpp +++ b/src/ui/thread/ListedKeyServerTestTask.cpp @@ -38,13 +38,12 @@ GpgFrontend::UI::ListedKeyServerTestTask::ListedKeyServerTestTask( timeout_(timeout), network_manager_(new QNetworkAccessManager(this)), result_(urls_.size(), kTestResultType_Error) { + HoldOnLifeCycle(true); qRegisterMetaType<std::vector<KeyServerTestResultType>>( "std::vector<KeyServerTestResultType>"); } void GpgFrontend::UI::ListedKeyServerTestTask::run() { - HoldOnLifeCycle(true); - size_t index = 0; for (const auto& url : urls_) { auto key_url = QUrl{url}; @@ -87,6 +86,6 @@ void GpgFrontend::UI::ListedKeyServerTestTask::slot_process_network_reply( if (++result_count_ == urls_.size()) { emit SignalKeyServerListTestResult(result_); - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } } diff --git a/src/ui/thread/ProxyConnectionTestTask.cpp b/src/ui/thread/ProxyConnectionTestTask.cpp index 01313cf7..9a723727 100644 --- a/src/ui/thread/ProxyConnectionTestTask.cpp +++ b/src/ui/thread/ProxyConnectionTestTask.cpp @@ -35,11 +35,11 @@ GpgFrontend::UI::ProxyConnectionTestTask::ProxyConnectionTestTask(QString url, : Task("proxy_connection_test_task"), url_(std::move(url)), timeout_(timeout), - network_manager_(new QNetworkAccessManager(this)) {} - -void GpgFrontend::UI::ProxyConnectionTestTask::run() { + network_manager_(new QNetworkAccessManager(this)) { HoldOnLifeCycle(true); +} +void GpgFrontend::UI::ProxyConnectionTestTask::run() { auto* network_reply = network_manager_->get(QNetworkRequest{url_}); auto* timer = new QTimer(this); @@ -74,5 +74,5 @@ void GpgFrontend::UI::ProxyConnectionTestTask::slot_process_network_reply( } emit SignalProxyConnectionTestResult(result_); - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } diff --git a/src/ui/thread/VersionCheckTask.cpp b/src/ui/thread/VersionCheckTask.cpp index 84f21317..c03ccb16 100644 --- a/src/ui/thread/VersionCheckTask.cpp +++ b/src/ui/thread/VersionCheckTask.cpp @@ -66,7 +66,7 @@ void VersionCheckTask::Run() { } catch (...) { SPDLOG_ERROR("unknown error occurred"); - emit SignalTaskRunnableEnd(-1); + emit SignalTaskShouldEnd(-1); } } @@ -130,7 +130,7 @@ void VersionCheckTask::slot_parse_latest_version_info() { &VersionCheckTask::slot_parse_current_version_info); } catch (...) { SPDLOG_ERROR("current version request create error"); - emit SignalTaskRunnableEnd(-1); + emit SignalTaskShouldEnd(-1); } } @@ -173,7 +173,7 @@ void VersionCheckTask::slot_parse_current_version_info() { } emit SignalUpgradeVersion(version_); - emit SignalTaskRunnableEnd(0); + emit SignalTaskShouldEnd(0); } } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/PlainTextEditorPage.cpp b/src/ui/widgets/PlainTextEditorPage.cpp index 321c6c40..483973f2 100644 --- a/src/ui/widgets/PlainTextEditorPage.cpp +++ b/src/ui/widgets/PlainTextEditorPage.cpp @@ -184,10 +184,10 @@ void PlainTextEditorPage::ReadFile() { connect(this, &PlainTextEditorPage::SignalUIBytesDisplayed, read_task, &FileReadTask::SignalFileBytesReadNext, Qt::QueuedConnection); - connect(read_task, &FileReadTask::SignalTaskRunnableEnd, this, + connect(read_task, &FileReadTask::SignalTaskShouldEnd, this, []() { SPDLOG_DEBUG("read thread closed"); }); connect(this, &PlainTextEditorPage::close, read_task, - [=]() { read_task->SignalTaskRunnableEnd(0); }); + [=]() { read_task->SignalTaskShouldEnd(0); }); connect(read_task, &FileReadTask::SignalFileBytesReadEnd, this, [=]() { // set the UI if (!binary_mode_) text_page->setReadOnly(false); |