From 16528ac295215e788cb226f0cc49f11f82919741 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 6 Jun 2025 13:30:34 -0500 Subject: Get threadpool implementation working --- src/shared.c | 127 +++++++++++++++++++---------------------------------------- 1 file changed, 40 insertions(+), 87 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index 02fe9a0..1d5aa7f 100644 --- a/src/shared.c +++ b/src/shared.c @@ -224,108 +224,61 @@ char * xdirname(const char * const path) { } -int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]) { - if(!loc) - RETURNWERR(EINVAL, -1); - if(size < 1) - RETURNWERR(EINVAL, -1); - if(!funcs) - RETURNWERR(EINVAL, -1); - if(!args) - RETURNWERR(EINVAL, -1); - - loc->funcs = funcs; - loc->args = args; - loc->size = size; - loc->used = 0; - - return 0; -} - -int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg) { - if(!loc) - RETURNWERR(EINVAL, -1); - if(loc->used >= loc->size) - RETURNWERR(ENOMEM, -1); - if(!cb) - RETURNWERR(EINVAL, -1); +int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) { + if(!loc || !callbacks || !arguments || size <= 0) {errno = EINVAL; return -1;} - loc->funcs[loc->used] = cb; - loc->args[loc->used] = arg; - loc->used++; + loc->callbacks = callbacks; + loc->arguments = arguments; + loc->size = size; + loc->used = 0; - return 0; + return 0; } -int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg) { - if(flag) - return 0; - - if(!loc) - RETURNWERR(EINVAL, -1); - if(loc->used >= loc->size) - RETURNWERR(ENOMEM, -1); - if(!cb) - RETURNWERR(EINVAL, -1); +// registers if flag is NOT set +int cleanup_register(cleanup *loc, fcallback cb, void *arg) { + if(!loc || !cb) {errno = EINVAL; return -1;} + if(loc->used >= loc->size || loc->used < 0) {errno = ENOMEM; return -1;} - loc->funcs[loc->used] = cb; - loc->args[loc->used] = arg; - loc->used++; + loc->callbacks[loc->used] = cb; + loc->arguments[loc->used] = arg; + loc->used++; - return 0; + return 0; } -int cleanup_clear(cleanup * const loc) { - if(!loc) - RETURNWERR(EINVAL, -1); - - loc->used = 0; - return 0; +int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) { + if(flag) + return 0; + return cleanup_register(loc, cb, arg); } -cleanup_callback cleanup_peekf(cleanup * const loc) { - if(!loc) - RETURNWERR(EINVAL, NULL); - if(loc->used == 0) - RETURNWERR(ENODATA, NULL); - - return loc->funcs[loc->used - 1]; +int cleanup_clear(cleanup *loc) { + if(!loc) {errno = EINVAL; return -1;} + + loc->used = 0; + return 0; } -cleanup_callback cleanup_popf(cleanup * const loc) { - cleanup_callback cb = cleanup_peekf(loc); - if(cb == NULL) - RETURNWERR(EINVAL, NULL); - loc->used--; - - return cb; -} +int cleanup_fire(cleanup *loc) { + if(!loc) {errno = EINVAL; return -1;} -void * cleanup_peeka(cleanup * const loc) { - if(!loc) - RETURNWERR(EINVAL, NULL); - if(loc->used == 0) - RETURNWERR(ENODATA, NULL); + for(int i = (loc->used - 1); i >= 0; i--) { + if(loc->callbacks[i] == NULL) { + error(0, EINVAL, "cleanup_fire: refusing to run null callback..."); + continue; + } - return loc->args[loc->used - 1]; -} -void * cleanup_popa(cleanup * const loc) { - void *mem = cleanup_peeka(loc); - if(!mem) - RETURNWERR(EINVAL, NULL); - - loc->used--; + loc->callbacks[i](loc->arguments[i]); + } + cleanup_clear(loc); - return mem; + return 0; } -int cleanup_fire(cleanup * const loc) { - if(!loc) - RETURNWERR(EINVAL, -1); - - for(int i = (loc->used - 1); i >= 0; i--) - loc->funcs[i](loc->args[i]); - loc->used = 0; - - return 0; +// Fires if flag is set +int cleanup_cndfire(cleanup *loc, unsigned char flag) { + if(flag) + return cleanup_fire(loc); + return 0; } \ No newline at end of file -- cgit v1.2.3