From 39c3fa785cafd5fa9b75bfbf92d7a702310ba480 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 22 Apr 2025 16:59:33 -0500 Subject: Yay segfault --- src/main.c | 19 +++++++++++++++++-- src/threadpool.c | 31 +++++++++++++++++++++++++++++-- src/threadpool.h | 7 ++++--- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 2fcf8ab..f89a124 100644 --- a/src/main.c +++ b/src/main.c @@ -3,12 +3,27 @@ #include "encryption.h" #include "scanner.h" #include "shared.h" - +#include "threadpool.h" #include #include +#include +int testcb(void *data) { + if(!data) + return -1; + + printf("%s\n", (char*)data); + return 0; +} + int main() { - error(1, ENOTSUP, "No main file lol"); + // error(1, ENOTSUP, "No main file lol"); + + threadpool *tp = threadpool_init(2); + task *tsk = task_init(testcb, "This is some data"); + threadpool_addtask(tp, tsk); + threadpool_free(tp); + return 0; } \ No newline at end of file diff --git a/src/threadpool.c b/src/threadpool.c index 66c0d06..31c300c 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -310,7 +310,7 @@ int cqueue_trypop(cqueue * const cq, task **ret) { } int cqueue_waitpop(cqueue * const cq, task **ret) { - if(!cq || !ret || !*ret) + if(!cq || !ret) RETURNWERR(EINVAL, -1); mtx_lock(cq->mutex); @@ -348,6 +348,23 @@ int cqueue_cancel(cqueue * const cq) { return retval; } +int cqueue_consumer(void *passed) { + if(!passed) + thrd_exit(thrd_error); + // Not setting errno because then I'd have to make a mutex for it + + cqueue *cq = (cqueue *)passed; + + for(task *current_task;;) { + cqueue_waitpop(cq, ¤t_task); + if(!current_task) + thrd_exit(thrd_error); + + current_task->cb(current_task->arg); + } + + thrd_exit(thrd_success); +} static void ___ucleanup_cqfree(void *cq) { if(!cq) @@ -384,6 +401,10 @@ threadpool * threadpool_init(int threads) { for(int j = 0; j < i; j++) free(tp->threads[j]); } + + if(!cleanup_ERRORFLAGGED) + thrd_create(tp->threads[i], cqueue_consumer, tp->taskqueue); + // TODO: Error Checking ^ } if(cleanup_ERRORFLAGGED) @@ -404,8 +425,14 @@ void threadpool_free(threadpool *tp) { free(tp->threads[i]); } free(tp->threads); - cqueue_free(tp->taskqueue); free(tp); return; +} + +int threadpool_addtask(threadpool * const tp, task * const task) { + if(!tp || !task) + RETURNWERR(EINVAL, -1); + + return cqueue_append(tp->taskqueue, task); } \ No newline at end of file diff --git a/src/threadpool.h b/src/threadpool.h index c28f03a..48ad9eb 100644 --- a/src/threadpool.h +++ b/src/threadpool.h @@ -22,13 +22,14 @@ int cqueue_trypop(cqueue * const cq, task **ret); int cqueue_waitpop(cqueue * const cq, task **ret); int cqueue_cancel(cqueue * const cq); +threadpool * threadpool_init(int threads); +void threadpool_free(threadpool *tp); +int threadpool_addtask(threadpool * const tp, task * const task); + typedef struct mtxp mtxpair; mtxpair * mtxpair_init(void * const data, int type); void mtxpair_free(mtxpair *mp); int mtxpair_setdata(mtxpair * const mp, void * const data); - int thrd_createwmx(thrd_t * const thr, thrd_start_t func, mtxpair * const mtxd); -int cqueue_cancel(cqueue * const cq); - #endif \ No newline at end of file -- cgit v1.2.3