aboutsummaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-01-12 07:53:06 +0100
committerMattias Andrée <maandree@kth.se>2017-01-12 07:53:06 +0100
commitae811c3ce7b71902b508c65278050f0358471e98 (patch)
treee6f802e8476da87c4f53320128e674c58ee9c6a3 /src/util
parentMakefile: clean: do not list .o files explicitly (diff)
downloadblind-ae811c3ce7b71902b508c65278050f0358471e98.tar.gz
blind-ae811c3ce7b71902b508c65278050f0358471e98.tar.bz2
blind-ae811c3ce7b71902b508c65278050f0358471e98.tar.xz
m + Add vu-gauss-blur
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--src/util.c50
-rw-r--r--src/util.h1
-rw-r--r--src/util/jobs.h7
3 files changed, 58 insertions, 0 deletions
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 <sys/prctl.h>
+#endif
+#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
+#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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:");
+}
diff --git a/src/util.h b/src/util.h
index d797356..3bb63fd 100644
--- a/src/util.h
+++ b/src/util.h
@@ -15,3 +15,4 @@
#include "util/to.h"
#include "util/colour.h"
#include "util/io.h"
+#include "util/jobs.h"
diff --git a/src/util/jobs.h b/src/util/jobs.h
new file mode 100644
index 0000000..bb9a8e4
--- /dev/null
+++ b/src/util/jobs.h
@@ -0,0 +1,7 @@
+/* See LICENSE file for copyright and license details. */
+
+#define efork_jobs(...) enfork_jobs(1, __VA_ARGS__)
+#define ejoin_jobs(...) enjoin_jobs(1, __VA_ARGS__)
+
+int enfork_jobs(int status, size_t *start, size_t *end, size_t jobs);
+void enjoin_jobs(int status, int is_master);