Mercurial > hg > nginx
annotate src/core/ngx_thread_pool.c @ 9322:d6f75dd66761 default tip
Mp4: added and updated sanity checks for "end" handling.
When handling incorrect data in ngx_http_mp4_crop_stsc_data(),
trak->end_chunk_samples might end up being arbitrary large, leading
to reading before the buffer in ngx_http_mp4_update_stsz_atom(). Fix
is to check that trak->end_chunk_samples corresponds to a memory within
the stsz atom data. For consistency, trak->start_chunk_samples
is checked similarly.
Similarly, trak->end_chunk might end up being smaller than trak->start_chunk,
leading to reading memory after the buffer in ngx_http_mp4_update_stco_atom()
and ngx_http_mp4_update_co64_atom(). Corresponding checks are updated
to explicitly test (trak->end_chunk - trak->start_chunk) instead of just
checking trak->end_chunk and assuming it is larger than trak->start_chunk.
This is generally in line with existing checks of
(trak->end_sample - trak->start_sample) in ngx_http_mp4_update_stsz_atom(),
where trak->end_sample might also become smaller than trak->start_sample
when handling incorrect data in ngx_http_mp4_crop_stts_data().
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sun, 25 Aug 2024 06:35:40 +0300 |
parents | 33d075b9097d |
children |
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 |
6667
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
140 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
141 if (err) { |
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
142 ngx_log_error(NGX_LOG_ALERT, log, err, |
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
143 "pthread_attr_setdetachstate() failed"); |
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
144 return NGX_ERROR; |
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
145 } |
33d075b9097d
Thread pools: create threads in detached state.
Piotr Sikora <piotrsikora@google.com>
parents:
6517
diff
changeset
|
146 |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
147 #if 0 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
148 err = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
149 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
150 ngx_log_error(NGX_LOG_ALERT, log, err, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
151 "pthread_attr_setstacksize() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
152 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
153 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
154 #endif |
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 for (n = 0; n < tp->threads; n++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
157 err = pthread_create(&tid, &attr, ngx_thread_pool_cycle, tp); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
158 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
159 ngx_log_error(NGX_LOG_ALERT, log, err, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
160 "pthread_create() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
161 return NGX_ERROR; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
165 (void) pthread_attr_destroy(&attr); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
166 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
167 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
168 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
169 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
170 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
171 static void |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
172 ngx_thread_pool_destroy(ngx_thread_pool_t *tp) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
173 { |
6042
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
174 ngx_uint_t n; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
175 ngx_thread_task_t task; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
176 volatile ngx_uint_t lock; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
177 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
178 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
|
179 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
180 task.handler = ngx_thread_pool_exit_handler; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
181 task.ctx = (void *) &lock; |
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 for (n = 0; n < tp->threads; n++) { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
184 lock = 1; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
185 |
6042
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
186 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
|
187 return; |
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 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
190 while (lock) { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
191 ngx_sched_yield(); |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
192 } |
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 task.event.active = 0; |
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 |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
197 (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
|
198 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
199 (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
|
200 } |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
201 |
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 static void |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
204 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
|
205 { |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
206 ngx_uint_t *lock = data; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
207 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
208 *lock = 0; |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
209 |
abde398f34a7
Thread pools: implemented graceful exiting of threads.
Valentin Bartenev <vbart@nginx.com>
parents:
6041
diff
changeset
|
210 pthread_exit(0); |
6018
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
213 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
214 ngx_thread_task_t * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
215 ngx_thread_task_alloc(ngx_pool_t *pool, size_t size) |
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 ngx_thread_task_t *task; |
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 task = ngx_pcalloc(pool, sizeof(ngx_thread_task_t) + size); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
220 if (task == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
221 return NULL; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
224 task->ctx = task + 1; |
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 return task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
227 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
228 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
229 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
230 ngx_int_t |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
231 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
|
232 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
233 if (task->event.active) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
234 ngx_log_error(NGX_LOG_ALERT, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
235 "task #%ui already active", task->id); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
236 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
237 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
238 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
239 if (ngx_thread_mutex_lock(&tp->mtx, tp->log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
240 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
241 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
242 |
6025
32099b107191
Thread pools: keep waiting tasks counter in ngx_thread_pool_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
243 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
|
244 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
245 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
246 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
|
247 "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
|
248 &tp->name, tp->waiting); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
249 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
250 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
251 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
252 task->event.active = 1; |
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 task->id = ngx_thread_pool_task_id++; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
255 task->next = NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
256 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
257 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
|
258 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
259 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
260 } |
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 *tp->queue.last = task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
263 tp->queue.last = &task->next; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
264 |
6025
32099b107191
Thread pools: keep waiting tasks counter in ngx_thread_pool_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
265 tp->waiting++; |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
266 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
267 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
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 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
270 "task #%ui added to thread pool \"%V\"", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
271 task->id, &tp->name); |
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 return NGX_OK; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
276 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
277 static void * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
278 ngx_thread_pool_cycle(void *data) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
279 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
280 ngx_thread_pool_t *tp = data; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
281 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
282 int err; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
283 sigset_t set; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
284 ngx_thread_task_t *task; |
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 #if 0 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
287 ngx_time_update(); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
288 #endif |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
289 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
290 ngx_log_debug1(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
291 "thread in pool \"%V\" started", &tp->name); |
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 sigfillset(&set); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
294 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
295 sigdelset(&set, SIGILL); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
296 sigdelset(&set, SIGFPE); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
297 sigdelset(&set, SIGSEGV); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
298 sigdelset(&set, SIGBUS); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
299 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
300 err = pthread_sigmask(SIG_BLOCK, &set, NULL); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
301 if (err) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
302 ngx_log_error(NGX_LOG_ALERT, tp->log, err, "pthread_sigmask() failed"); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
303 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
304 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
305 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
306 for ( ;; ) { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
307 if (ngx_thread_mutex_lock(&tp->mtx, tp->log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
308 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
309 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
310 |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
311 /* the number may become negative */ |
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
312 tp->waiting--; |
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
313 |
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
314 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
|
315 if (ngx_thread_cond_wait(&tp->cond, &tp->mtx, tp->log) |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
316 != NGX_OK) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
317 { |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
318 (void) ngx_thread_mutex_unlock(&tp->mtx, tp->log); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
319 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
320 } |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
323 task = tp->queue.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
324 tp->queue.first = task->next; |
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 if (tp->queue.first == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
327 tp->queue.last = &tp->queue.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
328 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
329 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
330 if (ngx_thread_mutex_unlock(&tp->mtx, tp->log) != NGX_OK) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
331 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
332 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
333 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
334 #if 0 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
335 ngx_time_update(); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
336 #endif |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
337 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
338 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
339 "run task #%ui in thread pool \"%V\"", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
340 task->id, &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
341 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
342 task->handler(task->ctx, tp->log); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
343 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
344 ngx_log_debug2(NGX_LOG_DEBUG_CORE, tp->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
345 "complete task #%ui in thread pool \"%V\"", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
346 task->id, &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
347 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
348 task->next = NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
349 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
350 ngx_spinlock(&ngx_thread_pool_done_lock, 1, 2048); |
6018
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 *ngx_thread_pool_done.last = task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
353 ngx_thread_pool_done.last = &task->next; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
354 |
6517
657e029bac28
Thread pools: memory barriers in task completion notifications.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6042
diff
changeset
|
355 ngx_memory_barrier(); |
657e029bac28
Thread pools: memory barriers in task completion notifications.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6042
diff
changeset
|
356 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
357 ngx_unlock(&ngx_thread_pool_done_lock); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
358 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
359 (void) ngx_notify(ngx_thread_pool_handler); |
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 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
362 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
363 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
364 static void |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
365 ngx_thread_pool_handler(ngx_event_t *ev) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
366 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
367 ngx_event_t *event; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
368 ngx_thread_task_t *task; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
369 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
370 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
|
371 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
372 ngx_spinlock(&ngx_thread_pool_done_lock, 1, 2048); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
373 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
374 task = ngx_thread_pool_done.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
375 ngx_thread_pool_done.first = NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
376 ngx_thread_pool_done.last = &ngx_thread_pool_done.first; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
377 |
6517
657e029bac28
Thread pools: memory barriers in task completion notifications.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6042
diff
changeset
|
378 ngx_memory_barrier(); |
657e029bac28
Thread pools: memory barriers in task completion notifications.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6042
diff
changeset
|
379 |
6039
fc36690e7f44
Thread pools: replaced completed tasks queue mutex with spinlock.
Valentin Bartenev <vbart@nginx.com>
parents:
6027
diff
changeset
|
380 ngx_unlock(&ngx_thread_pool_done_lock); |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
381 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
382 while (task) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
383 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
384 "run completion handler for task #%ui", task->id); |
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 event = &task->event; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
387 task = task->next; |
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 event->complete = 1; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
390 event->active = 0; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
391 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
392 event->handler(event); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
393 } |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
397 static void * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
398 ngx_thread_pool_create_conf(ngx_cycle_t *cycle) |
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 ngx_thread_pool_conf_t *tcf; |
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 tcf = ngx_pcalloc(cycle->pool, sizeof(ngx_thread_pool_conf_t)); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
403 if (tcf == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
404 return NULL; |
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 if (ngx_array_init(&tcf->pools, cycle->pool, 4, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
408 sizeof(ngx_thread_pool_t *)) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
409 != NGX_OK) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
410 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
411 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
412 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
413 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
414 return tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
415 } |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
418 static char * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
419 ngx_thread_pool_init_conf(ngx_cycle_t *cycle, void *conf) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
420 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
421 ngx_thread_pool_conf_t *tcf = conf; |
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 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
424 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
425 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
426 tpp = tcf->pools.elts; |
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 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
429 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
430 if (tpp[i]->threads) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
431 continue; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
434 if (tpp[i]->name.len == ngx_thread_pool_default.len |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
435 && ngx_strncmp(tpp[i]->name.data, ngx_thread_pool_default.data, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
436 ngx_thread_pool_default.len) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
437 == 0) |
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 tpp[i]->threads = 32; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
440 tpp[i]->max_queue = 65536; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
441 continue; |
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 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
445 "unknown thread pool \"%V\" in %s:%ui", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
446 &tpp[i]->name, tpp[i]->file, tpp[i]->line); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
447 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
448 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
449 } |
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 return NGX_CONF_OK; |
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 |
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 static char * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
456 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
|
457 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
458 ngx_str_t *value; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
459 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
460 ngx_thread_pool_t *tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
461 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
462 value = cf->args->elts; |
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 tp = ngx_thread_pool_add(cf, &value[1]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
465 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
466 if (tp == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
467 return NGX_CONF_ERROR; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
470 if (tp->threads) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
471 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
472 "duplicate thread pool \"%V\"", &tp->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
473 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
474 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
475 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
476 tp->max_queue = 65536; |
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 for (i = 2; i < cf->args->nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
479 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
480 if (ngx_strncmp(value[i].data, "threads=", 8) == 0) { |
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 tp->threads = ngx_atoi(value[i].data + 8, value[i].len - 8); |
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 if (tp->threads == (ngx_uint_t) NGX_ERROR || tp->threads == 0) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
485 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
486 "invalid threads value \"%V\"", &value[i]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
487 return NGX_CONF_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
488 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
489 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
490 continue; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
493 if (ngx_strncmp(value[i].data, "max_queue=", 10) == 0) { |
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 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
|
496 |
6026
25fda43e3bcb
Thread pools: fixed the waiting tasks accounting.
Ruslan Ermilov <ru@nginx.com>
parents:
6025
diff
changeset
|
497 if (tp->max_queue == NGX_ERROR) { |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
498 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
499 "invalid max_queue value \"%V\"", &value[i]); |
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 continue; |
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 if (tp->threads == 0) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
508 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
509 "\"%V\" must have \"threads\" parameter", |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
510 &cmd->name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
511 return NGX_CONF_ERROR; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
514 return NGX_CONF_OK; |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
518 ngx_thread_pool_t * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
519 ngx_thread_pool_add(ngx_conf_t *cf, ngx_str_t *name) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
520 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
521 ngx_thread_pool_t *tp, **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
522 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
523 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
524 if (name == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
525 name = &ngx_thread_pool_default; |
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 = ngx_thread_pool_get(cf->cycle, name); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
529 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
530 if (tp) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
531 return tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
532 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
533 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
534 tp = ngx_pcalloc(cf->pool, sizeof(ngx_thread_pool_t)); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
535 if (tp == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
536 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
537 } |
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 tp->name = *name; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
540 tp->file = cf->conf_file->file.name.data; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
541 tp->line = cf->conf_file->line; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
542 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
543 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
|
544 ngx_thread_pool_module); |
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 tpp = ngx_array_push(&tcf->pools); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
547 if (tpp == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
548 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
549 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
550 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
551 *tpp = tp; |
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 return tp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
554 } |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
557 ngx_thread_pool_t * |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
558 ngx_thread_pool_get(ngx_cycle_t *cycle, ngx_str_t *name) |
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 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
561 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
562 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
563 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
564 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
565 ngx_thread_pool_module); |
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 tpp = tcf->pools.elts; |
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 for (i = 0; i < tcf->pools.nelts; i++) { |
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 if (tpp[i]->name.len == name->len |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
572 && ngx_strncmp(tpp[i]->name.data, name->data, name->len) == 0) |
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 return tpp[i]; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
575 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
576 } |
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 return NULL; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
579 } |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
582 static ngx_int_t |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
583 ngx_thread_pool_init_worker(ngx_cycle_t *cycle) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
584 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
585 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
586 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
587 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
588 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
589 if (ngx_process != NGX_PROCESS_WORKER |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
590 && ngx_process != NGX_PROCESS_SINGLE) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
591 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
592 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
593 } |
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 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
596 ngx_thread_pool_module); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
597 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
598 if (tcf == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
599 return NGX_OK; |
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 |
6040
adaedab1e662
Thread pools: keep waiting tasks mutex in ngx_thread_pool_t.
Valentin Bartenev <vbart@nginx.com>
parents:
6039
diff
changeset
|
602 ngx_thread_pool_queue_init(&ngx_thread_pool_done); |
6018
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 tpp = tcf->pools.elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
605 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
606 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
607 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
|
608 return NGX_ERROR; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
609 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
610 } |
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 return NGX_OK; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
613 } |
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 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
616 static void |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
617 ngx_thread_pool_exit_worker(ngx_cycle_t *cycle) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
618 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
619 ngx_uint_t i; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
620 ngx_thread_pool_t **tpp; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
621 ngx_thread_pool_conf_t *tcf; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
622 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
623 if (ngx_process != NGX_PROCESS_WORKER |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
624 && ngx_process != NGX_PROCESS_SINGLE) |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
625 { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
626 return; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
627 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
628 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
629 tcf = (ngx_thread_pool_conf_t *) ngx_get_conf(cycle->conf_ctx, |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
630 ngx_thread_pool_module); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
631 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
632 if (tcf == NULL) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
633 return; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
634 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
635 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
636 tpp = tcf->pools.elts; |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
637 |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
638 for (i = 0; i < tcf->pools.nelts; i++) { |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
639 ngx_thread_pool_destroy(tpp[i]); |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
640 } |
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff
changeset
|
641 } |