From bb944f2958339e09a936c98f931c38e3fa3f8c5f Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 11 Jan 2019 12:50:30 +0100 Subject: Initial commit. --- threadpool.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 threadpool.c (limited to 'threadpool.c') 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 + * + * 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 . + */ + +#include +#include +#include +#include + +#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(); +} -- cgit v1.2.3