Fix bug in recycling jobs
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 28 Mar 2017 08:18:06 +0000 (10:18 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 28 Mar 2017 08:21:06 +0000 (10:21 +0200)
The bug was creating an infinite loop starving
the system (or other possible horrific stuff).

Also updated the test.

Change-Id: Id71dd112d2ed4651ac8aa56d2c57b088d69b8655
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/jobs.c
src/jobs.h
src/tests/test-thread.c
src/tests/test-thread.sh

index 1be6ec7..2910f0a 100644 (file)
@@ -146,7 +146,9 @@ static struct job *job_create(
 
        /* allocates the job */
        job = free_jobs;
-       if (!job) {
+       if (job)
+               free_jobs = job->next;
+       else {
                pthread_mutex_unlock(&mutex);
                job = malloc(sizeof *job);
                pthread_mutex_lock(&mutex);
@@ -369,7 +371,7 @@ int jobs_init(int allowed_count, int start_count, int waiter_count)
 }
 
 /* terminate all the threads and all pending requests */
-void jobs_terminate()
+void jobs_terminate(int wait)
 {
        struct job *job;
        pthread_t me, other;
index b6f277c..95565b8 100644 (file)
@@ -42,6 +42,6 @@ extern int jobs_add_event_loop(void *key, int timeout, void (*evloop)(int, void*
 
 extern int jobs_init(int allowed_count, int start_count, int waiter_count);
 extern int jobs_add_me();
-extern void jobs_terminate();
+extern void jobs_terminate(int wait);
 
 
index cc676bd..2f0e185 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <afb/afb-req-itf.h>
 #include "../afb-thread.h"
+#include "../jobs.h"
 
 struct foo {
        int value;
@@ -77,12 +78,12 @@ int main()
        struct timespec ts;
 
        req.itf = &itf;
-       afb_thread_init(4, 1);
+       jobs_init(4, 0, 20000);
        for (i = 0 ; i  < 10000 ; i++) {
                req.closure = foo = malloc(sizeof *foo);
                foo->value = i;
                foo->refcount = 1;
-               afb_thread_call(req, process, 5, (&ts) + (i % 4));
+               afb_thread_req_call(req, process, 5, (&ts) + (i % 7));
                unref(foo);
                ts.tv_sec = 0;
                ts.tv_nsec = 1000000;
@@ -91,7 +92,7 @@ int main()
        ts.tv_sec = 1;
        ts.tv_nsec = 0;
        nanosleep(&ts, NULL);
-       afb_thread_terminate();
+       jobs_terminate();
 }
 
 
index 6d1b3a5..d5186d1 100755 (executable)
@@ -1,4 +1,4 @@
 #!/bin/sh
 
-cc test-thread.c ../afb-thread.c ../verbose.c ../afb-sig-handler.c -o test-thread -lrt -lpthread -I../../include
-./test-thread
+cc test-thread.c ../afb-thread.c ../verbose.c ../sig-monitor.c ../jobs.c -o test-thread -lrt -lpthread -I../../include -g -O2
+gdb ./test-thread