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.139 by root, Sun Jan 14 21:13:41 2007 UTC vs.
Revision 1.146 by root, Sat Mar 17 19:51:57 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
31#else 33#else
32# define PAGESIZE 0 34# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0 35# define BOOT_PAGESIZE (void)0
34#endif 36#endif
35 37
36#if USE_VALGRIND 38#if CORO_USE_VALGRIND
37# include <valgrind/valgrind.h> 39# include <valgrind/valgrind.h>
40# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
41#else
42# define REGISTER_STACK(cctx,start,end)
38#endif 43#endif
39 44
40/* the maximum number of idle cctx that will be pooled */ 45/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8 46#define MAX_IDLE_CCTX 8
42 47
71#ifndef SvRV_set 76#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v) 77# define SvRV_set(s,v) SvRV(s) = (v)
73#endif 78#endif
74 79
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 80#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD 81# undef CORO_STACKGUARD
77#endif 82#endif
78 83
79#ifndef STACKGUARD 84#ifndef CORO_STACKGUARD
80# define STACKGUARD 0 85# define CORO_STACKGUARD 0
81#endif 86#endif
82 87
83/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0 90# define CORO_PREFER_PERL_FUNCTIONS 0
86#endif 91#endif
87 92
88/* The next macro should declare a variable stacklevel that contains and approximation 93/* The next macro should declare a variable stacklevel that contains and approximation
89 * to the current C stack pointer. Its property is that it changes with each call 94 * to the current C stack pointer. Its property is that it changes with each call
90 * and should be unique. */ 95 * and should be unique. */
121 I32 laststype; 126 I32 laststype;
122 int laststatval; 127 int laststatval;
123 Stat_t statcache; 128 Stat_t statcache;
124}; 129};
125 130
131static size_t coro_stacksize = CORO_STACKSIZE;
126static struct CoroAPI coroapi; 132static struct CoroAPI coroapi;
127static AV *main_mainstack; /* used to differentiate between $main and others */ 133static AV *main_mainstack; /* used to differentiate between $main and others */
128static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
129static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
130 136
135typedef struct coro_cctx { 141typedef struct coro_cctx {
136 struct coro_cctx *next; 142 struct coro_cctx *next;
137 143
138 /* the stack */ 144 /* the stack */
139 void *sptr; 145 void *sptr;
140 long ssize; /* positive == mmap, otherwise malloc */ 146 size_t ssize;
141 147
142 /* cpu state */ 148 /* cpu state */
143 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 149 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
144 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 */
145 JMPENV *top_env; 151 JMPENV *top_env;
146 coro_context cctx; 152 coro_context cctx;
147 153
148 int inuse;
149
150#if USE_VALGRIND 154#if CORO_USE_VALGRIND
151 int valgrind_id; 155 int valgrind_id;
152#endif 156#endif
157 char inuse, mapped;
153} coro_cctx; 158} coro_cctx;
154 159
155enum { 160enum {
156 CF_RUNNING = 0x0001, /* coroutine is running */ 161 CF_RUNNING = 0x0001, /* coroutine is running */
157 CF_READY = 0x0002, /* coroutine is ready */ 162 CF_READY = 0x0002, /* coroutine is ready */
203static int coro_nready; 208static int coro_nready;
204 209
205/** lowlevel stuff **********************************************************/ 210/** lowlevel stuff **********************************************************/
206 211
207static AV * 212static AV *
208coro_clone_padlist (CV *cv) 213coro_clone_padlist (pTHX_ CV *cv)
209{ 214{
210 AV *padlist = CvPADLIST (cv); 215 AV *padlist = CvPADLIST (cv);
211 AV *newpadlist, *newpad; 216 AV *newpadlist, *newpad;
212 217
213 newpadlist = newAV (); 218 newpadlist = newAV ();
225 230
226 return newpadlist; 231 return newpadlist;
227} 232}
228 233
229static void 234static void
230free_padlist (AV *padlist) 235free_padlist (pTHX_ AV *padlist)
231{ 236{
232 /* may be during global destruction */ 237 /* may be during global destruction */
233 if (SvREFCNT (padlist)) 238 if (SvREFCNT (padlist))
234 { 239 {
235 I32 i = AvFILLp (padlist); 240 I32 i = AvFILLp (padlist);
256 AV *padlist; 261 AV *padlist;
257 AV *av = (AV *)mg->mg_obj; 262 AV *av = (AV *)mg->mg_obj;
258 263
259 /* casting is fun. */ 264 /* casting is fun. */
260 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 265 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
261 free_padlist (padlist); 266 free_padlist (aTHX_ padlist);
262 267
263 SvREFCNT_dec (av); 268 SvREFCNT_dec (av);
264 269
265 return 0; 270 return 0;
266} 271}
276 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 281 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
277 : 0 282 : 0
278 283
279/* the next two functions merely cache the padlists */ 284/* the next two functions merely cache the padlists */
280static void 285static void
281get_padlist (CV *cv) 286get_padlist (pTHX_ CV *cv)
282{ 287{
283 MAGIC *mg = CORO_MAGIC (cv); 288 MAGIC *mg = CORO_MAGIC (cv);
284 AV *av; 289 AV *av;
285 290
286 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 291 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
287 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 292 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
288 else 293 else
289 { 294 {
290#if PREFER_PERL_FUNCTIONS 295#if CORO_PREFER_PERL_FUNCTIONS
291 /* this is probably cleaner, but also slower? */ 296 /* this is probably cleaner, but also slower? */
292 CV *cp = Perl_cv_clone (cv); 297 CV *cp = Perl_cv_clone (cv);
293 CvPADLIST (cv) = CvPADLIST (cp); 298 CvPADLIST (cv) = CvPADLIST (cp);
294 CvPADLIST (cp) = 0; 299 CvPADLIST (cp) = 0;
295 SvREFCNT_dec (cp); 300 SvREFCNT_dec (cp);
296#else 301#else
297 CvPADLIST (cv) = coro_clone_padlist (cv); 302 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
298#endif 303#endif
299 } 304 }
300} 305}
301 306
302static void 307static void
303put_padlist (CV *cv) 308put_padlist (pTHX_ CV *cv)
304{ 309{
305 MAGIC *mg = CORO_MAGIC (cv); 310 MAGIC *mg = CORO_MAGIC (cv);
306 AV *av; 311 AV *av;
307 312
308 if (!mg) 313 if (!mg)
327#define SE } while (0) 332#define SE } while (0)
328 333
329#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 334#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
330 335
331static void 336static void
332load_perl (Coro__State c) 337load_perl (pTHX_ Coro__State c)
333{ 338{
334#define VAR(name,type) PL_ ## name = c->name; 339#define VAR(name,type) PL_ ## name = c->name;
335# include "state.h" 340# include "state.h"
336#undef VAR 341#undef VAR
337 342
355 CV *cv; 360 CV *cv;
356 361
357 /* now do the ugly restore mess */ 362 /* now do the ugly restore mess */
358 while ((cv = (CV *)POPs)) 363 while ((cv = (CV *)POPs))
359 { 364 {
360 put_padlist (cv); /* mark this padlist as available */ 365 put_padlist (aTHX_ cv); /* mark this padlist as available */
361 CvDEPTH (cv) = PTR2IV (POPs); 366 CvDEPTH (cv) = PTR2IV (POPs);
362 CvPADLIST (cv) = (AV *)POPs; 367 CvPADLIST (cv) = (AV *)POPs;
363 } 368 }
364 369
365 PUTBACK; 370 PUTBACK;
366 } 371 }
372 assert (!PL_comppad || AvARRAY (PL_comppad));//D
367} 373}
368 374
369static void 375static void
370save_perl (Coro__State c) 376save_perl (pTHX_ Coro__State c)
371{ 377{
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
372 { 379 {
373 dSP; 380 dSP;
374 I32 cxix = cxstack_ix; 381 I32 cxix = cxstack_ix;
375 PERL_CONTEXT *ccstk = cxstack; 382 PERL_CONTEXT *ccstk = cxstack;
376 PERL_SI *top_si = PL_curstackinfo; 383 PERL_SI *top_si = PL_curstackinfo;
399 PUSHs ((SV *)CvPADLIST (cv)); 406 PUSHs ((SV *)CvPADLIST (cv));
400 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 407 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
401 PUSHs ((SV *)cv); 408 PUSHs ((SV *)cv);
402 409
403 CvDEPTH (cv) = 0; 410 CvDEPTH (cv) = 0;
404 get_padlist (cv); 411 get_padlist (aTHX_ cv);
405 } 412 }
406 } 413 }
407 } 414 }
408 415
409 if (top_si->si_type == PERLSI_MAIN) 416 if (top_si->si_type == PERLSI_MAIN)
431 * allocate various perl stacks. This is an exact copy 438 * allocate various perl stacks. This is an exact copy
432 * of perl.c:init_stacks, except that it uses less memory 439 * of perl.c:init_stacks, except that it uses less memory
433 * on the (sometimes correct) assumption that coroutines do 440 * on the (sometimes correct) assumption that coroutines do
434 * not usually need a lot of stackspace. 441 * not usually need a lot of stackspace.
435 */ 442 */
436#if PREFER_PERL_FUNCTIONS 443#if CORO_PREFER_PERL_FUNCTIONS
437# define coro_init_stacks init_stacks 444# define coro_init_stacks init_stacks
438#else 445#else
439static void 446static void
440coro_init_stacks () 447coro_init_stacks (pTHX)
441{ 448{
442 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 449 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
443 PL_curstackinfo->si_type = PERLSI_MAIN; 450 PL_curstackinfo->si_type = PERLSI_MAIN;
444 PL_curstack = PL_curstackinfo->si_stack; 451 PL_curstack = PL_curstackinfo->si_stack;
445 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 452 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
479 486
480/* 487/*
481 * destroy the stacks, the callchain etc... 488 * destroy the stacks, the callchain etc...
482 */ 489 */
483static void 490static void
484coro_destroy_stacks () 491coro_destroy_stacks (pTHX)
485{ 492{
486 if (!IN_DESTRUCT) 493 if (!IN_DESTRUCT)
487 { 494 {
488 /* restore all saved variables and stuff */ 495 /* restore all saved variables and stuff */
489 LEAVE_SCOPE (0); 496 LEAVE_SCOPE (0);
525} 532}
526 533
527/** coroutine stack handling ************************************************/ 534/** coroutine stack handling ************************************************/
528 535
529static void 536static void
530setup_coro (struct coro *coro) 537setup_coro (pTHX_ struct coro *coro)
531{ 538{
532 /* 539 /*
533 * emulate part of the perl startup here. 540 * emulate part of the perl startup here.
534 */ 541 */
535
536 coro_init_stacks (); 542 coro_init_stacks (aTHX);
537 543
538 PL_curcop = &PL_compiling; 544 PL_curcop = &PL_compiling;
539 PL_in_eval = EVAL_NULL; 545 PL_in_eval = EVAL_NULL;
546 PL_comppad = 0;
540 PL_curpm = 0; 547 PL_curpm = 0;
541 PL_localizing = 0; 548 PL_localizing = 0;
542 PL_dirty = 0; 549 PL_dirty = 0;
543 PL_restartop = 0; 550 PL_restartop = 0;
544 551
563 570
564 ENTER; /* necessary e.g. for dounwind */ 571 ENTER; /* necessary e.g. for dounwind */
565} 572}
566 573
567static void 574static void
568free_coro_mortal () 575free_coro_mortal (pTHX)
569{ 576{
570 if (coro_mortal) 577 if (coro_mortal)
571 { 578 {
572 SvREFCNT_dec (coro_mortal); 579 SvREFCNT_dec (coro_mortal);
573 coro_mortal = 0; 580 coro_mortal = 0;
574 } 581 }
575} 582}
576 583
577/* inject a fake call to Coro::State::_cctx_init into the execution */ 584/* inject a fake call to Coro::State::_cctx_init into the execution */
578static void NOINLINE 585static void NOINLINE
579prepare_cctx (coro_cctx *cctx) 586prepare_cctx (pTHX_ coro_cctx *cctx)
580{ 587{
581 dSP; 588 dSP;
582 LOGOP myop; 589 LOGOP myop;
583 590
584 Zero (&myop, 1, LOGOP); 591 Zero (&myop, 1, LOGOP);
596} 603}
597 604
598static void 605static void
599coro_run (void *arg) 606coro_run (void *arg)
600{ 607{
608 dTHX;
609
601 /* coro_run is the alternative tail of transfer(), so unlock here. */ 610 /* coro_run is the alternative tail of transfer(), so unlock here. */
602 UNLOCK; 611 UNLOCK;
603 612
604 /* 613 /*
605 * this is a _very_ stripped down perl interpreter ;) 614 * this is a _very_ stripped down perl interpreter ;)
606 */ 615 */
607 PL_top_env = &PL_start_env; 616 PL_top_env = &PL_start_env;
608 617
609 /* inject call to cctx_init */ 618 /* inject call to cctx_init */
610 prepare_cctx ((coro_cctx *)arg); 619 prepare_cctx (aTHX_ (coro_cctx *)arg);
611 620
612 /* somebody will hit me for both perl_run and PL_restartop */ 621 /* somebody will hit me for both perl_run and PL_restartop */
613 PL_restartop = PL_op; 622 PL_restartop = PL_op;
614 perl_run (PL_curinterp); 623 perl_run (PL_curinterp);
615 624
619 628
620static coro_cctx * 629static coro_cctx *
621cctx_new () 630cctx_new ()
622{ 631{
623 coro_cctx *cctx; 632 coro_cctx *cctx;
633 void *stack_start;
634 size_t stack_size;
624 635
625 ++cctx_count; 636 ++cctx_count;
626 637
627 Newz (0, cctx, 1, coro_cctx); 638 Newz (0, cctx, 1, coro_cctx);
628 639
629#if HAVE_MMAP 640#if HAVE_MMAP
630 641
631 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 642 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
632 /* mmap supposedly does allocate-on-write for us */ 643 /* mmap supposedly does allocate-on-write for us */
633 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 644 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
634 645
635 if (cctx->sptr == (void *)-1) 646 if (cctx->sptr != (void *)-1)
636 {
637 perror ("FATAL: unable to mmap stack for coroutine");
638 _exit (EXIT_FAILURE);
639 } 647 {
640
641# if STACKGUARD 648# if CORO_STACKGUARD
642 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 649 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
643# endif 650# endif
651 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
652 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
653 cctx->mapped = 1;
654 }
655 else
656#endif
657 {
658 cctx->ssize = coro_stacksize * (long)sizeof (long);
659 New (0, cctx->sptr, coro_stacksize, long);
644 660
645#else
646
647 cctx->ssize = STACKSIZE * (long)sizeof (long);
648 New (0, cctx->sptr, STACKSIZE, long);
649
650 if (!cctx->sptr) 661 if (!cctx->sptr)
651 { 662 {
652 perror ("FATAL: unable to malloc stack for coroutine"); 663 perror ("FATAL: unable to allocate stack for coroutine");
653 _exit (EXIT_FAILURE); 664 _exit (EXIT_FAILURE);
654 } 665 }
655 666
656#endif 667 stack_start = cctx->sptr;
668 stack_size = cctx->ssize;
669 }
657 670
658#if USE_VALGRIND 671 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
659 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
660 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
661 cctx->ssize + (char *)cctx->sptr
662 );
663#endif
664
665 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 672 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size);
666 673
667 return cctx; 674 return cctx;
668} 675}
669 676
670static void 677static void
673 if (!cctx) 680 if (!cctx)
674 return; 681 return;
675 682
676 --cctx_count; 683 --cctx_count;
677 684
678#if USE_VALGRIND 685#if CORO_USE_VALGRIND
679 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 686 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
680#endif 687#endif
681 688
682#if HAVE_MMAP 689#if HAVE_MMAP
690 if (cctx->mapped)
683 munmap (cctx->sptr, cctx->ssize); 691 munmap (cctx->sptr, cctx->ssize);
684#else 692 else
693#endif
685 Safefree (cctx->sptr); 694 Safefree (cctx->sptr);
686#endif
687 695
688 Safefree (cctx); 696 Safefree (cctx);
689} 697}
690 698
691static coro_cctx * 699static coro_cctx *
692cctx_get () 700cctx_get (pTHX)
693{ 701{
694 coro_cctx *cctx;
695
696 if (cctx_first) 702 while (cctx_first)
697 { 703 {
698 cctx = cctx_first; 704 coro_cctx *cctx = cctx_first;
699 cctx_first = cctx->next; 705 cctx_first = cctx->next;
700 --cctx_idle; 706 --cctx_idle;
707
708 if (cctx->ssize >= coro_stacksize)
709 return cctx;
710
711 cctx_destroy (cctx);
701 } 712 }
702 else 713
703 {
704 cctx = cctx_new ();
705 PL_op = PL_op->op_next; 714 PL_op = PL_op->op_next;
706 }
707
708 return cctx; 715 return cctx_new ();
709} 716}
710 717
711static void 718static void
712cctx_put (coro_cctx *cctx) 719cctx_put (coro_cctx *cctx)
713{ 720{
729 736
730/** coroutine switching *****************************************************/ 737/** coroutine switching *****************************************************/
731 738
732/* never call directly, always through the coro_state_transfer global variable */ 739/* never call directly, always through the coro_state_transfer global variable */
733static void NOINLINE 740static void NOINLINE
734transfer (struct coro *prev, struct coro *next) 741transfer (pTHX_ struct coro *prev, struct coro *next)
735{ 742{
736 dSTACKLEVEL; 743 dSTACKLEVEL;
737 744
738 /* sometimes transfer is only called to set idle_sp */ 745 /* sometimes transfer is only called to set idle_sp */
739 if (!next) 746 if (!next)
772 if (next->flags & CF_NEW) 779 if (next->flags & CF_NEW)
773 { 780 {
774 /* need to start coroutine */ 781 /* need to start coroutine */
775 next->flags &= ~CF_NEW; 782 next->flags &= ~CF_NEW;
776 /* first get rid of the old state */ 783 /* first get rid of the old state */
777 save_perl (prev); 784 save_perl (aTHX_ prev);
778 /* setup coroutine call */ 785 /* setup coroutine call */
779 setup_coro (next); 786 setup_coro (aTHX_ next);
780 /* need a new stack */ 787 /* need a new stack */
781 assert (!next->cctx); 788 assert (!next->cctx);
782 } 789 }
783 else 790 else
784 { 791 {
785 /* coroutine already started */ 792 /* coroutine already started */
786 save_perl (prev); 793 save_perl (aTHX_ prev);
787 load_perl (next); 794 load_perl (aTHX_ next);
788 } 795 }
789 796
790 prev__cctx = prev->cctx; 797 prev__cctx = prev->cctx;
791 798
792 /* possibly "free" the cctx */ 799 /* possibly "free" the cctx */
801 prev__cctx->inuse = 0; 808 prev__cctx->inuse = 0;
802 } 809 }
803 810
804 if (!next->cctx) 811 if (!next->cctx)
805 { 812 {
806 next->cctx = cctx_get (); 813 next->cctx = cctx_get (aTHX);
807 assert (!next->cctx->inuse); 814 assert (!next->cctx->inuse);
808 next->cctx->inuse = 1; 815 next->cctx->inuse = 1;
809 } 816 }
810 817
811 if (prev__cctx != next->cctx) 818 if (prev__cctx != next->cctx)
813 prev__cctx->top_env = PL_top_env; 820 prev__cctx->top_env = PL_top_env;
814 PL_top_env = next->cctx->top_env; 821 PL_top_env = next->cctx->top_env;
815 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 822 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
816 } 823 }
817 824
818 free_coro_mortal (); 825 free_coro_mortal (aTHX);
819 UNLOCK; 826 UNLOCK;
820 } 827 }
821} 828}
822 829
823struct transfer_args 830struct transfer_args
824{ 831{
825 struct coro *prev, *next; 832 struct coro *prev, *next;
826}; 833};
827 834
828#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 835#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
829 836
830/** high level stuff ********************************************************/ 837/** high level stuff ********************************************************/
831 838
832static int 839static int
833coro_state_destroy (struct coro *coro) 840coro_state_destroy (pTHX_ struct coro *coro)
834{ 841{
835 if (coro->flags & CF_DESTROYED) 842 if (coro->flags & CF_DESTROYED)
836 return 0; 843 return 0;
837 844
838 coro->flags |= CF_DESTROYED; 845 coro->flags |= CF_DESTROYED;
848 else 855 else
849 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 856 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
850 857
851 if (coro->mainstack && coro->mainstack != main_mainstack) 858 if (coro->mainstack && coro->mainstack != main_mainstack)
852 { 859 {
860 struct coro temp;
861
853 assert (!(coro->flags & CF_RUNNING)); 862 assert (!(coro->flags & CF_RUNNING));
854 863
855 struct coro temp;
856 Zero (&temp, 1, struct coro); 864 Zero (&temp, 1, struct coro);
857 temp.save = CORO_SAVE_ALL; 865 temp.save = CORO_SAVE_ALL;
858 866
859 if (coro->flags & CF_RUNNING) 867 if (coro->flags & CF_RUNNING)
860 croak ("FATAL: tried to destroy currently running coroutine"); 868 croak ("FATAL: tried to destroy currently running coroutine");
861 869
862 save_perl (&temp); 870 save_perl (aTHX_ &temp);
863 load_perl (coro); 871 load_perl (aTHX_ coro);
864 872
865 coro_destroy_stacks (); 873 coro_destroy_stacks (aTHX);
866 874
867 load_perl (&temp); /* this will get rid of defsv etc.. */ 875 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
868 876
869 coro->mainstack = 0; 877 coro->mainstack = 0;
870 } 878 }
871 879
872 cctx_destroy (coro->cctx); 880 cctx_destroy (coro->cctx);
881 struct coro *coro = (struct coro *)mg->mg_ptr; 889 struct coro *coro = (struct coro *)mg->mg_ptr;
882 mg->mg_ptr = 0; 890 mg->mg_ptr = 0;
883 891
884 if (--coro->refcnt < 0) 892 if (--coro->refcnt < 0)
885 { 893 {
886 coro_state_destroy (coro); 894 coro_state_destroy (aTHX_ coro);
887 Safefree (coro); 895 Safefree (coro);
888 } 896 }
889 897
890 return 0; 898 return 0;
891} 899}
910# define MGf_DUP 0 918# define MGf_DUP 0
911#endif 919#endif
912}; 920};
913 921
914static struct coro * 922static struct coro *
915SvSTATE (SV *coro) 923SvSTATE_ (pTHX_ SV *coro)
916{ 924{
917 HV *stash; 925 HV *stash;
918 MAGIC *mg; 926 MAGIC *mg;
919 927
920 if (SvROK (coro)) 928 if (SvROK (coro))
926 /* very slow, but rare, check */ 934 /* very slow, but rare, check */
927 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 935 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
928 croak ("Coro::State object required"); 936 croak ("Coro::State object required");
929 } 937 }
930 938
931 mg = SvMAGIC (coro); 939 mg = CORO_MAGIC (coro);
932 assert (mg->mg_type == PERL_MAGIC_ext);
933 return (struct coro *)mg->mg_ptr; 940 return (struct coro *)mg->mg_ptr;
934} 941}
935 942
943#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
944
936static void 945static void
937prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 946prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
938{ 947{
939 ta->prev = SvSTATE (prev_sv); 948 ta->prev = SvSTATE (prev_sv);
940 ta->next = SvSTATE (next_sv); 949 ta->next = SvSTATE (next_sv);
941} 950}
942 951
943static void 952static void
944api_transfer (SV *prev_sv, SV *next_sv) 953api_transfer (SV *prev_sv, SV *next_sv)
945{ 954{
955 dTHX;
946 struct transfer_args ta; 956 struct transfer_args ta;
947 957
948 prepare_transfer (&ta, prev_sv, next_sv); 958 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
949 TRANSFER (ta); 959 TRANSFER (ta);
950} 960}
951 961
952static int 962static int
953api_save (SV *coro_sv, int new_save) 963api_save (SV *coro_sv, int new_save)
954{ 964{
965 dTHX;
955 struct coro *coro = SvSTATE (coro_sv); 966 struct coro *coro = SvSTATE (coro_sv);
956 int old_save = coro->save; 967 int old_save = coro->save;
957 968
958 if (new_save >= 0) 969 if (new_save >= 0)
959 coro->save = new_save; 970 coro->save = new_save;
962} 973}
963 974
964/** Coro ********************************************************************/ 975/** Coro ********************************************************************/
965 976
966static void 977static void
967coro_enq (SV *coro_sv) 978coro_enq (pTHX_ SV *coro_sv)
968{ 979{
969 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 980 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
970} 981}
971 982
972static SV * 983static SV *
973coro_deq (int min_prio) 984coro_deq (pTHX_ int min_prio)
974{ 985{
975 int prio = PRIO_MAX - PRIO_MIN; 986 int prio = PRIO_MAX - PRIO_MIN;
976 987
977 min_prio -= PRIO_MIN; 988 min_prio -= PRIO_MIN;
978 if (min_prio < 0) 989 if (min_prio < 0)
986} 997}
987 998
988static int 999static int
989api_ready (SV *coro_sv) 1000api_ready (SV *coro_sv)
990{ 1001{
1002 dTHX;
991 struct coro *coro; 1003 struct coro *coro;
992 1004
993 if (SvROK (coro_sv)) 1005 if (SvROK (coro_sv))
994 coro_sv = SvRV (coro_sv); 1006 coro_sv = SvRV (coro_sv);
995 1007
999 return 0; 1011 return 0;
1000 1012
1001 coro->flags |= CF_READY; 1013 coro->flags |= CF_READY;
1002 1014
1003 LOCK; 1015 LOCK;
1004 coro_enq (SvREFCNT_inc (coro_sv)); 1016 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1005 ++coro_nready; 1017 ++coro_nready;
1006 UNLOCK; 1018 UNLOCK;
1007 1019
1008 return 1; 1020 return 1;
1009} 1021}
1010 1022
1011static int 1023static int
1012api_is_ready (SV *coro_sv) 1024api_is_ready (SV *coro_sv)
1013{ 1025{
1026 dTHX;
1014 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1027 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1015} 1028}
1016 1029
1017static void 1030static void
1018prepare_schedule (struct transfer_args *ta) 1031prepare_schedule (pTHX_ struct transfer_args *ta)
1019{ 1032{
1020 SV *prev_sv, *next_sv; 1033 SV *prev_sv, *next_sv;
1021 1034
1022 for (;;) 1035 for (;;)
1023 { 1036 {
1024 LOCK; 1037 LOCK;
1025 next_sv = coro_deq (PRIO_MIN); 1038 next_sv = coro_deq (aTHX_ PRIO_MIN);
1026 1039
1027 /* nothing to schedule: call the idle handler */ 1040 /* nothing to schedule: call the idle handler */
1028 if (!next_sv) 1041 if (!next_sv)
1029 { 1042 {
1043 dSP;
1030 UNLOCK; 1044 UNLOCK;
1031 dSP;
1032 1045
1033 ENTER; 1046 ENTER;
1034 SAVETMPS; 1047 SAVETMPS;
1035 1048
1036 PUSHMARK (SP); 1049 PUSHMARK (SP);
1065 1078
1066 assert (ta->next->flags & CF_READY); 1079 assert (ta->next->flags & CF_READY);
1067 ta->next->flags &= ~CF_READY; 1080 ta->next->flags &= ~CF_READY;
1068 1081
1069 LOCK; 1082 LOCK;
1070 free_coro_mortal (); 1083 free_coro_mortal (aTHX);
1071 coro_mortal = prev_sv; 1084 coro_mortal = prev_sv;
1072 UNLOCK; 1085 UNLOCK;
1073} 1086}
1074 1087
1075static void 1088static void
1076prepare_cede (struct transfer_args *ta) 1089prepare_cede (pTHX_ struct transfer_args *ta)
1077{ 1090{
1078 api_ready (coro_current); 1091 api_ready (coro_current);
1079 prepare_schedule (ta); 1092 prepare_schedule (aTHX_ ta);
1080} 1093}
1081 1094
1082static int 1095static int
1083prepare_cede_notself (struct transfer_args *ta) 1096prepare_cede_notself (pTHX_ struct transfer_args *ta)
1084{ 1097{
1085 if (coro_nready) 1098 if (coro_nready)
1086 { 1099 {
1087 SV *prev = SvRV (coro_current); 1100 SV *prev = SvRV (coro_current);
1088 prepare_schedule (ta); 1101 prepare_schedule (aTHX_ ta);
1089 api_ready (prev); 1102 api_ready (prev);
1090 return 1; 1103 return 1;
1091 } 1104 }
1092 else 1105 else
1093 return 0; 1106 return 0;
1094} 1107}
1095 1108
1096static void 1109static void
1097api_schedule (void) 1110api_schedule (void)
1098{ 1111{
1112 dTHX;
1099 struct transfer_args ta; 1113 struct transfer_args ta;
1100 1114
1101 prepare_schedule (&ta); 1115 prepare_schedule (aTHX_ &ta);
1102 TRANSFER (ta); 1116 TRANSFER (ta);
1103} 1117}
1104 1118
1105static int 1119static int
1106api_cede (void) 1120api_cede (void)
1107{ 1121{
1122 dTHX;
1108 struct transfer_args ta; 1123 struct transfer_args ta;
1109 1124
1110 prepare_cede (&ta); 1125 prepare_cede (aTHX_ &ta);
1111 1126
1112 if (ta.prev != ta.next) 1127 if (ta.prev != ta.next)
1113 { 1128 {
1114 TRANSFER (ta); 1129 TRANSFER (ta);
1115 return 1; 1130 return 1;
1119} 1134}
1120 1135
1121static int 1136static int
1122api_cede_notself (void) 1137api_cede_notself (void)
1123{ 1138{
1139 dTHX;
1124 struct transfer_args ta; 1140 struct transfer_args ta;
1125 1141
1126 if (prepare_cede_notself (&ta)) 1142 if (prepare_cede_notself (aTHX_ &ta))
1127 { 1143 {
1128 TRANSFER (ta); 1144 TRANSFER (ta);
1129 return 1; 1145 return 1;
1130 } 1146 }
1131 else 1147 else
1209 1225
1210 case 1: 1226 case 1:
1211 if (items != 2) 1227 if (items != 2)
1212 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1228 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1213 1229
1214 prepare_transfer (&ta, ST (0), ST (1)); 1230 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1215 break; 1231 break;
1216 1232
1217 case 2: 1233 case 2:
1218 prepare_schedule (&ta); 1234 prepare_schedule (aTHX_ &ta);
1219 break; 1235 break;
1220 1236
1221 case 3: 1237 case 3:
1222 prepare_cede (&ta); 1238 prepare_cede (aTHX_ &ta);
1223 break; 1239 break;
1224 1240
1225 case 4: 1241 case 4:
1226 if (!prepare_cede_notself (&ta)) 1242 if (!prepare_cede_notself (aTHX_ &ta))
1227 XSRETURN_EMPTY; 1243 XSRETURN_EMPTY;
1228 1244
1229 break; 1245 break;
1230 } 1246 }
1231 1247
1237} 1253}
1238 1254
1239bool 1255bool
1240_destroy (SV *coro_sv) 1256_destroy (SV *coro_sv)
1241 CODE: 1257 CODE:
1242 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1258 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1243 OUTPUT: 1259 OUTPUT:
1244 RETVAL 1260 RETVAL
1245 1261
1246void 1262void
1247_exit (code) 1263_exit (code)
1248 int code 1264 int code
1249 PROTOTYPE: $ 1265 PROTOTYPE: $
1250 CODE: 1266 CODE:
1251 _exit (code); 1267 _exit (code);
1268
1269int
1270cctx_stacksize (int new_stacksize = 0)
1271 CODE:
1272 RETVAL = coro_stacksize;
1273 if (new_stacksize)
1274 coro_stacksize = new_stacksize;
1275 OUTPUT:
1276 RETVAL
1252 1277
1253int 1278int
1254cctx_count () 1279cctx_count ()
1255 CODE: 1280 CODE:
1256 RETVAL = cctx_count; 1281 RETVAL = cctx_count;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines