From 37c4373dce60b31ccc0100d85d0013aef82809e7 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 30 Aug 2022 22:55:26 +0200 Subject: implement thread pools --- src/cli/CMakeLists.txt | 5 +---- src/cli/commands.c | 2 ++ src/cli/updater.c | 35 ++++++++--------------------------- 3 files changed, 11 insertions(+), 31 deletions(-) (limited to 'src/cli') diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index 2947a5b..fcb386b 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -8,11 +8,8 @@ SET(CLI_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/updater.h ) -set(THREADS_PREFER_PTHREAD_FLAG ON) -find_package(Threads REQUIRED) - add_executable(${FRONTEND_NAME} ${CLI_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) install(TARGETS ${FRONTEND_NAME}) diff --git a/src/cli/commands.c b/src/cli/commands.c index 85b05c2..30d3bca 100644 --- a/src/cli/commands.c +++ b/src/cli/commands.c @@ -16,6 +16,8 @@ static int run(int, char**); static int version(int, char**); static int info(int, char**); +#include "pool.h" + const struct Command commands[] = { { .name = "install", .func = install, .description = "Install OpenFortress"}, { .name = "update", .func = update, .description = "Update an existing install"}, diff --git a/src/cli/updater.c b/src/cli/updater.c index 9dcd9da..2aaa8f1 100644 --- a/src/cli/updater.c +++ b/src/cli/updater.c @@ -1,20 +1,16 @@ #include #include #include -#include #include "fs.h" #include "toast.h" +#include "pool.h" #include "updater.h" #include -#define THREAD_COUNT 4 - struct thread_object_info { - int working; - char* of_dir; char* remote; struct revision_t* rev; @@ -43,10 +39,6 @@ static void* thread_download(void* pinfo) } } } - - info->working = 0; - pthread_exit(0); - return NULL; } @@ -70,35 +62,24 @@ void update_setup(char* of_dir, char* remote, int local_rev, int remote_rev) } } - pthread_t download_threads[THREAD_COUNT] = {0}; - struct thread_object_info thread_info[THREAD_COUNT] = {0}; - size_t tindex = 0; + struct thread_object_info* thread_info = malloc(sizeof(struct thread_object_info) * rev->file_count); + struct pool_t* pool = pool_init(); 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]; + struct thread_object_info* info = &thread_info[i]; - info->working = 1; info->of_dir = of_dir; info->remote = remote; info->rev = rev; info->index = i; - 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); + free(thread_info); for (size_t i = 0; i < rev->file_count; ++i) { -- cgit v1.2.3