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.13 by root, Mon Jul 23 22:09:39 2001 UTC vs.
Revision 1.22 by root, Sat Aug 11 23:10:56 2001 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "libcoro/coro.c" 5#include "libcoro/coro.c"
6 6
7#include <signal.h>
8
7#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
8# include <unistd.h> 10# include <unistd.h>
9# include <sys/mman.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
10#endif 19#endif
11 20
12#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize */
13 22
14#define SUB_INIT "Coro::State::initialize" 23/* perl-related */
15
16#define TRANSFER_SAVE_DEFAV 0x00000001 24#define TRANSFER_SAVE_DEFAV 0x00000001
17#define TRANSFER_SAVE_DEFSV 0x00000002 25#define TRANSFER_SAVE_DEFSV 0x00000002
18#define TRANSFER_SAVE_ERRSV 0x00000004 26#define TRANSFER_SAVE_ERRSV 0x00000004
27/* c-related */
19#define TRANSFER_SAVE_CCTXT 0x00000008 28#define TRANSFER_SAVE_CCTXT 0x00000008
29#ifdef CORO_LAZY_STACK
30# define TRANSFER_LAZY_STACK 0x00000010
31#else
32# define TRANSFER_LAZY_STACK 0x00000000
33#endif
20 34
21#define TRANSFER_SAVE_ALL -1 35#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
36 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
37
38#define SUB_INIT "Coro::State::initialize"
39#define UCORO_STATE "_coro_state"
40
41/* The next macro should delcare a variable stacklevel that contains and approximation
42 * to the current C stack pointer. It's property is that it changes with each call
43 * and should be unique. */
44#define dSTACKLEVEL void *stacklevel = &stacklevel
45
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;
22 59
23struct coro { 60struct coro {
24 /* the optional C context */ 61 /* the optional C context */
25 coro_context cctx; 62 coro_stack *stack;
26 void *sptr; 63 void *cursp;
27 long ssize; 64 int gencnt;
28 65
29 /* optionally saved, might be zero */ 66 /* optionally saved, might be zero */
30 AV *defav; 67 AV *defav;
31 SV *defsv; 68 SV *defsv;
32 SV *errsv; 69 SV *errsv;
33 70
34 /* saved global state not related to stacks */ 71 /* saved global state not related to stacks */
35 U8 dowarn; 72 U8 dowarn;
73 I32 in_eval;
36 74
37 /* the stacks and related info (callchain etc..) */ 75 /* the stacks and related info (callchain etc..) */
38 PERL_SI *curstackinfo; 76 PERL_SI *curstackinfo;
39 AV *curstack; 77 AV *curstack;
40 AV *mainstack; 78 AV *mainstack;
58 I32 savestack_max; 96 I32 savestack_max;
59 OP **retstack; 97 OP **retstack;
60 I32 retstack_ix; 98 I32 retstack_ix;
61 I32 retstack_max; 99 I32 retstack_max;
62 COP *curcop; 100 COP *curcop;
63 JMPENV start_env;
64 JMPENV *top_env; 101 JMPENV *top_env;
65 102
66 /* data associated with this coroutine (initial args) */ 103 /* data associated with this coroutine (initial args) */
67 AV *args; 104 AV *args;
68}; 105};
70typedef struct coro *Coro__State; 107typedef struct coro *Coro__State;
71typedef struct coro *Coro__State_or_hashref; 108typedef struct coro *Coro__State_or_hashref;
72 109
73static AV *main_mainstack; /* used to differentiate between $main and others */ 110static AV *main_mainstack; /* used to differentiate between $main and others */
74static HV *coro_state_stash; 111static HV *coro_state_stash;
112static SV *ucoro_state_sv;
113static U32 ucoro_state_hash;
75static HV *padlist_cache; 114static HV *padlist_cache;
76 115
77/* mostly copied from op.c:cv_clone2 */ 116/* mostly copied from op.c:cv_clone2 */
78STATIC AV * 117STATIC AV *
79clone_padlist (AV *protopadlist) 118clone_padlist (AV *protopadlist)
251 290
252static void 291static void
253load_state(pTHX_ Coro__State c) 292load_state(pTHX_ Coro__State c)
254{ 293{
255 PL_dowarn = c->dowarn; 294 PL_dowarn = c->dowarn;
295 PL_in_eval = c->in_eval;
256 296
257 PL_curstackinfo = c->curstackinfo; 297 PL_curstackinfo = c->curstackinfo;
258 PL_curstack = c->curstack; 298 PL_curstack = c->curstack;
259 PL_mainstack = c->mainstack; 299 PL_mainstack = c->mainstack;
260 PL_stack_sp = c->stack_sp; 300 PL_stack_sp = c->stack_sp;
277 PL_savestack_max = c->savestack_max; 317 PL_savestack_max = c->savestack_max;
278 PL_retstack = c->retstack; 318 PL_retstack = c->retstack;
279 PL_retstack_ix = c->retstack_ix; 319 PL_retstack_ix = c->retstack_ix;
280 PL_retstack_max = c->retstack_max; 320 PL_retstack_max = c->retstack_max;
281 PL_curcop = c->curcop; 321 PL_curcop = c->curcop;
282 PL_start_env = c->start_env;
283 PL_top_env = c->top_env; 322 PL_top_env = c->top_env;
284 323
285 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
286 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
287 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
386 /* but if it's missing the defav contents magically get replaced sometimes */ 425 /* but if it's missing the defav contents magically get replaced sometimes */
387 if (c->defav) 426 if (c->defav)
388 av_reify (c->defav); 427 av_reify (c->defav);
389 428
390 c->dowarn = PL_dowarn; 429 c->dowarn = PL_dowarn;
430 c->in_eval = PL_in_eval;
391 431
392 c->curstackinfo = PL_curstackinfo; 432 c->curstackinfo = PL_curstackinfo;
393 c->curstack = PL_curstack; 433 c->curstack = PL_curstack;
394 c->mainstack = PL_mainstack; 434 c->mainstack = PL_mainstack;
395 c->stack_sp = PL_stack_sp; 435 c->stack_sp = PL_stack_sp;
412 c->savestack_max = PL_savestack_max; 452 c->savestack_max = PL_savestack_max;
413 c->retstack = PL_retstack; 453 c->retstack = PL_retstack;
414 c->retstack_ix = PL_retstack_ix; 454 c->retstack_ix = PL_retstack_ix;
415 c->retstack_max = PL_retstack_max; 455 c->retstack_max = PL_retstack_max;
416 c->curcop = PL_curcop; 456 c->curcop = PL_curcop;
417 c->start_env = PL_start_env;
418 c->top_env = PL_top_env; 457 c->top_env = PL_top_env;
419} 458}
420 459
421/* 460/*
422 * allocate various perl stacks. This is an exact copy 461 * allocate various perl stacks. This is an exact copy
434 473
435 PL_stack_base = AvARRAY(PL_curstack); 474 PL_stack_base = AvARRAY(PL_curstack);
436 PL_stack_sp = PL_stack_base; 475 PL_stack_sp = PL_stack_base;
437 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 476 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
438 477
439 New(50,PL_tmps_stack,64,SV*); 478 New(50,PL_tmps_stack,96,SV*);
440 PL_tmps_floor = -1; 479 PL_tmps_floor = -1;
441 PL_tmps_ix = -1; 480 PL_tmps_ix = -1;
442 PL_tmps_max = 64; 481 PL_tmps_max = 96;
443 482
444 New(54,PL_markstack,12,I32); 483 New(54,PL_markstack,16,I32);
445 PL_markstack_ptr = PL_markstack; 484 PL_markstack_ptr = PL_markstack;
446 PL_markstack_max = PL_markstack + 12; 485 PL_markstack_max = PL_markstack + 16;
447 486
448 SET_MARK_OFFSET; 487 SET_MARK_OFFSET;
449 488
450 New(54,PL_scopestack,12,I32); 489 New(54,PL_scopestack,16,I32);
451 PL_scopestack_ix = 0; 490 PL_scopestack_ix = 0;
452 PL_scopestack_max = 12; 491 PL_scopestack_max = 16;
453 492
454 New(54,PL_savestack,64,ANY); 493 New(54,PL_savestack,96,ANY);
455 PL_savestack_ix = 0; 494 PL_savestack_ix = 0;
456 PL_savestack_max = 64; 495 PL_savestack_max = 96;
457 496
458 New(54,PL_retstack,8,OP*); 497 New(54,PL_retstack,8,OP*);
459 PL_retstack_ix = 0; 498 PL_retstack_ix = 0;
460 PL_retstack_max = 8; 499 PL_retstack_max = 8;
461} 500}
502 Safefree(PL_savestack); 541 Safefree(PL_savestack);
503 Safefree(PL_retstack); 542 Safefree(PL_retstack);
504} 543}
505 544
506static void 545static void
507allocate_stack (Coro__State ctx) 546allocate_stack (Coro__State ctx, int alloc)
508{ 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 {
509#ifdef HAVE_MMAP 557#ifdef HAVE_MMAP
510 ctx->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ 558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
511 ctx->sptr = mmap (0, ctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
512 if (ctx->sptr == (void *)-1) 560 if (stack->sptr == (void *)-1)
513#endif 561#endif
514 { 562 {
515 /*FIXME*//*D*//* reasonable stack size! */ 563 /*FIXME*//*D*//* reasonable stack size! */
516 ctx->ssize = 4096 * sizeof (long); 564 stack->ssize = -4096 * sizeof (long);
517 New (0, ctx->sptr, 4096, long); 565 New (0, stack->sptr, 4096, long);
566 }
518 } 567 }
568 else
569 stack->sptr = 0;
570
571 ctx->stack = stack;
519} 572}
520 573
521static void 574static void
522deallocate_stack (Coro__State ctx) 575deallocate_stack (Coro__State ctx)
523{ 576{
577 coro_stack *stack = ctx->stack;
578
579 ctx->stack = 0;
580
581 if (stack)
582 {
583 if (!--stack->refcnt)
584 {
524#ifdef HAVE_MMAP 585#ifdef HAVE_MMAP
525 munmap (ctx->sptr, ctx->ssize); 586 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize);
588 else
526#else 589#else
527 Safefree (ctx->sptr); 590 Safefree (stack->sptr);
528#endif 591#endif
592 Safefree (stack);
593 }
594 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt;
596 }
529} 597}
530 598
531/* might go away together with optional SAVE_CCTXT */
532static void 599static void
533setup_coro (void *arg) 600setup_coro (void *arg)
534{ 601{
535 /* 602 /*
536 * emulate part of the perl startup here. 603 * emulate part of the perl startup here.
538 dSP; 605 dSP;
539 Coro__State ctx = (Coro__State)arg; 606 Coro__State ctx = (Coro__State)arg;
540 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
541 608
542 coro_init_stacks (aTHX); 609 coro_init_stacks (aTHX);
543 JMPENV_BOOTSTRAP;
544 SPAGAIN;
545
546 /*PL_curcop = 0;*/ 610 /*PL_curcop = 0;*/
611 /*PL_in_eval = PL_in_eval;*/ /* inherit */
547 SvREFCNT_dec (GvAV (PL_defgv)); 612 SvREFCNT_dec (GvAV (PL_defgv));
548 GvAV (PL_defgv) = ctx->args; 613 GvAV (PL_defgv) = ctx->args;
549 614
615 SPAGAIN;
616
550 if (ctx->sptr) 617 if (ctx->stack)
551 { 618 {
619 ctx->cursp = 0;
620
552 PUSHMARK(SP); 621 PUSHMARK(SP);
553 PUTBACK; 622 PUTBACK;
554 (void) call_sv (sub_init, G_VOID|G_NOARGS); 623 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
624
625 if (SvTRUE (ERRSV))
626 croak (NULL);
627 else
555 croak ("FATAL: CCTXT coroutine returned!"); 628 croak ("FATAL: CCTXT coroutine returned!");
556 } 629 }
557 else 630 else
558 { 631 {
559 UNOP myop; 632 UNOP myop;
560 633
579 652
580 ENTER; /* necessary e.g. for dounwind */ 653 ENTER; /* necessary e.g. for dounwind */
581 } 654 }
582} 655}
583 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
584STATIC void 673STATIC void
585transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
586{ 675{
587 dSP; 676 dSP;
677 dSTACKLEVEL;
588 678
589 if (prev != next) 679 if (prev != next)
590 { 680 {
591 /*
592 * this could be done in newprocess which would lead to
593 * extremely elegant and fast (basically just SAVE/LOAD)
594 * code here, but lazy allocation of stacks has also
595 * some virtues and the overhead of the if() is nil.
596 */
597 if (next->mainstack) 681 if (next->mainstack)
598 { 682 {
599 SAVE (prev, flags); 683 SAVE (prev, flags);
600 LOAD (next); 684 LOAD (next);
601 685
602 /* mark this state as in-use */ 686 /* mark this state as in-use */
603 next->mainstack = 0; 687 next->mainstack = 0;
604 next->tmps_ix = -2; 688 next->tmps_ix = -2;
605 689
690 /* stacklevel changed? if yes, grab the stack for us! */
606 if (flags & TRANSFER_SAVE_CCTXT) 691 if (flags & TRANSFER_SAVE_CCTXT)
607 { 692 {
608 if (!next->ssize)
609 croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize);
610
611 if (!prev->ssize) 693 if (!prev->stack)
612 prev->ssize = 1; /* mark cctx as valid ;) */ 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 }
613 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
614 coro_transfer (&(prev->cctx), &(next->cctx)); 712 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
615 } 713 }
616 714
617 } 715 }
618 else if (next->tmps_ix == -2) 716 else if (next->tmps_ix == -2)
619 croak ("tried to transfer to running coroutine"); 717 croak ("tried to transfer to running coroutine");
621 { 719 {
622 SAVE (prev, -1); /* first get rid of the old state */ 720 SAVE (prev, -1); /* first get rid of the old state */
623 721
624 if (flags & TRANSFER_SAVE_CCTXT) 722 if (flags & TRANSFER_SAVE_CCTXT)
625 { 723 {
626 if (!next->ssize) 724 if (!prev->stack)
725 allocate_stack (prev, 0);
726
727 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
627 { 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 {
628 allocate_stack (next); 738 allocate_stack (next, 1);
629 coro_create (&(next->cctx), 739 coro_create (&(next->stack->cctx),
630 setup_coro, (void *)next, 740 setup_coro, (void *)next,
631 next->sptr, next->ssize); 741 next->stack->sptr, labs (next->stack->ssize));
742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
632 } 743 }
633
634 if (!prev->ssize)
635 prev->ssize = 1; /* mark cctx as valid ;) */
636
637 coro_transfer (&(prev->cctx), &(next->cctx));
638 } 744 }
639 else 745 else
640 setup_coro (next); 746 setup_coro (next);
641 } 747 }
642 } 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;
643} 824}
644 825
645MODULE = Coro::State PACKAGE = Coro::State 826MODULE = Coro::State PACKAGE = Coro::State
646 827
647PROTOTYPES: ENABLE 828PROTOTYPES: ENABLE
648 829
649BOOT: 830BOOT:
650{ /* {} 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);
651 coro_state_stash = gv_stashpvn ("Coro::State", 10, TRUE); 834 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
652 835
653 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 836 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
654 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 837 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
655 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 838 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
656 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 839 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
673 856
674 New (0, coro, 1, struct coro); 857 New (0, coro, 1, struct coro);
675 858
676 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 859 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
677 coro->mainstack = 0; /* actual work is done inside transfer */ 860 coro->mainstack = 0; /* actual work is done inside transfer */
678 coro->sptr = 0; 861 coro->stack = 0;
679 coro->ssize = 0;
680 862
681 RETVAL = coro; 863 RETVAL = coro;
682 OUTPUT: 864 OUTPUT:
683 RETVAL 865 RETVAL
684 866
685void 867void
686transfer(prev, next, flags = TRANSFER_SAVE_ALL) 868transfer(prev, next, flags)
687 Coro::State_or_hashref prev 869 Coro::State_or_hashref prev
688 Coro::State_or_hashref next 870 Coro::State_or_hashref next
689 int flags 871 int flags
690 PROTOTYPE: @ 872 PROTOTYPE: @
691 CODE: 873 CODE:
874 PUTBACK;
692 transfer (aTHX_ prev, next, flags); 875 transfer (aTHX_ prev, next, flags);
876 SPAGAIN;
693 877
694void 878void
695DESTROY(coro) 879DESTROY(coro)
696 Coro::State coro 880 Coro::State coro
697 CODE: 881 CODE:
708 LOAD((&temp)); /* this will get rid of defsv etc.. */ 892 LOAD((&temp)); /* this will get rid of defsv etc.. */
709 893
710 coro->mainstack = 0; 894 coro->mainstack = 0;
711 } 895 }
712 896
713 if (coro->sptr)
714 {
715 deallocate_stack (coro); 897 deallocate_stack (coro);
716 coro->sptr = 0;
717 }
718 898
719 Safefree (coro); 899 Safefree (coro);
720 900
721void 901void
722flush() 902flush()
723 CODE: 903 CODE:
724#ifdef MAY_FLUSH 904#ifdef MAY_FLUSH
725 flush_padlist_cache (); 905 flush_padlist_cache ();
726#endif 906#endif
727 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
728MODULE = Coro::State PACKAGE = Coro::Cont 921MODULE = Coro::State PACKAGE = Coro::Cont
729 922
730# this is slightly dirty 923# this is slightly dirty (should expose a c-level api)
731 924
732void 925void
733yield(...) 926yield(...)
734 PROTOTYPE: @ 927 PROTOTYPE: @
735 CODE: 928 CODE:
753 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)));
754 SvREFCNT_dec (sv); 947 SvREFCNT_dec (sv);
755 948
756 transfer(aTHX_ prev, next, 0); 949 transfer(aTHX_ prev, next, 0);
757 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