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.429 by root, Fri Dec 7 14:21:09 2012 UTC vs.
Revision 1.450 by root, Sun Jun 28 22:31:03 2015 UTC

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
34typedef AV PADNAMELIST; 35typedef AV PADNAMELIST;
35# if !PERL_VERSION_ATLEAST(5,8,0) 36# if !PERL_VERSION_ATLEAST(5,8,0)
36typedef AV PADLIST; 37typedef 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
235 SV *saved_deffh; 248 SV *saved_deffh;
236 SV *invoke_cb; 249 SV *invoke_cb;
237 AV *invoke_av; 250 AV *invoke_av;
238 251
239 /* on_enter/on_leave */ 252 /* on_enter/on_leave */
240 AV *on_enter; 253 AV *on_enter; AV *on_enter_xs;
241 AV *on_leave; 254 AV *on_leave; AV *on_leave_xs;
242 255
243 /* swap_sv */ 256 /* swap_sv */
244 AV *swap_sv; 257 AV *swap_sv;
245 258
246 /* times */ 259 /* times */
279 292
280#if CORO_JIT 293#if CORO_JIT
281 /* APPLE doesn't have mmap though */ 294 /* APPLE doesn't have mmap though */
282 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) 295 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
283 #ifndef CORO_JIT_TYPE 296 #ifndef CORO_JIT_TYPE
284 #if __x86_64 && CORO_JIT_UNIXY 297 #if ECB_AMD64 && CORO_JIT_UNIXY
285 #define CORO_JIT_TYPE "amd64-unix" 298 #define CORO_JIT_TYPE "amd64-unix"
286 #elif __i386 && CORO_JIT_UNIXY 299 #elif __i386 && CORO_JIT_UNIXY
287 #define CORO_JIT_TYPE "x86-unix" 300 #define CORO_JIT_TYPE "x86-unix"
288 #endif 301 #endif
289 #endif 302 #endif
482 495
483 return 0; 496 return 0;
484} 497}
485 498
486ecb_inline struct coro * 499ecb_inline struct coro *
487SvSTATE_ (pTHX_ SV *coro) 500SvSTATE_ (pTHX_ SV *coro_sv)
488{ 501{
489 MAGIC *mg; 502 MAGIC *mg;
490 503
491 if (SvROK (coro)) 504 if (SvROK (coro_sv))
492 coro = SvRV (coro); 505 coro_sv = SvRV (coro_sv);
493 506
494 mg = SvSTATEhv_p (aTHX_ coro); 507 mg = SvSTATEhv_p (aTHX_ coro_sv);
495 if (!mg) 508 if (!mg)
496 croak ("Coro::State object required"); 509 croak ("Coro::State object required");
497 510
498 return (struct coro *)mg->mg_ptr; 511 return (struct coro *)mg->mg_ptr;
499} 512}
510ecb_inline PADLIST * 523ecb_inline PADLIST *
511coro_derive_padlist (pTHX_ CV *cv) 524coro_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);
740 780
741 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 781 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
742 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 782 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
743 } 783 }
744 784
785 if (ecb_expect_false (c->on_enter_xs))
786 {
787 int i;
788
789 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2)
790 ((coro_enterleave_hook)AvARRAY (c->on_enter_xs)[i]) (aTHX_ AvARRAY (c->on_enter_xs)[i + 1]);
791 }
792
745 SWAP_SVS (c); 793 SWAP_SVS (c);
746} 794}
747 795
748static void 796static void
749save_perl (pTHX_ Coro__State c) 797save_perl (pTHX_ Coro__State c)
750{ 798{
751 SWAP_SVS (c); 799 SWAP_SVS (c);
800
801 if (ecb_expect_false (c->on_leave_xs))
802 {
803 int i;
804
805 for (i = AvFILLp (c->on_leave_xs) - 1; i >= 0; i -= 2)
806 ((coro_enterleave_hook)AvARRAY (c->on_leave_xs)[i]) (aTHX_ AvARRAY (c->on_leave_xs)[i + 1]);
807 }
752 808
753 if (ecb_expect_false (c->on_leave)) 809 if (ecb_expect_false (c->on_leave))
754 { 810 {
755 int i; 811 int i;
756 812
1109 1165
1110 GvSV (PL_defgv) = newSV (0); 1166 GvSV (PL_defgv) = newSV (0);
1111 GvAV (PL_defgv) = coro->args; coro->args = 0; 1167 GvAV (PL_defgv) = coro->args; coro->args = 0;
1112 GvSV (PL_errgv) = newSV (0); 1168 GvSV (PL_errgv) = newSV (0);
1113 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1169 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1114 GvHV (PL_hintgv) = 0; 1170 GvHV (PL_hintgv) = newHV ();
1171#if PERL_VERSION_ATLEAST (5,10,0)
1172 hv_magic (GvHV (PL_hintgv), 0, PERL_MAGIC_hints);
1173#endif
1115 PL_rs = newSVsv (GvSV (irsgv)); 1174 PL_rs = newSVsv (GvSV (irsgv));
1116 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 1175 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
1117 1176
1118 { 1177 {
1119 dSP; 1178 dSP;
1229 1288
1230 SvREFCNT_dec (coro->saved_deffh); 1289 SvREFCNT_dec (coro->saved_deffh);
1231 SvREFCNT_dec (coro->rouse_cb); 1290 SvREFCNT_dec (coro->rouse_cb);
1232 SvREFCNT_dec (coro->invoke_cb); 1291 SvREFCNT_dec (coro->invoke_cb);
1233 SvREFCNT_dec (coro->invoke_av); 1292 SvREFCNT_dec (coro->invoke_av);
1293 SvREFCNT_dec (coro->on_enter_xs);
1294 SvREFCNT_dec (coro->on_leave_xs);
1234 } 1295 }
1235} 1296}
1236 1297
1237ecb_inline void 1298ecb_inline void
1238free_coro_mortal (pTHX) 1299free_coro_mortal (pTHX)
1301 1362
1302 if (PL_curcop != &PL_compiling) 1363 if (PL_curcop != &PL_compiling)
1303 { 1364 {
1304 SV **cb; 1365 SV **cb;
1305 1366
1306 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) 1367 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB && cxstack_ix >= 0)
1307 { 1368 {
1308 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1369 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1309 1370
1310 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1371 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1311 { 1372 {
1888 /* nothing to schedule: call the idle handler */ 1949 /* nothing to schedule: call the idle handler */
1889 if (SvROK (sv_idle) 1950 if (SvROK (sv_idle)
1890 && SvOBJECT (SvRV (sv_idle))) 1951 && SvOBJECT (SvRV (sv_idle)))
1891 { 1952 {
1892 if (SvRV (sv_idle) == SvRV (coro_current)) 1953 if (SvRV (sv_idle) == SvRV (coro_current))
1954 {
1955 require_pv ("Carp");
1956
1957 {
1958 dSP;
1959
1960 ENTER;
1961 SAVETMPS;
1962
1963 PUSHMARK (SP);
1893 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); 1964 XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::idle blocked itself - did you try to block inside an event loop callback? Caught", 0)));
1965 PUTBACK;
1966 call_pv ("Carp::confess", G_VOID | G_DISCARD);
1967
1968 FREETMPS;
1969 LEAVE;
1970 }
1971 }
1894 1972
1895 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1973 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1896 api_ready (aTHX_ SvRV (sv_idle)); 1974 api_ready (aTHX_ SvRV (sv_idle));
1897 --coro_nready; 1975 --coro_nready;
1898 } 1976 }
2096 AV *od = coro->on_destroy; 2174 AV *od = coro->on_destroy;
2097 2175
2098 if (!od) 2176 if (!od)
2099 return; 2177 return;
2100 2178
2179 coro->on_destroy = 0;
2180 sv_2mortal ((SV *)od);
2181
2101 while (AvFILLp (od) >= 0) 2182 while (AvFILLp (od) >= 0)
2102 { 2183 {
2103 SV *cb = sv_2mortal (av_pop (od)); 2184 SV *cb = sv_2mortal (av_pop (od));
2104 2185
2105 /* coro hv's (and only hv's at the moment) are supported as well */ 2186 /* coro hv's (and only hv's at the moment) are supported as well */
2199 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); 2280 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2200 } 2281 }
2201 else 2282 else
2202 { 2283 {
2203 struct coro *self = SvSTATE_current; 2284 struct coro *self = SvSTATE_current;
2285
2286 if (!self)
2287 croak ("Coro::cancel called outside of thread content,");
2204 2288
2205 /* otherwise we cancel directly, purely for speed reasons 2289 /* otherwise we cancel directly, purely for speed reasons
2206 * unfortunately, this requires some magic trickery, as 2290 * unfortunately, this requires some magic trickery, as
2207 * somebody else could cancel us, so we have to fight the cancellation. 2291 * somebody else could cancel us, so we have to fight the cancellation.
2208 * this is ugly, and hopefully fully worth the extra speed. 2292 * this is ugly, and hopefully fully worth the extra speed.
2550 2634
2551/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ 2635/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */
2552static void 2636static void
2553slf_destroy (pTHX_ struct coro *coro) 2637slf_destroy (pTHX_ struct coro *coro)
2554{ 2638{
2555 /* this callback is reserved for slf functions needing to do cleanup */ 2639 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 2640
2559 /* 2641 /*
2560 * The on_destroy above most likely is from an SLF call. 2642 * The on_destroy below most likely is from an SLF call.
2561 * Since by definition the SLF call will not finish when we destroy 2643 * Since by definition the SLF call will not finish when we destroy
2562 * the coro, we will have to force-finish it here, otherwise 2644 * the coro, we will have to force-finish it here, otherwise
2563 * cleanup functions cannot call SLF functions. 2645 * cleanup functions cannot call SLF functions.
2564 */ 2646 */
2565 coro->slf_frame.prepare = 0; 2647 coro->slf_frame.prepare = 0;
2648
2649 /* this callback is reserved for slf functions needing to do cleanup */
2650 if (frame.destroy && frame.prepare && !PL_dirty)
2651 frame.destroy (aTHX_ &frame);
2566} 2652}
2567 2653
2568/* 2654/*
2569 * these not obviously related functions are all rolled into one 2655 * these not obviously related functions are all rolled into one
2570 * function to increase chances that they all will call transfer with the same 2656 * function to increase chances that they all will call transfer with the same
2753static void 2839static void
2754coro_pop_on_leave (pTHX_ void *coro) 2840coro_pop_on_leave (pTHX_ void *coro)
2755{ 2841{
2756 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); 2842 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2757 on_enterleave_call (aTHX_ sv_2mortal (cb)); 2843 on_enterleave_call (aTHX_ sv_2mortal (cb));
2844}
2845
2846static void
2847enterleave_hook_xs (pTHX_ struct coro *coro, AV **avp, coro_enterleave_hook hook, void *arg)
2848{
2849 if (!hook)
2850 return;
2851
2852 if (!*avp)
2853 {
2854 *avp = newAV ();
2855 AvREAL_off (*avp);
2856 }
2857
2858 av_push (*avp, (SV *)hook);
2859 av_push (*avp, (SV *)arg);
2860}
2861
2862static void
2863enterleave_unhook_xs (pTHX_ struct coro *coro, AV **avp, coro_enterleave_hook hook, int execute)
2864{
2865 AV *av = *avp;
2866 int i;
2867
2868 if (!av)
2869 return;
2870
2871 for (i = AvFILLp (av) - 1; i >= 0; i -= 2)
2872 if (AvARRAY (av)[i] == (SV *)hook)
2873 {
2874 if (execute)
2875 hook ((void *)AvARRAY (av)[i + 1]);
2876
2877 memmove (AvARRAY (av) + i, AvARRAY (av) + i + 2, AvFILLp (av) - i - 1);
2878 av_pop (av);
2879 av_pop (av);
2880 break;
2881 }
2882
2883 if (AvFILLp (av) >= 0)
2884 {
2885 *avp = 0;
2886 SvREFCNT_dec_NN (av);
2887 }
2888}
2889
2890static void
2891api_enterleave_hook (pTHX_ SV *coro_sv, coro_enterleave_hook enter, void *enter_arg, coro_enterleave_hook leave, void *leave_arg)
2892{
2893 struct coro *coro = SvSTATE (coro_sv);
2894
2895 if (SvSTATE_current == coro)
2896 if (enter)
2897 enter (aTHX enter_arg);
2898
2899 enterleave_hook_xs (aTHX_ coro, &coro->on_enter_xs, enter, enter_arg);
2900 enterleave_hook_xs (aTHX_ coro, &coro->on_leave_xs, leave, leave_arg);
2901}
2902
2903static void
2904api_enterleave_unhook (pTHX_ SV *coro_sv, coro_enterleave_hook enter, coro_enterleave_hook leave)
2905{
2906 struct coro *coro = SvSTATE (coro_sv);
2907
2908 enterleave_unhook_xs (aTHX_ coro, &coro->on_enter_xs, enter, 0);
2909 enterleave_unhook_xs (aTHX_ coro, &coro->on_leave_xs, leave, SvSTATE_current == coro);
2910}
2911
2912static void
2913savedestructor_unhook_enter (pTHX_ coro_enterleave_hook enter)
2914{
2915 struct coro *coro = SvSTATE_current;
2916
2917 enterleave_unhook_xs (aTHX_ coro, &coro->on_enter_xs, enter, 0);
2918}
2919
2920static void
2921savedestructor_unhook_leave (pTHX_ coro_enterleave_hook leave)
2922{
2923 struct coro *coro = SvSTATE_current;
2924
2925 enterleave_unhook_xs (aTHX_ coro, &coro->on_leave_xs, leave, 1);
2926}
2927
2928static void
2929api_enterleave_scope_hook (pTHX_ coro_enterleave_hook enter, void *enter_arg, coro_enterleave_hook leave, void *leave_arg)
2930{
2931 api_enterleave_hook (aTHX_ coro_current, enter, enter_arg, leave, leave_arg);
2932
2933 /* this ought to be much cheaper than malloc + a single destructor call */
2934 if (enter) SAVEDESTRUCTOR_X (savedestructor_unhook_enter, enter);
2935 if (leave) SAVEDESTRUCTOR_X (savedestructor_unhook_leave, leave);
2758} 2936}
2759 2937
2760/*****************************************************************************/ 2938/*****************************************************************************/
2761/* PerlIO::cede */ 2939/* PerlIO::cede */
2762 2940
2892 XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); 3070 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2893 PUTBACK; 3071 PUTBACK;
2894 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); 3072 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2895 } 3073 }
2896 3074
2897 SvREFCNT_dec (cb); 3075 SvREFCNT_dec_NN (cb);
2898 } 3076 }
2899} 3077}
2900 3078
2901static void 3079static void
2902coro_semaphore_destroy (pTHX_ struct CoroSLF *frame) 3080coro_semaphore_destroy (pTHX_ struct CoroSLF *frame)
2910{ 3088{
2911 AV *av = (AV *)frame->data; 3089 AV *av = (AV *)frame->data;
2912 SV *count_sv = AvARRAY (av)[0]; 3090 SV *count_sv = AvARRAY (av)[0];
2913 SV *coro_hv = SvRV (coro_current); 3091 SV *coro_hv = SvRV (coro_current);
2914 3092
3093 frame->destroy = 0;
3094
2915 /* if we are about to throw, don't actually acquire the lock, just throw */ 3095 /* if we are about to throw, don't actually acquire the lock, just throw */
2916 if (CORO_THROW) 3096 if (ecb_expect_false (CORO_THROW))
3097 {
3098 /* we still might be responsible for the semaphore, so wake up others */
3099 coro_semaphore_adjust (aTHX_ av, 0);
3100
2917 return 0; 3101 return 0;
3102 }
2918 else if (SvIVX (count_sv) > 0) 3103 else if (SvIVX (count_sv) > 0)
2919 { 3104 {
2920 frame->destroy = 0;
2921
2922 if (acquire) 3105 if (acquire)
2923 SvIVX (count_sv) = SvIVX (count_sv) - 1; 3106 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2924 else 3107 else
2925 coro_semaphore_adjust (aTHX_ av, 0); 3108 coro_semaphore_adjust (aTHX_ av, 0);
2926 3109
3037 { 3220 {
3038 api_ready (aTHX_ cb); 3221 api_ready (aTHX_ cb);
3039 sv_setiv (cb, 0); /* signal waiter */ 3222 sv_setiv (cb, 0); /* signal waiter */
3040 } 3223 }
3041 3224
3042 SvREFCNT_dec (cb); 3225 SvREFCNT_dec_NN (cb);
3043 3226
3044 --count; 3227 --count;
3045 } 3228 }
3046} 3229}
3047 3230
3132 } 3315 }
3133 3316
3134 av_push (state, data_sv); 3317 av_push (state, data_sv);
3135 3318
3136 api_ready (aTHX_ coro); 3319 api_ready (aTHX_ coro);
3137 SvREFCNT_dec (coro); 3320 SvREFCNT_dec_NN (coro);
3138 SvREFCNT_dec ((AV *)state); 3321 SvREFCNT_dec_NN ((AV *)state);
3139} 3322}
3140 3323
3141static int 3324static int
3142slf_check_aio_req (pTHX_ struct CoroSLF *frame) 3325slf_check_aio_req (pTHX_ struct CoroSLF *frame)
3143{ 3326{
3148 /* it quickly returns */ 3331 /* it quickly returns */
3149 if (CORO_THROW) 3332 if (CORO_THROW)
3150 return 0; 3333 return 0;
3151 3334
3152 /* one element that is an RV? repeat! */ 3335 /* one element that is an RV? repeat! */
3153 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 3336 if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV)
3154 return 1; 3337 return 1;
3155 3338
3156 /* restore status */ 3339 /* restore status */
3157 { 3340 {
3158 SV *data_sv = av_pop (state); 3341 SV *data_sv = av_pop (state);
3161 errno = data->errorno; 3344 errno = data->errorno;
3162 PL_laststype = data->laststype; 3345 PL_laststype = data->laststype;
3163 PL_laststatval = data->laststatval; 3346 PL_laststatval = data->laststatval;
3164 PL_statcache = data->statcache; 3347 PL_statcache = data->statcache;
3165 3348
3166 SvREFCNT_dec (data_sv); 3349 SvREFCNT_dec_NN (data_sv);
3167 } 3350 }
3168 3351
3169 /* push result values */ 3352 /* push result values */
3170 { 3353 {
3171 dSP; 3354 dSP;
3386 3569
3387PROTOTYPES: DISABLE 3570PROTOTYPES: DISABLE
3388 3571
3389BOOT: 3572BOOT:
3390{ 3573{
3574#define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl.");
3575#include "state.h"
3391#ifdef USE_ITHREADS 3576#ifdef USE_ITHREADS
3392# if CORO_PTHREAD 3577# if CORO_PTHREAD
3393 coro_thx = PERL_GET_CONTEXT; 3578 coro_thx = PERL_GET_CONTEXT;
3394# endif 3579# endif
3395#endif 3580#endif
3470void 3655void
3471transfer (...) 3656transfer (...)
3472 PROTOTYPE: $$ 3657 PROTOTYPE: $$
3473 CODE: 3658 CODE:
3474 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3659 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3475
3476void
3477_exit (int code)
3478 PROTOTYPE: $
3479 CODE:
3480 _exit (code);
3481 3660
3482SV * 3661SV *
3483clone (Coro::State coro) 3662clone (Coro::State coro)
3484 CODE: 3663 CODE:
3485{ 3664{
3787 coroapi.ready = api_ready; 3966 coroapi.ready = api_ready;
3788 coroapi.is_ready = api_is_ready; 3967 coroapi.is_ready = api_is_ready;
3789 coroapi.nready = coro_nready; 3968 coroapi.nready = coro_nready;
3790 coroapi.current = coro_current; 3969 coroapi.current = coro_current;
3791 3970
3971 coroapi.enterleave_hook = api_enterleave_hook;
3972 coroapi.enterleave_unhook = api_enterleave_unhook;
3973 coroapi.enterleave_scope_hook = api_enterleave_scope_hook;
3974
3792 /*GCoroAPI = &coroapi;*/ 3975 /*GCoroAPI = &coroapi;*/
3793 sv_setiv (sv, (IV)&coroapi); 3976 sv_setiv (sv, (IV)&coroapi);
3794 SvREADONLY_on (sv); 3977 SvREADONLY_on (sv);
3795 } 3978 }
3796} 3979}
3861 4044
3862void 4045void
3863_set_current (SV *current) 4046_set_current (SV *current)
3864 PROTOTYPE: $ 4047 PROTOTYPE: $
3865 CODE: 4048 CODE:
3866 SvREFCNT_dec (SvRV (coro_current)); 4049 SvREFCNT_dec_NN (SvRV (coro_current));
3867 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 4050 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3868 4051
3869void 4052void
3870_set_readyhook (SV *hook) 4053_set_readyhook (SV *hook)
3871 PROTOTYPE: $ 4054 PROTOTYPE: $
3955 4138
3956 if ((SV *)hv == &PL_sv_undef) 4139 if ((SV *)hv == &PL_sv_undef)
3957 { 4140 {
3958 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); 4141 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1);
3959 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); 4142 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3960 SvREFCNT_dec (sv); 4143 SvREFCNT_dec_NN (sv);
3961 } 4144 }
3962 4145
3963 { 4146 {
3964 struct coro *coro = SvSTATE_hv (hv); 4147 struct coro *coro = SvSTATE_hv (hv);
3965 4148
3972 api_ready (aTHX_ (SV *)hv); 4155 api_ready (aTHX_ (SV *)hv);
3973 4156
3974 if (GIMME_V != G_VOID) 4157 if (GIMME_V != G_VOID)
3975 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 4158 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3976 else 4159 else
3977 SvREFCNT_dec (hv); 4160 SvREFCNT_dec_NN (hv);
3978} 4161}
3979 4162
3980SV * 4163SV *
3981rouse_cb () 4164rouse_cb ()
3982 PROTOTYPE: 4165 PROTOTYPE:
4237 { 4420 {
4238 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 4421 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
4239 coro_old_pp_sselect = 0; 4422 coro_old_pp_sselect = 0;
4240 } 4423 }
4241 4424
4425MODULE = Coro::State PACKAGE = Coro::Util
4426
4427void
4428_exit (int code)
4429 CODE:
4430 _exit (code);
4431
4432NV
4433time ()
4434 CODE:
4435 RETVAL = nvtime (aTHX);
4436 OUTPUT:
4437 RETVAL
4438
4439NV
4440gettimeofday ()
4441 PPCODE:
4442{
4443 UV tv [2];
4444 u2time (aTHX_ tv);
4445 EXTEND (SP, 2);
4446 PUSHs (sv_2mortal (newSVuv (tv [0])));
4447 PUSHs (sv_2mortal (newSVuv (tv [1])));
4448}
4449

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines