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.48 by pcg, Sat Nov 15 03:53:37 2003 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"
39# undef HAVE_MMAP 41# undef HAVE_MMAP
40# endif 42# endif
41# endif 43# endif
42#endif 44#endif
43 45
44#define MAY_FLUSH /* increases codesize and is rarely used */
45
46#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
48 48
49/* The next macro should declare a variable stacklevel that contains and approximation 49/* The next macro should declare a variable stacklevel that contains and approximation
50 * to the current C stack pointer. Its property is that it changes with each call 50 * to the current C stack pointer. Its property is that it changes with each call
55 55
56#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
57 57
58#include "CoroAPI.h" 58#include "CoroAPI.h"
59 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
60static 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 */
61 75
62/* 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... */
63typedef struct { 77typedef struct {
64 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
65 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
123}; 137};
124 138
125typedef struct coro *Coro__State; 139typedef struct coro *Coro__State;
126typedef struct coro *Coro__State_or_hashref; 140typedef struct coro *Coro__State_or_hashref;
127 141
128static AV *main_mainstack; /* used to differentiate between $main and others */
129static HV *coro_state_stash;
130static SV *ucoro_state_sv;
131static U32 ucoro_state_hash;
132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
134
135/* mostly copied from op.c:cv_clone2 */ 142/* mostly copied from op.c:cv_clone2 */
136STATIC AV * 143STATIC AV *
137clone_padlist (AV *protopadlist) 144clone_padlist (pTHX_ AV *protopadlist)
138{ 145{
139 AV *av; 146 AV *av;
140 I32 ix; 147 I32 ix;
141 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 148 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
142 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 149 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
166 AvFLAGS (av) = AVf_REIFY; 173 AvFLAGS (av) = AVf_REIFY;
167 174
168 for (ix = fpad; ix > 0; ix--) 175 for (ix = fpad; ix > 0; ix--)
169 { 176 {
170 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 177 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
178
171 if (namesv && namesv != &PL_sv_undef) 179 if (namesv && namesv != &PL_sv_undef)
172 { 180 {
173 char *name = SvPVX (namesv); /* XXX */ 181 char *name = SvPVX (namesv); /* XXX */
182
174 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 183 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
175 { /* lexical from outside? */ 184 { /* lexical from outside? */
176 npad[ix] = SvREFCNT_inc (ppad[ix]); 185 npad[ix] = SvREFCNT_inc (ppad[ix]);
177 } 186 }
178 else 187 else
184 sv = (SV *) newAV (); 193 sv = (SV *) newAV ();
185 else if (*name == '%') 194 else if (*name == '%')
186 sv = (SV *) newHV (); 195 sv = (SV *) newHV ();
187 else 196 else
188 sv = NEWSV (0, 0); 197 sv = NEWSV (0, 0);
198
199#ifdef SvPADBUSY
189 if (!SvPADBUSY (sv)) 200 if (!SvPADBUSY (sv))
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 {
224#endif 237#endif
225 238
226 return newpadlist; 239 return newpadlist;
227} 240}
228 241
229#ifdef MAY_FLUSH
230STATIC void 242STATIC void
231free_padlist (AV *padlist) 243free_padlist (pTHX_ AV *padlist)
232{ 244{
233 /* may be during global destruction */ 245 /* may be during global destruction */
234 if (SvREFCNT(padlist)) 246 if (SvREFCNT (padlist))
235 { 247 {
236 I32 i = AvFILLp(padlist); 248 I32 i = AvFILLp (padlist);
237 while (i >= 0) 249 while (i >= 0)
238 { 250 {
239 SV **svp = av_fetch(padlist, i--, FALSE); 251 SV **svp = av_fetch (padlist, i--, FALSE);
240 SV *sv = svp ? *svp : Nullsv;
241 if (sv) 252 if (svp)
253 {
254 SV *sv;
255 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
242 SvREFCNT_dec(sv); 256 SvREFCNT_dec (sv);
257
258 SvREFCNT_dec (*svp);
259 }
243 } 260 }
244 261
245 SvREFCNT_dec((SV*)padlist); 262 SvREFCNT_dec ((SV*)padlist);
246 } 263 }
247} 264}
248#endif 265
266STATIC int
267coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
268{
269 AV *padlist;
270 AV *av = (AV *)mg->mg_obj;
271
272 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist);
275
276 SvREFCNT_dec (av);
277}
278
279#define PERL_MAGIC_coro PERL_MAGIC_ext
280
281static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
249 282
250/* the next two functions merely cache the padlists */ 283/* the next two functions merely cache the padlists */
251STATIC void 284STATIC void
252get_padlist (CV *cv) 285get_padlist (pTHX_ CV *cv)
253{ 286{
254 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); 287 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
255 288
256 if (he && AvFILLp ((AV *)*he) >= 0) 289 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
257 CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); 290 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
258 else 291 else
259 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 292 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
260} 293}
261 294
262STATIC void 295STATIC void
263put_padlist (CV *cv) 296put_padlist (pTHX_ CV *cv)
264{ 297{
265 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); 298 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
266 299
267 if (SvTYPE (*he) != SVt_PVAV) 300 if (!mg)
268 { 301 {
269 SvREFCNT_dec (*he); 302 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
303 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
304 mg->mg_virtual = &vtbl_coro;
270 *he = (SV *)newAV (); 305 mg->mg_obj = (SV *)newAV ();
271 } 306 }
272 307
273 av_push ((AV *)*he, (SV *)CvPADLIST (cv)); 308 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv));
274} 309}
275
276#ifdef MAY_FLUSH
277STATIC void
278flush_padlist_cache ()
279{
280 HV *hv = padlist_cache;
281 padlist_cache = newHV ();
282
283 if (hv_iterinit (hv))
284 {
285 HE *he;
286 AV *padlist;
287
288 while (!!(he = hv_iternext (hv)))
289 {
290 AV *av = (AV *)HeVAL(he);
291
292 /* casting is fun. */
293 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
294 free_padlist (padlist);
295 }
296 }
297
298 SvREFCNT_dec (hv);
299}
300#endif
301 310
302#define SB do { 311#define SB do {
303#define SE } while (0) 312#define SE } while (0)
304 313
305#define LOAD(state) load_state(aTHX_ (state)); 314#define LOAD(state) load_state(aTHX_ (state));
354 { 363 {
355 AV *padlist = (AV *)POPs; 364 AV *padlist = (AV *)POPs;
356 365
357 if (padlist) 366 if (padlist)
358 { 367 {
359 put_padlist (cv); /* mark this padlist as available */ 368 put_padlist (aTHX_ cv); /* mark this padlist as available */
360 CvPADLIST(cv) = padlist; 369 CvPADLIST(cv) = padlist;
361#ifdef USE_THREADS
362 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
363#endif
364 } 370 }
365 371
366 ++CvDEPTH(cv); 372 ++CvDEPTH(cv);
367 } 373 }
368 374
395 if (CxTYPE(cx) == CXt_SUB) 401 if (CxTYPE(cx) == CXt_SUB)
396 { 402 {
397 CV *cv = cx->blk_sub.cv; 403 CV *cv = cx->blk_sub.cv;
398 if (CvDEPTH(cv)) 404 if (CvDEPTH(cv))
399 { 405 {
400#ifdef USE_THREADS
401 /*XPUSHs ((SV *)CvOWNER(cv));*/
402 /*CvOWNER(cv) = 0;*/
403 /*error must unlock this cv etc.. etc...*/
404#endif
405 EXTEND (SP, CvDEPTH(cv)*2); 406 EXTEND (SP, CvDEPTH(cv)*2);
406 407
407 while (--CvDEPTH(cv)) 408 while (--CvDEPTH(cv))
408 { 409 {
409 /* this tells the restore code to increment CvDEPTH */ 410 /* this tells the restore code to increment CvDEPTH */
412 } 413 }
413 414
414 PUSHs ((SV *)CvPADLIST(cv)); 415 PUSHs ((SV *)CvPADLIST(cv));
415 PUSHs ((SV *)cv); 416 PUSHs ((SV *)cv);
416 417
417 get_padlist (cv); /* this is a monster */ 418 get_padlist (aTHX_ cv); /* this is a monster */
418 } 419 }
419 } 420 }
420#ifdef CXt_FORMAT 421#ifdef CXt_FORMAT
421 else if (CxTYPE(cx) == CXt_FORMAT) 422 else if (CxTYPE(cx) == CXt_FORMAT)
422 { 423 {
475} 476}
476 477
477/* 478/*
478 * allocate various perl stacks. This is an exact copy 479 * allocate various perl stacks. This is an exact copy
479 * of perl.c:init_stacks, except that it uses less memory 480 * of perl.c:init_stacks, except that it uses less memory
480 * on the assumption that coroutines do not usually need 481 * on the (sometimes correct) assumption that coroutines do
481 * a lot of stackspace. 482 * not usually need a lot of stackspace.
482 */ 483 */
483STATIC void 484STATIC void
484coro_init_stacks (pTHX) 485coro_init_stacks (pTHX)
485{ 486{
487 LOCK;
488
486 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 489 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
487 PL_curstackinfo->si_type = PERLSI_MAIN; 490 PL_curstackinfo->si_type = PERLSI_MAIN;
488 PL_curstack = PL_curstackinfo->si_stack; 491 PL_curstack = PL_curstackinfo->si_stack;
489 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 492 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
490 493
514 PL_savestack_max = 96; 517 PL_savestack_max = 96;
515 518
516 New(54,PL_retstack,8,OP*); 519 New(54,PL_retstack,8,OP*);
517 PL_retstack_ix = 0; 520 PL_retstack_ix = 0;
518 PL_retstack_max = 8; 521 PL_retstack_max = 8;
522
523 UNLOCK;
519} 524}
520 525
521/* 526/*
522 * destroy the stacks, the callchain etc... 527 * destroy the stacks, the callchain etc...
523 */ 528 */
525destroy_stacks(pTHX) 530destroy_stacks(pTHX)
526{ 531{
527 if (!IN_DESTRUCT) 532 if (!IN_DESTRUCT)
528 { 533 {
529 /* is this ugly, I ask? */ 534 /* is this ugly, I ask? */
530 while (PL_scopestack_ix) 535 LEAVE_SCOPE (0);
531 LEAVE;
532 536
533 /* sure it is, but more important: is it correct?? :/ */ 537 /* sure it is, but more important: is it correct?? :/ */
534 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
535 FREETMPS; 538 FREETMPS;
539
540 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
536 } 541 }
537 542
538 while (PL_curstackinfo->si_next) 543 while (PL_curstackinfo->si_next)
539 PL_curstackinfo = PL_curstackinfo->si_next; 544 PL_curstackinfo = PL_curstackinfo->si_next;
540 545
541 while (PL_curstackinfo) 546 while (PL_curstackinfo)
542 { 547 {
543 PERL_SI *p = PL_curstackinfo->si_prev; 548 PERL_SI *p = PL_curstackinfo->si_prev;
544 549
545 { 550 { /*D*//*remove*/
546 dSP; 551 dSP;
547 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 552 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
548 PUTBACK; /* possibly superfluous */ 553 PUTBACK; /* possibly superfluous */
549 } 554 }
550 555
551 if (!IN_DESTRUCT) 556 if (!IN_DESTRUCT)
552 { 557 {
553 dounwind(-1); 558 dounwind (-1);/*D*//*remove*/
554 SvREFCNT_dec(PL_curstackinfo->si_stack); 559 SvREFCNT_dec (PL_curstackinfo->si_stack);
555 } 560 }
556 561
557 Safefree(PL_curstackinfo->si_cxstack); 562 Safefree (PL_curstackinfo->si_cxstack);
558 Safefree(PL_curstackinfo); 563 Safefree (PL_curstackinfo);
559 PL_curstackinfo = p; 564 PL_curstackinfo = p;
560 } 565 }
561 566
562 Safefree(PL_tmps_stack); 567 Safefree (PL_tmps_stack);
563 Safefree(PL_markstack); 568 Safefree (PL_markstack);
564 Safefree(PL_scopestack); 569 Safefree (PL_scopestack);
565 Safefree(PL_savestack); 570 Safefree (PL_savestack);
566 Safefree(PL_retstack); 571 Safefree (PL_retstack);
567} 572}
568 573
569static void 574static void
570allocate_stack (Coro__State ctx, int alloc) 575allocate_stack (Coro__State ctx, int alloc)
571{ 576{
574 New (0, stack, 1, coro_stack); 579 New (0, stack, 1, coro_stack);
575 580
576 stack->refcnt = 1; 581 stack->refcnt = 1;
577 stack->usecnt = 1; 582 stack->usecnt = 1;
578 stack->gencnt = ctx->gencnt = 0; 583 stack->gencnt = ctx->gencnt = 0;
584
579 if (alloc) 585 if (alloc)
580 { 586 {
581#if HAVE_MMAP 587#if HAVE_MMAP
582 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 588 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */
583 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);
584 if (stack->sptr == (void *)-1) 590 if (stack->sptr == (void *)-1)
585#endif 591#endif
586 { 592 {
587 /*FIXME*//*D*//* reasonable stack size! */ 593 /*FIXME*//*D*//* reasonable stack size! */
588 stack->ssize = -4096 * sizeof (long); 594 stack->ssize = - (8192 * sizeof (long));
589 New (0, stack->sptr, 4096, long); 595 New (0, stack->sptr, 8192, long);
590 } 596 }
591 } 597 }
592 else 598 else
593 stack->sptr = 0; 599 stack->sptr = 0;
594 600
624setup_coro (void *arg) 630setup_coro (void *arg)
625{ 631{
626 /* 632 /*
627 * emulate part of the perl startup here. 633 * emulate part of the perl startup here.
628 */ 634 */
635 dTHX;
629 dSP; 636 dSP;
630 Coro__State ctx = (Coro__State)arg; 637 Coro__State ctx = (Coro__State)arg;
631 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 638 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
632 639
633 coro_init_stacks (aTHX); 640 coro_init_stacks (aTHX);
634 /*PL_curcop = 0;*/ 641 /*PL_curcop = 0;*/
635 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 642 /*PL_in_eval = PL_in_eval;*/ /* inherit */
636 SvREFCNT_dec (GvAV (PL_defgv)); 643 SvREFCNT_dec (GvAV (PL_defgv));
682continue_coro (void *arg) 689continue_coro (void *arg)
683{ 690{
684 /* 691 /*
685 * this is a _very_ stripped down perl interpreter ;) 692 * this is a _very_ stripped down perl interpreter ;)
686 */ 693 */
694 dTHX;
687 Coro__State ctx = (Coro__State)arg; 695 Coro__State ctx = (Coro__State)arg;
688 JMPENV coro_start_env; 696 JMPENV coro_start_env;
689 697
690 PL_top_env = &ctx->start_env; 698 PL_top_env = &ctx->start_env;
691 699
695 703
696 abort (); 704 abort ();
697} 705}
698 706
699STATIC void 707STATIC void
700transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 708transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
701{ 709{
702 dSTACKLEVEL; 710 dSTACKLEVEL;
703 static struct coro *xnext;
704 711
705 if (prev != next) 712 if (prev != next)
706 { 713 {
707 xnext = next;
708
709 if (next->mainstack) 714 if (next->mainstack)
710 { 715 {
716 LOCK;
711 SAVE (prev, flags); 717 SAVE (prev, flags);
712 LOAD (next); 718 LOAD (next);
719 UNLOCK;
713 720
714 /* mark this state as in-use */ 721 /* mark this state as in-use */
715 next->mainstack = 0; 722 next->mainstack = 0;
716 next->tmps_ix = -2; 723 next->tmps_ix = -2;
717 724
736 continue_coro, (void *)next, 743 continue_coro, (void *)next,
737 next->stack->sptr, labs (next->stack->ssize)); 744 next->stack->sptr, labs (next->stack->ssize));
738 } 745 }
739 746
740 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 747 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
748 prev->cursp = stacklevel;
741 /* don't add any code here */ 749 /* don't add any code here */
742 } 750 }
743 751 else
752 next->cursp = stacklevel;
744 } 753 }
745 else if (next->tmps_ix == -2) 754 else if (next->tmps_ix == -2)
746 croak ("tried to transfer to running coroutine"); 755 croak ("tried to transfer to running coroutine");
747 else 756 else
748 { 757 {
758 LOCK;
749 SAVE (prev, -1); /* first get rid of the old state */ 759 SAVE (prev, -1); /* first get rid of the old state */
760 UNLOCK;
750 761
751 if (flags & TRANSFER_SAVE_CCTXT) 762 if (flags & TRANSFER_SAVE_CCTXT)
752 { 763 {
753 if (!prev->stack) 764 if (!prev->stack)
754 allocate_stack (prev, 0); 765 allocate_stack (prev, 0);
756 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 767 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
757 { 768 {
758 PL_top_env = &next->start_env; 769 PL_top_env = &next->start_env;
759 770
760 setup_coro (next); 771 setup_coro (next);
772 next->cursp = stacklevel;
761 773
762 prev->stack->refcnt++; 774 prev->stack->refcnt++;
763 prev->stack->usecnt++; 775 prev->stack->usecnt++;
764 next->stack = prev->stack; 776 next->stack = prev->stack;
765 next->gencnt = prev->gencnt; 777 next->gencnt = prev->gencnt;
770 allocate_stack (next, 1); 782 allocate_stack (next, 1);
771 coro_create (&(next->stack->cctx), 783 coro_create (&(next->stack->cctx),
772 setup_coro, (void *)next, 784 setup_coro, (void *)next,
773 next->stack->sptr, labs (next->stack->ssize)); 785 next->stack->sptr, labs (next->stack->ssize));
774 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 786 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
787 prev->cursp = stacklevel;
775 /* don't add any code here */ 788 /* don't add any code here */
776 } 789 }
777 } 790 }
778 else 791 else
792 {
779 setup_coro (next); 793 setup_coro (next);
794 next->cursp = stacklevel;
795 }
780 } 796 }
781
782 /*
783 * xnext is now either prev or next, depending on wether
784 * we switched the c stack or not. that's why I use a global
785 * variable, that should become thread-specific at one point.
786 */
787 xnext->cursp = stacklevel;
788 } 797 }
789 798
799 LOCK;
790 if (coro_mortal) 800 if (coro_mortal)
791 { 801 {
792 SvREFCNT_dec (coro_mortal); 802 SvREFCNT_dec (coro_mortal);
793 coro_mortal = 0; 803 coro_mortal = 0;
794 } 804 }
805 UNLOCK;
795} 806}
796 807
797#define SV_CORO(sv,func) \ 808#define SV_CORO(sv,func) \
798 do { \ 809 do { \
799 if (SvROK (sv)) \ 810 if (SvROK (sv)) \
800 sv = SvRV (sv); \ 811 sv = SvRV (sv); \
801 \ 812 \
802 if (SvTYPE(sv) == SVt_PVHV) \ 813 if (SvTYPE (sv) == SVt_PVHV) \
803 { \ 814 { \
804 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); \
805 \ 816 \
806 if (!he) \ 817 if (!he) \
807 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); \
808 \ 819 \
809 (sv) = SvRV (HeVAL(he)); \ 820 (sv) = SvRV (HeVAL(he)); \
810 } \ 821 } \
811 \ 822 \
812 /* must also be changed inside Coro::Cont::yield */ \ 823 /* must also be changed inside Coro::Cont::yield */ \
813 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 824 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
814 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); \
815 \ 826 \
816 } while(0) 827 } while(0)
817 828
818#define SvSTATE(sv) (struct coro *)SvIV (sv) 829#define SvSTATE(sv) (struct coro *)SvIV (sv)
821api_transfer(pTHX_ SV *prev, SV *next, int flags) 832api_transfer(pTHX_ SV *prev, SV *next, int flags)
822{ 833{
823 SV_CORO (prev, "Coro::transfer"); 834 SV_CORO (prev, "Coro::transfer");
824 SV_CORO (next, "Coro::transfer"); 835 SV_CORO (next, "Coro::transfer");
825 836
826 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 837 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
827} 838}
828 839
829/** Coro ********************************************************************/ 840/** Coro ********************************************************************/
830 841
831#define PRIO_MAX 3 842#define PRIO_MAX 3
839static GV *coro_current, *coro_idle; 850static GV *coro_current, *coro_idle;
840static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 851static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
841static int coro_nready; 852static int coro_nready;
842 853
843static void 854static void
844coro_enq (SV *sv) 855coro_enq (pTHX_ SV *sv)
845{ 856{
846 if (SvTYPE (sv) == SVt_PVHV) 857 if (SvTYPE (sv) == SVt_PVHV)
847 { 858 {
848 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 859 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
849 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 860 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
860 871
861 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");
862} 873}
863 874
864static SV * 875static SV *
865coro_deq (int min_prio) 876coro_deq (pTHX_ int min_prio)
866{ 877{
867 int prio = PRIO_MAX - PRIO_MIN; 878 int prio = PRIO_MAX - PRIO_MIN;
868 879
869 min_prio -= PRIO_MIN; 880 min_prio -= PRIO_MIN;
870 if (min_prio < 0) 881 if (min_prio < 0)
881} 892}
882 893
883static void 894static void
884api_ready (SV *coro) 895api_ready (SV *coro)
885{ 896{
897 dTHX;
898
886 if (SvROK (coro)) 899 if (SvROK (coro))
887 coro = SvRV (coro); 900 coro = SvRV (coro);
888 901
902 LOCK;
889 coro_enq (SvREFCNT_inc (coro)); 903 coro_enq (aTHX_ SvREFCNT_inc (coro));
904 UNLOCK;
890} 905}
891 906
892static void 907static void
893api_schedule (void) 908api_schedule (void)
894{ 909{
910 dTHX;
911
895 SV *prev, *next; 912 SV *prev, *next;
896 913
914 LOCK;
915
897 prev = SvRV (GvSV (coro_current)); 916 prev = SvRV (GvSV (coro_current));
898 next = coro_deq (PRIO_MIN); 917 next = coro_deq (aTHX_ PRIO_MIN);
899 918
900 if (!next) 919 if (!next)
901 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 920 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
902 921
903 /* free this only after the transfer */ 922 /* free this only after the transfer */
906 925
907 SvRV (GvSV (coro_current)) = next; 926 SvRV (GvSV (coro_current)) = next;
908 927
909 SV_CORO (next, "Coro::schedule"); 928 SV_CORO (next, "Coro::schedule");
910 929
930 UNLOCK;
931
911 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 932 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
912 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 933 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
913} 934}
914 935
915static void 936static void
916api_cede (void) 937api_cede (void)
917{ 938{
939 dTHX;
940
941 LOCK;
918 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 942 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
943 UNLOCK;
919 944
920 api_schedule (); 945 api_schedule ();
921} 946}
922 947
923MODULE = Coro::State PACKAGE = Coro::State 948MODULE = Coro::State PACKAGE = Coro::State
924 949
925PROTOTYPES: ENABLE 950PROTOTYPES: ENABLE
926 951
927BOOT: 952BOOT:
928{ /* {} 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
929 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 958 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
930 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 959 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
931 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 960 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
932 961
933 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 962 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
934 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 963 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
935 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 964 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
936 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 965 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
937
938 if (!padlist_cache)
939 padlist_cache = newHV ();
940 966
941 main_mainstack = PL_mainstack; 967 main_mainstack = PL_mainstack;
942 968
943 coroapi.ver = CORO_API_VERSION; 969 coroapi.ver = CORO_API_VERSION;
944 coroapi.transfer = api_transfer; 970 coroapi.transfer = api_transfer;
955 croak ("Coro::State::_newprocess expects an arrayref"); 981 croak ("Coro::State::_newprocess expects an arrayref");
956 982
957 Newz (0, coro, 1, struct coro); 983 Newz (0, coro, 1, struct coro);
958 984
959 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 985 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
960 coro->mainstack = 0; /* actual work is done inside transfer */ 986 /*coro->mainstack = 0; *//*actual work is done inside transfer */
961 coro->stack = 0; 987 /*coro->stack = 0;*/
962 988
963 /* same as JMPENV_BOOTSTRAP */ 989 /* same as JMPENV_BOOTSTRAP */
964 /* 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 */
965 //Zero(&coro->start_env, 1, JMPENV); 991 /*Zero(&coro->start_env, 1, JMPENV);*/
966 coro->start_env.je_ret = -1; 992 coro->start_env.je_ret = -1;
967 coro->start_env.je_mustcatch = TRUE; 993 coro->start_env.je_mustcatch = TRUE;
968 994
969 RETVAL = coro; 995 RETVAL = coro;
970 OUTPUT: 996 OUTPUT:
991 if (coro->mainstack && coro->mainstack != main_mainstack) 1017 if (coro->mainstack && coro->mainstack != main_mainstack)
992 { 1018 {
993 struct coro temp; 1019 struct coro temp;
994 1020
995 PUTBACK; 1021 PUTBACK;
996 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1022 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
997 LOAD(aTHX_ coro); 1023 LOAD (aTHX_ coro);
998 SPAGAIN; 1024 SPAGAIN;
999 1025
1000 destroy_stacks (aTHX); 1026 destroy_stacks (aTHX);
1001 1027
1002 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1028 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1003 SPAGAIN; 1029 SPAGAIN;
1004 1030
1005 coro->mainstack = 0; 1031 coro->mainstack = 0;
1006 } 1032 }
1007 1033
1008 deallocate_stack (coro); 1034 deallocate_stack (coro);
1009 SvREFCNT_dec (coro->args); 1035 SvREFCNT_dec (coro->args);
1010 Safefree (coro); 1036 Safefree (coro);
1011
1012void
1013flush()
1014 CODE:
1015#ifdef MAY_FLUSH
1016 flush_padlist_cache ();
1017#endif
1018 1037
1019void 1038void
1020_exit(code) 1039_exit(code)
1021 int code 1040 int code
1022 PROTOTYPE: $ 1041 PROTOTYPE: $
1023 CODE: 1042 CODE:
1024#if defined(__GLIBC__) || _POSIX_C_SOURCE
1025 _exit (code); 1043 _exit (code);
1026#else
1027 signal (SIGTERM, SIG_DFL);
1028 raise (SIGTERM);
1029 exit (code);
1030#endif
1031 1044
1032MODULE = Coro::State PACKAGE = Coro::Cont 1045MODULE = Coro::State PACKAGE = Coro::Cont
1033 1046
1034# this is slightly dirty (should expose a c-level api) 1047# this is slightly dirty (should expose a c-level api)
1035 1048
1049 av_clear (defav); 1062 av_clear (defav);
1050 av_fill (defav, items - 1); 1063 av_fill (defav, items - 1);
1051 while (items--) 1064 while (items--)
1052 av_store (defav, items, SvREFCNT_inc (ST(items))); 1065 av_store (defav, items, SvREFCNT_inc (ST(items)));
1053 1066
1054 mg_get (returnstk); /* isn't documentation wrong for mg_get? */ 1067 SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */
1055 sv = av_pop ((AV *)SvRV (returnstk)); 1068 sv = av_pop ((AV *)SvRV (returnstk));
1056 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)));
1057 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)));
1058 SvREFCNT_dec (sv); 1071 SvREFCNT_dec (sv);
1059 1072
1060 transfer(aTHX_ prev, next, 0); 1073 transfer (aTHX_ prev, next, 0);
1061 1074
1062MODULE = Coro::State PACKAGE = Coro 1075MODULE = Coro::State PACKAGE = Coro
1063 1076
1064# this is slightly dirty (should expose a c-level api) 1077# this is slightly dirty (should expose a c-level api)
1065 1078
1094 sv_setiv(sv, (IV)&coroapi); 1107 sv_setiv(sv, (IV)&coroapi);
1095 SvREADONLY_on(sv); 1108 SvREADONLY_on(sv);
1096 } 1109 }
1097} 1110}
1098 1111
1112#if !PERL_MICRO
1113
1099void 1114void
1100ready(self) 1115ready(self)
1101 SV * self 1116 SV * self
1102 PROTOTYPE: $ 1117 PROTOTYPE: $
1103 CODE: 1118 CODE:
1104 api_ready (self); 1119 api_ready (self);
1120
1121#endif
1105 1122
1106int 1123int
1107nready(...) 1124nready(...)
1108 PROTOTYPE: 1125 PROTOTYPE:
1109 CODE: 1126 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines