aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/thread-pool.c
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-23 02:01:39 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2018-01-28 17:06:02 +0100
commite01115702ad73c32efdf11b97d84a81f59300e1d (patch)
treead59f6e47978e3873cc0f1229d6aaac5aae221e8 /src/common/thread-pool.c
parentab6e81997cf8c5bcf7d1778bb32d8e01425b13f1 (diff)
downloadblogc-e01115702ad73c32efdf11b97d84a81f59300e1d.tar.gz
blogc-e01115702ad73c32efdf11b97d84a81f59300e1d.tar.bz2
blogc-e01115702ad73c32efdf11b97d84a81f59300e1d.zip
common: thread-pool: initial work
Diffstat (limited to 'src/common/thread-pool.c')
-rw-r--r--src/common/thread-pool.c70
1 files changed, 70 insertions, 0 deletions
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 <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#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;
+}
+
+
+