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.51 by pcg, Sun Nov 30 17:03:48 2003 UTC vs.
Revision 1.58 by pcg, Fri May 14 13:25:09 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);
249 SvREFCNT_dec ((SV*)padlist); 262 SvREFCNT_dec ((SV*)padlist);
250 } 263 }
251} 264}
252 265
253STATIC int 266STATIC int
254coro_cv_free (SV *sv, MAGIC *mg) 267coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
255{ 268{
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);
262} 275}
263 276
264#define PERL_MAGIC_coro PERL_MAGIC_ext 277#define PERL_MAGIC_coro PERL_MAGIC_ext
265 278
266static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
267 280
268/* the next two functions merely cache the padlists */ 281/* the next two functions merely cache the padlists */
269STATIC void 282STATIC void
270get_padlist (CV *cv) 283get_padlist (pTHX_ CV *cv)
271{ 284{
272 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 285 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
273 286
274 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 287 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
275 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 288 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
276 else 289 else
277 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 290 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
278} 291}
279 292
280STATIC void 293STATIC void
281put_padlist (CV *cv) 294put_padlist (pTHX_ CV *cv)
282{ 295{
283 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 296 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
284 297
285 if (!mg) 298 if (!mg)
286 { 299 {
348 { 361 {
349 AV *padlist = (AV *)POPs; 362 AV *padlist = (AV *)POPs;
350 363
351 if (padlist) 364 if (padlist)
352 { 365 {
353 put_padlist (cv); /* mark this padlist as available */ 366 put_padlist (aTHX_ cv); /* mark this padlist as available */
354 CvPADLIST(cv) = padlist; 367 CvPADLIST(cv) = padlist;
355#ifdef USE_THREADS
356 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
357#endif
358 } 368 }
359 369
360 ++CvDEPTH(cv); 370 ++CvDEPTH(cv);
361 } 371 }
362 372
389 if (CxTYPE(cx) == CXt_SUB) 399 if (CxTYPE(cx) == CXt_SUB)
390 { 400 {
391 CV *cv = cx->blk_sub.cv; 401 CV *cv = cx->blk_sub.cv;
392 if (CvDEPTH(cv)) 402 if (CvDEPTH(cv))
393 { 403 {
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); 404 EXTEND (SP, CvDEPTH(cv)*2);
400 405
401 while (--CvDEPTH(cv)) 406 while (--CvDEPTH(cv))
402 { 407 {
403 /* this tells the restore code to increment CvDEPTH */ 408 /* this tells the restore code to increment CvDEPTH */
406 } 411 }
407 412
408 PUSHs ((SV *)CvPADLIST(cv)); 413 PUSHs ((SV *)CvPADLIST(cv));
409 PUSHs ((SV *)cv); 414 PUSHs ((SV *)cv);
410 415
411 get_padlist (cv); /* this is a monster */ 416 get_padlist (aTHX_ cv); /* this is a monster */
412 } 417 }
413 } 418 }
414#ifdef CXt_FORMAT 419#ifdef CXt_FORMAT
415 else if (CxTYPE(cx) == CXt_FORMAT) 420 else if (CxTYPE(cx) == CXt_FORMAT)
416 { 421 {
469} 474}
470 475
471/* 476/*
472 * allocate various perl stacks. This is an exact copy 477 * allocate various perl stacks. This is an exact copy
473 * of perl.c:init_stacks, except that it uses less memory 478 * of perl.c:init_stacks, except that it uses less memory
474 * on the assumption that coroutines do not usually need 479 * on the (sometimes correct) assumption that coroutines do
475 * a lot of stackspace. 480 * not usually need a lot of stackspace.
476 */ 481 */
477STATIC void 482STATIC void
478coro_init_stacks (pTHX) 483coro_init_stacks (pTHX)
479{ 484{
485 LOCK;
486
480 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 487 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
481 PL_curstackinfo->si_type = PERLSI_MAIN; 488 PL_curstackinfo->si_type = PERLSI_MAIN;
482 PL_curstack = PL_curstackinfo->si_stack; 489 PL_curstack = PL_curstackinfo->si_stack;
483 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 490 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 491
508 PL_savestack_max = 96; 515 PL_savestack_max = 96;
509 516
510 New(54,PL_retstack,8,OP*); 517 New(54,PL_retstack,8,OP*);
511 PL_retstack_ix = 0; 518 PL_retstack_ix = 0;
512 PL_retstack_max = 8; 519 PL_retstack_max = 8;
520
521 UNLOCK;
513} 522}
514 523
515/* 524/*
516 * destroy the stacks, the callchain etc... 525 * destroy the stacks, the callchain etc...
517 */ 526 */
519destroy_stacks(pTHX) 528destroy_stacks(pTHX)
520{ 529{
521 if (!IN_DESTRUCT) 530 if (!IN_DESTRUCT)
522 { 531 {
523 /* is this ugly, I ask? */ 532 /* is this ugly, I ask? */
524 while (PL_scopestack_ix) 533 LEAVE_SCOPE (0);
525 LEAVE;
526 534
527 /* sure it is, but more important: is it correct?? :/ */ 535 /* sure it is, but more important: is it correct?? :/ */
528 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
529 FREETMPS; 536 FREETMPS;
537
538 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
530 } 539 }
531 540
532 while (PL_curstackinfo->si_next) 541 while (PL_curstackinfo->si_next)
533 PL_curstackinfo = PL_curstackinfo->si_next; 542 PL_curstackinfo = PL_curstackinfo->si_next;
534 543
535 while (PL_curstackinfo) 544 while (PL_curstackinfo)
536 { 545 {
537 PERL_SI *p = PL_curstackinfo->si_prev; 546 PERL_SI *p = PL_curstackinfo->si_prev;
538 547
539 { 548 { /*D*//*remove*/
540 dSP; 549 dSP;
541 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 550 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
542 PUTBACK; /* possibly superfluous */ 551 PUTBACK; /* possibly superfluous */
543 } 552 }
544 553
545 if (!IN_DESTRUCT) 554 if (!IN_DESTRUCT)
546 { 555 {
547 dounwind(-1); 556 dounwind (-1);/*D*//*remove*/
548 SvREFCNT_dec(PL_curstackinfo->si_stack); 557 SvREFCNT_dec (PL_curstackinfo->si_stack);
549 } 558 }
550 559
551 Safefree(PL_curstackinfo->si_cxstack); 560 Safefree (PL_curstackinfo->si_cxstack);
552 Safefree(PL_curstackinfo); 561 Safefree (PL_curstackinfo);
553 PL_curstackinfo = p; 562 PL_curstackinfo = p;
554 } 563 }
555 564
556 Safefree(PL_tmps_stack); 565 Safefree (PL_tmps_stack);
557 Safefree(PL_markstack); 566 Safefree (PL_markstack);
558 Safefree(PL_scopestack); 567 Safefree (PL_scopestack);
559 Safefree(PL_savestack); 568 Safefree (PL_savestack);
560 Safefree(PL_retstack); 569 Safefree (PL_retstack);
561} 570}
562 571
563static void 572static void
564allocate_stack (Coro__State ctx, int alloc) 573allocate_stack (Coro__State ctx, int alloc)
565{ 574{
568 New (0, stack, 1, coro_stack); 577 New (0, stack, 1, coro_stack);
569 578
570 stack->refcnt = 1; 579 stack->refcnt = 1;
571 stack->usecnt = 1; 580 stack->usecnt = 1;
572 stack->gencnt = ctx->gencnt = 0; 581 stack->gencnt = ctx->gencnt = 0;
582
573 if (alloc) 583 if (alloc)
574 { 584 {
575#if HAVE_MMAP 585#if HAVE_MMAP
576 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 586 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */
577 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 587 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
578 if (stack->sptr == (void *)-1) 588 if (stack->sptr == (void *)-1)
579#endif 589#endif
580 { 590 {
581 /*FIXME*//*D*//* reasonable stack size! */ 591 /*FIXME*//*D*//* reasonable stack size! */
582 stack->ssize = - (16384 * sizeof (long)); 592 stack->ssize = - (8192 * sizeof (long));
583 New (0, stack->sptr, 16384, long); 593 New (0, stack->sptr, 8192, long);
584 } 594 }
585 } 595 }
586 else 596 else
587 stack->sptr = 0; 597 stack->sptr = 0;
588 598
618setup_coro (void *arg) 628setup_coro (void *arg)
619{ 629{
620 /* 630 /*
621 * emulate part of the perl startup here. 631 * emulate part of the perl startup here.
622 */ 632 */
633 dTHX;
623 dSP; 634 dSP;
624 Coro__State ctx = (Coro__State)arg; 635 Coro__State ctx = (Coro__State)arg;
625 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 636 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
626 637
627 coro_init_stacks (aTHX); 638 coro_init_stacks (aTHX);
628 /*PL_curcop = 0;*/ 639 /*PL_curcop = 0;*/
629 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 640 /*PL_in_eval = PL_in_eval;*/ /* inherit */
630 SvREFCNT_dec (GvAV (PL_defgv)); 641 SvREFCNT_dec (GvAV (PL_defgv));
676continue_coro (void *arg) 687continue_coro (void *arg)
677{ 688{
678 /* 689 /*
679 * this is a _very_ stripped down perl interpreter ;) 690 * this is a _very_ stripped down perl interpreter ;)
680 */ 691 */
692 dTHX;
681 Coro__State ctx = (Coro__State)arg; 693 Coro__State ctx = (Coro__State)arg;
682 JMPENV coro_start_env; 694 JMPENV coro_start_env;
683 695
684 PL_top_env = &ctx->start_env; 696 PL_top_env = &ctx->start_env;
685 697
689 701
690 abort (); 702 abort ();
691} 703}
692 704
693STATIC void 705STATIC void
694transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 706transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
695{ 707{
696 dSTACKLEVEL; 708 dSTACKLEVEL;
697 static struct coro *xnext;
698 709
699 if (prev != next) 710 if (prev != next)
700 { 711 {
701 xnext = next;
702
703 if (next->mainstack) 712 if (next->mainstack)
704 { 713 {
714 LOCK;
705 SAVE (prev, flags); 715 SAVE (prev, flags);
706 LOAD (next); 716 LOAD (next);
717 UNLOCK;
707 718
708 /* mark this state as in-use */ 719 /* mark this state as in-use */
709 next->mainstack = 0; 720 next->mainstack = 0;
710 next->tmps_ix = -2; 721 next->tmps_ix = -2;
711 722
730 continue_coro, (void *)next, 741 continue_coro, (void *)next,
731 next->stack->sptr, labs (next->stack->ssize)); 742 next->stack->sptr, labs (next->stack->ssize));
732 } 743 }
733 744
734 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 745 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
746 prev->cursp = stacklevel;
735 /* don't add any code here */ 747 /* don't add any code here */
736 } 748 }
737 749 else
750 next->cursp = stacklevel;
738 } 751 }
739 else if (next->tmps_ix == -2) 752 else if (next->tmps_ix == -2)
740 croak ("tried to transfer to running coroutine"); 753 croak ("tried to transfer to running coroutine");
741 else 754 else
742 { 755 {
756 LOCK;
743 SAVE (prev, -1); /* first get rid of the old state */ 757 SAVE (prev, -1); /* first get rid of the old state */
758 UNLOCK;
744 759
745 if (flags & TRANSFER_SAVE_CCTXT) 760 if (flags & TRANSFER_SAVE_CCTXT)
746 { 761 {
747 if (!prev->stack) 762 if (!prev->stack)
748 allocate_stack (prev, 0); 763 allocate_stack (prev, 0);
750 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 765 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
751 { 766 {
752 PL_top_env = &next->start_env; 767 PL_top_env = &next->start_env;
753 768
754 setup_coro (next); 769 setup_coro (next);
770 next->cursp = stacklevel;
755 771
756 prev->stack->refcnt++; 772 prev->stack->refcnt++;
757 prev->stack->usecnt++; 773 prev->stack->usecnt++;
758 next->stack = prev->stack; 774 next->stack = prev->stack;
759 next->gencnt = prev->gencnt; 775 next->gencnt = prev->gencnt;
764 allocate_stack (next, 1); 780 allocate_stack (next, 1);
765 coro_create (&(next->stack->cctx), 781 coro_create (&(next->stack->cctx),
766 setup_coro, (void *)next, 782 setup_coro, (void *)next,
767 next->stack->sptr, labs (next->stack->ssize)); 783 next->stack->sptr, labs (next->stack->ssize));
768 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 784 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
785 prev->cursp = stacklevel;
769 /* don't add any code here */ 786 /* don't add any code here */
770 } 787 }
771 } 788 }
772 else 789 else
790 {
773 setup_coro (next); 791 setup_coro (next);
792 next->cursp = stacklevel;
793 }
774 } 794 }
775
776 /*
777 * xnext is now either prev or next, depending on wether
778 * we switched the c stack or not. that's why I use a global
779 * variable, that should become thread-specific at one point.
780 */
781 xnext->cursp = stacklevel;
782 } 795 }
783 796
797 LOCK;
784 if (coro_mortal) 798 if (coro_mortal)
785 { 799 {
786 SvREFCNT_dec (coro_mortal); 800 SvREFCNT_dec (coro_mortal);
787 coro_mortal = 0; 801 coro_mortal = 0;
788 } 802 }
803 UNLOCK;
789} 804}
790 805
791#define SV_CORO(sv,func) \ 806#define SV_CORO(sv,func) \
792 do { \ 807 do { \
793 if (SvROK (sv)) \ 808 if (SvROK (sv)) \
794 sv = SvRV (sv); \ 809 sv = SvRV (sv); \
795 \ 810 \
796 if (SvTYPE(sv) == SVt_PVHV) \ 811 if (SvTYPE (sv) == SVt_PVHV) \
797 { \ 812 { \
798 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 813 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
799 \ 814 \
800 if (!he) \ 815 if (!he) \
801 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 816 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
802 \ 817 \
803 (sv) = SvRV (HeVAL(he)); \ 818 (sv) = SvRV (HeVAL(he)); \
804 } \ 819 } \
805 \ 820 \
806 /* must also be changed inside Coro::Cont::yield */ \ 821 /* must also be changed inside Coro::Cont::yield */ \
807 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 822 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
808 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 823 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
809 \ 824 \
810 } while(0) 825 } while(0)
811 826
812#define SvSTATE(sv) (struct coro *)SvIV (sv) 827#define SvSTATE(sv) (struct coro *)SvIV (sv)
815api_transfer(pTHX_ SV *prev, SV *next, int flags) 830api_transfer(pTHX_ SV *prev, SV *next, int flags)
816{ 831{
817 SV_CORO (prev, "Coro::transfer"); 832 SV_CORO (prev, "Coro::transfer");
818 SV_CORO (next, "Coro::transfer"); 833 SV_CORO (next, "Coro::transfer");
819 834
820 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 835 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
821} 836}
822 837
823/** Coro ********************************************************************/ 838/** Coro ********************************************************************/
824 839
825#define PRIO_MAX 3 840#define PRIO_MAX 3
833static GV *coro_current, *coro_idle; 848static GV *coro_current, *coro_idle;
834static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 849static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
835static int coro_nready; 850static int coro_nready;
836 851
837static void 852static void
838coro_enq (SV *sv) 853coro_enq (pTHX_ SV *sv)
839{ 854{
840 if (SvTYPE (sv) == SVt_PVHV) 855 if (SvTYPE (sv) == SVt_PVHV)
841 { 856 {
842 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 857 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
843 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 858 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
854 869
855 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 870 croak ("Coro::ready tried to enqueue something that is not a coroutine");
856} 871}
857 872
858static SV * 873static SV *
859coro_deq (int min_prio) 874coro_deq (pTHX_ int min_prio)
860{ 875{
861 int prio = PRIO_MAX - PRIO_MIN; 876 int prio = PRIO_MAX - PRIO_MIN;
862 877
863 min_prio -= PRIO_MIN; 878 min_prio -= PRIO_MIN;
864 if (min_prio < 0) 879 if (min_prio < 0)
875} 890}
876 891
877static void 892static void
878api_ready (SV *coro) 893api_ready (SV *coro)
879{ 894{
895 dTHX;
896
880 if (SvROK (coro)) 897 if (SvROK (coro))
881 coro = SvRV (coro); 898 coro = SvRV (coro);
882 899
900 LOCK;
883 coro_enq (SvREFCNT_inc (coro)); 901 coro_enq (aTHX_ SvREFCNT_inc (coro));
902 UNLOCK;
884} 903}
885 904
886static void 905static void
887api_schedule (void) 906api_schedule (void)
888{ 907{
908 dTHX;
909
889 SV *prev, *next; 910 SV *prev, *next;
890 911
912 LOCK;
913
891 prev = SvRV (GvSV (coro_current)); 914 prev = SvRV (GvSV (coro_current));
892 next = coro_deq (PRIO_MIN); 915 next = coro_deq (aTHX_ PRIO_MIN);
893 916
894 if (!next) 917 if (!next)
895 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 918 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
896 919
897 /* free this only after the transfer */ 920 /* free this only after the transfer */
900 923
901 SvRV (GvSV (coro_current)) = next; 924 SvRV (GvSV (coro_current)) = next;
902 925
903 SV_CORO (next, "Coro::schedule"); 926 SV_CORO (next, "Coro::schedule");
904 927
928 UNLOCK;
929
905 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 930 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
906 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 931 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
907} 932}
908 933
909static void 934static void
910api_cede (void) 935api_cede (void)
911{ 936{
937 dTHX;
938
939 LOCK;
912 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 940 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
941 UNLOCK;
913 942
914 api_schedule (); 943 api_schedule ();
915} 944}
916 945
917MODULE = Coro::State PACKAGE = Coro::State 946MODULE = Coro::State PACKAGE = Coro::State
918 947
919PROTOTYPES: ENABLE 948PROTOTYPES: ENABLE
920 949
921BOOT: 950BOOT:
922{ /* {} necessary for stoopid perl-5.6.x */ 951{ /* {} necessary for stoopid perl-5.6.x */
952#ifdef USE_ITHREADS
953 MUTEX_INIT (&coro_mutex);
954#endif
955
923 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 956 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
924 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 957 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
925 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 958 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
926 959
927 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 960 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
928 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 961 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
929 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 962 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
930 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 963 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
931
932 if (!padlist_cache)
933 padlist_cache = newHV ();
934 964
935 main_mainstack = PL_mainstack; 965 main_mainstack = PL_mainstack;
936 966
937 coroapi.ver = CORO_API_VERSION; 967 coroapi.ver = CORO_API_VERSION;
938 coroapi.transfer = api_transfer; 968 coroapi.transfer = api_transfer;
1006void 1036void
1007_exit(code) 1037_exit(code)
1008 int code 1038 int code
1009 PROTOTYPE: $ 1039 PROTOTYPE: $
1010 CODE: 1040 CODE:
1011#if defined(__GLIBC__) || _POSIX_C_SOURCE
1012 _exit (code); 1041 _exit (code);
1013#else
1014 signal (SIGTERM, SIG_DFL);
1015 raise (SIGTERM);
1016 exit (code);
1017#endif
1018 1042
1019MODULE = Coro::State PACKAGE = Coro::Cont 1043MODULE = Coro::State PACKAGE = Coro::Cont
1020 1044
1021# this is slightly dirty (should expose a c-level api) 1045# this is slightly dirty (should expose a c-level api)
1022 1046

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines