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.123 by root, Mon Dec 4 21:56:00 2006 UTC vs.
Revision 1.136 by root, Fri Jan 12 01:05:55 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#endif
39
40/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8
42
43#define PERL_VERSION_ATLEAST(a,b,c) \
44 (PERL_REVISION > (a) \
45 || (PERL_REVISION == (a) \
46 && (PERL_VERSION > (b) \
47 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
48
49#if !PERL_VERSION_ATLEAST (5,6,0)
50# ifndef PL_ppaddr
51# define PL_ppaddr ppaddr
52# endif
53# ifndef call_sv
54# define call_sv perl_call_sv
55# endif
56# ifndef get_sv
57# define get_sv perl_get_sv
58# endif
59# ifndef get_cv
60# define get_cv perl_get_cv
61# endif
62# ifndef IS_PADGV
63# define IS_PADGV(v) 0
64# endif
65# ifndef IS_PADCONST
66# define IS_PADCONST(v) 0
67# endif
68#endif
69
70/* 5.8.7 */
71#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v)
73#endif
74
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD
77#endif
78
79#ifndef STACKGUARD
80# define STACKGUARD 0
81#endif
82
83/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0
86#endif
87
77/* The next macro should declare a variable stacklevel that contains and approximation 88/* 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 89 * to the current C stack pointer. Its property is that it changes with each call
79 * and should be unique. */ 90 * and should be unique. */
80#define dSTACKLEVEL int stacklevel 91#define dSTACKLEVEL int stacklevel
81#define STACKLEVEL ((void *)&stacklevel) 92#define STACKLEVEL ((void *)&stacklevel)
82 93
83#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
84 95
85#if __GNUC__ >= 3 96#if __GNUC__ >= 3
86# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
87#else 99#else
88# define attribute(x) 100# define attribute(x)
101# define BARRIER
89#endif 102#endif
90 103
91#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
92 105
93#include "CoroAPI.h" 106#include "CoroAPI.h"
99#else 112#else
100# define LOCK (void)0 113# define LOCK (void)0
101# define UNLOCK (void)0 114# define UNLOCK (void)0
102#endif 115#endif
103 116
117/* helper storage struct for Coro::AIO */
104struct io_state 118struct io_state
105{ 119{
106 int errorno; 120 int errorno;
107 I32 laststype; 121 I32 laststype;
108 int laststatval; 122 int laststatval;
124 /* the stack */ 138 /* the stack */
125 void *sptr; 139 void *sptr;
126 long ssize; /* positive == mmap, otherwise malloc */ 140 long ssize; /* positive == mmap, otherwise malloc */
127 141
128 /* cpu state */ 142 /* cpu state */
129 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 143 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
144 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
130 JMPENV *top_env; 145 JMPENV *top_env;
131 coro_context cctx; 146 coro_context cctx;
132 147
133 int inuse; 148 int inuse;
134 149
136 int valgrind_id; 151 int valgrind_id;
137#endif 152#endif
138} coro_cctx; 153} coro_cctx;
139 154
140enum { 155enum {
141 CF_RUNNING = 0x0001, /* coroutine is running */ 156 CF_RUNNING = 0x0001, /* coroutine is running */
142 CF_READY = 0x0002, /* coroutine is ready */ 157 CF_READY = 0x0002, /* coroutine is ready */
143 CF_NEW = 0x0004, /* ahs never been switched to */ 158 CF_NEW = 0x0004, /* has never been switched to */
159 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
144}; 160};
145 161
146/* this is a structure representing a perl-level coroutine */ 162/* this is a structure representing a perl-level coroutine */
147struct coro { 163struct coro {
148 /* the c coroutine allocated to this perl coroutine, if any */ 164 /* the c coroutine allocated to this perl coroutine, if any */
253 269
254 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 270 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
255 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 271 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
256 else 272 else
257 { 273 {
258#if 0 274#if PREFER_PERL_FUNCTIONS
259 /* this is probably cleaner, but also slower? */ 275 /* this is probably cleaner, but also slower? */
260 CV *cp = Perl_cv_clone (cv); 276 CV *cp = Perl_cv_clone (cv);
261 CvPADLIST (cv) = CvPADLIST (cp); 277 CvPADLIST (cv) = CvPADLIST (cp);
262 CvPADLIST (cp) = 0; 278 CvPADLIST (cp) = 0;
263 SvREFCNT_dec (cp); 279 SvREFCNT_dec (cp);
344 /* 360 /*
345 * the worst thing you can imagine happens first - we have to save 361 * the worst thing you can imagine happens first - we have to save
346 * (and reinitialize) all cv's in the whole callchain :( 362 * (and reinitialize) all cv's in the whole callchain :(
347 */ 363 */
348 364
365 EXTEND (SP, 3 + 1);
349 PUSHs (Nullsv); 366 PUSHs (Nullsv);
350 /* this loop was inspired by pp_caller */ 367 /* this loop was inspired by pp_caller */
351 for (;;) 368 for (;;)
352 { 369 {
353 while (cxix >= 0) 370 while (cxix >= 0)
359 CV *cv = cx->blk_sub.cv; 376 CV *cv = cx->blk_sub.cv;
360 377
361 if (CvDEPTH (cv)) 378 if (CvDEPTH (cv))
362 { 379 {
363 EXTEND (SP, 3); 380 EXTEND (SP, 3);
364
365 PUSHs ((SV *)CvPADLIST (cv)); 381 PUSHs ((SV *)CvPADLIST (cv));
366 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 382 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
367 PUSHs ((SV *)cv); 383 PUSHs ((SV *)cv);
368 384
369 CvDEPTH (cv) = 0; 385 CvDEPTH (cv) = 0;
370 get_padlist (cv); 386 get_padlist (cv);
371 } 387 }
372 } 388 }
373#ifdef CXt_FORMAT
374 else if (CxTYPE (cx) == CXt_FORMAT)
375 {
376 /* I never used formats, so how should I know how these are implemented? */
377 /* my bold guess is as a simple, plain sub... */
378 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
379 }
380#endif
381 } 389 }
382 390
383 if (top_si->si_type == PERLSI_MAIN) 391 if (top_si->si_type == PERLSI_MAIN)
384 break; 392 break;
385 393
405 * allocate various perl stacks. This is an exact copy 413 * allocate various perl stacks. This is an exact copy
406 * of perl.c:init_stacks, except that it uses less memory 414 * of perl.c:init_stacks, except that it uses less memory
407 * on the (sometimes correct) assumption that coroutines do 415 * on the (sometimes correct) assumption that coroutines do
408 * not usually need a lot of stackspace. 416 * not usually need a lot of stackspace.
409 */ 417 */
418#if PREFER_PERL_FUNCTIONS
419# define coro_init_stacks init_stacks
420#else
410static void 421static void
411coro_init_stacks () 422coro_init_stacks ()
412{ 423{
413 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 424 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
414 PL_curstackinfo->si_type = PERLSI_MAIN; 425 PL_curstackinfo->si_type = PERLSI_MAIN;
444 New(54,PL_retstack,16,OP*); 455 New(54,PL_retstack,16,OP*);
445 PL_retstack_ix = 0; 456 PL_retstack_ix = 0;
446 PL_retstack_max = 16; 457 PL_retstack_max = 16;
447#endif 458#endif
448} 459}
460#endif
449 461
450/* 462/*
451 * destroy the stacks, the callchain etc... 463 * destroy the stacks, the callchain etc...
452 */ 464 */
453static void 465static void
454coro_destroy_stacks () 466coro_destroy_stacks ()
455{ 467{
456 if (!IN_DESTRUCT) 468 if (!IN_DESTRUCT)
457 { 469 {
458 /* is this ugly, I ask? */ 470 /* restore all saved variables and stuff */
459 LEAVE_SCOPE (0); 471 LEAVE_SCOPE (0);
472 assert (PL_tmps_floor == -1);
460 473
461 /* sure it is, but more important: is it correct?? :/ */ 474 /* free all temporaries */
462 FREETMPS; 475 FREETMPS;
476 assert (PL_tmps_ix == -1);
463 477
478 /* unwind all extra stacks */
464 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 479 POPSTACK_TO (PL_mainstack);
480
481 /* unwind main stack */
482 dounwind (-1);
465 } 483 }
466 484
467 while (PL_curstackinfo->si_next) 485 while (PL_curstackinfo->si_next)
468 PL_curstackinfo = PL_curstackinfo->si_next; 486 PL_curstackinfo = PL_curstackinfo->si_next;
469 487
470 while (PL_curstackinfo) 488 while (PL_curstackinfo)
471 { 489 {
472 PERL_SI *p = PL_curstackinfo->si_prev; 490 PERL_SI *p = PL_curstackinfo->si_prev;
473 491
474 { /*D*//*remove*/
475 dSP;
476 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
477 PUTBACK; /* possibly superfluous */
478 }
479
480 if (!IN_DESTRUCT) 492 if (!IN_DESTRUCT)
481 {
482 dounwind (-1);/*D*//*remove*/
483 SvREFCNT_dec (PL_curstackinfo->si_stack); 493 SvREFCNT_dec (PL_curstackinfo->si_stack);
484 }
485 494
486 Safefree (PL_curstackinfo->si_cxstack); 495 Safefree (PL_curstackinfo->si_cxstack);
487 Safefree (PL_curstackinfo); 496 Safefree (PL_curstackinfo);
488 PL_curstackinfo = p; 497 PL_curstackinfo = p;
489 } 498 }
593{ 602{
594 coro_cctx *cctx; 603 coro_cctx *cctx;
595 604
596 ++cctx_count; 605 ++cctx_count;
597 606
598 New (0, cctx, 1, coro_cctx); 607 Newz (0, cctx, 1, coro_cctx);
599 608
600#if HAVE_MMAP 609#if HAVE_MMAP
601 610
602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 611 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
603 /* mmap supposedly does allocate-on-write for us */ 612 /* mmap supposedly does allocate-on-write for us */
704{ 713{
705 dSTACKLEVEL; 714 dSTACKLEVEL;
706 715
707 /* sometimes transfer is only called to set idle_sp */ 716 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 717 if (!next)
718 {
709 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 719 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
720 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
721 }
710 else if (prev != next) 722 else if (prev != next)
711 { 723 {
712 coro_cctx *prev__cctx; 724 coro_cctx *prev__cctx;
713 725
714 if (prev->flags & CF_NEW) 726 if (prev->flags & CF_NEW)
724 if (!prev->flags & CF_RUNNING) 736 if (!prev->flags & CF_RUNNING)
725 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 737 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
726 738
727 if (next->flags & CF_RUNNING) 739 if (next->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 740 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
741
742 if (next->flags & CF_DESTROYED)
743 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
729 744
730 prev->flags &= ~CF_RUNNING; 745 prev->flags &= ~CF_RUNNING;
731 next->flags |= CF_RUNNING; 746 next->flags |= CF_RUNNING;
732 747
733 LOCK; 748 LOCK;
739 /* first get rid of the old state */ 754 /* first get rid of the old state */
740 save_perl (prev); 755 save_perl (prev);
741 /* setup coroutine call */ 756 /* setup coroutine call */
742 setup_coro (next); 757 setup_coro (next);
743 /* need a new stack */ 758 /* need a new stack */
744 assert (!next->stack); 759 assert (!next->cctx);
745 } 760 }
746 else 761 else
747 { 762 {
748 /* coroutine already started */ 763 /* coroutine already started */
749 save_perl (prev); 764 save_perl (prev);
754 769
755 /* possibly "free" the cctx */ 770 /* possibly "free" the cctx */
756 if (prev__cctx->idle_sp == STACKLEVEL) 771 if (prev__cctx->idle_sp == STACKLEVEL)
757 { 772 {
758 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 773 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
759 assert (PL_top_env == prev__cctx->top_env); 774 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
760 775
761 prev->cctx = 0; 776 prev->cctx = 0;
762 777
763 cctx_put (prev__cctx); 778 cctx_put (prev__cctx);
764 prev__cctx->inuse = 0; 779 prev__cctx->inuse = 0;
777 PL_top_env = next->cctx->top_env; 792 PL_top_env = next->cctx->top_env;
778 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 793 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
779 } 794 }
780 795
781 free_coro_mortal (); 796 free_coro_mortal ();
782
783 UNLOCK; 797 UNLOCK;
784 } 798 }
785} 799}
786 800
787struct transfer_args 801struct transfer_args
789 struct coro *prev, *next; 803 struct coro *prev, *next;
790}; 804};
791 805
792#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 806#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
793 807
794static void 808static int
795coro_state_destroy (struct coro *coro) 809coro_state_destroy (struct coro *coro)
796{ 810{
797 if (coro->refcnt--) 811 if (coro->flags & CF_DESTROYED)
798 return; 812 return 0;
813
814 coro->flags |= CF_DESTROYED;
799 815
800 if (coro->mainstack && coro->mainstack != main_mainstack) 816 if (coro->mainstack && coro->mainstack != main_mainstack)
801 { 817 {
818 assert (!(coro->flags & CF_RUNNING));
819
802 struct coro temp; 820 struct coro temp;
803 Zero (&temp, 1, struct coro); 821 Zero (&temp, 1, struct coro);
804 temp.save = CORO_SAVE_ALL; 822 temp.save = CORO_SAVE_ALL;
805 823
806 if (coro->flags & CF_RUNNING) 824 if (coro->flags & CF_RUNNING)
816 coro->mainstack = 0; 834 coro->mainstack = 0;
817 } 835 }
818 836
819 cctx_destroy (coro->cctx); 837 cctx_destroy (coro->cctx);
820 SvREFCNT_dec (coro->args); 838 SvREFCNT_dec (coro->args);
821 Safefree (coro); 839
840 return 1;
822} 841}
823 842
824static int 843static int
825coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 844coro_state_free (pTHX_ SV *sv, MAGIC *mg)
826{ 845{
827 struct coro *coro = (struct coro *)mg->mg_ptr; 846 struct coro *coro = (struct coro *)mg->mg_ptr;
828 mg->mg_ptr = 0; 847 mg->mg_ptr = 0;
829 848
849 if (--coro->refcnt < 0)
850 {
830 coro_state_destroy (coro); 851 coro_state_destroy (coro);
852 Safefree (coro);
853 }
831 854
832 return 0; 855 return 0;
833} 856}
834 857
835static int 858static int
842 return 0; 865 return 0;
843} 866}
844 867
845static MGVTBL coro_state_vtbl = { 868static MGVTBL coro_state_vtbl = {
846 0, 0, 0, 0, 869 0, 0, 0, 0,
847 coro_state_clear, 870 coro_state_free,
848 0, 871 0,
849#ifdef MGf_DUP 872#ifdef MGf_DUP
850 coro_state_dup, 873 coro_state_dup,
851#else 874#else
852# define MGf_DUP 0 875# define MGf_DUP 0
954 coro = SvSTATE (coro_sv); 977 coro = SvSTATE (coro_sv);
955 978
956 if (coro->flags & CF_READY) 979 if (coro->flags & CF_READY)
957 return 0; 980 return 0;
958 981
959#if 0 /* this is actually harmless */
960 if (coro->flags & CF_RUNNING)
961 croak ("Coro::ready called on currently running coroutine");
962#endif
963
964 coro->flags |= CF_READY; 982 coro->flags |= CF_READY;
965 983
966 LOCK; 984 LOCK;
967 coro_enq (SvREFCNT_inc (coro_sv)); 985 coro_enq (SvREFCNT_inc (coro_sv));
968 UNLOCK; 986 UNLOCK;
971} 989}
972 990
973static int 991static int
974api_is_ready (SV *coro_sv) 992api_is_ready (SV *coro_sv)
975{ 993{
976 return !!SvSTATE (coro_sv)->flags & CF_READY; 994 return !!(SvSTATE (coro_sv)->flags & CF_READY);
977} 995}
978 996
979static void 997static void
980prepare_schedule (struct transfer_args *ta) 998prepare_schedule (struct transfer_args *ta)
981{ 999{
982 SV *prev, *next; 1000 SV *prev_sv, *next_sv;
983 1001
984 for (;;) 1002 for (;;)
985 { 1003 {
986 LOCK; 1004 LOCK;
987 next = coro_deq (PRIO_MIN); 1005 next_sv = coro_deq (PRIO_MIN);
988 UNLOCK; 1006 UNLOCK;
989 1007
1008 /* nothing to schedule: call the idle handler */
990 if (next) 1009 if (!next_sv)
991 break;
992
993 { 1010 {
994 dSP; 1011 dSP;
995 1012
996 ENTER; 1013 ENTER;
997 SAVETMPS; 1014 SAVETMPS;
998 1015
999 PUSHMARK (SP); 1016 PUSHMARK (SP);
1000 PUTBACK; 1017 PUTBACK;
1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1018 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1002 1019
1003 FREETMPS; 1020 FREETMPS;
1004 LEAVE; 1021 LEAVE;
1022 continue;
1005 } 1023 }
1006 }
1007 1024
1008 prev = SvRV (coro_current); 1025 ta->next = SvSTATE (next_sv);
1009 SvRV_set (coro_current, next); 1026
1027 /* cannot transfer to destroyed coros, skip and look for next */
1028 if (ta->next->flags & CF_DESTROYED)
1029 {
1030 SvREFCNT_dec (next_sv);
1031 continue;
1032 }
1033
1034 break;
1035 }
1010 1036
1011 /* free this only after the transfer */ 1037 /* free this only after the transfer */
1038 prev_sv = SvRV (coro_current);
1039 SvRV_set (coro_current, next_sv);
1040 ta->prev = SvSTATE (prev_sv);
1041
1042 assert (ta->next->flags & CF_READY);
1043 ta->next->flags &= ~CF_READY;
1044
1012 LOCK; 1045 LOCK;
1013 free_coro_mortal (); 1046 free_coro_mortal ();
1047 coro_mortal = prev_sv;
1014 UNLOCK; 1048 UNLOCK;
1015 coro_mortal = prev;
1016
1017 assert (!SvROK(prev));//D
1018 assert (!SvROK(next));//D
1019
1020 ta->prev = SvSTATE (prev);
1021 ta->next = SvSTATE (next);
1022
1023 assert (ta->next->flags & CF_READY);
1024 ta->next->flags &= ~CF_READY;
1025} 1049}
1026 1050
1027static void 1051static void
1028prepare_cede (struct transfer_args *ta) 1052prepare_cede (struct transfer_args *ta)
1029{ 1053{
1030 api_ready (coro_current); 1054 api_ready (coro_current);
1031
1032 prepare_schedule (ta); 1055 prepare_schedule (ta);
1056}
1057
1058static int
1059prepare_cede_notself (struct transfer_args *ta)
1060{
1061 if (coro_nready)
1062 {
1063 SV *prev = SvRV (coro_current);
1064 prepare_schedule (ta);
1065 api_ready (prev);
1066 return 1;
1067 }
1068 else
1069 return 0;
1033} 1070}
1034 1071
1035static void 1072static void
1036api_schedule (void) 1073api_schedule (void)
1037{ 1074{
1039 1076
1040 prepare_schedule (&ta); 1077 prepare_schedule (&ta);
1041 TRANSFER (ta); 1078 TRANSFER (ta);
1042} 1079}
1043 1080
1044static void 1081static int
1045api_cede (void) 1082api_cede (void)
1046{ 1083{
1047 struct transfer_args ta; 1084 struct transfer_args ta;
1048 1085
1049 prepare_cede (&ta); 1086 prepare_cede (&ta);
1087
1088 if (ta.prev != ta.next)
1089 {
1050 TRANSFER (ta); 1090 TRANSFER (ta);
1091 return 1;
1092 }
1093 else
1094 return 0;
1095}
1096
1097static int
1098api_cede_notself (void)
1099{
1100 struct transfer_args ta;
1101
1102 if (prepare_cede_notself (&ta))
1103 {
1104 TRANSFER (ta);
1105 return 1;
1106 }
1107 else
1108 return 0;
1051} 1109}
1052 1110
1053MODULE = Coro::State PACKAGE = Coro::State 1111MODULE = Coro::State PACKAGE = Coro::State
1054 1112
1055PROTOTYPES: DISABLE 1113PROTOTYPES: DISABLE
1111_set_stacklevel (...) 1169_set_stacklevel (...)
1112 ALIAS: 1170 ALIAS:
1113 Coro::State::transfer = 1 1171 Coro::State::transfer = 1
1114 Coro::schedule = 2 1172 Coro::schedule = 2
1115 Coro::cede = 3 1173 Coro::cede = 3
1174 Coro::cede_notself = 4
1116 CODE: 1175 CODE:
1117{ 1176{
1118 struct transfer_args ta; 1177 struct transfer_args ta;
1119 1178
1120 switch (ix) 1179 switch (ix)
1136 break; 1195 break;
1137 1196
1138 case 3: 1197 case 3:
1139 prepare_cede (&ta); 1198 prepare_cede (&ta);
1140 break; 1199 break;
1200
1201 case 4:
1202 if (!prepare_cede_notself (&ta))
1203 XSRETURN_EMPTY;
1204
1205 break;
1141 } 1206 }
1142 1207
1208 BARRIER;
1143 TRANSFER (ta); 1209 TRANSFER (ta);
1144}
1145 1210
1146void 1211 if (GIMME_V != G_VOID && ta.next != ta.prev)
1147_clone_state_from (SV *dst, SV *src) 1212 XSRETURN_YES;
1213}
1214
1215bool
1216_destroy (SV *coro_sv)
1148 CODE: 1217 CODE:
1149{ 1218 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1150 struct coro *coro_src = SvSTATE (src); 1219 OUTPUT:
1151 1220 RETVAL
1152 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1153
1154 ++coro_src->refcnt;
1155 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1156}
1157 1221
1158void 1222void
1159_exit (code) 1223_exit (code)
1160 int code 1224 int code
1161 PROTOTYPE: $ 1225 PROTOTYPE: $
1198 coro_ready[i] = newAV (); 1262 coro_ready[i] = newAV ();
1199 1263
1200 { 1264 {
1201 SV *sv = perl_get_sv("Coro::API", 1); 1265 SV *sv = perl_get_sv("Coro::API", 1);
1202 1266
1203 coroapi.schedule = api_schedule; 1267 coroapi.schedule = api_schedule;
1204 coroapi.save = api_save; 1268 coroapi.save = api_save;
1205 coroapi.cede = api_cede; 1269 coroapi.cede = api_cede;
1270 coroapi.cede_notself = api_cede_notself;
1206 coroapi.ready = api_ready; 1271 coroapi.ready = api_ready;
1207 coroapi.is_ready = api_is_ready; 1272 coroapi.is_ready = api_is_ready;
1208 coroapi.nready = &coro_nready; 1273 coroapi.nready = &coro_nready;
1209 coroapi.current = coro_current; 1274 coroapi.current = coro_current;
1210 1275
1211 GCoroAPI = &coroapi; 1276 GCoroAPI = &coroapi;
1212 sv_setiv (sv, (IV)&coroapi); 1277 sv_setiv (sv, (IV)&coroapi);
1213 SvREADONLY_on (sv); 1278 SvREADONLY_on (sv);
1214 } 1279 }
1230 RETVAL = coro->prio; 1295 RETVAL = coro->prio;
1231 1296
1232 if (items > 1) 1297 if (items > 1)
1233 { 1298 {
1234 if (ix) 1299 if (ix)
1235 newprio += coro->prio; 1300 newprio = coro->prio - newprio;
1236 1301
1237 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1302 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1238 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1303 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1239 1304
1240 coro->prio = newprio; 1305 coro->prio = newprio;
1241 } 1306 }
1242} 1307}
1308 OUTPUT:
1309 RETVAL
1243 1310
1244SV * 1311SV *
1245ready (SV *self) 1312ready (SV *self)
1246 PROTOTYPE: $ 1313 PROTOTYPE: $
1247 CODE: 1314 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines