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.124 by root, Tue Dec 5 12:50:04 2006 UTC vs.
Revision 1.141 by root, Mon Jan 22 18:45:17 2007 UTC

4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
23# ifndef PL_ppaddr
24# define PL_ppaddr ppaddr
25# endif
26# ifndef call_sv
27# define call_sv perl_call_sv
28# endif
29# ifndef get_sv
30# define get_sv perl_get_sv
31# endif
32# ifndef get_cv
33# define get_cv perl_get_cv
34# endif
35# ifndef IS_PADGV
36# define IS_PADGV(v) 0
37# endif
38# ifndef IS_PADCONST
39# define IS_PADCONST(v) 0
40# endif
41#endif
42
43#include <stdio.h> 9#include <stdio.h>
44#include <errno.h> 10#include <errno.h>
45#include <assert.h> 11#include <assert.h>
46
47#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
48# undef STACKGUARD
49#endif
50
51#ifndef STACKGUARD
52# define STACKGUARD 0
53#endif
54 12
55#ifdef HAVE_MMAP 13#ifdef HAVE_MMAP
56# include <unistd.h> 14# include <unistd.h>
57# include <sys/mman.h> 15# include <sys/mman.h>
58# ifndef MAP_ANONYMOUS 16# ifndef MAP_ANONYMOUS
73#else 31#else
74# define PAGESIZE 0 32# define PAGESIZE 0
75# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
76#endif 34#endif
77 35
36#if USE_VALGRIND
37# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else
40# define REGISTER_STACK(cctx,start,end)
41#endif
42
43/* the maximum number of idle cctx that will be pooled */
44#define MAX_IDLE_CCTX 8
45
46#define PERL_VERSION_ATLEAST(a,b,c) \
47 (PERL_REVISION > (a) \
48 || (PERL_REVISION == (a) \
49 && (PERL_VERSION > (b) \
50 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
51
52#if !PERL_VERSION_ATLEAST (5,6,0)
53# ifndef PL_ppaddr
54# define PL_ppaddr ppaddr
55# endif
56# ifndef call_sv
57# define call_sv perl_call_sv
58# endif
59# ifndef get_sv
60# define get_sv perl_get_sv
61# endif
62# ifndef get_cv
63# define get_cv perl_get_cv
64# endif
65# ifndef IS_PADGV
66# define IS_PADGV(v) 0
67# endif
68# ifndef IS_PADCONST
69# define IS_PADCONST(v) 0
70# endif
71#endif
72
73/* 5.8.7 */
74#ifndef SvRV_set
75# define SvRV_set(s,v) SvRV(s) = (v)
76#endif
77
78#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
79# undef STACKGUARD
80#endif
81
82#ifndef STACKGUARD
83# define STACKGUARD 0
84#endif
85
86/* prefer perl internal functions over our own? */
87#ifndef PREFER_PERL_FUNCTIONS
88# define PREFER_PERL_FUNCTIONS 0
89#endif
90
78/* The next macro should declare a variable stacklevel that contains and approximation 91/* The next macro should declare a variable stacklevel that contains and approximation
79 * to the current C stack pointer. Its property is that it changes with each call 92 * to the current C stack pointer. Its property is that it changes with each call
80 * and should be unique. */ 93 * and should be unique. */
81#define dSTACKLEVEL int stacklevel 94#define dSTACKLEVEL int stacklevel
82#define STACKLEVEL ((void *)&stacklevel) 95#define STACKLEVEL ((void *)&stacklevel)
83 96
84#define IN_DESTRUCT (PL_main_cv == Nullcv) 97#define IN_DESTRUCT (PL_main_cv == Nullcv)
85 98
86#if __GNUC__ >= 3 99#if __GNUC__ >= 3
87# define attribute(x) __attribute__(x) 100# define attribute(x) __attribute__(x)
101# define BARRIER __asm__ __volatile__ ("" : : : "memory")
88#else 102#else
89# define attribute(x) 103# define attribute(x)
104# define BARRIER
90#endif 105#endif
91 106
92#define NOINLINE attribute ((noinline)) 107#define NOINLINE attribute ((noinline))
93 108
94#include "CoroAPI.h" 109#include "CoroAPI.h"
100#else 115#else
101# define LOCK (void)0 116# define LOCK (void)0
102# define UNLOCK (void)0 117# define UNLOCK (void)0
103#endif 118#endif
104 119
120/* helper storage struct for Coro::AIO */
105struct io_state 121struct io_state
106{ 122{
107 int errorno; 123 int errorno;
108 I32 laststype; 124 I32 laststype;
109 int laststatval; 125 int laststatval;
122typedef struct coro_cctx { 138typedef struct coro_cctx {
123 struct coro_cctx *next; 139 struct coro_cctx *next;
124 140
125 /* the stack */ 141 /* the stack */
126 void *sptr; 142 void *sptr;
127 long ssize; /* positive == mmap, otherwise malloc */ 143 ssize_t ssize; /* positive == mmap, otherwise malloc */
128 144
129 /* cpu state */ 145 /* cpu state */
130 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 146 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 */
131 JMPENV *top_env; 148 JMPENV *top_env;
132 coro_context cctx; 149 coro_context cctx;
133 150
134 int inuse; 151 int inuse;
135 152
137 int valgrind_id; 154 int valgrind_id;
138#endif 155#endif
139} coro_cctx; 156} coro_cctx;
140 157
141enum { 158enum {
142 CF_RUNNING = 0x0001, /* coroutine is running */ 159 CF_RUNNING = 0x0001, /* coroutine is running */
143 CF_READY = 0x0002, /* coroutine is ready */ 160 CF_READY = 0x0002, /* coroutine is ready */
144 CF_NEW = 0x0004, /* ahs never been switched to */ 161 CF_NEW = 0x0004, /* has never been switched to */
162 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
145}; 163};
146 164
147/* this is a structure representing a perl-level coroutine */ 165/* this is a structure representing a perl-level coroutine */
148struct coro { 166struct coro {
149 /* the c coroutine allocated to this perl coroutine, if any */ 167 /* the c coroutine allocated to this perl coroutine, if any */
171}; 189};
172 190
173typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
174typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
175 193
194/** Coro ********************************************************************/
195
196#define PRIO_MAX 3
197#define PRIO_HIGH 1
198#define PRIO_NORMAL 0
199#define PRIO_LOW -1
200#define PRIO_IDLE -3
201#define PRIO_MIN -4
202
203/* for Coro.pm */
204static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready;
207
208/** lowlevel stuff **********************************************************/
209
176static AV * 210static AV *
177coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
178{ 212{
179 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
180 AV *newpadlist, *newpad; 214 AV *newpadlist, *newpad;
254 288
255 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
256 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
257 else 291 else
258 { 292 {
259#if 0 293#if PREFER_PERL_FUNCTIONS
260 /* this is probably cleaner, but also slower? */ 294 /* this is probably cleaner, but also slower? */
261 CV *cp = Perl_cv_clone (cv); 295 CV *cp = Perl_cv_clone (cv);
262 CvPADLIST (cv) = CvPADLIST (cp); 296 CvPADLIST (cv) = CvPADLIST (cp);
263 CvPADLIST (cp) = 0; 297 CvPADLIST (cp) = 0;
264 SvREFCNT_dec (cp); 298 SvREFCNT_dec (cp);
287 if (AvFILLp (av) >= AvMAX (av)) 321 if (AvFILLp (av) >= AvMAX (av))
288 av_extend (av, AvMAX (av) + 1); 322 av_extend (av, AvMAX (av) + 1);
289 323
290 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
291} 325}
326
327/** load & save, init *******************************************************/
292 328
293#define SB do { 329#define SB do {
294#define SE } while (0) 330#define SE } while (0)
295 331
296#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
345 /* 381 /*
346 * the worst thing you can imagine happens first - we have to save 382 * the worst thing you can imagine happens first - we have to save
347 * (and reinitialize) all cv's in the whole callchain :( 383 * (and reinitialize) all cv's in the whole callchain :(
348 */ 384 */
349 385
386 EXTEND (SP, 3 + 1);
350 PUSHs (Nullsv); 387 PUSHs (Nullsv);
351 /* this loop was inspired by pp_caller */ 388 /* this loop was inspired by pp_caller */
352 for (;;) 389 for (;;)
353 { 390 {
354 while (cxix >= 0) 391 while (cxix >= 0)
360 CV *cv = cx->blk_sub.cv; 397 CV *cv = cx->blk_sub.cv;
361 398
362 if (CvDEPTH (cv)) 399 if (CvDEPTH (cv))
363 { 400 {
364 EXTEND (SP, 3); 401 EXTEND (SP, 3);
365
366 PUSHs ((SV *)CvPADLIST (cv)); 402 PUSHs ((SV *)CvPADLIST (cv));
367 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 403 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
368 PUSHs ((SV *)cv); 404 PUSHs ((SV *)cv);
369 405
370 CvDEPTH (cv) = 0; 406 CvDEPTH (cv) = 0;
371 get_padlist (cv); 407 get_padlist (cv);
372 } 408 }
373 } 409 }
374#ifdef CXt_FORMAT
375 else if (CxTYPE (cx) == CXt_FORMAT)
376 {
377 /* I never used formats, so how should I know how these are implemented? */
378 /* my bold guess is as a simple, plain sub... */
379 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
380 }
381#endif
382 } 410 }
383 411
384 if (top_si->si_type == PERLSI_MAIN) 412 if (top_si->si_type == PERLSI_MAIN)
385 break; 413 break;
386 414
406 * allocate various perl stacks. This is an exact copy 434 * allocate various perl stacks. This is an exact copy
407 * of perl.c:init_stacks, except that it uses less memory 435 * of perl.c:init_stacks, except that it uses less memory
408 * on the (sometimes correct) assumption that coroutines do 436 * on the (sometimes correct) assumption that coroutines do
409 * not usually need a lot of stackspace. 437 * not usually need a lot of stackspace.
410 */ 438 */
439#if PREFER_PERL_FUNCTIONS
440# define coro_init_stacks init_stacks
441#else
411static void 442static void
412coro_init_stacks () 443coro_init_stacks ()
413{ 444{
414 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 445 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
415 PL_curstackinfo->si_type = PERLSI_MAIN; 446 PL_curstackinfo->si_type = PERLSI_MAIN;
445 New(54,PL_retstack,16,OP*); 476 New(54,PL_retstack,16,OP*);
446 PL_retstack_ix = 0; 477 PL_retstack_ix = 0;
447 PL_retstack_max = 16; 478 PL_retstack_max = 16;
448#endif 479#endif
449} 480}
481#endif
450 482
451/* 483/*
452 * destroy the stacks, the callchain etc... 484 * destroy the stacks, the callchain etc...
453 */ 485 */
454static void 486static void
455coro_destroy_stacks () 487coro_destroy_stacks ()
456{ 488{
457 if (!IN_DESTRUCT) 489 if (!IN_DESTRUCT)
458 { 490 {
459 /* is this ugly, I ask? */ 491 /* restore all saved variables and stuff */
460 LEAVE_SCOPE (0); 492 LEAVE_SCOPE (0);
493 assert (PL_tmps_floor == -1);
461 494
462 /* sure it is, but more important: is it correct?? :/ */ 495 /* free all temporaries */
463 FREETMPS; 496 FREETMPS;
497 assert (PL_tmps_ix == -1);
464 498
499 /* unwind all extra stacks */
465 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 500 POPSTACK_TO (PL_mainstack);
501
502 /* unwind main stack */
503 dounwind (-1);
466 } 504 }
467 505
468 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
469 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
470 508
471 while (PL_curstackinfo) 509 while (PL_curstackinfo)
472 { 510 {
473 PERL_SI *p = PL_curstackinfo->si_prev; 511 PERL_SI *p = PL_curstackinfo->si_prev;
474 512
475 { /*D*//*remove*/
476 dSP;
477 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
478 PUTBACK; /* possibly superfluous */
479 }
480
481 if (!IN_DESTRUCT) 513 if (!IN_DESTRUCT)
482 {
483 dounwind (-1);/*D*//*remove*/
484 SvREFCNT_dec (PL_curstackinfo->si_stack); 514 SvREFCNT_dec (PL_curstackinfo->si_stack);
485 }
486 515
487 Safefree (PL_curstackinfo->si_cxstack); 516 Safefree (PL_curstackinfo->si_cxstack);
488 Safefree (PL_curstackinfo); 517 Safefree (PL_curstackinfo);
489 PL_curstackinfo = p; 518 PL_curstackinfo = p;
490 } 519 }
495 Safefree (PL_savestack); 524 Safefree (PL_savestack);
496#if !PERL_VERSION_ATLEAST (5,9,0) 525#if !PERL_VERSION_ATLEAST (5,9,0)
497 Safefree (PL_retstack); 526 Safefree (PL_retstack);
498#endif 527#endif
499} 528}
529
530/** coroutine stack handling ************************************************/
500 531
501static void 532static void
502setup_coro (struct coro *coro) 533setup_coro (struct coro *coro)
503{ 534{
504 /* 535 /*
602 633
603 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 634 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
604 /* mmap supposedly does allocate-on-write for us */ 635 /* mmap supposedly does allocate-on-write for us */
605 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 636 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
606 637
607 if (cctx->sptr == (void *)-1) 638 if (cctx->sptr != (void *)-1)
608 {
609 perror ("FATAL: unable to mmap stack for coroutine");
610 _exit (EXIT_FAILURE);
611 } 639 {
612
613# if STACKGUARD 640# if STACKGUARD
614 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 641 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
615# endif 642# endif
616 643 REGISTER_STACK (
617#else 644 cctx,
618
619 cctx->ssize = STACKSIZE * (long)sizeof (long);
620 New (0, cctx->sptr, STACKSIZE, long);
621
622 if (!cctx->sptr)
623 {
624 perror ("FATAL: unable to malloc stack for coroutine");
625 _exit (EXIT_FAILURE);
626 }
627
628#endif
629
630#if USE_VALGRIND
631 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
632 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 645 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
633 cctx->ssize + (char *)cctx->sptr 646 cctx->ssize + (char *)cctx->sptr
634 ); 647 );
635#endif
636 648
637 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 649 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
650 }
651 else
652#endif
653 {
654 cctx->ssize = -STACKSIZE * (long)sizeof (long);
655 New (0, cctx->sptr, STACKSIZE, long);
656
657 if (!cctx->sptr)
658 {
659 perror ("FATAL: unable to allocate stack for coroutine");
660 _exit (EXIT_FAILURE);
661 }
662
663 REGISTER_STACK (
664 cctx,
665 (char *)cctx->sptr,
666 (char *)cctx->sptr - cctx->ssize
667 );
668
669 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
670 }
638 671
639 return cctx; 672 return cctx;
640} 673}
641 674
642static void 675static void
650#if USE_VALGRIND 683#if USE_VALGRIND
651 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 684 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
652#endif 685#endif
653 686
654#if HAVE_MMAP 687#if HAVE_MMAP
688 if (cctx->ssize > 0)
655 munmap (cctx->sptr, cctx->ssize); 689 munmap (cctx->sptr, cctx->ssize);
656#else 690 else
691#endif
657 Safefree (cctx->sptr); 692 Safefree (cctx->sptr);
658#endif
659 693
660 Safefree (cctx); 694 Safefree (cctx);
661} 695}
662 696
663static coro_cctx * 697static coro_cctx *
697 ++cctx_idle; 731 ++cctx_idle;
698 cctx->next = cctx_first; 732 cctx->next = cctx_first;
699 cctx_first = cctx; 733 cctx_first = cctx;
700} 734}
701 735
736/** coroutine switching *****************************************************/
737
702/* never call directly, always through the coro_state_transfer global variable */ 738/* never call directly, always through the coro_state_transfer global variable */
703static void NOINLINE 739static void NOINLINE
704transfer (struct coro *prev, struct coro *next) 740transfer (struct coro *prev, struct coro *next)
705{ 741{
706 dSTACKLEVEL; 742 dSTACKLEVEL;
707 743
708 /* sometimes transfer is only called to set idle_sp */ 744 /* sometimes transfer is only called to set idle_sp */
709 if (!next) 745 if (!next)
710 { 746 {
711 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 747 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
712 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ 748 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
713 } 749 }
714 else if (prev != next) 750 else if (prev != next)
715 { 751 {
716 coro_cctx *prev__cctx; 752 coro_cctx *prev__cctx;
717 753
728 if (!prev->flags & CF_RUNNING) 764 if (!prev->flags & CF_RUNNING)
729 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 765 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
730 766
731 if (next->flags & CF_RUNNING) 767 if (next->flags & CF_RUNNING)
732 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 768 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
769
770 if (next->flags & CF_DESTROYED)
771 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
733 772
734 prev->flags &= ~CF_RUNNING; 773 prev->flags &= ~CF_RUNNING;
735 next->flags |= CF_RUNNING; 774 next->flags |= CF_RUNNING;
736 775
737 LOCK; 776 LOCK;
758 797
759 /* possibly "free" the cctx */ 798 /* possibly "free" the cctx */
760 if (prev__cctx->idle_sp == STACKLEVEL) 799 if (prev__cctx->idle_sp == STACKLEVEL)
761 { 800 {
762 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 801 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
763 assert (PL_top_env == prev__cctx->top_env); 802 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
764 803
765 prev->cctx = 0; 804 prev->cctx = 0;
766 805
767 cctx_put (prev__cctx); 806 cctx_put (prev__cctx);
768 prev__cctx->inuse = 0; 807 prev__cctx->inuse = 0;
781 PL_top_env = next->cctx->top_env; 820 PL_top_env = next->cctx->top_env;
782 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 821 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
783 } 822 }
784 823
785 free_coro_mortal (); 824 free_coro_mortal ();
786
787 UNLOCK; 825 UNLOCK;
788 } 826 }
789} 827}
790 828
791struct transfer_args 829struct transfer_args
793 struct coro *prev, *next; 831 struct coro *prev, *next;
794}; 832};
795 833
796#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 834#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
797 835
836/** high level stuff ********************************************************/
837
798static void 838static int
799coro_state_destroy (struct coro *coro) 839coro_state_destroy (struct coro *coro)
800{ 840{
801 if (coro->refcnt--) 841 if (coro->flags & CF_DESTROYED)
802 return; 842 return 0;
843
844 coro->flags |= CF_DESTROYED;
845
846 if (coro->flags & CF_READY)
847 {
848 /* reduce nready, as destroying a ready coro effectively unreadies it */
849 /* alternative: look through all ready queues and remove the coro */
850 LOCK;
851 --coro_nready;
852 UNLOCK;
853 }
854 else
855 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
803 856
804 if (coro->mainstack && coro->mainstack != main_mainstack) 857 if (coro->mainstack && coro->mainstack != main_mainstack)
805 { 858 {
806 struct coro temp; 859 struct coro temp;
860
861 assert (!(coro->flags & CF_RUNNING));
862
807 Zero (&temp, 1, struct coro); 863 Zero (&temp, 1, struct coro);
808 temp.save = CORO_SAVE_ALL; 864 temp.save = CORO_SAVE_ALL;
809 865
810 if (coro->flags & CF_RUNNING) 866 if (coro->flags & CF_RUNNING)
811 croak ("FATAL: tried to destroy currently running coroutine"); 867 croak ("FATAL: tried to destroy currently running coroutine");
820 coro->mainstack = 0; 876 coro->mainstack = 0;
821 } 877 }
822 878
823 cctx_destroy (coro->cctx); 879 cctx_destroy (coro->cctx);
824 SvREFCNT_dec (coro->args); 880 SvREFCNT_dec (coro->args);
825 Safefree (coro); 881
882 return 1;
826} 883}
827 884
828static int 885static int
829coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 886coro_state_free (pTHX_ SV *sv, MAGIC *mg)
830{ 887{
831 struct coro *coro = (struct coro *)mg->mg_ptr; 888 struct coro *coro = (struct coro *)mg->mg_ptr;
832 mg->mg_ptr = 0; 889 mg->mg_ptr = 0;
833 890
891 if (--coro->refcnt < 0)
892 {
834 coro_state_destroy (coro); 893 coro_state_destroy (coro);
894 Safefree (coro);
895 }
835 896
836 return 0; 897 return 0;
837} 898}
838 899
839static int 900static int
846 return 0; 907 return 0;
847} 908}
848 909
849static MGVTBL coro_state_vtbl = { 910static MGVTBL coro_state_vtbl = {
850 0, 0, 0, 0, 911 0, 0, 0, 0,
851 coro_state_clear, 912 coro_state_free,
852 0, 913 0,
853#ifdef MGf_DUP 914#ifdef MGf_DUP
854 coro_state_dup, 915 coro_state_dup,
855#else 916#else
856# define MGf_DUP 0 917# define MGf_DUP 0
907 return old_save; 968 return old_save;
908} 969}
909 970
910/** Coro ********************************************************************/ 971/** Coro ********************************************************************/
911 972
912#define PRIO_MAX 3
913#define PRIO_HIGH 1
914#define PRIO_NORMAL 0
915#define PRIO_LOW -1
916#define PRIO_IDLE -3
917#define PRIO_MIN -4
918
919/* for Coro.pm */
920static SV *coro_current;
921static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
922static int coro_nready;
923
924static void 973static void
925coro_enq (SV *coro_sv) 974coro_enq (SV *coro_sv)
926{ 975{
927 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 976 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
928 coro_nready++;
929} 977}
930 978
931static SV * 979static SV *
932coro_deq (int min_prio) 980coro_deq (int min_prio)
933{ 981{
937 if (min_prio < 0) 985 if (min_prio < 0)
938 min_prio = 0; 986 min_prio = 0;
939 987
940 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 988 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
941 if (AvFILLp (coro_ready [prio]) >= 0) 989 if (AvFILLp (coro_ready [prio]) >= 0)
942 {
943 coro_nready--;
944 return av_shift (coro_ready [prio]); 990 return av_shift (coro_ready [prio]);
945 }
946 991
947 return 0; 992 return 0;
948} 993}
949 994
950static int 995static int
958 coro = SvSTATE (coro_sv); 1003 coro = SvSTATE (coro_sv);
959 1004
960 if (coro->flags & CF_READY) 1005 if (coro->flags & CF_READY)
961 return 0; 1006 return 0;
962 1007
963#if 0 /* this is actually harmless */
964 if (coro->flags & CF_RUNNING)
965 croak ("Coro::ready called on currently running coroutine");
966#endif
967
968 coro->flags |= CF_READY; 1008 coro->flags |= CF_READY;
969 1009
970 LOCK; 1010 LOCK;
971 coro_enq (SvREFCNT_inc (coro_sv)); 1011 coro_enq (SvREFCNT_inc (coro_sv));
1012 ++coro_nready;
972 UNLOCK; 1013 UNLOCK;
973 1014
974 return 1; 1015 return 1;
975} 1016}
976 1017
977static int 1018static int
978api_is_ready (SV *coro_sv) 1019api_is_ready (SV *coro_sv)
979{ 1020{
980 return !!SvSTATE (coro_sv)->flags & CF_READY; 1021 return !!(SvSTATE (coro_sv)->flags & CF_READY);
981} 1022}
982 1023
983static void 1024static void
984prepare_schedule (struct transfer_args *ta) 1025prepare_schedule (struct transfer_args *ta)
985{ 1026{
986 SV *prev, *next; 1027 SV *prev_sv, *next_sv;
987 1028
988 for (;;) 1029 for (;;)
989 { 1030 {
990 LOCK; 1031 LOCK;
991 next = coro_deq (PRIO_MIN); 1032 next_sv = coro_deq (PRIO_MIN);
1033
1034 /* nothing to schedule: call the idle handler */
1035 if (!next_sv)
1036 {
1037 dSP;
1038 UNLOCK;
1039
1040 ENTER;
1041 SAVETMPS;
1042
1043 PUSHMARK (SP);
1044 PUTBACK;
1045 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1046
1047 FREETMPS;
1048 LEAVE;
1049 continue;
1050 }
1051
1052 ta->next = SvSTATE (next_sv);
1053
1054 /* cannot transfer to destroyed coros, skip and look for next */
1055 if (ta->next->flags & CF_DESTROYED)
1056 {
1057 UNLOCK;
1058 SvREFCNT_dec (next_sv);
1059 /* coro_nready is already taken care of by destroy */
1060 continue;
1061 }
1062
1063 --coro_nready;
992 UNLOCK; 1064 UNLOCK;
993
994 if (next)
995 break; 1065 break;
996
997 {
998 dSP;
999
1000 ENTER;
1001 SAVETMPS;
1002
1003 PUSHMARK (SP);
1004 PUTBACK;
1005 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1006
1007 FREETMPS;
1008 LEAVE;
1009 } 1066 }
1010 }
1011
1012 prev = SvRV (coro_current);
1013 SvRV_set (coro_current, next);
1014 1067
1015 /* free this only after the transfer */ 1068 /* free this only after the transfer */
1069 prev_sv = SvRV (coro_current);
1070 SvRV_set (coro_current, next_sv);
1071 ta->prev = SvSTATE (prev_sv);
1072
1073 assert (ta->next->flags & CF_READY);
1074 ta->next->flags &= ~CF_READY;
1075
1016 LOCK; 1076 LOCK;
1017 free_coro_mortal (); 1077 free_coro_mortal ();
1078 coro_mortal = prev_sv;
1018 UNLOCK; 1079 UNLOCK;
1019 coro_mortal = prev;
1020
1021 assert (!SvROK(prev));//D
1022 assert (!SvROK(next));//D
1023
1024 ta->prev = SvSTATE (prev);
1025 ta->next = SvSTATE (next);
1026
1027 assert (ta->next->flags & CF_READY);
1028 ta->next->flags &= ~CF_READY;
1029} 1080}
1030 1081
1031static void 1082static void
1032prepare_cede (struct transfer_args *ta) 1083prepare_cede (struct transfer_args *ta)
1033{ 1084{
1034 api_ready (coro_current); 1085 api_ready (coro_current);
1035
1036 prepare_schedule (ta); 1086 prepare_schedule (ta);
1087}
1088
1089static int
1090prepare_cede_notself (struct transfer_args *ta)
1091{
1092 if (coro_nready)
1093 {
1094 SV *prev = SvRV (coro_current);
1095 prepare_schedule (ta);
1096 api_ready (prev);
1097 return 1;
1098 }
1099 else
1100 return 0;
1037} 1101}
1038 1102
1039static void 1103static void
1040api_schedule (void) 1104api_schedule (void)
1041{ 1105{
1043 1107
1044 prepare_schedule (&ta); 1108 prepare_schedule (&ta);
1045 TRANSFER (ta); 1109 TRANSFER (ta);
1046} 1110}
1047 1111
1048static void 1112static int
1049api_cede (void) 1113api_cede (void)
1050{ 1114{
1051 struct transfer_args ta; 1115 struct transfer_args ta;
1052 1116
1053 prepare_cede (&ta); 1117 prepare_cede (&ta);
1118
1119 if (ta.prev != ta.next)
1120 {
1054 TRANSFER (ta); 1121 TRANSFER (ta);
1122 return 1;
1123 }
1124 else
1125 return 0;
1126}
1127
1128static int
1129api_cede_notself (void)
1130{
1131 struct transfer_args ta;
1132
1133 if (prepare_cede_notself (&ta))
1134 {
1135 TRANSFER (ta);
1136 return 1;
1137 }
1138 else
1139 return 0;
1055} 1140}
1056 1141
1057MODULE = Coro::State PACKAGE = Coro::State 1142MODULE = Coro::State PACKAGE = Coro::State
1058 1143
1059PROTOTYPES: DISABLE 1144PROTOTYPES: DISABLE
1115_set_stacklevel (...) 1200_set_stacklevel (...)
1116 ALIAS: 1201 ALIAS:
1117 Coro::State::transfer = 1 1202 Coro::State::transfer = 1
1118 Coro::schedule = 2 1203 Coro::schedule = 2
1119 Coro::cede = 3 1204 Coro::cede = 3
1205 Coro::cede_notself = 4
1120 CODE: 1206 CODE:
1121{ 1207{
1122 struct transfer_args ta; 1208 struct transfer_args ta;
1123 1209
1124 switch (ix) 1210 switch (ix)
1140 break; 1226 break;
1141 1227
1142 case 3: 1228 case 3:
1143 prepare_cede (&ta); 1229 prepare_cede (&ta);
1144 break; 1230 break;
1231
1232 case 4:
1233 if (!prepare_cede_notself (&ta))
1234 XSRETURN_EMPTY;
1235
1236 break;
1145 } 1237 }
1146 1238
1239 BARRIER;
1147 TRANSFER (ta); 1240 TRANSFER (ta);
1148}
1149 1241
1150void 1242 if (GIMME_V != G_VOID && ta.next != ta.prev)
1151_clone_state_from (SV *dst, SV *src) 1243 XSRETURN_YES;
1244}
1245
1246bool
1247_destroy (SV *coro_sv)
1152 CODE: 1248 CODE:
1153{ 1249 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1154 struct coro *coro_src = SvSTATE (src); 1250 OUTPUT:
1155 1251 RETVAL
1156 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1157
1158 ++coro_src->refcnt;
1159 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1160}
1161 1252
1162void 1253void
1163_exit (code) 1254_exit (code)
1164 int code 1255 int code
1165 PROTOTYPE: $ 1256 PROTOTYPE: $
1202 coro_ready[i] = newAV (); 1293 coro_ready[i] = newAV ();
1203 1294
1204 { 1295 {
1205 SV *sv = perl_get_sv("Coro::API", 1); 1296 SV *sv = perl_get_sv("Coro::API", 1);
1206 1297
1207 coroapi.schedule = api_schedule; 1298 coroapi.schedule = api_schedule;
1208 coroapi.save = api_save; 1299 coroapi.save = api_save;
1209 coroapi.cede = api_cede; 1300 coroapi.cede = api_cede;
1301 coroapi.cede_notself = api_cede_notself;
1210 coroapi.ready = api_ready; 1302 coroapi.ready = api_ready;
1211 coroapi.is_ready = api_is_ready; 1303 coroapi.is_ready = api_is_ready;
1212 coroapi.nready = &coro_nready; 1304 coroapi.nready = &coro_nready;
1213 coroapi.current = coro_current; 1305 coroapi.current = coro_current;
1214 1306
1215 GCoroAPI = &coroapi; 1307 GCoroAPI = &coroapi;
1216 sv_setiv (sv, (IV)&coroapi); 1308 sv_setiv (sv, (IV)&coroapi);
1217 SvREADONLY_on (sv); 1309 SvREADONLY_on (sv);
1218 } 1310 }
1234 RETVAL = coro->prio; 1326 RETVAL = coro->prio;
1235 1327
1236 if (items > 1) 1328 if (items > 1)
1237 { 1329 {
1238 if (ix) 1330 if (ix)
1239 newprio += coro->prio; 1331 newprio = coro->prio - newprio;
1240 1332
1241 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1333 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1242 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1334 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1243 1335
1244 coro->prio = newprio; 1336 coro->prio = newprio;
1245 } 1337 }
1246} 1338}
1339 OUTPUT:
1340 RETVAL
1247 1341
1248SV * 1342SV *
1249ready (SV *self) 1343ready (SV *self)
1250 PROTOTYPE: $ 1344 PROTOTYPE: $
1251 CODE: 1345 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines