Mercurial > hg > nginx
annotate src/core/ngx_thread_pool.c @ 6042:abde398f34a7
Thread pools: implemented graceful exiting of threads.
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Mon, 23 Mar 2015 17:51:21 +0300 |
parents | 2097cd49a158 |
children | 657e029bac28 |
rev | line source |
---|---|
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
1 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
2 /* |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
3 * Copyright (C) Nginx, Inc. |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
4 * Copyright (C) Valentin V. Bartenev |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
5 * Copyright (C) Ruslan Ermilov |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
6 */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
7 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
8 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
9 #include <ngx_config.h> |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
10 #include <ngx_core.h> |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
11 #include <ngx_thread_pool.h> |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
12 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
13 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
14 typedef struct { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
15 ngx_array_t pools; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
16 } ngx_thread_pool_conf_t; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
17 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
18 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
19 typedef struct { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
20 ngx_thread_task_t *first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
21 ngx_thread_task_t **last; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
22 } ngx_thread_pool_queue_t; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
23 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
24 #define ngx_thread_pool_queue_init(q) \ |
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
25 (q)->first = NULL; \ |
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
26 (q)->last = &(q)->first |
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
27 |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
28 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
29 struct ngx_thread_pool_s { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
30 ngx_thread_mutex_t mtx; |
6025
32099b107191
Thread pools: keep waiting tasks counter in ngx_thread_pool_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
31 ngx_thread_pool_queue_t queue; |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
32 ngx_int_t waiting; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
33 ngx_thread_cond_t cond; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
34 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
35 ngx_log_t *log; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
36 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
37 ngx_str_t name; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
38 ngx_uint_t threads; |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
39 ngx_int_t max_queue; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
40 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
41 u_char *file; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
42 ngx_uint_t line; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
43 }; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
44 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
45 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
46 static ngx_int_t ngx_thread_pool_init(ngx_thread_pool_t *tp, ngx_log_t *log, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
47 ngx_pool_t *pool); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
48 static void ngx_thread_pool_destroy(ngx_thread_pool_t *tp); |
6042
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
49 static void ngx_thread_pool_exit_handler(void *data, ngx_log_t *log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
50 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
51 static void *ngx_thread_pool_cycle(void *data); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
52 static void ngx_thread_pool_handler(ngx_event_t *ev); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
53 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
54 static char *ngx_thread_pool(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
55 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
56 static void *ngx_thread_pool_create_conf(ngx_cycle_t *cycle); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
57 static char *ngx_thread_pool_init_conf(ngx_cycle_t *cycle, void *conf); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
58 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
59 static ngx_int_t ngx_thread_pool_init_worker(ngx_cycle_t *cycle); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
60 static void ngx_thread_pool_exit_worker(ngx_cycle_t *cycle); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
61 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
62 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
63 static ngx_command_t ngx_thread_pool_commands[] = { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
64 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
65 { ngx_string("thread_pool"), |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
66 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE23, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
67 ngx_thread_pool, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
68 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
69 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
70 NULL }, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
71 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
72 ngx_null_command |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
73 }; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
74 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
75 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
76 static ngx_core_module_t ngx_thread_pool_module_ctx = { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
77 ngx_string("thread_pool"), |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
78 ngx_thread_pool_create_conf, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
79 ngx_thread_pool_init_conf |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
80 }; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
81 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
82 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
83 ngx_module_t ngx_thread_pool_module = { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
84 NGX_MODULE_V1, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
85 &ngx_thread_pool_module_ctx, /* module context */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
86 ngx_thread_pool_commands, /* module directives */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
87 NGX_CORE_MODULE, /* module type */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
88 NULL, /* init master */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
89 NULL, /* init module */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
90 ngx_thread_pool_init_worker, /* init process */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
91 NULL, /* init thread */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
92 NULL, /* exit thread */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
93 ngx_thread_pool_exit_worker, /* exit process */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
94 NULL, /* exit master */ |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
95 NGX_MODULE_V1_PADDING |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
96 }; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
97 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
98 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
99 static ngx_str_t ngx_thread_pool_default = ngx_string("default"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
100 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
101 static ngx_uint_t ngx_thread_pool_task_id; |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
102 static ngx_atomic_t ngx_thread_pool_done_lock; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
103 static ngx_thread_pool_queue_t ngx_thread_pool_done; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
104 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
105 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
106 static ngx_int_t |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
107 ngx_thread_pool_init(ngx_thread_pool_t *tp, ngx_log_t *log, ngx_pool_t *pool) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
108 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
109 int err; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
110 pthread_t tid; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
111 ngx_uint_t n; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
112 pthread_attr_t attr; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
113 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
114 if (ngx_notify == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
115 ngx_log_error(NGX_LOG_ALERT, log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
116 "the configured event method cannot be used with thread pools"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
117 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
118 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
119 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
120 ngx_thread_pool_queue_init(&tp->queue); |
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
121 |
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
122 if (ngx_thread_mutex_create(&tp->mtx, log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
123 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
124 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
125 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
126 if (ngx_thread_cond_create(&tp->cond, log) != NGX_OK) { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
127 (void) ngx_thread_mutex_destroy(&tp->mtx, log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
128 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
129 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
130 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
131 tp->log = log; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
132 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
133 err = pthread_attr_init(&attr); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
134 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
135 ngx_log_error(NGX_LOG_ALERT, log, err, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
136 "pthread_attr_init() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
137 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
138 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
139 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
140 #if 0 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
141 err = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
142 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
143 ngx_log_error(NGX_LOG_ALERT, log, err, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
144 "pthread_attr_setstacksize() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
145 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
146 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
147 #endif |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
148 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
149 for (n = 0; n < tp->threads; n++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
150 err = pthread_create(&tid, &attr, ngx_thread_pool_cycle, tp); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
151 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
152 ngx_log_error(NGX_LOG_ALERT, log, err, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
153 "pthread_create() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
154 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
155 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
156 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
157 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
158 (void) pthread_attr_destroy(&attr); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
159 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
160 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
161 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
162 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
163 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
164 static void |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
165 ngx_thread_pool_destroy(ngx_thread_pool_t *tp) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
166 { |
6042
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
167 ngx_uint_t n; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
168 ngx_thread_task_t task; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
169 volatile ngx_uint_t lock; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
170 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
171 ngx_memzero(&task, sizeof(ngx_thread_task_t)); |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
172 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
173 task.handler = ngx_thread_pool_exit_handler; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
174 task.ctx = (void *) &lock; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
175 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
176 for (n = 0; n < tp->threads; n++) { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
177 lock = 1; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
178 |
6042
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
179 if (ngx_thread_task_post(tp, &task) != NGX_OK) { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
180 return; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
181 } |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
182 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
183 while (lock) { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
184 ngx_sched_yield(); |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
185 } |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
186 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
187 task.event.active = 0; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
188 } |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
189 |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
190 (void) ngx_thread_cond_destroy(&tp->cond, tp->log); |
6027
67717d4e4f47
Thread pools: silence warning on process exit.
Ruslan Ermilov <ru@nginx.com>
parents:
6026
diff
changeset
|
191 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
192 (void) ngx_thread_mutex_destroy(&tp->mtx, tp->log); |
6042
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
193 } |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
194 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
195 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
196 static void |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
197 ngx_thread_pool_exit_handler(void *data, ngx_log_t *log) |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
198 { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
199 ngx_uint_t *lock = data; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
200 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
201 *lock = 0; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
202 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
203 pthread_exit(0); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
204 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
205 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
206 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
207 ngx_thread_task_t * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
208 ngx_thread_task_alloc(ngx_pool_t *pool, size_t size) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
209 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
210 ngx_thread_task_t *task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
211 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
212 task = ngx_pcalloc(pool, sizeof(ngx_thread_task_t) + size); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
213 if (task == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
214 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
215 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
216 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
217 task->ctx = task + 1; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
218 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
219 return task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
220 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
221 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
222 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
223 ngx_int_t |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
224 ngx_thread_task_post(ngx_thread_pool_t *tp, ngx_thread_task_t *task) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
225 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
226 if (task->event.active) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
227 ngx_log_error(NGX_LOG_ALERT, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
228 "task #%ui already active", task->id); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
229 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
230 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
231 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
232 if (ngx_thread_mutex_lock(&tp->mtx, tp->log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
233 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
234 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
235 |
6025
32099b107191
Thread pools: keep waiting tasks counter in ngx_thread_pool_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
236 if (tp->waiting >= tp->max_queue) { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
237 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
238 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
239 ngx_log_error(NGX_LOG_ERR, tp->log, 0, |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
240 "thread pool \"%V\" queue overflow: %i tasks waiting", |
6025
32099b107191
Thread pools: keep waiting tasks counter in ngx_thread_pool_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
241 &tp->name, tp->waiting); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
242 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
243 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
244 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
245 task->event.active = 1; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
246 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
247 task->id = ngx_thread_pool_task_id++; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
248 task->next = NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
249 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
250 if (ngx_thread_cond_signal(&tp->cond, tp->log) != NGX_OK) { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
251 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
252 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
253 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
254 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
255 *tp->queue.last = task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
256 tp->queue.last = &task->next; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
257 |
6025
32099b107191
Thread pools: keep waiting tasks counter in ngx_thread_pool_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
258 tp->waiting++; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
259 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
260 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
261 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
262 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
263 "task #%ui added to thread pool \"%V\"", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
264 task->id, &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
265 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
266 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
267 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
268 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
269 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
270 static void * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
271 ngx_thread_pool_cycle(void *data) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
272 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
273 ngx_thread_pool_t *tp = data; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
274 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
275 int err; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
276 sigset_t set; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
277 ngx_thread_task_t *task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
278 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
279 #if 0 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
280 ngx_time_update(); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
281 #endif |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
282 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
283 ngx_log_debug1(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
284 "thread in pool \"%V\" started", &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
285 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
286 sigfillset(&set); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
287 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
288 sigdelset(&set, SIGILL); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
289 sigdelset(&set, SIGFPE); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
290 sigdelset(&set, SIGSEGV); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
291 sigdelset(&set, SIGBUS); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
292 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
293 err = pthread_sigmask(SIG_BLOCK, &set, NULL); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
294 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
295 ngx_log_error(NGX_LOG_ALERT, tp->log, err, "pthread_sigmask() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
296 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
297 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
298 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
299 for ( ;; ) { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
300 if (ngx_thread_mutex_lock(&tp->mtx, tp->log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
301 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
302 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
303 |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
304 /* the number may become negative */ |
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
305 tp->waiting--; |
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
306 |
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
307 while (tp->queue.first == NULL) { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
308 if (ngx_thread_cond_wait(&tp->cond, &tp->mtx, tp->log) |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
309 != NGX_OK) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
310 { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
311 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
312 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
313 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
314 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
315 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
316 task = tp->queue.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
317 tp->queue.first = task->next; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
318 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
319 if (tp->queue.first == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
320 tp->queue.last = &tp->queue.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
321 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
322 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
323 if (ngx_thread_mutex_unlock(&tp->mtx, tp->log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
324 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
325 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
326 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
327 #if 0 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
328 ngx_time_update(); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
329 #endif |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
330 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
331 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
332 "run task #%ui in thread pool \"%V\"", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
333 task->id, &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
334 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
335 task->handler(task->ctx, tp->log); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
336 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
337 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
338 "complete task #%ui in thread pool \"%V\"", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
339 task->id, &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
340 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
341 task->next = NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
342 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
343 ngx_spinlock(&ngx_thread_pool_done_lock, 1, 2048); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
344 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
345 *ngx_thread_pool_done.last = task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
346 ngx_thread_pool_done.last = &task->next; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
347 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
348 ngx_unlock(&ngx_thread_pool_done_lock); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
349 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
350 (void) ngx_notify(ngx_thread_pool_handler); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
351 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
352 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
353 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
354 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
355 static void |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
356 ngx_thread_pool_handler(ngx_event_t *ev) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
357 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
358 ngx_event_t *event; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
359 ngx_thread_task_t *task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
360 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
361 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ev->log, 0, "thread pool handler"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
362 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
363 ngx_spinlock(&ngx_thread_pool_done_lock, 1, 2048); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
364 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
365 task = ngx_thread_pool_done.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
366 ngx_thread_pool_done.first = NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
367 ngx_thread_pool_done.last = &ngx_thread_pool_done.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
368 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
369 ngx_unlock(&ngx_thread_pool_done_lock); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
370 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
371 while (task) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
372 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
373 "run completion handler for task #%ui", task->id); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
374 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
375 event = &task->event; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
376 task = task->next; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
377 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
378 event->complete = 1; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
379 event->active = 0; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
380 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
381 event->handler(event); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
382 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
383 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
384 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
385 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
386 static void * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
387 ngx_thread_pool_create_conf(ngx_cycle_t *cycle) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
388 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
389 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
390 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
391 tcf = ngx_pcalloc(cycle->pool, sizeof(ngx_thread_pool_conf_t)); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
392 if (tcf == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
393 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
394 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
395 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
396 if (ngx_array_init(&tcf->pools, cycle->pool, 4, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
397 sizeof(ngx_thread_pool_t *)) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
398 != NGX_OK) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
399 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
400 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
401 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
402 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
403 return tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
404 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
405 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
406 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
407 static char * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
408 ngx_thread_pool_init_conf(ngx_cycle_t *cycle, void *conf) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
409 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
410 ngx_thread_pool_conf_t *tcf = conf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
411 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
412 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
413 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
414 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
415 tpp = tcf->pools.elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
416 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
417 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
418 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
419 if (tpp[i]->threads) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
420 continue; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
421 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
422 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
423 if (tpp[i]->name.len == ngx_thread_pool_default.len |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
424 && ngx_strncmp(tpp[i]->name.data, ngx_thread_pool_default.data, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
425 ngx_thread_pool_default.len) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
426 == 0) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
427 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
428 tpp[i]->threads = 32; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
429 tpp[i]->max_queue = 65536; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
430 continue; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
431 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
432 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
433 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
434 "unknown thread pool \"%V\" in %s:%ui", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
435 &tpp[i]->name, tpp[i]->file, tpp[i]->line); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
436 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
437 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
438 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
439 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
440 return NGX_CONF_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
441 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
442 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
443 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
444 static char * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
445 ngx_thread_pool(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
446 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
447 ngx_str_t *value; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
448 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
449 ngx_thread_pool_t *tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
450 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
451 value = cf->args->elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
452 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
453 tp = ngx_thread_pool_add(cf, &value[1]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
454 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
455 if (tp == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
456 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
457 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
458 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
459 if (tp->threads) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
460 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
461 "duplicate thread pool \"%V\"", &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
462 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
463 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
464 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
465 tp->max_queue = 65536; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
466 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
467 for (i = 2; i < cf->args->nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
468 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
469 if (ngx_strncmp(value[i].data, "threads=", 8) == 0) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
470 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
471 tp->threads = ngx_atoi(value[i].data + 8, value[i].len - 8); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
472 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
473 if (tp->threads == (ngx_uint_t) NGX_ERROR || tp->threads == 0) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
474 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
475 "invalid threads value \"%V\"", &value[i]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
476 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
477 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
478 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
479 continue; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
480 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
481 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
482 if (ngx_strncmp(value[i].data, "max_queue=", 10) == 0) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
483 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
484 tp->max_queue = ngx_atoi(value[i].data + 10, value[i].len - 10); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
485 |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
486 if (tp->max_queue == NGX_ERROR) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
487 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
488 "invalid max_queue value \"%V\"", &value[i]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
489 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
490 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
491 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
492 continue; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
493 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
494 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
495 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
496 if (tp->threads == 0) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
497 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
498 "\"%V\" must have \"threads\" parameter", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
499 &cmd->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
500 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
501 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
502 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
503 return NGX_CONF_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
504 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
505 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
506 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
507 ngx_thread_pool_t * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
508 ngx_thread_pool_add(ngx_conf_t *cf, ngx_str_t *name) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
509 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
510 ngx_thread_pool_t *tp, **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
511 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
512 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
513 if (name == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
514 name = &ngx_thread_pool_default; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
515 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
516 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
517 tp = ngx_thread_pool_get(cf->cycle, name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
518 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
519 if (tp) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
520 return tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
521 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
522 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
523 tp = ngx_pcalloc(cf->pool, sizeof(ngx_thread_pool_t)); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
524 if (tp == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
525 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
526 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
527 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
528 tp->name = *name; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
529 tp->file = cf->conf_file->file.name.data; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
530 tp->line = cf->conf_file->line; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
531 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
532 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cf->cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
533 ngx_thread_pool_module); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
534 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
535 tpp = ngx_array_push(&tcf->pools); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
536 if (tpp == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
537 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
538 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
539 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
540 *tpp = tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
541 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
542 return tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
543 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
544 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
545 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
546 ngx_thread_pool_t * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
547 ngx_thread_pool_get(ngx_cycle_t *cycle, ngx_str_t *name) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
548 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
549 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
550 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
551 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
552 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
553 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
554 ngx_thread_pool_module); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
555 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
556 tpp = tcf->pools.elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
557 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
558 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
559 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
560 if (tpp[i]->name.len == name->len |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
561 && ngx_strncmp(tpp[i]->name.data, name->data, name->len) == 0) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
562 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
563 return tpp[i]; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
564 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
565 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
566 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
567 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
568 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
569 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
570 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
571 static ngx_int_t |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
572 ngx_thread_pool_init_worker(ngx_cycle_t *cycle) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
573 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
574 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
575 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
576 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
577 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
578 if (ngx_process != NGX_PROCESS_WORKER |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
579 && ngx_process != NGX_PROCESS_SINGLE) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
580 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
581 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
582 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
583 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
584 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
585 ngx_thread_pool_module); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
586 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
587 if (tcf == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
588 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
589 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
590 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
591 ngx_thread_pool_queue_init(&ngx_thread_pool_done); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
592 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
593 tpp = tcf->pools.elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
594 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
595 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
596 if (ngx_thread_pool_init(tpp[i], cycle->log, cycle->pool) != NGX_OK) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
597 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
598 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
599 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
600 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
601 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
602 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
603 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
604 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
605 static void |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
606 ngx_thread_pool_exit_worker(ngx_cycle_t *cycle) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
607 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
608 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
609 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
610 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
611 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
612 if (ngx_process != NGX_PROCESS_WORKER |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
613 && ngx_process != NGX_PROCESS_SINGLE) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
614 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
615 return; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
616 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
617 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
618 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
619 ngx_thread_pool_module); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
620 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
621 if (tcf == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
622 return; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
623 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
624 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
625 tpp = tcf->pools.elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
626 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
627 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
628 ngx_thread_pool_destroy(tpp[i]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
629 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
630 } |