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.144 by root, Sun Mar 4 11:45:23 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 CORO_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 CORO_STACKGUARD
80#endif
81
82#ifndef CORO_STACKGUARD
83# define CORO_STACKGUARD 0
84#endif
85
86/* prefer perl internal functions over our own? */
87#ifndef CORO_PREFER_PERL_FUNCTIONS
88# define CORO_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
136#if USE_VALGRIND 153#if CORO_USE_VALGRIND
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 CORO_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
329 CvPADLIST (cv) = (AV *)POPs; 365 CvPADLIST (cv) = (AV *)POPs;
330 } 366 }
331 367
332 PUTBACK; 368 PUTBACK;
333 } 369 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D
334} 371}
335 372
336static void 373static void
337save_perl (Coro__State c) 374save_perl (Coro__State c)
338{ 375{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
339 { 377 {
340 dSP; 378 dSP;
341 I32 cxix = cxstack_ix; 379 I32 cxix = cxstack_ix;
342 PERL_CONTEXT *ccstk = cxstack; 380 PERL_CONTEXT *ccstk = cxstack;
343 PERL_SI *top_si = PL_curstackinfo; 381 PERL_SI *top_si = PL_curstackinfo;
345 /* 383 /*
346 * the worst thing you can imagine happens first - we have to save 384 * the worst thing you can imagine happens first - we have to save
347 * (and reinitialize) all cv's in the whole callchain :( 385 * (and reinitialize) all cv's in the whole callchain :(
348 */ 386 */
349 387
388 EXTEND (SP, 3 + 1);
350 PUSHs (Nullsv); 389 PUSHs (Nullsv);
351 /* this loop was inspired by pp_caller */ 390 /* this loop was inspired by pp_caller */
352 for (;;) 391 for (;;)
353 { 392 {
354 while (cxix >= 0) 393 while (cxix >= 0)
360 CV *cv = cx->blk_sub.cv; 399 CV *cv = cx->blk_sub.cv;
361 400
362 if (CvDEPTH (cv)) 401 if (CvDEPTH (cv))
363 { 402 {
364 EXTEND (SP, 3); 403 EXTEND (SP, 3);
365
366 PUSHs ((SV *)CvPADLIST (cv)); 404 PUSHs ((SV *)CvPADLIST (cv));
367 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 405 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
368 PUSHs ((SV *)cv); 406 PUSHs ((SV *)cv);
369 407
370 CvDEPTH (cv) = 0; 408 CvDEPTH (cv) = 0;
371 get_padlist (cv); 409 get_padlist (cv);
372 } 410 }
373 } 411 }
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 } 412 }
383 413
384 if (top_si->si_type == PERLSI_MAIN) 414 if (top_si->si_type == PERLSI_MAIN)
385 break; 415 break;
386 416
406 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
407 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
408 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
409 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
410 */ 440 */
441#if CORO_PREFER_PERL_FUNCTIONS
442# define coro_init_stacks init_stacks
443#else
411static void 444static void
412coro_init_stacks () 445coro_init_stacks ()
413{ 446{
414 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 447 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
415 PL_curstackinfo->si_type = PERLSI_MAIN; 448 PL_curstackinfo->si_type = PERLSI_MAIN;
445 New(54,PL_retstack,16,OP*); 478 New(54,PL_retstack,16,OP*);
446 PL_retstack_ix = 0; 479 PL_retstack_ix = 0;
447 PL_retstack_max = 16; 480 PL_retstack_max = 16;
448#endif 481#endif
449} 482}
483#endif
450 484
451/* 485/*
452 * destroy the stacks, the callchain etc... 486 * destroy the stacks, the callchain etc...
453 */ 487 */
454static void 488static void
455coro_destroy_stacks () 489coro_destroy_stacks ()
456{ 490{
457 if (!IN_DESTRUCT) 491 if (!IN_DESTRUCT)
458 { 492 {
459 /* is this ugly, I ask? */ 493 /* restore all saved variables and stuff */
460 LEAVE_SCOPE (0); 494 LEAVE_SCOPE (0);
495 assert (PL_tmps_floor == -1);
461 496
462 /* sure it is, but more important: is it correct?? :/ */ 497 /* free all temporaries */
463 FREETMPS; 498 FREETMPS;
499 assert (PL_tmps_ix == -1);
464 500
501 /* unwind all extra stacks */
465 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 502 POPSTACK_TO (PL_mainstack);
503
504 /* unwind main stack */
505 dounwind (-1);
466 } 506 }
467 507
468 while (PL_curstackinfo->si_next) 508 while (PL_curstackinfo->si_next)
469 PL_curstackinfo = PL_curstackinfo->si_next; 509 PL_curstackinfo = PL_curstackinfo->si_next;
470 510
471 while (PL_curstackinfo) 511 while (PL_curstackinfo)
472 { 512 {
473 PERL_SI *p = PL_curstackinfo->si_prev; 513 PERL_SI *p = PL_curstackinfo->si_prev;
474 514
475 { /*D*//*remove*/
476 dSP;
477 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
478 PUTBACK; /* possibly superfluous */
479 }
480
481 if (!IN_DESTRUCT) 515 if (!IN_DESTRUCT)
482 {
483 dounwind (-1);/*D*//*remove*/
484 SvREFCNT_dec (PL_curstackinfo->si_stack); 516 SvREFCNT_dec (PL_curstackinfo->si_stack);
485 }
486 517
487 Safefree (PL_curstackinfo->si_cxstack); 518 Safefree (PL_curstackinfo->si_cxstack);
488 Safefree (PL_curstackinfo); 519 Safefree (PL_curstackinfo);
489 PL_curstackinfo = p; 520 PL_curstackinfo = p;
490 } 521 }
496#if !PERL_VERSION_ATLEAST (5,9,0) 527#if !PERL_VERSION_ATLEAST (5,9,0)
497 Safefree (PL_retstack); 528 Safefree (PL_retstack);
498#endif 529#endif
499} 530}
500 531
532/** coroutine stack handling ************************************************/
533
501static void 534static void
502setup_coro (struct coro *coro) 535setup_coro (struct coro *coro)
503{ 536{
504 /* 537 /*
505 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
507 540
508 coro_init_stacks (); 541 coro_init_stacks ();
509 542
510 PL_curcop = &PL_compiling; 543 PL_curcop = &PL_compiling;
511 PL_in_eval = EVAL_NULL; 544 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0;
512 PL_curpm = 0; 546 PL_curpm = 0;
513 PL_localizing = 0; 547 PL_localizing = 0;
514 PL_dirty = 0; 548 PL_dirty = 0;
515 PL_restartop = 0; 549 PL_restartop = 0;
516 550
598 632
599 Newz (0, cctx, 1, coro_cctx); 633 Newz (0, cctx, 1, coro_cctx);
600 634
601#if HAVE_MMAP 635#if HAVE_MMAP
602 636
603 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
604 /* mmap supposedly does allocate-on-write for us */ 638 /* 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); 639 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
606 640
607 if (cctx->sptr == (void *)-1) 641 if (cctx->sptr != (void *)-1)
608 {
609 perror ("FATAL: unable to mmap stack for coroutine");
610 _exit (EXIT_FAILURE);
611 } 642 {
612
613# if STACKGUARD 643# if CORO_STACKGUARD
614 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
615# endif 645# endif
616 646 REGISTER_STACK (
617#else 647 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, 648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr,
633 cctx->ssize + (char *)cctx->sptr 649 cctx->ssize + (char *)cctx->sptr
634 ); 650 );
635#endif
636 651
637 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 }
654 else
655#endif
656 {
657 cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long);
658 New (0, cctx->sptr, CORO_STACKSIZE, long);
659
660 if (!cctx->sptr)
661 {
662 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE);
664 }
665
666 REGISTER_STACK (
667 cctx,
668 (char *)cctx->sptr,
669 (char *)cctx->sptr - cctx->ssize
670 );
671
672 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
673 }
638 674
639 return cctx; 675 return cctx;
640} 676}
641 677
642static void 678static void
645 if (!cctx) 681 if (!cctx)
646 return; 682 return;
647 683
648 --cctx_count; 684 --cctx_count;
649 685
650#if USE_VALGRIND 686#if CORO_USE_VALGRIND
651 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
652#endif 688#endif
653 689
654#if HAVE_MMAP 690#if HAVE_MMAP
691 if (cctx->ssize > 0)
655 munmap (cctx->sptr, cctx->ssize); 692 munmap (cctx->sptr, cctx->ssize);
656#else 693 else
694#endif
657 Safefree (cctx->sptr); 695 Safefree (cctx->sptr);
658#endif
659 696
660 Safefree (cctx); 697 Safefree (cctx);
661} 698}
662 699
663static coro_cctx * 700static coro_cctx *
697 ++cctx_idle; 734 ++cctx_idle;
698 cctx->next = cctx_first; 735 cctx->next = cctx_first;
699 cctx_first = cctx; 736 cctx_first = cctx;
700} 737}
701 738
739/** coroutine switching *****************************************************/
740
702/* never call directly, always through the coro_state_transfer global variable */ 741/* never call directly, always through the coro_state_transfer global variable */
703static void NOINLINE 742static void NOINLINE
704transfer (struct coro *prev, struct coro *next) 743transfer (struct coro *prev, struct coro *next)
705{ 744{
706 dSTACKLEVEL; 745 dSTACKLEVEL;
707 746
708 /* sometimes transfer is only called to set idle_sp */ 747 /* sometimes transfer is only called to set idle_sp */
709 if (!next) 748 if (!next)
710 { 749 {
711 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 750 ((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 */ 751 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
713 } 752 }
714 else if (prev != next) 753 else if (prev != next)
715 { 754 {
716 coro_cctx *prev__cctx; 755 coro_cctx *prev__cctx;
717 756
728 if (!prev->flags & CF_RUNNING) 767 if (!prev->flags & CF_RUNNING)
729 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 768 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
730 769
731 if (next->flags & CF_RUNNING) 770 if (next->flags & CF_RUNNING)
732 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 771 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
772
773 if (next->flags & CF_DESTROYED)
774 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
733 775
734 prev->flags &= ~CF_RUNNING; 776 prev->flags &= ~CF_RUNNING;
735 next->flags |= CF_RUNNING; 777 next->flags |= CF_RUNNING;
736 778
737 LOCK; 779 LOCK;
758 800
759 /* possibly "free" the cctx */ 801 /* possibly "free" the cctx */
760 if (prev__cctx->idle_sp == STACKLEVEL) 802 if (prev__cctx->idle_sp == STACKLEVEL)
761 { 803 {
762 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 804 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
763 assert (PL_top_env == prev__cctx->top_env); 805 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
764 806
765 prev->cctx = 0; 807 prev->cctx = 0;
766 808
767 cctx_put (prev__cctx); 809 cctx_put (prev__cctx);
768 prev__cctx->inuse = 0; 810 prev__cctx->inuse = 0;
781 PL_top_env = next->cctx->top_env; 823 PL_top_env = next->cctx->top_env;
782 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
783 } 825 }
784 826
785 free_coro_mortal (); 827 free_coro_mortal ();
786
787 UNLOCK; 828 UNLOCK;
788 } 829 }
789} 830}
790 831
791struct transfer_args 832struct transfer_args
793 struct coro *prev, *next; 834 struct coro *prev, *next;
794}; 835};
795 836
796#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 837#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
797 838
839/** high level stuff ********************************************************/
840
798static void 841static int
799coro_state_destroy (struct coro *coro) 842coro_state_destroy (struct coro *coro)
800{ 843{
801 if (coro->refcnt--) 844 if (coro->flags & CF_DESTROYED)
802 return; 845 return 0;
846
847 coro->flags |= CF_DESTROYED;
848
849 if (coro->flags & CF_READY)
850 {
851 /* reduce nready, as destroying a ready coro effectively unreadies it */
852 /* alternative: look through all ready queues and remove the coro */
853 LOCK;
854 --coro_nready;
855 UNLOCK;
856 }
857 else
858 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
803 859
804 if (coro->mainstack && coro->mainstack != main_mainstack) 860 if (coro->mainstack && coro->mainstack != main_mainstack)
805 { 861 {
806 struct coro temp; 862 struct coro temp;
863
864 assert (!(coro->flags & CF_RUNNING));
865
807 Zero (&temp, 1, struct coro); 866 Zero (&temp, 1, struct coro);
808 temp.save = CORO_SAVE_ALL; 867 temp.save = CORO_SAVE_ALL;
809 868
810 if (coro->flags & CF_RUNNING) 869 if (coro->flags & CF_RUNNING)
811 croak ("FATAL: tried to destroy currently running coroutine"); 870 croak ("FATAL: tried to destroy currently running coroutine");
820 coro->mainstack = 0; 879 coro->mainstack = 0;
821 } 880 }
822 881
823 cctx_destroy (coro->cctx); 882 cctx_destroy (coro->cctx);
824 SvREFCNT_dec (coro->args); 883 SvREFCNT_dec (coro->args);
825 Safefree (coro); 884
885 return 1;
826} 886}
827 887
828static int 888static int
829coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 889coro_state_free (pTHX_ SV *sv, MAGIC *mg)
830{ 890{
831 struct coro *coro = (struct coro *)mg->mg_ptr; 891 struct coro *coro = (struct coro *)mg->mg_ptr;
832 mg->mg_ptr = 0; 892 mg->mg_ptr = 0;
833 893
894 if (--coro->refcnt < 0)
895 {
834 coro_state_destroy (coro); 896 coro_state_destroy (coro);
897 Safefree (coro);
898 }
835 899
836 return 0; 900 return 0;
837} 901}
838 902
839static int 903static int
846 return 0; 910 return 0;
847} 911}
848 912
849static MGVTBL coro_state_vtbl = { 913static MGVTBL coro_state_vtbl = {
850 0, 0, 0, 0, 914 0, 0, 0, 0,
851 coro_state_clear, 915 coro_state_free,
852 0, 916 0,
853#ifdef MGf_DUP 917#ifdef MGf_DUP
854 coro_state_dup, 918 coro_state_dup,
855#else 919#else
856# define MGf_DUP 0 920# define MGf_DUP 0
872 /* very slow, but rare, check */ 936 /* very slow, but rare, check */
873 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 937 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
874 croak ("Coro::State object required"); 938 croak ("Coro::State object required");
875 } 939 }
876 940
877 mg = SvMAGIC (coro); 941 mg = CORO_MAGIC (coro);
878 assert (mg->mg_type == PERL_MAGIC_ext);
879 return (struct coro *)mg->mg_ptr; 942 return (struct coro *)mg->mg_ptr;
880} 943}
881 944
882static void 945static void
883prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 946prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
907 return old_save; 970 return old_save;
908} 971}
909 972
910/** Coro ********************************************************************/ 973/** Coro ********************************************************************/
911 974
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 975static void
925coro_enq (SV *coro_sv) 976coro_enq (SV *coro_sv)
926{ 977{
927 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 978 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
928 coro_nready++;
929} 979}
930 980
931static SV * 981static SV *
932coro_deq (int min_prio) 982coro_deq (int min_prio)
933{ 983{
937 if (min_prio < 0) 987 if (min_prio < 0)
938 min_prio = 0; 988 min_prio = 0;
939 989
940 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 990 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
941 if (AvFILLp (coro_ready [prio]) >= 0) 991 if (AvFILLp (coro_ready [prio]) >= 0)
942 {
943 coro_nready--;
944 return av_shift (coro_ready [prio]); 992 return av_shift (coro_ready [prio]);
945 }
946 993
947 return 0; 994 return 0;
948} 995}
949 996
950static int 997static int
958 coro = SvSTATE (coro_sv); 1005 coro = SvSTATE (coro_sv);
959 1006
960 if (coro->flags & CF_READY) 1007 if (coro->flags & CF_READY)
961 return 0; 1008 return 0;
962 1009
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; 1010 coro->flags |= CF_READY;
969 1011
970 LOCK; 1012 LOCK;
971 coro_enq (SvREFCNT_inc (coro_sv)); 1013 coro_enq (SvREFCNT_inc (coro_sv));
1014 ++coro_nready;
972 UNLOCK; 1015 UNLOCK;
973 1016
974 return 1; 1017 return 1;
975} 1018}
976 1019
977static int 1020static int
978api_is_ready (SV *coro_sv) 1021api_is_ready (SV *coro_sv)
979{ 1022{
980 return !!SvSTATE (coro_sv)->flags & CF_READY; 1023 return !!(SvSTATE (coro_sv)->flags & CF_READY);
981} 1024}
982 1025
983static void 1026static void
984prepare_schedule (struct transfer_args *ta) 1027prepare_schedule (struct transfer_args *ta)
985{ 1028{
986 SV *prev, *next; 1029 SV *prev_sv, *next_sv;
987 1030
988 for (;;) 1031 for (;;)
989 { 1032 {
990 LOCK; 1033 LOCK;
991 next = coro_deq (PRIO_MIN); 1034 next_sv = coro_deq (PRIO_MIN);
1035
1036 /* nothing to schedule: call the idle handler */
1037 if (!next_sv)
1038 {
1039 dSP;
1040 UNLOCK;
1041
1042 ENTER;
1043 SAVETMPS;
1044
1045 PUSHMARK (SP);
1046 PUTBACK;
1047 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1048
1049 FREETMPS;
1050 LEAVE;
1051 continue;
1052 }
1053
1054 ta->next = SvSTATE (next_sv);
1055
1056 /* cannot transfer to destroyed coros, skip and look for next */
1057 if (ta->next->flags & CF_DESTROYED)
1058 {
1059 UNLOCK;
1060 SvREFCNT_dec (next_sv);
1061 /* coro_nready is already taken care of by destroy */
1062 continue;
1063 }
1064
1065 --coro_nready;
992 UNLOCK; 1066 UNLOCK;
993
994 if (next)
995 break; 1067 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 } 1068 }
1010 }
1011
1012 prev = SvRV (coro_current);
1013 SvRV_set (coro_current, next);
1014 1069
1015 /* free this only after the transfer */ 1070 /* free this only after the transfer */
1071 prev_sv = SvRV (coro_current);
1072 SvRV_set (coro_current, next_sv);
1073 ta->prev = SvSTATE (prev_sv);
1074
1075 assert (ta->next->flags & CF_READY);
1076 ta->next->flags &= ~CF_READY;
1077
1016 LOCK; 1078 LOCK;
1017 free_coro_mortal (); 1079 free_coro_mortal ();
1080 coro_mortal = prev_sv;
1018 UNLOCK; 1081 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} 1082}
1030 1083
1031static void 1084static void
1032prepare_cede (struct transfer_args *ta) 1085prepare_cede (struct transfer_args *ta)
1033{ 1086{
1034 api_ready (coro_current); 1087 api_ready (coro_current);
1035
1036 prepare_schedule (ta); 1088 prepare_schedule (ta);
1089}
1090
1091static int
1092prepare_cede_notself (struct transfer_args *ta)
1093{
1094 if (coro_nready)
1095 {
1096 SV *prev = SvRV (coro_current);
1097 prepare_schedule (ta);
1098 api_ready (prev);
1099 return 1;
1100 }
1101 else
1102 return 0;
1037} 1103}
1038 1104
1039static void 1105static void
1040api_schedule (void) 1106api_schedule (void)
1041{ 1107{
1043 1109
1044 prepare_schedule (&ta); 1110 prepare_schedule (&ta);
1045 TRANSFER (ta); 1111 TRANSFER (ta);
1046} 1112}
1047 1113
1048static void 1114static int
1049api_cede (void) 1115api_cede (void)
1050{ 1116{
1051 struct transfer_args ta; 1117 struct transfer_args ta;
1052 1118
1053 prepare_cede (&ta); 1119 prepare_cede (&ta);
1120
1121 if (ta.prev != ta.next)
1122 {
1054 TRANSFER (ta); 1123 TRANSFER (ta);
1124 return 1;
1125 }
1126 else
1127 return 0;
1128}
1129
1130static int
1131api_cede_notself (void)
1132{
1133 struct transfer_args ta;
1134
1135 if (prepare_cede_notself (&ta))
1136 {
1137 TRANSFER (ta);
1138 return 1;
1139 }
1140 else
1141 return 0;
1055} 1142}
1056 1143
1057MODULE = Coro::State PACKAGE = Coro::State 1144MODULE = Coro::State PACKAGE = Coro::State
1058 1145
1059PROTOTYPES: DISABLE 1146PROTOTYPES: DISABLE
1115_set_stacklevel (...) 1202_set_stacklevel (...)
1116 ALIAS: 1203 ALIAS:
1117 Coro::State::transfer = 1 1204 Coro::State::transfer = 1
1118 Coro::schedule = 2 1205 Coro::schedule = 2
1119 Coro::cede = 3 1206 Coro::cede = 3
1207 Coro::cede_notself = 4
1120 CODE: 1208 CODE:
1121{ 1209{
1122 struct transfer_args ta; 1210 struct transfer_args ta;
1123 1211
1124 switch (ix) 1212 switch (ix)
1140 break; 1228 break;
1141 1229
1142 case 3: 1230 case 3:
1143 prepare_cede (&ta); 1231 prepare_cede (&ta);
1144 break; 1232 break;
1233
1234 case 4:
1235 if (!prepare_cede_notself (&ta))
1236 XSRETURN_EMPTY;
1237
1238 break;
1145 } 1239 }
1146 1240
1241 BARRIER;
1147 TRANSFER (ta); 1242 TRANSFER (ta);
1148}
1149 1243
1150void 1244 if (GIMME_V != G_VOID && ta.next != ta.prev)
1151_clone_state_from (SV *dst, SV *src) 1245 XSRETURN_YES;
1246}
1247
1248bool
1249_destroy (SV *coro_sv)
1152 CODE: 1250 CODE:
1153{ 1251 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1154 struct coro *coro_src = SvSTATE (src); 1252 OUTPUT:
1155 1253 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 1254
1162void 1255void
1163_exit (code) 1256_exit (code)
1164 int code 1257 int code
1165 PROTOTYPE: $ 1258 PROTOTYPE: $
1202 coro_ready[i] = newAV (); 1295 coro_ready[i] = newAV ();
1203 1296
1204 { 1297 {
1205 SV *sv = perl_get_sv("Coro::API", 1); 1298 SV *sv = perl_get_sv("Coro::API", 1);
1206 1299
1207 coroapi.schedule = api_schedule; 1300 coroapi.schedule = api_schedule;
1208 coroapi.save = api_save; 1301 coroapi.save = api_save;
1209 coroapi.cede = api_cede; 1302 coroapi.cede = api_cede;
1303 coroapi.cede_notself = api_cede_notself;
1210 coroapi.ready = api_ready; 1304 coroapi.ready = api_ready;
1211 coroapi.is_ready = api_is_ready; 1305 coroapi.is_ready = api_is_ready;
1212 coroapi.nready = &coro_nready; 1306 coroapi.nready = &coro_nready;
1213 coroapi.current = coro_current; 1307 coroapi.current = coro_current;
1214 1308
1215 GCoroAPI = &coroapi; 1309 GCoroAPI = &coroapi;
1216 sv_setiv (sv, (IV)&coroapi); 1310 sv_setiv (sv, (IV)&coroapi);
1217 SvREADONLY_on (sv); 1311 SvREADONLY_on (sv);
1218 } 1312 }
1234 RETVAL = coro->prio; 1328 RETVAL = coro->prio;
1235 1329
1236 if (items > 1) 1330 if (items > 1)
1237 { 1331 {
1238 if (ix) 1332 if (ix)
1239 newprio += coro->prio; 1333 newprio = coro->prio - newprio;
1240 1334
1241 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1335 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1242 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1336 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1243 1337
1244 coro->prio = newprio; 1338 coro->prio = newprio;
1245 } 1339 }
1246} 1340}
1341 OUTPUT:
1342 RETVAL
1247 1343
1248SV * 1344SV *
1249ready (SV *self) 1345ready (SV *self)
1250 PROTOTYPE: $ 1346 PROTOTYPE: $
1251 CODE: 1347 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines