diff options
author | Ruslan Ermilov <ru@nginx.com> | 2015-03-19 13:00:48 +0300 |
---|---|---|
committer | Ruslan Ermilov <ru@nginx.com> | 2015-03-19 13:00:48 +0300 |
commit | afe1fcffaae50a0b56555635c9b4debba0ee72df (patch) | |
tree | 4b7f29ce1f38d1d06e1b335f41859082a7996e7a | |
parent | 20d07074e3d1cd9069fd786aa4861a51a44f45b5 (diff) | |
download | nginx-afe1fcffaae50a0b56555635c9b4debba0ee72df.tar.gz nginx-afe1fcffaae50a0b56555635c9b4debba0ee72df.zip |
Thread pools: fixed the waiting tasks accounting.
Behave like POSIX semaphores. If N worker threads are waiting for tasks,
at least that number of tasks should be allowed to be put into the queue.
-rw-r--r-- | src/core/ngx_thread_pool.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/core/ngx_thread_pool.c b/src/core/ngx_thread_pool.c index 0be022758..d6e85202f 100644 --- a/src/core/ngx_thread_pool.c +++ b/src/core/ngx_thread_pool.c @@ -25,7 +25,7 @@ typedef struct { struct ngx_thread_pool_s { ngx_thread_pool_queue_t queue; - ngx_uint_t waiting; + ngx_int_t waiting; ngx_thread_cond_t cond; ngx_log_t *log; @@ -33,7 +33,7 @@ struct ngx_thread_pool_s { ngx_str_t name; ngx_uint_t threads; - ngx_uint_t max_queue; + ngx_int_t max_queue; u_char *file; ngx_uint_t line; @@ -219,7 +219,7 @@ ngx_thread_task_post(ngx_thread_pool_t *tp, ngx_thread_task_t *task) (void) ngx_thread_mutex_unlock(&tp->queue.mtx, tp->log); ngx_log_error(NGX_LOG_ERR, tp->log, 0, - "thread pool \"%V\" queue overflow: %ui tasks waiting", + "thread pool \"%V\" queue overflow: %i tasks waiting", &tp->name, tp->waiting); return NGX_ERROR; } @@ -283,7 +283,10 @@ ngx_thread_pool_cycle(void *data) return NULL; } - while (tp->waiting == 0) { + /* the number may become negative */ + tp->waiting--; + + while (tp->queue.first == NULL) { if (ngx_thread_cond_wait(&tp->cond, &tp->queue.mtx, tp->log) != NGX_OK) { @@ -292,8 +295,6 @@ ngx_thread_pool_cycle(void *data) } } - tp->waiting--; - task = tp->queue.first; tp->queue.first = task->next; @@ -476,7 +477,7 @@ ngx_thread_pool(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) tp->max_queue = ngx_atoi(value[i].data + 10, value[i].len - 10); - if (tp->max_queue == (ngx_uint_t) NGX_ERROR) { + if (tp->max_queue == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid max_queue value \"%V\"", &value[i]); return NGX_CONF_ERROR; |