-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
} |