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.10 by root, Sat Jul 21 04:19:34 2001 UTC vs.
Revision 1.22 by root, Sat Aug 11 23:10:56 2001 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#if 1 5#include "libcoro/coro.c"
6# define CHK(x) (void *)0 6
7#include <signal.h>
8
9#ifdef HAVE_MMAP
10# include <unistd.h>
11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS
15# else
16# undef HAVE_MMAP
17# endif
18# endif
19#endif
20
21#define MAY_FLUSH /* increases codesize */
22
23/* perl-related */
24#define TRANSFER_SAVE_DEFAV 0x00000001
25#define TRANSFER_SAVE_DEFSV 0x00000002
26#define TRANSFER_SAVE_ERRSV 0x00000004
27/* c-related */
28#define TRANSFER_SAVE_CCTXT 0x00000008
29#ifdef CORO_LAZY_STACK
30# define TRANSFER_LAZY_STACK 0x00000010
7#else 31#else
8# define CHK(x) if (!(x)) croak("FATAL, CHK: " #x) 32# define TRANSFER_LAZY_STACK 0x00000000
9#endif 33#endif
10 34
11#define MAY_FLUSH /* increases codesize */ 35#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
36 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
12 37
13#define SUB_INIT "Coro::State::coroutine_initialization" 38#define SUB_INIT "Coro::State::initialize"
39#define UCORO_STATE "_coro_state"
14 40
15#define SAVE_DEFAV 0x00000001 41/* The next macro should delcare a variable stacklevel that contains and approximation
16#define SAVE_DEFSV 0x00000002 42 * to the current C stack pointer. It's property is that it changes with each call
17#define SAVE_ERRSV 0x00000004 43 * and should be unique. */
44#define dSTACKLEVEL void *stacklevel = &stacklevel
18 45
19#define SAVE_ALL -1 46#define labs(l) ((l) >= 0 ? (l) : -(l))
47
48/* this is actually not only the c stack but also c registers etc... */
49typedef struct {
50 int refcnt; /* pointer reference counter */
51 int usecnt; /* shared by how many coroutines */
52 int gencnt; /* generation counter */
53
54 coro_context cctx;
55
56 void *sptr;
57 long ssize; /* positive == mmap, otherwise malloc */
58} coro_stack;
20 59
21struct coro { 60struct coro {
61 /* the optional C context */
62 coro_stack *stack;
63 void *cursp;
64 int gencnt;
65
22 /* optionally saved, might be zero */ 66 /* optionally saved, might be zero */
23 AV *defav; 67 AV *defav;
24 SV *defsv; 68 SV *defsv;
25 SV *errsv; 69 SV *errsv;
26 70
27 /* saved global state not related to stacks */ 71 /* saved global state not related to stacks */
28 U8 dowarn; 72 U8 dowarn;
73 I32 in_eval;
29 74
30 /* the stacks and related info (callchain etc..) */ 75 /* the stacks and related info (callchain etc..) */
31 PERL_SI *curstackinfo; 76 PERL_SI *curstackinfo;
32 AV *curstack; 77 AV *curstack;
33 AV *mainstack; 78 AV *mainstack;
51 I32 savestack_max; 96 I32 savestack_max;
52 OP **retstack; 97 OP **retstack;
53 I32 retstack_ix; 98 I32 retstack_ix;
54 I32 retstack_max; 99 I32 retstack_max;
55 COP *curcop; 100 COP *curcop;
101 JMPENV *top_env;
56 102
57 /* data associated with this coroutine (initial args) */ 103 /* data associated with this coroutine (initial args) */
58 AV *args; 104 AV *args;
59}; 105};
60 106
61typedef struct coro *Coro__State; 107typedef struct coro *Coro__State;
62typedef struct coro *Coro__State_or_hashref; 108typedef struct coro *Coro__State_or_hashref;
63 109
110static AV *main_mainstack; /* used to differentiate between $main and others */
111static HV *coro_state_stash;
112static SV *ucoro_state_sv;
113static U32 ucoro_state_hash;
64static HV *padlist_cache; 114static HV *padlist_cache;
65 115
66/* mostly copied from op.c:cv_clone2 */ 116/* mostly copied from op.c:cv_clone2 */
67STATIC AV * 117STATIC AV *
68clone_padlist (AV *protopadlist) 118clone_padlist (AV *protopadlist)
231#endif 281#endif
232 282
233#define SB do { 283#define SB do {
234#define SE } while (0) 284#define SE } while (0)
235 285
236#define LOAD(state) SB load_state(aTHX_ state); SPAGAIN; SE 286#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE
237#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ state,flags); SE 287#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE
238 288
239#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 289#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE
240 290
241static void 291static void
242load_state(pTHX_ Coro__State c) 292load_state(pTHX_ Coro__State c)
243{ 293{
244 PL_dowarn = c->dowarn; 294 PL_dowarn = c->dowarn;
295 PL_in_eval = c->in_eval;
245 296
246 PL_curstackinfo = c->curstackinfo; 297 PL_curstackinfo = c->curstackinfo;
247 PL_curstack = c->curstack; 298 PL_curstack = c->curstack;
248 PL_mainstack = c->mainstack; 299 PL_mainstack = c->mainstack;
249 PL_stack_sp = c->stack_sp; 300 PL_stack_sp = c->stack_sp;
266 PL_savestack_max = c->savestack_max; 317 PL_savestack_max = c->savestack_max;
267 PL_retstack = c->retstack; 318 PL_retstack = c->retstack;
268 PL_retstack_ix = c->retstack_ix; 319 PL_retstack_ix = c->retstack_ix;
269 PL_retstack_max = c->retstack_max; 320 PL_retstack_max = c->retstack_max;
270 PL_curcop = c->curcop; 321 PL_curcop = c->curcop;
322 PL_top_env = c->top_env;
271 323
272 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
273 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
274 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
275 327
302save_state(pTHX_ Coro__State c, int flags) 354save_state(pTHX_ Coro__State c, int flags)
303{ 355{
304 { 356 {
305 dSP; 357 dSP;
306 I32 cxix = cxstack_ix; 358 I32 cxix = cxstack_ix;
359 PERL_CONTEXT *ccstk = cxstack;
307 PERL_SI *top_si = PL_curstackinfo; 360 PERL_SI *top_si = PL_curstackinfo;
308 PERL_CONTEXT *ccstk = cxstack;
309 361
310 /* 362 /*
311 * the worst thing you can imagine happens first - we have to save 363 * the worst thing you can imagine happens first - we have to save
312 * (and reinitialize) all cv's in the whole callchain :( 364 * (and reinitialize) all cv's in the whole callchain :(
313 */ 365 */
314 366
315 PUSHs (Nullsv); 367 PUSHs (Nullsv);
316 /* this loop was inspired by pp_caller */ 368 /* this loop was inspired by pp_caller */
317 for (;;) 369 for (;;)
318 { 370 {
319 do 371 do
320 { 372 {
321 PERL_CONTEXT *cx = &ccstk[cxix--]; 373 PERL_CONTEXT *cx = &ccstk[cxix--];
322 374
323 if (CxTYPE(cx) == CXt_SUB) 375 if (CxTYPE(cx) == CXt_SUB)
324 { 376 {
363 } 415 }
364 416
365 PUTBACK; 417 PUTBACK;
366 } 418 }
367 419
368 c->defav = flags & SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 420 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
369 c->defsv = flags & SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 421 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
370 c->errsv = flags & SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 422 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
423
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);
371 428
372 c->dowarn = PL_dowarn; 429 c->dowarn = PL_dowarn;
430 c->in_eval = PL_in_eval;
373 431
374 c->curstackinfo = PL_curstackinfo; 432 c->curstackinfo = PL_curstackinfo;
375 c->curstack = PL_curstack; 433 c->curstack = PL_curstack;
376 c->mainstack = PL_mainstack; 434 c->mainstack = PL_mainstack;
377 c->stack_sp = PL_stack_sp; 435 c->stack_sp = PL_stack_sp;
394 c->savestack_max = PL_savestack_max; 452 c->savestack_max = PL_savestack_max;
395 c->retstack = PL_retstack; 453 c->retstack = PL_retstack;
396 c->retstack_ix = PL_retstack_ix; 454 c->retstack_ix = PL_retstack_ix;
397 c->retstack_max = PL_retstack_max; 455 c->retstack_max = PL_retstack_max;
398 c->curcop = PL_curcop; 456 c->curcop = PL_curcop;
457 c->top_env = PL_top_env;
458}
459
460/*
461 * allocate various perl stacks. This is an exact copy
462 * of perl.c:init_stacks, except that it uses less memory
463 * on the assumption that coroutines do not usually need
464 * a lot of stackspace.
465 */
466STATIC void
467coro_init_stacks (pTHX)
468{
469 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
470 PL_curstackinfo->si_type = PERLSI_MAIN;
471 PL_curstack = PL_curstackinfo->si_stack;
472 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
473
474 PL_stack_base = AvARRAY(PL_curstack);
475 PL_stack_sp = PL_stack_base;
476 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
477
478 New(50,PL_tmps_stack,96,SV*);
479 PL_tmps_floor = -1;
480 PL_tmps_ix = -1;
481 PL_tmps_max = 96;
482
483 New(54,PL_markstack,16,I32);
484 PL_markstack_ptr = PL_markstack;
485 PL_markstack_max = PL_markstack + 16;
486
487 SET_MARK_OFFSET;
488
489 New(54,PL_scopestack,16,I32);
490 PL_scopestack_ix = 0;
491 PL_scopestack_max = 16;
492
493 New(54,PL_savestack,96,ANY);
494 PL_savestack_ix = 0;
495 PL_savestack_max = 96;
496
497 New(54,PL_retstack,8,OP*);
498 PL_retstack_ix = 0;
499 PL_retstack_max = 8;
399} 500}
400 501
401/* 502/*
402 * destroy the stacks, the callchain etc... 503 * destroy the stacks, the callchain etc...
403 * still there is a memleak of 128 bytes... 504 * still there is a memleak of 128 bytes...
439 Safefree(PL_scopestack); 540 Safefree(PL_scopestack);
440 Safefree(PL_savestack); 541 Safefree(PL_savestack);
441 Safefree(PL_retstack); 542 Safefree(PL_retstack);
442} 543}
443 544
545static void
546allocate_stack (Coro__State ctx, int alloc)
547{
548 coro_stack *stack;
549
550 New (0, stack, 1, coro_stack);
551
552 stack->refcnt = 1;
553 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0;
555 if (alloc)
556 {
557#ifdef HAVE_MMAP
558 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);
560 if (stack->sptr == (void *)-1)
561#endif
562 {
563 /*FIXME*//*D*//* reasonable stack size! */
564 stack->ssize = -4096 * sizeof (long);
565 New (0, stack->sptr, 4096, long);
566 }
567 }
568 else
569 stack->sptr = 0;
570
571 ctx->stack = stack;
572}
573
574static void
575deallocate_stack (Coro__State ctx)
576{
577 coro_stack *stack = ctx->stack;
578
579 ctx->stack = 0;
580
581 if (stack)
582 {
583 if (!--stack->refcnt)
584 {
585#ifdef HAVE_MMAP
586 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize);
588 else
589#else
590 Safefree (stack->sptr);
591#endif
592 Safefree (stack);
593 }
594 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt;
596 }
597}
598
599static void
600setup_coro (void *arg)
601{
602 /*
603 * emulate part of the perl startup here.
604 */
605 dSP;
606 Coro__State ctx = (Coro__State)arg;
607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
608
609 coro_init_stacks (aTHX);
610 /*PL_curcop = 0;*/
611 /*PL_in_eval = PL_in_eval;*/ /* inherit */
612 SvREFCNT_dec (GvAV (PL_defgv));
613 GvAV (PL_defgv) = ctx->args;
614
615 SPAGAIN;
616
617 if (ctx->stack)
618 {
619 ctx->cursp = 0;
620
621 PUSHMARK(SP);
622 PUTBACK;
623 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
624
625 if (SvTRUE (ERRSV))
626 croak (NULL);
627 else
628 croak ("FATAL: CCTXT coroutine returned!");
629 }
630 else
631 {
632 UNOP myop;
633
634 PL_op = (OP *)&myop;
635
636 Zero(&myop, 1, UNOP);
637 myop.op_next = Nullop;
638 myop.op_flags = OPf_WANT_VOID;
639
640 PUSHMARK(SP);
641 XPUSHs (sub_init);
642 /*
643 * the next line is slightly wrong, as PL_op->op_next
644 * is actually being executed so we skip the first op.
645 * that doesn't matter, though, since it is only
646 * pp_nextstate and we never return...
647 * ah yes, and I don't care anyways ;)
648 */
649 PUTBACK;
650 PL_op = pp_entersub();
651 SPAGAIN;
652
653 ENTER; /* necessary e.g. for dounwind */
654 }
655}
656
657static void
658continue_coro (void *arg)
659{
660 /*
661 * this is a _very_ stripped down perl interpreter ;)
662 */
663 Coro__State ctx = (Coro__State)arg;
664
665 /*FIXME*//* must set up top_env here */
666 ctx->cursp = 0;
667 PL_op = PL_op->op_next;
668 CALLRUNOPS(aTHX);
669
670 abort ();
671}
672
444STATIC void 673STATIC void
445transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
446{ 675{
447 dSP; 676 dSP;
677 dSTACKLEVEL;
448 678
449 if (prev != next) 679 if (prev != next)
450 { 680 {
451 /*
452 * this could be done in newprocess which would lead to
453 * extremely elegant and fast (just SAVE/LOAD)
454 * code here, but lazy allocation of stacks has also
455 * some virtues and the overhead of the if() is nil.
456 */
457 if (next->mainstack) 681 if (next->mainstack)
458 { 682 {
459 SAVE (prev, flags); 683 SAVE (prev, flags);
460 LOAD (next); 684 LOAD (next);
685
461 /* mark this state as in-use */ 686 /* mark this state as in-use */
462 next->mainstack = 0; 687 next->mainstack = 0;
463 next->tmps_ix = -2; 688 next->tmps_ix = -2;
689
690 /* stacklevel changed? if yes, grab the stack for us! */
691 if (flags & TRANSFER_SAVE_CCTXT)
692 {
693 if (!prev->stack)
694 allocate_stack (prev, 0);
695 else if (prev->cursp != stacklevel
696 && prev->stack->usecnt > 1)
697 {
698 prev->gencnt = ++prev->stack->gencnt;
699 prev->stack->usecnt = 1;
700 }
701
702 /* has our stack been invalidated? */
703 if (next->stack && next->stack->gencnt != next->gencnt)
704 {
705 deallocate_stack (next);
706 allocate_stack (next, 1);
707 coro_create (&(next->stack->cctx),
708 continue_coro, (void *)next,
709 next->stack->sptr, labs (next->stack->ssize));
710 }
711
712 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
713 }
714
464 } 715 }
465 else if (next->tmps_ix == -2) 716 else if (next->tmps_ix == -2)
466 {
467 croak ("tried to transfer to running coroutine"); 717 croak ("tried to transfer to running coroutine");
468 }
469 else 718 else
470 { 719 {
471 /*
472 * emulate part of the perl startup here.
473 */
474 UNOP myop;
475
476 SAVE (prev, -1); /* first get rid of the old state */ 720 SAVE (prev, -1); /* first get rid of the old state */
477 721
478 init_stacks (); /* from perl.c */ 722 if (flags & TRANSFER_SAVE_CCTXT)
479 SPAGAIN;
480
481 PL_op = (OP *)&myop;
482 /*PL_curcop = 0;*/
483 SvREFCNT_dec (GvAV (PL_defgv));
484 GvAV (PL_defgv) = next->args;
485
486 Zero(&myop, 1, UNOP);
487 myop.op_next = Nullop;
488 myop.op_flags = OPf_WANT_VOID;
489
490 PUSHMARK(SP);
491 XPUSHs ((SV*)get_cv(SUB_INIT, TRUE));
492 /*
493 * the next line is slightly wrong, as PL_op->op_next
494 * is actually being executed so we skip the first op.
495 * that doesn't matter, though, since it is only
496 * pp_nextstate and we never return...
497 * ah yes, and I don't care anyways ;)
498 */ 723 {
499 PUTBACK; 724 if (!prev->stack)
500 PL_op = pp_entersub(aTHX); 725 allocate_stack (prev, 0);
501 SPAGAIN;
502 726
503 ENTER; /* necessary e.g. for dounwind */ 727 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
728 {
729 setup_coro (next);
730
731 prev->stack->refcnt++;
732 prev->stack->usecnt++;
733 next->stack = prev->stack;
734 next->gencnt = prev->gencnt;
735 }
736 else
737 {
738 allocate_stack (next, 1);
739 coro_create (&(next->stack->cctx),
740 setup_coro, (void *)next,
741 next->stack->sptr, labs (next->stack->ssize));
742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
743 }
744 }
745 else
746 setup_coro (next);
504 } 747 }
505 } 748 }
749
750 next->cursp = stacklevel;
751}
752
753static struct coro *
754sv_to_coro (SV *arg, const char *funcname, const char *varname)
755{
756 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
757 {
758 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
759
760 if (!he)
761 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
762
763 arg = HeVAL(he);
764 }
765
766 /* must also be changed inside Coro::Cont::yield */
767 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash)
768 return (struct coro *) SvIV((SV*)SvRV(arg));
769 else
770 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
771}
772
773/** Coro ********************************************************************/
774
775#define PRIO_MAX 3
776#define PRIO_HIGH 1
777#define PRIO_NORMAL 0
778#define PRIO_LOW -1
779#define PRIO_IDLE -3
780#define PRIO_MIN -4
781
782/* for Coro.pm */
783static GV *coro_current, *coro_idle;
784static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
785
786static void
787coro_enq (SV *sv)
788{
789 if (SvROK (sv))
790 {
791 SV *hv = SvRV (sv);
792 if (SvTYPE (hv) == SVt_PVHV)
793 {
794 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
795 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
796
797 prio = prio > PRIO_MAX ? PRIO_MAX
798 : prio < PRIO_MIN ? PRIO_MIN
799 : prio;
800
801 av_push (coro_ready [prio - PRIO_MIN], sv);
802
803 return;
804 }
805 }
806
807 croak ("Coro::ready tried to enqueue something that is not a coroutine");
808}
809
810static SV *
811coro_deq (int min_prio)
812{
813 int prio = PRIO_MAX - PRIO_MIN;
814
815 min_prio -= PRIO_MIN;
816 if (min_prio < 0)
817 min_prio = 0;
818
819 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
820 if (av_len (coro_ready[prio]) >= 0)
821 return av_shift (coro_ready[prio]);
822
823 return 0;
506} 824}
507 825
508MODULE = Coro::State PACKAGE = Coro::State 826MODULE = Coro::State PACKAGE = Coro::State
509 827
510PROTOTYPES: ENABLE 828PROTOTYPES: ENABLE
511 829
512BOOT: 830BOOT:
513{ /* {} necessary for stoopid perl-5.6.x */ 831{ /* {} necessary for stoopid perl-5.6.x */
832 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
833 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
514 HV * stash = gv_stashpvn("Coro::State", 10, TRUE); 834 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
515 835
516 newCONSTSUB (stash, "SAVE_DEFAV", newSViv (SAVE_DEFAV)); 836 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
517 newCONSTSUB (stash, "SAVE_DEFSV", newSViv (SAVE_DEFSV)); 837 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
518 newCONSTSUB (stash, "SAVE_ERRSV", newSViv (SAVE_ERRSV)); 838 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
839 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
519 840
520 if (!padlist_cache) 841 if (!padlist_cache)
521 padlist_cache = newHV (); 842 padlist_cache = newHV ();
843
844 main_mainstack = PL_mainstack;
522} 845}
523 846
524Coro::State 847Coro::State
525_newprocess(args) 848_newprocess(args)
526 SV * args 849 SV * args
531 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 854 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
532 croak ("Coro::State::_newprocess expects an arrayref"); 855 croak ("Coro::State::_newprocess expects an arrayref");
533 856
534 New (0, coro, 1, struct coro); 857 New (0, coro, 1, struct coro);
535 858
859 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
536 coro->mainstack = 0; /* actual work is done inside transfer */ 860 coro->mainstack = 0; /* actual work is done inside transfer */
537 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 861 coro->stack = 0;
538 862
539 RETVAL = coro; 863 RETVAL = coro;
540 OUTPUT: 864 OUTPUT:
541 RETVAL 865 RETVAL
542 866
543void 867void
544transfer(prev, next, flags = SAVE_DEFAV) 868transfer(prev, next, flags)
545 Coro::State_or_hashref prev 869 Coro::State_or_hashref prev
546 Coro::State_or_hashref next 870 Coro::State_or_hashref next
547 int flags 871 int flags
548 PROTOTYPE: @ 872 PROTOTYPE: @
549 CODE: 873 CODE:
550 874 PUTBACK;
551 transfer (aTHX_ prev, next, flags); 875 transfer (aTHX_ prev, next, flags);
876 SPAGAIN;
552 877
553void 878void
554DESTROY(coro) 879DESTROY(coro)
555 Coro::State coro 880 Coro::State coro
556 CODE: 881 CODE:
557 882
558 if (coro->mainstack) 883 if (coro->mainstack && coro->mainstack != main_mainstack)
559 { 884 {
560 struct coro temp; 885 struct coro temp;
561 886
562 SAVE(aTHX_ (&temp), SAVE_ALL); 887 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
563 LOAD(aTHX_ coro); 888 LOAD(aTHX_ coro);
564 889
565 destroy_stacks (); 890 destroy_stacks (aTHX);
566 891
567 LOAD((&temp)); /* this will get rid of defsv etc.. */ 892 LOAD((&temp)); /* this will get rid of defsv etc.. */
893
894 coro->mainstack = 0;
568 } 895 }
896
897 deallocate_stack (coro);
569 898
570 Safefree (coro); 899 Safefree (coro);
571 900
572void 901void
573flush() 902flush()
574 CODE: 903 CODE:
575#ifdef MAY_FLUSH 904#ifdef MAY_FLUSH
576 flush_padlist_cache (); 905 flush_padlist_cache ();
577#endif 906#endif
578 907
908void
909_exit(code)
910 int code
911 PROTOTYPE: $
912 CODE:
913#if defined(__GLIBC__) || _POSIX_C_SOURCE
914 _exit (code);
915#else
916 signal (SIGTERM, SIG_DFL);
917 raise (SIGTERM);
918 exit (code);
919#endif
920
579MODULE = Coro::State PACKAGE = Coro::Cont 921MODULE = Coro::State PACKAGE = Coro::Cont
580 922
581# this is dirty and should be in it's own .xs 923# this is slightly dirty (should expose a c-level api)
582 924
583void 925void
584result(...) 926yield(...)
585 PROTOTYPE: @ 927 PROTOTYPE: @
586 CODE: 928 CODE:
587 static SV *returnstk; 929 static SV *returnstk;
588 SV *sv; 930 SV *sv;
589 AV *defav = GvAV (PL_defgv); 931 AV *defav = GvAV (PL_defgv);
590 struct coro *prev, *next; 932 struct coro *prev, *next;
591 933
592 if (!returnstk) 934 if (!returnstk)
593 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 935 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE));
594 936
595 /* set up @_ */ 937 /* set up @_ -- ugly */
596 av_clear (defav); 938 av_clear (defav);
597 av_fill (defav, items - 1); 939 av_fill (defav, items - 1);
598 while (items--) 940 while (items--)
599 av_store (defav, items, SvREFCNT_inc (ST(items))); 941 av_store (defav, items, SvREFCNT_inc (ST(items)));
600 942
601 mg_get (returnstk); /* isn't documentation wrong for mg_get? */ 943 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
602 sv = av_pop ((AV *)SvRV (returnstk)); 944 sv = av_pop ((AV *)SvRV (returnstk));
603 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 945 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
604 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 946 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
605 SvREFCNT_dec (sv); 947 SvREFCNT_dec (sv);
948
606 transfer(prev, next, 0); 949 transfer(aTHX_ prev, next, 0);
607 950
951MODULE = Coro::State PACKAGE = Coro
952
953# this is slightly dirty (should expose a c-level api)
954
955BOOT:
956{
957 int i;
958 HV *stash = gv_stashpv ("Coro", TRUE);
959
960 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
961 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
962 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
963 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
964 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
965 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
966
967 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
968 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
969
970 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
971 coro_ready[i] = newAV ();
972}
973
974void
975ready(self)
976 SV * self
977 CODE:
978 coro_enq (SvREFCNT_inc (self));
979
980void
981schedule(...)
982 ALIAS:
983 cede = 1
984 CODE:
985 SV *prev, *next;
986
987 prev = GvSV (coro_current);
988
989 if (ix)
990 coro_enq (SvREFCNT_inc (prev));
991
992 next = coro_deq (PRIO_MIN);
993
994 if (!next)
995 next = SvREFCNT_inc (GvSV (coro_idle));
996
997 GvSV (coro_current) = SvREFCNT_inc (next);
998 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"),
999 sv_to_coro (next, "Coro::schedule", "next coroutine"),
1000 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
1001 SvREFCNT_dec (next);
1002 SvREFCNT_dec (prev);
1003

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines