Mon, 26 May 2025 21:02:30 +0200
memset threadpool object
| 1 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
|
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
| 1 | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include <stdio.h> | |
| 30 | #include <stdlib.h> | |
| 31 | #include <unistd.h> | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
32 | |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
33 | #include "atomic.h" |
| 1 | 34 | #include "thrpool.h" |
| 35 | ||
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
36 | static threadpool_job kill_job; |
| 1 | 37 | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
38 | threadpool_t* threadpool_new(int min, int max) { |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
39 | log_ereport(LOG_INFORM, "new threadpool (min: %d, max: %d)", min, max); |
| 1 | 40 | threadpool_t *pool = malloc(sizeof(threadpool_t)); |
|
581
4a049e416021
memset threadpool object
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
577
diff
changeset
|
41 | memset(pool, 0, sizeof(threadpool_t)); |
| 1 | 42 | pool->queue = NULL; |
| 43 | pool->queue_len = 0; | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
44 | pool->num_idle = 0; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
45 | pool->min_threads = min; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
46 | pool->max_threads = max; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
47 | pool->num_threads = 0; |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
48 | pool->last_job = 0; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
49 | pool->last_thread = -1; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
50 | |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
51 | pool->threads = calloc(max, sizeof(pthread_t)); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
52 | pool->thrstatus = calloc(max, sizeof(int)); |
| 1 | 53 | |
| 54 | pthread_mutex_init(&pool->queue_lock, NULL); | |
| 55 | pthread_mutex_init(&pool->avlbl_lock, NULL); | |
|
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
56 | pthread_cond_init(&pool->available, NULL); |
| 1 | 57 | |
|
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
58 | return pool; |
|
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
59 | } |
|
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
60 | |
|
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
61 | int threadpool_start(threadpool_t *pool) { |
| 1 | 62 | /* create pool threads */ |
|
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
63 | for(int i=0;i<pool->min_threads;i++) { |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
64 | if (pthread_create(&pool->threads[i], NULL, threadpool_func, pool) != 0) { |
|
408
56edda8701e0
replace perror() messages with log_ereport in thrpool.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
358
diff
changeset
|
65 | log_ereport(LOG_FAILURE, "threadpool_start: pthread_create failed: %s", strerror(errno)); |
|
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
66 | return 1; |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
67 | } else { |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
68 | log_ereport(LOG_DEBUG, "thread started: %lu", (unsigned long)pool->threads[i]); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
69 | pthread_detach(pool->threads[i]); |
| 1 | 70 | } |
| 71 | } | |
|
357
f45e962edf45
add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
72 | return 0; |
| 1 | 73 | } |
| 74 | ||
| 75 | void* threadpool_func(void *data) { | |
| 76 | threadpool_t *pool = (threadpool_t*)data; | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
77 | |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
78 | pthread_t thr_self = pthread_self(); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
79 | int thr_index = -1; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
80 | for(int i=0;i<pool->max_threads;i++) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
81 | if(pool->threads[i] == thr_self) { |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
82 | thr_index = i; // TODO: this is stupid, trasfer the thread index per data |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
83 | break; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
84 | } |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
85 | } |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
86 | |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
87 | if(thr_index == -1) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
88 | log_ereport(LOG_CATASTROPHE, "threadpool: cannot find thread index for thread %ull\n", (unsigned long long)thr_self); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
89 | return NULL; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
90 | } |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
91 | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
92 | ws_atomic_inc32(&pool->num_threads); |
| 1 | 93 | for(;;) { |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
94 | threadpool_job *job = threadpool_get_job(pool, thr_index); |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
95 | if(job == &kill_job) { |
| 1 | 96 | break; |
| 97 | } | |
| 98 | ||
| 99 | job->callback(job->data); | |
| 100 | ||
| 101 | free(job); | |
| 102 | } | |
|
556
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
103 | uint32_t nthreads = ws_atomic_dec32(&pool->num_threads); |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
104 | if(nthreads == 0) { |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
105 | log_ereport(LOG_INFORM, "threadpool closed"); // TODO: log threadpool name |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
106 | } |
| 1 | 107 | return NULL; |
| 108 | } | |
| 109 | ||
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
110 | threadpool_job* threadpool_get_job(threadpool_t *pool, int thread_index) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
111 | struct timespec timeout; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
112 | clock_gettime(CLOCK_REALTIME, &timeout); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
113 | timeout.tv_sec += 30; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
114 | |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
115 | while(pthread_mutex_timedlock(&pool->queue_lock, &timeout)) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
116 | log_ereport(LOG_INFORM, "threadpool_get_job: mutex timeout"); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
117 | timeout.tv_sec += 30; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
118 | } |
| 1 | 119 | |
| 120 | threadpool_job *job = NULL; | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
121 | pool->num_idle++; |
| 1 | 122 | while(job == NULL) { |
| 123 | if(pool->queue_len == 0) { | |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
124 | timeout.tv_sec += 30; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
125 | while(pthread_cond_timedwait(&pool->available, &pool->queue_lock, &timeout)) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
126 | log_ereport(LOG_DEBUG, "threadpool_get_job: cond timeout: thread: %d queue: %u", thread_index, (unsigned int)pool->queue_len); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
127 | timeout.tv_sec += 60; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
128 | } |
| 1 | 129 | continue; |
| 130 | } else { | |
| 131 | pool_queue_t *q = pool->queue; | |
| 132 | job = q->job; | |
| 133 | pool->queue = q->next; | |
| 134 | pool->queue_len--; | |
| 135 | free(q); | |
| 136 | } | |
| 137 | } | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
138 | pool->num_idle--; |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
139 | |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
140 | pool->last_thread = thread_index; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
141 | pool->last_job = time(NULL); |
| 1 | 142 | |
| 143 | pthread_mutex_unlock(&pool->queue_lock); | |
| 144 | return job; | |
| 145 | } | |
| 146 | ||
| 147 | void threadpool_run(threadpool_t *pool, job_callback_f func, void *data) { | |
|
358
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
148 | // TODO: handle errors |
|
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
149 | |
|
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
150 | if(pool->num_threads == 0) { |
|
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
151 | threadpool_start(pool); |
|
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
152 | } |
|
f3b490a2150c
start threadpool in threadpool_run() if no threads are created yet
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
357
diff
changeset
|
153 | |
| 1 | 154 | threadpool_job *job = malloc(sizeof(threadpool_job)); |
| 155 | job->callback = func; | |
| 156 | job->data = data; | |
| 157 | ||
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
158 | struct timespec timeout; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
159 | clock_gettime(CLOCK_REALTIME, &timeout); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
160 | timeout.tv_sec += 30; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
161 | |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
162 | while(pthread_mutex_timedlock(&pool->queue_lock, &timeout)) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
163 | log_ereport(LOG_INFORM, "threadpool_run: mutex timeout"); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
164 | timeout.tv_sec += 30; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
165 | } |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
166 | threadpool_enqueue_job(pool, job); |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
167 | |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
168 | int create_thread = 0; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
169 | int destroy_thread = 0; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
170 | int diff = pool->queue_len - pool->num_idle; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
171 | if(diff > 0 && pool->num_threads < pool->max_threads) { |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
172 | create_thread = 1; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
173 | } else if(diff < 0 && pool->num_threads > pool->min_threads) { |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
174 | destroy_thread = 1; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
175 | } |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
176 | |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
177 | //if(pool->queue_len == 1) { |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
178 | pthread_cond_signal(&pool->available); |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
179 | //} |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
180 | |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
181 | if(create_thread) { |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
182 | pthread_t t; |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
183 | if (pthread_create(&t, NULL, threadpool_func, pool) != 0) { |
|
408
56edda8701e0
replace perror() messages with log_ereport in thrpool.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
358
diff
changeset
|
184 | log_ereport(LOG_FAILURE, "threadpool_run: pthread_create failed: %s", strerror(errno)); |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
185 | } |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
186 | } |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
187 | if(destroy_thread) { |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
188 | threadpool_enqueue_job(pool, &kill_job); |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
189 | pthread_cond_signal(&pool->available); |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
190 | } |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
191 | |
|
569
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
192 | // some diagnostics: |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
193 | // if the queue has multiple elements, but the last job was started |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
194 | // over a minute ago, print some diagnostic message, because |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
195 | // this does look wrong |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
196 | if(pool->queue_len > 5 && pool->last_job != 0) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
197 | // reuse timeout sec value, because we don't need the most accurate |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
198 | // time value here |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
199 | time_t current = timeout.tv_sec; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
200 | if(pool->last_job + 60 < current) { |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
201 | // looks like the threadpool is blocked |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
202 | struct tm lastjob; |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
203 | localtime_r(&pool->last_job, &lastjob); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
204 | log_ereport( |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
205 | LOG_WARN, |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
206 | "high threadpool wait time: queue: %u lastjob: %02d:%02d:%02d", |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
207 | (unsigned int)pool->queue_len, |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
208 | lastjob.tm_hour, lastjob.tm_min, lastjob.tm_sec); |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
209 | } |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
210 | } |
|
70bca6190669
add threadpool debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
556
diff
changeset
|
211 | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
212 | pthread_mutex_unlock(&pool->queue_lock); |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
213 | } |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
214 | |
|
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
215 | void threadpool_enqueue_job(threadpool_t *pool, threadpool_job *job) { |
| 1 | 216 | pool_queue_t *q = malloc(sizeof(pool_queue_t)); |
| 217 | q->job = job; | |
| 218 | q->next = NULL; | |
|
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
219 | |
| 1 | 220 | if(pool->queue == NULL) { |
| 221 | pool->queue = q; | |
| 222 | } else { | |
| 223 | pool_queue_t *last_elem = pool->queue; | |
| 224 | while(last_elem->next != NULL) { | |
| 225 | last_elem = last_elem->next; | |
| 226 | } | |
| 227 | last_elem->next = q; | |
| 228 | } | |
| 229 | pool->queue_len++; | |
| 230 | } | |
|
556
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
231 | |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
232 | void threadpool_shutdown(threadpool_t *pool, int timeout) { |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
233 | struct timespec ts; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
234 | |
|
556
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
235 | int nthreads = pool->max_threads; |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
236 | for(int i=0;i<nthreads;i++) { |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
237 | clock_gettime(CLOCK_REALTIME, &ts); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
238 | ts.tv_sec += timeout; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
239 | if(pthread_mutex_timedlock(&pool->queue_lock, &ts)) { |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
240 | log_ereport(LOG_FAILURE, "failed to shutdown threadpool: timeout"); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
241 | return; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
242 | } |
|
556
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
243 | threadpool_enqueue_job(pool, &kill_job); |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
244 | pthread_cond_signal(&pool->available); |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
245 | pthread_mutex_unlock(&pool->queue_lock); |
|
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
246 | } |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
247 | |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
248 | // not the nicest way to wait for threads to shutdown, but it is very |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
249 | // simple and good enough for the webserver shutdown |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
250 | sleep(1); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
251 | // check if all threads are closed |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
252 | time_t t = time(NULL); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
253 | time_t end = t + timeout; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
254 | int i = 0; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
255 | while(t < end || i < 2) { |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
256 | uint32_t num_threads = pool->num_threads; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
257 | if(num_threads == 0) { |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
258 | break; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
259 | } |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
260 | |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
261 | log_ereport(LOG_VERBOSE, "threadpool_shutdown: wait for thread shutdown: %u threads still running", (unsigned int)num_threads); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
262 | sleep(5); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
263 | i++; |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
264 | t = time(NULL); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
265 | } |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
266 | |
|
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
267 | if(pool->num_threads == 0) { |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
268 | // it's possible that we send too many kill jobs, cleanup the queue |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
269 | while(pool->queue) { |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
270 | pool_queue_t *q = pool->queue->next; |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
271 | free(pool->queue); |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
272 | pool->queue = q; |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
273 | } |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
274 | |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
275 | free(pool->threads); |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
276 | free(pool->thrstatus); |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
277 | free(pool); |
|
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
576
diff
changeset
|
278 | |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
279 | log_ereport(LOG_VERBOSE, "threadpool_shutdown successful"); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
280 | } else if(t > end) { |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
281 | log_ereport(LOG_WARN, "threadpool_shutdown: timeout"); |
|
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
282 | } |
|
556
b036ccad4b49
improve webserver shutdown and free some stuff to make the valgrind output cleaner
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
408
diff
changeset
|
283 | } |
|
570
f95868a8ec37
improve threadpool_shutdown cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
569
diff
changeset
|
284 |