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.404 by root, Fri Jun 10 12:27:02 2011 UTC vs.
Revision 1.422 by root, Sat Oct 6 21:25:24 2012 UTC

33# define setjmp _setjmp /* deep magic */ 33# define setjmp _setjmp /* deep magic */
34#else 34#else
35# include <inttypes.h> /* most portable stdint.h */ 35# include <inttypes.h> /* most portable stdint.h */
36#endif 36#endif
37 37
38#ifdef HAVE_MMAP 38#if HAVE_MMAP
39# include <unistd.h> 39# include <unistd.h>
40# include <sys/mman.h> 40# include <sys/mman.h>
41# ifndef MAP_ANONYMOUS 41# ifndef MAP_ANONYMOUS
42# ifdef MAP_ANON 42# ifdef MAP_ANON
43# define MAP_ANONYMOUS MAP_ANON 43# define MAP_ANONYMOUS MAP_ANON
63#endif 63#endif
64 64
65/* the maximum number of idle cctx that will be pooled */ 65/* the maximum number of idle cctx that will be pooled */
66static int cctx_max_idle = 4; 66static int cctx_max_idle = 4;
67 67
68#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
69# define HAS_SCOPESTACK_NAME 1
70#endif
71
68#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 72#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
69# undef CORO_STACKGUARD 73# undef CORO_STACKGUARD
70#endif 74#endif
71 75
72#ifndef CORO_STACKGUARD 76#ifndef CORO_STACKGUARD
174 void *sptr; 178 void *sptr;
175 size_t ssize; 179 size_t ssize;
176 180
177 /* cpu state */ 181 /* cpu state */
178 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 182 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
183#ifndef NDEBUG
179 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 184 JMPENV *idle_te; /* same as idle_sp, but for top_env */
185#endif
180 JMPENV *top_env; 186 JMPENV *top_env;
181 coro_context cctx; 187 coro_context cctx;
182 188
183 U32 gen; 189 U32 gen;
184#if CORO_USE_VALGRIND 190#if CORO_USE_VALGRIND
204}; 210};
205 211
206/* the structure where most of the perl state is stored, overlaid on the cxstack */ 212/* the structure where most of the perl state is stored, overlaid on the cxstack */
207typedef struct 213typedef struct
208{ 214{
209 SV *defsv;
210 AV *defav;
211 SV *errsv;
212 SV *irsgv;
213 HV *hinthv;
214#define VAR(name,type) type name; 215 #define VARx(name,expr,type) type name;
215# include "state.h" 216 #include "state.h"
216#undef VAR
217} perl_slots; 217} perl_slots;
218 218
219// how many context stack entries do we need for perl_slots 219/* how many context stack entries do we need for perl_slots */
220#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 220#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
221 221
222/* this is a structure representing a perl-level coroutine */ 222/* this is a structure representing a perl-level coroutine */
223struct coro 223struct coro
224{ 224{
290static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 290static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
291static CV *cv_coro_run; 291static CV *cv_coro_run;
292static struct coro *coro_first; 292static struct coro *coro_first;
293#define coro_nready coroapi.nready 293#define coro_nready coroapi.nready
294 294
295/** JIT *********************************************************************/
296
297#if CORO_JIT
298 /* APPLE doesn't have HAVE_MMAP though */
299 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
300 #ifndef CORO_JIT_TYPE
301 #if __x86_64 && CORO_JIT_UNIXY
302 #define CORO_JIT_TYPE "amd64-unix"
303 #elif __i386 && CORO_JIT_UNIXY
304 #define CORO_JIT_TYPE "x86-unix"
305 #endif
306 #endif
307#endif
308
309#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
310 #undef CORO_JIT
311#endif
312
313#if CORO_JIT
314 typedef void (*load_save_perl_slots_type)(perl_slots *);
315 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
316#endif
317
295/** Coro::Select ************************************************************/ 318/** Coro::Select ************************************************************/
296 319
297static OP *(*coro_old_pp_sselect) (pTHX); 320static OP *(*coro_old_pp_sselect) (pTHX);
298static SV *coro_select_select; 321static SV *coro_select_select;
299 322
314 337
315/** time stuff **************************************************************/ 338/** time stuff **************************************************************/
316 339
317#ifdef HAS_GETTIMEOFDAY 340#ifdef HAS_GETTIMEOFDAY
318 341
319ECB_INLINE void 342ecb_inline void
320coro_u2time (pTHX_ UV ret[2]) 343coro_u2time (pTHX_ UV ret[2])
321{ 344{
322 struct timeval tv; 345 struct timeval tv;
323 gettimeofday (&tv, 0); 346 gettimeofday (&tv, 0);
324 347
325 ret [0] = tv.tv_sec; 348 ret [0] = tv.tv_sec;
326 ret [1] = tv.tv_usec; 349 ret [1] = tv.tv_usec;
327} 350}
328 351
329ECB_INLINE double 352ecb_inline double
330coro_nvtime () 353coro_nvtime (void)
331{ 354{
332 struct timeval tv; 355 struct timeval tv;
333 gettimeofday (&tv, 0); 356 gettimeofday (&tv, 0);
334 357
335 return tv.tv_sec + tv.tv_usec * 1e-6; 358 return tv.tv_sec + tv.tv_usec * 1e-6;
336} 359}
337 360
338ECB_INLINE void 361ecb_inline void
339time_init (pTHX) 362time_init (pTHX)
340{ 363{
341 nvtime = coro_nvtime; 364 nvtime = coro_nvtime;
342 u2time = coro_u2time; 365 u2time = coro_u2time;
343} 366}
344 367
345#else 368#else
346 369
347ECB_INLINE void 370ecb_inline void
348time_init (pTHX) 371time_init (pTHX)
349{ 372{
350 SV **svp; 373 SV **svp;
351 374
352 require_pv ("Time/HiRes.pm"); 375 require_pv ("Time/HiRes.pm");
394 get_hv (name, create); 417 get_hv (name, create);
395#endif 418#endif
396 return get_hv (name, create); 419 return get_hv (name, create);
397} 420}
398 421
399ECB_INLINE void 422ecb_inline void
400coro_times_update () 423coro_times_update (void)
401{ 424{
402#ifdef coro_clock_gettime 425#ifdef coro_clock_gettime
403 struct timespec ts; 426 struct timespec ts;
404 427
405 ts.tv_sec = ts.tv_nsec = 0; 428 ts.tv_sec = ts.tv_nsec = 0;
417 time_real [0] = tv [0]; 440 time_real [0] = tv [0];
418 time_real [1] = tv [1] * 1000; 441 time_real [1] = tv [1] * 1000;
419#endif 442#endif
420} 443}
421 444
422ECB_INLINE void 445ecb_inline void
423coro_times_add (struct coro *c) 446coro_times_add (struct coro *c)
424{ 447{
425 c->t_real [1] += time_real [1]; 448 c->t_real [1] += time_real [1];
426 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 449 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
427 c->t_real [0] += time_real [0]; 450 c->t_real [0] += time_real [0];
429 c->t_cpu [1] += time_cpu [1]; 452 c->t_cpu [1] += time_cpu [1];
430 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 453 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
431 c->t_cpu [0] += time_cpu [0]; 454 c->t_cpu [0] += time_cpu [0];
432} 455}
433 456
434ECB_INLINE void 457ecb_inline void
435coro_times_sub (struct coro *c) 458coro_times_sub (struct coro *c)
436{ 459{
437 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 460 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
438 c->t_real [1] -= time_real [1]; 461 c->t_real [1] -= time_real [1];
439 c->t_real [0] -= time_real [0]; 462 c->t_real [0] -= time_real [0];
460 : 0) 483 : 0)
461 484
462#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 485#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
463#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 486#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
464 487
465ECB_INLINE MAGIC * 488ecb_inline MAGIC *
466SvSTATEhv_p (pTHX_ SV *coro) 489SvSTATEhv_p (pTHX_ SV *coro)
467{ 490{
468 MAGIC *mg; 491 MAGIC *mg;
469 492
470 if (ecb_expect_true ( 493 if (ecb_expect_true (
475 return mg; 498 return mg;
476 499
477 return 0; 500 return 0;
478} 501}
479 502
480ECB_INLINE struct coro * 503ecb_inline struct coro *
481SvSTATE_ (pTHX_ SV *coro) 504SvSTATE_ (pTHX_ SV *coro)
482{ 505{
483 MAGIC *mg; 506 MAGIC *mg;
484 507
485 if (SvROK (coro)) 508 if (SvROK (coro))
499#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 522#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
500 523
501/*****************************************************************************/ 524/*****************************************************************************/
502/* padlist management and caching */ 525/* padlist management and caching */
503 526
504ECB_INLINE AV * 527ecb_inline AV *
505coro_derive_padlist (pTHX_ CV *cv) 528coro_derive_padlist (pTHX_ CV *cv)
506{ 529{
507 AV *padlist = CvPADLIST (cv); 530 AV *padlist = CvPADLIST (cv);
508 AV *newpadlist, *newpad; 531 AV *newpadlist, *newpad;
509 532
521 av_store (newpadlist, 1, (SV *)newpad); 544 av_store (newpadlist, 1, (SV *)newpad);
522 545
523 return newpadlist; 546 return newpadlist;
524} 547}
525 548
526ECB_INLINE void 549ecb_inline void
527free_padlist (pTHX_ AV *padlist) 550free_padlist (pTHX_ AV *padlist)
528{ 551{
529 /* may be during global destruction */ 552 /* may be during global destruction */
530 if (!IN_DESTRUCT) 553 if (!IN_DESTRUCT)
531 { 554 {
574 0, 0, 0, 0, 597 0, 0, 0, 0,
575 coro_cv_free 598 coro_cv_free
576}; 599};
577 600
578/* the next two functions merely cache the padlists */ 601/* the next two functions merely cache the padlists */
579ECB_INLINE void 602ecb_inline void
580get_padlist (pTHX_ CV *cv) 603get_padlist (pTHX_ CV *cv)
581{ 604{
582 MAGIC *mg = CORO_MAGIC_cv (cv); 605 MAGIC *mg = CORO_MAGIC_cv (cv);
583 AV *av; 606 AV *av;
584 607
597 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 620 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
598#endif 621#endif
599 } 622 }
600} 623}
601 624
602ECB_INLINE void 625ecb_inline void
603put_padlist (pTHX_ CV *cv) 626put_padlist (pTHX_ CV *cv)
604{ 627{
605 MAGIC *mg = CORO_MAGIC_cv (cv); 628 MAGIC *mg = CORO_MAGIC_cv (cv);
606 AV *av; 629 AV *av;
607 630
616 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 639 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
617} 640}
618 641
619/** load & save, init *******************************************************/ 642/** load & save, init *******************************************************/
620 643
644ecb_inline void
645swap_sv (SV *a, SV *b)
646{
647 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
648 SV tmp;
649
650 /* swap sv_any */
651 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
652
653 /* swap sv_flags */
654 SvFLAGS (&tmp) = SvFLAGS (a);
655 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
656 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
657
658#if PERL_VERSION_ATLEAST (5,10,0)
659 /* perl 5.10 and later complicates this _quite_ a bit, but it also
660 * is much faster, so no quarrels here. alternatively, we could
661 * sv_upgrade to avoid this.
662 */
663 {
664 /* swap sv_u */
665 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
666
667 /* if SvANY points to the head, we need to adjust the pointers,
668 * as the pointer for a still points to b, and maybe vice versa.
669 */
670 #define svany_in_head(type) \
671 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
672
673 if (svany_in_head (SvTYPE (a)))
674 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
675
676 if (svany_in_head (SvTYPE (b)))
677 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
678 }
679#endif
680}
681
621/* swap sv heads, at least logically */ 682/* swap sv heads, at least logically */
622static void 683static void
623swap_svs (pTHX_ Coro__State c) 684swap_svs (pTHX_ Coro__State c)
624{ 685{
625 int i; 686 int i;
626 687
627 for (i = 0; i <= AvFILLp (c->swap_sv); ) 688 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
628 { 689 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
629 SV *a = AvARRAY (c->swap_sv)[i++];
630 SV *b = AvARRAY (c->swap_sv)[i++];
631
632 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
633 SV tmp;
634
635 /* swap sv_any */
636 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
637
638 /* swap sv_flags */
639 SvFLAGS (&tmp) = SvFLAGS (a);
640 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
641 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
642
643#if PERL_VERSION_ATLEAST (5,10,0)
644 /* perl 5.10 complicates this _quite_ a bit, but it also is
645 * much faster, so no quarrels here. alternatively, we could
646 * sv_upgrade to avoid this.
647 */
648 {
649 /* swap sv_u */
650 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
651
652 /* if SvANY points to the head, we need to adjust the pointers,
653 * as the pointer for a still points to b, and maybe vice versa.
654 */
655 #define svany_in_head(type) \
656 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
657
658 if (svany_in_head (SvTYPE (a)))
659 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
660
661 if (svany_in_head (SvTYPE (b)))
662 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
663 }
664#endif
665 }
666} 690}
667 691
668#define SWAP_SVS(coro) \ 692#define SWAP_SVS(coro) \
669 if (ecb_expect_false ((coro)->swap_sv)) \ 693 if (ecb_expect_false ((coro)->swap_sv)) \
670 swap_svs (aTHX_ (coro)) 694 swap_svs (aTHX_ (coro))
678 perl_slots *slot = c->slot; 702 perl_slots *slot = c->slot;
679 c->slot = 0; 703 c->slot = 0;
680 704
681 PL_mainstack = c->mainstack; 705 PL_mainstack = c->mainstack;
682 706
683 GvSV (PL_defgv) = slot->defsv; 707#if CORO_JIT
684 GvAV (PL_defgv) = slot->defav; 708 load_perl_slots (slot);
685 GvSV (PL_errgv) = slot->errsv; 709#else
686 GvSV (irsgv) = slot->irsgv;
687 GvHV (PL_hintgv) = slot->hinthv;
688
689 #define VAR(name,type) PL_ ## name = slot->name; 710 #define VARx(name,expr,type) expr = slot->name;
690 # include "state.h" 711 #include "state.h"
691 #undef VAR 712#endif
692 713
693 { 714 {
694 dSP; 715 dSP;
695 716
696 CV *cv; 717 CV *cv;
798 819
799 PUTBACK; 820 PUTBACK;
800 } 821 }
801 822
802 /* allocate some space on the context stack for our purposes */ 823 /* allocate some space on the context stack for our purposes */
803 if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 824 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
804 { 825 {
805 unsigned int i; 826 unsigned int i;
806 827
807 for (i = 0; i < SLOT_COUNT; ++i) 828 for (i = 0; i < SLOT_COUNT; ++i)
808 CXINC; 829 CXINC;
813 c->mainstack = PL_mainstack; 834 c->mainstack = PL_mainstack;
814 835
815 { 836 {
816 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 837 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
817 838
818 slot->defav = GvAV (PL_defgv); 839#if CORO_JIT
819 slot->defsv = DEFSV; 840 save_perl_slots (slot);
820 slot->errsv = ERRSV; 841#else
821 slot->irsgv = GvSV (irsgv);
822 slot->hinthv = GvHV (PL_hintgv);
823
824 #define VAR(name,type) slot->name = PL_ ## name; 842 #define VARx(name,expr,type) slot->name = expr;
825 # include "state.h" 843 #include "state.h"
826 #undef VAR 844#endif
827 } 845 }
828} 846}
829 847
830/* 848/*
831 * allocate various perl stacks. This is almost an exact copy 849 * allocate various perl stacks. This is almost an exact copy
862#endif 880#endif
863 881
864 New(54,PL_scopestack,8,I32); 882 New(54,PL_scopestack,8,I32);
865 PL_scopestack_ix = 0; 883 PL_scopestack_ix = 0;
866 PL_scopestack_max = 8; 884 PL_scopestack_max = 8;
885#if HAS_SCOPESTACK_NAME
886 New(54,PL_scopestack_name,8,const char*);
887#endif
867 888
868 New(54,PL_savestack,24,ANY); 889 New(54,PL_savestack,24,ANY);
869 PL_savestack_ix = 0; 890 PL_savestack_ix = 0;
870 PL_savestack_max = 24; 891 PL_savestack_max = 24;
871 892
899 } 920 }
900 921
901 Safefree (PL_tmps_stack); 922 Safefree (PL_tmps_stack);
902 Safefree (PL_markstack); 923 Safefree (PL_markstack);
903 Safefree (PL_scopestack); 924 Safefree (PL_scopestack);
925#if HAS_SCOPESTACK_NAME
926 Safefree (PL_scopestack_name);
927#endif
904 Safefree (PL_savestack); 928 Safefree (PL_savestack);
905#if !PERL_VERSION_ATLEAST (5,10,0) 929#if !PERL_VERSION_ATLEAST (5,10,0)
906 Safefree (PL_retstack); 930 Safefree (PL_retstack);
907#endif 931#endif
908} 932}
971 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 995 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
972 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 996 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
973 997
974 if (svp) 998 if (svp)
975 { 999 {
976 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1000 SV *ssv;
1001
1002 if (!*svp)
1003 ssv = &PL_sv_undef;
1004 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1005 ssv = sv_2mortal (newRV_inc (*svp));
1006 else
1007 ssv = *svp;
1008
1009 sv_setsv (sv, ssv);
977 return 0; 1010 return 0;
978 } 1011 }
979 } 1012 }
980 1013
981 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1014 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1173 /* restore swapped sv's */ 1206 /* restore swapped sv's */
1174 SWAP_SVS (coro); 1207 SWAP_SVS (coro);
1175 1208
1176 coro_destruct_stacks (aTHX); 1209 coro_destruct_stacks (aTHX);
1177 1210
1178 // now save some sv's to be free'd later 1211 /* now save some sv's to be free'd later */
1179 svf [0] = GvSV (PL_defgv); 1212 svf [0] = GvSV (PL_defgv);
1180 svf [1] = (SV *)GvAV (PL_defgv); 1213 svf [1] = (SV *)GvAV (PL_defgv);
1181 svf [2] = GvSV (PL_errgv); 1214 svf [2] = GvSV (PL_errgv);
1182 svf [3] = (SV *)PL_defoutgv; 1215 svf [3] = (SV *)PL_defoutgv;
1183 svf [4] = PL_rs; 1216 svf [4] = PL_rs;
1203 SvREFCNT_dec (coro->invoke_cb); 1236 SvREFCNT_dec (coro->invoke_cb);
1204 SvREFCNT_dec (coro->invoke_av); 1237 SvREFCNT_dec (coro->invoke_av);
1205 } 1238 }
1206} 1239}
1207 1240
1208ECB_INLINE void 1241ecb_inline void
1209free_coro_mortal (pTHX) 1242free_coro_mortal (pTHX)
1210{ 1243{
1211 if (ecb_expect_true (coro_mortal)) 1244 if (ecb_expect_true (coro_mortal))
1212 { 1245 {
1213 SvREFCNT_dec ((SV *)coro_mortal); 1246 SvREFCNT_dec ((SV *)coro_mortal);
1372 slf_frame.prepare = slf_prepare_set_stacklevel; 1405 slf_frame.prepare = slf_prepare_set_stacklevel;
1373 slf_frame.check = slf_check_set_stacklevel; 1406 slf_frame.check = slf_check_set_stacklevel;
1374} 1407}
1375 1408
1376/* the tail of transfer: execute stuff we can only do after a transfer */ 1409/* the tail of transfer: execute stuff we can only do after a transfer */
1377ECB_INLINE void 1410ecb_inline void
1378transfer_tail (pTHX) 1411transfer_tail (pTHX)
1379{ 1412{
1380 free_coro_mortal (aTHX); 1413 free_coro_mortal (aTHX);
1414}
1415
1416/* try to exit the same way perl's main function would do */
1417/* we do not bother resetting the environment or other things *7
1418/* that are not, uhm, essential */
1419/* this obviously also doesn't work when perl is embedded */
1420static void ecb_noinline ecb_cold
1421perlish_exit (pTHX)
1422{
1423 int exitstatus = perl_destruct (PL_curinterp);
1424 perl_free (PL_curinterp);
1425 exit (exitstatus);
1381} 1426}
1382 1427
1383/* 1428/*
1384 * this is a _very_ stripped down perl interpreter ;) 1429 * this is a _very_ stripped down perl interpreter ;)
1385 */ 1430 */
1414 */ 1459 */
1415 1460
1416 /* 1461 /*
1417 * If perl-run returns we assume exit() was being called or the coro 1462 * If perl-run returns we assume exit() was being called or the coro
1418 * fell off the end, which seems to be the only valid (non-bug) 1463 * fell off the end, which seems to be the only valid (non-bug)
1419 * reason for perl_run to return. We try to exit by jumping to the 1464 * reason for perl_run to return. We try to mimic whatever perl is normally
1420 * bootstrap-time "top" top_env, as we cannot restore the "main" 1465 * doing in that case. YMMV.
1421 * coroutine as Coro has no such concept.
1422 * This actually isn't valid with the pthread backend, but OSes requiring
1423 * that backend are too broken to do it in a standards-compliant way.
1424 */ 1466 */
1425 PL_top_env = main_top_env; 1467 perlish_exit (aTHX);
1426 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1427 } 1468 }
1428} 1469}
1429 1470
1430static coro_cctx * 1471static coro_cctx *
1431cctx_new () 1472cctx_new (void)
1432{ 1473{
1433 coro_cctx *cctx; 1474 coro_cctx *cctx;
1434 1475
1435 ++cctx_count; 1476 ++cctx_count;
1436 New (0, cctx, 1, coro_cctx); 1477 New (0, cctx, 1, coro_cctx);
1442 return cctx; 1483 return cctx;
1443} 1484}
1444 1485
1445/* create a new cctx only suitable as source */ 1486/* create a new cctx only suitable as source */
1446static coro_cctx * 1487static coro_cctx *
1447cctx_new_empty () 1488cctx_new_empty (void)
1448{ 1489{
1449 coro_cctx *cctx = cctx_new (); 1490 coro_cctx *cctx = cctx_new ();
1450 1491
1451 cctx->sptr = 0; 1492 cctx->sptr = 0;
1452 coro_create (&cctx->cctx, 0, 0, 0, 0); 1493 coro_create (&cctx->cctx, 0, 0, 0, 0);
1454 return cctx; 1495 return cctx;
1455} 1496}
1456 1497
1457/* create a new cctx suitable as destination/running a perl interpreter */ 1498/* create a new cctx suitable as destination/running a perl interpreter */
1458static coro_cctx * 1499static coro_cctx *
1459cctx_new_run () 1500cctx_new_run (void)
1460{ 1501{
1461 coro_cctx *cctx = cctx_new (); 1502 coro_cctx *cctx = cctx_new ();
1462 void *stack_start; 1503 void *stack_start;
1463 size_t stack_size; 1504 size_t stack_size;
1464 1505
1465#if HAVE_MMAP 1506#if HAVE_MMAP
1466 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1507 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1467 /* mmap supposedly does allocate-on-write for us */ 1508 /* mmap supposedly does allocate-on-write for us */
1468 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1509 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1469 1510
1470 if (cctx->sptr != (void *)-1) 1511 if (cctx->sptr != (void *)-1)
1471 { 1512 {
1472 #if CORO_STACKGUARD 1513 #if CORO_STACKGUARD
1473 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1514 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1775 TRANSFER (ta, 1); 1816 TRANSFER (ta, 1);
1776} 1817}
1777 1818
1778/** Coro ********************************************************************/ 1819/** Coro ********************************************************************/
1779 1820
1780ECB_INLINE void 1821ecb_inline void
1781coro_enq (pTHX_ struct coro *coro) 1822coro_enq (pTHX_ struct coro *coro)
1782{ 1823{
1783 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1824 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1784 1825
1785 SvREFCNT_inc_NN (coro->hv); 1826 SvREFCNT_inc_NN (coro->hv);
1787 coro->next_ready = 0; 1828 coro->next_ready = 0;
1788 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1829 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1789 ready [1] = coro; 1830 ready [1] = coro;
1790} 1831}
1791 1832
1792ECB_INLINE struct coro * 1833ecb_inline struct coro *
1793coro_deq (pTHX) 1834coro_deq (pTHX)
1794{ 1835{
1795 int prio; 1836 int prio;
1796 1837
1797 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1838 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1850{ 1891{
1851 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1892 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1852} 1893}
1853 1894
1854/* expects to own a reference to next->hv */ 1895/* expects to own a reference to next->hv */
1855ECB_INLINE void 1896ecb_inline void
1856prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1897prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1857{ 1898{
1858 SV *prev_sv = SvRV (coro_current); 1899 SV *prev_sv = SvRV (coro_current);
1859 1900
1860 ta->prev = SvSTATE_hv (prev_sv); 1901 ta->prev = SvSTATE_hv (prev_sv);
1919 } 1960 }
1920 } 1961 }
1921 } 1962 }
1922} 1963}
1923 1964
1924ECB_INLINE void 1965ecb_inline void
1925prepare_cede (pTHX_ struct coro_transfer_args *ta) 1966prepare_cede (pTHX_ struct coro_transfer_args *ta)
1926{ 1967{
1927 api_ready (aTHX_ coro_current); 1968 api_ready (aTHX_ coro_current);
1928 prepare_schedule (aTHX_ ta); 1969 prepare_schedule (aTHX_ ta);
1929} 1970}
1930 1971
1931ECB_INLINE void 1972ecb_inline void
1932prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1973prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1933{ 1974{
1934 SV *prev = SvRV (coro_current); 1975 SV *prev = SvRV (coro_current);
1935 1976
1936 if (coro_nready) 1977 if (coro_nready)
2936 { 2977 {
2937 int i; 2978 int i;
2938 /* if we were woken up but can't down, we look through the whole */ 2979 /* if we were woken up but can't down, we look through the whole */
2939 /* waiters list and only add us if we aren't in there already */ 2980 /* waiters list and only add us if we aren't in there already */
2940 /* this avoids some degenerate memory usage cases */ 2981 /* this avoids some degenerate memory usage cases */
2941 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 2982 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2942 if (AvARRAY (av)[i] == coro_hv) 2983 if (AvARRAY (av)[i] == coro_hv)
2943 return 1; 2984 return 1;
2944 2985
2945 av_push (av, SvREFCNT_inc (coro_hv)); 2986 av_push (av, SvREFCNT_inc (coro_hv));
2946 return 1; 2987 return 1;
3323 3364
3324#ifndef __cplusplus 3365#ifndef __cplusplus
3325ecb_cold XS(boot_Coro__State); 3366ecb_cold XS(boot_Coro__State);
3326#endif 3367#endif
3327 3368
3369#if CORO_JIT
3370
3371static void ecb_noinline ecb_cold
3372pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3373{
3374 dSP;
3375 AV *av = newAV ();
3376
3377 av_store (av, 3, newSVuv (d));
3378 av_store (av, 2, newSVuv (c));
3379 av_store (av, 1, newSVuv (b));
3380 av_store (av, 0, newSVuv (a));
3381
3382 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3383
3384 PUTBACK;
3385}
3386
3387static void ecb_noinline ecb_cold
3388jit_init (pTHX)
3389{
3390 dSP;
3391 SV *load, *save;
3392 char *map_base;
3393 char *load_ptr, *save_ptr;
3394 STRLEN load_len, save_len, map_len;
3395 int count;
3396
3397 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3398
3399 PUSHMARK (SP);
3400 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3401 #include "state.h"
3402 count = call_pv ("Coro::State::_jit", G_ARRAY);
3403 SPAGAIN;
3404
3405 save = POPs; save_ptr = SvPVbyte (save, save_len);
3406 load = POPs; load_ptr = SvPVbyte (load, load_len);
3407
3408 map_len = load_len + save_len + 16;
3409
3410 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3411
3412 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3413
3414 load_perl_slots = (load_save_perl_slots_type)map_base;
3415 memcpy (map_base, load_ptr, load_len);
3416
3417 map_base += (load_len + 15) & ~15;
3418
3419 save_perl_slots = (load_save_perl_slots_type)map_base;
3420 memcpy (map_base, save_ptr, save_len);
3421
3422 /* we are good citizens and try to make the page read-only, so the evil evil */
3423 /* hackers might have it a bit more difficult */
3424 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3425
3426 PUTBACK;
3427 eval_pv ("undef &Coro::State::_jit", 1);
3428}
3429
3430#endif
3431
3328MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3432MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3329 3433
3330PROTOTYPES: DISABLE 3434PROTOTYPES: DISABLE
3331 3435
3332BOOT: 3436BOOT:
3335# if CORO_PTHREAD 3439# if CORO_PTHREAD
3336 coro_thx = PERL_GET_CONTEXT; 3440 coro_thx = PERL_GET_CONTEXT;
3337# endif 3441# endif
3338#endif 3442#endif
3339 BOOT_PAGESIZE; 3443 BOOT_PAGESIZE;
3444
3445 /* perl defines these to check for existance first, but why it doesn't */
3446 /* just create them one at init time is not clear to me, except for */
3447 /* programs trying to delete them, but... */
3448 /* anyway, we declare this as invalid and make sure they are initialised here */
3449 DEFSV;
3450 ERRSV;
3340 3451
3341 cctx_current = cctx_new_empty (); 3452 cctx_current = cctx_new_empty ();
3342 3453
3343 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3454 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3344 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3455 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3387 coroapi.prepare_cede_notself = prepare_cede_notself; 3498 coroapi.prepare_cede_notself = prepare_cede_notself;
3388 3499
3389 time_init (aTHX); 3500 time_init (aTHX);
3390 3501
3391 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3502 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3503#if CORO_JIT
3504 PUTBACK;
3505 jit_init (aTHX);
3506 SPAGAIN;
3507#endif
3392} 3508}
3393 3509
3394SV * 3510SV *
3395new (SV *klass, ...) 3511new (SV *klass, ...)
3396 ALIAS: 3512 ALIAS:
3547 RETVAL = boolSV (coro->flags & ix); 3663 RETVAL = boolSV (coro->flags & ix);
3548 OUTPUT: 3664 OUTPUT:
3549 RETVAL 3665 RETVAL
3550 3666
3551void 3667void
3552throw (Coro::State self, SV *exception = &PL_sv_undef) 3668throw (SV *self, SV *exception = &PL_sv_undef)
3553 PROTOTYPE: $;$ 3669 PROTOTYPE: $;$
3554 CODE: 3670 CODE:
3555{ 3671{
3672 struct coro *coro = SvSTATE (self);
3556 struct coro *current = SvSTATE_current; 3673 struct coro *current = SvSTATE_current;
3557 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3674 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3558 SvREFCNT_dec (*exceptionp); 3675 SvREFCNT_dec (*exceptionp);
3559 SvGETMAGIC (exception); 3676 SvGETMAGIC (exception);
3560 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3677 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3678
3679 api_ready (aTHX_ self);
3561} 3680}
3562 3681
3563void 3682void
3564api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3683api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3565 PROTOTYPE: $;$ 3684 PROTOTYPE: $;$
4046} 4165}
4047 4166
4048MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4167MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4049 4168
4050void 4169void
4051_may_delete (SV *sem, int count, int extra_refs) 4170_may_delete (SV *sem, int count, unsigned int extra_refs)
4052 PPCODE: 4171 PPCODE:
4053{ 4172{
4054 AV *av = (AV *)SvRV (sem); 4173 AV *av = (AV *)SvRV (sem);
4055 4174
4056 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4175 if (SvREFCNT ((SV *)av) == 1 + extra_refs

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines