Skip to content

Commit

Permalink
parallel remove fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 20, 2024
1 parent 426e997 commit 43885fb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
17 changes: 4 additions & 13 deletions src/ccode/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,31 @@
void* worker_thread(void* arg) {
jobs* j = (jobs*)arg;
while (1) {
pthread_mutex_lock(&j->mutex);
while (j->current == 0 && j->finished < j->total) {
pthread_cond_wait(&j->cond, &j->mutex);
}
if (j->finished >= j->total) {
pthread_mutex_unlock(&j->mutex);
break;
}
int i;
for (i = 0; i < j->max; ++i) {
for (i = 0; i < j->total; ++i) {
if (j->jobs[i].callback != NULL) {
j->jobs[i].callback((void*)j->jobs[i].ctx, (void*)j->jobs[i].args);
void (*callback)(void*, ...) = j->jobs[i].callback;
j->jobs[i].callback = NULL;
callback((void*)j->jobs[i].ctx, (void*)j->jobs[i].args);
j->finished++;
j->current--;
j->jobs[i].callback = NULL;
break;
}
}
pthread_mutex_unlock(&j->mutex);
}
pthread_exit(NULL);
}

void jobs_unref(jobs *j) {
free(j->jobs);
pthread_mutex_destroy(&j->mutex);
pthread_cond_destroy(&j->cond);
free(j);
}

void jobs_add(jobs* j, void (*callback)(void*, ...), void* ctx, void* args, ...) {
pthread_mutex_lock(&j->mutex);
if (j->total < j->max) {
job new_job;
new_job.callback = callback;
Expand All @@ -51,7 +44,6 @@ void jobs_add(jobs* j, void (*callback)(void*, ...), void* ctx, void* args, ...)
j->current++;
pthread_cond_signal(&j->cond);
}
pthread_mutex_unlock(&j->mutex);
}

void jobs_run(jobs* j) {
Expand All @@ -74,7 +66,6 @@ jobs* jobs_new() {
j->total = 0;
j->parallel = JOB_PARALLEL;
j->jobs = (job*)malloc(j->max * sizeof(job));
pthread_mutex_init(&j->mutex, NULL);
pthread_cond_init(&j->cond, NULL);
return j;
}
1 change: 0 additions & 1 deletion src/include/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ typedef struct _jobs {
int parallel;
int finished; /* Track the number of finished jobs */
int total; /* Total number of jobs */
pthread_mutex_t mutex; /* Mutex for accessing finished and total */
pthread_cond_t cond; /* Condition variable for signaling job completion */
} jobs;

Expand Down

0 comments on commit 43885fb

Please sign in to comment.