|
|
1 | #define NDEBUG 1 |
|
|
2 | |
1 | #include "libcoro/coro.c" |
3 | #include "libcoro/coro.c" |
2 | |
4 | |
3 | #define PERL_NO_GET_CONTEXT |
5 | #define PERL_NO_GET_CONTEXT |
4 | #define PERL_EXT |
6 | #define PERL_EXT |
5 | |
7 | |
… | |
… | |
11 | #include "schmorp.h" |
13 | #include "schmorp.h" |
12 | |
14 | |
13 | #include <stdio.h> |
15 | #include <stdio.h> |
14 | #include <errno.h> |
16 | #include <errno.h> |
15 | #include <assert.h> |
17 | #include <assert.h> |
|
|
18 | |
|
|
19 | #ifndef SVs_PADSTALE |
|
|
20 | # define SVs_PADSTALE 0 |
|
|
21 | #endif |
16 | |
22 | |
17 | #ifdef WIN32 |
23 | #ifdef WIN32 |
18 | # undef setjmp |
24 | # undef setjmp |
19 | # undef longjmp |
25 | # undef longjmp |
20 | # undef _exit |
26 | # undef _exit |
… | |
… | |
250 | |
256 | |
251 | /* on_enter/on_leave */ |
257 | /* on_enter/on_leave */ |
252 | AV *on_enter; |
258 | AV *on_enter; |
253 | AV *on_leave; |
259 | AV *on_leave; |
254 | |
260 | |
|
|
261 | /* swap_sv */ |
|
|
262 | AV *swap_sv; |
|
|
263 | |
255 | /* times */ |
264 | /* times */ |
256 | coro_ts t_cpu, t_real; |
265 | coro_ts t_cpu, t_real; |
257 | |
266 | |
258 | /* linked list */ |
267 | /* linked list */ |
259 | struct coro *next, *prev; |
268 | struct coro *next, *prev; |
… | |
… | |
289 | static OP *(*coro_old_pp_sselect) (pTHX); |
298 | static OP *(*coro_old_pp_sselect) (pTHX); |
290 | static SV *coro_select_select; |
299 | static SV *coro_select_select; |
291 | |
300 | |
292 | /* horrible hack, but if it works... */ |
301 | /* horrible hack, but if it works... */ |
293 | static OP * |
302 | static OP * |
294 | coro_pp_sselect (aTHX) |
303 | coro_pp_sselect (pTHX) |
295 | { |
304 | { |
296 | dSP; |
305 | dSP; |
297 | PUSHMARK (SP - 4); /* fake argument list */ |
306 | PUSHMARK (SP - 4); /* fake argument list */ |
298 | XPUSHs (coro_select_select); |
307 | XPUSHs (coro_select_select); |
299 | PUTBACK; |
308 | PUTBACK; |
… | |
… | |
550 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
559 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
551 | } |
560 | } |
552 | |
561 | |
553 | /** load & save, init *******************************************************/ |
562 | /** load & save, init *******************************************************/ |
554 | |
563 | |
|
|
564 | /* swap sv heads, at least logically */ |
|
|
565 | static void |
|
|
566 | swap_svs (pTHX_ Coro__State c) |
|
|
567 | { |
|
|
568 | int i; |
|
|
569 | |
|
|
570 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
|
|
571 | { |
|
|
572 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
573 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
574 | |
|
|
575 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
576 | SV tmp; |
|
|
577 | |
|
|
578 | /* swap sv_any */ |
|
|
579 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
580 | |
|
|
581 | /* swap sv_flags */ |
|
|
582 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
583 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
584 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
585 | |
|
|
586 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
587 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
588 | * is much faster, so no quarrels here. alternatively, we could |
|
|
589 | * sv_upgrade to avoid this. |
|
|
590 | */ |
|
|
591 | { |
|
|
592 | /* swap sv_u */ |
|
|
593 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
594 | |
|
|
595 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
596 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
597 | */ |
|
|
598 | #define svany_in_head(type) \ |
|
|
599 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
600 | |
|
|
601 | if (svany_in_head (SvTYPE (a))) |
|
|
602 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
603 | |
|
|
604 | if (svany_in_head (SvTYPE (b))) |
|
|
605 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
606 | } |
|
|
607 | #endif |
|
|
608 | } |
|
|
609 | } |
|
|
610 | |
|
|
611 | #define SWAP_SVS(coro) \ |
|
|
612 | if (expect_false ((coro)->swap_sv)) \ |
|
|
613 | swap_svs (aTHX_ (coro)) |
|
|
614 | |
555 | static void |
615 | static void |
556 | on_enterleave_call (pTHX_ SV *cb); |
616 | on_enterleave_call (pTHX_ SV *cb); |
557 | |
617 | |
558 | static void |
618 | static void |
559 | load_perl (pTHX_ Coro__State c) |
619 | load_perl (pTHX_ Coro__State c) |
… | |
… | |
605 | int i; |
665 | int i; |
606 | |
666 | |
607 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
667 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
608 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
668 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
609 | } |
669 | } |
|
|
670 | |
|
|
671 | SWAP_SVS (c); |
610 | } |
672 | } |
611 | |
673 | |
612 | static void |
674 | static void |
613 | save_perl (pTHX_ Coro__State c) |
675 | save_perl (pTHX_ Coro__State c) |
614 | { |
676 | { |
|
|
677 | SWAP_SVS (c); |
|
|
678 | |
615 | if (expect_false (c->on_leave)) |
679 | if (expect_false (c->on_leave)) |
616 | { |
680 | { |
617 | int i; |
681 | int i; |
618 | |
682 | |
619 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
683 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
… | |
… | |
926 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
990 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
927 | { |
991 | { |
928 | return 1; |
992 | return 1; |
929 | } |
993 | } |
930 | |
994 | |
931 | static UNOP coro_setup_op; |
995 | static UNOP init_perl_op; |
932 | |
996 | |
933 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
997 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
934 | coro_setup (pTHX_ struct coro *coro) |
998 | init_perl (pTHX_ struct coro *coro) |
935 | { |
999 | { |
936 | /* |
1000 | /* |
937 | * emulate part of the perl startup here. |
1001 | * emulate part of the perl startup here. |
938 | */ |
1002 | */ |
939 | coro_init_stacks (aTHX); |
1003 | coro_init_stacks (aTHX); |
… | |
… | |
988 | */ |
1052 | */ |
989 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
1053 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
990 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
1054 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
991 | |
1055 | |
992 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
1056 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
993 | coro_setup_op.op_next = PL_op; |
1057 | init_perl_op.op_next = PL_op; |
994 | coro_setup_op.op_type = OP_ENTERSUB; |
1058 | init_perl_op.op_type = OP_ENTERSUB; |
995 | coro_setup_op.op_ppaddr = pp_slf; |
1059 | init_perl_op.op_ppaddr = pp_slf; |
996 | /* no flags etc. required, as an init function won't be called */ |
1060 | /* no flags etc. required, as an init function won't be called */ |
997 | |
1061 | |
998 | PL_op = (OP *)&coro_setup_op; |
1062 | PL_op = (OP *)&init_perl_op; |
999 | |
1063 | |
1000 | /* copy throw, in case it was set before coro_setup */ |
1064 | /* copy throw, in case it was set before init_perl */ |
1001 | CORO_THROW = coro->except; |
1065 | CORO_THROW = coro->except; |
|
|
1066 | |
|
|
1067 | SWAP_SVS (coro); |
1002 | |
1068 | |
1003 | if (expect_false (enable_times)) |
1069 | if (expect_false (enable_times)) |
1004 | { |
1070 | { |
1005 | coro_times_update (); |
1071 | coro_times_update (); |
1006 | coro_times_sub (coro); |
1072 | coro_times_sub (coro); |
… | |
… | |
1027 | dounwind (-1); |
1093 | dounwind (-1); |
1028 | } |
1094 | } |
1029 | } |
1095 | } |
1030 | |
1096 | |
1031 | static void |
1097 | static void |
1032 | coro_destruct_perl (pTHX_ struct coro *coro) |
1098 | destroy_perl (pTHX_ struct coro *coro) |
1033 | { |
1099 | { |
1034 | SV *svf [9]; |
1100 | SV *svf [9]; |
1035 | |
1101 | |
1036 | { |
1102 | { |
1037 | struct coro *current = SvSTATE_current; |
1103 | struct coro *current = SvSTATE_current; |
… | |
… | |
1041 | save_perl (aTHX_ current); |
1107 | save_perl (aTHX_ current); |
1042 | load_perl (aTHX_ coro); |
1108 | load_perl (aTHX_ coro); |
1043 | |
1109 | |
1044 | coro_unwind_stacks (aTHX); |
1110 | coro_unwind_stacks (aTHX); |
1045 | coro_destruct_stacks (aTHX); |
1111 | coro_destruct_stacks (aTHX); |
|
|
1112 | |
|
|
1113 | /* restore swapped sv's */ |
|
|
1114 | SWAP_SVS (coro); |
1046 | |
1115 | |
1047 | // now save some sv's to be free'd later |
1116 | // now save some sv's to be free'd later |
1048 | svf [0] = GvSV (PL_defgv); |
1117 | svf [0] = GvSV (PL_defgv); |
1049 | svf [1] = (SV *)GvAV (PL_defgv); |
1118 | svf [1] = (SV *)GvAV (PL_defgv); |
1050 | svf [2] = GvSV (PL_errgv); |
1119 | svf [2] = GvSV (PL_errgv); |
… | |
… | |
1492 | if (expect_false (next->flags & CF_NEW)) |
1561 | if (expect_false (next->flags & CF_NEW)) |
1493 | { |
1562 | { |
1494 | /* need to start coroutine */ |
1563 | /* need to start coroutine */ |
1495 | next->flags &= ~CF_NEW; |
1564 | next->flags &= ~CF_NEW; |
1496 | /* setup coroutine call */ |
1565 | /* setup coroutine call */ |
1497 | coro_setup (aTHX_ next); |
1566 | init_perl (aTHX_ next); |
1498 | } |
1567 | } |
1499 | else |
1568 | else |
1500 | load_perl (aTHX_ next); |
1569 | load_perl (aTHX_ next); |
1501 | |
1570 | |
1502 | /* possibly untie and reuse the cctx */ |
1571 | /* possibly untie and reuse the cctx */ |
… | |
… | |
1565 | |
1634 | |
1566 | if (coro->mainstack |
1635 | if (coro->mainstack |
1567 | && coro->mainstack != main_mainstack |
1636 | && coro->mainstack != main_mainstack |
1568 | && coro->slot |
1637 | && coro->slot |
1569 | && !PL_dirty) |
1638 | && !PL_dirty) |
1570 | coro_destruct_perl (aTHX_ coro); |
1639 | destroy_perl (aTHX_ coro); |
|
|
1640 | |
|
|
1641 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1642 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1643 | if (coro == coro_first) coro_first = coro->next; |
1571 | |
1644 | |
1572 | cctx_destroy (coro->cctx); |
1645 | cctx_destroy (coro->cctx); |
1573 | SvREFCNT_dec (coro->startcv); |
1646 | SvREFCNT_dec (coro->startcv); |
1574 | SvREFCNT_dec (coro->args); |
1647 | SvREFCNT_dec (coro->args); |
|
|
1648 | SvREFCNT_dec (coro->swap_sv); |
1575 | SvREFCNT_dec (CORO_THROW); |
1649 | SvREFCNT_dec (CORO_THROW); |
1576 | |
|
|
1577 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1578 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1579 | if (coro == coro_first) coro_first = coro->next; |
|
|
1580 | |
1650 | |
1581 | return 1; |
1651 | return 1; |
1582 | } |
1652 | } |
1583 | |
1653 | |
1584 | static int |
1654 | static int |
… | |
… | |
1754 | { |
1824 | { |
1755 | /* nothing to schedule: call the idle handler */ |
1825 | /* nothing to schedule: call the idle handler */ |
1756 | if (SvROK (sv_idle) |
1826 | if (SvROK (sv_idle) |
1757 | && SvOBJECT (SvRV (sv_idle))) |
1827 | && SvOBJECT (SvRV (sv_idle))) |
1758 | { |
1828 | { |
|
|
1829 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1830 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
|
|
1831 | |
1759 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1832 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1760 | api_ready (aTHX_ SvRV (sv_idle)); |
1833 | api_ready (aTHX_ SvRV (sv_idle)); |
1761 | --coro_nready; |
1834 | --coro_nready; |
1762 | } |
1835 | } |
1763 | else |
1836 | else |
1764 | { |
1837 | { |
|
|
1838 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1765 | dSP; |
1839 | dSP; |
1766 | |
1840 | |
1767 | ENTER; |
1841 | ENTER; |
1768 | SAVETMPS; |
1842 | SAVETMPS; |
1769 | |
1843 | |
… | |
… | |
2348 | if (PL_op->op_flags & OPf_STACKED) |
2422 | if (PL_op->op_flags & OPf_STACKED) |
2349 | { |
2423 | { |
2350 | if (items > slf_arga) |
2424 | if (items > slf_arga) |
2351 | { |
2425 | { |
2352 | slf_arga = items; |
2426 | slf_arga = items; |
2353 | free (slf_argv); |
2427 | Safefree (slf_argv); |
2354 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2428 | New (0, slf_argv, slf_arga, SV *); |
2355 | } |
2429 | } |
2356 | |
2430 | |
2357 | slf_argc = items; |
2431 | slf_argc = items; |
2358 | |
2432 | |
2359 | for (i = 0; i < items; ++i) |
2433 | for (i = 0; i < items; ++i) |
… | |
… | |
3201 | RETVAL = boolSV (coro->flags & ix); |
3275 | RETVAL = boolSV (coro->flags & ix); |
3202 | OUTPUT: |
3276 | OUTPUT: |
3203 | RETVAL |
3277 | RETVAL |
3204 | |
3278 | |
3205 | void |
3279 | void |
3206 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3280 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3207 | PROTOTYPE: $;$ |
3281 | PROTOTYPE: $;$ |
3208 | CODE: |
3282 | CODE: |
3209 | { |
3283 | { |
3210 | struct coro *current = SvSTATE_current; |
3284 | struct coro *current = SvSTATE_current; |
3211 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3285 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3212 | SvREFCNT_dec (*throwp); |
3286 | SvREFCNT_dec (*exceptionp); |
3213 | SvGETMAGIC (throw); |
3287 | SvGETMAGIC (exception); |
3214 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3288 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3215 | } |
3289 | } |
3216 | |
3290 | |
3217 | void |
3291 | void |
3218 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3292 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3219 | PROTOTYPE: $;$ |
3293 | PROTOTYPE: $;$ |
… | |
… | |
3314 | |
3388 | |
3315 | if (expect_false (current == self)) |
3389 | if (expect_false (current == self)) |
3316 | coro_times_sub (SvSTATE (coro_current)); |
3390 | coro_times_sub (SvSTATE (coro_current)); |
3317 | } |
3391 | } |
3318 | |
3392 | |
|
|
3393 | void |
|
|
3394 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
|
|
3395 | CODE: |
|
|
3396 | { |
|
|
3397 | struct coro *current = SvSTATE_current; |
|
|
3398 | |
|
|
3399 | if (current == coro) |
|
|
3400 | SWAP_SVS (current); |
|
|
3401 | |
|
|
3402 | if (!coro->swap_sv) |
|
|
3403 | coro->swap_sv = newAV (); |
|
|
3404 | |
|
|
3405 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
|
|
3406 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
|
|
3407 | |
|
|
3408 | if (current == coro) |
|
|
3409 | SWAP_SVS (current); |
|
|
3410 | } |
|
|
3411 | |
|
|
3412 | |
3319 | MODULE = Coro::State PACKAGE = Coro |
3413 | MODULE = Coro::State PACKAGE = Coro |
3320 | |
3414 | |
3321 | BOOT: |
3415 | BOOT: |
3322 | { |
3416 | { |
3323 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3417 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3365 | SV * |
3459 | SV * |
3366 | async (...) |
3460 | async (...) |
3367 | PROTOTYPE: &@ |
3461 | PROTOTYPE: &@ |
3368 | CODE: |
3462 | CODE: |
3369 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
3463 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
3370 | api_ready (RETVAL); |
3464 | api_ready (aTHX_ RETVAL); |
3371 | OUTPUT: |
3465 | OUTPUT: |
3372 | RETVAL |
3466 | RETVAL |
3373 | |
3467 | |
3374 | void |
3468 | void |
3375 | terminate (...) |
3469 | terminate (...) |