summaryrefslogtreecommitdiff
path: root/src/shared.c
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-06-06 13:30:34 -0500
committer@syxhe <https://t.me/syxhe>2025-06-06 13:30:34 -0500
commit16528ac295215e788cb226f0cc49f11f82919741 (patch)
tree8bbe621a58a421b894330205bf07285e80e40e9e /src/shared.c
parent8fe1a7ea459829145dfef4b0aa8f627f96841cbd (diff)
Get threadpool implementation workingthreadpool-debugging
Diffstat (limited to 'src/shared.c')
-rw-r--r--src/shared.c127
1 files changed, 40 insertions, 87 deletions
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) {
224} 224}
225 225
226 226
227int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]) { 227int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) {
228 if(!loc) 228 if(!loc || !callbacks || !arguments || size <= 0) {errno = EINVAL; return -1;}
229 RETURNWERR(EINVAL, -1);
230 if(size < 1)
231 RETURNWERR(EINVAL, -1);
232 if(!funcs)
233 RETURNWERR(EINVAL, -1);
234 if(!args)
235 RETURNWERR(EINVAL, -1);
236
237 loc->funcs = funcs;
238 loc->args = args;
239 loc->size = size;
240 loc->used = 0;
241
242 return 0;
243}
244
245int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg) {
246 if(!loc)
247 RETURNWERR(EINVAL, -1);
248 if(loc->used >= loc->size)
249 RETURNWERR(ENOMEM, -1);
250 if(!cb)
251 RETURNWERR(EINVAL, -1);
252 229
253 loc->funcs[loc->used] = cb; 230 loc->callbacks = callbacks;
254 loc->args[loc->used] = arg; 231 loc->arguments = arguments;
255 loc->used++; 232 loc->size = size;
233 loc->used = 0;
256 234
257 return 0; 235 return 0;
258} 236}
259 237
260int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg) { 238// registers if flag is NOT set
261 if(flag) 239int cleanup_register(cleanup *loc, fcallback cb, void *arg) {
262 return 0; 240 if(!loc || !cb) {errno = EINVAL; return -1;}
263 241 if(loc->used >= loc->size || loc->used < 0) {errno = ENOMEM; return -1;}
264 if(!loc)
265 RETURNWERR(EINVAL, -1);
266 if(loc->used >= loc->size)
267 RETURNWERR(ENOMEM, -1);
268 if(!cb)
269 RETURNWERR(EINVAL, -1);
270 242
271 loc->funcs[loc->used] = cb; 243 loc->callbacks[loc->used] = cb;
272 loc->args[loc->used] = arg; 244 loc->arguments[loc->used] = arg;
273 loc->used++; 245 loc->used++;
274 246
275 return 0; 247 return 0;
276} 248}
277 249
278int cleanup_clear(cleanup * const loc) { 250int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) {
279 if(!loc) 251 if(flag)
280 RETURNWERR(EINVAL, -1); 252 return 0;
281 253 return cleanup_register(loc, cb, arg);
282 loc->used = 0;
283 return 0;
284} 254}
285 255
286cleanup_callback cleanup_peekf(cleanup * const loc) { 256int cleanup_clear(cleanup *loc) {
287 if(!loc) 257 if(!loc) {errno = EINVAL; return -1;}
288 RETURNWERR(EINVAL, NULL); 258
289 if(loc->used == 0) 259 loc->used = 0;
290 RETURNWERR(ENODATA, NULL); 260 return 0;
291
292 return loc->funcs[loc->used - 1];
293} 261}
294cleanup_callback cleanup_popf(cleanup * const loc) {
295 cleanup_callback cb = cleanup_peekf(loc);
296 if(cb == NULL)
297 RETURNWERR(EINVAL, NULL);
298 262
299 loc->used--; 263int cleanup_fire(cleanup *loc) {
300 264 if(!loc) {errno = EINVAL; return -1;}
301 return cb;
302}
303 265
304void * cleanup_peeka(cleanup * const loc) { 266 for(int i = (loc->used - 1); i >= 0; i--) {
305 if(!loc) 267 if(loc->callbacks[i] == NULL) {
306 RETURNWERR(EINVAL, NULL); 268 error(0, EINVAL, "cleanup_fire: refusing to run null callback...");
307 if(loc->used == 0) 269 continue;
308 RETURNWERR(ENODATA, NULL); 270 }
309 271
310 return loc->args[loc->used - 1]; 272 loc->callbacks[i](loc->arguments[i]);
311} 273 }
312void * cleanup_popa(cleanup * const loc) { 274 cleanup_clear(loc);
313 void *mem = cleanup_peeka(loc);
314 if(!mem)
315 RETURNWERR(EINVAL, NULL);
316
317 loc->used--;
318 275
319 return mem; 276 return 0;
320} 277}
321 278
322int cleanup_fire(cleanup * const loc) { 279// Fires if flag is set
323 if(!loc) 280int cleanup_cndfire(cleanup *loc, unsigned char flag) {
324 RETURNWERR(EINVAL, -1); 281 if(flag)
325 282 return cleanup_fire(loc);
326 for(int i = (loc->used - 1); i >= 0; i--) 283 return 0;
327 loc->funcs[i](loc->args[i]);
328 loc->used = 0;
329
330 return 0;
331} \ No newline at end of file 284} \ No newline at end of file