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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
static char *nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib);
static void nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent);
static void nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data);
static void nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib);
static void nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data);
#define \
nxt_fiber_enqueue(thr, task, fib) \
nxt_work_queue_add(&(thr)->engine->fast_work_queue, \
nxt_fiber_switch_handler, task, fib, NULL)
nxt_fiber_main_t *
nxt_fiber_main_create(nxt_event_engine_t *engine)
{
nxt_fiber_main_t *fm;
fm = nxt_zalloc(sizeof(nxt_fiber_main_t));
if (nxt_slow_path(fm == NULL)) {
return NULL;
}
fm->engine = engine;
fm->stack_size = 512 * 1024 - nxt_pagesize;
fm->idle = NULL;
return fm;
}
nxt_int_t
nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack)
{
int ret;
jmp_buf parent;
nxt_fid_t fid;
nxt_fiber_t *fib;
nxt_thread_t *thr;
nxt_fiber_main_t *fm;
thr = nxt_thread();
fm = thr->engine->fibers;
fid = ++fm->fid;
if (fid == 0) {
fid = ++fm->fid;
}
fib = fm->idle;
if (fib != NULL) {
fm->idle = fib->next;
fib->fid = fid;
fib->start = start;
fib->data = data;
fib->main = fm;
fib->task.thread = thr;
fib->task.log = thr->log;
fib->task.ident = nxt_task_next_ident();
nxt_debug(&fib->task, "fiber create cached: %PF", fib->fid);
nxt_fiber_enqueue(thr, &fm->engine->task, fib);
return NXT_OK;
}
nxt_log_debug(thr->log, "fiber create");
fib = nxt_malloc(sizeof(nxt_fiber_t));
if (nxt_slow_path(fib == NULL)) {
return NXT_ERROR;
}
fib->fid = fid;
fib->start = start;
fib->data = data;
fib->stack_size = fm->stack_size;
fib->main = fm;
fib->task.thread = thr;
fib->task.log = thr->log;
fib->task.ident = nxt_task_next_ident();
fib->stack = nxt_fiber_create_stack(&fib->task, fib);
if (nxt_fast_path(fib->stack != NULL)) {
if (_setjmp(parent) != 0) {
nxt_log_debug(thr->log, "fiber create: %PF", fib->fid);
return NXT_OK;
}
nxt_fiber_switch_stack(fib, &parent);
/* It does not return if the switch was successful. */
}
ret = munmap(fib->stack - nxt_pagesize, fib->stack_size + nxt_pagesize);
if (nxt_slow_path(ret != 0)) {
nxt_log_alert(thr->log, "munmap() failed %E", nxt_errno);
}
nxt_free(fib);
return NXT_ERROR;
}
#if (NXT_LINUX)
static char *
nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib)
{
char *s;
size_t size;
size = fib->stack_size + nxt_pagesize;
s = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, -1, 0);
if (nxt_slow_path(s == MAP_FAILED)) {
nxt_alert(task, "fiber stack "
"mmap(%uz, MAP_PRIVATE|MAP_ANON|MAP_GROWSDOWN) failed %E",
size, nxt_errno);
return NULL;
}
if (nxt_slow_path(mprotect(s, nxt_pagesize, PROT_NONE) != 0)) {
nxt_alert(task, "fiber stack mprotect(%uz, PROT_NONE) failed %E",
size, nxt_errno);
return NULL;
}
s += nxt_pagesize;
nxt_debug(task, "fiber stack mmap: %p", s);
return s;
}
#else /* Generic version. */
static char *
nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib)
{
char *s;
size_t size;
size = fib->stack_size + nxt_pagesize;
s = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (nxt_slow_path(s == MAP_FAILED)) {
nxt_alert(task, "fiber stack mmap(%uz, MAP_PRIVATE|MAP_ANON) failed %E",
size, nxt_errno);
return NULL;
}
if (nxt_slow_path(mprotect(s, nxt_pagesize, PROT_NONE) != 0)) {
nxt_alert(task, "fiber stack mprotect(%uz, PROT_NONE) failed %E",
size, nxt_errno);
return NULL;
}
s += nxt_pagesize;
nxt_debug(task, "fiber stack mmap: %p", s);
return s;
}
#endif
#if (NXT_LINUX && NXT_64BIT)
/*
* Linux 64-bit ucontext version. 64-bit glibc makecontext() passes
* pointers as signed int's. The bug has been fixed in glibc 2.8.
*/
static void nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph,
uint32_t pl);
static void
nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent)
{
ucontext_t uc;
nxt_debug(&fib->task, "fiber switch to stack: %p", fib->stack);
if (nxt_slow_path(getcontext(&uc) != 0)) {
nxt_alert(&fib->task, "getcontext() failed");
return;
}
uc.uc_link = NULL;
uc.uc_stack.ss_sp = fib->stack;
uc.uc_stack.ss_size = fib->stack_size;
makecontext(&uc, (void (*)(void)) nxt_fiber_trampoline, 4,
(uint32_t) ((uintptr_t) fib >> 32),
(uint32_t) ((uintptr_t) fib & 0xFFFFFFFF),
(uint32_t) ((uintptr_t) parent >> 32),
(uint32_t) ((uintptr_t) parent & 0xFFFFFFFF));
setcontext(&uc);
nxt_alert(&fib->task, "setcontext() failed");
}
static void
nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, uint32_t pl)
{
jmp_buf *parent;
nxt_task_t *task;
nxt_fiber_t *fib;
fib = (nxt_fiber_t *) (((uintptr_t) fh << 32) + fl);
parent = (jmp_buf *) (((uintptr_t) ph << 32) + pl);
task = &fib->task;
if (_setjmp(fib->jmp) == 0) {
nxt_debug(task, "fiber return to parent stack");
nxt_fiber_enqueue(task->thread, task, fib);
_longjmp(*parent, 1);
nxt_unreachable();
}
nxt_debug(task, "fiber start");
fib->start(fib->data);
nxt_fiber_exit(task, &fib->main->fiber, NULL);
nxt_unreachable();
}
#elif (NXT_HAVE_UCONTEXT)
/* Generic ucontext version. */
static void nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent);
static void
nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent)
{
ucontext_t uc;
nxt_debug(&fib->task, "fiber switch to stack: %p", fib->stack);
if (nxt_slow_path(getcontext(&uc) != 0)) {
nxt_alert(&fib->task, "getcontext() failed");
return;
}
uc.uc_link = NULL;
uc.uc_stack.ss_sp = fib->stack;
uc.uc_stack.ss_size = fib->stack_size;
makecontext(&uc, (void (*)(void)) nxt_fiber_trampoline, 2, fib, parent);
setcontext(&uc);
#if !(NXT_SOLARIS)
/* Solaris declares setcontext() as __NORETURN. */
nxt_alert(&fib->task, "setcontext() failed");
#endif
}
static void
nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent)
{
nxt_task_t *task;
task = &fib->task;
if (_setjmp(fib->jmp) == 0) {
nxt_debug(task, "fiber return to parent stack");
nxt_fiber_enqueue(task->thread, task, fib);
_longjmp(*parent, 1);
nxt_unreachable();
}
nxt_debug(task, "fiber start");
fib->start(fib->data);
nxt_fiber_exit(task, &fib->main->fiber, NULL);
nxt_unreachable();
}
#else
#error No ucontext(3) interface.
#endif
static void
nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data)
{
nxt_fiber_t *fib;
fib = obj;
nxt_fiber_switch(task, fib);
nxt_unreachable();
}
static void
nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib)
{
nxt_debug(task, "fiber switch: %PF", fib->fid);
task->thread->fiber = fib;
_longjmp(fib->jmp, 1);
nxt_unreachable();
}
nxt_fiber_t *
nxt_fiber_self(nxt_thread_t *thr)
{
return (nxt_fast_path(thr != NULL)) ? thr->fiber : NULL;
}
void
nxt_fiber_yield(nxt_task_t *task)
{
nxt_fiber_t *fib;
fib = task->thread->fiber;
if (_setjmp(fib->jmp) == 0) {
nxt_debug(task, "fiber yield");
nxt_fiber_enqueue(task->thread, &fib->main->engine->task, fib);
nxt_fiber_switch(task, &fib->main->fiber);
nxt_unreachable();
}
nxt_debug(task, "fiber yield return");
}
void
nxt_fiber_sleep(nxt_task_t *task, nxt_msec_t timeout)
{
nxt_fiber_t *fib;
fib = task->thread->fiber;
fib->timer.work_queue = &task->thread->engine->fast_work_queue;
fib->timer.handler = nxt_fiber_timer_handler;
fib->timer.log = &nxt_main_log;
task = &fib->task;
nxt_timer_add(task->thread->engine, &fib->timer, timeout);
if (_setjmp(fib->jmp) == 0) {
nxt_debug(task, "fiber sleep: %T", timeout);
nxt_fiber_switch(task, &fib->main->fiber);
nxt_unreachable();
}
nxt_debug(task, "fiber sleep return");
}
static void
nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data)
{
nxt_fiber_t *fib;
nxt_timer_t *ev;
ev = obj;
nxt_debug(task, "fiber timer handler");
fib = nxt_timer_data(ev, nxt_fiber_t, timer);
nxt_fiber_switch(task, fib);
nxt_unreachable();
}
void
nxt_fiber_wait(nxt_task_t *task)
{
nxt_fiber_t *fib;
fib = task->thread->fiber;
if (_setjmp(fib->jmp) == 0) {
nxt_debug(task, "fiber wait");
nxt_fiber_switch(task, &fib->main->fiber);
nxt_unreachable();
}
nxt_debug(task, "fiber wait return");
}
void
nxt_fiber_exit(nxt_task_t *task, nxt_fiber_t *next, void *data)
{
nxt_fiber_t *fib;
fib = task->thread->fiber;
nxt_debug(task, "fiber exit");
/* TODO: limit idle fibers. */
fib->next = fib->main->idle;
fib->main->idle = fib;
nxt_fiber_switch(task, next);
nxt_unreachable();
}
|