1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
/*
* Available work items are crucial for overall engine operation, so
* the items are preallocated in two chunks: cache and spare chunks.
* By default each chunk preallocates 409 work items on two or four
* CPU pages depending on platform. If all items in a cache chunk are
* exhausted then a spare chunk becomes a cache chunk, and a new spare
* chunk is allocated. This two-step allocation mitigates low memory
* condition impact on work queue operation. However, if both chunks
* are exhausted then a thread will sleep in reliance on another thread
* frees some memory. However, this may lead to deadlock and probably
* a process should be aborted. This behaviour should be considered as
* abort on program stack exhaustion.
*
* The cache and spare chunks initially are also allocated in two steps:
* a spare chunk is allocated first, then it becomes the cache chunk and
* a new spare chunk is allocated again.
*/
static void nxt_work_queue_allocate(nxt_work_queue_cache_t *cache);
/* It should be adjusted with the "work_queue_bucket_items" directive. */
static nxt_uint_t nxt_work_queue_bucket_items = 409;
#if (NXT_DEBUG)
nxt_inline void
nxt_work_queue_thread_assert(nxt_work_queue_t *wq)
{
nxt_tid_t tid;
nxt_thread_t *thread;
thread = nxt_thread();
tid = nxt_thread_tid(thread);
if (nxt_fast_path(wq->tid == tid)) {
return;
}
if (nxt_slow_path(nxt_pid != wq->pid)) {
wq->pid = nxt_pid;
wq->tid = tid;
return;
}
nxt_log_alert(thread->log, "work queue locked by thread %PT", wq->tid);
nxt_abort();
}
void nxt_work_queue_thread_adopt(nxt_work_queue_t *wq)
{
nxt_thread_t *thread;
thread = nxt_thread();
wq->pid = nxt_pid;
wq->tid = nxt_thread_tid(thread);
}
void
nxt_work_queue_name(nxt_work_queue_t *wq, const char *name)
{
nxt_work_queue_thread_assert(wq);
wq->name = name;
}
#else
#define nxt_work_queue_thread_assert(wq)
#endif
void
nxt_work_queue_cache_create(nxt_work_queue_cache_t *cache, size_t chunk_size)
{
nxt_memzero(cache, sizeof(nxt_work_queue_cache_t));
if (chunk_size == 0) {
chunk_size = nxt_work_queue_bucket_items;
}
/* nxt_work_queue_chunk_t already has one work item. */
cache->chunk_size = chunk_size - 1;
while (cache->next == NULL) {
nxt_work_queue_allocate(cache);
}
}
void
nxt_work_queue_cache_destroy(nxt_work_queue_cache_t *cache)
{
nxt_work_queue_chunk_t *chunk, *next;
for (chunk = cache->chunk; chunk; chunk = next) {
next = chunk->next;
nxt_free(chunk);
}
}
static void
nxt_work_queue_allocate(nxt_work_queue_cache_t *cache)
{
size_t size;
nxt_uint_t i, n;
nxt_work_t *work;
nxt_work_queue_chunk_t *chunk;
n = cache->chunk_size;
size = sizeof(nxt_work_queue_chunk_t) + n * sizeof(nxt_work_t);
chunk = nxt_malloc(size);
if (nxt_fast_path(chunk != NULL)) {
chunk->next = cache->chunk;
cache->chunk = chunk;
work = &chunk->work;
for (i = 0; i < n; i++) {
work[i].next = &work[i + 1];
}
work[i].next = NULL;
work++;
} else if (cache->spare != NULL) {
work = NULL;
} else {
return;
}
cache->next = cache->spare;
cache->spare = work;
}
/* Add a work to a work queue tail. */
void
nxt_work_queue_add(nxt_work_queue_t *wq, nxt_work_handler_t handler,
nxt_task_t *task, void *obj, void *data)
{
nxt_work_t *work;
nxt_work_queue_thread_assert(wq);
for ( ;; ) {
work = wq->cache->next;
if (nxt_fast_path(work != NULL)) {
wq->cache->next = work->next;
work->next = NULL;
work->handler = handler;
work->task = task;
work->obj = obj;
work->data = data;
if (wq->tail != NULL) {
wq->tail->next = work;
} else {
wq->head = work;
}
wq->tail = work;
return;
}
nxt_work_queue_allocate(wq->cache);
}
}
nxt_work_handler_t
nxt_work_queue_pop(nxt_work_queue_t *wq, nxt_task_t **task, void **obj,
void **data)
{
nxt_work_t *work;
nxt_work_queue_thread_assert(wq);
work = wq->head;
wq->head = work->next;
if (work->next == NULL) {
wq->tail = NULL;
}
*task = work->task;
*obj = work->obj;
nxt_prefetch(*obj);
*data = work->data;
nxt_prefetch(*data);
work->next = wq->cache->next;
wq->cache->next = work;
return work->handler;
}
/* Add a work to a locked work queue tail. */
void
nxt_locked_work_queue_add(nxt_locked_work_queue_t *lwq, nxt_work_t *work)
{
nxt_thread_spin_lock(&lwq->lock);
if (lwq->tail != NULL) {
lwq->tail->next = work;
} else {
lwq->head = work;
}
lwq->tail = work;
nxt_thread_spin_unlock(&lwq->lock);
}
/* Pop a work from a locked work queue head. */
nxt_work_handler_t
nxt_locked_work_queue_pop(nxt_locked_work_queue_t *lwq, nxt_task_t **task,
void **obj, void **data)
{
nxt_work_t *work;
nxt_work_handler_t handler;
handler = NULL;
nxt_thread_spin_lock(&lwq->lock);
work = lwq->head;
if (work != NULL) {
*task = work->task;
*obj = work->obj;
nxt_prefetch(*obj);
*data = work->data;
nxt_prefetch(*data);
lwq->head = work->next;
if (work->next == NULL) {
lwq->tail = NULL;
}
handler = work->handler;
}
nxt_thread_spin_unlock(&lwq->lock);
return handler;
}
/* Move all works from a locked work queue to a usual work queue. */
void
nxt_locked_work_queue_move(nxt_thread_t *thr, nxt_locked_work_queue_t *lwq,
nxt_work_queue_t *wq)
{
nxt_work_t *work;
nxt_thread_spin_lock(&lwq->lock);
work = lwq->head;
lwq->head = NULL;
lwq->tail = NULL;
nxt_thread_spin_unlock(&lwq->lock);
while (work != NULL) {
work->task->thread = thr;
nxt_work_queue_add(wq, work->handler, work->task,
work->obj, work->data);
work = work->next;
}
}
|