… | |
… | |
14 | #include "perliol.h" |
14 | #include "perliol.h" |
15 | |
15 | |
16 | #include "schmorp.h" |
16 | #include "schmorp.h" |
17 | |
17 | |
18 | #define ECB_NO_THREADS 1 |
18 | #define ECB_NO_THREADS 1 |
|
|
19 | #define ECB_NO_LIBM 1 |
19 | #include "ecb.h" |
20 | #include "ecb.h" |
20 | |
21 | |
21 | #include <stddef.h> |
22 | #include <stddef.h> |
22 | #include <stdio.h> |
23 | #include <stdio.h> |
23 | #include <errno.h> |
24 | #include <errno.h> |
… | |
… | |
41 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
42 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
42 | # define PadARRAY AvARRAY |
43 | # define PadARRAY AvARRAY |
43 | # define PadMAX AvFILLp |
44 | # define PadMAX AvFILLp |
44 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
45 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
45 | #endif |
46 | #endif |
|
|
47 | #ifndef PadnamelistREFCNT |
|
|
48 | # define PadnamelistREFCNT(pnl) SvREFCNT (pnl) |
|
|
49 | #endif |
|
|
50 | #ifndef PadnamelistREFCNT_dec |
|
|
51 | # define PadnamelistREFCNT_dec(pnl) SvREFCNT_dec (pnl) |
|
|
52 | #endif |
46 | |
53 | |
47 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
54 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
48 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
55 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
49 | #if PERL_VERSION_ATLEAST(5,19,0) |
56 | #if PERL_VERSION_ATLEAST(5,19,0) |
50 | # define SVt_BIND SVt_IV |
57 | # define SVt_BIND SVt_IV |
… | |
… | |
516 | ecb_inline PADLIST * |
523 | ecb_inline PADLIST * |
517 | coro_derive_padlist (pTHX_ CV *cv) |
524 | coro_derive_padlist (pTHX_ CV *cv) |
518 | { |
525 | { |
519 | PADLIST *padlist = CvPADLIST (cv); |
526 | PADLIST *padlist = CvPADLIST (cv); |
520 | PADLIST *newpadlist; |
527 | PADLIST *newpadlist; |
|
|
528 | PADNAMELIST *padnames; |
521 | PAD *newpad; |
529 | PAD *newpad; |
522 | PADOFFSET off = PadlistMAX (padlist) + 1; |
530 | PADOFFSET off = PadlistMAX (padlist) + 1; |
523 | |
531 | |
524 | #if NEWPADAPI |
532 | #if NEWPADAPI |
525 | |
533 | |
… | |
… | |
552 | AvREAL_off (newpadlist); |
560 | AvREAL_off (newpadlist); |
553 | #endif |
561 | #endif |
554 | |
562 | |
555 | /* Already extended to 2 elements by newPADLIST. */ |
563 | /* Already extended to 2 elements by newPADLIST. */ |
556 | PadlistMAX (newpadlist) = 1; |
564 | PadlistMAX (newpadlist) = 1; |
557 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
565 | |
|
|
566 | padnames = PadlistNAMES (padlist); |
|
|
567 | ++PadnamelistREFCNT (padnames); |
|
|
568 | PadlistNAMES (newpadlist) = padnames; |
|
|
569 | |
558 | PadlistARRAY (newpadlist)[1] = newpad; |
570 | PadlistARRAY (newpadlist)[1] = newpad; |
559 | |
571 | |
560 | return newpadlist; |
572 | return newpadlist; |
561 | } |
573 | } |
562 | |
574 | |
… | |
… | |
570 | |
582 | |
571 | while (i > 0) /* special-case index 0 */ |
583 | while (i > 0) /* special-case index 0 */ |
572 | { |
584 | { |
573 | /* we try to be extra-careful here */ |
585 | /* we try to be extra-careful here */ |
574 | PAD *pad = PadlistARRAY (padlist)[i--]; |
586 | PAD *pad = PadlistARRAY (padlist)[i--]; |
|
|
587 | |
|
|
588 | if (pad) |
|
|
589 | { |
575 | I32 j = PadMAX (pad); |
590 | I32 j = PadMAX (pad); |
576 | |
591 | |
577 | while (j >= 0) |
592 | while (j >= 0) |
578 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
593 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
579 | |
594 | |
580 | PadMAX (pad) = -1; |
595 | PadMAX (pad) = -1; |
581 | SvREFCNT_dec (pad); |
596 | SvREFCNT_dec (pad); |
|
|
597 | } |
582 | } |
598 | } |
583 | |
599 | |
584 | SvREFCNT_dec (PadlistNAMES (padlist)); |
600 | PadnamelistREFCNT_dec (PadlistNAMES (padlist)); |
585 | |
601 | |
586 | #if NEWPADAPI |
602 | #if NEWPADAPI |
587 | Safefree (PadlistARRAY (padlist)); |
603 | Safefree (PadlistARRAY (padlist)); |
588 | Safefree (padlist); |
604 | Safefree (padlist); |
589 | #else |
605 | #else |
… | |
… | |
1133 | |
1149 | |
1134 | GvSV (PL_defgv) = newSV (0); |
1150 | GvSV (PL_defgv) = newSV (0); |
1135 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1151 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1136 | GvSV (PL_errgv) = newSV (0); |
1152 | GvSV (PL_errgv) = newSV (0); |
1137 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1153 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1138 | GvHV (PL_hintgv) = 0; |
1154 | GvHV (PL_hintgv) = newHV (); |
|
|
1155 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
1156 | hv_magic (GvHV (PL_hintgv), 0, PERL_MAGIC_hints); |
|
|
1157 | #endif |
1139 | PL_rs = newSVsv (GvSV (irsgv)); |
1158 | PL_rs = newSVsv (GvSV (irsgv)); |
1140 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1159 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1141 | |
1160 | |
1142 | { |
1161 | { |
1143 | dSP; |
1162 | dSP; |
… | |
… | |
1325 | |
1344 | |
1326 | if (PL_curcop != &PL_compiling) |
1345 | if (PL_curcop != &PL_compiling) |
1327 | { |
1346 | { |
1328 | SV **cb; |
1347 | SV **cb; |
1329 | |
1348 | |
1330 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1349 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB && cxstack_ix >= 0) |
1331 | { |
1350 | { |
1332 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1351 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1333 | |
1352 | |
1334 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1353 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1335 | { |
1354 | { |
… | |
… | |
1912 | /* nothing to schedule: call the idle handler */ |
1931 | /* nothing to schedule: call the idle handler */ |
1913 | if (SvROK (sv_idle) |
1932 | if (SvROK (sv_idle) |
1914 | && SvOBJECT (SvRV (sv_idle))) |
1933 | && SvOBJECT (SvRV (sv_idle))) |
1915 | { |
1934 | { |
1916 | if (SvRV (sv_idle) == SvRV (coro_current)) |
1935 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1936 | { |
|
|
1937 | require_pv ("Carp"); |
|
|
1938 | |
|
|
1939 | { |
|
|
1940 | dSP; |
|
|
1941 | |
|
|
1942 | ENTER; |
|
|
1943 | SAVETMPS; |
|
|
1944 | |
|
|
1945 | PUSHMARK (SP); |
1917 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
1946 | XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::idle blocked itself - did you try to block inside an event loop callback? Caught", 0))); |
|
|
1947 | PUTBACK; |
|
|
1948 | call_pv ("Carp::confess", G_VOID | G_DISCARD); |
|
|
1949 | |
|
|
1950 | FREETMPS; |
|
|
1951 | LEAVE; |
|
|
1952 | } |
|
|
1953 | } |
1918 | |
1954 | |
1919 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1955 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1920 | api_ready (aTHX_ SvRV (sv_idle)); |
1956 | api_ready (aTHX_ SvRV (sv_idle)); |
1921 | --coro_nready; |
1957 | --coro_nready; |
1922 | } |
1958 | } |
… | |
… | |
2226 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2262 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2227 | } |
2263 | } |
2228 | else |
2264 | else |
2229 | { |
2265 | { |
2230 | struct coro *self = SvSTATE_current; |
2266 | struct coro *self = SvSTATE_current; |
|
|
2267 | |
|
|
2268 | if (!self) |
|
|
2269 | croak ("Coro::cancel called outside of thread content,"); |
2231 | |
2270 | |
2232 | /* otherwise we cancel directly, purely for speed reasons |
2271 | /* otherwise we cancel directly, purely for speed reasons |
2233 | * unfortunately, this requires some magic trickery, as |
2272 | * unfortunately, this requires some magic trickery, as |
2234 | * somebody else could cancel us, so we have to fight the cancellation. |
2273 | * somebody else could cancel us, so we have to fight the cancellation. |
2235 | * this is ugly, and hopefully fully worth the extra speed. |
2274 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2577 | |
2616 | |
2578 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2617 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2579 | static void |
2618 | static void |
2580 | slf_destroy (pTHX_ struct coro *coro) |
2619 | slf_destroy (pTHX_ struct coro *coro) |
2581 | { |
2620 | { |
2582 | /* this callback is reserved for slf functions needing to do cleanup */ |
2621 | struct CoroSLF frame = coro->slf_frame; |
2583 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2584 | coro->slf_frame.destroy (aTHX_ &coro->slf_frame); |
|
|
2585 | |
2622 | |
2586 | /* |
2623 | /* |
2587 | * The on_destroy above most likely is from an SLF call. |
2624 | * The on_destroy below most likely is from an SLF call. |
2588 | * Since by definition the SLF call will not finish when we destroy |
2625 | * Since by definition the SLF call will not finish when we destroy |
2589 | * the coro, we will have to force-finish it here, otherwise |
2626 | * the coro, we will have to force-finish it here, otherwise |
2590 | * cleanup functions cannot call SLF functions. |
2627 | * cleanup functions cannot call SLF functions. |
2591 | */ |
2628 | */ |
2592 | coro->slf_frame.prepare = 0; |
2629 | coro->slf_frame.prepare = 0; |
|
|
2630 | |
|
|
2631 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
2632 | if (frame.destroy && frame.prepare && !PL_dirty) |
|
|
2633 | frame.destroy (aTHX_ &frame); |
2593 | } |
2634 | } |
2594 | |
2635 | |
2595 | /* |
2636 | /* |
2596 | * these not obviously related functions are all rolled into one |
2637 | * these not obviously related functions are all rolled into one |
2597 | * function to increase chances that they all will call transfer with the same |
2638 | * function to increase chances that they all will call transfer with the same |
… | |
… | |
2937 | { |
2978 | { |
2938 | AV *av = (AV *)frame->data; |
2979 | AV *av = (AV *)frame->data; |
2939 | SV *count_sv = AvARRAY (av)[0]; |
2980 | SV *count_sv = AvARRAY (av)[0]; |
2940 | SV *coro_hv = SvRV (coro_current); |
2981 | SV *coro_hv = SvRV (coro_current); |
2941 | |
2982 | |
|
|
2983 | frame->destroy = 0; |
|
|
2984 | |
2942 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2985 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2943 | if (CORO_THROW) |
2986 | if (ecb_expect_false (CORO_THROW)) |
|
|
2987 | { |
|
|
2988 | /* we still might be responsible for the semaphore, so wake up others */ |
|
|
2989 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2990 | |
2944 | return 0; |
2991 | return 0; |
|
|
2992 | } |
2945 | else if (SvIVX (count_sv) > 0) |
2993 | else if (SvIVX (count_sv) > 0) |
2946 | { |
2994 | { |
2947 | frame->destroy = 0; |
|
|
2948 | |
|
|
2949 | if (acquire) |
2995 | if (acquire) |
2950 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2996 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2951 | else |
2997 | else |
2952 | coro_semaphore_adjust (aTHX_ av, 0); |
2998 | coro_semaphore_adjust (aTHX_ av, 0); |
2953 | |
2999 | |
… | |
… | |
3413 | |
3459 | |
3414 | PROTOTYPES: DISABLE |
3460 | PROTOTYPES: DISABLE |
3415 | |
3461 | |
3416 | BOOT: |
3462 | BOOT: |
3417 | { |
3463 | { |
|
|
3464 | #define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl."); |
|
|
3465 | #include "state.h" |
3418 | #ifdef USE_ITHREADS |
3466 | #ifdef USE_ITHREADS |
3419 | # if CORO_PTHREAD |
3467 | # if CORO_PTHREAD |
3420 | coro_thx = PERL_GET_CONTEXT; |
3468 | coro_thx = PERL_GET_CONTEXT; |
3421 | # endif |
3469 | # endif |
3422 | #endif |
3470 | #endif |