diff options
author | Saturn&Eric <[email protected]> | 2022-05-20 19:13:55 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2022-05-20 19:13:55 +0000 |
commit | 306cc5b41af9596875c2999af638eaa35899e404 (patch) | |
tree | 6d8d5b3cdb35e79527d33c63d4e69995f9c3f2df /src/core/thread/TaskRunner.cpp | |
parent | Merge pull request #65 from saturneric/develop-2.0.8 (diff) | |
parent | fix(ui): there is possible null pointer dereference (diff) | |
download | GpgFrontend-306cc5b41af9596875c2999af638eaa35899e404.tar.gz GpgFrontend-306cc5b41af9596875c2999af638eaa35899e404.zip |
Merge pull request #66 from saturneric/develop-2.0.8
Develop 2.0.8.2
Diffstat (limited to 'src/core/thread/TaskRunner.cpp')
-rw-r--r-- | src/core/thread/TaskRunner.cpp | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/core/thread/TaskRunner.cpp b/src/core/thread/TaskRunner.cpp index 3b565abb..7116ca71 100644 --- a/src/core/thread/TaskRunner.cpp +++ b/src/core/thread/TaskRunner.cpp @@ -36,12 +36,23 @@ GpgFrontend::Thread::TaskRunner::TaskRunner() = default; GpgFrontend::Thread::TaskRunner::~TaskRunner() = default; void GpgFrontend::Thread::TaskRunner::PostTask(Task* task) { - LOG(INFO) << "called" - << "Post Task" << task->GetUUID(); + LOG(TRACE) << "Post Task" << task->GetUUID(); if (task == nullptr) return; task->setParent(nullptr); task->moveToThread(this); + + connect(task, &Task::SignalTaskPostFinishedDone, this, [=]() { + auto it = pending_tasks_.find(task->GetUUID()); + if (it == pending_tasks_.end()) { + LOG(ERROR) << "Task" << task->GetUUID() << "not found in pending tasks"; + return; + } else { + LOG(TRACE) << "Task" << task->GetUUID() << "found in pending tasks"; + it->second->deleteLater(); + pending_tasks_.erase(it); + } + }); { std::lock_guard<std::mutex> lock(tasks_mutex_); tasks.push(task); @@ -50,33 +61,43 @@ void GpgFrontend::Thread::TaskRunner::PostTask(Task* task) { } void GpgFrontend::Thread::TaskRunner::run() { - LOG(INFO) << "called" - << "thread id:" << QThread::currentThreadId(); + LOG(TRACE) << "called" + << "thread id:" << QThread::currentThreadId(); while (true) { + LOG(TRACE) << "TaskRunner: A new cycle start"; if (tasks.empty()) { - LOG(INFO) << "TaskRunner: No tasks to run."; + LOG(TRACE) << "TaskRunner: No tasks to run, trapping into event loop..."; exec(); } else { - LOG(INFO) << "TaskRunner: Queue size:" << tasks.size(); + LOG(TRACE) << "TaskRunner: Task queue size:" << tasks.size(); Task* task = nullptr; { std::lock_guard<std::mutex> lock(tasks_mutex_); task = std::move(tasks.front()); tasks.pop(); + pending_tasks_.insert({task->GetUUID(), task}); } if (task != nullptr) { // Run the task - LOG(INFO) << "TaskRunner: Running Task" << task->GetUUID(); + LOG(TRACE) << "TaskRunner: Running Task" << task->GetUUID(); try { task->run(); } catch (const std::exception& e) { LOG(ERROR) << "TaskRunner: Exception in Task" << task->GetUUID() << "Exception: " << e.what(); + + // destroy the task, remove the task from the pending tasks + task->deleteLater(); + pending_tasks_.erase(task->GetUUID()); } catch (...) { LOG(ERROR) << "TaskRunner: Unknwon Exception in Task" << task->GetUUID(); + + // destroy the task, remove the task from the pending tasks + task->deleteLater(); + pending_tasks_.erase(task->GetUUID()); } } } |