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.64 by root, Mon Mar 21 15:06:51 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines