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.54 by pcg, Sat Mar 13 06:45:04 2004 UTC vs.
Revision 1.61 by root, Tue Aug 10 01:56:30 2004 UTC

1#define PERL_NO_GET_CONTEXT
2
1#include "EXTERN.h" 3#include "EXTERN.h"
2#include "perl.h" 4#include "perl.h"
3#include "XSUB.h" 5#include "XSUB.h"
4 6
5#include "patchlevel.h" 7#include "patchlevel.h"
53 55
54#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
55 57
56#include "CoroAPI.h" 58#include "CoroAPI.h"
57 59
60#ifdef USE_ITHREADS
61static perl_mutex coro_mutex;
62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
64#else
65# define LOCK 0
66# define UNLOCK 0
67#endif
68
58static struct CoroAPI coroapi; 69static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash;
72static SV *ucoro_state_sv;
73static U32 ucoro_state_hash;
74static SV *coro_mortal; /* will be freed after next transfer */
59 75
60/* this is actually not only the c stack but also c registers etc... */ 76/* this is actually not only the c stack but also c registers etc... */
61typedef struct { 77typedef struct {
62 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
63 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
121}; 137};
122 138
123typedef struct coro *Coro__State; 139typedef struct coro *Coro__State;
124typedef struct coro *Coro__State_or_hashref; 140typedef struct coro *Coro__State_or_hashref;
125 141
126static AV *main_mainstack; /* used to differentiate between $main and others */
127static HV *coro_state_stash;
128static SV *ucoro_state_sv;
129static U32 ucoro_state_hash;
130static HV *padlist_cache;
131static SV *coro_mortal; /* will be freed after next transfer */
132
133/* mostly copied from op.c:cv_clone2 */ 142/* mostly copied from op.c:cv_clone2 */
134STATIC AV * 143STATIC AV *
135clone_padlist (AV *protopadlist) 144clone_padlist (pTHX_ AV *protopadlist)
136{ 145{
137 AV *av; 146 AV *av;
138 I32 ix; 147 I32 ix;
139 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 148 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
140 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 149 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
164 AvFLAGS (av) = AVf_REIFY; 173 AvFLAGS (av) = AVf_REIFY;
165 174
166 for (ix = fpad; ix > 0; ix--) 175 for (ix = fpad; ix > 0; ix--)
167 { 176 {
168 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 177 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
178
169 if (namesv && namesv != &PL_sv_undef) 179 if (namesv && namesv != &PL_sv_undef)
170 { 180 {
171 char *name = SvPVX (namesv); /* XXX */ 181 char *name = SvPVX (namesv); /* XXX */
182
172 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 183 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
173 { /* lexical from outside? */ 184 { /* lexical from outside? */
174 npad[ix] = SvREFCNT_inc (ppad[ix]); 185 npad[ix] = SvREFCNT_inc (ppad[ix]);
175 } 186 }
176 else 187 else
182 sv = (SV *) newAV (); 193 sv = (SV *) newAV ();
183 else if (*name == '%') 194 else if (*name == '%')
184 sv = (SV *) newHV (); 195 sv = (SV *) newHV ();
185 else 196 else
186 sv = NEWSV (0, 0); 197 sv = NEWSV (0, 0);
198
187#ifdef SvPADBUSY 199#ifdef SvPADBUSY
188 if (!SvPADBUSY (sv)) 200 if (!SvPADBUSY (sv))
189#endif 201#endif
190 SvPADMY_on (sv); 202 SvPADMY_on (sv);
203
191 npad[ix] = sv; 204 npad[ix] = sv;
192 } 205 }
193 } 206 }
194 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 207 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
195 { 208 {
225 238
226 return newpadlist; 239 return newpadlist;
227} 240}
228 241
229STATIC void 242STATIC void
230free_padlist (AV *padlist) 243free_padlist (pTHX_ AV *padlist)
231{ 244{
232 /* may be during global destruction */ 245 /* may be during global destruction */
233 if (SvREFCNT (padlist)) 246 if (SvREFCNT (padlist))
234 { 247 {
235 I32 i = AvFILLp (padlist); 248 I32 i = AvFILLp (padlist);
256 AV *padlist; 269 AV *padlist;
257 AV *av = (AV *)mg->mg_obj; 270 AV *av = (AV *)mg->mg_obj;
258 271
259 /* casting is fun. */ 272 /* casting is fun. */
260 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
261 free_padlist (padlist); 274 free_padlist (aTHX_ padlist);
275
276 SvREFCNT_dec (av);
262} 277}
263 278
264#define PERL_MAGIC_coro PERL_MAGIC_ext 279#define PERL_MAGIC_coro PERL_MAGIC_ext
265 280
266static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 281static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
267 282
268/* the next two functions merely cache the padlists */ 283/* the next two functions merely cache the padlists */
269STATIC void 284STATIC void
270get_padlist (CV *cv) 285get_padlist (pTHX_ CV *cv)
271{ 286{
272 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 287 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
273 288
274 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 289 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
275 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 290 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
276 else 291 else
277 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 292 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
278} 293}
279 294
280STATIC void 295STATIC void
281put_padlist (CV *cv) 296put_padlist (pTHX_ CV *cv)
282{ 297{
283 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 298 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
284 299
285 if (!mg) 300 if (!mg)
286 { 301 {
348 { 363 {
349 AV *padlist = (AV *)POPs; 364 AV *padlist = (AV *)POPs;
350 365
351 if (padlist) 366 if (padlist)
352 { 367 {
353 put_padlist (cv); /* mark this padlist as available */ 368 put_padlist (aTHX_ cv); /* mark this padlist as available */
354 CvPADLIST(cv) = padlist; 369 CvPADLIST(cv) = padlist;
355#ifdef USE_THREADS
356 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
357#endif
358 } 370 }
359 371
360 ++CvDEPTH(cv); 372 ++CvDEPTH(cv);
361 } 373 }
362 374
389 if (CxTYPE(cx) == CXt_SUB) 401 if (CxTYPE(cx) == CXt_SUB)
390 { 402 {
391 CV *cv = cx->blk_sub.cv; 403 CV *cv = cx->blk_sub.cv;
392 if (CvDEPTH(cv)) 404 if (CvDEPTH(cv))
393 { 405 {
394#ifdef USE_THREADS
395 /*XPUSHs ((SV *)CvOWNER(cv));*/
396 /*CvOWNER(cv) = 0;*/
397 /*error must unlock this cv etc.. etc...*/
398#endif
399 EXTEND (SP, CvDEPTH(cv)*2); 406 EXTEND (SP, CvDEPTH(cv)*2);
400 407
401 while (--CvDEPTH(cv)) 408 while (--CvDEPTH(cv))
402 { 409 {
403 /* this tells the restore code to increment CvDEPTH */ 410 /* this tells the restore code to increment CvDEPTH */
406 } 413 }
407 414
408 PUSHs ((SV *)CvPADLIST(cv)); 415 PUSHs ((SV *)CvPADLIST(cv));
409 PUSHs ((SV *)cv); 416 PUSHs ((SV *)cv);
410 417
411 get_padlist (cv); /* this is a monster */ 418 get_padlist (aTHX_ cv); /* this is a monster */
412 } 419 }
413 } 420 }
414#ifdef CXt_FORMAT 421#ifdef CXt_FORMAT
415 else if (CxTYPE(cx) == CXt_FORMAT) 422 else if (CxTYPE(cx) == CXt_FORMAT)
416 { 423 {
475 * not usually need a lot of stackspace. 482 * not usually need a lot of stackspace.
476 */ 483 */
477STATIC void 484STATIC void
478coro_init_stacks (pTHX) 485coro_init_stacks (pTHX)
479{ 486{
487 LOCK;
488
480 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 489 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
481 PL_curstackinfo->si_type = PERLSI_MAIN; 490 PL_curstackinfo->si_type = PERLSI_MAIN;
482 PL_curstack = PL_curstackinfo->si_stack; 491 PL_curstack = PL_curstackinfo->si_stack;
483 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 492 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 493
508 PL_savestack_max = 96; 517 PL_savestack_max = 96;
509 518
510 New(54,PL_retstack,8,OP*); 519 New(54,PL_retstack,8,OP*);
511 PL_retstack_ix = 0; 520 PL_retstack_ix = 0;
512 PL_retstack_max = 8; 521 PL_retstack_max = 8;
522
523 UNLOCK;
513} 524}
514 525
515/* 526/*
516 * destroy the stacks, the callchain etc... 527 * destroy the stacks, the callchain etc...
517 */ 528 */
523 /* is this ugly, I ask? */ 534 /* is this ugly, I ask? */
524 LEAVE_SCOPE (0); 535 LEAVE_SCOPE (0);
525 536
526 /* sure it is, but more important: is it correct?? :/ */ 537 /* sure it is, but more important: is it correct?? :/ */
527 FREETMPS; 538 FREETMPS;
539
540 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
528 } 541 }
529 542
530 while (PL_curstackinfo->si_next) 543 while (PL_curstackinfo->si_next)
531 PL_curstackinfo = PL_curstackinfo->si_next; 544 PL_curstackinfo = PL_curstackinfo->si_next;
532 545
533 while (PL_curstackinfo) 546 while (PL_curstackinfo)
534 { 547 {
535 PERL_SI *p = PL_curstackinfo->si_prev; 548 PERL_SI *p = PL_curstackinfo->si_prev;
536 549
537 { 550 { /*D*//*remove*/
538 dSP; 551 dSP;
539 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 552 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
540 PUTBACK; /* possibly superfluous */ 553 PUTBACK; /* possibly superfluous */
541 } 554 }
542 555
543 if (!IN_DESTRUCT) 556 if (!IN_DESTRUCT)
544 { 557 {
545 dounwind(-1); 558 dounwind (-1);/*D*//*remove*/
546 SvREFCNT_dec(PL_curstackinfo->si_stack); 559 SvREFCNT_dec (PL_curstackinfo->si_stack);
547 } 560 }
548 561
549 Safefree(PL_curstackinfo->si_cxstack); 562 Safefree (PL_curstackinfo->si_cxstack);
550 Safefree(PL_curstackinfo); 563 Safefree (PL_curstackinfo);
551 PL_curstackinfo = p; 564 PL_curstackinfo = p;
552 } 565 }
553 566
554 Safefree(PL_tmps_stack); 567 Safefree (PL_tmps_stack);
555 Safefree(PL_markstack); 568 Safefree (PL_markstack);
556 Safefree(PL_scopestack); 569 Safefree (PL_scopestack);
557 Safefree(PL_savestack); 570 Safefree (PL_savestack);
558 Safefree(PL_retstack); 571 Safefree (PL_retstack);
559} 572}
560 573
561static void 574static void
562allocate_stack (Coro__State ctx, int alloc) 575allocate_stack (Coro__State ctx, int alloc)
563{ 576{
566 New (0, stack, 1, coro_stack); 579 New (0, stack, 1, coro_stack);
567 580
568 stack->refcnt = 1; 581 stack->refcnt = 1;
569 stack->usecnt = 1; 582 stack->usecnt = 1;
570 stack->gencnt = ctx->gencnt = 0; 583 stack->gencnt = ctx->gencnt = 0;
584
571 if (alloc) 585 if (alloc)
572 { 586 {
573#if HAVE_MMAP 587#if HAVE_MMAP
574 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 588 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */
575 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 589 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
616setup_coro (void *arg) 630setup_coro (void *arg)
617{ 631{
618 /* 632 /*
619 * emulate part of the perl startup here. 633 * emulate part of the perl startup here.
620 */ 634 */
635 dTHX;
621 dSP; 636 dSP;
622 Coro__State ctx = (Coro__State)arg; 637 Coro__State ctx = (Coro__State)arg;
623 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 638 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
624 639
625 coro_init_stacks (aTHX); 640 coro_init_stacks (aTHX);
674continue_coro (void *arg) 689continue_coro (void *arg)
675{ 690{
676 /* 691 /*
677 * this is a _very_ stripped down perl interpreter ;) 692 * this is a _very_ stripped down perl interpreter ;)
678 */ 693 */
694 dTHX;
679 Coro__State ctx = (Coro__State)arg; 695 Coro__State ctx = (Coro__State)arg;
680 JMPENV coro_start_env; 696 JMPENV coro_start_env;
681 697
682 PL_top_env = &ctx->start_env; 698 PL_top_env = &ctx->start_env;
683 699
687 703
688 abort (); 704 abort ();
689} 705}
690 706
691STATIC void 707STATIC void
692transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 708transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
693{ 709{
694 dSTACKLEVEL; 710 dSTACKLEVEL;
695 static struct coro *xnext;
696 711
697 if (prev != next) 712 if (prev != next)
698 { 713 {
699 xnext = next;
700
701 if (next->mainstack) 714 if (next->mainstack)
702 { 715 {
716 LOCK;
703 SAVE (prev, flags); 717 SAVE (prev, flags);
704 LOAD (next); 718 LOAD (next);
719 UNLOCK;
705 720
706 /* mark this state as in-use */ 721 /* mark this state as in-use */
707 next->mainstack = 0; 722 next->mainstack = 0;
708 next->tmps_ix = -2; 723 next->tmps_ix = -2;
709 724
728 continue_coro, (void *)next, 743 continue_coro, (void *)next,
729 next->stack->sptr, labs (next->stack->ssize)); 744 next->stack->sptr, labs (next->stack->ssize));
730 } 745 }
731 746
732 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 747 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
748 prev->cursp = stacklevel;
733 /* don't add any code here */ 749 /* don't add any code here */
734 } 750 }
735 751 else
752 next->cursp = stacklevel;
736 } 753 }
737 else if (next->tmps_ix == -2) 754 else if (next->tmps_ix == -2)
738 croak ("tried to transfer to running coroutine"); 755 croak ("tried to transfer to running coroutine");
739 else 756 else
740 { 757 {
758 LOCK;
741 SAVE (prev, -1); /* first get rid of the old state */ 759 SAVE (prev, -1); /* first get rid of the old state */
760 UNLOCK;
742 761
743 if (flags & TRANSFER_SAVE_CCTXT) 762 if (flags & TRANSFER_SAVE_CCTXT)
744 { 763 {
745 if (!prev->stack) 764 if (!prev->stack)
746 allocate_stack (prev, 0); 765 allocate_stack (prev, 0);
748 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 767 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
749 { 768 {
750 PL_top_env = &next->start_env; 769 PL_top_env = &next->start_env;
751 770
752 setup_coro (next); 771 setup_coro (next);
772 next->cursp = stacklevel;
753 773
754 prev->stack->refcnt++; 774 prev->stack->refcnt++;
755 prev->stack->usecnt++; 775 prev->stack->usecnt++;
756 next->stack = prev->stack; 776 next->stack = prev->stack;
757 next->gencnt = prev->gencnt; 777 next->gencnt = prev->gencnt;
762 allocate_stack (next, 1); 782 allocate_stack (next, 1);
763 coro_create (&(next->stack->cctx), 783 coro_create (&(next->stack->cctx),
764 setup_coro, (void *)next, 784 setup_coro, (void *)next,
765 next->stack->sptr, labs (next->stack->ssize)); 785 next->stack->sptr, labs (next->stack->ssize));
766 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 786 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
787 prev->cursp = stacklevel;
767 /* don't add any code here */ 788 /* don't add any code here */
768 } 789 }
769 } 790 }
770 else 791 else
792 {
771 setup_coro (next); 793 setup_coro (next);
794 next->cursp = stacklevel;
795 }
772 } 796 }
773
774 /*
775 * xnext is now either prev or next, depending on wether
776 * we switched the c stack or not. that's why I use a global
777 * variable, that should become thread-specific at one point.
778 */
779 xnext->cursp = stacklevel;
780 } 797 }
781 798
799 LOCK;
782 if (coro_mortal) 800 if (coro_mortal)
783 { 801 {
784 SvREFCNT_dec (coro_mortal); 802 SvREFCNT_dec (coro_mortal);
785 coro_mortal = 0; 803 coro_mortal = 0;
786 } 804 }
805 UNLOCK;
787} 806}
788 807
789#define SV_CORO(sv,func) \ 808#define SV_CORO(sv,func) \
790 do { \ 809 do { \
791 if (SvROK (sv)) \ 810 if (SvROK (sv)) \
792 sv = SvRV (sv); \ 811 sv = SvRV (sv); \
793 \ 812 \
794 if (SvTYPE(sv) == SVt_PVHV) \ 813 if (SvTYPE (sv) == SVt_PVHV) \
795 { \ 814 { \
796 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 815 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
797 \ 816 \
798 if (!he) \ 817 if (!he) \
799 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 818 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
800 \ 819 \
801 (sv) = SvRV (HeVAL(he)); \ 820 (sv) = SvRV (HeVAL(he)); \
802 } \ 821 } \
803 \ 822 \
804 /* must also be changed inside Coro::Cont::yield */ \ 823 /* must also be changed inside Coro::Cont::yield */ \
805 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 824 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
806 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 825 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
807 \ 826 \
808 } while(0) 827 } while(0)
809 828
810#define SvSTATE(sv) (struct coro *)SvIV (sv) 829#define SvSTATE(sv) (struct coro *)SvIV (sv)
813api_transfer(pTHX_ SV *prev, SV *next, int flags) 832api_transfer(pTHX_ SV *prev, SV *next, int flags)
814{ 833{
815 SV_CORO (prev, "Coro::transfer"); 834 SV_CORO (prev, "Coro::transfer");
816 SV_CORO (next, "Coro::transfer"); 835 SV_CORO (next, "Coro::transfer");
817 836
818 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 837 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
819} 838}
820 839
821/** Coro ********************************************************************/ 840/** Coro ********************************************************************/
822 841
823#define PRIO_MAX 3 842#define PRIO_MAX 3
831static GV *coro_current, *coro_idle; 850static GV *coro_current, *coro_idle;
832static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 851static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
833static int coro_nready; 852static int coro_nready;
834 853
835static void 854static void
836coro_enq (SV *sv) 855coro_enq (pTHX_ SV *sv)
837{ 856{
838 if (SvTYPE (sv) == SVt_PVHV) 857 if (SvTYPE (sv) == SVt_PVHV)
839 { 858 {
840 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 859 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
841 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 860 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
852 871
853 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 872 croak ("Coro::ready tried to enqueue something that is not a coroutine");
854} 873}
855 874
856static SV * 875static SV *
857coro_deq (int min_prio) 876coro_deq (pTHX_ int min_prio)
858{ 877{
859 int prio = PRIO_MAX - PRIO_MIN; 878 int prio = PRIO_MAX - PRIO_MIN;
860 879
861 min_prio -= PRIO_MIN; 880 min_prio -= PRIO_MIN;
862 if (min_prio < 0) 881 if (min_prio < 0)
873} 892}
874 893
875static void 894static void
876api_ready (SV *coro) 895api_ready (SV *coro)
877{ 896{
897 dTHX;
898
878 if (SvROK (coro)) 899 if (SvROK (coro))
879 coro = SvRV (coro); 900 coro = SvRV (coro);
880 901
902 LOCK;
881 coro_enq (SvREFCNT_inc (coro)); 903 coro_enq (aTHX_ SvREFCNT_inc (coro));
904 UNLOCK;
882} 905}
883 906
884static void 907static void
885api_schedule (void) 908api_schedule (void)
886{ 909{
910 dTHX;
911
887 SV *prev, *next; 912 SV *prev, *next;
888 913
914 LOCK;
915
889 prev = SvRV (GvSV (coro_current)); 916 prev = SvRV (GvSV (coro_current));
890 next = coro_deq (PRIO_MIN); 917 next = coro_deq (aTHX_ PRIO_MIN);
891 918
892 if (!next) 919 if (!next)
893 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 920 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
894 921
895 /* free this only after the transfer */ 922 /* free this only after the transfer */
898 925
899 SvRV (GvSV (coro_current)) = next; 926 SvRV (GvSV (coro_current)) = next;
900 927
901 SV_CORO (next, "Coro::schedule"); 928 SV_CORO (next, "Coro::schedule");
902 929
930 UNLOCK;
931
903 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 932 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
904 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 933 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
905} 934}
906 935
907static void 936static void
908api_cede (void) 937api_cede (void)
909{ 938{
939 dTHX;
940
941 LOCK;
910 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 942 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
943 UNLOCK;
911 944
912 api_schedule (); 945 api_schedule ();
913} 946}
914 947
915MODULE = Coro::State PACKAGE = Coro::State 948MODULE = Coro::State PACKAGE = Coro::State
916 949
917PROTOTYPES: ENABLE 950PROTOTYPES: ENABLE
918 951
919BOOT: 952BOOT:
920{ /* {} necessary for stoopid perl-5.6.x */ 953{ /* {} necessary for stoopid perl-5.6.x */
954#ifdef USE_ITHREADS
955 MUTEX_INIT (&coro_mutex);
956#endif
957
921 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 958 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
922 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 959 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
923 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 960 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
924 961
925 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 962 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
926 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 963 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
927 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 964 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
928 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 965 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
929
930 if (!padlist_cache)
931 padlist_cache = newHV ();
932 966
933 main_mainstack = PL_mainstack; 967 main_mainstack = PL_mainstack;
934 968
935 coroapi.ver = CORO_API_VERSION; 969 coroapi.ver = CORO_API_VERSION;
936 coroapi.transfer = api_transfer; 970 coroapi.transfer = api_transfer;
947 croak ("Coro::State::_newprocess expects an arrayref"); 981 croak ("Coro::State::_newprocess expects an arrayref");
948 982
949 Newz (0, coro, 1, struct coro); 983 Newz (0, coro, 1, struct coro);
950 984
951 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 985 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
952 coro->mainstack = 0; /* actual work is done inside transfer */ 986 /*coro->mainstack = 0; *//*actual work is done inside transfer */
953 coro->stack = 0; 987 /*coro->stack = 0;*/
954 988
955 /* same as JMPENV_BOOTSTRAP */ 989 /* same as JMPENV_BOOTSTRAP */
956 /* we might be able to recycle start_env, but safe is safe */ 990 /* we might be able to recycle start_env, but safe is safe */
957 //Zero(&coro->start_env, 1, JMPENV); 991 /*Zero(&coro->start_env, 1, JMPENV);*/
958 coro->start_env.je_ret = -1; 992 coro->start_env.je_ret = -1;
959 coro->start_env.je_mustcatch = TRUE; 993 coro->start_env.je_mustcatch = TRUE;
960 994
961 RETVAL = coro; 995 RETVAL = coro;
962 OUTPUT: 996 OUTPUT:
983 if (coro->mainstack && coro->mainstack != main_mainstack) 1017 if (coro->mainstack && coro->mainstack != main_mainstack)
984 { 1018 {
985 struct coro temp; 1019 struct coro temp;
986 1020
987 PUTBACK; 1021 PUTBACK;
988 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1022 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
989 LOAD(aTHX_ coro); 1023 LOAD (aTHX_ coro);
990 SPAGAIN; 1024 SPAGAIN;
991 1025
992 destroy_stacks (aTHX); 1026 destroy_stacks (aTHX);
993 1027
994 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1028 LOAD ((&temp)); /* this will get rid of defsv etc.. */
995 SPAGAIN; 1029 SPAGAIN;
996 1030
997 coro->mainstack = 0; 1031 coro->mainstack = 0;
998 } 1032 }
999 1033
1028 av_clear (defav); 1062 av_clear (defav);
1029 av_fill (defav, items - 1); 1063 av_fill (defav, items - 1);
1030 while (items--) 1064 while (items--)
1031 av_store (defav, items, SvREFCNT_inc (ST(items))); 1065 av_store (defav, items, SvREFCNT_inc (ST(items)));
1032 1066
1033 mg_get (returnstk); /* isn't documentation wrong for mg_get? */ 1067 SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */
1034 sv = av_pop ((AV *)SvRV (returnstk)); 1068 sv = av_pop ((AV *)SvRV (returnstk));
1035 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1069 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1036 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1070 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1037 SvREFCNT_dec (sv); 1071 SvREFCNT_dec (sv);
1038 1072
1039 transfer(aTHX_ prev, next, 0); 1073 transfer (aTHX_ prev, next, 0);
1040 1074
1041MODULE = Coro::State PACKAGE = Coro 1075MODULE = Coro::State PACKAGE = Coro
1042 1076
1043# this is slightly dirty (should expose a c-level api) 1077# this is slightly dirty (should expose a c-level api)
1044 1078

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines