remove thread index from threadpool

Sat, 18 Jul 2026 20:34:33 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 18 Jul 2026 20:34:33 +0200
changeset 738
677bff22d1b2
parent 737
b240ff52cee5
child 739
f5ccacc7bab8

remove thread index from threadpool

src/server/public/nsapi.h file | annotate | diff | comparison | revisions
src/server/util/thrpool.c file | annotate | diff | comparison | revisions
src/server/util/thrpool.h file | annotate | diff | comparison | revisions
--- a/src/server/public/nsapi.h	Thu Mar 19 21:00:01 2026 +0100
+++ b/src/server/public/nsapi.h	Sat Jul 18 20:34:33 2026 +0200
@@ -1626,7 +1626,7 @@
 threadpool_t* threadpool_new(int min, int max);
 int threadpool_start(threadpool_t *pool);
 void* threadpool_func(void *data);
-threadpool_job* threadpool_get_job(threadpool_t *pool, int thread_index);
+threadpool_job* threadpool_get_job(threadpool_t *pool, pthread_t thr);
 void threadpool_run(threadpool_t *pool, job_callback_f func, void *data);
 
 int event_pollin(EventHandler *ev, SYS_NETFD fd, Event *event);
--- a/src/server/util/thrpool.c	Thu Mar 19 21:00:01 2026 +0100
+++ b/src/server/util/thrpool.c	Sat Jul 18 20:34:33 2026 +0200
@@ -46,7 +46,6 @@
     pool->max_threads = max;
     pool->num_threads = 0;
     pool->last_job = 0;
-    pool->last_thread = -1;
     
     pool->threads = calloc(max, sizeof(pthread_t));
     pool->thrstatus = calloc(max, sizeof(int));
@@ -76,22 +75,10 @@
     threadpool_t *pool = (threadpool_t*)data;
     
     pthread_t thr_self = pthread_self();
-    int thr_index = -1;
-    for(int i=0;i<pool->max_threads;i++) {
-        if(pool->threads[i] == thr_self) {
-            thr_index = i; // TODO: this is stupid, trasfer the thread index per data
-            break;
-        }
-    }
-    
-    if(thr_index == -1) {
-        log_ereport(LOG_CATASTROPHE, "threadpool: cannot find thread index for thread %ull", (unsigned long long)thr_self);
-        return NULL;
-    }
     
     ws_atomic_inc32(&pool->num_threads);
     for(;;) {
-        threadpool_job *job = threadpool_get_job(pool, thr_index);
+        threadpool_job *job = threadpool_get_job(pool, thr_self);
         if(job == &kill_job) {
             break;
         }
@@ -107,7 +94,7 @@
     return NULL;
 }
 
-threadpool_job* threadpool_get_job(threadpool_t *pool, int thread_index) {
+threadpool_job* threadpool_get_job(threadpool_t *pool, pthread_t thr) {
     struct timespec timeout;
     clock_gettime(CLOCK_REALTIME, &timeout);
     timeout.tv_sec += 30;
@@ -123,7 +110,7 @@
         if(pool->queue_len == 0) {
             timeout.tv_sec += 30;
             while(pthread_cond_timedwait(&pool->available, &pool->queue_lock, &timeout)) {
-                log_ereport(LOG_DEBUG, "threadpool_get_job: cond timeout: thread: %d queue: %u", thread_index, (unsigned int)pool->queue_len);
+                log_ereport(LOG_DEBUG, "threadpool_get_job: cond timeout: thread: %d queue: %ull", (unsigned long long)thr, (unsigned int)pool->queue_len);
                 timeout.tv_sec += 60;
             }
             continue;
@@ -137,7 +124,6 @@
     }
     pool->num_idle--;
     
-    pool->last_thread = thread_index;
     pool->last_job = time(NULL);
 
     pthread_mutex_unlock(&pool->queue_lock);
--- a/src/server/util/thrpool.h	Thu Mar 19 21:00:01 2026 +0100
+++ b/src/server/util/thrpool.h	Sat Jul 18 20:34:33 2026 +0200
@@ -51,7 +51,6 @@
     pthread_t       *threads;
     int             *thrstatus;
     time_t          last_job;
-    int             last_thread;
 };
 
 struct _threadpool_job {

mercurial