From e01115702ad73c32efdf11b97d84a81f59300e1d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 23 Dec 2016 02:01:39 +0100 Subject: common: thread-pool: initial work --- Makefile.am | 23 ++++++++++++++++ configure.ac | 3 +++ src/common/error.c | 3 +++ src/common/error.h | 1 + src/common/thread-pool.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ src/common/thread-pool.h | 27 +++++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 src/common/thread-pool.c create mode 100644 src/common/thread-pool.h diff --git a/Makefile.am b/Makefile.am index 219d843..b7388a0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,6 +65,7 @@ noinst_HEADERS = \ src/common/error.h \ src/common/file.h \ src/common/stdin.h \ + src/common/thread-pool.h \ src/common/utf8.h \ src/common/utils.h \ $(NULL) @@ -111,6 +112,12 @@ noinst_LTLIBRARIES += \ $(NULL) endif +if USE_THREADS +noinst_LTLIBRARIES += \ + libblogc_common_tp.la \ + $(NULL) +endif + check_PROGRAMS = \ $(NULL) @@ -152,6 +159,22 @@ libblogc_common_la_CFLAGS = \ $(NULL) +if USE_THREADS +libblogc_common_tp_la_SOURCES = \ + src/common/thread-pool.c \ + $(NULL) + +libblogc_common_tp_la_CFLAGS = \ + $(AM_CFLAGS) \ + $(PTHREAD_CFLAGS) \ + $(NULL) + +libblogc_common_tp_la_LIBADD = \ + $(PTHREAD_LIBS) \ + $(NULL) +endif + + blogc_SOURCES = \ src/blogc/main.c \ $(NULL) diff --git a/configure.ac b/configure.ac index 9e6d7eb..35cbba9 100644 --- a/configure.ac +++ b/configure.ac @@ -135,6 +135,7 @@ AS_IF([test "x$enable_runserver" = "xyes"], [ AC_MSG_ERROR([blogc-runserver tool requested but pthread is not supported]) ]) RUNSERVER="enabled" + have_threads=yes have_runserver=yes ]) AM_CONDITIONAL([BUILD_RUNSERVER], [test "x$have_runserver" = "xyes"]) @@ -220,6 +221,8 @@ AM_CONDITIONAL([USE_BGR_DEPS], [test "x$have_bgr_deps" = "xyes"]) BASH="$ac_cv_path_bash" AC_SUBST(BASH) +AM_CONDITIONAL([USE_THREADS], [test "x$have_threads" = "xyes"]) + AC_CHECK_HEADERS([sys/stat.h time.h]) LT_LIB_M diff --git a/src/common/error.c b/src/common/error.c index 19f369c..38c7fdb 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -118,6 +118,9 @@ bc_error_print(bc_error_t *err, const char *prefix) case BC_ERROR_FILE: fprintf(stderr, "error: file: %s\n", err->msg); break; + case BC_ERROR_THREADPOOL: + fprintf(stderr, "error: thread-pool: %s\n", err->msg); + break; case BLOGC_ERROR_SOURCE_PARSER: fprintf(stderr, "error: source: %s\n", err->msg); break; diff --git a/src/common/error.h b/src/common/error.h index 34aab74..c10868c 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -17,6 +17,7 @@ typedef enum { // errors for src/common BC_ERROR_CONFIG_PARSER = 1, BC_ERROR_FILE, + BC_ERROR_THREADPOOL, // errors for src/blogc BLOGC_ERROR_SOURCE_PARSER = 100, diff --git a/src/common/thread-pool.c b/src/common/thread-pool.c new file mode 100644 index 0000000..ad345bb --- /dev/null +++ b/src/common/thread-pool.c @@ -0,0 +1,70 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include "error.h" +#include "utils.h" +#include "thread-pool.h" + +pthread_mutex_t jobs_mutex; + +typedef struct { + bc_threadpool_t *pool; + pthread_t thread; + size_t id; +} thread_info_t; + + +static void* +worker(void *arg) +{ + thread_info_t *info = arg; + + + return NULL; +} + + +bc_threadpool_t* +bc_threadpool_new(bc_threadpool_func_t func, size_t max_threads, + void *user_data, bc_error_t **err) +{ + if (err != NULL && *err != NULL) + return NULL; + + bc_threadpool_t *rv = bc_malloc(sizeof(bc_threadpool_t)); + rv->jobs = NULL; + rv->threads = NULL; + rv->func = func; + rv->max_threads = max_threads; + rv->user_data = user_data; + + int e; + + for (size_t i = 0; i < rv->max_threads; i++) { + thread_info_t *info = bc_malloc(sizeof(thread_info_t)); + info->pool = rv; + info->id = i+1; + if (0 != (e = pthread_create(&(info->thread), NULL, worker, info))) { + *err = bc_error_new_printf(BC_ERROR_THREADPOOL, + "Failed to create pool: %s", strerror(e)); + // FIXME: kill any existing threads. currently leaking. + free(info); + return NULL; + } + rv->threads = bc_slist_append(rv->threads, info); + } + + return rv; +} + + + diff --git a/src/common/thread-pool.h b/src/common/thread-pool.h new file mode 100644 index 0000000..4c14691 --- /dev/null +++ b/src/common/thread-pool.h @@ -0,0 +1,27 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _THREAD_POOL_H +#define _THREAD_POOL_H + +#include "utils.h" + +typedef void (*bc_threadpool_func_t) (void *job, void *user_data); + +typedef struct { + bc_slist_t *jobs; + bc_slist_t *threads; + size_t max_threads; + bc_threadpool_func_t func; + void *user_data; +} bc_threadpool_t; + +bc_threadpool_t* bc_threadpool_new(bc_threadpool_func_t func, + size_t max_threads, void *user_data, bc_error_t **err); + +#endif /* _THREAD_POOL_H */ -- cgit v1.2.3-18-g5258