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.h | 92 ++++++++++++++++++++++-------------------------------------- 1 file changed, 33 insertions(+), 59 deletions(-) (limited to 'src/shared.h') diff --git a/src/shared.h b/src/shared.h index 66d1af3..adaad4a 100644 --- a/src/shared.h +++ b/src/shared.h @@ -1,11 +1,9 @@ #ifndef __VXGG_REWRITE___SHARED_H___3880294315821___ -#define __VXGG_REWRITE___SHARED_H___3880294315821___ +#define __VXGG_REWRITE___SHARED_H___3880294315821___ 1 #include #define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) -#define FALSE 0 -#define TRUE 1 typedef int (*gcallback)(void*); // Generic callback signature typedef void (*fcallback)(void*); // free()-like callback signature @@ -44,82 +42,58 @@ typedef void (*fcallback)(void*); // free()-like callback signature // `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void * xmalloc(size_t size); - // `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void * xcalloc(size_t nmemb, size_t size); - // `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void * xreallocarray(void *ptr, size_t nmemb, size_t size); - // Read the entire contents of a file descriptor into a malloc()'ed buffer int rwbuf(char **str, unsigned long int initsize, int fd); - // Write the entire contents of a buffer into a file descriptor int wwbuf(int fd, const unsigned char *buf, int len); - // `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. char * xdirname(const char * const path); -// Cleanup callback. Should act like `free()`, in that it doesn't crash if the pointer it's given is null -typedef fcallback cleanup_callback; - -// Cleanup struct. Stores a STATICALLY defined array of callbacks and void* arguments +// A locally defined structure designed for easier function cleanup typedef struct cl { - cleanup_callback *funcs; // Actual type: cleanup_callback funcs[] - void **args; // Actual type: void *args[] - - int size; - int used; + fcallback *callbacks; // Actual Type: fcallback callbacks[] + void * *arguments; // Actual Type: void *arguments[] + int size; + 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); +int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size); +int cleanup_register(cleanup *loc, fcallback cb, void *arg); +int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag); +int cleanup_clear(cleanup *loc); +int cleanup_fire(cleanup *loc); +int cleanup_cndfire(cleanup *loc, unsigned char flag); -/* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes -// via `cleanup_init()` using these variables. */ #define cleanup_CREATE(size) \ cleanup __CLEANUP; \ -cleanup_callback __CLEANUP_FUNCS[(size)]; \ +fcallback __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(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) +cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size)) + +#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) +#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, (cb), (arg), __FLAG) +#define cleanup_CLEAR() cleanup_clear(&__CLEANUP) +#define cleanup_FIRE() cleanup_fire(&__CLEANUP) +#define cleanup_CNDFIRE() cleanup_cndfire(&__CLEANUP, __FLAG) +#define cleanup_MARK() (__FLAG = 1) +#define cleanup_UNMARK() (__FLAG = 0) +#define cleanup_ERRORFLAGGED (__FLAG != 0) +#define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;} + + + +// Generic task to be executed by a consumer +typedef struct task task; +// A queue of tasks +typedef struct taskqueue taskqueue; +// A concurrent queue of tasks, basically a threadpool tasks can be dispatched to +typedef struct ctqueue ctqueue; #endif \ No newline at end of file -- cgit v1.2.3