summaryrefslogtreecommitdiff
path: root/src/shared.h
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.h
parent8fe1a7ea459829145dfef4b0aa8f627f96841cbd (diff)
Get threadpool implementation workingthreadpool-debugging
Diffstat (limited to 'src/shared.h')
-rw-r--r--src/shared.h92
1 files changed, 33 insertions, 59 deletions
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 @@
1#ifndef __VXGG_REWRITE___SHARED_H___3880294315821___ 1#ifndef __VXGG_REWRITE___SHARED_H___3880294315821___
2#define __VXGG_REWRITE___SHARED_H___3880294315821___ 2#define __VXGG_REWRITE___SHARED_H___3880294315821___ 1
3 3
4#include <stddef.h> 4#include <stddef.h>
5 5
6#define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) 6#define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0]))
7#define FALSE 0
8#define TRUE 1
9 7
10typedef int (*gcallback)(void*); // Generic callback signature 8typedef int (*gcallback)(void*); // Generic callback signature
11typedef void (*fcallback)(void*); // free()-like callback signature 9typedef void (*fcallback)(void*); // free()-like callback signature
@@ -44,82 +42,58 @@ typedef void (*fcallback)(void*); // free()-like callback signature
44 42
45// `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` 43// `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`
46void * xmalloc(size_t size); 44void * xmalloc(size_t size);
47
48// `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` 45// `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`
49void * xcalloc(size_t nmemb, size_t size); 46void * xcalloc(size_t nmemb, size_t size);
50
51// `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` 47// `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`
52void * xreallocarray(void *ptr, size_t nmemb, size_t size); 48void * xreallocarray(void *ptr, size_t nmemb, size_t size);
53
54// Read the entire contents of a file descriptor into a malloc()'ed buffer 49// Read the entire contents of a file descriptor into a malloc()'ed buffer
55int rwbuf(char **str, unsigned long int initsize, int fd); 50int rwbuf(char **str, unsigned long int initsize, int fd);
56
57// Write the entire contents of a buffer into a file descriptor 51// Write the entire contents of a buffer into a file descriptor
58int wwbuf(int fd, const unsigned char *buf, int len); 52int wwbuf(int fd, const unsigned char *buf, int len);
59
60// `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. 53// `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error.
61char * xdirname(const char * const path); 54char * xdirname(const char * const path);
62 55
63 56
64 57
65// Cleanup callback. Should act like `free()`, in that it doesn't crash if the pointer it's given is null 58// A locally defined structure designed for easier function cleanup
66typedef fcallback cleanup_callback;
67
68// Cleanup struct. Stores a STATICALLY defined array of callbacks and void* arguments
69typedef struct cl { 59typedef struct cl {
70 cleanup_callback *funcs; // Actual type: cleanup_callback funcs[] 60 fcallback *callbacks; // Actual Type: fcallback callbacks[]
71 void **args; // Actual type: void *args[] 61 void * *arguments; // Actual Type: void *arguments[]
72 62 int size;
73 int size; 63 int used;
74 int used;
75} cleanup; 64} cleanup;
76 65
77// 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 66int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size);
78int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); 67int cleanup_register(cleanup *loc, fcallback cb, void *arg);
79 68int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag);
80// Register a cleanup callback for a given cleanup object 69int cleanup_clear(cleanup *loc);
81int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); 70int cleanup_fire(cleanup *loc);
82 71int cleanup_cndfire(cleanup *loc, unsigned char flag);
83// Register a cleanup callback, if and only if `flag == 0`
84int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg);
85
86// Clear the contents of a cleanup stack
87int cleanup_clear(cleanup * const loc);
88
89// Get the top callback without removing it from the cleanup stack
90cleanup_callback cleanup_peekf(cleanup * const loc);
91
92// Get and remove the top callback from the cleanup stack. Does not return the argument for the given callback
93cleanup_callback cleanup_popf(cleanup * const loc);
94
95// Get the top argument without removing it from the cleanup stack
96void * cleanup_peeka(cleanup * const loc);
97
98// Get and remove the top argument from the cleanup stack. Does not return the callback it was to be fed into
99void * cleanup_popa(cleanup * const loc);
100
101// Fire all the callbacks in the cleanup stack
102int cleanup_fire(cleanup * const loc);
103 72
104/* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes
105// via `cleanup_init()` using these variables. */
106#define cleanup_CREATE(size) \ 73#define cleanup_CREATE(size) \
107cleanup __CLEANUP; \ 74cleanup __CLEANUP; \
108cleanup_callback __CLEANUP_FUNCS[(size)]; \ 75fcallback __CLEANUP_FUNCS[(size)]; \
109void *__CLEANUP_ARGS[(size)]; \ 76void *__CLEANUP_ARGS[(size)]; \
110unsigned char __FLAG = 0; \ 77unsigned char __FLAG = 0; \
111cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS) 78cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size))
112 79
113#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) 80#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg))
114#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, __FLAG, (cb), (arg)) 81#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, (cb), (arg), __FLAG)
115#define cleanup_CLEAR() cleanup_clear(&__CLEANUP) 82#define cleanup_CLEAR() cleanup_clear(&__CLEANUP)
116#define cleanup_PEEKF() cleanup_peekf(&__CLEANUP) 83#define cleanup_FIRE() cleanup_fire(&__CLEANUP)
117#define cleanup_POPF() cleanup_popf(&__CLEANUP) 84#define cleanup_CNDFIRE() cleanup_cndfire(&__CLEANUP, __FLAG)
118#define cleanup_PEEKA() cleanup_peeka(&__CLEANUP) 85#define cleanup_MARK() (__FLAG = 1)
119#define cleanup_POPA() cleanup_popa(&__CLEANUP) 86#define cleanup_UNMARK() (__FLAG = 0)
120#define cleanup_FIRE() cleanup_fire(&__CLEANUP) 87#define cleanup_ERRORFLAGGED (__FLAG != 0)
121#define cleanup_MARK() (__FLAG = 1) 88#define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;}
122#define cleanup_UNMARK() (__FLAG = 0) 89
123#define cleanup_ERRORFLAGGED (__FLAG != 0) 90
91
92// Generic task to be executed by a consumer
93typedef struct task task;
94// A queue of tasks
95typedef struct taskqueue taskqueue;
96// A concurrent queue of tasks, basically a threadpool tasks can be dispatched to
97typedef struct ctqueue ctqueue;
124 98
125#endif \ No newline at end of file 99#endif \ No newline at end of file