aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2019-01-11 12:50:30 +0100
committerAnton Khirnov <anton@khirnov.net>2019-01-11 12:50:30 +0100
commitbb944f2958339e09a936c98f931c38e3fa3f8c5f (patch)
treed26e5d99ea08eb94a52f09f972bda1ce5af8e8fc
Initial commit.
-rw-r--r--libthreadpool.v4
-rw-r--r--meson.build16
-rw-r--r--threadpool.c80
-rw-r--r--threadpool.h71
4 files changed, 171 insertions, 0 deletions
diff --git a/libthreadpool.v b/libthreadpool.v
new file mode 100644
index 0000000..6597847
--- /dev/null
+++ b/libthreadpool.v
@@ -0,0 +1,4 @@
+LIBTHREADPOOL_2 {
+ global: tp_*;
+ local: *;
+};
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..c2f6c76
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,16 @@
+project('libthreadpool', 'c',
+ default_options : ['c_std=c11'])
+
+add_project_arguments('-D_XOPEN_SOURCE=700', language : 'c')
+
+lib_src = [
+ 'threadpool.c',
+]
+
+verscript = 'libthreadpool.v'
+ver_flag = '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(), verscript)
+
+dep_omp = dependency('openmp')
+deps = [dep_omp]
+
+library('threadpool', lib_src, link_args : ver_flag, dependencies : deps)
diff --git a/threadpool.c b/threadpool.c
new file mode 100644
index 0000000..57d046b
--- /dev/null
+++ b/threadpool.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2018 Anton Khirnov <anton@khirnov.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <omp.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "threadpool.h"
+
+struct TPContext {
+ char dummy;
+};
+
+void tp_free(TPContext **pctx)
+{
+ TPContext *ctx = *pctx;
+
+ if (!ctx)
+ return;
+
+ free(ctx);
+ *pctx = NULL;
+}
+
+int tp_init(TPContext **pctx, unsigned int nb_threads)
+{
+ TPContext *ctx = NULL;
+ int ret;
+
+ if (!nb_threads) {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ ctx = calloc(1, sizeof(*ctx));
+ if (!ctx) {
+ ret = -ENOMEM;
+ goto fail;
+ }
+
+ omp_set_num_threads(nb_threads);
+
+ *pctx = ctx;
+ return 0;
+
+fail:
+ tp_free(&ctx);
+ *pctx = NULL;
+ return ret;
+}
+
+int tp_execute(TPContext *ctx, unsigned int nb_jobs,
+ TPExecuteCallback func, void *func_arg)
+{
+#pragma omp parallel for
+ for (unsigned int i = 0; i < nb_jobs; i++)
+ func(func_arg, i, omp_get_thread_num());
+
+ return 0;
+}
+
+unsigned int tp_get_nb_threads(TPContext *ctx)
+{
+ return omp_get_num_threads();
+}
diff --git a/threadpool.h b/threadpool.h
new file mode 100644
index 0000000..0f120ab
--- /dev/null
+++ b/threadpool.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2016-2018 Anton Khirnov <anton@khirnov.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TP_THREADPOOL_H
+#define TP_THREADPOOL_H
+
+/**
+ * The thread pool object, allocated by tp_init() and destroyed by tp_free().
+ */
+typedef struct TPContext TPContext;
+
+/**
+ * The callback passed to tp_execute().
+ *
+ * @param arg the user pointer supplied to tp_execute
+ * @param job_idx the job index, between 0 and nb_jobs
+ * @param thread_idx the index of the thread executing this job, between 0 and
+ * nb_threads
+ *
+ * @return the callback should return 0 on success and a negative number on
+ * failure
+ */
+typedef int (*TPExecuteCallback)(void *arg, unsigned int job_idx, unsigned int thread_idx);
+
+/**
+ * Create the thread pool object.
+ *
+ * @param ctx the newly created thread pool will be written here
+ * @param nb_threads the number of threads to execute in parallel
+ * @return 0 on success, a negative error code on failure
+ */
+int tp_init(TPContext **ctx, unsigned int nb_threads);
+/**
+ * Free the thread pool object and write NULL into the supplied pointer.
+ */
+void tp_free(TPContext **ctx);
+
+/**
+ * Execute a function multiple times in parallel.
+ *
+ * @param ctx the thread pool object
+ * @param nb_jobs number of times to call func
+ * @param func the callback to call
+ * @param func_arg arbitrary user data to pass to func
+ *
+ * @return 0 if all the calls to func returned successfully, a negative error
+ * code otherwise
+ */
+int tp_execute(TPContext *ctx, unsigned int nb_jobs,
+ TPExecuteCallback func, void *func_arg);
+
+/**
+ * Get the number of threads in this thread pool.
+ */
+unsigned int tp_get_nb_threads(TPContext *ctx);
+
+#endif /* TP_THREADPOOL_H */