summaryrefslogtreecommitdiff
path: root/src/threadpool.c
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-11-10 16:09:10 -0600
committer@syxhe <https://t.me/syxhe>2025-11-10 16:09:10 -0600
commitc8ab26e9f5aa398a208fcac3d8d11335f6c72632 (patch)
tree2dc2c0f18e98b71c02991ea5511cdcb0039e346b /src/threadpool.c
parent0c541f74d346625618dc3ea8974bfb2cf042c000 (diff)
TouchupHEADmaster
Diffstat (limited to 'src/threadpool.c')
-rw-r--r--src/threadpool.c34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/threadpool.c b/src/threadpool.c
index 959c060..7a2f93a 100644
--- a/src/threadpool.c
+++ b/src/threadpool.c
@@ -182,14 +182,15 @@ taskqueue * taskqueue_new(void) {
182 * @param tq A taskqueue to be freed. May be null 182 * @param tq A taskqueue to be freed. May be null
183 */ 183 */
184void taskqueue_free(void *tq) { 184void taskqueue_free(void *tq) {
185 if(!tq) return; 185 taskqueue *real = tq;
186 if(!real) return;
186 187
187 for(tqnode *p = ((taskqueue*)tq)->start, *n; p != NULL;) { 188 for(tqnode *p = real->start, *n; p != NULL;) {
188 n = p->next; 189 n = p->next;
189 tqnode_free(p); 190 tqnode_free(p);
190 p = n; 191 p = n;
191 } 192 }
192 free(tq); 193 free(real);
193 194
194 return; 195 return;
195} 196}
@@ -221,7 +222,8 @@ int taskqueue_push(taskqueue *tq, task *tsk) {
221 int hf; 222 int hf;
222 if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1; 223 if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1;
223 224
224 tqnode *curstart = tq->start, *newstart = tqnode_new(tq->start, NULL, tsk); 225 tqnode *curstart = tq->start;
226 tqnode *newstart = tqnode_new(curstart, NULL, tsk);
225 if(!newstart) return -1; 227 if(!newstart) return -1;
226 228
227 curstart->prev = newstart; 229 curstart->prev = newstart;
@@ -241,18 +243,18 @@ task * taskqueue_pop(taskqueue *tq) {
241 if(!tq) ERRRET(EINVAL, NULL); 243 if(!tq) ERRRET(EINVAL, NULL);
242 if(tq->size <= 0) ERRRET(ENODATA, NULL); 244 if(tq->size <= 0) ERRRET(ENODATA, NULL);
243 245
244 tqnode *end = tq->end; 246 tqnode *curend = tq->end;
245 task *ret = end->task; 247 task *ret = curend->task;
246 248
247 if(tq->size == 1) { 249 if(tq->size == 1) {
248 tq->end = NULL; 250 tq->end = NULL;
249 tq->start = NULL; 251 tq->start = NULL;
250 } else { 252 } else {
251 tq->end = end->prev; 253 tq->end = curend->prev;
252 tq->end->next = NULL; 254 tq->end->next = NULL;
253 } 255 }
254 256
255 free(end); 257 free(curend);
256 tq->size--; 258 tq->size--;
257 return ret; 259 return ret;
258} 260}
@@ -270,7 +272,8 @@ int taskqueue_pushfront(taskqueue *tq, task *tsk) {
270 int hf; 272 int hf;
271 if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1; 273 if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1;
272 274
273 tqnode *end =tq->end, *newend = tqnode_new(NULL, tq->end, tsk); 275 tqnode *end = tq->end;
276 tqnode *newend = tqnode_new(NULL, end, tsk);
274 if(!newend) return -1; 277 if(!newend) return -1;
275 278
276 end->next = newend; 279 end->next = newend;
@@ -290,18 +293,18 @@ task * taskqueue_popback(taskqueue *tq) {
290 if(!tq) ERRRET(EINVAL, NULL); 293 if(!tq) ERRRET(EINVAL, NULL);
291 if(tq->size <= 0) ERRRET(ENODATA, NULL); 294 if(tq->size <= 0) ERRRET(ENODATA, NULL);
292 295
293 tqnode *start = tq->start; 296 tqnode *curstart = tq->start;
294 task *ret = start->task; 297 task *ret = curstart->task;
295 298
296 if(tq->size == 1) { 299 if(tq->size == 1) {
297 tq->start = NULL; 300 tq->start = NULL;
298 tq->end = NULL; 301 tq->end = NULL;
299 } else { 302 } else {
300 tq->start = start->next; 303 tq->start = curstart->next;
301 tq->start->prev = NULL; 304 tq->start->prev = NULL;
302 } 305 }
303 306
304 free(start); 307 free(curstart);
305 tq->size--; 308 tq->size--;
306 return ret; 309 return ret;
307} 310}
@@ -321,7 +324,6 @@ int taskqueue_size(taskqueue *tq) {
321 mtx_unlock(&(ctq)->mutex); \ 324 mtx_unlock(&(ctq)->mutex); \
322 return (retval); \ 325 return (retval); \
323 } \ 326 } \
324 \
325 code \ 327 code \
326 mtx_unlock(&(ctq)->mutex); \ 328 mtx_unlock(&(ctq)->mutex); \
327} while (0) 329} while (0)
@@ -413,8 +415,6 @@ void ctqueue_free(void *ctq) {
413 free(real->thrdarr); 415 free(real->thrdarr);
414 free(real); 416 free(real);
415 417
416 // TODO: figure out if it's necessary / a good idea to do error handling on these functions
417
418 return; 418 return;
419} 419}
420 420
@@ -424,7 +424,7 @@ void ctqueue_free(void *ctq) {
424 * 424 *
425 * @param ctq The concurrent taskqueue to modify. Must be non-null 425 * @param ctq The concurrent taskqueue to modify. Must be non-null
426 * @param tsk The task to push. Must be non-null 426 * @param tsk The task to push. Must be non-null
427 * @retval (int) Returns `thrd_success` on success, returns `thrd_error` or `thrd_nomem` on error 427 * @retval (int)[`thrd_error` | `thrd_nomem`, `thrd_success`]
428 */ 428 */
429int ctqueue_waitpush(ctqueue *ctq, task *tsk) { 429int ctqueue_waitpush(ctqueue *ctq, task *tsk) {
430 if(!ctq || !tsk) ERRRET(EINVAL, -1); 430 if(!ctq || !tsk) ERRRET(EINVAL, -1);