job copies errors; add variadic version of job_abort;

I caved and depended on malloc/sprintf because I couldn't find anywhere
else good to put it...
This commit is contained in:
bjorn 2024-01-06 13:21:32 -08:00
parent f6af859984
commit 901dd268f2
2 changed files with 20 additions and 4 deletions

View File

@ -2,6 +2,8 @@
#include <stdatomic.h>
#include <threads.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORKERS 64
@ -10,7 +12,7 @@
struct job {
job* next;
atomic_uint done;
union { fn_job* fn; const char* error; };
union { fn_job* fn; char* error; };
void* arg;
};
@ -129,7 +131,18 @@ job* job_start(fn_job* fn, void* arg) {
}
void job_abort(job* job, const char* error) {
job->error = error;
size_t length = strlen(error);
job->error = malloc(length + 1);
if (job->error) memcpy(job->error, error, length + 1);
else job->error = "Out of memory";
longjmp(catch, 22);
}
void job_vabort(job* job, const char* format, va_list args) {
int length = vsnprintf(NULL, 0, format, args);
job->error = malloc(length + 1);
if (job->error) vsnprintf(job->error, length + 1, format, args);
else job->error = "Out of memory";
longjmp(catch, 22);
}
@ -146,12 +159,13 @@ void job_wait(job* job) {
}
}
char* job_get_error(job* job) {
const char* job_get_error(job* job) {
return (char*) job->error;
}
void job_free(job* job) {
mtx_lock(&state.lock);
if (job->error && job->error != "Out of memory") free(job->error);
job->next = state.pool;
state.pool = job;
mtx_unlock(&state.lock);

View File

@ -1,3 +1,4 @@
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
@ -10,6 +11,7 @@ bool job_init(uint32_t workerCount);
void job_destroy(void);
job* job_start(fn_job* fn, void* arg);
void job_abort(job* job, const char* error);
void job_vabort(job* job, const char* format, va_list args);
void job_wait(job* job);
char* job_get_error(job* job);
const char* job_get_error(job* job);
void job_free(job* job);