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.122 by root, Mon Dec 4 13:47:56 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 */
157 /* optionally saved, might be zero */ 176 /* optionally saved, might be zero */
158 AV *defav; /* @_ */ 177 AV *defav; /* @_ */
159 SV *defsv; /* $_ */ 178 SV *defsv; /* $_ */
160 SV *errsv; /* $@ */ 179 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */ 180 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */
162 182
163#define VAR(name,type) type name; 183#define VAR(name,type) type name;
164# include "state.h" 184# include "state.h"
165#undef VAR 185#undef VAR
166 186
168 int prio; 188 int prio;
169}; 189};
170 190
171typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
172typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
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 **********************************************************/
173 209
174static AV * 210static AV *
175coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
176{ 212{
177 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
252 288
253 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
254 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
255 else 291 else
256 { 292 {
257#if 0 293#if PREFER_PERL_FUNCTIONS
258 /* this is probably cleaner, but also slower? */ 294 /* this is probably cleaner, but also slower? */
259 CV *cp = Perl_cv_clone (cv); 295 CV *cp = Perl_cv_clone (cv);
260 CvPADLIST (cv) = CvPADLIST (cp); 296 CvPADLIST (cv) = CvPADLIST (cp);
261 CvPADLIST (cp) = 0; 297 CvPADLIST (cp) = 0;
262 SvREFCNT_dec (cp); 298 SvREFCNT_dec (cp);
286 av_extend (av, AvMAX (av) + 1); 322 av_extend (av, AvMAX (av) + 1);
287 323
288 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
289} 325}
290 326
327/** load & save, init *******************************************************/
328
291#define SB do { 329#define SB do {
292#define SE } while (0) 330#define SE } while (0)
293 331
294#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
295 333
301#undef VAR 339#undef VAR
302 340
303 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
304 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
305 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
306 if (c->irssv) REPLACE_SV (PL_rs , c->irssv); 344 if (c->irssv)
345 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
347 SvREFCNT_dec (c->irssv);
348 else
349 {
350 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs);
353 }
354 }
307 355
308 { 356 {
309 dSP; 357 dSP;
310 CV *cv; 358 CV *cv;
311 359
333 /* 381 /*
334 * 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
335 * (and reinitialize) all cv's in the whole callchain :( 383 * (and reinitialize) all cv's in the whole callchain :(
336 */ 384 */
337 385
386 EXTEND (SP, 3 + 1);
338 PUSHs (Nullsv); 387 PUSHs (Nullsv);
339 /* this loop was inspired by pp_caller */ 388 /* this loop was inspired by pp_caller */
340 for (;;) 389 for (;;)
341 { 390 {
342 while (cxix >= 0) 391 while (cxix >= 0)
348 CV *cv = cx->blk_sub.cv; 397 CV *cv = cx->blk_sub.cv;
349 398
350 if (CvDEPTH (cv)) 399 if (CvDEPTH (cv))
351 { 400 {
352 EXTEND (SP, 3); 401 EXTEND (SP, 3);
353
354 PUSHs ((SV *)CvPADLIST (cv)); 402 PUSHs ((SV *)CvPADLIST (cv));
355 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 403 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
356 PUSHs ((SV *)cv); 404 PUSHs ((SV *)cv);
357 405
358 CvDEPTH (cv) = 0; 406 CvDEPTH (cv) = 0;
359 get_padlist (cv); 407 get_padlist (cv);
360 } 408 }
361 } 409 }
362#ifdef CXt_FORMAT
363 else if (CxTYPE (cx) == CXt_FORMAT)
364 {
365 /* I never used formats, so how should I know how these are implemented? */
366 /* my bold guess is as a simple, plain sub... */
367 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
368 }
369#endif
370 } 410 }
371 411
372 if (top_si->si_type == PERLSI_MAIN) 412 if (top_si->si_type == PERLSI_MAIN)
373 break; 413 break;
374 414
394 * allocate various perl stacks. This is an exact copy 434 * allocate various perl stacks. This is an exact copy
395 * of perl.c:init_stacks, except that it uses less memory 435 * of perl.c:init_stacks, except that it uses less memory
396 * on the (sometimes correct) assumption that coroutines do 436 * on the (sometimes correct) assumption that coroutines do
397 * not usually need a lot of stackspace. 437 * not usually need a lot of stackspace.
398 */ 438 */
439#if PREFER_PERL_FUNCTIONS
440# define coro_init_stacks init_stacks
441#else
399static void 442static void
400coro_init_stacks () 443coro_init_stacks ()
401{ 444{
402 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 445 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
403 PL_curstackinfo->si_type = PERLSI_MAIN; 446 PL_curstackinfo->si_type = PERLSI_MAIN;
433 New(54,PL_retstack,16,OP*); 476 New(54,PL_retstack,16,OP*);
434 PL_retstack_ix = 0; 477 PL_retstack_ix = 0;
435 PL_retstack_max = 16; 478 PL_retstack_max = 16;
436#endif 479#endif
437} 480}
481#endif
438 482
439/* 483/*
440 * destroy the stacks, the callchain etc... 484 * destroy the stacks, the callchain etc...
441 */ 485 */
442static void 486static void
443coro_destroy_stacks () 487coro_destroy_stacks ()
444{ 488{
445 if (!IN_DESTRUCT) 489 if (!IN_DESTRUCT)
446 { 490 {
447 /* is this ugly, I ask? */ 491 /* restore all saved variables and stuff */
448 LEAVE_SCOPE (0); 492 LEAVE_SCOPE (0);
493 assert (PL_tmps_floor == -1);
449 494
450 /* sure it is, but more important: is it correct?? :/ */ 495 /* free all temporaries */
451 FREETMPS; 496 FREETMPS;
497 assert (PL_tmps_ix == -1);
452 498
499 /* unwind all extra stacks */
453 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 500 POPSTACK_TO (PL_mainstack);
501
502 /* unwind main stack */
503 dounwind (-1);
454 } 504 }
455 505
456 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
457 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
458 508
459 while (PL_curstackinfo) 509 while (PL_curstackinfo)
460 { 510 {
461 PERL_SI *p = PL_curstackinfo->si_prev; 511 PERL_SI *p = PL_curstackinfo->si_prev;
462 512
463 { /*D*//*remove*/
464 dSP;
465 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
466 PUTBACK; /* possibly superfluous */
467 }
468
469 if (!IN_DESTRUCT) 513 if (!IN_DESTRUCT)
470 {
471 dounwind (-1);/*D*//*remove*/
472 SvREFCNT_dec (PL_curstackinfo->si_stack); 514 SvREFCNT_dec (PL_curstackinfo->si_stack);
473 }
474 515
475 Safefree (PL_curstackinfo->si_cxstack); 516 Safefree (PL_curstackinfo->si_cxstack);
476 Safefree (PL_curstackinfo); 517 Safefree (PL_curstackinfo);
477 PL_curstackinfo = p; 518 PL_curstackinfo = p;
478 } 519 }
483 Safefree (PL_savestack); 524 Safefree (PL_savestack);
484#if !PERL_VERSION_ATLEAST (5,9,0) 525#if !PERL_VERSION_ATLEAST (5,9,0)
485 Safefree (PL_retstack); 526 Safefree (PL_retstack);
486#endif 527#endif
487} 528}
529
530/** coroutine stack handling ************************************************/
488 531
489static void 532static void
490setup_coro (struct coro *coro) 533setup_coro (struct coro *coro)
491{ 534{
492 /* 535 /*
582{ 625{
583 coro_cctx *cctx; 626 coro_cctx *cctx;
584 627
585 ++cctx_count; 628 ++cctx_count;
586 629
587 New (0, cctx, 1, coro_cctx); 630 Newz (0, cctx, 1, coro_cctx);
588 631
589#if HAVE_MMAP 632#if HAVE_MMAP
590 633
591 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 634 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
592 /* mmap supposedly does allocate-on-write for us */ 635 /* mmap supposedly does allocate-on-write for us */
593 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);
594 637
595 if (cctx->sptr == (void *)-1) 638 if (cctx->sptr != (void *)-1)
596 {
597 perror ("FATAL: unable to mmap stack for coroutine");
598 _exit (EXIT_FAILURE);
599 } 639 {
600
601# if STACKGUARD 640# if STACKGUARD
602 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 641 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
603# endif 642# endif
604 643 REGISTER_STACK (
605#else 644 cctx,
606
607 cctx->ssize = STACKSIZE * (long)sizeof (long);
608 New (0, cctx->sptr, STACKSIZE, long);
609
610 if (!cctx->sptr)
611 {
612 perror ("FATAL: unable to malloc stack for coroutine");
613 _exit (EXIT_FAILURE);
614 }
615
616#endif
617
618#if USE_VALGRIND
619 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
620 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 645 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
621 cctx->ssize + (char *)cctx->sptr 646 cctx->ssize + (char *)cctx->sptr
622 ); 647 );
623#endif
624 648
625 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 }
626 671
627 return cctx; 672 return cctx;
628} 673}
629 674
630static void 675static void
638#if USE_VALGRIND 683#if USE_VALGRIND
639 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 684 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
640#endif 685#endif
641 686
642#if HAVE_MMAP 687#if HAVE_MMAP
688 if (cctx->ssize > 0)
643 munmap (cctx->sptr, cctx->ssize); 689 munmap (cctx->sptr, cctx->ssize);
644#else 690 else
691#endif
645 Safefree (cctx->sptr); 692 Safefree (cctx->sptr);
646#endif
647 693
648 Safefree (cctx); 694 Safefree (cctx);
649} 695}
650 696
651static coro_cctx * 697static coro_cctx *
685 ++cctx_idle; 731 ++cctx_idle;
686 cctx->next = cctx_first; 732 cctx->next = cctx_first;
687 cctx_first = cctx; 733 cctx_first = cctx;
688} 734}
689 735
736/** coroutine switching *****************************************************/
737
690/* never call directly, always through the coro_state_transfer global variable */ 738/* never call directly, always through the coro_state_transfer global variable */
691static void NOINLINE 739static void NOINLINE
692transfer (struct coro *prev, struct coro *next) 740transfer (struct coro *prev, struct coro *next)
693{ 741{
694 dSTACKLEVEL; 742 dSTACKLEVEL;
695 743
696 /* sometimes transfer is only called to set idle_sp */ 744 /* sometimes transfer is only called to set idle_sp */
697 if (!next) 745 if (!next)
746 {
698 ((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 }
699 else if (prev != next) 750 else if (prev != next)
700 { 751 {
701 coro_cctx *prev__cctx; 752 coro_cctx *prev__cctx;
702 753
703 if (prev->flags & CF_NEW) 754 if (prev->flags & CF_NEW)
713 if (!prev->flags & CF_RUNNING) 764 if (!prev->flags & CF_RUNNING)
714 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");
715 766
716 if (next->flags & CF_RUNNING) 767 if (next->flags & CF_RUNNING)
717 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");
718 772
719 prev->flags &= ~CF_RUNNING; 773 prev->flags &= ~CF_RUNNING;
720 next->flags |= CF_RUNNING; 774 next->flags |= CF_RUNNING;
721 775
722 LOCK; 776 LOCK;
728 /* first get rid of the old state */ 782 /* first get rid of the old state */
729 save_perl (prev); 783 save_perl (prev);
730 /* setup coroutine call */ 784 /* setup coroutine call */
731 setup_coro (next); 785 setup_coro (next);
732 /* need a new stack */ 786 /* need a new stack */
733 assert (!next->stack); 787 assert (!next->cctx);
734 } 788 }
735 else 789 else
736 { 790 {
737 /* coroutine already started */ 791 /* coroutine already started */
738 save_perl (prev); 792 save_perl (prev);
743 797
744 /* possibly "free" the cctx */ 798 /* possibly "free" the cctx */
745 if (prev__cctx->idle_sp == STACKLEVEL) 799 if (prev__cctx->idle_sp == STACKLEVEL)
746 { 800 {
747 /* 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 */
748 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));
749 803
750 prev->cctx = 0; 804 prev->cctx = 0;
751 805
752 cctx_put (prev__cctx); 806 cctx_put (prev__cctx);
753 prev__cctx->inuse = 0; 807 prev__cctx->inuse = 0;
766 PL_top_env = next->cctx->top_env; 820 PL_top_env = next->cctx->top_env;
767 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 821 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
768 } 822 }
769 823
770 free_coro_mortal (); 824 free_coro_mortal ();
771
772 UNLOCK; 825 UNLOCK;
773 } 826 }
774} 827}
775 828
776struct transfer_args 829struct transfer_args
778 struct coro *prev, *next; 831 struct coro *prev, *next;
779}; 832};
780 833
781#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 834#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
782 835
836/** high level stuff ********************************************************/
837
783static void 838static int
784coro_state_destroy (struct coro *coro) 839coro_state_destroy (struct coro *coro)
785{ 840{
786 if (coro->refcnt--) 841 if (coro->flags & CF_DESTROYED)
787 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 */
788 856
789 if (coro->mainstack && coro->mainstack != main_mainstack) 857 if (coro->mainstack && coro->mainstack != main_mainstack)
790 { 858 {
791 struct coro temp; 859 struct coro temp;
860
861 assert (!(coro->flags & CF_RUNNING));
862
792 Zero (&temp, 1, struct coro); 863 Zero (&temp, 1, struct coro);
793 temp.save = CORO_SAVE_ALL; 864 temp.save = CORO_SAVE_ALL;
794 865
795 if (coro->flags & CF_RUNNING) 866 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine"); 867 croak ("FATAL: tried to destroy currently running coroutine");
805 coro->mainstack = 0; 876 coro->mainstack = 0;
806 } 877 }
807 878
808 cctx_destroy (coro->cctx); 879 cctx_destroy (coro->cctx);
809 SvREFCNT_dec (coro->args); 880 SvREFCNT_dec (coro->args);
810 Safefree (coro); 881
882 return 1;
811} 883}
812 884
813static int 885static int
814coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 886coro_state_free (pTHX_ SV *sv, MAGIC *mg)
815{ 887{
816 struct coro *coro = (struct coro *)mg->mg_ptr; 888 struct coro *coro = (struct coro *)mg->mg_ptr;
817 mg->mg_ptr = 0; 889 mg->mg_ptr = 0;
818 890
891 if (--coro->refcnt < 0)
892 {
819 coro_state_destroy (coro); 893 coro_state_destroy (coro);
894 Safefree (coro);
895 }
820 896
821 return 0; 897 return 0;
822} 898}
823 899
824static int 900static int
831 return 0; 907 return 0;
832} 908}
833 909
834static MGVTBL coro_state_vtbl = { 910static MGVTBL coro_state_vtbl = {
835 0, 0, 0, 0, 911 0, 0, 0, 0,
836 coro_state_clear, 912 coro_state_free,
837 0, 913 0,
838#ifdef MGf_DUP 914#ifdef MGf_DUP
839 coro_state_dup, 915 coro_state_dup,
840#else 916#else
841# define MGf_DUP 0 917# define MGf_DUP 0
892 return old_save; 968 return old_save;
893} 969}
894 970
895/** Coro ********************************************************************/ 971/** Coro ********************************************************************/
896 972
897#define PRIO_MAX 3
898#define PRIO_HIGH 1
899#define PRIO_NORMAL 0
900#define PRIO_LOW -1
901#define PRIO_IDLE -3
902#define PRIO_MIN -4
903
904/* for Coro.pm */
905static SV *coro_current;
906static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
907static int coro_nready;
908
909static void 973static void
910coro_enq (SV *coro_sv) 974coro_enq (SV *coro_sv)
911{ 975{
912 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);
913 coro_nready++;
914} 977}
915 978
916static SV * 979static SV *
917coro_deq (int min_prio) 980coro_deq (int min_prio)
918{ 981{
922 if (min_prio < 0) 985 if (min_prio < 0)
923 min_prio = 0; 986 min_prio = 0;
924 987
925 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 988 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
926 if (AvFILLp (coro_ready [prio]) >= 0) 989 if (AvFILLp (coro_ready [prio]) >= 0)
927 {
928 coro_nready--;
929 return av_shift (coro_ready [prio]); 990 return av_shift (coro_ready [prio]);
930 }
931 991
932 return 0; 992 return 0;
933} 993}
934 994
935static int 995static int
943 coro = SvSTATE (coro_sv); 1003 coro = SvSTATE (coro_sv);
944 1004
945 if (coro->flags & CF_READY) 1005 if (coro->flags & CF_READY)
946 return 0; 1006 return 0;
947 1007
948#if 0 /* this is actually harmless */
949 if (coro->flags & CF_RUNNING)
950 croak ("Coro::ready called on currently running coroutine");
951#endif
952
953 coro->flags |= CF_READY; 1008 coro->flags |= CF_READY;
954 1009
955 LOCK; 1010 LOCK;
956 coro_enq (SvREFCNT_inc (coro_sv)); 1011 coro_enq (SvREFCNT_inc (coro_sv));
1012 ++coro_nready;
957 UNLOCK; 1013 UNLOCK;
958 1014
959 return 1; 1015 return 1;
960} 1016}
961 1017
962static int 1018static int
963api_is_ready (SV *coro_sv) 1019api_is_ready (SV *coro_sv)
964{ 1020{
965 return !!SvSTATE (coro_sv)->flags & CF_READY; 1021 return !!(SvSTATE (coro_sv)->flags & CF_READY);
966} 1022}
967 1023
968static void 1024static void
969prepare_schedule (struct transfer_args *ta) 1025prepare_schedule (struct transfer_args *ta)
970{ 1026{
971 SV *prev, *next; 1027 SV *prev_sv, *next_sv;
972 1028
973 for (;;) 1029 for (;;)
974 { 1030 {
975 LOCK; 1031 LOCK;
976 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;
977 UNLOCK; 1064 UNLOCK;
978
979 if (next)
980 break; 1065 break;
981
982 {
983 dSP;
984
985 ENTER;
986 SAVETMPS;
987
988 PUSHMARK (SP);
989 PUTBACK;
990 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
991
992 FREETMPS;
993 LEAVE;
994 } 1066 }
995 }
996
997 prev = SvRV (coro_current);
998 SvRV_set (coro_current, next);
999 1067
1000 /* 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
1001 LOCK; 1076 LOCK;
1002 free_coro_mortal (); 1077 free_coro_mortal ();
1078 coro_mortal = prev_sv;
1003 UNLOCK; 1079 UNLOCK;
1004 coro_mortal = prev;
1005
1006 assert (!SvROK(prev));//D
1007 assert (!SvROK(next));//D
1008
1009 ta->prev = SvSTATE (prev);
1010 ta->next = SvSTATE (next);
1011
1012 assert (ta->next->flags & CF_READY);
1013 ta->next->flags &= ~CF_READY;
1014} 1080}
1015 1081
1016static void 1082static void
1017prepare_cede (struct transfer_args *ta) 1083prepare_cede (struct transfer_args *ta)
1018{ 1084{
1019 api_ready (coro_current); 1085 api_ready (coro_current);
1020
1021 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;
1022} 1101}
1023 1102
1024static void 1103static void
1025api_schedule (void) 1104api_schedule (void)
1026{ 1105{
1028 1107
1029 prepare_schedule (&ta); 1108 prepare_schedule (&ta);
1030 TRANSFER (ta); 1109 TRANSFER (ta);
1031} 1110}
1032 1111
1033static void 1112static int
1034api_cede (void) 1113api_cede (void)
1035{ 1114{
1036 struct transfer_args ta; 1115 struct transfer_args ta;
1037 1116
1038 prepare_cede (&ta); 1117 prepare_cede (&ta);
1118
1119 if (ta.prev != ta.next)
1120 {
1039 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;
1040} 1140}
1041 1141
1042MODULE = Coro::State PACKAGE = Coro::State 1142MODULE = Coro::State PACKAGE = Coro::State
1043 1143
1044PROTOTYPES: DISABLE 1144PROTOTYPES: DISABLE
1100_set_stacklevel (...) 1200_set_stacklevel (...)
1101 ALIAS: 1201 ALIAS:
1102 Coro::State::transfer = 1 1202 Coro::State::transfer = 1
1103 Coro::schedule = 2 1203 Coro::schedule = 2
1104 Coro::cede = 3 1204 Coro::cede = 3
1205 Coro::cede_notself = 4
1105 CODE: 1206 CODE:
1106{ 1207{
1107 struct transfer_args ta; 1208 struct transfer_args ta;
1108 1209
1109 switch (ix) 1210 switch (ix)
1125 break; 1226 break;
1126 1227
1127 case 3: 1228 case 3:
1128 prepare_cede (&ta); 1229 prepare_cede (&ta);
1129 break; 1230 break;
1231
1232 case 4:
1233 if (!prepare_cede_notself (&ta))
1234 XSRETURN_EMPTY;
1235
1236 break;
1130 } 1237 }
1131 1238
1239 BARRIER;
1132 TRANSFER (ta); 1240 TRANSFER (ta);
1133}
1134 1241
1135void 1242 if (GIMME_V != G_VOID && ta.next != ta.prev)
1136_clone_state_from (SV *dst, SV *src) 1243 XSRETURN_YES;
1244}
1245
1246bool
1247_destroy (SV *coro_sv)
1137 CODE: 1248 CODE:
1138{ 1249 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1139 struct coro *coro_src = SvSTATE (src); 1250 OUTPUT:
1140 1251 RETVAL
1141 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1142
1143 ++coro_src->refcnt;
1144 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1145}
1146 1252
1147void 1253void
1148_exit (code) 1254_exit (code)
1149 int code 1255 int code
1150 PROTOTYPE: $ 1256 PROTOTYPE: $
1187 coro_ready[i] = newAV (); 1293 coro_ready[i] = newAV ();
1188 1294
1189 { 1295 {
1190 SV *sv = perl_get_sv("Coro::API", 1); 1296 SV *sv = perl_get_sv("Coro::API", 1);
1191 1297
1192 coroapi.schedule = api_schedule; 1298 coroapi.schedule = api_schedule;
1193 coroapi.save = api_save; 1299 coroapi.save = api_save;
1194 coroapi.cede = api_cede; 1300 coroapi.cede = api_cede;
1301 coroapi.cede_notself = api_cede_notself;
1195 coroapi.ready = api_ready; 1302 coroapi.ready = api_ready;
1196 coroapi.is_ready = api_is_ready; 1303 coroapi.is_ready = api_is_ready;
1197 coroapi.nready = &coro_nready; 1304 coroapi.nready = &coro_nready;
1198 coroapi.current = coro_current; 1305 coroapi.current = coro_current;
1199 1306
1200 GCoroAPI = &coroapi; 1307 GCoroAPI = &coroapi;
1201 sv_setiv (sv, (IV)&coroapi); 1308 sv_setiv (sv, (IV)&coroapi);
1202 SvREADONLY_on (sv); 1309 SvREADONLY_on (sv);
1203 } 1310 }
1219 RETVAL = coro->prio; 1326 RETVAL = coro->prio;
1220 1327
1221 if (items > 1) 1328 if (items > 1)
1222 { 1329 {
1223 if (ix) 1330 if (ix)
1224 newprio += coro->prio; 1331 newprio = coro->prio - newprio;
1225 1332
1226 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1333 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1227 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1334 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1228 1335
1229 coro->prio = newprio; 1336 coro->prio = newprio;
1230 } 1337 }
1231} 1338}
1339 OUTPUT:
1340 RETVAL
1232 1341
1233SV * 1342SV *
1234ready (SV *self) 1343ready (SV *self)
1235 PROTOTYPE: $ 1344 PROTOTYPE: $
1236 CODE: 1345 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines