… | |
… | |
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> |
… | |
… | |
26 | #ifndef SVs_PADSTALE |
27 | #ifndef SVs_PADSTALE |
27 | # define SVs_PADSTALE 0 |
28 | # define SVs_PADSTALE 0 |
28 | #endif |
29 | #endif |
29 | |
30 | |
30 | #ifdef PadARRAY |
31 | #ifdef PadARRAY |
31 | # define NEWPADAPI |
32 | # define NEWPADAPI 1 |
32 | # define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *)) |
33 | # define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *)) |
33 | #else |
34 | #else |
34 | typedef AV PADNAMELIST; |
35 | typedef AV PADNAMELIST; |
35 | # if !PERL_VERSION_ATLEAST(5,8,0) |
36 | # if !PERL_VERSION_ATLEAST(5,8,0) |
36 | typedef AV PADLIST; |
37 | typedef AV PADLIST; |
… | |
… | |
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 |
|
|
53 | |
|
|
54 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
|
|
55 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
|
|
56 | #if PERL_VERSION_ATLEAST(5,19,0) |
|
|
57 | # define SVt_BIND SVt_IV |
|
|
58 | #endif |
46 | |
59 | |
47 | #if defined(_WIN32) |
60 | #if defined(_WIN32) |
48 | # undef HAS_GETTIMEOFDAY |
61 | # undef HAS_GETTIMEOFDAY |
49 | # undef setjmp |
62 | # undef setjmp |
50 | # undef longjmp |
63 | # undef longjmp |
… | |
… | |
510 | ecb_inline PADLIST * |
523 | ecb_inline PADLIST * |
511 | coro_derive_padlist (pTHX_ CV *cv) |
524 | coro_derive_padlist (pTHX_ CV *cv) |
512 | { |
525 | { |
513 | PADLIST *padlist = CvPADLIST (cv); |
526 | PADLIST *padlist = CvPADLIST (cv); |
514 | PADLIST *newpadlist; |
527 | PADLIST *newpadlist; |
|
|
528 | PADNAMELIST *padnames; |
515 | PAD *newpad; |
529 | PAD *newpad; |
516 | PADOFFSET const off = PadlistMAX (padlist) + 1; |
530 | PADOFFSET off = PadlistMAX (padlist) + 1; |
517 | |
531 | |
518 | newPADLIST(newpadlist); |
532 | #if NEWPADAPI |
519 | #if !PERL_VERSION_ATLEAST(5,15,3) |
533 | |
520 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
534 | /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */ |
521 | AvREAL_off (newpadlist); |
535 | /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */ |
522 | #endif |
536 | while (!PadlistARRAY (padlist)[off - 1]) |
|
|
537 | --off; |
|
|
538 | |
|
|
539 | Perl_pad_push (aTHX_ padlist, off); |
|
|
540 | |
|
|
541 | newpad = PadlistARRAY (padlist)[off]; |
|
|
542 | PadlistARRAY (padlist)[off] = 0; |
|
|
543 | |
|
|
544 | #else |
|
|
545 | |
523 | #if PERL_VERSION_ATLEAST (5,10,0) |
546 | #if PERL_VERSION_ATLEAST (5,10,0) |
524 | Perl_pad_push (aTHX_ padlist, off); |
547 | Perl_pad_push (aTHX_ padlist, off); |
525 | #else |
548 | #else |
526 | Perl_pad_push (aTHX_ padlist, off, 1); |
549 | Perl_pad_push (aTHX_ padlist, off, 1); |
527 | #endif |
550 | #endif |
|
|
551 | |
528 | newpad = PadlistARRAY (padlist)[off]; |
552 | newpad = PadlistARRAY (padlist)[off]; |
529 | PadlistMAX (padlist) = off - 1; |
553 | PadlistMAX (padlist) = off - 1; |
530 | |
554 | |
|
|
555 | #endif |
|
|
556 | |
|
|
557 | newPADLIST (newpadlist); |
|
|
558 | #if !PERL_VERSION_ATLEAST(5,15,3) |
|
|
559 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
|
|
560 | AvREAL_off (newpadlist); |
|
|
561 | #endif |
|
|
562 | |
531 | /* Already extended to 2 elements by newPADLIST. */ |
563 | /* Already extended to 2 elements by newPADLIST. */ |
532 | PadlistMAX (newpadlist) = 1; |
564 | PadlistMAX (newpadlist) = 1; |
533 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
565 | |
|
|
566 | padnames = PadlistNAMES (padlist); |
|
|
567 | ++PadnamelistREFCNT (padnames); |
|
|
568 | PadlistNAMES (newpadlist) = padnames; |
|
|
569 | |
534 | PadlistARRAY (newpadlist)[1] = newpad; |
570 | PadlistARRAY (newpadlist)[1] = newpad; |
535 | |
571 | |
536 | return newpadlist; |
572 | return newpadlist; |
537 | } |
573 | } |
538 | |
574 | |
… | |
… | |
546 | |
582 | |
547 | while (i > 0) /* special-case index 0 */ |
583 | while (i > 0) /* special-case index 0 */ |
548 | { |
584 | { |
549 | /* we try to be extra-careful here */ |
585 | /* we try to be extra-careful here */ |
550 | PAD *pad = PadlistARRAY (padlist)[i--]; |
586 | PAD *pad = PadlistARRAY (padlist)[i--]; |
|
|
587 | |
|
|
588 | if (pad) |
|
|
589 | { |
551 | I32 j = PadMAX (pad); |
590 | I32 j = PadMAX (pad); |
552 | |
591 | |
553 | while (j >= 0) |
592 | while (j >= 0) |
554 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
593 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
555 | |
594 | |
556 | PadMAX (pad) = -1; |
595 | PadMAX (pad) = -1; |
557 | SvREFCNT_dec (pad); |
596 | SvREFCNT_dec (pad); |
|
|
597 | } |
558 | } |
598 | } |
559 | |
599 | |
560 | SvREFCNT_dec (PadlistNAMES (padlist)); |
600 | PadnamelistREFCNT_dec (PadlistNAMES (padlist)); |
561 | |
601 | |
562 | #ifdef NEWPADAPI |
602 | #if NEWPADAPI |
563 | Safefree (PadlistARRAY (padlist)); |
603 | Safefree (PadlistARRAY (padlist)); |
564 | Safefree (padlist); |
604 | Safefree (padlist); |
565 | #else |
605 | #else |
566 | AvFILLp (padlist) = -1; |
606 | AvFILLp (padlist) = -1; |
567 | AvREAL_off (padlist); |
607 | AvREAL_off (padlist); |
… | |
… | |
1109 | |
1149 | |
1110 | GvSV (PL_defgv) = newSV (0); |
1150 | GvSV (PL_defgv) = newSV (0); |
1111 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1151 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1112 | GvSV (PL_errgv) = newSV (0); |
1152 | GvSV (PL_errgv) = newSV (0); |
1113 | 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); |
1114 | 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 |
1115 | PL_rs = newSVsv (GvSV (irsgv)); |
1158 | PL_rs = newSVsv (GvSV (irsgv)); |
1116 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1159 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1117 | |
1160 | |
1118 | { |
1161 | { |
1119 | dSP; |
1162 | dSP; |
… | |
… | |
1888 | /* nothing to schedule: call the idle handler */ |
1931 | /* nothing to schedule: call the idle handler */ |
1889 | if (SvROK (sv_idle) |
1932 | if (SvROK (sv_idle) |
1890 | && SvOBJECT (SvRV (sv_idle))) |
1933 | && SvOBJECT (SvRV (sv_idle))) |
1891 | { |
1934 | { |
1892 | 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); |
1893 | 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 | } |
1894 | |
1954 | |
1895 | ++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 */ |
1896 | api_ready (aTHX_ SvRV (sv_idle)); |
1956 | api_ready (aTHX_ SvRV (sv_idle)); |
1897 | --coro_nready; |
1957 | --coro_nready; |
1898 | } |
1958 | } |
… | |
… | |
2096 | AV *od = coro->on_destroy; |
2156 | AV *od = coro->on_destroy; |
2097 | |
2157 | |
2098 | if (!od) |
2158 | if (!od) |
2099 | return; |
2159 | return; |
2100 | |
2160 | |
|
|
2161 | coro->on_destroy = 0; |
|
|
2162 | sv_2mortal ((SV *)od); |
|
|
2163 | |
2101 | while (AvFILLp (od) >= 0) |
2164 | while (AvFILLp (od) >= 0) |
2102 | { |
2165 | { |
2103 | SV *cb = sv_2mortal (av_pop (od)); |
2166 | SV *cb = sv_2mortal (av_pop (od)); |
2104 | |
2167 | |
2105 | /* coro hv's (and only hv's at the moment) are supported as well */ |
2168 | /* coro hv's (and only hv's at the moment) are supported as well */ |
… | |
… | |
2199 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2262 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2200 | } |
2263 | } |
2201 | else |
2264 | else |
2202 | { |
2265 | { |
2203 | struct coro *self = SvSTATE_current; |
2266 | struct coro *self = SvSTATE_current; |
|
|
2267 | |
|
|
2268 | if (!self) |
|
|
2269 | croak ("Coro::cancel called outside of thread content,"); |
2204 | |
2270 | |
2205 | /* otherwise we cancel directly, purely for speed reasons |
2271 | /* otherwise we cancel directly, purely for speed reasons |
2206 | * unfortunately, this requires some magic trickery, as |
2272 | * unfortunately, this requires some magic trickery, as |
2207 | * 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. |
2208 | * this is ugly, and hopefully fully worth the extra speed. |
2274 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2550 | |
2616 | |
2551 | /* "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 */ |
2552 | static void |
2618 | static void |
2553 | slf_destroy (pTHX_ struct coro *coro) |
2619 | slf_destroy (pTHX_ struct coro *coro) |
2554 | { |
2620 | { |
2555 | /* this callback is reserved for slf functions needing to do cleanup */ |
2621 | struct CoroSLF frame = coro->slf_frame; |
2556 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2557 | coro->slf_frame.destroy (aTHX_ &coro->slf_frame); |
|
|
2558 | |
2622 | |
2559 | /* |
2623 | /* |
2560 | * The on_destroy above most likely is from an SLF call. |
2624 | * The on_destroy below most likely is from an SLF call. |
2561 | * 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 |
2562 | * the coro, we will have to force-finish it here, otherwise |
2626 | * the coro, we will have to force-finish it here, otherwise |
2563 | * cleanup functions cannot call SLF functions. |
2627 | * cleanup functions cannot call SLF functions. |
2564 | */ |
2628 | */ |
2565 | 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); |
2566 | } |
2634 | } |
2567 | |
2635 | |
2568 | /* |
2636 | /* |
2569 | * these not obviously related functions are all rolled into one |
2637 | * these not obviously related functions are all rolled into one |
2570 | * 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 |
… | |
… | |
2910 | { |
2978 | { |
2911 | AV *av = (AV *)frame->data; |
2979 | AV *av = (AV *)frame->data; |
2912 | SV *count_sv = AvARRAY (av)[0]; |
2980 | SV *count_sv = AvARRAY (av)[0]; |
2913 | SV *coro_hv = SvRV (coro_current); |
2981 | SV *coro_hv = SvRV (coro_current); |
2914 | |
2982 | |
|
|
2983 | frame->destroy = 0; |
|
|
2984 | |
2915 | /* 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 */ |
2916 | 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 | |
2917 | return 0; |
2991 | return 0; |
|
|
2992 | } |
2918 | else if (SvIVX (count_sv) > 0) |
2993 | else if (SvIVX (count_sv) > 0) |
2919 | { |
2994 | { |
2920 | frame->destroy = 0; |
|
|
2921 | |
|
|
2922 | if (acquire) |
2995 | if (acquire) |
2923 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2996 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2924 | else |
2997 | else |
2925 | coro_semaphore_adjust (aTHX_ av, 0); |
2998 | coro_semaphore_adjust (aTHX_ av, 0); |
2926 | |
2999 | |
… | |
… | |
3148 | /* it quickly returns */ |
3221 | /* it quickly returns */ |
3149 | if (CORO_THROW) |
3222 | if (CORO_THROW) |
3150 | return 0; |
3223 | return 0; |
3151 | |
3224 | |
3152 | /* one element that is an RV? repeat! */ |
3225 | /* one element that is an RV? repeat! */ |
3153 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
3226 | if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV) |
3154 | return 1; |
3227 | return 1; |
3155 | |
3228 | |
3156 | /* restore status */ |
3229 | /* restore status */ |
3157 | { |
3230 | { |
3158 | SV *data_sv = av_pop (state); |
3231 | SV *data_sv = av_pop (state); |
… | |
… | |
3386 | |
3459 | |
3387 | PROTOTYPES: DISABLE |
3460 | PROTOTYPES: DISABLE |
3388 | |
3461 | |
3389 | BOOT: |
3462 | BOOT: |
3390 | { |
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" |
3391 | #ifdef USE_ITHREADS |
3466 | #ifdef USE_ITHREADS |
3392 | # if CORO_PTHREAD |
3467 | # if CORO_PTHREAD |
3393 | coro_thx = PERL_GET_CONTEXT; |
3468 | coro_thx = PERL_GET_CONTEXT; |
3394 | # endif |
3469 | # endif |
3395 | #endif |
3470 | #endif |
… | |
… | |
3470 | void |
3545 | void |
3471 | transfer (...) |
3546 | transfer (...) |
3472 | PROTOTYPE: $$ |
3547 | PROTOTYPE: $$ |
3473 | CODE: |
3548 | CODE: |
3474 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3549 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3475 | |
|
|
3476 | void |
|
|
3477 | _exit (int code) |
|
|
3478 | PROTOTYPE: $ |
|
|
3479 | CODE: |
|
|
3480 | _exit (code); |
|
|
3481 | |
3550 | |
3482 | SV * |
3551 | SV * |
3483 | clone (Coro::State coro) |
3552 | clone (Coro::State coro) |
3484 | CODE: |
3553 | CODE: |
3485 | { |
3554 | { |
… | |
… | |
4237 | { |
4306 | { |
4238 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4307 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4239 | coro_old_pp_sselect = 0; |
4308 | coro_old_pp_sselect = 0; |
4240 | } |
4309 | } |
4241 | |
4310 | |
|
|
4311 | MODULE = Coro::State PACKAGE = Coro::Util |
|
|
4312 | |
|
|
4313 | void |
|
|
4314 | _exit (int code) |
|
|
4315 | CODE: |
|
|
4316 | _exit (code); |
|
|
4317 | |
|
|
4318 | NV |
|
|
4319 | time () |
|
|
4320 | CODE: |
|
|
4321 | RETVAL = nvtime (aTHX); |
|
|
4322 | OUTPUT: |
|
|
4323 | RETVAL |
|
|
4324 | |
|
|
4325 | NV |
|
|
4326 | gettimeofday () |
|
|
4327 | PPCODE: |
|
|
4328 | { |
|
|
4329 | UV tv [2]; |
|
|
4330 | u2time (aTHX_ tv); |
|
|
4331 | EXTEND (SP, 2); |
|
|
4332 | PUSHs (sv_2mortal (newSVuv (tv [0]))); |
|
|
4333 | PUSHs (sv_2mortal (newSVuv (tv [1]))); |
|
|
4334 | } |
|
|
4335 | |