Skip to content

Commit

Permalink
test2 for easy debug & job library improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 20, 2024
1 parent e033257 commit 9c3ad6f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ test: test-clean
ninja -C build/_test
cd build/_test ; env LD_LIBRARY_PATH="$$(pwd)"/build G_DEBUG=fatal-criticals yes | timeout 30 ./ymp-test --allow-oem --ask

test2:
valac --pkg ymp test/test2.vala -C --pkg array --pkg jobs \
--pkg ymp --vapidir=src/vapi -C -X -g3
mv test/test2.c build/test2.c
gcc -o build/test2 build/test2.c `pkgconf --cflags --libs ymp` \
-Lbuild -Isrc/include -Ibuild -Lbuild -g3 \
-Wl,--copy-dt-needed-entries -lgobject-2.0
env LD_LIBRARY_PATH="$$(pwd)"/build ./build/test2 --allow-oem

install:
DESTDIR=$(DESTDIR) ninja -C build install

Expand Down
7 changes: 4 additions & 3 deletions src/ccode/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <stdarg.h>

#include <jobs.h>

Expand All @@ -19,7 +20,7 @@ void* worker_thread(void* arg) {
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->jobs[i].callback((void*)j->jobs[i].ctx, (void*)j->jobs[i].args);
j->finished++;
j->current--;
j->jobs[i].callback = NULL;
Expand All @@ -38,13 +39,13 @@ void jobs_unref(jobs *j) {
free(j);
}

void jobs_add(jobs* j, void (*callback)(void*), void* args, ...) {
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;
new_job.args = malloc(sizeof(args));
new_job.args = args;
new_job.ctx = ctx;
new_job.id = j->total;
j->jobs[j->total++] = new_job;
j->current++;
Expand Down
5 changes: 3 additions & 2 deletions src/include/jobs.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <pthread.h>

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

Expand All @@ -20,6 +21,6 @@ typedef struct _jobs {
#define MAX_JOB 1024

void jobs_unref(jobs *j);
void jobs_add(jobs* j, void (*callback)(void*), void* args, ...);
void jobs_add(jobs* j, void (*callback)(void*,...), void* ctx, void* args, ...);
void jobs_run(jobs* j);
jobs* jobs_new();
2 changes: 1 addition & 1 deletion src/vapi/jobs.vapi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class jobs {
public int total;

public void unref ();
public void add (void* callback, void* args);
public void add (void* callback, void* args, ...);
public void run ();
}
22 changes: 22 additions & 0 deletions test/test2.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class test {
public test(){}
public void p(string message){
stdout.printf(message);
}

}

public void test2(string message){
stdout.printf(message);
}

int main(string[] args){
jobs j = new jobs();
test t = new test();
t.p("Hello World\n");
j.add((void*)t.p, t, "Hello World\n");
j.add((void*)test2, "Hello World\n");
j.run();
ymp_init(args);
return 0;
}

0 comments on commit 9c3ad6f

Please sign in to comment.