ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.469 by root, Fri Jul 14 02:43:50 2017 UTC vs.
Revision 1.478 by root, Mon Mar 16 11:12:52 2020 UTC

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"
116# define CORO_CLOCK_MONOTONIC 1 136# define CORO_CLOCK_MONOTONIC 1
117# define CORO_CLOCK_THREAD_CPUTIME_ID 3 137# define CORO_CLOCK_THREAD_CPUTIME_ID 3
118# endif 138# endif
119#endif 139#endif
120 140
141/* one off bugfix for perl 5.22 */
142#if PERL_VERSION_ATLEAST(5,22,0) && !PERL_VERSION_ATLEAST(5,24,0)
143# undef PadlistNAMES
144# define PadlistNAMES(pl) *((PADNAMELIST **)PadlistARRAY (pl))
145#endif
146
121#if PERL_VERSION_ATLEAST(5,24,0) 147#if PERL_VERSION_ATLEAST(5,24,0)
122# define SUB_ARGARRAY PL_curpad[0] 148# define SUB_ARGARRAY PL_curpad[0]
123#else 149#else
124# define SUB_ARGARRAY (SV *)cx->blk_sub.argarray 150# define SUB_ARGARRAY (SV *)cx->blk_sub.argarray
125#endif 151#endif
126 152
127/* perl usually suppressed asserts. for debugging, we sometimes force it to be on */ 153/* perl usually suppresses asserts. for debugging, we sometimes force it to be on */
128#if 0 154#if 0
129# undef NDEBUG 155# undef NDEBUG
130# include <assert.h> 156# include <assert.h>
131#endif 157#endif
132 158
257 int usecount; /* number of transfers to this coro */ 283 int usecount; /* number of transfers to this coro */
258 284
259 /* coro process data */ 285 /* coro process data */
260 int prio; 286 int prio;
261 SV *except; /* exception to be thrown */ 287 SV *except; /* exception to be thrown */
262 SV *rouse_cb; /* last rouse callback */ 288 SV *rouse_cb; /* most recently created rouse callback */
263 AV *on_destroy; /* callbacks or coros to notify on destroy */ 289 AV *on_destroy; /* callbacks or coros to notify on destroy */
264 AV *status; /* the exit status list */ 290 AV *status; /* the exit status list */
265 291
266 /* async_pool */ 292 /* async_pool */
267 SV *saved_deffh; 293 SV *saved_deffh;
1216 myop.op_next = Nullop; 1242 myop.op_next = Nullop;
1217 myop.op_type = OP_ENTERSUB; 1243 myop.op_type = OP_ENTERSUB;
1218 myop.op_flags = OPf_WANT_VOID; 1244 myop.op_flags = OPf_WANT_VOID;
1219 1245
1220 PUSHMARK (SP); 1246 PUSHMARK (SP);
1221 PUSHs ((SV *)coro->startcv); 1247 XPUSHs ((SV *)coro->startcv);
1222 PUTBACK; 1248 PUTBACK;
1223 PL_op = (OP *)&myop; 1249 PL_op = (OP *)&myop;
1224 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1250 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1225 } 1251 }
1226 1252
1374 av_push (av, SvREFCNT_inc_NN (*bot++)); 1400 av_push (av, SvREFCNT_inc_NN (*bot++));
1375 1401
1376 PL_runops = RUNOPS_DEFAULT; 1402 PL_runops = RUNOPS_DEFAULT;
1377 ENTER; 1403 ENTER;
1378 SAVETMPS; 1404 SAVETMPS;
1405 PUSHMARK (SP);
1379 EXTEND (SP, 3); 1406 EXTEND (SP, 3);
1380 PUSHMARK (SP);
1381 PUSHs (&PL_sv_no); 1407 PUSHs (&PL_sv_no);
1382 PUSHs (fullname); 1408 PUSHs (fullname);
1383 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 1409 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1384 PUTBACK; 1410 PUTBACK;
1385 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);
1412 gv_efullname3 (fullname, gv, 0); 1438 gv_efullname3 (fullname, gv, 0);
1413 1439
1414 PL_runops = RUNOPS_DEFAULT; 1440 PL_runops = RUNOPS_DEFAULT;
1415 ENTER; 1441 ENTER;
1416 SAVETMPS; 1442 SAVETMPS;
1443 PUSHMARK (SP);
1417 EXTEND (SP, 3); 1444 EXTEND (SP, 3);
1418 PUSHMARK (SP);
1419 PUSHs (&PL_sv_yes); 1445 PUSHs (&PL_sv_yes);
1420 PUSHs (fullname); 1446 PUSHs (fullname);
1421 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);
1422 PUTBACK; 1448 PUTBACK;
1423 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);
1436 dSP; 1462 dSP;
1437 1463
1438 PL_runops = RUNOPS_DEFAULT; 1464 PL_runops = RUNOPS_DEFAULT;
1439 ENTER; 1465 ENTER;
1440 SAVETMPS; 1466 SAVETMPS;
1441 EXTEND (SP, 3);
1442 PL_runops = RUNOPS_DEFAULT;
1443 PUSHMARK (SP); 1467 PUSHMARK (SP);
1468 EXTEND (SP, 2);
1444 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 1469 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
1445 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 1470 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
1446 PUTBACK; 1471 PUTBACK;
1447 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);
1448 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);
1834 1859
1835static int ecb_cold 1860static int ecb_cold
1836coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) 1861coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1837{ 1862{
1838 /* called when perl clones the current process the slow way (windows process emulation) */ 1863 /* called when perl clones the current process the slow way (windows process emulation) */
1839 /* WE SIMply nuke the pointers in the copy, causing perl to croak */ 1864 /* we simply nuke the pointers in the copy, causing perl to croak */
1840 mg->mg_ptr = 0; 1865 mg->mg_ptr = 0;
1841 mg->mg_virtual = 0; 1866 mg->mg_virtual = 0;
1842 1867
1843 return 0; 1868 return 0;
1844} 1869}
2366safe_cancel (pTHX_ struct coro *coro, SV **arg, int items) 2391safe_cancel (pTHX_ struct coro *coro, SV **arg, int items)
2367{ 2392{
2368 if (coro->cctx) 2393 if (coro->cctx)
2369 croak ("coro inside C callback, unable to cancel at this time, caught"); 2394 croak ("coro inside C callback, unable to cancel at this time, caught");
2370 2395
2371 if (coro->flags & CF_NEW) 2396 if (coro->flags & (CF_NEW | CF_ZOMBIE))
2372 { 2397 {
2373 coro_set_status (aTHX_ coro, arg, items); 2398 coro_set_status (aTHX_ coro, arg, items);
2374 coro_state_destroy (aTHX_ coro); 2399 coro_state_destroy (aTHX_ coro);
2375 } 2400 }
2376 else 2401 else
2456 2481
2457 if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE)) 2482 if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE))
2458 api_trace (aTHX_ coro_current, 0); 2483 api_trace (aTHX_ coro_current, 0);
2459 2484
2460 frame->prepare = prepare_schedule; 2485 frame->prepare = prepare_schedule;
2461 av_push (av_async_pool, SvREFCNT_inc (hv)); 2486 av_push (av_async_pool, SvREFCNT_inc_NN (hv));
2462 } 2487 }
2463 } 2488 }
2464 else 2489 else
2465 { 2490 {
2466 /* first iteration, simply fall through */ 2491 /* first iteration, simply fall through */
2479static void 2504static void
2480coro_rouse_callback (pTHX_ CV *cv) 2505coro_rouse_callback (pTHX_ CV *cv)
2481{ 2506{
2482 dXSARGS; 2507 dXSARGS;
2483 SV *data = (SV *)S_GENSUB_ARG; 2508 SV *data = (SV *)S_GENSUB_ARG;
2509 SV *coro = SvRV (data);
2484 2510
2511 /* data starts being either undef or a coro, and is replaced by the results when done */
2485 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2512 if (SvTYPE (coro) != SVt_PVAV)
2486 { 2513 {
2487 /* first call, set args */ 2514 /* first call, set args */
2488 SV *coro = SvRV (data);
2489 AV *av = newAV ();
2490 2515
2491 SvRV_set (data, (SV *)av); 2516 assert (&ST (0) < &ST (1)); /* ensure the stack is in the order we expect it to be */
2517 SvRV_set (data, (SV *)av_make (items, &ST (0))); /* av_make copies the SVs */
2492 2518
2493 /* better take a full copy of the arguments */ 2519 if (coro != &PL_sv_undef)
2494 while (items--) 2520 {
2495 av_store (av, items, newSVsv (ST (items)));
2496
2497 api_ready (aTHX_ coro); 2521 api_ready (aTHX_ coro);
2498 SvREFCNT_dec (coro); 2522 SvREFCNT_dec_NN (coro);
2523 }
2499 } 2524 }
2500 2525
2501 XSRETURN_EMPTY; 2526 XSRETURN_EMPTY;
2502} 2527}
2503 2528
2520 2545
2521 EXTEND (SP, AvFILLp (av) + 1); 2546 EXTEND (SP, AvFILLp (av) + 1);
2522 for (i = 0; i <= AvFILLp (av); ++i) 2547 for (i = 0; i <= AvFILLp (av); ++i)
2523 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2548 PUSHs (sv_2mortal (AvARRAY (av)[i]));
2524 2549
2525 /* we have stolen the elements, so set length to zero and free */ 2550 /* we have stolen the elements, make it unreal and free */
2526 AvFILLp (av) = -1; 2551 AvREAL_off (av);
2527 av_undef (av); 2552 av_undef (av);
2528 2553
2529 PUTBACK; 2554 PUTBACK;
2530 } 2555 }
2531 2556
2556 croak ("Coro::rouse_wait called with illegal callback argument,"); 2581 croak ("Coro::rouse_wait called with illegal callback argument,");
2557 2582
2558 { 2583 {
2559 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2584 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2560 SV *data = (SV *)S_GENSUB_ARG; 2585 SV *data = (SV *)S_GENSUB_ARG;
2586 int data_ready = SvTYPE (SvRV (data)) == SVt_PVAV;
2587
2588 /* if there is no data, we need to store the current coro in the reference so we can be woken up */
2589 if (!data_ready)
2590 if (SvRV (data) != &PL_sv_undef)
2591 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");
2592 else
2593 SvRV_set (data, SvREFCNT_inc_NN (SvRV (coro_current)));
2561 2594
2562 frame->data = (void *)data; 2595 frame->data = (void *)data;
2563 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; 2596 frame->prepare = data_ready ? prepare_nop : prepare_schedule;
2564 frame->check = slf_check_rouse_wait; 2597 frame->check = slf_check_rouse_wait;
2565 } 2598 }
2566} 2599}
2567 2600
2568static SV * 2601static SV *
2569coro_new_rouse_cb (pTHX) 2602coro_new_rouse_cb (pTHX)
2570{ 2603{
2571 HV *hv = (HV *)SvRV (coro_current); 2604 HV *hv = (HV *)SvRV (coro_current);
2572 struct coro *coro = SvSTATE_hv (hv); 2605 struct coro *coro = SvSTATE_hv (hv);
2573 SV *data = newRV_inc ((SV *)hv); 2606 SV *data = newRV_noinc (&PL_sv_undef);
2574 SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data); 2607 SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data);
2575 2608
2576 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); 2609 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
2577 SvREFCNT_dec (data); /* magicext increases the refcount */ 2610 SvREFCNT_dec_NN (data); /* magicext increases the refcount */
2578 2611
2579 SvREFCNT_dec (coro->rouse_cb); 2612 SvREFCNT_dec (coro->rouse_cb);
2580 coro->rouse_cb = SvREFCNT_inc_NN (cb); 2613 coro->rouse_cb = SvREFCNT_inc_NN (cb);
2581 2614
2582 return cb; 2615 return cb;
3669 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 3702 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
3670 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 3703 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
3671 3704
3672 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 3705 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
3673 3706
3707 newCONSTSUB (coro_state_stash, "BACKEND", newSVpv (CORO_BACKEND, 0)); /* undocumented */
3708
3674 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 3709 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
3675 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 3710 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
3676 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); 3711 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
3677 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); 3712 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
3678 3713
3802call (Coro::State coro, SV *coderef) 3837call (Coro::State coro, SV *coderef)
3803 ALIAS: 3838 ALIAS:
3804 eval = 1 3839 eval = 1
3805 CODE: 3840 CODE:
3806{ 3841{
3842 struct coro *current = SvSTATE_current;
3843
3807 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3844 if ((coro == current) || (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)))
3808 { 3845 {
3809 struct coro *current = SvSTATE_current;
3810 struct CoroSLF slf_save; 3846 struct CoroSLF slf_save;
3811 3847
3812 if (current != coro) 3848 if (current != coro)
3813 { 3849 {
3814 PUTBACK; 3850 PUTBACK;
4073 coroapi.enterleave_hook = api_enterleave_hook; 4109 coroapi.enterleave_hook = api_enterleave_hook;
4074 coroapi.enterleave_unhook = api_enterleave_unhook; 4110 coroapi.enterleave_unhook = api_enterleave_unhook;
4075 coroapi.enterleave_scope_hook = api_enterleave_scope_hook; 4111 coroapi.enterleave_scope_hook = api_enterleave_scope_hook;
4076 4112
4077 /*GCoroAPI = &coroapi;*/ 4113 /*GCoroAPI = &coroapi;*/
4078 sv_setiv (sv, (IV)&coroapi); 4114 sv_setiv (sv, PTR2IV (&coroapi));
4079 SvREADONLY_on (sv); 4115 SvREADONLY_on (sv);
4080 } 4116 }
4081} 4117}
4082 4118
4083SV * 4119SV *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines