summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c19
-rw-r--r--src/threadpool.c31
-rw-r--r--src/threadpool.h7
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 @@
3#include "encryption.h" 3#include "encryption.h"
4#include "scanner.h" 4#include "scanner.h"
5#include "shared.h" 5#include "shared.h"
6 6#include "threadpool.h"
7 7
8#include <errno.h> 8#include <errno.h>
9#include <error.h> 9#include <error.h>
10 10
11#include <stdio.h>
12int testcb(void *data) {
13 if(!data)
14 return -1;
15
16 printf("%s\n", (char*)data);
17 return 0;
18}
19
11int main() { 20int main() {
12 error(1, ENOTSUP, "No main file lol"); 21 // error(1, ENOTSUP, "No main file lol");
22
23 threadpool *tp = threadpool_init(2);
24 task *tsk = task_init(testcb, "This is some data");
25 threadpool_addtask(tp, tsk);
26 threadpool_free(tp);
27
13 return 0; 28 return 0;
14} \ No newline at end of file 29} \ 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) {
310} 310}
311 311
312int cqueue_waitpop(cqueue * const cq, task **ret) { 312int cqueue_waitpop(cqueue * const cq, task **ret) {
313 if(!cq || !ret || !*ret) 313 if(!cq || !ret)
314 RETURNWERR(EINVAL, -1); 314 RETURNWERR(EINVAL, -1);
315 315
316 mtx_lock(cq->mutex); 316 mtx_lock(cq->mutex);
@@ -348,6 +348,23 @@ int cqueue_cancel(cqueue * const cq) {
348 return retval; 348 return retval;
349} 349}
350 350
351int cqueue_consumer(void *passed) {
352 if(!passed)
353 thrd_exit(thrd_error);
354 // Not setting errno because then I'd have to make a mutex for it
355
356 cqueue *cq = (cqueue *)passed;
357
358 for(task *current_task;;) {
359 cqueue_waitpop(cq, &current_task);
360 if(!current_task)
361 thrd_exit(thrd_error);
362
363 current_task->cb(current_task->arg);
364 }
365
366 thrd_exit(thrd_success);
367}
351 368
352static void ___ucleanup_cqfree(void *cq) { 369static void ___ucleanup_cqfree(void *cq) {
353 if(!cq) 370 if(!cq)
@@ -384,6 +401,10 @@ threadpool * threadpool_init(int threads) {
384 for(int j = 0; j < i; j++) 401 for(int j = 0; j < i; j++)
385 free(tp->threads[j]); 402 free(tp->threads[j]);
386 } 403 }
404
405 if(!cleanup_ERRORFLAGGED)
406 thrd_create(tp->threads[i], cqueue_consumer, tp->taskqueue);
407 // TODO: Error Checking ^
387 } 408 }
388 409
389 if(cleanup_ERRORFLAGGED) 410 if(cleanup_ERRORFLAGGED)
@@ -404,8 +425,14 @@ void threadpool_free(threadpool *tp) {
404 free(tp->threads[i]); 425 free(tp->threads[i]);
405 } 426 }
406 free(tp->threads); 427 free(tp->threads);
407 cqueue_free(tp->taskqueue);
408 free(tp); 428 free(tp);
409 429
410 return; 430 return;
431}
432
433int threadpool_addtask(threadpool * const tp, task * const task) {
434 if(!tp || !task)
435 RETURNWERR(EINVAL, -1);
436
437 return cqueue_append(tp->taskqueue, task);
411} \ No newline at end of file 438} \ 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);
22int cqueue_waitpop(cqueue * const cq, task **ret); 22int cqueue_waitpop(cqueue * const cq, task **ret);
23int cqueue_cancel(cqueue * const cq); 23int cqueue_cancel(cqueue * const cq);
24 24
25threadpool * threadpool_init(int threads);
26void threadpool_free(threadpool *tp);
27int threadpool_addtask(threadpool * const tp, task * const task);
28
25typedef struct mtxp mtxpair; 29typedef struct mtxp mtxpair;
26mtxpair * mtxpair_init(void * const data, int type); 30mtxpair * mtxpair_init(void * const data, int type);
27void mtxpair_free(mtxpair *mp); 31void mtxpair_free(mtxpair *mp);
28int mtxpair_setdata(mtxpair * const mp, void * const data); 32int mtxpair_setdata(mtxpair * const mp, void * const data);
29
30int thrd_createwmx(thrd_t * const thr, thrd_start_t func, mtxpair * const mtxd); 33int thrd_createwmx(thrd_t * const thr, thrd_start_t func, mtxpair * const mtxd);
31 34
32int cqueue_cancel(cqueue * const cq);
33
34#endif \ No newline at end of file 35#endif \ No newline at end of file