From c710a0da4ebcc126275c42a0387ff85b2557e3ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Thu, 30 Mar 2017 14:22:03 +0200 Subject: [PATCH] Overall integration of job initialisation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Move the job initialisation from main to jobs. Change-Id: I8f5b54adb62e60592884ff1e3fad9811b5934d47 Signed-off-by: José Bollo --- src/jobs.c | 31 +++++++++++++++++++++++++++++++ src/jobs.h | 1 + src/main.c | 29 +++++------------------------ src/tests/test-thread.sh | 2 +- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/jobs.c b/src/jobs.c index 8ffd6b61..5d2a29b4 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -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(); +}; diff --git a/src/jobs.h b/src/jobs.h index 0eceef3d..6eb0a83d 100644 --- a/src/jobs.h +++ b/src/jobs.h @@ -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)()); diff --git a/src/main.c b/src/main.c index 9c2f3c56..71b21388 100644 --- a/src/main.c +++ b/src/main.c @@ -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; } diff --git a/src/tests/test-thread.sh b/src/tests/test-thread.sh index e353e028..a9e2ee3b 100755 --- a/src/tests/test-thread.sh +++ b/src/tests/test-thread.sh @@ -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 -- 2.16.6