diff options
| author | @syxhe <https://t.me/syxhe> | 2025-04-21 17:31:37 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-04-21 17:31:37 -0500 |
| commit | 9cf4667331b97f1123f4156273a46558e27c2d2d (patch) | |
| tree | 091766ab0cb3345bc9e61a2387dc65ca7714569c /src/threadpool.c | |
| parent | d47f45a5e3e40b48131409071b119b442c78bffc (diff) | |
Start work on cqueue implementation, create cleanup set of functions
Diffstat (limited to 'src/threadpool.c')
| -rw-r--r-- | src/threadpool.c | 113 |
1 files changed, 107 insertions, 6 deletions
diff --git a/src/threadpool.c b/src/threadpool.c index d058d52..e230ebb 100644 --- a/src/threadpool.c +++ b/src/threadpool.c | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #include "threadpool.h" | 1 | #include "threadpool.h" |
| 2 | #include "arena.h" | ||
| 2 | #include "shared.h" | 3 | #include "shared.h" |
| 3 | 4 | ||
| 4 | #include "ll-internal.h" | ||
| 5 | #include "ll.h" | 5 | #include "ll.h" |
| 6 | 6 | ||
| 7 | #include <threads.h> | 7 | #include <threads.h> |
| @@ -95,14 +95,18 @@ int thrd_createwmx(thrd_t * const thr, thrd_start_t func, mtxpair * const mtxd) | |||
| 95 | 95 | ||
| 96 | // Here's a good reference of this implemented in C++ using Boost: https://gist.github.com/mikeando/482342 | 96 | // Here's a good reference of this implemented in C++ using Boost: https://gist.github.com/mikeando/482342 |
| 97 | 97 | ||
| 98 | typedef struct cq { | 98 | typedef int (*task_callback)(void*); |
| 99 | void*placehoilder; | ||
| 100 | } cqueue; | ||
| 101 | |||
| 102 | typedef struct task { | 99 | typedef struct task { |
| 103 | int (*callback)(void*); | 100 | task_callback cb; |
| 104 | void *arg; | 101 | void *arg; |
| 105 | } task; | 102 | } task; |
| 103 | |||
| 104 | typedef struct cq { | ||
| 105 | dlinkedlist *list; | ||
| 106 | mtx_t *mutex; | ||
| 107 | cnd_t *conditional; | ||
| 108 | } cqueue; | ||
| 109 | |||
| 106 | typedef struct tp { | 110 | typedef struct tp { |
| 107 | thrd_t **threads; | 111 | thrd_t **threads; |
| 108 | int nthreads; | 112 | int nthreads; |
| @@ -110,3 +114,100 @@ typedef struct tp { | |||
| 110 | cqueue *taskqueue; | 114 | cqueue *taskqueue; |
| 111 | } threadpool; | 115 | } threadpool; |
| 112 | 116 | ||
| 117 | task * task_init(task_callback cb, void *arg) { | ||
| 118 | if(cb == NULL) | ||
| 119 | RETURNWERR(EINVAL, NULL); | ||
| 120 | task *task = VALLOC(1, sizeof(*task)); | ||
| 121 | if(!task) | ||
| 122 | return NULL; | ||
| 123 | |||
| 124 | task->cb = cb; | ||
| 125 | task->arg = arg; | ||
| 126 | |||
| 127 | return task; | ||
| 128 | } | ||
| 129 | |||
| 130 | void task_free(task *ts) { | ||
| 131 | if(!ts) | ||
| 132 | return; | ||
| 133 | |||
| 134 | free(ts); // Not making any assumptions about the data in the task | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | static void ___ucleanup_mtxd(void *mtx) { | ||
| 140 | if(!mtx) | ||
| 141 | return; | ||
| 142 | |||
| 143 | mtx_destroy((mtx_t *)mtx); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | static void ___ucleanup_cndd(void *cnd) { | ||
| 147 | if(!cnd) | ||
| 148 | return; | ||
| 149 | |||
| 150 | cnd_destroy((cnd_t *)cnd); | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | static void ___ucleanup_dll(void *dll) { | ||
| 154 | if(!dll) | ||
| 155 | return; | ||
| 156 | |||
| 157 | dlinkedlist_free((dlinkedlist *)dll); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | cqueue * cqueue_init(int mtx_type) { | ||
| 163 | unsigned char flag = 0; | ||
| 164 | cleanup_create(10); | ||
| 165 | |||
| 166 | cqueue *cq = VALLOC(1, sizeof(*cq)); | ||
| 167 | if(!cq) | ||
| 168 | return NULL; | ||
| 169 | cleanup_register(&__CLEANUP, free, cq); | ||
| 170 | |||
| 171 | cq->mutex = VALLOC(1, sizeof(*(cq->mutex))); | ||
| 172 | if(!(cq->mutex)) | ||
| 173 | flag++; | ||
| 174 | cleanup_cndregister(&__CLEANUP, flag, free, cq->mutex); | ||
| 175 | |||
| 176 | if(!flag && mtx_init(cq->mutex, mtx_type) != thrd_success) | ||
| 177 | flag++; | ||
| 178 | cleanup_cndregister(&__CLEANUP, flag, ___ucleanup_mtxd, cq->mutex); | ||
| 179 | |||
| 180 | if(!flag && !(cq->conditional = VALLOC(1, sizeof(*(cq->conditional))))) | ||
| 181 | flag++; | ||
| 182 | cleanup_cndregister(&__CLEANUP, flag, free, cq->conditional); | ||
| 183 | |||
| 184 | if(!flag && cnd_init(cq->conditional) != thrd_success) | ||
| 185 | flag++; | ||
| 186 | cleanup_cndregister(&__CLEANUP, flag, ___ucleanup_cndd, cq->conditional); | ||
| 187 | |||
| 188 | cq->list = dlinkedlist_init(); | ||
| 189 | if(!flag && !cq->list) | ||
| 190 | flag++; | ||
| 191 | cleanup_cndregister(&__CLEANUP, flag, ___ucleanup_dll, cq->list); | ||
| 192 | |||
| 193 | if(flag) | ||
| 194 | cleanup_fire(&__CLEANUP); | ||
| 195 | |||
| 196 | // This implementation is better and should be far less error prone than the thing I did earlier, but it would be nicer if C had anonymous functions | ||
| 197 | |||
| 198 | return cq; | ||
| 199 | } | ||
| 200 | |||
| 201 | void cqueue_free(cqueue *cq) { | ||
| 202 | if(!cq) | ||
| 203 | return; | ||
| 204 | |||
| 205 | dlinkedlist_free(cq->list); | ||
| 206 | cnd_destroy(cq->conditional); | ||
| 207 | mtx_destroy(cq->mutex); | ||
| 208 | free(cq->conditional); | ||
| 209 | free(cq->mutex); | ||
| 210 | free(cq); | ||
| 211 | |||
| 212 | return; | ||
| 213 | } \ No newline at end of file | ||
