… | |
… | |
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; |
… | |
… | |
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 |
46 | |
47 | |
|
|
48 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
|
|
49 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
|
|
50 | #if PERL_VERSION_ATLEAST(5,19,0) |
|
|
51 | # define SVt_BIND SVt_IV |
|
|
52 | #endif |
|
|
53 | |
47 | #if defined(_WIN32) |
54 | #if defined(_WIN32) |
48 | # undef HAS_GETTIMEOFDAY |
55 | # undef HAS_GETTIMEOFDAY |
49 | # undef setjmp |
56 | # undef setjmp |
50 | # undef longjmp |
57 | # undef longjmp |
51 | # undef _exit |
58 | # undef _exit |
… | |
… | |
279 | |
286 | |
280 | #if CORO_JIT |
287 | #if CORO_JIT |
281 | /* APPLE doesn't have mmap though */ |
288 | /* APPLE doesn't have mmap though */ |
282 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
289 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
283 | #ifndef CORO_JIT_TYPE |
290 | #ifndef CORO_JIT_TYPE |
284 | #if __x86_64 && CORO_JIT_UNIXY |
291 | #if ECB_AMD64 && CORO_JIT_UNIXY |
285 | #define CORO_JIT_TYPE "amd64-unix" |
292 | #define CORO_JIT_TYPE "amd64-unix" |
286 | #elif __i386 && CORO_JIT_UNIXY |
293 | #elif __i386 && CORO_JIT_UNIXY |
287 | #define CORO_JIT_TYPE "x86-unix" |
294 | #define CORO_JIT_TYPE "x86-unix" |
288 | #endif |
295 | #endif |
289 | #endif |
296 | #endif |
… | |
… | |
511 | coro_derive_padlist (pTHX_ CV *cv) |
518 | coro_derive_padlist (pTHX_ CV *cv) |
512 | { |
519 | { |
513 | PADLIST *padlist = CvPADLIST (cv); |
520 | PADLIST *padlist = CvPADLIST (cv); |
514 | PADLIST *newpadlist; |
521 | PADLIST *newpadlist; |
515 | PAD *newpad; |
522 | PAD *newpad; |
516 | PADOFFSET const off = PadlistMAX (padlist) + 1; |
523 | PADOFFSET off = PadlistMAX (padlist) + 1; |
517 | |
524 | |
518 | newPADLIST(newpadlist); |
525 | #if NEWPADAPI |
519 | #if !PERL_VERSION_ATLEAST(5,15,3) |
526 | |
520 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
527 | /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */ |
521 | AvREAL_off (newpadlist); |
528 | /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */ |
522 | #endif |
529 | while (!PadlistARRAY (padlist)[off - 1]) |
|
|
530 | --off; |
|
|
531 | |
|
|
532 | Perl_pad_push (aTHX_ padlist, off); |
|
|
533 | |
|
|
534 | newpad = PadlistARRAY (padlist)[off]; |
|
|
535 | PadlistARRAY (padlist)[off] = 0; |
|
|
536 | |
|
|
537 | #else |
|
|
538 | |
523 | #if PERL_VERSION_ATLEAST (5,10,0) |
539 | #if PERL_VERSION_ATLEAST (5,10,0) |
524 | Perl_pad_push (aTHX_ padlist, off); |
540 | Perl_pad_push (aTHX_ padlist, off); |
525 | #else |
541 | #else |
526 | Perl_pad_push (aTHX_ padlist, off, 1); |
542 | Perl_pad_push (aTHX_ padlist, off, 1); |
527 | #endif |
543 | #endif |
|
|
544 | |
528 | newpad = PadlistARRAY (padlist)[off]; |
545 | newpad = PadlistARRAY (padlist)[off]; |
529 | PadlistMAX (padlist) = off - 1; |
546 | PadlistMAX (padlist) = off - 1; |
|
|
547 | |
|
|
548 | #endif |
|
|
549 | |
|
|
550 | newPADLIST (newpadlist); |
|
|
551 | #if !PERL_VERSION_ATLEAST(5,15,3) |
|
|
552 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
|
|
553 | AvREAL_off (newpadlist); |
|
|
554 | #endif |
530 | |
555 | |
531 | /* Already extended to 2 elements by newPADLIST. */ |
556 | /* Already extended to 2 elements by newPADLIST. */ |
532 | PadlistMAX (newpadlist) = 1; |
557 | PadlistMAX (newpadlist) = 1; |
533 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
558 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
534 | PadlistARRAY (newpadlist)[1] = newpad; |
559 | PadlistARRAY (newpadlist)[1] = newpad; |
… | |
… | |
546 | |
571 | |
547 | while (i > 0) /* special-case index 0 */ |
572 | while (i > 0) /* special-case index 0 */ |
548 | { |
573 | { |
549 | /* we try to be extra-careful here */ |
574 | /* we try to be extra-careful here */ |
550 | PAD *pad = PadlistARRAY (padlist)[i--]; |
575 | PAD *pad = PadlistARRAY (padlist)[i--]; |
|
|
576 | |
|
|
577 | if (pad) |
|
|
578 | { |
551 | I32 j = PadMAX (pad); |
579 | I32 j = PadMAX (pad); |
552 | |
580 | |
553 | while (j >= 0) |
581 | while (j >= 0) |
554 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
582 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
555 | |
583 | |
556 | PadMAX (pad) = -1; |
584 | PadMAX (pad) = -1; |
557 | SvREFCNT_dec (pad); |
585 | SvREFCNT_dec (pad); |
|
|
586 | } |
558 | } |
587 | } |
559 | |
588 | |
560 | SvREFCNT_dec (PadlistNAMES (padlist)); |
589 | SvREFCNT_dec (PadlistNAMES (padlist)); |
561 | |
590 | |
562 | #ifdef NEWPADAPI |
591 | #if NEWPADAPI |
563 | Safefree (PadlistARRAY (padlist)); |
592 | Safefree (PadlistARRAY (padlist)); |
564 | Safefree (padlist); |
593 | Safefree (padlist); |
565 | #else |
594 | #else |
566 | AvFILLp (padlist) = -1; |
595 | AvFILLp (padlist) = -1; |
567 | AvREAL_off (padlist); |
596 | AvREAL_off (padlist); |
… | |
… | |
1109 | |
1138 | |
1110 | GvSV (PL_defgv) = newSV (0); |
1139 | GvSV (PL_defgv) = newSV (0); |
1111 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1140 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1112 | GvSV (PL_errgv) = newSV (0); |
1141 | GvSV (PL_errgv) = newSV (0); |
1113 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1142 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1114 | GvHV (PL_hintgv) = 0; |
1143 | GvHV (PL_hintgv) = newHV (); |
|
|
1144 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
1145 | hv_magic (GvHV (PL_hintgv), 0, PERL_MAGIC_hints); |
|
|
1146 | #endif |
1115 | PL_rs = newSVsv (GvSV (irsgv)); |
1147 | PL_rs = newSVsv (GvSV (irsgv)); |
1116 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1148 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1117 | |
1149 | |
1118 | { |
1150 | { |
1119 | dSP; |
1151 | dSP; |
… | |
… | |
1888 | /* nothing to schedule: call the idle handler */ |
1920 | /* nothing to schedule: call the idle handler */ |
1889 | if (SvROK (sv_idle) |
1921 | if (SvROK (sv_idle) |
1890 | && SvOBJECT (SvRV (sv_idle))) |
1922 | && SvOBJECT (SvRV (sv_idle))) |
1891 | { |
1923 | { |
1892 | if (SvRV (sv_idle) == SvRV (coro_current)) |
1924 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1925 | { |
|
|
1926 | require_pv ("Carp"); |
|
|
1927 | |
|
|
1928 | { |
|
|
1929 | dSP; |
|
|
1930 | |
|
|
1931 | ENTER; |
|
|
1932 | SAVETMPS; |
|
|
1933 | |
|
|
1934 | PUSHMARK (SP); |
1893 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
1935 | XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::idle blocked itself - did you try to block inside an event loop callback? Caught", 0))); |
|
|
1936 | PUTBACK; |
|
|
1937 | call_pv ("Carp::confess", G_VOID | G_DISCARD); |
|
|
1938 | |
|
|
1939 | FREETMPS; |
|
|
1940 | LEAVE; |
|
|
1941 | } |
|
|
1942 | } |
1894 | |
1943 | |
1895 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1944 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1896 | api_ready (aTHX_ SvRV (sv_idle)); |
1945 | api_ready (aTHX_ SvRV (sv_idle)); |
1897 | --coro_nready; |
1946 | --coro_nready; |
1898 | } |
1947 | } |
… | |
… | |
2096 | AV *od = coro->on_destroy; |
2145 | AV *od = coro->on_destroy; |
2097 | |
2146 | |
2098 | if (!od) |
2147 | if (!od) |
2099 | return; |
2148 | return; |
2100 | |
2149 | |
|
|
2150 | coro->on_destroy = 0; |
|
|
2151 | sv_2mortal ((SV *)od); |
|
|
2152 | |
2101 | while (AvFILLp (od) >= 0) |
2153 | while (AvFILLp (od) >= 0) |
2102 | { |
2154 | { |
2103 | SV *cb = sv_2mortal (av_pop (od)); |
2155 | SV *cb = sv_2mortal (av_pop (od)); |
2104 | |
2156 | |
2105 | /* coro hv's (and only hv's at the moment) are supported as well */ |
2157 | /* coro hv's (and only hv's at the moment) are supported as well */ |
… | |
… | |
2199 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2251 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2200 | } |
2252 | } |
2201 | else |
2253 | else |
2202 | { |
2254 | { |
2203 | struct coro *self = SvSTATE_current; |
2255 | struct coro *self = SvSTATE_current; |
|
|
2256 | |
|
|
2257 | if (!self) |
|
|
2258 | croak ("Coro::cancel called outside of thread content,"); |
2204 | |
2259 | |
2205 | /* otherwise we cancel directly, purely for speed reasons |
2260 | /* otherwise we cancel directly, purely for speed reasons |
2206 | * unfortunately, this requires some magic trickery, as |
2261 | * unfortunately, this requires some magic trickery, as |
2207 | * somebody else could cancel us, so we have to fight the cancellation. |
2262 | * somebody else could cancel us, so we have to fight the cancellation. |
2208 | * this is ugly, and hopefully fully worth the extra speed. |
2263 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2550 | |
2605 | |
2551 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2606 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2552 | static void |
2607 | static void |
2553 | slf_destroy (pTHX_ struct coro *coro) |
2608 | slf_destroy (pTHX_ struct coro *coro) |
2554 | { |
2609 | { |
2555 | /* this callback is reserved for slf functions needing to do cleanup */ |
2610 | 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 | |
2611 | |
2559 | /* |
2612 | /* |
2560 | * The on_destroy above most likely is from an SLF call. |
2613 | * The on_destroy below most likely is from an SLF call. |
2561 | * Since by definition the SLF call will not finish when we destroy |
2614 | * Since by definition the SLF call will not finish when we destroy |
2562 | * the coro, we will have to force-finish it here, otherwise |
2615 | * the coro, we will have to force-finish it here, otherwise |
2563 | * cleanup functions cannot call SLF functions. |
2616 | * cleanup functions cannot call SLF functions. |
2564 | */ |
2617 | */ |
2565 | coro->slf_frame.prepare = 0; |
2618 | coro->slf_frame.prepare = 0; |
|
|
2619 | |
|
|
2620 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
2621 | if (frame.destroy && frame.prepare && !PL_dirty) |
|
|
2622 | frame.destroy (aTHX_ &frame); |
2566 | } |
2623 | } |
2567 | |
2624 | |
2568 | /* |
2625 | /* |
2569 | * these not obviously related functions are all rolled into one |
2626 | * these not obviously related functions are all rolled into one |
2570 | * function to increase chances that they all will call transfer with the same |
2627 | * function to increase chances that they all will call transfer with the same |
… | |
… | |
2910 | { |
2967 | { |
2911 | AV *av = (AV *)frame->data; |
2968 | AV *av = (AV *)frame->data; |
2912 | SV *count_sv = AvARRAY (av)[0]; |
2969 | SV *count_sv = AvARRAY (av)[0]; |
2913 | SV *coro_hv = SvRV (coro_current); |
2970 | SV *coro_hv = SvRV (coro_current); |
2914 | |
2971 | |
|
|
2972 | frame->destroy = 0; |
|
|
2973 | |
2915 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2974 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2916 | if (CORO_THROW) |
2975 | if (ecb_expect_false (CORO_THROW)) |
|
|
2976 | { |
|
|
2977 | /* we still might be responsible for the semaphore, so wake up others */ |
|
|
2978 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2979 | |
2917 | return 0; |
2980 | return 0; |
|
|
2981 | } |
2918 | else if (SvIVX (count_sv) > 0) |
2982 | else if (SvIVX (count_sv) > 0) |
2919 | { |
2983 | { |
2920 | frame->destroy = 0; |
|
|
2921 | |
|
|
2922 | if (acquire) |
2984 | if (acquire) |
2923 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2985 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2924 | else |
2986 | else |
2925 | coro_semaphore_adjust (aTHX_ av, 0); |
2987 | coro_semaphore_adjust (aTHX_ av, 0); |
2926 | |
2988 | |
… | |
… | |
3148 | /* it quickly returns */ |
3210 | /* it quickly returns */ |
3149 | if (CORO_THROW) |
3211 | if (CORO_THROW) |
3150 | return 0; |
3212 | return 0; |
3151 | |
3213 | |
3152 | /* one element that is an RV? repeat! */ |
3214 | /* one element that is an RV? repeat! */ |
3153 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
3215 | if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV) |
3154 | return 1; |
3216 | return 1; |
3155 | |
3217 | |
3156 | /* restore status */ |
3218 | /* restore status */ |
3157 | { |
3219 | { |
3158 | SV *data_sv = av_pop (state); |
3220 | SV *data_sv = av_pop (state); |
… | |
… | |
3386 | |
3448 | |
3387 | PROTOTYPES: DISABLE |
3449 | PROTOTYPES: DISABLE |
3388 | |
3450 | |
3389 | BOOT: |
3451 | BOOT: |
3390 | { |
3452 | { |
|
|
3453 | #define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl."); |
|
|
3454 | #include "state.h" |
3391 | #ifdef USE_ITHREADS |
3455 | #ifdef USE_ITHREADS |
3392 | # if CORO_PTHREAD |
3456 | # if CORO_PTHREAD |
3393 | coro_thx = PERL_GET_CONTEXT; |
3457 | coro_thx = PERL_GET_CONTEXT; |
3394 | # endif |
3458 | # endif |
3395 | #endif |
3459 | #endif |
… | |
… | |
3470 | void |
3534 | void |
3471 | transfer (...) |
3535 | transfer (...) |
3472 | PROTOTYPE: $$ |
3536 | PROTOTYPE: $$ |
3473 | CODE: |
3537 | CODE: |
3474 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3538 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3475 | |
|
|
3476 | void |
|
|
3477 | _exit (int code) |
|
|
3478 | PROTOTYPE: $ |
|
|
3479 | CODE: |
|
|
3480 | _exit (code); |
|
|
3481 | |
3539 | |
3482 | SV * |
3540 | SV * |
3483 | clone (Coro::State coro) |
3541 | clone (Coro::State coro) |
3484 | CODE: |
3542 | CODE: |
3485 | { |
3543 | { |
… | |
… | |
4237 | { |
4295 | { |
4238 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4296 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4239 | coro_old_pp_sselect = 0; |
4297 | coro_old_pp_sselect = 0; |
4240 | } |
4298 | } |
4241 | |
4299 | |
|
|
4300 | MODULE = Coro::State PACKAGE = Coro::Util |
|
|
4301 | |
|
|
4302 | void |
|
|
4303 | _exit (int code) |
|
|
4304 | CODE: |
|
|
4305 | _exit (code); |
|
|
4306 | |
|
|
4307 | NV |
|
|
4308 | time () |
|
|
4309 | CODE: |
|
|
4310 | RETVAL = nvtime (aTHX); |
|
|
4311 | OUTPUT: |
|
|
4312 | RETVAL |
|
|
4313 | |
|
|
4314 | NV |
|
|
4315 | gettimeofday () |
|
|
4316 | PPCODE: |
|
|
4317 | { |
|
|
4318 | UV tv [2]; |
|
|
4319 | u2time (aTHX_ tv); |
|
|
4320 | EXTEND (SP, 2); |
|
|
4321 | PUSHs (sv_2mortal (newSVuv (tv [0]))); |
|
|
4322 | PUSHs (sv_2mortal (newSVuv (tv [1]))); |
|
|
4323 | } |
|
|
4324 | |