From 833b16d109444ed15643246f1f419d6e96f101c3 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Fri, 2 Sep 2022 18:19:53 +0200 Subject: stop threads when condition is reached, gracefully deal with status text --- src/qt/mainwindow.cpp | 3 ++- src/qt/workers.cpp | 25 ++++++++++++++----------- src/qt/workers.hpp | 2 +- src/threading/pool.c | 11 ++++++----- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/qt/mainwindow.cpp b/src/qt/mainwindow.cpp index 5bf013d..84d6ee9 100644 --- a/src/qt/mainwindow.cpp +++ b/src/qt/mainwindow.cpp @@ -98,6 +98,8 @@ void MainWindow::workerResult(const enum Worker::Results_t& result) case Worker::RESULT_IS_INSTALLED: installed = true; + ui->mainButton->setText("Play"); + ui->statusLabel->setText("Checking for updates..."); workerOperate(Worker::TASK_IS_UPTODATE); break; @@ -108,7 +110,6 @@ void MainWindow::workerResult(const enum Worker::Results_t& result) case Worker::RESULT_IS_UPTODATE: uptodate = true; - ui->mainButton->setText("Play"); ui->statusLabel->setText("Up to Date"); break; diff --git a/src/qt/workers.cpp b/src/qt/workers.cpp index e4faa74..a54509f 100644 --- a/src/qt/workers.cpp +++ b/src/qt/workers.cpp @@ -23,7 +23,10 @@ struct thread_object_info { static void* thread_download(void* pinfo) { struct thread_object_info* info = (struct thread_object_info*)pinfo; - if (info) + Worker* worker = info->worker; + int* do_work = &worker->do_work; + + if (info && *do_work) { char* of_dir = info->of_dir; char* remote = info->remote; @@ -31,10 +34,10 @@ static void* thread_download(void* pinfo) size_t i = info->index; struct file_info* file = &rev->files[i]; - if (file->type == TYPE_WRITE) + if (file->type == TYPE_WRITE && *do_work) { info->infoText = QString("Verifying %1").arg(file->object); - if (verifyFileHash(of_dir, file)) + if (verifyFileHash(of_dir, file) && *do_work) { info->infoText = QString("Downloading %1").arg(file->object); downloadObject(of_dir, remote, file); @@ -42,17 +45,17 @@ static void* thread_download(void* pinfo) } QString* threadString = &info->infoText; - if (!threadString->isEmpty()) + if (!threadString->isEmpty() && *do_work) { - pthread_mutex_lock(&info->worker->textMutex); + pthread_mutex_lock(&worker->textMutex); // allow the main thread to clear the string before we continue - while (!info->worker->infoText.isEmpty()) {}; + while (!worker->infoText.isEmpty() && *do_work) {}; - info->worker->progress = (int)(((info->index * 100) + 1) / rev->file_count); + worker->progress = (int)(((info->index * 100) + 1) / rev->file_count); - info->worker->infoText = *threadString; - emit info->worker->resultReady(Worker::RESULT_UPDATE_TEXT); - pthread_mutex_unlock(&info->worker->textMutex); + worker->infoText = *threadString; + emit worker->resultReady(Worker::RESULT_UPDATE_TEXT); + pthread_mutex_unlock(&worker->textMutex); } } return NULL; @@ -123,7 +126,7 @@ bool Worker::isOutdated() void Worker::stop_work() { - do_work = false; + do_work = 0; } int Worker::update_setup(int local_rev, int remote_rev) diff --git a/src/qt/workers.hpp b/src/qt/workers.hpp index 8e2a274..19d83f5 100644 --- a/src/qt/workers.hpp +++ b/src/qt/workers.hpp @@ -21,12 +21,12 @@ private: char* remote; size_t remote_len; - int do_work = 1; bool update_in_progress = false; QSettings settings; public: + int do_work = 1; int progress = -1; QString infoText; pthread_mutex_t textMutex; diff --git a/src/threading/pool.c b/src/threading/pool.c index 266254e..1598852 100644 --- a/src/threading/pool.c +++ b/src/threading/pool.c @@ -9,6 +9,7 @@ struct worker_t { struct pool_t* pool; int id; + int done; }; struct pool_t* pool_init() @@ -51,19 +52,17 @@ static void* task_executor(void* pinfo) { struct pool_task_t* pool_end = pool->tasks + pool->pool_size; struct pool_task_t* task = pool->task_next++; - while (pool_end > task) + while (pool_end > task && (pool->condition == NULL || *pool->condition)) { if (!task->done) { task->func(task->arg); task->done = 1; } - task = pool->task_next++; } } - - return NULL; + worker->done = 1; } void pool_complete(struct pool_t* pool) @@ -81,13 +80,15 @@ void pool_complete(struct pool_t* pool) worker->pool = pool; worker->id = i; + worker->done = 0; pthread_create(thread, NULL, task_executor, worker); } for (int i = 0; i < pool->workers; ++i) { - pthread_join(threads[i], NULL); + if (!workers[i].done) + pthread_join(threads[i], NULL); } free(threads); -- cgit v1.2.3