diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-08-30 22:55:26 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-08-30 22:55:26 +0200 |
commit | 37c4373dce60b31ccc0100d85d0013aef82809e7 (patch) | |
tree | 5779a0f437ea1a88b91a4a9e6a3c879d216c9d50 /src/cli | |
parent | 91f695797fac3e1c2d20bea70f8c877d7df03b68 (diff) | |
download | OFQT-37c4373dce60b31ccc0100d85d0013aef82809e7.tar.gz OFQT-37c4373dce60b31ccc0100d85d0013aef82809e7.zip |
implement thread pools
Diffstat (limited to 'src/cli')
-rw-r--r-- | src/cli/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/cli/commands.c | 2 | ||||
-rw-r--r-- | src/cli/updater.c | 35 |
3 files changed, 11 insertions, 31 deletions
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 <stdlib.h> #include <stdio.h> #include <string.h> -#include <pthread.h> #include "fs.h" #include "toast.h" +#include "pool.h" #include "updater.h" #include <assert.h> -#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) { |