summaryrefslogblamecommitdiffhomepage
path: root/src/nxt_fiber.c
blob: 74473f850eeba38963e47b671fbd9df2421afee7 (plain) (tree)
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





























































































































































































































































































































































































































































                                                                                

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


static char *nxt_fiber_create_stack(nxt_fiber_t *fib);
static void nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent);
static void nxt_fiber_switch_handler(nxt_thread_t *thr, void *obj,
    void *data);
static void nxt_fiber_switch(nxt_thread_t *thr, nxt_fiber_t *fib);
static void nxt_fiber_timer_handler(nxt_thread_t *thr, void *obj,
    void *data);


#define                                                                       \
nxt_fiber_enqueue(thr, fib)                                                   \
    nxt_thread_work_queue_add(thr, &(thr)->work_queue.main,                   \
                              nxt_fiber_switch_handler, fib, NULL, thr->log)


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->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;

        nxt_log_debug(thr->log, "fiber create cached: %PF", fib->fid);
        nxt_fiber_enqueue(thr, 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->stack = nxt_fiber_create_stack(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_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_thread_log_alert("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_thread_log_alert("fiber stack mprotect(%uz, PROT_NONE) failed %E",
                             size, nxt_errno);
        return NULL;
    }

    s += nxt_pagesize;

    nxt_thread_log_debug("fiber stack mmap: %p", s);

    return s;
}

#else /* Generic version. */

static char *
nxt_fiber_create_stack(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_thread_log_alert("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_thread_log_alert("fiber stack mprotect(%uz, PROT_NONE) failed %E",
                              size, nxt_errno);
        return NULL;
    }

    s += nxt_pagesize;

    nxt_thread_log_debug("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_thread_log_debug("fiber switch to stack: %p", fib->stack);

    if (nxt_slow_path(getcontext(&uc) != 0)) {
        nxt_thread_log_alert("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_thread_log_alert("setcontext() failed");
}


static void
nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, uint32_t pl)
{
    jmp_buf       *parent;
    nxt_fiber_t   *fib;
    nxt_thread_t  *thr;

    fib = (nxt_fiber_t *) (((uintptr_t) fh << 32) + fl);
    parent = (jmp_buf *) (((uintptr_t) ph << 32) + pl);

    thr = nxt_thread();

    if (_setjmp(fib->jmp) == 0) {
        nxt_log_debug(thr->log, "fiber return to parent stack");

        nxt_fiber_enqueue(thr, fib);
        _longjmp(*parent, 1);
        nxt_unreachable();
    }

    nxt_log_debug(thr->log, "fiber start");

    fib->start(fib->data);

    nxt_fiber_exit(&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_thread_log_debug("fiber switch to stack: %p", fib->stack);

    if (nxt_slow_path(getcontext(&uc) != 0)) {
        nxt_thread_log_alert("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_thread_log_alert("setcontext() failed");
#endif
}


static void
nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent)
{
    nxt_thread_t  *thr;

    thr = nxt_thread();

    if (_setjmp(fib->jmp) == 0) {
        nxt_log_debug(thr->log, "fiber return to parent stack");

        nxt_fiber_enqueue(thr, fib);
        _longjmp(*parent, 1);
        nxt_unreachable();
    }

    nxt_log_debug(thr->log, "fiber start");

    fib->start(fib->data);

    nxt_fiber_exit(&fib->main->fiber, NULL);
    nxt_unreachable();
}

#else

#error No ucontext(3) interface.

#endif


static void
nxt_fiber_switch_handler(nxt_thread_t *thr, void *obj, void *data)
{
    nxt_fiber_t  *fib;

    fib = obj;

    nxt_fiber_switch(thr, fib);
    nxt_unreachable();
}


static void
nxt_fiber_switch(nxt_thread_t *thr, nxt_fiber_t *fib)
{
    nxt_log_debug(thr->log, "fiber switch: %PF", fib->fid);

    thr->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(void)
{
    nxt_fiber_t   *fib;
    nxt_thread_t  *thr;

    thr = nxt_thread();
    fib = thr->fiber;

    if (_setjmp(fib->jmp) == 0) {

        nxt_log_debug(thr->log, "fiber yield");

        nxt_fiber_enqueue(thr, fib);
        nxt_fiber_switch(thr, &fib->main->fiber);
        nxt_unreachable();
    }

    nxt_log_debug(thr->log, "fiber yield return");
}


void
nxt_fiber_sleep(nxt_msec_t timeout)
{
    nxt_fiber_t   *fib;
    nxt_thread_t  *thr;

    thr = nxt_thread();
    fib = thr->fiber;

    fib->timer.work_queue = &thr->work_queue.main;
    fib->timer.handler = nxt_fiber_timer_handler;
    fib->timer.log = &nxt_main_log;

    nxt_event_timer_add(thr->engine, &fib->timer, timeout);

    if (_setjmp(fib->jmp) == 0) {

        nxt_log_debug(thr->log, "fiber sleep: %T", timeout);

        nxt_fiber_switch(thr, &fib->main->fiber);
        nxt_unreachable();
    }

    nxt_log_debug(thr->log, "fiber sleep return");
}


static void
nxt_fiber_timer_handler(nxt_thread_t *thr, void *obj, void *data)
{
    nxt_fiber_t        *fib;
    nxt_event_timer_t  *ev;

    ev = obj;

    nxt_log_debug(thr->log, "fiber timer handler");

    fib = nxt_event_timer_data(ev, nxt_fiber_t, timer);

    nxt_fiber_switch(thr, fib);
    nxt_unreachable();
}


void
nxt_fiber_wait(void)
{
    nxt_fiber_t   *fib;
    nxt_thread_t  *thr;

    thr = nxt_thread();
    fib = thr->fiber;

    if (_setjmp(fib->jmp) == 0) {
        nxt_log_debug(thr->log, "fiber wait");

        nxt_fiber_switch(thr, &fib->main->fiber);
        nxt_unreachable();
    }

    nxt_log_debug(thr->log, "fiber wait return");
}


void
nxt_fiber_exit(nxt_fiber_t *next, void *data)
{
    nxt_fiber_t   *fib;
    nxt_thread_t  *thr;

    thr = nxt_thread();
    fib = thr->fiber;

    nxt_log_debug(thr->log, "fiber exit");

    /* TODO: limit idle fibers. */
    fib->next = fib->main->idle;
    fib->main->idle = fib;

    nxt_fiber_switch(thr, next);
    nxt_unreachable();
}