job: return NULL if queue is full when starting job;

The current implementation might spin forever if only one thread is
starting/waiting on jobs, since nothing will be waiting on the jobs
(i.e. freeing up queue slots) while spinning.

The correct thing to do is to return NULL and let the caller handle
backpressure.  It's a bit more annoying to program since you might not
get a job back and need to handle it, but it's the right thing to do.
This commit is contained in:
bjorn 2024-03-01 16:48:28 -08:00
parent 79e28eb7e3
commit 80b596daf6
1 changed files with 6 additions and 10 deletions

View File

@ -96,17 +96,13 @@ void job_destroy(void) {
}
job* job_start(fn_job* fn, void* arg) {
for (;;) {
mtx_lock(&state.lock);
mtx_lock(&state.lock);
if (state.pool) {
break;
} else if (state.head) {
runJob();
} else { // Might not be a job to do if worker count is bigger than pool size
mtx_unlock(&state.lock);
thrd_yield();
}
// If there's no free job available, return NULL as a backpressure signal
// Caller can choose how to handle: error, wait on older jobs, just run the job, etc.
if (!state.pool) {
mtx_unlock(&state.lock);
return NULL;
}
job* job = state.pool;