Skip to content

Commit

Permalink
job library
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 19, 2024
1 parent 611cd1b commit 11a08f0
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/ccode/job.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

#include <jobs.h>

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) {
if (j->jobs[i].callback != NULL) {
j->jobs[i].callback((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* args, ...) {
pthread_mutex_lock(&j->mutex);
if (j->total < j->max) {
job new_job;
new_job.callback = callback;
new_job.args = malloc(sizeof(args));
memcpy(new_job.args, args, sizeof(args));
new_job.id = j->total;
j->jobs[j->total++] = new_job;
j->current++;
pthread_cond_signal(&j->cond);
}
pthread_mutex_unlock(&j->mutex);
}

void jobs_run(jobs* j) {
pthread_t* threads = (pthread_t*)malloc(j->parallel * sizeof(pthread_t));
int i;
for (i = 0; i < j->parallel; ++i) {
pthread_create(&threads[i], NULL, worker_thread, (void*)j);
}
for (i = 0; i < j->parallel; ++i) {
pthread_join(threads[i], NULL);
}
free(threads);
}

jobs* jobs_new() {
jobs* j = (jobs*)malloc(sizeof(jobs));
j->max = MAX_JOB;
j->current = 0;
j->finished = 0;
j->total = 0;
j->parallel = 4; /* Change as needed */
j->jobs = (job*)malloc(j->max * sizeof(job));
pthread_mutex_init(&j->mutex, NULL);
pthread_cond_init(&j->cond, NULL);
return j;
}
25 changes: 25 additions & 0 deletions src/include/jobs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <pthread.h>

typedef struct _job {
void (*callback)(void*);
void* args;
int id;
} job;

typedef struct _jobs {
job* jobs;
int max;
int current;
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;

#define MAX_JOB 1024

void jobs_unref(jobs *j);
void jobs_add(jobs* j, void (*callback)(void*), void* args, ...);
void jobs_run(jobs* j);
jobs* jobs_new();
20 changes: 20 additions & 0 deletions src/vapi/jobs.vapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public struct job {
public unowned void* callback;
public unowned void* args;
public int id;
}

[CCode (cheader_filename = "jobs.h")]
public class jobs {
public job* jobs;
public jobs();
public int max;
public int current;
public int parallel;
public int finished;
public int total;

public void unref ();
public void add (void* callback, void* args);
public void run ();
}

0 comments on commit 11a08f0

Please sign in to comment.