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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines