aboutsummaryrefslogtreecommitdiff
path: root/src/qt
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2022-08-30 22:55:26 +0200
committerJan200101 <sentrycraft123@gmail.com>2022-08-30 22:55:26 +0200
commit37c4373dce60b31ccc0100d85d0013aef82809e7 (patch)
tree5779a0f437ea1a88b91a4a9e6a3c879d216c9d50 /src/qt
parent91f695797fac3e1c2d20bea70f8c877d7df03b68 (diff)
downloadOFQT-37c4373dce60b31ccc0100d85d0013aef82809e7.tar.gz
OFQT-37c4373dce60b31ccc0100d85d0013aef82809e7.zip
implement thread pools
Diffstat (limited to 'src/qt')
-rw-r--r--src/qt/CMakeLists.txt5
-rw-r--r--src/qt/workers.cpp65
-rw-r--r--src/qt/workers.hpp4
3 files changed, 31 insertions, 43 deletions
diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt
index 3702ad5..4a4caf3 100644
--- a/src/qt/CMakeLists.txt
+++ b/src/qt/CMakeLists.txt
@@ -28,13 +28,10 @@ if(WIN32)
list(APPEND QT_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
endif()
-set(THREADS_PREFER_PTHREAD_FLAG ON)
-find_package(Threads REQUIRED)
-
add_executable(${FRONTEND_NAME} WIN32 ${QT_SOURCES})
target_compile_options(${FRONTEND_NAME} PUBLIC ${CFLAGS})
target_link_libraries(${FRONTEND_NAME} PRIVATE tvn)
-target_link_libraries(${FRONTEND_NAME} PRIVATE Threads::Threads)
+target_link_libraries(${FRONTEND_NAME} PRIVATE threading)
target_link_libraries(${FRONTEND_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
set_property(TARGET ${FRONTEND_NAME} PROPERTY CXX_STANDARD 11)
install(TARGETS ${FRONTEND_NAME})
diff --git a/src/qt/workers.cpp b/src/qt/workers.cpp
index 5c20f0e..e4faa74 100644
--- a/src/qt/workers.cpp
+++ b/src/qt/workers.cpp
@@ -5,20 +5,19 @@
#include "net.h"
#include "steam.h"
#include "toast.h"
+#include "pool.h"
#include "./ui_mainwindow.h"
#include "workers.hpp"
-#define THREAD_COUNT 4
-
struct thread_object_info {
- int working;
-
QString infoText;
char* of_dir;
char* remote;
struct revision_t* rev;
size_t index;
+
+ Worker* worker;
};
static void* thread_download(void* pinfo)
@@ -41,11 +40,21 @@ static void* thread_download(void* pinfo)
downloadObject(of_dir, remote, file);
}
}
- }
- info->working = 0;
- pthread_exit(0);
+ QString* threadString = &info->infoText;
+ if (!threadString->isEmpty())
+ {
+ pthread_mutex_lock(&info->worker->textMutex);
+ // allow the main thread to clear the string before we continue
+ while (!info->worker->infoText.isEmpty()) {};
+
+ info->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);
+ }
+ }
return NULL;
}
@@ -54,6 +63,7 @@ Worker::Worker()
net_init();
of_dir = NULL;
remote = NULL;
+ textMutex = PTHREAD_MUTEX_INITIALIZER;
}
Worker::~Worker()
@@ -143,47 +153,26 @@ int Worker::update_setup(int local_rev, int remote_rev)
}
}
- pthread_t download_threads[THREAD_COUNT] = {0};
- struct thread_object_info thread_info[THREAD_COUNT] = {0, NULL, NULL, NULL, NULL, 0};
- size_t tindex = 0;
- QString infoStrings[THREAD_COUNT];
+ struct thread_object_info* thread_info = new struct thread_object_info[rev->file_count];
+ struct pool_t* pool = pool_init();
+ pool->condition = &do_work;
- for (size_t i = 0; i < rev->file_count && do_work; ++i)
+ for (size_t i = 0; i < rev->file_count; ++i)
{
- while (thread_info[tindex].working)
- {
- tindex = (tindex+1) % THREAD_COUNT;
- }
-
- pthread_t* thread = &download_threads[tindex];
- struct thread_object_info* info = &thread_info[tindex];
-
- QString* threadString = &info->infoText;
- if (!threadString->isEmpty())
- {
- infoText = *threadString;
- emit resultReady(RESULT_UPDATE_TEXT);
+ struct thread_object_info* info = &thread_info[i];
- // allow the main thread to clear the string before we continue
- while (!infoText.isEmpty() && do_work) {};
- }
-
- info->working = 1;
info->of_dir = of_dir;
info->remote = remote;
info->rev = rev;
info->index = i;
- progress = (int)(((i * 100) + 1) / rev->file_count);
+ info->worker = this;
- pthread_create(thread, NULL, thread_download, info);
+ pool_submit(pool, thread_download, info);
}
- for (size_t i = 0; i < THREAD_COUNT; ++i)
- {
- pthread_t* thread = &download_threads[i];
- if (*thread)
- pthread_join(*thread, NULL);
- }
+ pool_complete(pool);
+ pool_free(pool);
+ delete[] thread_info;
progress = 0;
infoText = QString("Processing");
diff --git a/src/qt/workers.hpp b/src/qt/workers.hpp
index 506d7ce..8e2a274 100644
--- a/src/qt/workers.hpp
+++ b/src/qt/workers.hpp
@@ -4,6 +4,7 @@
#include <QObject>
#include <QSettings>
#include <limits.h>
+#include <pthread.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@@ -20,7 +21,7 @@ private:
char* remote;
size_t remote_len;
- bool do_work = true;
+ int do_work = 1;
bool update_in_progress = false;
QSettings settings;
@@ -28,6 +29,7 @@ private:
public:
int progress = -1;
QString infoText;
+ pthread_mutex_t textMutex;
Worker();
~Worker();