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.424 by root, Fri Nov 30 19:27:28 2012 UTC vs.
Revision 1.428 by root, Wed Dec 5 16:34:12 2012 UTC

23#include <errno.h> 23#include <errno.h>
24#include <assert.h> 24#include <assert.h>
25 25
26#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
27# 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))
28#endif 45#endif
29 46
30#if defined(_WIN32) 47#if defined(_WIN32)
31# undef HAS_GETTIMEOFDAY 48# undef HAS_GETTIMEOFDAY
32# undef setjmp 49# undef setjmp
524#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 541#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
525 542
526/*****************************************************************************/ 543/*****************************************************************************/
527/* padlist management and caching */ 544/* padlist management and caching */
528 545
529ecb_inline AV * 546ecb_inline PADLIST *
530coro_derive_padlist (pTHX_ CV *cv) 547coro_derive_padlist (pTHX_ CV *cv)
531{ 548{
532 AV *padlist = CvPADLIST (cv); 549 PADLIST *padlist = CvPADLIST (cv);
533 AV *newpadlist, *newpad; 550 PADLIST *newpadlist;
551 PAD *newpad;
552 PADOFFSET const off = PadlistMAX (padlist) + 1;
534 553
535 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. */
536 AvREAL_off (newpadlist); 557 AvREAL_off (newpadlist);
558#endif
537#if PERL_VERSION_ATLEAST (5,10,0) 559#if PERL_VERSION_ATLEAST (5,10,0)
538 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 560 Perl_pad_push (aTHX_ padlist, off);
539#else 561#else
540 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 562 Perl_pad_push (aTHX_ padlist, off, 1);
541#endif 563#endif
542 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 564 newpad = PadlistARRAY (padlist)[off];
543 --AvFILLp (padlist); 565 PadlistMAX (padlist) = off - 1;
544 566
545 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); 567 /* Already extended to 2 elements by newPADLIST. */
546 av_store (newpadlist, 1, (SV *)newpad); 568 PadlistMAX (newpadlist) = 1;
569 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
570 PadlistARRAY (newpadlist)[1] = newpad;
547 571
548 return newpadlist; 572 return newpadlist;
549} 573}
550 574
551ecb_inline void 575ecb_inline void
552free_padlist (pTHX_ AV *padlist) 576free_padlist (pTHX_ PADLIST *padlist)
553{ 577{
554 /* may be during global destruction */ 578 /* may be during global destruction */
555 if (!IN_DESTRUCT) 579 if (!IN_DESTRUCT)
556 { 580 {
557 I32 i = AvFILLp (padlist); 581 I32 i = PadlistMAX (padlist);
558 582
559 while (i > 0) /* special-case index 0 */ 583 while (i > 0) /* special-case index 0 */
560 { 584 {
561 /* we try to be extra-careful here */ 585 /* we try to be extra-careful here */
562 AV *av = (AV *)AvARRAY (padlist)[i--]; 586 PAD *pad = PadlistARRAY (padlist)[i--];
563 I32 j = AvFILLp (av); 587 I32 j = PadMAX (pad);
564 588
565 while (j >= 0) 589 while (j >= 0)
566 SvREFCNT_dec (AvARRAY (av)[j--]); 590 SvREFCNT_dec (PadARRAY (pad)[j--]);
567 591
568 AvFILLp (av) = -1; 592 PadMAX (pad) = -1;
569 SvREFCNT_dec (av); 593 SvREFCNT_dec (pad);
570 } 594 }
571 595
572 SvREFCNT_dec (AvARRAY (padlist)[0]); 596 SvREFCNT_dec (PadlistNAMES (padlist));
573 597
598#ifdef NEWPADAPI
599 Safefree (PadlistARRAY (padlist));
600 Safefree (padlist);
601#else
574 AvFILLp (padlist) = -1; 602 AvFILLp (padlist) = -1;
603 AvREAL_off (padlist);
575 SvREFCNT_dec ((SV*)padlist); 604 SvREFCNT_dec ((SV*)padlist);
605#endif
576 } 606 }
577} 607}
578 608
579static int 609static int
580coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 610coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
581{ 611{
582 AV *padlist; 612 PADLIST *padlist;
583 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;
584 615
585 /* perl manages to free our internal AV and _then_ call us */ 616 /* perl manages to free our internal AV and _then_ call us */
586 if (IN_DESTRUCT) 617 if (IN_DESTRUCT)
587 return 0; 618 return 0;
588 619
589 /* casting is fun. */ 620 while (len--)
590 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
591 free_padlist (aTHX_ padlist); 621 free_padlist (aTHX_ padlists[len]);
592
593 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
594 622
595 return 0; 623 return 0;
596} 624}
597 625
598static MGVTBL coro_cv_vtbl = { 626static MGVTBL coro_cv_vtbl = {
603/* the next two functions merely cache the padlists */ 631/* the next two functions merely cache the padlists */
604ecb_inline void 632ecb_inline void
605get_padlist (pTHX_ CV *cv) 633get_padlist (pTHX_ CV *cv)
606{ 634{
607 MAGIC *mg = CORO_MAGIC_cv (cv); 635 MAGIC *mg = CORO_MAGIC_cv (cv);
608 AV *av; 636 size_t *lenp;
609 637
610 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 638 if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr)))
611 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 639 CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp];
612 else 640 else
613 { 641 {
614#if CORO_PREFER_PERL_FUNCTIONS 642#if CORO_PREFER_PERL_FUNCTIONS
615 /* this is probably cleaner? but also slower! */ 643 /* this is probably cleaner? but also slower! */
616 /* in practise, it seems to be less stable */ 644 /* in practise, it seems to be less stable */
626 654
627ecb_inline void 655ecb_inline void
628put_padlist (pTHX_ CV *cv) 656put_padlist (pTHX_ CV *cv)
629{ 657{
630 MAGIC *mg = CORO_MAGIC_cv (cv); 658 MAGIC *mg = CORO_MAGIC_cv (cv);
631 AV *av;
632 659
633 if (ecb_expect_false (!mg)) 660 if (ecb_expect_false (!mg))
661 {
634 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);
635 670
636 av = (AV *)mg->mg_obj; 671 ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv);
637
638 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
639 av_extend (av, AvFILLp (av) + 1);
640
641 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
642} 672}
643 673
644/** load & save, init *******************************************************/ 674/** load & save, init *******************************************************/
645 675
646ecb_inline void 676ecb_inline void
721 /* now do the ugly restore mess */ 751 /* now do the ugly restore mess */
722 while (ecb_expect_true (cv = (CV *)POPs)) 752 while (ecb_expect_true (cv = (CV *)POPs))
723 { 753 {
724 put_padlist (aTHX_ cv); /* mark this padlist as available */ 754 put_padlist (aTHX_ cv); /* mark this padlist as available */
725 CvDEPTH (cv) = PTR2IV (POPs); 755 CvDEPTH (cv) = PTR2IV (POPs);
726 CvPADLIST (cv) = (AV *)POPs; 756 CvPADLIST (cv) = (PADLIST *)POPs;
727 } 757 }
728 758
729 PUTBACK; 759 PUTBACK;
730 } 760 }
731 761
1503{ 1533{
1504 coro_cctx *cctx = cctx_new (); 1534 coro_cctx *cctx = cctx_new ();
1505 void *stack_start; 1535 void *stack_start;
1506 size_t stack_size; 1536 size_t stack_size;
1507 1537
1538#if CORO_FIBER
1539
1540 cctx->ssize = cctx_stacksize * sizeof (long);
1541 cctx->sptr = 0;
1542
1543#else
1544
1508#if HAVE_MMAP 1545 #if HAVE_MMAP
1509 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1546 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1510 /* mmap supposedly does allocate-on-write for us */ 1547 /* mmap supposedly does allocate-on-write for us */
1511 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0); 1548 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1512 1549
1513 if (cctx->sptr != (void *)-1) 1550 if (cctx->sptr != (void *)-1)
1514 { 1551 {
1515 #if CORO_STACKGUARD 1552 #if CORO_STACKGUARD
1516 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1553 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1517 #endif 1554 #endif
1518 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; 1555 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1519 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1556 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1520 cctx->flags |= CC_MAPPED; 1557 cctx->flags |= CC_MAPPED;
1521 } 1558 }
1522 else 1559 else
1523#endif 1560 #endif
1524 { 1561 {
1525 cctx->ssize = cctx_stacksize * (long)sizeof (long); 1562 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1526 New (0, cctx->sptr, cctx_stacksize, long); 1563 New (0, cctx->sptr, cctx_stacksize, long);
1527 1564
1528 if (!cctx->sptr) 1565 if (!cctx->sptr)
1529 { 1566 {
1530 perror ("FATAL: unable to allocate stack for coroutine, exiting."); 1567 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1531 _exit (EXIT_FAILURE); 1568 _exit (EXIT_FAILURE);
1532 } 1569 }
1533 1570
1534 stack_start = cctx->sptr; 1571 stack_start = cctx->sptr;
1535 stack_size = cctx->ssize; 1572 stack_size = cctx->ssize;
1536 } 1573 }
1574 #endif
1537 1575
1538 #if CORO_USE_VALGRIND 1576 #if CORO_USE_VALGRIND
1539 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); 1577 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1540 #endif 1578 #endif
1541 1579
1560 { 1598 {
1561 #if CORO_USE_VALGRIND 1599 #if CORO_USE_VALGRIND
1562 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1600 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1563 #endif 1601 #endif
1564 1602
1565#if HAVE_MMAP 1603 #if HAVE_MMAP
1566 if (cctx->flags & CC_MAPPED) 1604 if (cctx->flags & CC_MAPPED)
1567 munmap (cctx->sptr, cctx->ssize); 1605 munmap (cctx->sptr, cctx->ssize);
1568 else 1606 else
1569#endif 1607 #endif
1570 Safefree (cctx->sptr); 1608 Safefree (cctx->sptr);
1571 } 1609 }
1572 1610
1573 Safefree (cctx); 1611 Safefree (cctx);
1574} 1612}
1575 1613
3591void 3629void
3592list () 3630list ()
3593 PROTOTYPE: 3631 PROTOTYPE:
3594 PPCODE: 3632 PPCODE:
3595{ 3633{
3596 struct coro *coro; 3634 struct coro *coro;
3597 for (coro = coro_first; coro; coro = coro->next) 3635 for (coro = coro_first; coro; coro = coro->next)
3598 if (coro->hv) 3636 if (coro->hv)
3599 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 3637 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
3600} 3638}
3601 3639
3763 3801
3764void 3802void
3765times (Coro::State self) 3803times (Coro::State self)
3766 PPCODE: 3804 PPCODE:
3767{ 3805{
3768 struct coro *current = SvSTATE (coro_current); 3806 struct coro *current = SvSTATE (coro_current);
3769 3807
3770 if (ecb_expect_false (current == self)) 3808 if (ecb_expect_false (current == self))
3771 { 3809 {
3772 coro_times_update (); 3810 coro_times_update ();
3773 coro_times_add (SvSTATE (coro_current)); 3811 coro_times_add (SvSTATE (coro_current));
4048 on_leave = 1 4086 on_leave = 1
4049 PROTOTYPE: & 4087 PROTOTYPE: &
4050 CODE: 4088 CODE:
4051{ 4089{
4052 struct coro *coro = SvSTATE_current; 4090 struct coro *coro = SvSTATE_current;
4053 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 4091 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
4054 4092
4055 block = s_get_cv_croak (block); 4093 block = s_get_cv_croak (block);
4056 4094
4057 if (!*avp) 4095 if (!*avp)
4058 *avp = newAV (); 4096 *avp = newAV ();
4078 4116
4079SV * 4117SV *
4080new (SV *klass, SV *count = 0) 4118new (SV *klass, SV *count = 0)
4081 CODE: 4119 CODE:
4082{ 4120{
4083 int semcnt = 1; 4121 int semcnt = 1;
4084 4122
4085 if (count) 4123 if (count)
4086 { 4124 {
4087 SvGETMAGIC (count); 4125 SvGETMAGIC (count);
4088 4126
4148 XSRETURN_NO; 4186 XSRETURN_NO;
4149} 4187}
4150 4188
4151void 4189void
4152waiters (SV *self) 4190waiters (SV *self)
4153 PPCODE: 4191 PPCODE:
4154{ 4192{
4155 AV *av = (AV *)SvRV (self); 4193 AV *av = (AV *)SvRV (self);
4156 int wcount = AvFILLp (av) + 1 - 1; 4194 int wcount = AvFILLp (av) + 1 - 1;
4157 4195
4158 if (GIMME_V == G_SCALAR) 4196 if (GIMME_V == G_SCALAR)
4170 4208
4171void 4209void
4172_may_delete (SV *sem, int count, unsigned int extra_refs) 4210_may_delete (SV *sem, int count, unsigned int extra_refs)
4173 PPCODE: 4211 PPCODE:
4174{ 4212{
4175 AV *av = (AV *)SvRV (sem); 4213 AV *av = (AV *)SvRV (sem);
4176 4214
4177 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4215 if (SvREFCNT ((SV *)av) == 1 + extra_refs
4178 && AvFILLp (av) == 0 /* no waiters, just count */ 4216 && AvFILLp (av) == 0 /* no waiters, just count */
4179 && SvIV (AvARRAY (av)[0]) == count) 4217 && SvIV (AvARRAY (av)[0]) == count)
4180 XSRETURN_YES; 4218 XSRETURN_YES;
4201 4239
4202void 4240void
4203broadcast (SV *self) 4241broadcast (SV *self)
4204 CODE: 4242 CODE:
4205{ 4243{
4206 AV *av = (AV *)SvRV (self); 4244 AV *av = (AV *)SvRV (self);
4207 coro_signal_wake (aTHX_ av, AvFILLp (av)); 4245 coro_signal_wake (aTHX_ av, AvFILLp (av));
4208} 4246}
4209 4247
4210void 4248void
4211send (SV *self) 4249send (SV *self)
4219 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ 4257 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
4220} 4258}
4221 4259
4222IV 4260IV
4223awaited (SV *self) 4261awaited (SV *self)
4224 CODE: 4262 CODE:
4225 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; 4263 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
4226 OUTPUT: 4264 OUTPUT:
4227 RETVAL 4265 RETVAL
4228 4266
4229 4267
4234 4272
4235void 4273void
4236_schedule (...) 4274_schedule (...)
4237 CODE: 4275 CODE:
4238{ 4276{
4239 static int incede; 4277 static int incede;
4240 4278
4241 api_cede_notself (aTHX); 4279 api_cede_notself (aTHX);
4242 4280
4243 ++incede; 4281 ++incede;
4244 while (coro_nready >= incede && api_cede (aTHX)) 4282 while (coro_nready >= incede && api_cede (aTHX))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines