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.16 by root, Wed Jul 25 04:26:55 2001 UTC vs.
Revision 1.49 by pcg, Sun Nov 23 03:34:13 2003 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "patchlevel.h"
6
7#if PATCHLEVEL < 6
8# ifndef PL_ppaddr
9# define PL_ppaddr ppaddr
10# endif
11# ifndef call_sv
12# define call_sv perl_call_sv
13# endif
14# ifndef get_sv
15# define get_sv perl_get_sv
16# endif
17# ifndef get_cv
18# define get_cv perl_get_cv
19# endif
20# ifndef IS_PADGV
21# define IS_PADGV(v) 0
22# endif
23# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0
25# endif
26#endif
27
5#include "libcoro/coro.c" 28#include "libcoro/coro.c"
29
30#include <signal.h>
6 31
7#ifdef HAVE_MMAP 32#ifdef HAVE_MMAP
8# include <unistd.h> 33# include <unistd.h>
9# include <sys/mman.h> 34# include <sys/mman.h>
10# ifndef MAP_ANON
11# ifdef MAP_ANONYMOUS 35# ifndef MAP_ANONYMOUS
12# define MAP_ANON MAP_ANONYMOUS 36# ifdef MAP_ANON
37# define MAP_ANONYMOUS MAP_ANON
13# else 38# else
14# undef HAVE_MMAP 39# undef HAVE_MMAP
15# endif 40# endif
16# endif 41# endif
17#endif 42#endif
18 43
19#define MAY_FLUSH /* increases codesize */ 44#define MAY_FLUSH /* increases codesize and is rarely used */
20
21/* perl-related */
22#define TRANSFER_SAVE_DEFAV 0x00000001
23#define TRANSFER_SAVE_DEFSV 0x00000002
24#define TRANSFER_SAVE_ERRSV 0x00000004
25/* c-related */
26#define TRANSFER_SAVE_CCTXT 0x00000008
27#ifdef CORO_LAZY_STACK
28# define TRANSFER_LAZY_STACK 0x00000010
29#else
30# define TRANSFER_LAZY_STACK 0x00000000
31#endif
32
33#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
34 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
35 45
36#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
37#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
38 48
39/* The next macro should delcare a variable stacklevel that contains and approximation 49/* The next macro should declare a variable stacklevel that contains and approximation
40 * to the current C stack pointer. It's property is that it changes with each call 50 * to the current C stack pointer. Its property is that it changes with each call
41 * and should be unique. */ 51 * and should be unique. */
42#define dSTACKLEVEL void *stacklevel = &stacklevel 52#define dSTACKLEVEL void *stacklevel = &stacklevel
43 53
54#define IN_DESTRUCT (PL_main_cv == Nullcv)
55
44#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
57
58#include "CoroAPI.h"
59
60static struct CoroAPI coroapi;
45 61
46/* this is actually not only the c stack but also c registers etc... */ 62/* this is actually not only the c stack but also c registers etc... */
47typedef struct { 63typedef struct {
48 int refcnt; /* pointer reference counter */ 64 int refcnt; /* pointer reference counter */
49 int usecnt; /* shared by how many coroutines */ 65 int usecnt; /* shared by how many coroutines */
53 69
54 void *sptr; 70 void *sptr;
55 long ssize; /* positive == mmap, otherwise malloc */ 71 long ssize; /* positive == mmap, otherwise malloc */
56} coro_stack; 72} coro_stack;
57 73
58static coro_stack main_stack = { 1, 0, 0 };
59
60struct coro { 74struct coro {
75 /* the top-level JMPENV for each coroutine, needed to catch dies. */
76 JMPENV start_env;
77
61 /* the optional C context */ 78 /* the optional C context */
62 coro_stack *stack; 79 coro_stack *stack;
63 void *cursp; 80 void *cursp;
64 int gencnt; 81 int gencnt;
65 82
68 SV *defsv; 85 SV *defsv;
69 SV *errsv; 86 SV *errsv;
70 87
71 /* saved global state not related to stacks */ 88 /* saved global state not related to stacks */
72 U8 dowarn; 89 U8 dowarn;
90 I32 in_eval;
73 91
74 /* the stacks and related info (callchain etc..) */ 92 /* the stacks and related info (callchain etc..) */
75 PERL_SI *curstackinfo; 93 PERL_SI *curstackinfo;
76 AV *curstack; 94 AV *curstack;
77 AV *mainstack; 95 AV *mainstack;
78 SV **stack_sp; 96 SV **stack_sp;
79 OP *op; 97 OP *op;
80 SV **curpad; 98 SV **curpad;
99 AV *comppad;
81 SV **stack_base; 100 SV **stack_base;
82 SV **stack_max; 101 SV **stack_max;
83 SV **tmps_stack; 102 SV **tmps_stack;
84 I32 tmps_floor; 103 I32 tmps_floor;
85 I32 tmps_ix; 104 I32 tmps_ix;
95 I32 savestack_max; 114 I32 savestack_max;
96 OP **retstack; 115 OP **retstack;
97 I32 retstack_ix; 116 I32 retstack_ix;
98 I32 retstack_max; 117 I32 retstack_max;
99 COP *curcop; 118 COP *curcop;
100 JMPENV start_env;
101 JMPENV *top_env; 119 JMPENV *top_env;
102 120
103 /* data associated with this coroutine (initial args) */ 121 /* data associated with this coroutine (initial args) */
104 AV *args; 122 AV *args;
105}; 123};
110static AV *main_mainstack; /* used to differentiate between $main and others */ 128static AV *main_mainstack; /* used to differentiate between $main and others */
111static HV *coro_state_stash; 129static HV *coro_state_stash;
112static SV *ucoro_state_sv; 130static SV *ucoro_state_sv;
113static U32 ucoro_state_hash; 131static U32 ucoro_state_hash;
114static HV *padlist_cache; 132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
115 134
116/* mostly copied from op.c:cv_clone2 */ 135/* mostly copied from op.c:cv_clone2 */
117STATIC AV * 136STATIC AV *
118clone_padlist (AV *protopadlist) 137clone_padlist (AV *protopadlist)
119{ 138{
165 sv = (SV *) newAV (); 184 sv = (SV *) newAV ();
166 else if (*name == '%') 185 else if (*name == '%')
167 sv = (SV *) newHV (); 186 sv = (SV *) newHV ();
168 else 187 else
169 sv = NEWSV (0, 0); 188 sv = NEWSV (0, 0);
189#ifdef SvPADBUSY
170 if (!SvPADBUSY (sv)) 190 if (!SvPADBUSY (sv))
191#endif
171 SvPADMY_on (sv); 192 SvPADMY_on (sv);
172 npad[ix] = sv; 193 npad[ix] = sv;
173 } 194 }
174 } 195 }
175 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 196 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
206 227
207 return newpadlist; 228 return newpadlist;
208} 229}
209 230
210#ifdef MAY_FLUSH 231#ifdef MAY_FLUSH
211STATIC AV * 232STATIC void
212free_padlist (AV *padlist) 233free_padlist (AV *padlist)
213{ 234{
214 /* may be during global destruction */ 235 /* may be during global destruction */
215 if (SvREFCNT(padlist)) 236 if (SvREFCNT(padlist))
216 { 237 {
281#endif 302#endif
282 303
283#define SB do { 304#define SB do {
284#define SE } while (0) 305#define SE } while (0)
285 306
286#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 307#define LOAD(state) load_state(aTHX_ (state));
287#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 308#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
288 309
289#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 310#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
290 311
291static void 312static void
292load_state(pTHX_ Coro__State c) 313load_state(pTHX_ Coro__State c)
293{ 314{
294 PL_dowarn = c->dowarn; 315 PL_dowarn = c->dowarn;
316 PL_in_eval = c->in_eval;
295 317
296 PL_curstackinfo = c->curstackinfo; 318 PL_curstackinfo = c->curstackinfo;
297 PL_curstack = c->curstack; 319 PL_curstack = c->curstack;
298 PL_mainstack = c->mainstack; 320 PL_mainstack = c->mainstack;
299 PL_stack_sp = c->stack_sp; 321 PL_stack_sp = c->stack_sp;
300 PL_op = c->op; 322 PL_op = c->op;
301 PL_curpad = c->curpad; 323 PL_curpad = c->curpad;
324 PL_comppad = c->comppad;
302 PL_stack_base = c->stack_base; 325 PL_stack_base = c->stack_base;
303 PL_stack_max = c->stack_max; 326 PL_stack_max = c->stack_max;
304 PL_tmps_stack = c->tmps_stack; 327 PL_tmps_stack = c->tmps_stack;
305 PL_tmps_floor = c->tmps_floor; 328 PL_tmps_floor = c->tmps_floor;
306 PL_tmps_ix = c->tmps_ix; 329 PL_tmps_ix = c->tmps_ix;
316 PL_savestack_max = c->savestack_max; 339 PL_savestack_max = c->savestack_max;
317 PL_retstack = c->retstack; 340 PL_retstack = c->retstack;
318 PL_retstack_ix = c->retstack_ix; 341 PL_retstack_ix = c->retstack_ix;
319 PL_retstack_max = c->retstack_max; 342 PL_retstack_max = c->retstack_max;
320 PL_curcop = c->curcop; 343 PL_curcop = c->curcop;
321 PL_start_env = c->start_env;
322 PL_top_env = c->top_env; 344 PL_top_env = c->top_env;
323 345
324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 346 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 347 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 348 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
366 388
367 PUSHs (Nullsv); 389 PUSHs (Nullsv);
368 /* this loop was inspired by pp_caller */ 390 /* this loop was inspired by pp_caller */
369 for (;;) 391 for (;;)
370 { 392 {
371 do 393 while (cxix >= 0)
372 { 394 {
373 PERL_CONTEXT *cx = &ccstk[cxix--]; 395 PERL_CONTEXT *cx = &ccstk[cxix--];
374 396
375 if (CxTYPE(cx) == CXt_SUB) 397 if (CxTYPE(cx) == CXt_SUB)
376 { 398 {
395 PUSHs ((SV *)cv); 417 PUSHs ((SV *)cv);
396 418
397 get_padlist (cv); /* this is a monster */ 419 get_padlist (cv); /* this is a monster */
398 } 420 }
399 } 421 }
422#ifdef CXt_FORMAT
400 else if (CxTYPE(cx) == CXt_FORMAT) 423 else if (CxTYPE(cx) == CXt_FORMAT)
401 { 424 {
402 /* I never used formats, so how should I know how these are implemented? */ 425 /* I never used formats, so how should I know how these are implemented? */
403 /* my bold guess is as a simple, plain sub... */ 426 /* my bold guess is as a simple, plain sub... */
404 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 427 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
405 } 428 }
429#endif
406 } 430 }
407 while (cxix >= 0);
408 431
409 if (top_si->si_type == PERLSI_MAIN) 432 if (top_si->si_type == PERLSI_MAIN)
410 break; 433 break;
411 434
412 top_si = top_si->si_prev; 435 top_si = top_si->si_prev;
419 442
420 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 443 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
421 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 444 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
422 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 445 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
423 446
424 /* I have not the slightest idea of why av_reify is necessary */
425 /* but if it's missing the defav contents magically get replaced sometimes */
426 if (c->defav)
427 av_reify (c->defav);
428
429 c->dowarn = PL_dowarn; 447 c->dowarn = PL_dowarn;
448 c->in_eval = PL_in_eval;
430 449
431 c->curstackinfo = PL_curstackinfo; 450 c->curstackinfo = PL_curstackinfo;
432 c->curstack = PL_curstack; 451 c->curstack = PL_curstack;
433 c->mainstack = PL_mainstack; 452 c->mainstack = PL_mainstack;
434 c->stack_sp = PL_stack_sp; 453 c->stack_sp = PL_stack_sp;
435 c->op = PL_op; 454 c->op = PL_op;
436 c->curpad = PL_curpad; 455 c->curpad = PL_curpad;
456 c->comppad = PL_comppad;
437 c->stack_base = PL_stack_base; 457 c->stack_base = PL_stack_base;
438 c->stack_max = PL_stack_max; 458 c->stack_max = PL_stack_max;
439 c->tmps_stack = PL_tmps_stack; 459 c->tmps_stack = PL_tmps_stack;
440 c->tmps_floor = PL_tmps_floor; 460 c->tmps_floor = PL_tmps_floor;
441 c->tmps_ix = PL_tmps_ix; 461 c->tmps_ix = PL_tmps_ix;
451 c->savestack_max = PL_savestack_max; 471 c->savestack_max = PL_savestack_max;
452 c->retstack = PL_retstack; 472 c->retstack = PL_retstack;
453 c->retstack_ix = PL_retstack_ix; 473 c->retstack_ix = PL_retstack_ix;
454 c->retstack_max = PL_retstack_max; 474 c->retstack_max = PL_retstack_max;
455 c->curcop = PL_curcop; 475 c->curcop = PL_curcop;
456 c->start_env = PL_start_env;
457 c->top_env = PL_top_env; 476 c->top_env = PL_top_env;
458} 477}
459 478
460/* 479/*
461 * allocate various perl stacks. This is an exact copy 480 * allocate various perl stacks. This is an exact copy
482 501
483 New(54,PL_markstack,16,I32); 502 New(54,PL_markstack,16,I32);
484 PL_markstack_ptr = PL_markstack; 503 PL_markstack_ptr = PL_markstack;
485 PL_markstack_max = PL_markstack + 16; 504 PL_markstack_max = PL_markstack + 16;
486 505
506#ifdef SET_MARK_OFFSET
487 SET_MARK_OFFSET; 507 SET_MARK_OFFSET;
508#endif
488 509
489 New(54,PL_scopestack,16,I32); 510 New(54,PL_scopestack,16,I32);
490 PL_scopestack_ix = 0; 511 PL_scopestack_ix = 0;
491 PL_scopestack_max = 16; 512 PL_scopestack_max = 16;
492 513
499 PL_retstack_max = 8; 520 PL_retstack_max = 8;
500} 521}
501 522
502/* 523/*
503 * destroy the stacks, the callchain etc... 524 * destroy the stacks, the callchain etc...
504 * still there is a memleak of 128 bytes...
505 */ 525 */
506STATIC void 526STATIC void
507destroy_stacks(pTHX) 527destroy_stacks(pTHX)
508{ 528{
529 if (!IN_DESTRUCT)
530 {
509 /* is this ugly, I ask? */ 531 /* is this ugly, I ask? */
510 while (PL_scopestack_ix) 532 while (PL_scopestack_ix)
511 LEAVE; 533 LEAVE;
512 534
513 /* sure it is, but more important: is it correct?? :/ */ 535 /* sure it is, but more important: is it correct?? :/ */
514 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 536 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
515 FREETMPS; 537 FREETMPS;
538 }
516 539
517 while (PL_curstackinfo->si_next) 540 while (PL_curstackinfo->si_next)
518 PL_curstackinfo = PL_curstackinfo->si_next; 541 PL_curstackinfo = PL_curstackinfo->si_next;
519 542
520 while (PL_curstackinfo) 543 while (PL_curstackinfo)
525 dSP; 548 dSP;
526 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 549 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
527 PUTBACK; /* possibly superfluous */ 550 PUTBACK; /* possibly superfluous */
528 } 551 }
529 552
553 if (!IN_DESTRUCT)
554 {
530 dounwind(-1); 555 dounwind(-1);
531
532 SvREFCNT_dec(PL_curstackinfo->si_stack); 556 SvREFCNT_dec(PL_curstackinfo->si_stack);
557 }
558
533 Safefree(PL_curstackinfo->si_cxstack); 559 Safefree(PL_curstackinfo->si_cxstack);
534 Safefree(PL_curstackinfo); 560 Safefree(PL_curstackinfo);
535 PL_curstackinfo = p; 561 PL_curstackinfo = p;
536 } 562 }
537 563
552 stack->refcnt = 1; 578 stack->refcnt = 1;
553 stack->usecnt = 1; 579 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0; 580 stack->gencnt = ctx->gencnt = 0;
555 if (alloc) 581 if (alloc)
556 { 582 {
557#ifdef HAVE_MMAP 583#if HAVE_MMAP
558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write */ 584 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 585 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
560 if (stack->sptr == (void *)-1) 586 if (stack->sptr == (void *)-1)
561#endif 587#endif
562 { 588 {
563 /*FIXME*//*D*//* reasonable stack size! */ 589 /*FIXME*//*D*//* reasonable stack size! */
564 stack->ssize = -4096 * sizeof (long); 590 stack->ssize = -4096 * sizeof (long);
584 { 610 {
585#ifdef HAVE_MMAP 611#ifdef HAVE_MMAP
586 if (stack->ssize > 0 && stack->sptr) 612 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize); 613 munmap (stack->sptr, stack->ssize);
588 else 614 else
589#else 615#endif
590 Safefree (stack->sptr); 616 Safefree (stack->sptr);
591#endif 617
592 Safefree (stack); 618 Safefree (stack);
593 } 619 }
594 else if (ctx->gencnt == stack->gencnt) 620 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt; 621 --stack->usecnt;
596 } 622 }
605 dSP; 631 dSP;
606 Coro__State ctx = (Coro__State)arg; 632 Coro__State ctx = (Coro__State)arg;
607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 633 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
608 634
609 coro_init_stacks (aTHX); 635 coro_init_stacks (aTHX);
610 JMPENV_BOOTSTRAP; 636 /*PL_curcop = 0;*/
637 /*PL_in_eval = PL_in_eval;*/ /* inherit */
638 SvREFCNT_dec (GvAV (PL_defgv));
639 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
640
611 SPAGAIN; 641 SPAGAIN;
612
613 /*PL_curcop = 0;*/
614 SvREFCNT_dec (GvAV (PL_defgv));
615 GvAV (PL_defgv) = ctx->args;
616 642
617 if (ctx->stack) 643 if (ctx->stack)
618 { 644 {
619 ctx->cursp = 0; 645 ctx->cursp = 0;
620 646
621 PUSHMARK(SP); 647 PUSHMARK(SP);
622 PUTBACK; 648 PUTBACK;
623 (void) call_sv (sub_init, G_VOID|G_NOARGS); 649 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
650
651 if (SvTRUE (ERRSV))
652 croak (NULL);
653 else
624 croak ("FATAL: CCTXT coroutine returned!"); 654 croak ("FATAL: CCTXT coroutine returned!");
625 } 655 }
626 else 656 else
627 { 657 {
628 UNOP myop; 658 UNOP myop;
629 659
641 * that doesn't matter, though, since it is only 671 * that doesn't matter, though, since it is only
642 * pp_nextstate and we never return... 672 * pp_nextstate and we never return...
643 * ah yes, and I don't care anyways ;) 673 * ah yes, and I don't care anyways ;)
644 */ 674 */
645 PUTBACK; 675 PUTBACK;
646 PL_op = pp_entersub(); 676 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
647 SPAGAIN; 677 SPAGAIN;
648 678
649 ENTER; /* necessary e.g. for dounwind */ 679 ENTER; /* necessary e.g. for dounwind */
650 } 680 }
651} 681}
655{ 685{
656 /* 686 /*
657 * this is a _very_ stripped down perl interpreter ;) 687 * this is a _very_ stripped down perl interpreter ;)
658 */ 688 */
659 Coro__State ctx = (Coro__State)arg; 689 Coro__State ctx = (Coro__State)arg;
690 JMPENV coro_start_env;
691
692 PL_top_env = &ctx->start_env;
660 693
661 ctx->cursp = 0; 694 ctx->cursp = 0;
662 PL_op = PL_op->op_next; 695 PL_op = PL_op->op_next;
663 CALLRUNOPS(aTHX); 696 CALLRUNOPS(aTHX);
664 /*NORETURN*/ 697
665 abort (); 698 abort ();
666} 699}
667 700
668STATIC void 701STATIC void
669transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 702transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
670{ 703{
671 dSP;
672 dSTACKLEVEL; 704 dSTACKLEVEL;
705 static struct coro *xnext;
673 706
674 if (prev != next) 707 if (prev != next)
675 { 708 {
709 xnext = next;
710
676 if (next->mainstack) 711 if (next->mainstack)
677 { 712 {
678 SAVE (prev, flags); 713 SAVE (prev, flags);
679 LOAD (next); 714 LOAD (next);
680 715
703 continue_coro, (void *)next, 738 continue_coro, (void *)next,
704 next->stack->sptr, labs (next->stack->ssize)); 739 next->stack->sptr, labs (next->stack->ssize));
705 } 740 }
706 741
707 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
743 /* don't add any code here */
708 } 744 }
709 745
710 } 746 }
711 else if (next->tmps_ix == -2) 747 else if (next->tmps_ix == -2)
712 croak ("tried to transfer to running coroutine"); 748 croak ("tried to transfer to running coroutine");
719 if (!prev->stack) 755 if (!prev->stack)
720 allocate_stack (prev, 0); 756 allocate_stack (prev, 0);
721 757
722 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 758 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
723 { 759 {
760 PL_top_env = &next->start_env;
761
724 setup_coro (next); 762 setup_coro (next);
725 763
726 prev->stack->refcnt++; 764 prev->stack->refcnt++;
727 prev->stack->usecnt++; 765 prev->stack->usecnt++;
728 next->stack = prev->stack; 766 next->stack = prev->stack;
729 next->gencnt = prev->gencnt; 767 next->gencnt = prev->gencnt;
730 } 768 }
731 else 769 else
732 { 770 {
771 assert (!next->stack);
733 allocate_stack (next, 1); 772 allocate_stack (next, 1);
734 coro_create (&(next->stack->cctx), 773 coro_create (&(next->stack->cctx),
735 setup_coro, (void *)next, 774 setup_coro, (void *)next,
736 next->stack->sptr, labs (next->stack->ssize)); 775 next->stack->sptr, labs (next->stack->ssize));
737 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 776 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
777 /* don't add any code here */
738 } 778 }
739 } 779 }
740 else 780 else
741 setup_coro (next); 781 setup_coro (next);
742 } 782 }
783
784 /*
785 * xnext is now either prev or next, depending on wether
786 * we switched the c stack or not. that's why I use a global
787 * variable, that should become thread-specific at one point.
788 */
789 xnext->cursp = stacklevel;
743 } 790 }
744 791
745 next->cursp = stacklevel; 792 if (coro_mortal)
793 {
794 SvREFCNT_dec (coro_mortal);
795 coro_mortal = 0;
796 }
797}
798
799#define SV_CORO(sv,func) \
800 do { \
801 if (SvROK (sv)) \
802 sv = SvRV (sv); \
803 \
804 if (SvTYPE(sv) == SVt_PVHV) \
805 { \
806 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
807 \
808 if (!he) \
809 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
810 \
811 (sv) = SvRV (HeVAL(he)); \
812 } \
813 \
814 /* must also be changed inside Coro::Cont::yield */ \
815 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
816 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
817 \
818 } while(0)
819
820#define SvSTATE(sv) (struct coro *)SvIV (sv)
821
822static void
823api_transfer(pTHX_ SV *prev, SV *next, int flags)
824{
825 SV_CORO (prev, "Coro::transfer");
826 SV_CORO (next, "Coro::transfer");
827
828 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
829}
830
831/** Coro ********************************************************************/
832
833#define PRIO_MAX 3
834#define PRIO_HIGH 1
835#define PRIO_NORMAL 0
836#define PRIO_LOW -1
837#define PRIO_IDLE -3
838#define PRIO_MIN -4
839
840/* for Coro.pm */
841static GV *coro_current, *coro_idle;
842static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
843static int coro_nready;
844
845static void
846coro_enq (SV *sv)
847{
848 if (SvTYPE (sv) == SVt_PVHV)
849 {
850 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
851 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
852
853 prio = prio > PRIO_MAX ? PRIO_MAX
854 : prio < PRIO_MIN ? PRIO_MIN
855 : prio;
856
857 av_push (coro_ready [prio - PRIO_MIN], sv);
858 coro_nready++;
859
860 return;
861 }
862
863 croak ("Coro::ready tried to enqueue something that is not a coroutine");
864}
865
866static SV *
867coro_deq (int min_prio)
868{
869 int prio = PRIO_MAX - PRIO_MIN;
870
871 min_prio -= PRIO_MIN;
872 if (min_prio < 0)
873 min_prio = 0;
874
875 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
876 if (av_len (coro_ready[prio]) >= 0)
877 {
878 coro_nready--;
879 return av_shift (coro_ready[prio]);
880 }
881
882 return 0;
883}
884
885static void
886api_ready (SV *coro)
887{
888 if (SvROK (coro))
889 coro = SvRV (coro);
890
891 coro_enq (SvREFCNT_inc (coro));
892}
893
894static void
895api_schedule (void)
896{
897 SV *prev, *next;
898
899 prev = SvRV (GvSV (coro_current));
900 next = coro_deq (PRIO_MIN);
901
902 if (!next)
903 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
904
905 /* free this only after the transfer */
906 coro_mortal = prev;
907 SV_CORO (prev, "Coro::schedule");
908
909 SvRV (GvSV (coro_current)) = next;
910
911 SV_CORO (next, "Coro::schedule");
912
913 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
914 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
915}
916
917static void
918api_cede (void)
919{
920 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
921
922 api_schedule ();
746} 923}
747 924
748MODULE = Coro::State PACKAGE = Coro::State 925MODULE = Coro::State PACKAGE = Coro::State
749 926
750PROTOTYPES: ENABLE 927PROTOTYPES: ENABLE
762 939
763 if (!padlist_cache) 940 if (!padlist_cache)
764 padlist_cache = newHV (); 941 padlist_cache = newHV ();
765 942
766 main_mainstack = PL_mainstack; 943 main_mainstack = PL_mainstack;
944
945 coroapi.ver = CORO_API_VERSION;
946 coroapi.transfer = api_transfer;
767} 947}
768 948
769Coro::State 949Coro::State
770_newprocess(args) 950_newprocess(args)
771 SV * args 951 SV * args
774 Coro__State coro; 954 Coro__State coro;
775 955
776 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 956 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
777 croak ("Coro::State::_newprocess expects an arrayref"); 957 croak ("Coro::State::_newprocess expects an arrayref");
778 958
779 New (0, coro, 1, struct coro); 959 Newz (0, coro, 1, struct coro);
780 960
781 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 961 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
782 coro->mainstack = 0; /* actual work is done inside transfer */ 962 coro->mainstack = 0; /* actual work is done inside transfer */
783 coro->stack = 0; 963 coro->stack = 0;
784 964
965 /* same as JMPENV_BOOTSTRAP */
966 /* we might be able to recycle start_env, but safe is safe */
967 //Zero(&coro->start_env, 1, JMPENV);
968 coro->start_env.je_ret = -1;
969 coro->start_env.je_mustcatch = TRUE;
970
785 RETVAL = coro; 971 RETVAL = coro;
786 OUTPUT: 972 OUTPUT:
787 RETVAL 973 RETVAL
788 974
789void 975void
790transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 976transfer(prev, next, flags)
791 Coro::State_or_hashref prev 977 SV *prev
792 Coro::State_or_hashref next 978 SV *next
793 int flags 979 int flags
794 PROTOTYPE: @ 980 PROTOTYPE: @
795 CODE: 981 CODE:
982 PUTBACK;
983 SV_CORO (next, "Coro::transfer");
984 SV_CORO (prev, "Coro::transfer");
796 transfer (aTHX_ prev, next, flags); 985 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
986 SPAGAIN;
797 987
798void 988void
799DESTROY(coro) 989DESTROY(coro)
800 Coro::State coro 990 Coro::State coro
801 CODE: 991 CODE:
802 992
803 if (coro->mainstack && coro->mainstack != main_mainstack) 993 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 994 {
805 struct coro temp; 995 struct coro temp;
806 996
997 PUTBACK;
807 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 998 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
808 LOAD(aTHX_ coro); 999 LOAD(aTHX_ coro);
1000 SPAGAIN;
809 1001
810 destroy_stacks (aTHX); 1002 destroy_stacks (aTHX);
811 1003
812 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1004 LOAD((&temp)); /* this will get rid of defsv etc.. */
1005 SPAGAIN;
813 1006
814 coro->mainstack = 0; 1007 coro->mainstack = 0;
815 } 1008 }
816 1009
817 deallocate_stack (coro); 1010 deallocate_stack (coro);
818 1011 SvREFCNT_dec (coro->args);
819 Safefree (coro); 1012 Safefree (coro);
820 1013
821void 1014void
822flush() 1015flush()
823 CODE: 1016 CODE:
824#ifdef MAY_FLUSH 1017#ifdef MAY_FLUSH
825 flush_padlist_cache (); 1018 flush_padlist_cache ();
826#endif 1019#endif
827 1020
1021void
1022_exit(code)
1023 int code
1024 PROTOTYPE: $
1025 CODE:
1026#if defined(__GLIBC__) || _POSIX_C_SOURCE
1027 _exit (code);
1028#else
1029 signal (SIGTERM, SIG_DFL);
1030 raise (SIGTERM);
1031 exit (code);
1032#endif
1033
828MODULE = Coro::State PACKAGE = Coro::Cont 1034MODULE = Coro::State PACKAGE = Coro::Cont
829 1035
830# this is slightly dirty 1036# this is slightly dirty (should expose a c-level api)
831 1037
832void 1038void
833yield(...) 1039yield(...)
834 PROTOTYPE: @ 1040 PROTOTYPE: @
835 CODE: 1041 CODE:
837 SV *sv; 1043 SV *sv;
838 AV *defav = GvAV (PL_defgv); 1044 AV *defav = GvAV (PL_defgv);
839 struct coro *prev, *next; 1045 struct coro *prev, *next;
840 1046
841 if (!returnstk) 1047 if (!returnstk)
842 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1048 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
843 1049
844 /* set up @_ -- ugly */ 1050 /* set up @_ -- ugly */
845 av_clear (defav); 1051 av_clear (defav);
846 av_fill (defav, items - 1); 1052 av_fill (defav, items - 1);
847 while (items--) 1053 while (items--)
853 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1059 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
854 SvREFCNT_dec (sv); 1060 SvREFCNT_dec (sv);
855 1061
856 transfer(aTHX_ prev, next, 0); 1062 transfer(aTHX_ prev, next, 0);
857 1063
1064MODULE = Coro::State PACKAGE = Coro
1065
1066# this is slightly dirty (should expose a c-level api)
1067
1068BOOT:
1069{
1070 int i;
1071 HV *stash = gv_stashpv ("Coro", TRUE);
1072
1073 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1074 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1075 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1076 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1077 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1078 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1079
1080 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1081 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1082
1083 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1084 coro_ready[i] = newAV ();
1085
1086 {
1087 SV *sv = perl_get_sv("Coro::API", 1);
1088
1089 coroapi.schedule = api_schedule;
1090 coroapi.cede = api_cede;
1091 coroapi.ready = api_ready;
1092 coroapi.nready = &coro_nready;
1093 coroapi.current = coro_current;
1094
1095 GCoroAPI = &coroapi;
1096 sv_setiv(sv, (IV)&coroapi);
1097 SvREADONLY_on(sv);
1098 }
1099}
1100
1101void
1102ready(self)
1103 SV * self
1104 PROTOTYPE: $
1105 CODE:
1106 api_ready (self);
1107
1108int
1109nready(...)
1110 PROTOTYPE:
1111 CODE:
1112 RETVAL = coro_nready;
1113 OUTPUT:
1114 RETVAL
1115
1116void
1117schedule(...)
1118 PROTOTYPE:
1119 CODE:
1120 api_schedule ();
1121
1122void
1123cede(...)
1124 PROTOTYPE:
1125 CODE:
1126 api_cede ();
1127

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines