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.411 by root, Mon Jun 13 08:38:15 2011 UTC vs.
Revision 1.425 by root, Wed Dec 5 00:14:22 2012 UTC

12#include "perl.h" 12#include "perl.h"
13#include "XSUB.h" 13#include "XSUB.h"
14#include "perliol.h" 14#include "perliol.h"
15 15
16#include "schmorp.h" 16#include "schmorp.h"
17
18#define ECB_NO_THREADS 1
17#include "ecb.h" 19#include "ecb.h"
18 20
19#include <stddef.h> 21#include <stddef.h>
20#include <stdio.h> 22#include <stdio.h>
21#include <errno.h> 23#include <errno.h>
22#include <assert.h> 24#include <assert.h>
23 25
24#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
25# define SVs_PADSTALE 0 27# define SVs_PADSTALE 0
28#endif
29
30#ifdef PadARRAY
31# define NEWPADAPI
32# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *))
33#else
34typedef AV PADNAMELIST;
35# if !PERL_VERSION_ATLEAST(5,8,0)
36typedef AV PADLIST;
37typedef AV PAD;
38# endif
39# define PadlistARRAY(pl) ((PAD **)AvARRAY (pl))
40# define PadlistMAX(pl) AvFILLp (pl)
41# define PadlistNAMES(pl) (*PadlistARRAY (pl))
42# define PadARRAY AvARRAY
43# define PadMAX AvFILLp
44# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1))
26#endif 45#endif
27 46
28#if defined(_WIN32) 47#if defined(_WIN32)
29# undef HAS_GETTIMEOFDAY 48# undef HAS_GETTIMEOFDAY
30# undef setjmp 49# undef setjmp
63#endif 82#endif
64 83
65/* the maximum number of idle cctx that will be pooled */ 84/* the maximum number of idle cctx that will be pooled */
66static int cctx_max_idle = 4; 85static int cctx_max_idle = 4;
67 86
87#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
88# define HAS_SCOPESTACK_NAME 1
89#endif
90
68#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 91#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
69# undef CORO_STACKGUARD 92# undef CORO_STACKGUARD
70#endif 93#endif
71 94
72#ifndef CORO_STACKGUARD 95#ifndef CORO_STACKGUARD
97# if CORO_PTHREAD 120# if CORO_PTHREAD
98static void *coro_thx; 121static void *coro_thx;
99# endif 122# endif
100#endif 123#endif
101 124
102/* used in state.h */
103#define VAR(name,type) VARx(name, PL_ ## name, type)
104
105#ifdef __linux 125#ifdef __linux
106# include <time.h> /* for timespec */ 126# include <time.h> /* for timespec */
107# include <syscall.h> /* for SYS_* */ 127# include <syscall.h> /* for SYS_* */
108# ifdef SYS_clock_gettime 128# ifdef SYS_clock_gettime
109# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 129# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
177 void *sptr; 197 void *sptr;
178 size_t ssize; 198 size_t ssize;
179 199
180 /* cpu state */ 200 /* cpu state */
181 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 201 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
202#ifndef NDEBUG
182 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 203 JMPENV *idle_te; /* same as idle_sp, but for top_env */
204#endif
183 JMPENV *top_env; 205 JMPENV *top_env;
184 coro_context cctx; 206 coro_context cctx;
185 207
186 U32 gen; 208 U32 gen;
187#if CORO_USE_VALGRIND 209#if CORO_USE_VALGRIND
207}; 229};
208 230
209/* the structure where most of the perl state is stored, overlaid on the cxstack */ 231/* the structure where most of the perl state is stored, overlaid on the cxstack */
210typedef struct 232typedef struct
211{ 233{
212#define VARx(name,expr,type) type name; 234 #define VARx(name,expr,type) type name;
213# include "state.h" 235 #include "state.h"
214#undef VARx
215} perl_slots; 236} perl_slots;
216 237
217/* how many context stack entries do we need for perl_slots */ 238/* how many context stack entries do we need for perl_slots */
218#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 239#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
219 240
335 356
336/** time stuff **************************************************************/ 357/** time stuff **************************************************************/
337 358
338#ifdef HAS_GETTIMEOFDAY 359#ifdef HAS_GETTIMEOFDAY
339 360
340ECB_INLINE void 361ecb_inline void
341coro_u2time (pTHX_ UV ret[2]) 362coro_u2time (pTHX_ UV ret[2])
342{ 363{
343 struct timeval tv; 364 struct timeval tv;
344 gettimeofday (&tv, 0); 365 gettimeofday (&tv, 0);
345 366
346 ret [0] = tv.tv_sec; 367 ret [0] = tv.tv_sec;
347 ret [1] = tv.tv_usec; 368 ret [1] = tv.tv_usec;
348} 369}
349 370
350ECB_INLINE double 371ecb_inline double
351coro_nvtime (void) 372coro_nvtime (void)
352{ 373{
353 struct timeval tv; 374 struct timeval tv;
354 gettimeofday (&tv, 0); 375 gettimeofday (&tv, 0);
355 376
356 return tv.tv_sec + tv.tv_usec * 1e-6; 377 return tv.tv_sec + tv.tv_usec * 1e-6;
357} 378}
358 379
359ECB_INLINE void 380ecb_inline void
360time_init (pTHX) 381time_init (pTHX)
361{ 382{
362 nvtime = coro_nvtime; 383 nvtime = coro_nvtime;
363 u2time = coro_u2time; 384 u2time = coro_u2time;
364} 385}
365 386
366#else 387#else
367 388
368ECB_INLINE void 389ecb_inline void
369time_init (pTHX) 390time_init (pTHX)
370{ 391{
371 SV **svp; 392 SV **svp;
372 393
373 require_pv ("Time/HiRes.pm"); 394 require_pv ("Time/HiRes.pm");
374 395
375 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 396 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
376 397
377 if (!svp) croak ("Time::HiRes is required, but missing. Caught"); 398 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
378 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); 399 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
379 400
415 get_hv (name, create); 436 get_hv (name, create);
416#endif 437#endif
417 return get_hv (name, create); 438 return get_hv (name, create);
418} 439}
419 440
420ECB_INLINE void 441ecb_inline void
421coro_times_update (void) 442coro_times_update (void)
422{ 443{
423#ifdef coro_clock_gettime 444#ifdef coro_clock_gettime
424 struct timespec ts; 445 struct timespec ts;
425 446
438 time_real [0] = tv [0]; 459 time_real [0] = tv [0];
439 time_real [1] = tv [1] * 1000; 460 time_real [1] = tv [1] * 1000;
440#endif 461#endif
441} 462}
442 463
443ECB_INLINE void 464ecb_inline void
444coro_times_add (struct coro *c) 465coro_times_add (struct coro *c)
445{ 466{
446 c->t_real [1] += time_real [1]; 467 c->t_real [1] += time_real [1];
447 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 468 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
448 c->t_real [0] += time_real [0]; 469 c->t_real [0] += time_real [0];
450 c->t_cpu [1] += time_cpu [1]; 471 c->t_cpu [1] += time_cpu [1];
451 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 472 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
452 c->t_cpu [0] += time_cpu [0]; 473 c->t_cpu [0] += time_cpu [0];
453} 474}
454 475
455ECB_INLINE void 476ecb_inline void
456coro_times_sub (struct coro *c) 477coro_times_sub (struct coro *c)
457{ 478{
458 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 479 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
459 c->t_real [1] -= time_real [1]; 480 c->t_real [1] -= time_real [1];
460 c->t_real [0] -= time_real [0]; 481 c->t_real [0] -= time_real [0];
481 : 0) 502 : 0)
482 503
483#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 504#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
484#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 505#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
485 506
486ECB_INLINE MAGIC * 507ecb_inline MAGIC *
487SvSTATEhv_p (pTHX_ SV *coro) 508SvSTATEhv_p (pTHX_ SV *coro)
488{ 509{
489 MAGIC *mg; 510 MAGIC *mg;
490 511
491 if (ecb_expect_true ( 512 if (ecb_expect_true (
496 return mg; 517 return mg;
497 518
498 return 0; 519 return 0;
499} 520}
500 521
501ECB_INLINE struct coro * 522ecb_inline struct coro *
502SvSTATE_ (pTHX_ SV *coro) 523SvSTATE_ (pTHX_ SV *coro)
503{ 524{
504 MAGIC *mg; 525 MAGIC *mg;
505 526
506 if (SvROK (coro)) 527 if (SvROK (coro))
520#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 541#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
521 542
522/*****************************************************************************/ 543/*****************************************************************************/
523/* padlist management and caching */ 544/* padlist management and caching */
524 545
525ECB_INLINE AV * 546ecb_inline PADLIST *
526coro_derive_padlist (pTHX_ CV *cv) 547coro_derive_padlist (pTHX_ CV *cv)
527{ 548{
528 AV *padlist = CvPADLIST (cv); 549 PADLIST *padlist = CvPADLIST (cv);
529 AV *newpadlist, *newpad; 550 PADLIST *newpadlist;
551 PAD *newpad;
552 PADOFFSET const off = PadlistMAX (padlist) + 1;
530 553
531 newpadlist = newAV (); 554 newPADLIST(newpadlist);
555#if !PERL_VERSION_ATLEAST(5,15,3)
556 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
532 AvREAL_off (newpadlist); 557 AvREAL_off (newpadlist);
558#endif
533#if PERL_VERSION_ATLEAST (5,10,0) 559#if PERL_VERSION_ATLEAST (5,10,0)
534 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 560 Perl_pad_push (aTHX_ padlist, off);
535#else 561#else
536 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 562 Perl_pad_push (aTHX_ padlist, off, 1);
537#endif 563#endif
538 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 564 newpad = PadlistARRAY (padlist)[off];
539 --AvFILLp (padlist); 565 PadlistMAX (padlist) = off - 1;
540 566
541 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); 567 /* Already extended to 2 elements by newPADLIST. */
542 av_store (newpadlist, 1, (SV *)newpad); 568 PadlistMAX (newpadlist) = 1;
569 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
570 PadlistARRAY (newpadlist)[1] = newpad;
543 571
544 return newpadlist; 572 return newpadlist;
545} 573}
546 574
547ECB_INLINE void 575ecb_inline void
548free_padlist (pTHX_ AV *padlist) 576free_padlist (pTHX_ PADLIST *padlist)
549{ 577{
550 /* may be during global destruction */ 578 /* may be during global destruction */
551 if (!IN_DESTRUCT) 579 if (!IN_DESTRUCT)
552 { 580 {
553 I32 i = AvFILLp (padlist); 581 I32 i = PadlistMAX (padlist);
554 582
555 while (i > 0) /* special-case index 0 */ 583 while (i > 0) /* special-case index 0 */
556 { 584 {
557 /* we try to be extra-careful here */ 585 /* we try to be extra-careful here */
558 AV *av = (AV *)AvARRAY (padlist)[i--]; 586 PAD *pad = PadlistARRAY (padlist)[i--];
559 I32 j = AvFILLp (av); 587 I32 j = PadMAX (pad);
560 588
561 while (j >= 0) 589 while (j >= 0)
562 SvREFCNT_dec (AvARRAY (av)[j--]); 590 SvREFCNT_dec (PadARRAY (pad)[j--]);
563 591
564 AvFILLp (av) = -1; 592 PadMAX (pad) = -1;
565 SvREFCNT_dec (av); 593 SvREFCNT_dec (pad);
566 } 594 }
567 595
568 SvREFCNT_dec (AvARRAY (padlist)[0]); 596 SvREFCNT_dec (PadlistNAMES (padlist));
569 597
598#ifdef NEWPADAPI
599 Safefree (PadlistARRAY (padlist));
600 Safefree (padlist);
601#else
570 AvFILLp (padlist) = -1; 602 AvFILLp (padlist) = -1;
603 AvREAL_off (padlist);
571 SvREFCNT_dec ((SV*)padlist); 604 SvREFCNT_dec ((SV*)padlist);
605#endif
572 } 606 }
573} 607}
574 608
575static int 609static int
576coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 610coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
577{ 611{
578 AV *padlist; 612 PADLIST *padlist;
579 AV *av = (AV *)mg->mg_obj; 613 PADLIST **padlists = (PADLIST **)(mg->mg_ptr + sizeof(size_t));
614 size_t len = *(size_t *)mg->mg_ptr;
580 615
581 /* perl manages to free our internal AV and _then_ call us */ 616 /* perl manages to free our internal AV and _then_ call us */
582 if (IN_DESTRUCT) 617 if (IN_DESTRUCT)
583 return 0; 618 return 0;
584 619
585 /* casting is fun. */ 620 while (len--)
586 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
587 free_padlist (aTHX_ padlist); 621 free_padlist (aTHX_ padlists[len]);
588
589 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
590 622
591 return 0; 623 return 0;
592} 624}
593 625
594static MGVTBL coro_cv_vtbl = { 626static MGVTBL coro_cv_vtbl = {
595 0, 0, 0, 0, 627 0, 0, 0, 0,
596 coro_cv_free 628 coro_cv_free
597}; 629};
598 630
599/* the next two functions merely cache the padlists */ 631/* the next two functions merely cache the padlists */
600ECB_INLINE void 632ecb_inline void
601get_padlist (pTHX_ CV *cv) 633get_padlist (pTHX_ CV *cv)
602{ 634{
603 MAGIC *mg = CORO_MAGIC_cv (cv); 635 MAGIC *mg = CORO_MAGIC_cv (cv);
604 AV *av; 636 size_t *lenp;
605 637
606 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 638 if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr)))
607 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 639 CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp];
608 else 640 else
609 { 641 {
610#if CORO_PREFER_PERL_FUNCTIONS 642#if CORO_PREFER_PERL_FUNCTIONS
611 /* this is probably cleaner? but also slower! */ 643 /* this is probably cleaner? but also slower! */
612 /* in practise, it seems to be less stable */ 644 /* in practise, it seems to be less stable */
618 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 650 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
619#endif 651#endif
620 } 652 }
621} 653}
622 654
623ECB_INLINE void 655ecb_inline void
624put_padlist (pTHX_ CV *cv) 656put_padlist (pTHX_ CV *cv)
625{ 657{
626 MAGIC *mg = CORO_MAGIC_cv (cv); 658 MAGIC *mg = CORO_MAGIC_cv (cv);
627 AV *av;
628 659
629 if (ecb_expect_false (!mg)) 660 if (ecb_expect_false (!mg))
661 {
630 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 662 mg = sv_magicext ((SV *)cv, 0, CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
663 Newz (0, mg->mg_ptr ,sizeof (size_t) + sizeof (PADLIST *), char);
664 mg->mg_len = 1; /* so mg_free frees mg_ptr */
665 }
666 else
667 Renew (mg->mg_ptr,
668 sizeof(size_t) + (*(size_t *)mg->mg_ptr + 1) * sizeof(PADLIST *),
669 char);
631 670
632 av = (AV *)mg->mg_obj; 671 ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv);
633
634 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
635 av_extend (av, AvFILLp (av) + 1);
636
637 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
638} 672}
639 673
640/** load & save, init *******************************************************/ 674/** load & save, init *******************************************************/
641 675
676ecb_inline void
677swap_sv (SV *a, SV *b)
678{
679 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
680 SV tmp;
681
682 /* swap sv_any */
683 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
684
685 /* swap sv_flags */
686 SvFLAGS (&tmp) = SvFLAGS (a);
687 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
688 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
689
690#if PERL_VERSION_ATLEAST (5,10,0)
691 /* perl 5.10 and later complicates this _quite_ a bit, but it also
692 * is much faster, so no quarrels here. alternatively, we could
693 * sv_upgrade to avoid this.
694 */
695 {
696 /* swap sv_u */
697 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
698
699 /* if SvANY points to the head, we need to adjust the pointers,
700 * as the pointer for a still points to b, and maybe vice versa.
701 */
702 #define svany_in_head(type) \
703 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
704
705 if (svany_in_head (SvTYPE (a)))
706 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
707
708 if (svany_in_head (SvTYPE (b)))
709 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
710 }
711#endif
712}
713
642/* swap sv heads, at least logically */ 714/* swap sv heads, at least logically */
643static void 715static void
644swap_svs (pTHX_ Coro__State c) 716swap_svs (pTHX_ Coro__State c)
645{ 717{
646 int i; 718 int i;
647 719
648 for (i = 0; i <= AvFILLp (c->swap_sv); ) 720 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
649 { 721 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
650 SV *a = AvARRAY (c->swap_sv)[i++];
651 SV *b = AvARRAY (c->swap_sv)[i++];
652
653 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
654 SV tmp;
655
656 /* swap sv_any */
657 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
658
659 /* swap sv_flags */
660 SvFLAGS (&tmp) = SvFLAGS (a);
661 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
662 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
663
664#if PERL_VERSION_ATLEAST (5,10,0)
665 /* perl 5.10 complicates this _quite_ a bit, but it also is
666 * much faster, so no quarrels here. alternatively, we could
667 * sv_upgrade to avoid this.
668 */
669 {
670 /* swap sv_u */
671 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
672
673 /* if SvANY points to the head, we need to adjust the pointers,
674 * as the pointer for a still points to b, and maybe vice versa.
675 */
676 #define svany_in_head(type) \
677 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
678
679 if (svany_in_head (SvTYPE (a)))
680 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
681
682 if (svany_in_head (SvTYPE (b)))
683 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
684 }
685#endif
686 }
687} 722}
688 723
689#define SWAP_SVS(coro) \ 724#define SWAP_SVS(coro) \
690 if (ecb_expect_false ((coro)->swap_sv)) \ 725 if (ecb_expect_false ((coro)->swap_sv)) \
691 swap_svs (aTHX_ (coro)) 726 swap_svs (aTHX_ (coro))
703 738
704#if CORO_JIT 739#if CORO_JIT
705 load_perl_slots (slot); 740 load_perl_slots (slot);
706#else 741#else
707 #define VARx(name,expr,type) expr = slot->name; 742 #define VARx(name,expr,type) expr = slot->name;
708 # include "state.h" 743 #include "state.h"
709 #undef VARx
710#endif 744#endif
711 745
712 { 746 {
713 dSP; 747 dSP;
714 748
717 /* now do the ugly restore mess */ 751 /* now do the ugly restore mess */
718 while (ecb_expect_true (cv = (CV *)POPs)) 752 while (ecb_expect_true (cv = (CV *)POPs))
719 { 753 {
720 put_padlist (aTHX_ cv); /* mark this padlist as available */ 754 put_padlist (aTHX_ cv); /* mark this padlist as available */
721 CvDEPTH (cv) = PTR2IV (POPs); 755 CvDEPTH (cv) = PTR2IV (POPs);
722 CvPADLIST (cv) = (AV *)POPs; 756 CvPADLIST (cv) = (PADLIST *)POPs;
723 } 757 }
724 758
725 PUTBACK; 759 PUTBACK;
726 } 760 }
727 761
836 870
837#if CORO_JIT 871#if CORO_JIT
838 save_perl_slots (slot); 872 save_perl_slots (slot);
839#else 873#else
840 #define VARx(name,expr,type) slot->name = expr; 874 #define VARx(name,expr,type) slot->name = expr;
841 # include "state.h" 875 #include "state.h"
842 #undef VARx
843#endif 876#endif
844 } 877 }
845} 878}
846 879
847/* 880/*
879#endif 912#endif
880 913
881 New(54,PL_scopestack,8,I32); 914 New(54,PL_scopestack,8,I32);
882 PL_scopestack_ix = 0; 915 PL_scopestack_ix = 0;
883 PL_scopestack_max = 8; 916 PL_scopestack_max = 8;
917#if HAS_SCOPESTACK_NAME
918 New(54,PL_scopestack_name,8,const char*);
919#endif
884 920
885 New(54,PL_savestack,24,ANY); 921 New(54,PL_savestack,24,ANY);
886 PL_savestack_ix = 0; 922 PL_savestack_ix = 0;
887 PL_savestack_max = 24; 923 PL_savestack_max = 24;
888 924
916 } 952 }
917 953
918 Safefree (PL_tmps_stack); 954 Safefree (PL_tmps_stack);
919 Safefree (PL_markstack); 955 Safefree (PL_markstack);
920 Safefree (PL_scopestack); 956 Safefree (PL_scopestack);
957#if HAS_SCOPESTACK_NAME
958 Safefree (PL_scopestack_name);
959#endif
921 Safefree (PL_savestack); 960 Safefree (PL_savestack);
922#if !PERL_VERSION_ATLEAST (5,10,0) 961#if !PERL_VERSION_ATLEAST (5,10,0)
923 Safefree (PL_retstack); 962 Safefree (PL_retstack);
924#endif 963#endif
925} 964}
985 { 1024 {
986 SV **svp = 0; 1025 SV **svp = 0;
987 1026
988 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1027 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
989 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1028 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
990 1029
991 if (svp) 1030 if (svp)
992 { 1031 {
993 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1032 SV *ssv;
1033
1034 if (!*svp)
1035 ssv = &PL_sv_undef;
1036 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1037 ssv = sv_2mortal (newRV_inc (*svp));
1038 else
1039 ssv = *svp;
1040
1041 sv_setsv (sv, ssv);
994 return 0; 1042 return 0;
995 } 1043 }
996 } 1044 }
997 1045
998 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1046 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1092 PL_hints = 0; 1140 PL_hints = 0;
1093 1141
1094 /* recreate the die/warn hooks */ 1142 /* recreate the die/warn hooks */
1095 PL_diehook = SvREFCNT_inc (rv_diehook); 1143 PL_diehook = SvREFCNT_inc (rv_diehook);
1096 PL_warnhook = SvREFCNT_inc (rv_warnhook); 1144 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1097 1145
1098 GvSV (PL_defgv) = newSV (0); 1146 GvSV (PL_defgv) = newSV (0);
1099 GvAV (PL_defgv) = coro->args; coro->args = 0; 1147 GvAV (PL_defgv) = coro->args; coro->args = 0;
1100 GvSV (PL_errgv) = newSV (0); 1148 GvSV (PL_errgv) = newSV (0);
1101 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1149 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1102 GvHV (PL_hintgv) = 0; 1150 GvHV (PL_hintgv) = 0;
1220 SvREFCNT_dec (coro->invoke_cb); 1268 SvREFCNT_dec (coro->invoke_cb);
1221 SvREFCNT_dec (coro->invoke_av); 1269 SvREFCNT_dec (coro->invoke_av);
1222 } 1270 }
1223} 1271}
1224 1272
1225ECB_INLINE void 1273ecb_inline void
1226free_coro_mortal (pTHX) 1274free_coro_mortal (pTHX)
1227{ 1275{
1228 if (ecb_expect_true (coro_mortal)) 1276 if (ecb_expect_true (coro_mortal))
1229 { 1277 {
1230 SvREFCNT_dec ((SV *)coro_mortal); 1278 SvREFCNT_dec ((SV *)coro_mortal);
1389 slf_frame.prepare = slf_prepare_set_stacklevel; 1437 slf_frame.prepare = slf_prepare_set_stacklevel;
1390 slf_frame.check = slf_check_set_stacklevel; 1438 slf_frame.check = slf_check_set_stacklevel;
1391} 1439}
1392 1440
1393/* the tail of transfer: execute stuff we can only do after a transfer */ 1441/* the tail of transfer: execute stuff we can only do after a transfer */
1394ECB_INLINE void 1442ecb_inline void
1395transfer_tail (pTHX) 1443transfer_tail (pTHX)
1396{ 1444{
1397 free_coro_mortal (aTHX); 1445 free_coro_mortal (aTHX);
1446}
1447
1448/* try to exit the same way perl's main function would do */
1449/* we do not bother resetting the environment or other things *7
1450/* that are not, uhm, essential */
1451/* this obviously also doesn't work when perl is embedded */
1452static void ecb_noinline ecb_cold
1453perlish_exit (pTHX)
1454{
1455 int exitstatus = perl_destruct (PL_curinterp);
1456 perl_free (PL_curinterp);
1457 exit (exitstatus);
1398} 1458}
1399 1459
1400/* 1460/*
1401 * this is a _very_ stripped down perl interpreter ;) 1461 * this is a _very_ stripped down perl interpreter ;)
1402 */ 1462 */
1431 */ 1491 */
1432 1492
1433 /* 1493 /*
1434 * If perl-run returns we assume exit() was being called or the coro 1494 * If perl-run returns we assume exit() was being called or the coro
1435 * fell off the end, which seems to be the only valid (non-bug) 1495 * fell off the end, which seems to be the only valid (non-bug)
1436 * reason for perl_run to return. We try to exit by jumping to the 1496 * reason for perl_run to return. We try to mimic whatever perl is normally
1437 * bootstrap-time "top" top_env, as we cannot restore the "main" 1497 * doing in that case. YMMV.
1438 * coroutine as Coro has no such concept.
1439 * This actually isn't valid with the pthread backend, but OSes requiring
1440 * that backend are too broken to do it in a standards-compliant way.
1441 */ 1498 */
1442 PL_top_env = main_top_env; 1499 perlish_exit (aTHX);
1443 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1444 } 1500 }
1445} 1501}
1446 1502
1447static coro_cctx * 1503static coro_cctx *
1448cctx_new (void) 1504cctx_new (void)
1480 size_t stack_size; 1536 size_t stack_size;
1481 1537
1482#if HAVE_MMAP 1538#if HAVE_MMAP
1483 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1539 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1484 /* mmap supposedly does allocate-on-write for us */ 1540 /* mmap supposedly does allocate-on-write for us */
1485 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); 1541 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1486 1542
1487 if (cctx->sptr != (void *)-1) 1543 if (cctx->sptr != (void *)-1)
1488 { 1544 {
1489 #if CORO_STACKGUARD 1545 #if CORO_STACKGUARD
1490 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1546 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1705 return; 1761 return;
1706 1762
1707 slf_destroy (aTHX_ coro); 1763 slf_destroy (aTHX_ coro);
1708 1764
1709 coro->flags |= CF_ZOMBIE; 1765 coro->flags |= CF_ZOMBIE;
1710 1766
1711 if (coro->flags & CF_READY) 1767 if (coro->flags & CF_READY)
1712 { 1768 {
1713 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1769 /* reduce nready, as destroying a ready coro effectively unreadies it */
1714 /* alternative: look through all ready queues and remove the coro */ 1770 /* alternative: look through all ready queues and remove the coro */
1715 --coro_nready; 1771 --coro_nready;
1792 TRANSFER (ta, 1); 1848 TRANSFER (ta, 1);
1793} 1849}
1794 1850
1795/** Coro ********************************************************************/ 1851/** Coro ********************************************************************/
1796 1852
1797ECB_INLINE void 1853ecb_inline void
1798coro_enq (pTHX_ struct coro *coro) 1854coro_enq (pTHX_ struct coro *coro)
1799{ 1855{
1800 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1856 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1801 1857
1802 SvREFCNT_inc_NN (coro->hv); 1858 SvREFCNT_inc_NN (coro->hv);
1804 coro->next_ready = 0; 1860 coro->next_ready = 0;
1805 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1861 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1806 ready [1] = coro; 1862 ready [1] = coro;
1807} 1863}
1808 1864
1809ECB_INLINE struct coro * 1865ecb_inline struct coro *
1810coro_deq (pTHX) 1866coro_deq (pTHX)
1811{ 1867{
1812 int prio; 1868 int prio;
1813 1869
1814 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1870 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1867{ 1923{
1868 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1924 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1869} 1925}
1870 1926
1871/* expects to own a reference to next->hv */ 1927/* expects to own a reference to next->hv */
1872ECB_INLINE void 1928ecb_inline void
1873prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1929prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1874{ 1930{
1875 SV *prev_sv = SvRV (coro_current); 1931 SV *prev_sv = SvRV (coro_current);
1876 1932
1877 ta->prev = SvSTATE_hv (prev_sv); 1933 ta->prev = SvSTATE_hv (prev_sv);
1936 } 1992 }
1937 } 1993 }
1938 } 1994 }
1939} 1995}
1940 1996
1941ECB_INLINE void 1997ecb_inline void
1942prepare_cede (pTHX_ struct coro_transfer_args *ta) 1998prepare_cede (pTHX_ struct coro_transfer_args *ta)
1943{ 1999{
1944 api_ready (aTHX_ coro_current); 2000 api_ready (aTHX_ coro_current);
1945 prepare_schedule (aTHX_ ta); 2001 prepare_schedule (aTHX_ ta);
1946} 2002}
1947 2003
1948ECB_INLINE void 2004ecb_inline void
1949prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 2005prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1950{ 2006{
1951 SV *prev = SvRV (coro_current); 2007 SV *prev = SvRV (coro_current);
1952 2008
1953 if (coro_nready) 2009 if (coro_nready)
2148 2204
2149static void 2205static void
2150coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) 2206coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2151{ 2207{
2152 AV *av; 2208 AV *av;
2153 2209
2154 if (coro->status) 2210 if (coro->status)
2155 { 2211 {
2156 av = coro->status; 2212 av = coro->status;
2157 av_clear (av); 2213 av_clear (av);
2158 } 2214 }
2205 2261
2206 coro = SvSTATE (arg [0]); 2262 coro = SvSTATE (arg [0]);
2207 coro_hv = coro->hv; 2263 coro_hv = coro->hv;
2208 2264
2209 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2265 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2210 2266
2211 if (ecb_expect_false (coro->flags & CF_NOCANCEL)) 2267 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2212 { 2268 {
2213 /* coro currently busy cancelling something, so just notify it */ 2269 /* coro currently busy cancelling something, so just notify it */
2214 coro->slf_frame.data = (void *)coro; 2270 coro->slf_frame.data = (void *)coro;
2215 2271
2394 2450
2395static int 2451static int
2396slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2452slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2397{ 2453{
2398 SV *data = (SV *)frame->data; 2454 SV *data = (SV *)frame->data;
2399 2455
2400 if (CORO_THROW) 2456 if (CORO_THROW)
2401 return 0; 2457 return 0;
2402 2458
2403 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2459 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2404 return 1; 2460 return 1;
2440 cb = sv_2mortal (coro->rouse_cb); 2496 cb = sv_2mortal (coro->rouse_cb);
2441 coro->rouse_cb = 0; 2497 coro->rouse_cb = 0;
2442 } 2498 }
2443 2499
2444 if (!SvROK (cb) 2500 if (!SvROK (cb)
2445 || SvTYPE (SvRV (cb)) != SVt_PVCV 2501 || SvTYPE (SvRV (cb)) != SVt_PVCV
2446 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2502 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2447 croak ("Coro::rouse_wait called with illegal callback argument,"); 2503 croak ("Coro::rouse_wait called with illegal callback argument,");
2448 2504
2449 { 2505 {
2450 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2506 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
3371 int count; 3427 int count;
3372 3428
3373 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); 3429 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3374 3430
3375 PUSHMARK (SP); 3431 PUSHMARK (SP);
3376#define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); 3432 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3377# include "state.h" 3433 #include "state.h"
3378#undef VARx
3379 count = call_pv ("Coro::State::_jit", G_ARRAY); 3434 count = call_pv ("Coro::State::_jit", G_ARRAY);
3380 SPAGAIN; 3435 SPAGAIN;
3381 3436
3382 save = POPs; save_ptr = SvPVbyte (save, save_len); 3437 save = POPs; save_ptr = SvPVbyte (save, save_len);
3383 load = POPs; load_ptr = SvPVbyte (load, load_len); 3438 load = POPs; load_ptr = SvPVbyte (load, load_len);
3640 RETVAL = boolSV (coro->flags & ix); 3695 RETVAL = boolSV (coro->flags & ix);
3641 OUTPUT: 3696 OUTPUT:
3642 RETVAL 3697 RETVAL
3643 3698
3644void 3699void
3645throw (Coro::State self, SV *exception = &PL_sv_undef) 3700throw (SV *self, SV *exception = &PL_sv_undef)
3646 PROTOTYPE: $;$ 3701 PROTOTYPE: $;$
3647 CODE: 3702 CODE:
3648{ 3703{
3704 struct coro *coro = SvSTATE (self);
3649 struct coro *current = SvSTATE_current; 3705 struct coro *current = SvSTATE_current;
3650 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3706 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3651 SvREFCNT_dec (*exceptionp); 3707 SvREFCNT_dec (*exceptionp);
3652 SvGETMAGIC (exception); 3708 SvGETMAGIC (exception);
3653 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3709 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3710
3711 api_ready (aTHX_ self);
3654} 3712}
3655 3713
3656void 3714void
3657api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3715api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3658 PROTOTYPE: $;$ 3716 PROTOTYPE: $;$
3788 3846
3789 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3847 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3790 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3848 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3791 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3849 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3792 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ 3850 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3793 3851
3794 coro_stash = gv_stashpv ("Coro", TRUE); 3852 coro_stash = gv_stashpv ("Coro", TRUE);
3795 3853
3796 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3854 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3797 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3855 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3798 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3856 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
4139} 4197}
4140 4198
4141MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4199MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4142 4200
4143void 4201void
4144_may_delete (SV *sem, int count, int extra_refs) 4202_may_delete (SV *sem, int count, unsigned int extra_refs)
4145 PPCODE: 4203 PPCODE:
4146{ 4204{
4147 AV *av = (AV *)SvRV (sem); 4205 AV *av = (AV *)SvRV (sem);
4148 4206
4149 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4207 if (SvREFCNT ((SV *)av) == 1 + extra_refs

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines