… | |
… | |
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
3 | |
3 | |
4 | #define NDEBUG 1 /* perl usually disables NDEBUG later */ |
4 | #define NDEBUG 1 /* perl usually disables NDEBUG later */ |
5 | |
5 | |
6 | #include "libcoro/coro.c" |
6 | #include "libcoro/coro.c" |
|
|
7 | |
|
|
8 | #if CORO_UCONTEXT |
|
|
9 | #define CORO_BACKEND "ucontext" |
|
|
10 | #elif CORO_SJLJ |
|
|
11 | #define CORO_BACKEND "sjlj" |
|
|
12 | #elif CORO_LINUX |
|
|
13 | #define CORO_BACKEND "linux" |
|
|
14 | #elif CORO_LOSER |
|
|
15 | #define CORO_BACKEND "loser" |
|
|
16 | #elif CORO_FIBER |
|
|
17 | #define CORO_BACKEND "fiber" |
|
|
18 | #elif CORO_IRIX |
|
|
19 | #define CORO_BACKEND "irix" |
|
|
20 | #elif CORO_ASM |
|
|
21 | #define CORO_BACKEND "asm" |
|
|
22 | #elif CORO_PTHREAD |
|
|
23 | #define CORO_BACKEND "pthread" |
|
|
24 | #else |
|
|
25 | #define CORO_BACKEND "unknown" |
|
|
26 | #endif |
7 | |
27 | |
8 | #define PERL_NO_GET_CONTEXT |
28 | #define PERL_NO_GET_CONTEXT |
9 | #define PERL_EXT |
29 | #define PERL_EXT |
10 | |
30 | |
11 | #include "EXTERN.h" |
31 | #include "EXTERN.h" |
… | |
… | |
263 | int usecount; /* number of transfers to this coro */ |
283 | int usecount; /* number of transfers to this coro */ |
264 | |
284 | |
265 | /* coro process data */ |
285 | /* coro process data */ |
266 | int prio; |
286 | int prio; |
267 | SV *except; /* exception to be thrown */ |
287 | SV *except; /* exception to be thrown */ |
268 | SV *rouse_cb; /* last rouse callback */ |
288 | SV *rouse_cb; /* most recently created rouse callback */ |
269 | AV *on_destroy; /* callbacks or coros to notify on destroy */ |
289 | AV *on_destroy; /* callbacks or coros to notify on destroy */ |
270 | AV *status; /* the exit status list */ |
290 | AV *status; /* the exit status list */ |
271 | |
291 | |
272 | /* async_pool */ |
292 | /* async_pool */ |
273 | SV *saved_deffh; |
293 | SV *saved_deffh; |
… | |
… | |
1222 | myop.op_next = Nullop; |
1242 | myop.op_next = Nullop; |
1223 | myop.op_type = OP_ENTERSUB; |
1243 | myop.op_type = OP_ENTERSUB; |
1224 | myop.op_flags = OPf_WANT_VOID; |
1244 | myop.op_flags = OPf_WANT_VOID; |
1225 | |
1245 | |
1226 | PUSHMARK (SP); |
1246 | PUSHMARK (SP); |
1227 | PUSHs ((SV *)coro->startcv); |
1247 | XPUSHs ((SV *)coro->startcv); |
1228 | PUTBACK; |
1248 | PUTBACK; |
1229 | PL_op = (OP *)&myop; |
1249 | PL_op = (OP *)&myop; |
1230 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1250 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1231 | } |
1251 | } |
1232 | |
1252 | |
… | |
… | |
1289 | |
1309 | |
1290 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1310 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1291 | |
1311 | |
1292 | save_perl (aTHX_ current); |
1312 | save_perl (aTHX_ current); |
1293 | |
1313 | |
1294 | /* this will cause transfer_check to croak on block*/ |
1314 | /* this will cause transfer_check to croak on block */ |
1295 | SvRV_set (coro_current, (SV *)coro->hv); |
1315 | SvRV_set (coro_current, (SV *)coro->hv); |
1296 | |
1316 | |
1297 | load_perl (aTHX_ coro); |
1317 | load_perl (aTHX_ coro); |
1298 | |
1318 | |
1299 | /* restore swapped sv's */ |
1319 | /* restore swapped sv's */ |
… | |
… | |
1380 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
1400 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
1381 | |
1401 | |
1382 | PL_runops = RUNOPS_DEFAULT; |
1402 | PL_runops = RUNOPS_DEFAULT; |
1383 | ENTER; |
1403 | ENTER; |
1384 | SAVETMPS; |
1404 | SAVETMPS; |
|
|
1405 | PUSHMARK (SP); |
1385 | EXTEND (SP, 3); |
1406 | EXTEND (SP, 3); |
1386 | PUSHMARK (SP); |
|
|
1387 | PUSHs (&PL_sv_no); |
1407 | PUSHs (&PL_sv_no); |
1388 | PUSHs (fullname); |
1408 | PUSHs (fullname); |
1389 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
1409 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
1390 | PUTBACK; |
1410 | PUTBACK; |
1391 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1411 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
… | |
… | |
1418 | gv_efullname3 (fullname, gv, 0); |
1438 | gv_efullname3 (fullname, gv, 0); |
1419 | |
1439 | |
1420 | PL_runops = RUNOPS_DEFAULT; |
1440 | PL_runops = RUNOPS_DEFAULT; |
1421 | ENTER; |
1441 | ENTER; |
1422 | SAVETMPS; |
1442 | SAVETMPS; |
|
|
1443 | PUSHMARK (SP); |
1423 | EXTEND (SP, 3); |
1444 | EXTEND (SP, 3); |
1424 | PUSHMARK (SP); |
|
|
1425 | PUSHs (&PL_sv_yes); |
1445 | PUSHs (&PL_sv_yes); |
1426 | PUSHs (fullname); |
1446 | PUSHs (fullname); |
1427 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc (SUB_ARGARRAY)) : &PL_sv_undef); |
1447 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc (SUB_ARGARRAY)) : &PL_sv_undef); |
1428 | PUTBACK; |
1448 | PUTBACK; |
1429 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1449 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
… | |
… | |
1442 | dSP; |
1462 | dSP; |
1443 | |
1463 | |
1444 | PL_runops = RUNOPS_DEFAULT; |
1464 | PL_runops = RUNOPS_DEFAULT; |
1445 | ENTER; |
1465 | ENTER; |
1446 | SAVETMPS; |
1466 | SAVETMPS; |
1447 | EXTEND (SP, 3); |
|
|
1448 | PL_runops = RUNOPS_DEFAULT; |
|
|
1449 | PUSHMARK (SP); |
1467 | PUSHMARK (SP); |
|
|
1468 | EXTEND (SP, 2); |
1450 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
1469 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
1451 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
1470 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
1452 | PUTBACK; |
1471 | PUTBACK; |
1453 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
1472 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
1454 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1473 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
… | |
… | |
1825 | |
1844 | |
1826 | static int |
1845 | static int |
1827 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1846 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1828 | { |
1847 | { |
1829 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1848 | struct coro *coro = (struct coro *)mg->mg_ptr; |
|
|
1849 | |
|
|
1850 | coro_state_destroy (aTHX_ coro); |
1830 | mg->mg_ptr = 0; |
1851 | mg->mg_ptr = 0; |
1831 | |
1852 | |
1832 | coro_state_destroy (aTHX_ coro); |
|
|
1833 | SvREFCNT_dec (coro->on_destroy); |
1853 | SvREFCNT_dec (coro->on_destroy); |
1834 | SvREFCNT_dec (coro->status); |
1854 | SvREFCNT_dec (coro->status); |
1835 | |
1855 | |
1836 | Safefree (coro); |
1856 | Safefree (coro); |
1837 | |
1857 | |
… | |
… | |
1840 | |
1860 | |
1841 | static int ecb_cold |
1861 | static int ecb_cold |
1842 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1862 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1843 | { |
1863 | { |
1844 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1864 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1845 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1865 | /* we simply nuke the pointers in the copy, causing perl to croak */ |
1846 | mg->mg_ptr = 0; |
1866 | mg->mg_ptr = 0; |
1847 | mg->mg_virtual = 0; |
1867 | mg->mg_virtual = 0; |
1848 | |
1868 | |
1849 | return 0; |
1869 | return 0; |
1850 | } |
1870 | } |
… | |
… | |
2372 | safe_cancel (pTHX_ struct coro *coro, SV **arg, int items) |
2392 | safe_cancel (pTHX_ struct coro *coro, SV **arg, int items) |
2373 | { |
2393 | { |
2374 | if (coro->cctx) |
2394 | if (coro->cctx) |
2375 | croak ("coro inside C callback, unable to cancel at this time, caught"); |
2395 | croak ("coro inside C callback, unable to cancel at this time, caught"); |
2376 | |
2396 | |
2377 | if (coro->flags & CF_NEW) |
2397 | if (coro->flags & (CF_NEW | CF_ZOMBIE)) |
2378 | { |
2398 | { |
2379 | coro_set_status (aTHX_ coro, arg, items); |
2399 | coro_set_status (aTHX_ coro, arg, items); |
2380 | coro_state_destroy (aTHX_ coro); |
2400 | coro_state_destroy (aTHX_ coro); |
2381 | } |
2401 | } |
2382 | else |
2402 | else |
… | |
… | |
2462 | |
2482 | |
2463 | if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE)) |
2483 | if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE)) |
2464 | api_trace (aTHX_ coro_current, 0); |
2484 | api_trace (aTHX_ coro_current, 0); |
2465 | |
2485 | |
2466 | frame->prepare = prepare_schedule; |
2486 | frame->prepare = prepare_schedule; |
2467 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2487 | av_push (av_async_pool, SvREFCNT_inc_NN (hv)); |
2468 | } |
2488 | } |
2469 | } |
2489 | } |
2470 | else |
2490 | else |
2471 | { |
2491 | { |
2472 | /* first iteration, simply fall through */ |
2492 | /* first iteration, simply fall through */ |
… | |
… | |
2485 | static void |
2505 | static void |
2486 | coro_rouse_callback (pTHX_ CV *cv) |
2506 | coro_rouse_callback (pTHX_ CV *cv) |
2487 | { |
2507 | { |
2488 | dXSARGS; |
2508 | dXSARGS; |
2489 | SV *data = (SV *)S_GENSUB_ARG; |
2509 | SV *data = (SV *)S_GENSUB_ARG; |
|
|
2510 | SV *coro = SvRV (data); |
2490 | |
2511 | |
|
|
2512 | /* data starts being either undef or a coro, and is replaced by the results when done */ |
2491 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2513 | if (SvTYPE (coro) != SVt_PVAV) |
2492 | { |
2514 | { |
2493 | /* first call, set args */ |
2515 | /* first call, set args */ |
2494 | SV *coro = SvRV (data); |
|
|
2495 | AV *av = newAV (); |
|
|
2496 | |
2516 | |
2497 | SvRV_set (data, (SV *)av); |
2517 | assert (&ST (0) < &ST (1)); /* ensure the stack is in the order we expect it to be */ |
|
|
2518 | SvRV_set (data, (SV *)av_make (items, &ST (0))); /* av_make copies the SVs */ |
2498 | |
2519 | |
2499 | /* better take a full copy of the arguments */ |
2520 | if (coro != &PL_sv_undef) |
2500 | while (items--) |
2521 | { |
2501 | av_store (av, items, newSVsv (ST (items))); |
|
|
2502 | |
|
|
2503 | api_ready (aTHX_ coro); |
2522 | api_ready (aTHX_ coro); |
2504 | SvREFCNT_dec (coro); |
2523 | SvREFCNT_dec_NN (coro); |
|
|
2524 | } |
2505 | } |
2525 | } |
2506 | |
2526 | |
2507 | XSRETURN_EMPTY; |
2527 | XSRETURN_EMPTY; |
2508 | } |
2528 | } |
2509 | |
2529 | |
… | |
… | |
2526 | |
2546 | |
2527 | EXTEND (SP, AvFILLp (av) + 1); |
2547 | EXTEND (SP, AvFILLp (av) + 1); |
2528 | for (i = 0; i <= AvFILLp (av); ++i) |
2548 | for (i = 0; i <= AvFILLp (av); ++i) |
2529 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2549 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2530 | |
2550 | |
2531 | /* we have stolen the elements, so set length to zero and free */ |
2551 | /* we have stolen the elements, make it unreal and free */ |
2532 | AvFILLp (av) = -1; |
2552 | AvREAL_off (av); |
2533 | av_undef (av); |
2553 | av_undef (av); |
2534 | |
2554 | |
2535 | PUTBACK; |
2555 | PUTBACK; |
2536 | } |
2556 | } |
2537 | |
2557 | |
… | |
… | |
2562 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2582 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2563 | |
2583 | |
2564 | { |
2584 | { |
2565 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2585 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2566 | SV *data = (SV *)S_GENSUB_ARG; |
2586 | SV *data = (SV *)S_GENSUB_ARG; |
|
|
2587 | int data_ready = SvTYPE (SvRV (data)) == SVt_PVAV; |
|
|
2588 | |
|
|
2589 | /* if there is no data, we need to store the current coro in the reference so we can be woken up */ |
|
|
2590 | if (!data_ready) |
|
|
2591 | if (SvRV (data) != &PL_sv_undef) |
|
|
2592 | croak ("Coro::rouse_wait was called on a calback that is already being waited for - only one thread can wait for a rouse callback, caught"); |
|
|
2593 | else |
|
|
2594 | SvRV_set (data, SvREFCNT_inc_NN (SvRV (coro_current))); |
2567 | |
2595 | |
2568 | frame->data = (void *)data; |
2596 | frame->data = (void *)data; |
2569 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
2597 | frame->prepare = data_ready ? prepare_nop : prepare_schedule; |
2570 | frame->check = slf_check_rouse_wait; |
2598 | frame->check = slf_check_rouse_wait; |
2571 | } |
2599 | } |
2572 | } |
2600 | } |
2573 | |
2601 | |
2574 | static SV * |
2602 | static SV * |
2575 | coro_new_rouse_cb (pTHX) |
2603 | coro_new_rouse_cb (pTHX) |
2576 | { |
2604 | { |
2577 | HV *hv = (HV *)SvRV (coro_current); |
2605 | HV *hv = (HV *)SvRV (coro_current); |
2578 | struct coro *coro = SvSTATE_hv (hv); |
2606 | struct coro *coro = SvSTATE_hv (hv); |
2579 | SV *data = newRV_inc ((SV *)hv); |
2607 | SV *data = newRV_noinc (&PL_sv_undef); |
2580 | SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data); |
2608 | SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data); |
2581 | |
2609 | |
2582 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
2610 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
2583 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
2611 | SvREFCNT_dec_NN (data); /* magicext increases the refcount */ |
2584 | |
2612 | |
2585 | SvREFCNT_dec (coro->rouse_cb); |
2613 | SvREFCNT_dec (coro->rouse_cb); |
2586 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
2614 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
2587 | |
2615 | |
2588 | return cb; |
2616 | return cb; |
… | |
… | |
3675 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3703 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
3676 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3704 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
3677 | |
3705 | |
3678 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3706 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3679 | |
3707 | |
|
|
3708 | newCONSTSUB (coro_state_stash, "BACKEND", newSVpv (CORO_BACKEND, 0)); /* undocumented */ |
|
|
3709 | |
3680 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
3710 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
3681 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
3711 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
3682 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
3712 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
3683 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
3713 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
3684 | |
3714 | |
… | |
… | |
3808 | call (Coro::State coro, SV *coderef) |
3838 | call (Coro::State coro, SV *coderef) |
3809 | ALIAS: |
3839 | ALIAS: |
3810 | eval = 1 |
3840 | eval = 1 |
3811 | CODE: |
3841 | CODE: |
3812 | { |
3842 | { |
|
|
3843 | struct coro *current = SvSTATE_current; |
|
|
3844 | |
3813 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3845 | if ((coro == current) || (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))) |
3814 | { |
3846 | { |
3815 | struct coro *current = SvSTATE_current; |
|
|
3816 | struct CoroSLF slf_save; |
3847 | struct CoroSLF slf_save; |
3817 | |
3848 | |
3818 | if (current != coro) |
3849 | if (current != coro) |
3819 | { |
3850 | { |
3820 | PUTBACK; |
3851 | PUTBACK; |