aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2018-01-29 22:14:12 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2018-01-29 22:14:12 +0100
commit4467e518e963135c0e827e889d242cdce9164b52 (patch)
treeadcbe523a513a9e64bc4dbdd6735f9630ebbf5f5 /src
parente01115702ad73c32efdf11b97d84a81f59300e1d (diff)
downloadblogc-feature/thread-pool.tar.gz
blogc-feature/thread-pool.tar.bz2
blogc-feature/thread-pool.zip
Diffstat (limited to 'src')
-rw-r--r--src/blogc-make/main.c7
-rw-r--r--src/common/thread-pool.c29
-rw-r--r--src/common/thread-pool.h3
3 files changed, 36 insertions, 3 deletions
diff --git a/src/blogc-make/main.c b/src/blogc-make/main.c
index 96a145b..02ab3b5 100644
--- a/src/blogc-make/main.c
+++ b/src/blogc-make/main.c
@@ -50,6 +50,8 @@ print_usage(void)
}
+
+#include "../common/thread-pool.h"
int
#ifdef MAKE_EMBEDDED
bm_main(int argc, char **argv)
@@ -58,6 +60,11 @@ main(int argc, char **argv)
#endif
{
setlocale(LC_ALL, "");
+ bc_threadpool_t *tp = bc_threadpool_new(NULL, 20, NULL, NULL);
+ bc_threadpool_append(tp, "bola");
+ bc_threadpool_append(tp, "guda");
+ sleep(10);
+ return 0;
int rv = 0;
bc_error_t *err = NULL;
diff --git a/src/common/thread-pool.c b/src/common/thread-pool.c
index ad345bb..1cc14ab 100644
--- a/src/common/thread-pool.c
+++ b/src/common/thread-pool.c
@@ -8,26 +8,42 @@
#include <errno.h>
#include <pthread.h>
+#include <stdbool.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;
+typedef struct {
+ bc_threadpool_t *pool;
+ void *user_data;
+} job_t;
+
static void*
worker(void *arg)
{
thread_info_t *info = arg;
+ printf("aqui\n");
+
+ while (1) {
+ job_t *job = NULL;
+
+ pthread_mutex_lock(&(info->pool->jobs_mutex));
+ info->pool->jobs = bc_slist_pop(info->pool->jobs, (void**) &job);
+ pthread_mutex_unlock(&(info->pool->jobs_mutex));
+
+ if (job != NULL)
+ printf("dentro[%d]: %s\n", info->id, job);
+ }
return NULL;
}
@@ -42,6 +58,7 @@ bc_threadpool_new(bc_threadpool_func_t func, size_t max_threads,
bc_threadpool_t *rv = bc_malloc(sizeof(bc_threadpool_t));
rv->jobs = NULL;
+ pthread_mutex_init(&(rv->jobs_mutex), NULL);
rv->threads = NULL;
rv->func = func;
rv->max_threads = max_threads;
@@ -67,4 +84,10 @@ bc_threadpool_new(bc_threadpool_func_t func, size_t max_threads,
}
-
+void
+bc_threadpool_append(bc_threadpool_t *pool, void *user_data)
+{
+ pthread_mutex_lock(&(pool->jobs_mutex));
+ pool->jobs = bc_slist_append(pool->jobs, user_data);
+ pthread_mutex_unlock(&(pool->jobs_mutex));
+}
diff --git a/src/common/thread-pool.h b/src/common/thread-pool.h
index 4c14691..f318a38 100644
--- a/src/common/thread-pool.h
+++ b/src/common/thread-pool.h
@@ -9,12 +9,14 @@
#ifndef _THREAD_POOL_H
#define _THREAD_POOL_H
+#include <pthread.h>
#include "utils.h"
typedef void (*bc_threadpool_func_t) (void *job, void *user_data);
typedef struct {
bc_slist_t *jobs;
+ pthread_mutex_t jobs_mutex;
bc_slist_t *threads;
size_t max_threads;
bc_threadpool_func_t func;
@@ -23,5 +25,6 @@ typedef struct {
bc_threadpool_t* bc_threadpool_new(bc_threadpool_func_t func,
size_t max_threads, void *user_data, bc_error_t **err);
+void bc_threadpool_append(bc_threadpool_t *pool, void *user_data);
#endif /* _THREAD_POOL_H */