From 0ee5044805c8d157d5023fc1322f980e3a480df7 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 22 Apr 2025 16:35:44 -0500 Subject: Start work on threadpool implementation --- src/shared.h | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src/shared.h') diff --git a/src/shared.h b/src/shared.h index 7034255..9e7eaa8 100644 --- a/src/shared.h +++ b/src/shared.h @@ -69,14 +69,31 @@ typedef struct cl { int used; } cleanup; +// Initialize a local cleanup stack. `loc`, `funcs` and `args` need to be locally defined, non allocated arrays, and must be at least `size` elements large int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); + +// Register a cleanup callback for a given cleanup object int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); + +// Register a cleanup callback, if and only if `flag == 0` int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg); + +// Clear the contents of a cleanup stack int cleanup_clear(cleanup * const loc); + +// Get the top callback without removing it from the cleanup stack cleanup_callback cleanup_peekf(cleanup * const loc); + +// Get and remove the top callback from the cleanup stack. Does not return the argument for the given callback cleanup_callback cleanup_popf(cleanup * const loc); + +// Get the top argument without removing it from the cleanup stack void * cleanup_peeka(cleanup * const loc); + +// Get and remove the top argument from the cleanup stack. Does not return the callback it was to be fed into void * cleanup_popa(cleanup * const loc); + +// Fire all the callbacks in the cleanup stack int cleanup_fire(cleanup * const loc); /* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes @@ -85,9 +102,19 @@ int cleanup_fire(cleanup * const loc); cleanup __CLEANUP; \ cleanup_callback __CLEANUP_FUNCS[(size)]; \ void *__CLEANUP_ARGS[(size)]; \ +unsigned char __FLAG = 0; \ cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS) -#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) -#define cleanup_CNDREGISTER(flag, cb, arg) cleanup_cndregister(&__CLEANUP, (flag), (cb), (arg)) +#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) +#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, __FLAG, (cb), (arg)) +#define cleanup_CLEAR() cleanup_clear(&__CLEANUP) +#define cleanup_PEEKF() cleanup_peekf(&__CLEANUP) +#define cleanup_POPF() cleanup_popf(&__CLEANUP) +#define cleanup_PEEKA() cleanup_peeka(&__CLEANUP) +#define cleanup_POPA() cleanup_popa(&__CLEANUP) +#define cleanup_FIRE() cleanup_fire(&__CLEANUP) +#define cleanup_MARK() (__FLAG = 1) +#define cleanup_UNMARK() (__FLAG = 0) +#define cleanup_ERRORFLAGGED (__FLAG != 0) #endif \ No newline at end of file -- cgit v1.2.3