From ae811c3ce7b71902b508c65278050f0358471e98 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 12 Jan 2017 07:53:06 +0100 Subject: m + Add vu-gauss-blur MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/util.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index 2a186e1..4654789 100644 --- a/src/util.c +++ b/src/util.c @@ -1,9 +1,14 @@ /* See LICENSE file for copyright and license details. */ #include "util.h" +#if defined(HAVE_PRCTL) +# include +#endif +#include #include #include #include +#include #include #include #include @@ -118,3 +123,48 @@ writeall(int fd, void *buf, size_t n) } return 0; } + + +static inline pid_t +enfork(int status) +{ + pid_t pid = fork(); + if (pid == -1) + enprintf(status, "fork:"); + return pid; +} + + +int +enfork_jobs(int status, size_t *start, size_t *end, size_t jobs) +{ + size_t j, s = *start, n = *end - *start; + if (jobs < 2) + return 1; + *end = n / jobs + s; + for (j = 1; j < jobs; j++) { + if (!enfork(status)) { +#if defined(HAVE_PRCTL) && defined(PR_SET_PDEATHSIG) + prctl(PR_SET_PDEATHSIG, SIGKILL); +#endif + *start = n * (j + 0) / jobs + s; + *end = n * (j + 1) / jobs + s; + return 0; + } + } + return 1; +} + + +void +enjoin_jobs(int status, int is_master) +{ + int stat; + if (!is_master) + exit(0); + while (wait(&stat) != -1) + if (!stat) + exit(status); + if (errno != ECHILD) + enprintf(status, "wait:"); +} -- cgit v1.2.3-70-g09d2