Overall integration of job initialisation
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 30 Mar 2017 12:22:03 +0000 (14:22 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 30 Mar 2017 12:24:01 +0000 (14:24 +0200)
Move the job initialisation from main to jobs.

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

index 8ffd6b6..5d2a29b 100644 (file)
@@ -911,3 +911,34 @@ struct sd_event *jobs_get_sd_event()
        return events ? events->event : NULL;
 }
 
+/**
+ * run the jobs as 
+ * @param allowed_count Maximum count of thread for jobs including this one
+ * @param start_count   Count of thread to start now, must be lower.
+ * @param waiter_count  Maximum count of jobs that can be waiting.
+ * @param start         The start routine to activate (can't be NULL)
+ * @return 0 in case of success or -1 in case of error.
+ */
+int jobs_enter(int allowed_count, int start_count, int waiter_count, void (*start)())
+{
+       /* start */
+       if (sig_monitor_init() < 0) {
+               ERROR("failed to initialise signal handlers");
+               return -1;
+       }
+
+       /* init job processing */
+       if (jobs_init(allowed_count, start_count, waiter_count) < 0) {
+               ERROR("failed to initialise threading");
+               return -1;
+       }
+
+       /* queue the start job */
+       if (jobs_queue0(NULL, 0, (void(*)(int))start) < 0) {
+               ERROR("failed to start runnning jobs");
+               return -1;
+       }
+
+       /* turn as processing thread */
+       return jobs_add_me();
+};
index 0eceef3..6eb0a83 100644 (file)
@@ -73,4 +73,5 @@ extern int jobs_init(int allowed_count, int start_count, int waiter_count);
 extern int jobs_add_me();
 extern void jobs_terminate();
 
+extern int jobs_enter(int allowed_count, int start_count, int waiter_count, void (*start)());
 
index 9c2f3c5..71b2138 100644 (file)
@@ -41,7 +41,6 @@
 #include "afb-hsrv.h"
 #include "afb-context.h"
 #include "afb-hreq.h"
-#include "sig-monitor.h"
 #include "jobs.h"
 #include "afb-session.h"
 #include "verbose.h"
@@ -397,7 +396,7 @@ static int execute_command()
  | job for starting the daemon
  +--------------------------------------------------------- */
 
-static void start(int signum)
+static void start()
 {
        struct afb_hsrv *hsrv;
 
@@ -495,27 +494,9 @@ int main(int argc, char *argv[])
        /* ignore any SIGPIPE */
        signal(SIGPIPE, SIG_IGN);
 
-       /* start */
-       if (sig_monitor_init() < 0) {
-               ERROR("failed to initialise signal handlers");
-               return 1;
-       }
-
-       /* init job processing */
-       if (jobs_init(3, 1, 20) < 0) {
-               ERROR("failed to initialise threading");
-               return 1;
-       }
-
-       /* queue the start job */
-       if (jobs_queue0(NULL, 0, start) < 0) {
-               ERROR("failed to start runnning jobs");
-               return 1;
-       }
-
-       /* turn as processing thread */
-       jobs_add_me();
-       WARNING("hoops returned from jobs_add_me! [report bug]");
-       return 0;
+       /* enter job processing */
+       jobs_enter(3, 1, 20, start);
+       WARNING("hoops returned from jobs_enter! [report bug]");
+       return 1;
 }
 
index e353e02..a9e2ee3 100755 (executable)
@@ -1,4 +1,4 @@
 #!/bin/sh
 
-cc test-thread.c ../afb-thread.c ../verbose.c ../sig-monitor.c ../jobs.c -o test-thread -lrt -lpthread -I../../include -g 
+cc test-thread.c ../afb-thread.c ../verbose.c ../sig-monitor.c ../jobs.c -o test-thread -lrt -lpthread -lsystemd I../../include -g 
 ./test-thread