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.200 by root, Sun Oct 7 15:08:23 2007 UTC vs.
Revision 1.220 by root, Thu Jan 10 05:43:14 2008 UTC

10#include "patchlevel.h" 10#include "patchlevel.h"
11 11
12#include <stdio.h> 12#include <stdio.h>
13#include <errno.h> 13#include <errno.h>
14#include <assert.h> 14#include <assert.h>
15#include <inttypes.h> /* portable stdint.h */
15 16
16#ifdef HAVE_MMAP 17#ifdef HAVE_MMAP
17# include <unistd.h> 18# include <unistd.h>
18# include <sys/mman.h> 19# include <sys/mman.h>
19# ifndef MAP_ANONYMOUS 20# ifndef MAP_ANONYMOUS
130#else 131#else
131# define LOCK (void)0 132# define LOCK (void)0
132# define UNLOCK (void)0 133# define UNLOCK (void)0
133#endif 134#endif
134 135
135#define strpair(const) const, sizeof (const) - 1
136
137/* helper storage struct for Coro::AIO */ 136/* helper storage struct for Coro::AIO */
138struct io_state 137struct io_state
139{ 138{
140 int errorno; 139 int errorno;
141 I32 laststype; 140 I32 laststype;
146static size_t coro_stacksize = CORO_STACKSIZE; 145static size_t coro_stacksize = CORO_STACKSIZE;
147static struct CoroAPI coroapi; 146static struct CoroAPI coroapi;
148static AV *main_mainstack; /* used to differentiate between $main and others */ 147static AV *main_mainstack; /* used to differentiate between $main and others */
149static JMPENV *main_top_env; 148static JMPENV *main_top_env;
150static HV *coro_state_stash, *coro_stash; 149static HV *coro_state_stash, *coro_stash;
151static SV *coro_mortal; /* will be freed after next transfer */ 150static volatile SV *coro_mortal; /* will be freed after next transfer */
152 151
153static GV *irsgv; /* $/ */ 152static GV *irsgv; /* $/ */
154static GV *stdoutgv; /* *STDOUT */ 153static GV *stdoutgv; /* *STDOUT */
155 154static SV *rv_diehook;
155static SV *rv_warnhook;
156static HV *hv_sig; /* %SIG */ 156static HV *hv_sig; /* %SIG */
157static SV *sv_diehook;
158static SV *sv_warnhook;
159 157
160/* async_pool helper stuff */ 158/* async_pool helper stuff */
161static SV *sv_pool_rss; 159static SV *sv_pool_rss;
162static SV *sv_pool_size; 160static SV *sv_pool_size;
163static AV *av_async_pool; 161static AV *av_async_pool;
221 219
222 /* process data */ 220 /* process data */
223 AV *mainstack; 221 AV *mainstack;
224 perl_slots *slot; /* basically the saved sp */ 222 perl_slots *slot; /* basically the saved sp */
225 223
226 /* data associated with this coroutine (initial args) */ 224 AV *args; /* data associated with this coroutine (initial args) */
227 AV *args; 225 int refcnt; /* coroutines are refcounted, yes */
228 int refcnt;
229 int flags; /* CF_ flags */ 226 int flags; /* CF_ flags */
227 HV *hv; /* the perl hash associated with this coro, if any */
230 228
231 /* statistics */ 229 /* statistics */
232 int usecount; /* number of transfers to this coro */ 230 int usecount; /* number of transfers to this coro */
233 231
234 /* coro process data */ 232 /* coro process data */
235 int prio; 233 int prio;
236 SV *throw; 234 SV *throw; /* exception to be thrown */
237 235
238 /* async_pool */ 236 /* async_pool */
239 SV *saved_deffh; 237 SV *saved_deffh;
240 238
241 /* linked list */ 239 /* linked list */
242 struct coro *next, *prev; 240 struct coro *next, *prev;
243 HV *hv; /* the perl hash associated with this coro, if any */
244}; 241};
245 242
246typedef struct coro *Coro__State; 243typedef struct coro *Coro__State;
247typedef struct coro *Coro__State_or_hashref; 244typedef struct coro *Coro__State_or_hashref;
248 245
262static struct coro *coro_first; 259static struct coro *coro_first;
263 260
264/** lowlevel stuff **********************************************************/ 261/** lowlevel stuff **********************************************************/
265 262
266static SV * 263static SV *
267coro_get_sv (const char *name, int create) 264coro_get_sv (pTHX_ const char *name, int create)
268{ 265{
269#if PERL_VERSION_ATLEAST (5,9,0) 266#if PERL_VERSION_ATLEAST (5,9,0)
270 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 267 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
271 get_sv (name, create); 268 get_sv (name, create);
272#endif 269#endif
273 return get_sv (name, create); 270 return get_sv (name, create);
274} 271}
275 272
276static AV * 273static AV *
277coro_get_av (const char *name, int create) 274coro_get_av (pTHX_ const char *name, int create)
278{ 275{
279#if PERL_VERSION_ATLEAST (5,9,0) 276#if PERL_VERSION_ATLEAST (5,9,0)
280 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 277 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
281 get_av (name, create); 278 get_av (name, create);
282#endif 279#endif
283 return get_av (name, create); 280 return get_av (name, create);
284} 281}
285 282
286static HV * 283static HV *
287coro_get_hv (const char *name, int create) 284coro_get_hv (pTHX_ const char *name, int create)
288{ 285{
289#if PERL_VERSION_ATLEAST (5,9,0) 286#if PERL_VERSION_ATLEAST (5,9,0)
290 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 287 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
291 get_hv (name, create); 288 get_hv (name, create);
292#endif 289#endif
347 344
348 /* casting is fun. */ 345 /* casting is fun. */
349 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 346 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
350 free_padlist (aTHX_ padlist); 347 free_padlist (aTHX_ padlist);
351 348
352 SvREFCNT_dec (av);
353
354 return 0; 349 return 0;
355} 350}
356 351
357#define PERL_MAGIC_coro PERL_MAGIC_ext 352#define CORO_MAGIC_type_cv PERL_MAGIC_ext
353#define CORO_MAGIC_type_state PERL_MAGIC_ext
358 354
359static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 355static MGVTBL coro_cv_vtbl = {
356 0, 0, 0, 0,
357 coro_cv_free
358};
360 359
361#define CORO_MAGIC(cv) \ 360#define CORO_MAGIC(sv,type) \
362 SvMAGIC (cv) \ 361 SvMAGIC (sv) \
363 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 362 ? SvMAGIC (sv)->mg_type == type \
364 ? SvMAGIC (cv) \ 363 ? SvMAGIC (sv) \
365 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 364 : mg_find (sv, type) \
366 : 0 365 : 0
366
367#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
368#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
367 369
368static struct coro * 370static struct coro *
369SvSTATE_ (pTHX_ SV *coro) 371SvSTATE_ (pTHX_ SV *coro)
370{ 372{
371 HV *stash; 373 HV *stash;
383 /* very slow, but rare, check */ 385 /* very slow, but rare, check */
384 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 386 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
385 croak ("Coro::State object required"); 387 croak ("Coro::State object required");
386 } 388 }
387 389
388 mg = CORO_MAGIC (coro); 390 mg = CORO_MAGIC_state (coro);
389 return (struct coro *)mg->mg_ptr; 391 return (struct coro *)mg->mg_ptr;
390} 392}
391 393
392#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 394#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
393 395
394/* the next two functions merely cache the padlists */ 396/* the next two functions merely cache the padlists */
395static void 397static void
396get_padlist (pTHX_ CV *cv) 398get_padlist (pTHX_ CV *cv)
397{ 399{
398 MAGIC *mg = CORO_MAGIC (cv); 400 MAGIC *mg = CORO_MAGIC_cv (cv);
399 AV *av; 401 AV *av;
400 402
401 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 403 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
402 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 404 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
403 else 405 else
415} 417}
416 418
417static void 419static void
418put_padlist (pTHX_ CV *cv) 420put_padlist (pTHX_ CV *cv)
419{ 421{
420 MAGIC *mg = CORO_MAGIC (cv); 422 MAGIC *mg = CORO_MAGIC_cv (cv);
421 AV *av; 423 AV *av;
422 424
423 if (expect_false (!mg)) 425 if (expect_false (!mg))
424 { 426 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
425 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
426 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
427 mg->mg_virtual = &vtbl_coro;
428 mg->mg_obj = (SV *)newAV ();
429 }
430 427
431 av = (AV *)mg->mg_obj; 428 av = (AV *)mg->mg_obj;
432 429
433 if (expect_false (AvFILLp (av) >= AvMAX (av))) 430 if (expect_false (AvFILLp (av) >= AvMAX (av)))
434 av_extend (av, AvMAX (av) + 1); 431 av_extend (av, AvMAX (av) + 1);
521 518
522 PUTBACK; 519 PUTBACK;
523 } 520 }
524 521
525 /* allocate some space on the context stack for our purposes */ 522 /* allocate some space on the context stack for our purposes */
523 /* we manually unroll here, as usually 2 slots is enough */
524 if (SLOT_COUNT >= 1) CXINC;
525 if (SLOT_COUNT >= 2) CXINC;
526 if (SLOT_COUNT >= 3) CXINC;
526 { 527 {
527 /* we manually unroll here, as usually 2 slots is enough */
528 int i; 528 int i;
529 if (SLOT_COUNT >= 1) CXINC;
530 if (SLOT_COUNT >= 2) CXINC;
531 if (SLOT_COUNT >= 3) CXINC;
532 for (i = 3; i < SLOT_COUNT; ++i) 529 for (i = 3; i < SLOT_COUNT; ++i)
533 CXINC; 530 CXINC;
534
535 cxstack_ix -= SLOT_COUNT; /* undo allocation */
536 } 531 }
532 cxstack_ix -= SLOT_COUNT; /* undo allocation */
537 533
538 c->mainstack = PL_mainstack; 534 c->mainstack = PL_mainstack;
539 535
540 { 536 {
541 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 537 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
668 return rss; 664 return rss;
669} 665}
670 666
671/** coroutine stack handling ************************************************/ 667/** coroutine stack handling ************************************************/
672 668
669static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
670static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
671
672/*
673 * This overrides the default magic get method of %SIG elements.
674 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
675 * and instead of tryign to save and restore the hash elements, we just provide
676 * readback here.
677 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
678 * not expecting this to actually change the hook. This is a potential problem
679 * when a schedule happens then, but we ignore this.
680 */
681static int
682coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
683{
684 const char *s = MgPV_nolen_const (mg);
685
686 if (*s == '_')
687 {
688 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0;
689 if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0;
690 }
691
692 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
693}
694
695static int
696coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
697{
698 const char *s = MgPV_nolen_const (mg);
699
700 if (*s == '_')
701 {
702 SV **svp = 0;
703
704 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
705 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
706
707 if (svp)
708 {
709 SV *old = *svp;
710 *svp = newSVsv (sv);
711 SvREFCNT_dec (old);
712 return;
713 }
714 }
715
716 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
717}
718
673static void 719static void
674coro_setup (pTHX_ struct coro *coro) 720coro_setup (pTHX_ struct coro *coro)
675{ 721{
676 /* 722 /*
677 * emulate part of the perl startup here. 723 * emulate part of the perl startup here.
685 PL_curpm = 0; 731 PL_curpm = 0;
686 PL_curpad = 0; 732 PL_curpad = 0;
687 PL_localizing = 0; 733 PL_localizing = 0;
688 PL_dirty = 0; 734 PL_dirty = 0;
689 PL_restartop = 0; 735 PL_restartop = 0;
690 SvREFCNT_inc (PL_diehook ); hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); 736
691 SvREFCNT_inc (PL_warnhook); hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); 737 /* recreate the die/warn hooks */
738 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
739 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
692 740
693 GvSV (PL_defgv) = newSV (0); 741 GvSV (PL_defgv) = newSV (0);
694 GvAV (PL_defgv) = coro->args; coro->args = 0; 742 GvAV (PL_defgv) = coro->args; coro->args = 0;
695 GvSV (PL_errgv) = newSV (0); 743 GvSV (PL_errgv) = newSV (0);
696 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 744 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
711 PL_op = (OP *)&myop; 759 PL_op = (OP *)&myop;
712 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 760 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
713 SPAGAIN; 761 SPAGAIN;
714 } 762 }
715 763
716 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 764 /* this newly created coroutine might be run on an existing cctx which most
765 * likely was suspended in set_stacklevel, called from entersub.
766 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
767 * so we ENTER here for symmetry
768 */
769 ENTER;
717} 770}
718 771
719static void 772static void
720coro_destroy (pTHX_ struct coro *coro) 773coro_destroy (pTHX_ struct coro *coro)
721{ 774{
805 PUSHMARK (SP); 858 PUSHMARK (SP);
806 PUSHs (&PL_sv_no); 859 PUSHs (&PL_sv_no);
807 PUSHs (fullname); 860 PUSHs (fullname);
808 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 861 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
809 PUTBACK; 862 PUTBACK;
810 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 863 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
811 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 864 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
812 SPAGAIN; 865 SPAGAIN;
813 FREETMPS; 866 FREETMPS;
814 LEAVE; 867 LEAVE;
815 PL_runops = runops_trace; 868 PL_runops = runops_trace;
844 PUSHMARK (SP); 897 PUSHMARK (SP);
845 PUSHs (&PL_sv_yes); 898 PUSHs (&PL_sv_yes);
846 PUSHs (fullname); 899 PUSHs (fullname);
847 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 900 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
848 PUTBACK; 901 PUTBACK;
849 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 902 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
850 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 903 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
851 SPAGAIN; 904 SPAGAIN;
852 FREETMPS; 905 FREETMPS;
853 LEAVE; 906 LEAVE;
854 PL_runops = runops_trace; 907 PL_runops = runops_trace;
868 PL_runops = RUNOPS_DEFAULT; 921 PL_runops = RUNOPS_DEFAULT;
869 PUSHMARK (SP); 922 PUSHMARK (SP);
870 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 923 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
871 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 924 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
872 PUTBACK; 925 PUTBACK;
873 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); 926 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
874 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 927 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
875 SPAGAIN; 928 SPAGAIN;
876 FREETMPS; 929 FREETMPS;
877 LEAVE; 930 LEAVE;
878 PL_runops = runops_trace; 931 PL_runops = runops_trace;
888 941
889/* inject a fake call to Coro::State::_cctx_init into the execution */ 942/* inject a fake call to Coro::State::_cctx_init into the execution */
890/* _cctx_init should be careful, as it could be called at almost any time */ 943/* _cctx_init should be careful, as it could be called at almost any time */
891/* during execution of a perl program */ 944/* during execution of a perl program */
892static void NOINLINE 945static void NOINLINE
893prepare_cctx (pTHX_ coro_cctx *cctx) 946cctx_prepare (pTHX_ coro_cctx *cctx)
894{ 947{
895 dSP; 948 dSP;
896 LOGOP myop; 949 LOGOP myop;
897 950
898 PL_top_env = &PL_start_env; 951 PL_top_env = &PL_start_env;
916 969
917/* 970/*
918 * this is a _very_ stripped down perl interpreter ;) 971 * this is a _very_ stripped down perl interpreter ;)
919 */ 972 */
920static void 973static void
921coro_run (void *arg) 974cctx_run (void *arg)
922{ 975{
923 dTHX; 976 dTHX;
924 977
925 /* coro_run is the alternative tail of transfer(), so unlock here. */ 978 /* cctx_run is the alternative tail of transfer(), so unlock here. */
926 UNLOCK; 979 UNLOCK;
927 980
928 /* we now skip the entersub that lead to transfer() */ 981 /* we now skip the entersub that lead to transfer() */
929 PL_op = PL_op->op_next; 982 PL_op = PL_op->op_next;
930 983
931 /* inject a fake subroutine call to cctx_init */ 984 /* inject a fake subroutine call to cctx_init */
932 prepare_cctx (aTHX_ (coro_cctx *)arg); 985 cctx_prepare (aTHX_ (coro_cctx *)arg);
933 986
934 /* somebody or something will hit me for both perl_run and PL_restartop */ 987 /* somebody or something will hit me for both perl_run and PL_restartop */
935 PL_restartop = PL_op; 988 PL_restartop = PL_op;
936 perl_run (PL_curinterp); 989 perl_run (PL_curinterp);
937 990
956 ++cctx_count; 1009 ++cctx_count;
957 1010
958 Newz (0, cctx, 1, coro_cctx); 1011 Newz (0, cctx, 1, coro_cctx);
959 1012
960#if HAVE_MMAP 1013#if HAVE_MMAP
961
962 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1014 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
963 /* mmap supposedly does allocate-on-write for us */ 1015 /* mmap supposedly does allocate-on-write for us */
964 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1016 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
965 1017
966 if (cctx->sptr != (void *)-1) 1018 if (cctx->sptr != (void *)-1)
987 stack_start = cctx->sptr; 1039 stack_start = cctx->sptr;
988 stack_size = cctx->ssize; 1040 stack_size = cctx->ssize;
989 } 1041 }
990 1042
991 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1043 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
992 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); 1044 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
993 1045
994 return cctx; 1046 return cctx;
995} 1047}
996 1048
997static void 1049static void
1085/* always use the TRANSFER macro */ 1137/* always use the TRANSFER macro */
1086static void NOINLINE 1138static void NOINLINE
1087transfer (pTHX_ struct coro *prev, struct coro *next) 1139transfer (pTHX_ struct coro *prev, struct coro *next)
1088{ 1140{
1089 dSTACKLEVEL; 1141 dSTACKLEVEL;
1142 static volatile int has_throw;
1090 1143
1091 /* sometimes transfer is only called to set idle_sp */ 1144 /* sometimes transfer is only called to set idle_sp */
1092 if (expect_false (!next)) 1145 if (expect_false (!next))
1093 { 1146 {
1094 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1147 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1109 prev->flags &= ~CF_RUNNING; 1162 prev->flags &= ~CF_RUNNING;
1110 next->flags |= CF_RUNNING; 1163 next->flags |= CF_RUNNING;
1111 1164
1112 LOCK; 1165 LOCK;
1113 1166
1167 /* first get rid of the old state */
1168 save_perl (aTHX_ prev);
1169
1114 if (expect_false (next->flags & CF_NEW)) 1170 if (expect_false (next->flags & CF_NEW))
1115 { 1171 {
1116 /* need to start coroutine */ 1172 /* need to start coroutine */
1117 next->flags &= ~CF_NEW; 1173 next->flags &= ~CF_NEW;
1118 /* first get rid of the old state */
1119 save_perl (aTHX_ prev);
1120 /* setup coroutine call */ 1174 /* setup coroutine call */
1121 coro_setup (aTHX_ next); 1175 coro_setup (aTHX_ next);
1122 } 1176 }
1123 else 1177 else
1124 {
1125 /* coroutine already started */
1126 save_perl (aTHX_ prev);
1127 load_perl (aTHX_ next); 1178 load_perl (aTHX_ next);
1128 }
1129 1179
1130 prev__cctx = prev->cctx; 1180 prev__cctx = prev->cctx;
1131 1181
1132 /* possibly "free" the cctx */ 1182 /* possibly "free" the cctx */
1133 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1183 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1138 prev->cctx = 0; 1188 prev->cctx = 0;
1139 1189
1140 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1190 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1141 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1191 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1142 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1192 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1193 if (!next->cctx)
1143 next->cctx = cctx_get (aTHX); 1194 next->cctx = cctx_get (aTHX);
1144 1195
1145 cctx_put (prev__cctx); 1196 cctx_put (prev__cctx);
1146 } 1197 }
1147 1198
1148 ++next->usecount; 1199 ++next->usecount;
1149 1200
1150 if (expect_true (!next->cctx)) 1201 if (expect_true (!next->cctx))
1151 next->cctx = cctx_get (aTHX); 1202 next->cctx = cctx_get (aTHX);
1203
1204 has_throw = !!next->throw;
1152 1205
1153 if (expect_false (prev__cctx != next->cctx)) 1206 if (expect_false (prev__cctx != next->cctx))
1154 { 1207 {
1155 prev__cctx->top_env = PL_top_env; 1208 prev__cctx->top_env = PL_top_env;
1156 PL_top_env = next->cctx->top_env; 1209 PL_top_env = next->cctx->top_env;
1158 } 1211 }
1159 1212
1160 free_coro_mortal (aTHX); 1213 free_coro_mortal (aTHX);
1161 UNLOCK; 1214 UNLOCK;
1162 1215
1163 if (expect_false (prev->throw || next->throw)) 1216 if (expect_false (has_throw))
1164 { 1217 {
1165 struct coro *coro = SvSTATE (coro_current); 1218 struct coro *coro = SvSTATE (coro_current);
1166 1219
1167 if (coro->throw) 1220 if (coro->throw)
1168 { 1221 {
1294{ 1347{
1295 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1348 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
1296} 1349}
1297 1350
1298static SV * 1351static SV *
1299coro_deq (pTHX_ int min_prio) 1352coro_deq (pTHX)
1300{ 1353{
1301 int prio = PRIO_MAX - PRIO_MIN; 1354 int prio;
1302 1355
1303 min_prio -= PRIO_MIN;
1304 if (min_prio < 0)
1305 min_prio = 0;
1306
1307 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 1356 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1308 if (AvFILLp (coro_ready [prio]) >= 0) 1357 if (AvFILLp (coro_ready [prio]) >= 0)
1309 return av_shift (coro_ready [prio]); 1358 return av_shift (coro_ready [prio]);
1310 1359
1311 return 0; 1360 return 0;
1312} 1361}
1348 SV *prev_sv, *next_sv; 1397 SV *prev_sv, *next_sv;
1349 1398
1350 for (;;) 1399 for (;;)
1351 { 1400 {
1352 LOCK; 1401 LOCK;
1353 next_sv = coro_deq (aTHX_ PRIO_MIN); 1402 next_sv = coro_deq (aTHX);
1354 1403
1355 /* nothing to schedule: call the idle handler */ 1404 /* nothing to schedule: call the idle handler */
1356 if (expect_false (!next_sv)) 1405 if (expect_false (!next_sv))
1357 { 1406 {
1358 dSP; 1407 dSP;
1362 SAVETMPS; 1411 SAVETMPS;
1363 1412
1364 PUSHMARK (SP); 1413 PUSHMARK (SP);
1365 PUTBACK; 1414 PUTBACK;
1366 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1415 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1416 SPAGAIN;
1367 1417
1368 FREETMPS; 1418 FREETMPS;
1369 LEAVE; 1419 LEAVE;
1370 continue; 1420 continue;
1371 } 1421 }
1501 BOOT_PAGESIZE; 1551 BOOT_PAGESIZE;
1502 1552
1503 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1553 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1504 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1554 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1505 1555
1556 orig_sigelem_get = PL_vtbl_sigelem.svt_get;
1557 PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1558 orig_sigelem_set = PL_vtbl_sigelem.svt_set;
1559 PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1560
1506 hv_sig = coro_get_hv ("SIG", TRUE); 1561 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1507 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1562 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1508 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1563 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1509
1510 if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
1511 if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
1512 1564
1513 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1565 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1514 1566
1515 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1567 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1516 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1568 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1522 1574
1523 while (main_top_env->je_prev) 1575 while (main_top_env->je_prev)
1524 main_top_env = main_top_env->je_prev; 1576 main_top_env = main_top_env->je_prev;
1525 1577
1526 coroapi.ver = CORO_API_VERSION; 1578 coroapi.ver = CORO_API_VERSION;
1579 coroapi.rev = CORO_API_REVISION;
1527 coroapi.transfer = api_transfer; 1580 coroapi.transfer = api_transfer;
1528 1581
1529 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1582 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1530} 1583}
1531 1584
1532SV * 1585SV *
1533new (char *klass, ...) 1586new (char *klass, ...)
1534 CODE: 1587 CODE:
1535{ 1588{
1536 struct coro *coro; 1589 struct coro *coro;
1590 MAGIC *mg;
1537 HV *hv; 1591 HV *hv;
1538 int i; 1592 int i;
1539 1593
1540 Newz (0, coro, 1, struct coro); 1594 Newz (0, coro, 1, struct coro);
1541 coro->args = newAV (); 1595 coro->args = newAV ();
1544 if (coro_first) coro_first->prev = coro; 1598 if (coro_first) coro_first->prev = coro;
1545 coro->next = coro_first; 1599 coro->next = coro_first;
1546 coro_first = coro; 1600 coro_first = coro;
1547 1601
1548 coro->hv = hv = newHV (); 1602 coro->hv = hv = newHV ();
1549 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1603 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1604 mg->mg_flags |= MGf_DUP;
1550 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1605 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1551 1606
1552 av_extend (coro->args, items - 1); 1607 av_extend (coro->args, items - 1);
1553 for (i = 1; i < items; i++) 1608 for (i = 1; i < items; i++)
1554 av_push (coro->args, newSVsv (ST (i))); 1609 av_push (coro->args, newSVsv (ST (i)));
1568 Coro::cede_notself = 4 1623 Coro::cede_notself = 4
1569 CODE: 1624 CODE:
1570{ 1625{
1571 struct transfer_args ta; 1626 struct transfer_args ta;
1572 1627
1628 PUTBACK;
1573 switch (ix) 1629 switch (ix)
1574 { 1630 {
1575 case 0: 1631 case 0:
1576 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1632 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1577 ta.next = 0; 1633 ta.next = 0;
1596 if (!prepare_cede_notself (aTHX_ &ta)) 1652 if (!prepare_cede_notself (aTHX_ &ta))
1597 XSRETURN_EMPTY; 1653 XSRETURN_EMPTY;
1598 1654
1599 break; 1655 break;
1600 } 1656 }
1657 SPAGAIN;
1601 1658
1602 BARRIER; 1659 BARRIER;
1660 PUTBACK;
1603 TRANSFER (ta); 1661 TRANSFER (ta);
1604 1662 SPAGAIN; /* might be the sp of a different coroutine now */
1605 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1663 /* be extra careful not to ever do anything after TRANSFER */
1606 XSRETURN_YES;
1607} 1664}
1608 1665
1609bool 1666bool
1610_destroy (SV *coro_sv) 1667_destroy (SV *coro_sv)
1611 CODE: 1668 CODE:
1663 { 1720 {
1664 struct coro temp; 1721 struct coro temp;
1665 1722
1666 if (!(coro->flags & CF_RUNNING)) 1723 if (!(coro->flags & CF_RUNNING))
1667 { 1724 {
1725 PUTBACK;
1668 save_perl (aTHX_ &temp); 1726 save_perl (aTHX_ &temp);
1669 load_perl (aTHX_ coro); 1727 load_perl (aTHX_ coro);
1670 } 1728 }
1671 1729
1672 { 1730 {
1673 dSP; 1731 dSP;
1674 ENTER; 1732 ENTER;
1675 SAVETMPS; 1733 SAVETMPS;
1734 PUTBACK;
1735 PUSHSTACK;
1676 PUSHMARK (SP); 1736 PUSHMARK (SP);
1677 PUTBACK;
1678 1737
1679 if (ix) 1738 if (ix)
1680 eval_sv (coderef, 0); 1739 eval_sv (coderef, 0);
1681 else 1740 else
1682 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1741 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1683 1742
1743 POPSTACK;
1684 SPAGAIN; 1744 SPAGAIN;
1685 FREETMPS; 1745 FREETMPS;
1686 LEAVE; 1746 LEAVE;
1687 PUTBACK; 1747 PUTBACK;
1688 } 1748 }
1689 1749
1690 if (!(coro->flags & CF_RUNNING)) 1750 if (!(coro->flags & CF_RUNNING))
1691 { 1751 {
1692 save_perl (aTHX_ coro); 1752 save_perl (aTHX_ coro);
1693 load_perl (aTHX_ &temp); 1753 load_perl (aTHX_ &temp);
1754 SPAGAIN;
1694 } 1755 }
1695 } 1756 }
1696} 1757}
1697 1758
1698SV * 1759SV *
1746 1807
1747BOOT: 1808BOOT:
1748{ 1809{
1749 int i; 1810 int i;
1750 1811
1751 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); 1812 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1752 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); 1813 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1753 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 1814 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1754 1815
1755 coro_current = coro_get_sv ("Coro::current", FALSE); 1816 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1756 SvREADONLY_on (coro_current); 1817 SvREADONLY_on (coro_current);
1757 1818
1758 coro_stash = gv_stashpv ("Coro", TRUE); 1819 coro_stash = gv_stashpv ("Coro", TRUE);
1759 1820
1760 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1821 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1843 CODE: 1904 CODE:
1844{ 1905{
1845 struct coro *coro = SvSTATE (coro_current); 1906 struct coro *coro = SvSTATE (coro_current);
1846 HV *hv = (HV *)SvRV (coro_current); 1907 HV *hv = (HV *)SvRV (coro_current);
1847 AV *defav = GvAV (PL_defgv); 1908 AV *defav = GvAV (PL_defgv);
1848 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); 1909 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1849 AV *invoke_av; 1910 AV *invoke_av;
1850 int i, len; 1911 int i, len;
1851 1912
1852 if (!invoke) 1913 if (!invoke)
1853 croak ("\3async_pool terminate\2\n"); 1914 croak ("\3async_pool terminate\2\n");
1854 1915
1855 SvREFCNT_dec (coro->saved_deffh); 1916 SvREFCNT_dec (coro->saved_deffh);
1856 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 1917 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1857 1918
1858 hv_store (hv, "desc", sizeof ("desc") - 1, 1919 hv_store (hv, "desc", sizeof ("desc") - 1,
1859 newSVpvn (strpair ("[async_pool]")), 0); 1920 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1860 1921
1861 invoke_av = (AV *)SvRV (invoke); 1922 invoke_av = (AV *)SvRV (invoke);
1862 len = av_len (invoke_av); 1923 len = av_len (invoke_av);
1863 1924
1864 sv_setsv (cb, AvARRAY (invoke_av)[0]); 1925 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1887 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1948 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1888 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1949 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1889 croak ("\3async_pool terminate\2\n"); 1950 croak ("\3async_pool terminate\2\n");
1890 1951
1891 av_clear (GvAV (PL_defgv)); 1952 av_clear (GvAV (PL_defgv));
1892 hv_store ((HV *)SvRV (coro_current), strpair ("desc"), 1953 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1893 newSVpvn (strpair ("[async_pool idle]")), 0); 1954 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1894 1955
1895 coro->prio = 0; 1956 coro->prio = 0;
1896 1957
1897 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 1958 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1898 api_trace (coro_current, 0); 1959 api_trace (coro_current, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines