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.143 by root, Wed Feb 28 11:43:03 2007 UTC vs.
Revision 1.147 by root, Mon Mar 19 15:50:48 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2
3#define PERL_NO_GET_CONTEXT
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
124 I32 laststype; 126 I32 laststype;
125 int laststatval; 127 int laststatval;
126 Stat_t statcache; 128 Stat_t statcache;
127}; 129};
128 130
131static size_t coro_stacksize = CORO_STACKSIZE;
129static struct CoroAPI coroapi; 132static struct CoroAPI coroapi;
130static AV *main_mainstack; /* used to differentiate between $main and others */ 133static AV *main_mainstack; /* used to differentiate between $main and others */
131static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
132static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
133 136
138typedef struct coro_cctx { 141typedef struct coro_cctx {
139 struct coro_cctx *next; 142 struct coro_cctx *next;
140 143
141 /* the stack */ 144 /* the stack */
142 void *sptr; 145 void *sptr;
143 ssize_t ssize; /* positive == mmap, otherwise malloc */ 146 size_t ssize;
144 147
145 /* cpu state */ 148 /* cpu state */
146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 149 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 150 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
148 JMPENV *top_env; 151 JMPENV *top_env;
149 coro_context cctx; 152 coro_context cctx;
150 153
151 int inuse;
152
153#if CORO_USE_VALGRIND 154#if CORO_USE_VALGRIND
154 int valgrind_id; 155 int valgrind_id;
155#endif 156#endif
157 char inuse, mapped;
156} coro_cctx; 158} coro_cctx;
157 159
158enum { 160enum {
159 CF_RUNNING = 0x0001, /* coroutine is running */ 161 CF_RUNNING = 0x0001, /* coroutine is running */
160 CF_READY = 0x0002, /* coroutine is ready */ 162 CF_READY = 0x0002, /* coroutine is ready */
175 177
176 /* optionally saved, might be zero */ 178 /* optionally saved, might be zero */
177 AV *defav; /* @_ */ 179 AV *defav; /* @_ */
178 SV *defsv; /* $_ */ 180 SV *defsv; /* $_ */
179 SV *errsv; /* $@ */ 181 SV *errsv; /* $@ */
182 GV *deffh; /* default filehandle */
180 SV *irssv; /* $/ */ 183 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */ 184 SV *irssv_sv; /* real $/ cache */
182 185
183#define VAR(name,type) type name; 186#define VAR(name,type) type name;
184# include "state.h" 187# include "state.h"
206static int coro_nready; 209static int coro_nready;
207 210
208/** lowlevel stuff **********************************************************/ 211/** lowlevel stuff **********************************************************/
209 212
210static AV * 213static AV *
211coro_clone_padlist (CV *cv) 214coro_clone_padlist (pTHX_ CV *cv)
212{ 215{
213 AV *padlist = CvPADLIST (cv); 216 AV *padlist = CvPADLIST (cv);
214 AV *newpadlist, *newpad; 217 AV *newpadlist, *newpad;
215 218
216 newpadlist = newAV (); 219 newpadlist = newAV ();
228 231
229 return newpadlist; 232 return newpadlist;
230} 233}
231 234
232static void 235static void
233free_padlist (AV *padlist) 236free_padlist (pTHX_ AV *padlist)
234{ 237{
235 /* may be during global destruction */ 238 /* may be during global destruction */
236 if (SvREFCNT (padlist)) 239 if (SvREFCNT (padlist))
237 { 240 {
238 I32 i = AvFILLp (padlist); 241 I32 i = AvFILLp (padlist);
259 AV *padlist; 262 AV *padlist;
260 AV *av = (AV *)mg->mg_obj; 263 AV *av = (AV *)mg->mg_obj;
261 264
262 /* casting is fun. */ 265 /* casting is fun. */
263 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 266 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
264 free_padlist (padlist); 267 free_padlist (aTHX_ padlist);
265 268
266 SvREFCNT_dec (av); 269 SvREFCNT_dec (av);
267 270
268 return 0; 271 return 0;
269} 272}
279 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 282 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
280 : 0 283 : 0
281 284
282/* the next two functions merely cache the padlists */ 285/* the next two functions merely cache the padlists */
283static void 286static void
284get_padlist (CV *cv) 287get_padlist (pTHX_ CV *cv)
285{ 288{
286 MAGIC *mg = CORO_MAGIC (cv); 289 MAGIC *mg = CORO_MAGIC (cv);
287 AV *av; 290 AV *av;
288 291
289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 292 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
295 CV *cp = Perl_cv_clone (cv); 298 CV *cp = Perl_cv_clone (cv);
296 CvPADLIST (cv) = CvPADLIST (cp); 299 CvPADLIST (cv) = CvPADLIST (cp);
297 CvPADLIST (cp) = 0; 300 CvPADLIST (cp) = 0;
298 SvREFCNT_dec (cp); 301 SvREFCNT_dec (cp);
299#else 302#else
300 CvPADLIST (cv) = coro_clone_padlist (cv); 303 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
301#endif 304#endif
302 } 305 }
303} 306}
304 307
305static void 308static void
306put_padlist (CV *cv) 309put_padlist (pTHX_ CV *cv)
307{ 310{
308 MAGIC *mg = CORO_MAGIC (cv); 311 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av; 312 AV *av;
310 313
311 if (!mg) 314 if (!mg)
330#define SE } while (0) 333#define SE } while (0)
331 334
332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 335#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
333 336
334static void 337static void
335load_perl (Coro__State c) 338load_perl (pTHX_ Coro__State c)
336{ 339{
337#define VAR(name,type) PL_ ## name = c->name; 340#define VAR(name,type) PL_ ## name = c->name;
338# include "state.h" 341# include "state.h"
339#undef VAR 342#undef VAR
340 343
341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 344 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 345 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 346 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
347 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
348
344 if (c->irssv) 349 if (c->irssv)
345 { 350 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 351 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
352 {
347 SvREFCNT_dec (c->irssv); 353 SvREFCNT_dec (c->irssv);
354 c->irssv = 0;
355 }
348 else 356 else
349 { 357 {
350 REPLACE_SV (PL_rs, c->irssv); 358 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 359 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs); 360 sv_setsv (c->irssv_sv, PL_rs);
358 CV *cv; 366 CV *cv;
359 367
360 /* now do the ugly restore mess */ 368 /* now do the ugly restore mess */
361 while ((cv = (CV *)POPs)) 369 while ((cv = (CV *)POPs))
362 { 370 {
363 put_padlist (cv); /* mark this padlist as available */ 371 put_padlist (aTHX_ cv); /* mark this padlist as available */
364 CvDEPTH (cv) = PTR2IV (POPs); 372 CvDEPTH (cv) = PTR2IV (POPs);
365 CvPADLIST (cv) = (AV *)POPs; 373 CvPADLIST (cv) = (AV *)POPs;
366 } 374 }
367 375
368 PUTBACK; 376 PUTBACK;
369 } 377 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D 378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
371} 379}
372 380
373static void 381static void
374save_perl (Coro__State c) 382save_perl (pTHX_ Coro__State c)
375{ 383{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D 384 assert (!PL_comppad || AvARRAY (PL_comppad));//D
377 { 385 {
378 dSP; 386 dSP;
379 I32 cxix = cxstack_ix; 387 I32 cxix = cxstack_ix;
404 PUSHs ((SV *)CvPADLIST (cv)); 412 PUSHs ((SV *)CvPADLIST (cv));
405 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 413 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
406 PUSHs ((SV *)cv); 414 PUSHs ((SV *)cv);
407 415
408 CvDEPTH (cv) = 0; 416 CvDEPTH (cv) = 0;
409 get_padlist (cv); 417 get_padlist (aTHX_ cv);
410 } 418 }
411 } 419 }
412 } 420 }
413 421
414 if (top_si->si_type == PERLSI_MAIN) 422 if (top_si->si_type == PERLSI_MAIN)
423 } 431 }
424 432
425 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 433 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
426 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 434 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
427 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 435 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
436 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
428 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 437 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
429 438
430#define VAR(name,type)c->name = PL_ ## name; 439#define VAR(name,type)c->name = PL_ ## name;
431# include "state.h" 440# include "state.h"
432#undef VAR 441#undef VAR
440 */ 449 */
441#if CORO_PREFER_PERL_FUNCTIONS 450#if CORO_PREFER_PERL_FUNCTIONS
442# define coro_init_stacks init_stacks 451# define coro_init_stacks init_stacks
443#else 452#else
444static void 453static void
445coro_init_stacks () 454coro_init_stacks (pTHX)
446{ 455{
447 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 456 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
448 PL_curstackinfo->si_type = PERLSI_MAIN; 457 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 458 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 459 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 493
485/* 494/*
486 * destroy the stacks, the callchain etc... 495 * destroy the stacks, the callchain etc...
487 */ 496 */
488static void 497static void
489coro_destroy_stacks () 498coro_destroy_stacks (pTHX)
490{ 499{
491 if (!IN_DESTRUCT) 500 if (!IN_DESTRUCT)
492 { 501 {
493 /* restore all saved variables and stuff */ 502 /* restore all saved variables and stuff */
494 LEAVE_SCOPE (0); 503 LEAVE_SCOPE (0);
530} 539}
531 540
532/** coroutine stack handling ************************************************/ 541/** coroutine stack handling ************************************************/
533 542
534static void 543static void
535setup_coro (struct coro *coro) 544setup_coro (pTHX_ struct coro *coro)
536{ 545{
537 /* 546 /*
538 * emulate part of the perl startup here. 547 * emulate part of the perl startup here.
539 */ 548 */
540
541 coro_init_stacks (); 549 coro_init_stacks (aTHX);
542 550
543 PL_curcop = &PL_compiling; 551 PL_curcop = &PL_compiling;
544 PL_in_eval = EVAL_NULL; 552 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0; 553 PL_comppad = 0;
546 PL_curpm = 0; 554 PL_curpm = 0;
569 577
570 ENTER; /* necessary e.g. for dounwind */ 578 ENTER; /* necessary e.g. for dounwind */
571} 579}
572 580
573static void 581static void
574free_coro_mortal () 582free_coro_mortal (pTHX)
575{ 583{
576 if (coro_mortal) 584 if (coro_mortal)
577 { 585 {
578 SvREFCNT_dec (coro_mortal); 586 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 587 coro_mortal = 0;
580 } 588 }
581} 589}
582 590
583/* inject a fake call to Coro::State::_cctx_init into the execution */ 591/* inject a fake call to Coro::State::_cctx_init into the execution */
584static void NOINLINE 592static void NOINLINE
585prepare_cctx (coro_cctx *cctx) 593prepare_cctx (pTHX_ coro_cctx *cctx)
586{ 594{
587 dSP; 595 dSP;
588 LOGOP myop; 596 LOGOP myop;
589 597
590 Zero (&myop, 1, LOGOP); 598 Zero (&myop, 1, LOGOP);
602} 610}
603 611
604static void 612static void
605coro_run (void *arg) 613coro_run (void *arg)
606{ 614{
615 dTHX;
616
607 /* coro_run is the alternative tail of transfer(), so unlock here. */ 617 /* coro_run is the alternative tail of transfer(), so unlock here. */
608 UNLOCK; 618 UNLOCK;
609 619
610 /* 620 /*
611 * this is a _very_ stripped down perl interpreter ;) 621 * this is a _very_ stripped down perl interpreter ;)
612 */ 622 */
613 PL_top_env = &PL_start_env; 623 PL_top_env = &PL_start_env;
614 624
615 /* inject call to cctx_init */ 625 /* inject call to cctx_init */
616 prepare_cctx ((coro_cctx *)arg); 626 prepare_cctx (aTHX_ (coro_cctx *)arg);
617 627
618 /* somebody will hit me for both perl_run and PL_restartop */ 628 /* somebody will hit me for both perl_run and PL_restartop */
619 PL_restartop = PL_op; 629 PL_restartop = PL_op;
620 perl_run (PL_curinterp); 630 perl_run (PL_curinterp);
621 631
625 635
626static coro_cctx * 636static coro_cctx *
627cctx_new () 637cctx_new ()
628{ 638{
629 coro_cctx *cctx; 639 coro_cctx *cctx;
640 void *stack_start;
641 size_t stack_size;
630 642
631 ++cctx_count; 643 ++cctx_count;
632 644
633 Newz (0, cctx, 1, coro_cctx); 645 Newz (0, cctx, 1, coro_cctx);
634 646
635#if HAVE_MMAP 647#if HAVE_MMAP
636 648
637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 649 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
638 /* mmap supposedly does allocate-on-write for us */ 650 /* mmap supposedly does allocate-on-write for us */
639 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 651 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
640 652
641 if (cctx->sptr != (void *)-1) 653 if (cctx->sptr != (void *)-1)
642 { 654 {
643# if CORO_STACKGUARD 655# if CORO_STACKGUARD
644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 656 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
645# endif 657# endif
646 REGISTER_STACK (
647 cctx,
648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr, 658 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
649 cctx->ssize + (char *)cctx->sptr 659 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
650 ); 660 cctx->mapped = 1;
651
652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 } 661 }
654 else 662 else
655#endif 663#endif
656 { 664 {
657 cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long); 665 cctx->ssize = coro_stacksize * (long)sizeof (long);
658 New (0, cctx->sptr, CORO_STACKSIZE, long); 666 New (0, cctx->sptr, coro_stacksize, long);
659 667
660 if (!cctx->sptr) 668 if (!cctx->sptr)
661 { 669 {
662 perror ("FATAL: unable to allocate stack for coroutine"); 670 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE); 671 _exit (EXIT_FAILURE);
664 } 672 }
665 673
666 REGISTER_STACK ( 674 stack_start = cctx->sptr;
667 cctx, 675 stack_size = cctx->ssize;
668 (char *)cctx->sptr, 676 }
669 (char *)cctx->sptr - cctx->ssize
670 );
671 677
678 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
672 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); 679 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size);
673 }
674 680
675 return cctx; 681 return cctx;
676} 682}
677 683
678static void 684static void
686#if CORO_USE_VALGRIND 692#if CORO_USE_VALGRIND
687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 693 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
688#endif 694#endif
689 695
690#if HAVE_MMAP 696#if HAVE_MMAP
691 if (cctx->ssize > 0) 697 if (cctx->mapped)
692 munmap (cctx->sptr, cctx->ssize); 698 munmap (cctx->sptr, cctx->ssize);
693 else 699 else
694#endif 700#endif
695 Safefree (cctx->sptr); 701 Safefree (cctx->sptr);
696 702
697 Safefree (cctx); 703 Safefree (cctx);
698} 704}
699 705
700static coro_cctx * 706static coro_cctx *
701cctx_get () 707cctx_get (pTHX)
702{ 708{
703 coro_cctx *cctx;
704
705 if (cctx_first) 709 while (cctx_first)
706 { 710 {
707 cctx = cctx_first; 711 coro_cctx *cctx = cctx_first;
708 cctx_first = cctx->next; 712 cctx_first = cctx->next;
709 --cctx_idle; 713 --cctx_idle;
714
715 if (cctx->ssize >= coro_stacksize)
716 return cctx;
717
718 cctx_destroy (cctx);
710 } 719 }
711 else 720
712 {
713 cctx = cctx_new ();
714 PL_op = PL_op->op_next; 721 PL_op = PL_op->op_next;
715 }
716
717 return cctx; 722 return cctx_new ();
718} 723}
719 724
720static void 725static void
721cctx_put (coro_cctx *cctx) 726cctx_put (coro_cctx *cctx)
722{ 727{
738 743
739/** coroutine switching *****************************************************/ 744/** coroutine switching *****************************************************/
740 745
741/* never call directly, always through the coro_state_transfer global variable */ 746/* never call directly, always through the coro_state_transfer global variable */
742static void NOINLINE 747static void NOINLINE
743transfer (struct coro *prev, struct coro *next) 748transfer (pTHX_ struct coro *prev, struct coro *next)
744{ 749{
745 dSTACKLEVEL; 750 dSTACKLEVEL;
746 751
747 /* sometimes transfer is only called to set idle_sp */ 752 /* sometimes transfer is only called to set idle_sp */
748 if (!next) 753 if (!next)
781 if (next->flags & CF_NEW) 786 if (next->flags & CF_NEW)
782 { 787 {
783 /* need to start coroutine */ 788 /* need to start coroutine */
784 next->flags &= ~CF_NEW; 789 next->flags &= ~CF_NEW;
785 /* first get rid of the old state */ 790 /* first get rid of the old state */
786 save_perl (prev); 791 save_perl (aTHX_ prev);
787 /* setup coroutine call */ 792 /* setup coroutine call */
788 setup_coro (next); 793 setup_coro (aTHX_ next);
789 /* need a new stack */ 794 /* need a new stack */
790 assert (!next->cctx); 795 assert (!next->cctx);
791 } 796 }
792 else 797 else
793 { 798 {
794 /* coroutine already started */ 799 /* coroutine already started */
795 save_perl (prev); 800 save_perl (aTHX_ prev);
796 load_perl (next); 801 load_perl (aTHX_ next);
797 } 802 }
798 803
799 prev__cctx = prev->cctx; 804 prev__cctx = prev->cctx;
800 805
801 /* possibly "free" the cctx */ 806 /* possibly "free" the cctx */
810 prev__cctx->inuse = 0; 815 prev__cctx->inuse = 0;
811 } 816 }
812 817
813 if (!next->cctx) 818 if (!next->cctx)
814 { 819 {
815 next->cctx = cctx_get (); 820 next->cctx = cctx_get (aTHX);
816 assert (!next->cctx->inuse); 821 assert (!next->cctx->inuse);
817 next->cctx->inuse = 1; 822 next->cctx->inuse = 1;
818 } 823 }
819 824
820 if (prev__cctx != next->cctx) 825 if (prev__cctx != next->cctx)
822 prev__cctx->top_env = PL_top_env; 827 prev__cctx->top_env = PL_top_env;
823 PL_top_env = next->cctx->top_env; 828 PL_top_env = next->cctx->top_env;
824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 829 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
825 } 830 }
826 831
827 free_coro_mortal (); 832 free_coro_mortal (aTHX);
828 UNLOCK; 833 UNLOCK;
829 } 834 }
830} 835}
831 836
832struct transfer_args 837struct transfer_args
833{ 838{
834 struct coro *prev, *next; 839 struct coro *prev, *next;
835}; 840};
836 841
837#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 842#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
838 843
839/** high level stuff ********************************************************/ 844/** high level stuff ********************************************************/
840 845
841static int 846static int
842coro_state_destroy (struct coro *coro) 847coro_state_destroy (pTHX_ struct coro *coro)
843{ 848{
844 if (coro->flags & CF_DESTROYED) 849 if (coro->flags & CF_DESTROYED)
845 return 0; 850 return 0;
846 851
847 coro->flags |= CF_DESTROYED; 852 coro->flags |= CF_DESTROYED;
862 struct coro temp; 867 struct coro temp;
863 868
864 assert (!(coro->flags & CF_RUNNING)); 869 assert (!(coro->flags & CF_RUNNING));
865 870
866 Zero (&temp, 1, struct coro); 871 Zero (&temp, 1, struct coro);
867 temp.save = CORO_SAVE_ALL; 872 temp.save = CORO_SAVE_DEF;
868 873
869 if (coro->flags & CF_RUNNING) 874 if (coro->flags & CF_RUNNING)
870 croak ("FATAL: tried to destroy currently running coroutine"); 875 croak ("FATAL: tried to destroy currently running coroutine");
871 876
872 save_perl (&temp); 877 save_perl (aTHX_ &temp);
873 load_perl (coro); 878 load_perl (aTHX_ coro);
874 879
875 coro_destroy_stacks (); 880 coro_destroy_stacks (aTHX);
876 881
877 load_perl (&temp); /* this will get rid of defsv etc.. */ 882 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
878 883
879 coro->mainstack = 0; 884 coro->mainstack = 0;
880 } 885 }
881 886
882 cctx_destroy (coro->cctx); 887 cctx_destroy (coro->cctx);
891 struct coro *coro = (struct coro *)mg->mg_ptr; 896 struct coro *coro = (struct coro *)mg->mg_ptr;
892 mg->mg_ptr = 0; 897 mg->mg_ptr = 0;
893 898
894 if (--coro->refcnt < 0) 899 if (--coro->refcnt < 0)
895 { 900 {
896 coro_state_destroy (coro); 901 coro_state_destroy (aTHX_ coro);
897 Safefree (coro); 902 Safefree (coro);
898 } 903 }
899 904
900 return 0; 905 return 0;
901} 906}
920# define MGf_DUP 0 925# define MGf_DUP 0
921#endif 926#endif
922}; 927};
923 928
924static struct coro * 929static struct coro *
925SvSTATE (SV *coro) 930SvSTATE_ (pTHX_ SV *coro)
926{ 931{
927 HV *stash; 932 HV *stash;
928 MAGIC *mg; 933 MAGIC *mg;
929 934
930 if (SvROK (coro)) 935 if (SvROK (coro))
936 /* very slow, but rare, check */ 941 /* very slow, but rare, check */
937 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 942 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
938 croak ("Coro::State object required"); 943 croak ("Coro::State object required");
939 } 944 }
940 945
941 mg = SvMAGIC (coro); 946 mg = CORO_MAGIC (coro);
942 assert (mg->mg_type == PERL_MAGIC_ext);
943 return (struct coro *)mg->mg_ptr; 947 return (struct coro *)mg->mg_ptr;
944} 948}
945 949
950#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
951
946static void 952static void
947prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 953prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
948{ 954{
949 ta->prev = SvSTATE (prev_sv); 955 ta->prev = SvSTATE (prev_sv);
950 ta->next = SvSTATE (next_sv); 956 ta->next = SvSTATE (next_sv);
951} 957}
952 958
953static void 959static void
954api_transfer (SV *prev_sv, SV *next_sv) 960api_transfer (SV *prev_sv, SV *next_sv)
955{ 961{
962 dTHX;
956 struct transfer_args ta; 963 struct transfer_args ta;
957 964
958 prepare_transfer (&ta, prev_sv, next_sv); 965 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
959 TRANSFER (ta); 966 TRANSFER (ta);
960} 967}
961 968
962static int 969static int
963api_save (SV *coro_sv, int new_save) 970api_save (SV *coro_sv, int new_save)
964{ 971{
972 dTHX;
965 struct coro *coro = SvSTATE (coro_sv); 973 struct coro *coro = SvSTATE (coro_sv);
966 int old_save = coro->save; 974 int old_save = coro->save;
967 975
968 if (new_save >= 0) 976 if (new_save >= 0)
969 coro->save = new_save; 977 coro->save = new_save;
972} 980}
973 981
974/** Coro ********************************************************************/ 982/** Coro ********************************************************************/
975 983
976static void 984static void
977coro_enq (SV *coro_sv) 985coro_enq (pTHX_ SV *coro_sv)
978{ 986{
979 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 987 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
980} 988}
981 989
982static SV * 990static SV *
983coro_deq (int min_prio) 991coro_deq (pTHX_ int min_prio)
984{ 992{
985 int prio = PRIO_MAX - PRIO_MIN; 993 int prio = PRIO_MAX - PRIO_MIN;
986 994
987 min_prio -= PRIO_MIN; 995 min_prio -= PRIO_MIN;
988 if (min_prio < 0) 996 if (min_prio < 0)
996} 1004}
997 1005
998static int 1006static int
999api_ready (SV *coro_sv) 1007api_ready (SV *coro_sv)
1000{ 1008{
1009 dTHX;
1001 struct coro *coro; 1010 struct coro *coro;
1002 1011
1003 if (SvROK (coro_sv)) 1012 if (SvROK (coro_sv))
1004 coro_sv = SvRV (coro_sv); 1013 coro_sv = SvRV (coro_sv);
1005 1014
1009 return 0; 1018 return 0;
1010 1019
1011 coro->flags |= CF_READY; 1020 coro->flags |= CF_READY;
1012 1021
1013 LOCK; 1022 LOCK;
1014 coro_enq (SvREFCNT_inc (coro_sv)); 1023 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1015 ++coro_nready; 1024 ++coro_nready;
1016 UNLOCK; 1025 UNLOCK;
1017 1026
1018 return 1; 1027 return 1;
1019} 1028}
1020 1029
1021static int 1030static int
1022api_is_ready (SV *coro_sv) 1031api_is_ready (SV *coro_sv)
1023{ 1032{
1033 dTHX;
1024 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1034 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1025} 1035}
1026 1036
1027static void 1037static void
1028prepare_schedule (struct transfer_args *ta) 1038prepare_schedule (pTHX_ struct transfer_args *ta)
1029{ 1039{
1030 SV *prev_sv, *next_sv; 1040 SV *prev_sv, *next_sv;
1031 1041
1032 for (;;) 1042 for (;;)
1033 { 1043 {
1034 LOCK; 1044 LOCK;
1035 next_sv = coro_deq (PRIO_MIN); 1045 next_sv = coro_deq (aTHX_ PRIO_MIN);
1036 1046
1037 /* nothing to schedule: call the idle handler */ 1047 /* nothing to schedule: call the idle handler */
1038 if (!next_sv) 1048 if (!next_sv)
1039 { 1049 {
1040 dSP; 1050 dSP;
1075 1085
1076 assert (ta->next->flags & CF_READY); 1086 assert (ta->next->flags & CF_READY);
1077 ta->next->flags &= ~CF_READY; 1087 ta->next->flags &= ~CF_READY;
1078 1088
1079 LOCK; 1089 LOCK;
1080 free_coro_mortal (); 1090 free_coro_mortal (aTHX);
1081 coro_mortal = prev_sv; 1091 coro_mortal = prev_sv;
1082 UNLOCK; 1092 UNLOCK;
1083} 1093}
1084 1094
1085static void 1095static void
1086prepare_cede (struct transfer_args *ta) 1096prepare_cede (pTHX_ struct transfer_args *ta)
1087{ 1097{
1088 api_ready (coro_current); 1098 api_ready (coro_current);
1089 prepare_schedule (ta); 1099 prepare_schedule (aTHX_ ta);
1090} 1100}
1091 1101
1092static int 1102static int
1093prepare_cede_notself (struct transfer_args *ta) 1103prepare_cede_notself (pTHX_ struct transfer_args *ta)
1094{ 1104{
1095 if (coro_nready) 1105 if (coro_nready)
1096 { 1106 {
1097 SV *prev = SvRV (coro_current); 1107 SV *prev = SvRV (coro_current);
1098 prepare_schedule (ta); 1108 prepare_schedule (aTHX_ ta);
1099 api_ready (prev); 1109 api_ready (prev);
1100 return 1; 1110 return 1;
1101 } 1111 }
1102 else 1112 else
1103 return 0; 1113 return 0;
1104} 1114}
1105 1115
1106static void 1116static void
1107api_schedule (void) 1117api_schedule (void)
1108{ 1118{
1119 dTHX;
1109 struct transfer_args ta; 1120 struct transfer_args ta;
1110 1121
1111 prepare_schedule (&ta); 1122 prepare_schedule (aTHX_ &ta);
1112 TRANSFER (ta); 1123 TRANSFER (ta);
1113} 1124}
1114 1125
1115static int 1126static int
1116api_cede (void) 1127api_cede (void)
1117{ 1128{
1129 dTHX;
1118 struct transfer_args ta; 1130 struct transfer_args ta;
1119 1131
1120 prepare_cede (&ta); 1132 prepare_cede (aTHX_ &ta);
1121 1133
1122 if (ta.prev != ta.next) 1134 if (ta.prev != ta.next)
1123 { 1135 {
1124 TRANSFER (ta); 1136 TRANSFER (ta);
1125 return 1; 1137 return 1;
1129} 1141}
1130 1142
1131static int 1143static int
1132api_cede_notself (void) 1144api_cede_notself (void)
1133{ 1145{
1146 dTHX;
1134 struct transfer_args ta; 1147 struct transfer_args ta;
1135 1148
1136 if (prepare_cede_notself (&ta)) 1149 if (prepare_cede_notself (aTHX_ &ta))
1137 { 1150 {
1138 TRANSFER (ta); 1151 TRANSFER (ta);
1139 return 1; 1152 return 1;
1140 } 1153 }
1141 else 1154 else
1157 1170
1158 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1171 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1159 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1172 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1160 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1173 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1161 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1174 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1175 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1176 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1162 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1177 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1163 1178
1164 main_mainstack = PL_mainstack; 1179 main_mainstack = PL_mainstack;
1165 1180
1166 coroapi.ver = CORO_API_VERSION; 1181 coroapi.ver = CORO_API_VERSION;
1177 HV *hv; 1192 HV *hv;
1178 int i; 1193 int i;
1179 1194
1180 Newz (0, coro, 1, struct coro); 1195 Newz (0, coro, 1, struct coro);
1181 coro->args = newAV (); 1196 coro->args = newAV ();
1182 coro->save = CORO_SAVE_ALL; 1197 coro->save = CORO_SAVE_DEF;
1183 coro->flags = CF_NEW; 1198 coro->flags = CF_NEW;
1184 1199
1185 hv = newHV (); 1200 hv = newHV ();
1186 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1201 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1187 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1202 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1194 1209
1195int 1210int
1196save (SV *coro, int new_save = -1) 1211save (SV *coro, int new_save = -1)
1197 CODE: 1212 CODE:
1198 RETVAL = api_save (coro, new_save); 1213 RETVAL = api_save (coro, new_save);
1214 OUTPUT:
1215 RETVAL
1216
1217int
1218save_also (SV *coro_sv, int save_also)
1219 CODE:
1220{
1221 struct coro *coro = SvSTATE (coro_sv);
1222 RETVAL = coro->save;
1223 coro->save |= save_also;
1224}
1199 OUTPUT: 1225 OUTPUT:
1200 RETVAL 1226 RETVAL
1201 1227
1202void 1228void
1203_set_stacklevel (...) 1229_set_stacklevel (...)
1219 1245
1220 case 1: 1246 case 1:
1221 if (items != 2) 1247 if (items != 2)
1222 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1248 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1223 1249
1224 prepare_transfer (&ta, ST (0), ST (1)); 1250 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1225 break; 1251 break;
1226 1252
1227 case 2: 1253 case 2:
1228 prepare_schedule (&ta); 1254 prepare_schedule (aTHX_ &ta);
1229 break; 1255 break;
1230 1256
1231 case 3: 1257 case 3:
1232 prepare_cede (&ta); 1258 prepare_cede (aTHX_ &ta);
1233 break; 1259 break;
1234 1260
1235 case 4: 1261 case 4:
1236 if (!prepare_cede_notself (&ta)) 1262 if (!prepare_cede_notself (aTHX_ &ta))
1237 XSRETURN_EMPTY; 1263 XSRETURN_EMPTY;
1238 1264
1239 break; 1265 break;
1240 } 1266 }
1241 1267
1247} 1273}
1248 1274
1249bool 1275bool
1250_destroy (SV *coro_sv) 1276_destroy (SV *coro_sv)
1251 CODE: 1277 CODE:
1252 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1278 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1253 OUTPUT: 1279 OUTPUT:
1254 RETVAL 1280 RETVAL
1255 1281
1256void 1282void
1257_exit (code) 1283_exit (code)
1258 int code 1284 int code
1259 PROTOTYPE: $ 1285 PROTOTYPE: $
1260 CODE: 1286 CODE:
1261 _exit (code); 1287 _exit (code);
1288
1289int
1290cctx_stacksize (int new_stacksize = 0)
1291 CODE:
1292 RETVAL = coro_stacksize;
1293 if (new_stacksize)
1294 coro_stacksize = new_stacksize;
1295 OUTPUT:
1296 RETVAL
1262 1297
1263int 1298int
1264cctx_count () 1299cctx_count ()
1265 CODE: 1300 CODE:
1266 RETVAL = cctx_count; 1301 RETVAL = cctx_count;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines