src/server/util/thrpool.c

changeset 385
a1f4cb076d2f
parent 358
f3b490a2150c
child 408
56edda8701e0
--- a/src/server/util/thrpool.c	Tue Aug 13 22:14:32 2019 +0200
+++ b/src/server/util/thrpool.c	Sat Sep 24 16:26:10 2022 +0200
@@ -47,18 +47,21 @@
 
     pthread_mutex_init(&pool->queue_lock, NULL);
     pthread_mutex_init(&pool->avlbl_lock, NULL);
-    pthread_cond_init(&pool->available, NULL);
+    pthread_cond_init(&pool->available, NULL);  
 
+    return pool;
+}
+
+int threadpool_start(threadpool_t *pool) {
     /* create pool threads */
-    for(int i=0;i<min;i++) {
+    for(int i=0;i<pool->min_threads;i++) {
         pthread_t t;
         if (pthread_create(&t, NULL, threadpool_func, pool) != 0) {
-            perror("Error: threadpool_new: pthread_create");
-            return NULL;
+            perror("Error: threadpool_start: pthread_create");
+            return 1;
         }
     }
-
-    return pool;
+    return 0;
 }
 
 void* threadpool_func(void *data) {
@@ -103,6 +106,12 @@
 }
 
 void threadpool_run(threadpool_t *pool, job_callback_f func, void *data) {
+    // TODO: handle errors
+    
+    if(pool->num_threads == 0) {
+        threadpool_start(pool);
+    }
+    
     threadpool_job *job = malloc(sizeof(threadpool_job));
     job->callback = func;
     job->data = data;

mercurial