comparison src/core/ngx_thread_pool.c @ 6025:32099b107191

Thread pools: keep waiting tasks counter in ngx_thread_pool_t. It's not needed for completed tasks queue. No functional changes.
author Ruslan Ermilov <ru@nginx.com>
date Thu, 19 Mar 2015 23:19:35 +0300
parents 466bd63b63d1
children 25fda43e3bcb
comparison
equal deleted inserted replaced
6024:199c0dd313ea 6025:32099b107191
16 } ngx_thread_pool_conf_t; 16 } ngx_thread_pool_conf_t;
17 17
18 18
19 typedef struct { 19 typedef struct {
20 ngx_thread_mutex_t mtx; 20 ngx_thread_mutex_t mtx;
21 ngx_uint_t count;
22 ngx_thread_task_t *first; 21 ngx_thread_task_t *first;
23 ngx_thread_task_t **last; 22 ngx_thread_task_t **last;
24 } ngx_thread_pool_queue_t; 23 } ngx_thread_pool_queue_t;
25 24
26 25
27 struct ngx_thread_pool_s { 26 struct ngx_thread_pool_s {
27 ngx_thread_pool_queue_t queue;
28 ngx_uint_t waiting;
28 ngx_thread_cond_t cond; 29 ngx_thread_cond_t cond;
29
30 ngx_thread_pool_queue_t queue;
31 30
32 ngx_log_t *log; 31 ngx_log_t *log;
33 ngx_pool_t *pool; 32 ngx_pool_t *pool;
34 33
35 ngx_str_t name; 34 ngx_str_t name;
161 160
162 161
163 static ngx_int_t 162 static ngx_int_t
164 ngx_thread_pool_queue_init(ngx_thread_pool_queue_t *queue, ngx_log_t *log) 163 ngx_thread_pool_queue_init(ngx_thread_pool_queue_t *queue, ngx_log_t *log)
165 { 164 {
166 queue->count = 0;
167 queue->first = NULL; 165 queue->first = NULL;
168 queue->last = &queue->first; 166 queue->last = &queue->first;
169 167
170 return ngx_thread_mutex_create(&queue->mtx, log); 168 return ngx_thread_mutex_create(&queue->mtx, log);
171 } 169 }
215 213
216 if (ngx_thread_mutex_lock(&tp->queue.mtx, tp->log) != NGX_OK) { 214 if (ngx_thread_mutex_lock(&tp->queue.mtx, tp->log) != NGX_OK) {
217 return NGX_ERROR; 215 return NGX_ERROR;
218 } 216 }
219 217
220 if (tp->queue.count >= tp->max_queue) { 218 if (tp->waiting >= tp->max_queue) {
221 (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log); 219 (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log);
222 220
223 ngx_log_error(NGX_LOG_ERR, tp->log, 0, 221 ngx_log_error(NGX_LOG_ERR, tp->log, 0,
224 "thread pool \"%V\" queue overflow: %ui tasks waiting", 222 "thread pool \"%V\" queue overflow: %ui tasks waiting",
225 &tp->name, tp->queue.count); 223 &tp->name, tp->waiting);
226 return NGX_ERROR; 224 return NGX_ERROR;
227 } 225 }
228 226
229 task->event.active = 1; 227 task->event.active = 1;
230 228
237 } 235 }
238 236
239 *tp->queue.last = task; 237 *tp->queue.last = task;
240 tp->queue.last = &task->next; 238 tp->queue.last = &task->next;
241 239
242 tp->queue.count++; 240 tp->waiting++;
243 241
244 (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log); 242 (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log);
245 243
246 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, 244 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0,
247 "task #%ui added to thread pool \"%V\"", 245 "task #%ui added to thread pool \"%V\"",
283 for ( ;; ) { 281 for ( ;; ) {
284 if (ngx_thread_mutex_lock(&tp->queue.mtx, tp->log) != NGX_OK) { 282 if (ngx_thread_mutex_lock(&tp->queue.mtx, tp->log) != NGX_OK) {
285 return NULL; 283 return NULL;
286 } 284 }
287 285
288 while (tp->queue.count == 0) { 286 while (tp->waiting == 0) {
289 if (ngx_thread_cond_wait(&tp->cond, &tp->queue.mtx, tp->log) 287 if (ngx_thread_cond_wait(&tp->cond, &tp->queue.mtx, tp->log)
290 != NGX_OK) 288 != NGX_OK)
291 { 289 {
292 (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log); 290 (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log);
293 return NULL; 291 return NULL;
294 } 292 }
295 } 293 }
296 294
297 tp->queue.count--; 295 tp->waiting--;
298 296
299 task = tp->queue.first; 297 task = tp->queue.first;
300 tp->queue.first = task->next; 298 tp->queue.first = task->next;
301 299
302 if (tp->queue.first == NULL) { 300 if (tp->queue.first == NULL) {