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.432 by root, Wed May 8 00:44:44 2013 UTC vs.
Revision 1.448 by root, Thu Jun 4 22:58:28 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
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);
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 }
2202 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); 2262 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2203 } 2263 }
2204 else 2264 else
2205 { 2265 {
2206 struct coro *self = SvSTATE_current; 2266 struct coro *self = SvSTATE_current;
2267
2268 if (!self)
2269 croak ("Coro::cancel called outside of thread content,");
2207 2270
2208 /* otherwise we cancel directly, purely for speed reasons 2271 /* otherwise we cancel directly, purely for speed reasons
2209 * unfortunately, this requires some magic trickery, as 2272 * unfortunately, this requires some magic trickery, as
2210 * 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.
2211 * this is ugly, and hopefully fully worth the extra speed. 2274 * this is ugly, and hopefully fully worth the extra speed.
2553 2616
2554/* "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 */
2555static void 2618static void
2556slf_destroy (pTHX_ struct coro *coro) 2619slf_destroy (pTHX_ struct coro *coro)
2557{ 2620{
2558 /* this callback is reserved for slf functions needing to do cleanup */ 2621 struct CoroSLF frame = coro->slf_frame;
2559 if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty)
2560 coro->slf_frame.destroy (aTHX_ &coro->slf_frame);
2561 2622
2562 /* 2623 /*
2563 * The on_destroy above most likely is from an SLF call. 2624 * The on_destroy below most likely is from an SLF call.
2564 * 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
2565 * the coro, we will have to force-finish it here, otherwise 2626 * the coro, we will have to force-finish it here, otherwise
2566 * cleanup functions cannot call SLF functions. 2627 * cleanup functions cannot call SLF functions.
2567 */ 2628 */
2568 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);
2569} 2634}
2570 2635
2571/* 2636/*
2572 * these not obviously related functions are all rolled into one 2637 * these not obviously related functions are all rolled into one
2573 * 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
2913{ 2978{
2914 AV *av = (AV *)frame->data; 2979 AV *av = (AV *)frame->data;
2915 SV *count_sv = AvARRAY (av)[0]; 2980 SV *count_sv = AvARRAY (av)[0];
2916 SV *coro_hv = SvRV (coro_current); 2981 SV *coro_hv = SvRV (coro_current);
2917 2982
2983 frame->destroy = 0;
2984
2918 /* 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 */
2919 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
2920 return 0; 2991 return 0;
2992 }
2921 else if (SvIVX (count_sv) > 0) 2993 else if (SvIVX (count_sv) > 0)
2922 { 2994 {
2923 frame->destroy = 0;
2924
2925 if (acquire) 2995 if (acquire)
2926 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2996 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2927 else 2997 else
2928 coro_semaphore_adjust (aTHX_ av, 0); 2998 coro_semaphore_adjust (aTHX_ av, 0);
2929 2999
3151 /* it quickly returns */ 3221 /* it quickly returns */
3152 if (CORO_THROW) 3222 if (CORO_THROW)
3153 return 0; 3223 return 0;
3154 3224
3155 /* one element that is an RV? repeat! */ 3225 /* one element that is an RV? repeat! */
3156 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 3226 if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV)
3157 return 1; 3227 return 1;
3158 3228
3159 /* restore status */ 3229 /* restore status */
3160 { 3230 {
3161 SV *data_sv = av_pop (state); 3231 SV *data_sv = av_pop (state);
3389 3459
3390PROTOTYPES: DISABLE 3460PROTOTYPES: DISABLE
3391 3461
3392BOOT: 3462BOOT:
3393{ 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"
3394#ifdef USE_ITHREADS 3466#ifdef USE_ITHREADS
3395# if CORO_PTHREAD 3467# if CORO_PTHREAD
3396 coro_thx = PERL_GET_CONTEXT; 3468 coro_thx = PERL_GET_CONTEXT;
3397# endif 3469# endif
3398#endif 3470#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines