diff options
author | Valentin Bartenev <vbart@nginx.com> | 2015-03-23 17:51:21 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2015-03-23 17:51:21 +0300 |
commit | e87a565aab4da7ade1f016c2bab4371a70998974 (patch) | |
tree | 7cd0a5c3c80cd73b9da14444721f44601a2e8104 /src/core/ngx_thread_pool.c | |
parent | 510c9cdca8368667a52a9710b38fe477f9c0d0e9 (diff) | |
download | nginx-e87a565aab4da7ade1f016c2bab4371a70998974.tar.gz nginx-e87a565aab4da7ade1f016c2bab4371a70998974.zip |
Thread pools: implemented graceful exiting of threads.
Diffstat (limited to 'src/core/ngx_thread_pool.c')
-rw-r--r-- | src/core/ngx_thread_pool.c | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/core/ngx_thread_pool.c b/src/core/ngx_thread_pool.c index c38531794..03530851a 100644 --- a/src/core/ngx_thread_pool.c +++ b/src/core/ngx_thread_pool.c @@ -46,6 +46,7 @@ struct ngx_thread_pool_s { static ngx_int_t ngx_thread_pool_init(ngx_thread_pool_t *tp, ngx_log_t *log, ngx_pool_t *pool); static void ngx_thread_pool_destroy(ngx_thread_pool_t *tp); +static void ngx_thread_pool_exit_handler(void *data, ngx_log_t *log); static void *ngx_thread_pool_cycle(void *data); static void ngx_thread_pool_handler(ngx_event_t *ev); @@ -163,13 +164,43 @@ ngx_thread_pool_init(ngx_thread_pool_t *tp, ngx_log_t *log, ngx_pool_t *pool) static void ngx_thread_pool_destroy(ngx_thread_pool_t *tp) { - /* TODO: exit threads */ + ngx_uint_t n; + ngx_thread_task_t task; + volatile ngx_uint_t lock; + + ngx_memzero(&task, sizeof(ngx_thread_task_t)); + + task.handler = ngx_thread_pool_exit_handler; + task.ctx = (void *) &lock; + + for (n = 0; n < tp->threads; n++) { + lock = 1; + + if (ngx_thread_task_post(tp, &task) != NGX_OK) { + return; + } + + while (lock) { + ngx_sched_yield(); + } + + task.event.active = 0; + } -#if 0 (void) ngx_thread_cond_destroy(&tp->cond, tp->log); (void) ngx_thread_mutex_destroy(&tp->mtx, tp->log); - #endif +} + + +static void +ngx_thread_pool_exit_handler(void *data, ngx_log_t *log) +{ + ngx_uint_t *lock = data; + + *lock = 0; + + pthread_exit(0); } |