diff options
| author | @syxhe <https://t.me/syxhe> | 2025-10-22 00:41:19 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-10-22 00:41:19 -0500 |
| commit | 21c168bf02bbab8b473873f0822d68859a025c24 (patch) | |
| tree | b9caaf2e0f7188650d1c3e02aad777d4f498eb22 /src/shared.c | |
| parent | 13f8225fed47f3451bc8d601657cdfdf1c3379a6 (diff) | |
Fix threadpool after ripping out cleanup function suite
Diffstat (limited to 'src/shared.c')
| -rw-r--r-- | src/shared.c | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/src/shared.c b/src/shared.c index 9a147eb..1310ef2 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -46,26 +46,12 @@ | |||
| 46 | typedef int (*gcallback)(void*); //!< Generic callback signature | 46 | typedef int (*gcallback)(void*); //!< Generic callback signature |
| 47 | typedef void (*fcallback)(void*); //!< free()-like callback signature | 47 | typedef void (*fcallback)(void*); //!< free()-like callback signature |
| 48 | 48 | ||
| 49 | /** | ||
| 50 | * @brief A locally defined structure designed for easier function cleanup | ||
| 51 | * | ||
| 52 | */ | ||
| 53 | typedef struct cl { | ||
| 54 | fcallback *callbacks; //!< An array of free()-like callbacks. Actual Type: fcallback callbacks[] | ||
| 55 | void * *arguments; //!< An array of void pointers. Actual Type: void *arguments[] | ||
| 56 | int size; //!< The size of each array | ||
| 57 | int used; //!< The current number of used elements in each array | ||
| 58 | } cleanup; | ||
| 59 | // While the cleanup thing is useful, it's also a complete fucking mess. Swtich to error gotos asap | ||
| 60 | |||
| 61 | #include <stdlib.h> | 49 | #include <stdlib.h> |
| 62 | #include <string.h> | 50 | #include <string.h> |
| 63 | #include <unistd.h> | 51 | #include <unistd.h> |
| 64 | #include <errno.h> | 52 | #include <errno.h> |
| 65 | #include <error.h> | 53 | #include <error.h> |
| 66 | 54 | ||
| 67 | #include <threads.h> | ||
| 68 | |||
| 69 | /** | 55 | /** |
| 70 | * @brief Read the entire contents of a file descriptor into a malloc()'ed buffer | 56 | * @brief Read the entire contents of a file descriptor into a malloc()'ed buffer |
| 71 | * | 57 | * |
| @@ -241,138 +227,4 @@ char * vxdirname(const char * const path) { | |||
| 241 | return actual; | 227 | return actual; |
| 242 | } | 228 | } |
| 243 | 229 | ||
| 244 | |||
| 245 | /** | ||
| 246 | * @brief Initialize a cleanup object | ||
| 247 | * | ||
| 248 | * @param loc The cleanup object to be initialized | ||
| 249 | * @param callbacks An array of free()-like callbacks. Must be `size` elements long | ||
| 250 | * @param arguments An array of void pointers. Must be `size` elements long | ||
| 251 | * @param size The number of elements the callbacks and arguments array are long | ||
| 252 | * @retval (int)[-1, 0] Returns 0 on success, -1 on error | ||
| 253 | */ | ||
| 254 | int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) { | ||
| 255 | if(!loc || !callbacks || !arguments || size <= 0) ERRRET(EINVAL, -1); | ||
| 256 | |||
| 257 | loc->callbacks = callbacks; | ||
| 258 | loc->arguments = arguments; | ||
| 259 | loc->size = size; | ||
| 260 | loc->used = 0; | ||
| 261 | |||
| 262 | return 0; | ||
| 263 | } | ||
| 264 | |||
| 265 | /** | ||
| 266 | * @brief Register a new callback and argument onto a cleanup stack | ||
| 267 | * | ||
| 268 | * @param loc The cleanup object to modify | ||
| 269 | * @param cb A free()-like callback to run | ||
| 270 | * @param arg A piece of data for the callback to run | ||
| 271 | * @retval (int)[-1, 0] Returns 0 on success, -1 on error | ||
| 272 | */ | ||
| 273 | int cleanup_register(cleanup *loc, fcallback cb, void *arg) { | ||
| 274 | if(!loc || !cb) ERRRET(EINVAL, -1); | ||
| 275 | if(loc->used >= loc->size || loc->used < 0) ERRRET(ENOMEM, -1); | ||
| 276 | |||
| 277 | loc->callbacks[loc->used] = cb; | ||
| 278 | loc->arguments[loc->used] = arg; | ||
| 279 | loc->used++; | ||
| 280 | |||
| 281 | return 0; | ||
| 282 | } | ||
| 283 | |||
| 284 | /** | ||
| 285 | * @brief Conditionally register a callback and argument | ||
| 286 | * | ||
| 287 | * @param loc The cleanup object to modify | ||
| 288 | * @param cb A free()-like callback to run | ||
| 289 | * @param arg A piece of data for the callback to run | ||
| 290 | * @param flag Whether or not the register should take place. Will not run if `flag` is non-zero | ||
| 291 | * @retval (int)[-1, 0] Returns 0 on success or skip, -1 on error | ||
| 292 | */ | ||
| 293 | int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) { | ||
| 294 | if(flag) return 0; | ||
| 295 | return cleanup_register(loc, cb, arg); | ||
| 296 | } | ||
| 297 | |||
| 298 | /** | ||
| 299 | * @brief Clear a cleanup object | ||
| 300 | * @attention Does not free any registered callbacks or arguments, just marks them as available space | ||
| 301 | * | ||
| 302 | * @param loc The cleanup object to modify | ||
| 303 | * @retval (int)[-1, 0] Returns 0 on success, -1 on error | ||
| 304 | */ | ||
| 305 | int cleanup_clear(cleanup *loc) { | ||
| 306 | if(!loc) ERRRET(EINVAL, -1); | ||
| 307 | loc->used = 0; | ||
| 308 | return 0; | ||
| 309 | } | ||
| 310 | |||
| 311 | /** | ||
| 312 | * @brief Fires all the registered callbacks and arguments in a cleanup object in FIFO (stack) order | ||
| 313 | * | ||
| 314 | * @param loc The cleanup object to fire | ||
| 315 | * @retval (int)[-1, 0] Returns 0 on success, -1 on error | ||
| 316 | */ | ||
| 317 | int cleanup_fire(cleanup *loc) { | ||
| 318 | if(!loc) ERRRET(EINVAL, -1); | ||
| 319 | |||
| 320 | for(int i = (loc->used - 1); i >= 0; i--) { | ||
| 321 | if(loc->callbacks[i] == NULL) { | ||
| 322 | error(0, EINVAL, "cleanup_fire: refusing to run null callback..."); | ||
| 323 | continue; | ||
| 324 | } | ||
| 325 | |||
| 326 | loc->callbacks[i](loc->arguments[i]); | ||
| 327 | } | ||
| 328 | cleanup_clear(loc); | ||
| 329 | |||
| 330 | return 0; | ||
| 331 | } | ||
| 332 | |||
| 333 | /** | ||
| 334 | * @brief Conditionally fires a cleanup object | ||
| 335 | * | ||
| 336 | * @param loc The cleanup object in question | ||
| 337 | * @param flag Whether the object should be fired. Will skip firing if non-zero | ||
| 338 | * @retval (int)[-1, 0] Returns 0 on success, -1 on error | ||
| 339 | */ | ||
| 340 | int cleanup_cndfire(cleanup *loc, unsigned char flag) { | ||
| 341 | if(flag) | ||
| 342 | return cleanup_fire(loc); | ||
| 343 | return 0; | ||
| 344 | } | ||
| 345 | |||
| 346 | /** | ||
| 347 | * @brief Initializes a set of variables suitable for use in the cleanup macros | ||
| 348 | * @param size The number of elements long each array should be | ||
| 349 | */ | ||
| 350 | #define cleanup_CREATE(size) \ | ||
| 351 | cleanup __CLEANUP; \ | ||
| 352 | fcallback __CLEANUP_FUNCS[(size)]; \ | ||
| 353 | void *__CLEANUP_ARGS[(size)]; \ | ||
| 354 | unsigned char __FLAG = 0; \ | ||
| 355 | cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size)) | ||
| 356 | |||
| 357 | //! Register a callback-argument pair using the local cleanup object | ||
| 358 | #define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) | ||
| 359 | //! Conditionally register a callback-argument pair using the local cleanup object | ||
| 360 | #define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, (cb), (arg), __FLAG) | ||
| 361 | //! Clean the local cleanup object | ||
| 362 | #define cleanup_CLEAR() cleanup_clear(&__CLEANUP) | ||
| 363 | //! Fire the local cleanup object | ||
| 364 | #define cleanup_FIRE() cleanup_fire(&__CLEANUP) | ||
| 365 | //! Conditionally fire the local cleanup object | ||
| 366 | #define cleanup_CNDFIRE() cleanup_cndfire(&__CLEANUP, __FLAG) | ||
| 367 | //! Set the local cleanup flag to a non-zero number | ||
| 368 | #define cleanup_MARK() (__FLAG = 1) | ||
| 369 | //! Set the local cleanup flag to zero | ||
| 370 | #define cleanup_UNMARK() (__FLAG = 0) | ||
| 371 | //! Check if the local cleanup flag is non-zero | ||
| 372 | #define cleanup_ERRORFLAGGED (__FLAG != 0) | ||
| 373 | //! Conditionally execute some `code` if the local cleanup flag has not been marked | ||
| 374 | #define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;} | ||
| 375 | //! Conditionally fire the local cleanup object and return `ret` | ||
| 376 | #define cleanup_CNDFIRERET(ret) do {cleanup_CNDFIRE(); return ret;} while (0) | ||
| 377 | |||
| 378 | #endif \ No newline at end of file | 230 | #endif \ No newline at end of file |
