ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.122 by root, Mon Dec 4 13:47:56 2006 UTC vs.
Revision 1.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 */
157 /* optionally saved, might be zero */ 173 /* optionally saved, might be zero */
158 AV *defav; /* @_ */ 174 AV *defav; /* @_ */
159 SV *defsv; /* $_ */ 175 SV *defsv; /* $_ */
160 SV *errsv; /* $@ */ 176 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */ 177 SV *irssv; /* $/ */
178 SV *irssv_sv; /* real $/ cache */
162 179
163#define VAR(name,type) type name; 180#define VAR(name,type) type name;
164# include "state.h" 181# include "state.h"
165#undef VAR 182#undef VAR
166 183
252 269
253 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 270 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
254 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 271 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
255 else 272 else
256 { 273 {
257#if 0 274#if PREFER_PERL_FUNCTIONS
258 /* this is probably cleaner, but also slower? */ 275 /* this is probably cleaner, but also slower? */
259 CV *cp = Perl_cv_clone (cv); 276 CV *cp = Perl_cv_clone (cv);
260 CvPADLIST (cv) = CvPADLIST (cp); 277 CvPADLIST (cv) = CvPADLIST (cp);
261 CvPADLIST (cp) = 0; 278 CvPADLIST (cp) = 0;
262 SvREFCNT_dec (cp); 279 SvREFCNT_dec (cp);
301#undef VAR 318#undef VAR
302 319
303 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 320 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
304 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 321 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
305 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 322 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
306 if (c->irssv) REPLACE_SV (PL_rs , c->irssv); 323 if (c->irssv)
324 {
325 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
326 SvREFCNT_dec (c->irssv);
327 else
328 {
329 REPLACE_SV (PL_rs, c->irssv);
330 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
331 sv_setsv (c->irssv_sv, PL_rs);
332 }
333 }
307 334
308 { 335 {
309 dSP; 336 dSP;
310 CV *cv; 337 CV *cv;
311 338
333 /* 360 /*
334 * 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
335 * (and reinitialize) all cv's in the whole callchain :( 362 * (and reinitialize) all cv's in the whole callchain :(
336 */ 363 */
337 364
365 EXTEND (SP, 3 + 1);
338 PUSHs (Nullsv); 366 PUSHs (Nullsv);
339 /* this loop was inspired by pp_caller */ 367 /* this loop was inspired by pp_caller */
340 for (;;) 368 for (;;)
341 { 369 {
342 while (cxix >= 0) 370 while (cxix >= 0)
348 CV *cv = cx->blk_sub.cv; 376 CV *cv = cx->blk_sub.cv;
349 377
350 if (CvDEPTH (cv)) 378 if (CvDEPTH (cv))
351 { 379 {
352 EXTEND (SP, 3); 380 EXTEND (SP, 3);
353
354 PUSHs ((SV *)CvPADLIST (cv)); 381 PUSHs ((SV *)CvPADLIST (cv));
355 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 382 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
356 PUSHs ((SV *)cv); 383 PUSHs ((SV *)cv);
357 384
358 CvDEPTH (cv) = 0; 385 CvDEPTH (cv) = 0;
359 get_padlist (cv); 386 get_padlist (cv);
360 } 387 }
361 } 388 }
362#ifdef CXt_FORMAT
363 else if (CxTYPE (cx) == CXt_FORMAT)
364 {
365 /* I never used formats, so how should I know how these are implemented? */
366 /* my bold guess is as a simple, plain sub... */
367 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
368 }
369#endif
370 } 389 }
371 390
372 if (top_si->si_type == PERLSI_MAIN) 391 if (top_si->si_type == PERLSI_MAIN)
373 break; 392 break;
374 393
394 * allocate various perl stacks. This is an exact copy 413 * allocate various perl stacks. This is an exact copy
395 * of perl.c:init_stacks, except that it uses less memory 414 * of perl.c:init_stacks, except that it uses less memory
396 * on the (sometimes correct) assumption that coroutines do 415 * on the (sometimes correct) assumption that coroutines do
397 * not usually need a lot of stackspace. 416 * not usually need a lot of stackspace.
398 */ 417 */
418#if PREFER_PERL_FUNCTIONS
419# define coro_init_stacks init_stacks
420#else
399static void 421static void
400coro_init_stacks () 422coro_init_stacks ()
401{ 423{
402 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 424 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
403 PL_curstackinfo->si_type = PERLSI_MAIN; 425 PL_curstackinfo->si_type = PERLSI_MAIN;
433 New(54,PL_retstack,16,OP*); 455 New(54,PL_retstack,16,OP*);
434 PL_retstack_ix = 0; 456 PL_retstack_ix = 0;
435 PL_retstack_max = 16; 457 PL_retstack_max = 16;
436#endif 458#endif
437} 459}
460#endif
438 461
439/* 462/*
440 * destroy the stacks, the callchain etc... 463 * destroy the stacks, the callchain etc...
441 */ 464 */
442static void 465static void
443coro_destroy_stacks () 466coro_destroy_stacks ()
444{ 467{
445 if (!IN_DESTRUCT) 468 if (!IN_DESTRUCT)
446 { 469 {
447 /* is this ugly, I ask? */ 470 /* restore all saved variables and stuff */
448 LEAVE_SCOPE (0); 471 LEAVE_SCOPE (0);
472 assert (PL_tmps_floor == -1);
449 473
450 /* sure it is, but more important: is it correct?? :/ */ 474 /* free all temporaries */
451 FREETMPS; 475 FREETMPS;
476 assert (PL_tmps_ix == -1);
452 477
478 /* unwind all extra stacks */
453 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 479 POPSTACK_TO (PL_mainstack);
480
481 /* unwind main stack */
482 dounwind (-1);
454 } 483 }
455 484
456 while (PL_curstackinfo->si_next) 485 while (PL_curstackinfo->si_next)
457 PL_curstackinfo = PL_curstackinfo->si_next; 486 PL_curstackinfo = PL_curstackinfo->si_next;
458 487
459 while (PL_curstackinfo) 488 while (PL_curstackinfo)
460 { 489 {
461 PERL_SI *p = PL_curstackinfo->si_prev; 490 PERL_SI *p = PL_curstackinfo->si_prev;
462 491
463 { /*D*//*remove*/
464 dSP;
465 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
466 PUTBACK; /* possibly superfluous */
467 }
468
469 if (!IN_DESTRUCT) 492 if (!IN_DESTRUCT)
470 {
471 dounwind (-1);/*D*//*remove*/
472 SvREFCNT_dec (PL_curstackinfo->si_stack); 493 SvREFCNT_dec (PL_curstackinfo->si_stack);
473 }
474 494
475 Safefree (PL_curstackinfo->si_cxstack); 495 Safefree (PL_curstackinfo->si_cxstack);
476 Safefree (PL_curstackinfo); 496 Safefree (PL_curstackinfo);
477 PL_curstackinfo = p; 497 PL_curstackinfo = p;
478 } 498 }
582{ 602{
583 coro_cctx *cctx; 603 coro_cctx *cctx;
584 604
585 ++cctx_count; 605 ++cctx_count;
586 606
587 New (0, cctx, 1, coro_cctx); 607 Newz (0, cctx, 1, coro_cctx);
588 608
589#if HAVE_MMAP 609#if HAVE_MMAP
590 610
591 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 611 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
592 /* mmap supposedly does allocate-on-write for us */ 612 /* mmap supposedly does allocate-on-write for us */
693{ 713{
694 dSTACKLEVEL; 714 dSTACKLEVEL;
695 715
696 /* sometimes transfer is only called to set idle_sp */ 716 /* sometimes transfer is only called to set idle_sp */
697 if (!next) 717 if (!next)
718 {
698 ((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 }
699 else if (prev != next) 722 else if (prev != next)
700 { 723 {
701 coro_cctx *prev__cctx; 724 coro_cctx *prev__cctx;
702 725
703 if (prev->flags & CF_NEW) 726 if (prev->flags & CF_NEW)
713 if (!prev->flags & CF_RUNNING) 736 if (!prev->flags & CF_RUNNING)
714 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");
715 738
716 if (next->flags & CF_RUNNING) 739 if (next->flags & CF_RUNNING)
717 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");
718 744
719 prev->flags &= ~CF_RUNNING; 745 prev->flags &= ~CF_RUNNING;
720 next->flags |= CF_RUNNING; 746 next->flags |= CF_RUNNING;
721 747
722 LOCK; 748 LOCK;
728 /* first get rid of the old state */ 754 /* first get rid of the old state */
729 save_perl (prev); 755 save_perl (prev);
730 /* setup coroutine call */ 756 /* setup coroutine call */
731 setup_coro (next); 757 setup_coro (next);
732 /* need a new stack */ 758 /* need a new stack */
733 assert (!next->stack); 759 assert (!next->cctx);
734 } 760 }
735 else 761 else
736 { 762 {
737 /* coroutine already started */ 763 /* coroutine already started */
738 save_perl (prev); 764 save_perl (prev);
743 769
744 /* possibly "free" the cctx */ 770 /* possibly "free" the cctx */
745 if (prev__cctx->idle_sp == STACKLEVEL) 771 if (prev__cctx->idle_sp == STACKLEVEL)
746 { 772 {
747 /* 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 */
748 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));
749 775
750 prev->cctx = 0; 776 prev->cctx = 0;
751 777
752 cctx_put (prev__cctx); 778 cctx_put (prev__cctx);
753 prev__cctx->inuse = 0; 779 prev__cctx->inuse = 0;
766 PL_top_env = next->cctx->top_env; 792 PL_top_env = next->cctx->top_env;
767 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 793 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
768 } 794 }
769 795
770 free_coro_mortal (); 796 free_coro_mortal ();
771
772 UNLOCK; 797 UNLOCK;
773 } 798 }
774} 799}
775 800
776struct transfer_args 801struct transfer_args
778 struct coro *prev, *next; 803 struct coro *prev, *next;
779}; 804};
780 805
781#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 806#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
782 807
783static void 808static int
784coro_state_destroy (struct coro *coro) 809coro_state_destroy (struct coro *coro)
785{ 810{
786 if (coro->refcnt--) 811 if (coro->flags & CF_DESTROYED)
787 return; 812 return 0;
813
814 coro->flags |= CF_DESTROYED;
788 815
789 if (coro->mainstack && coro->mainstack != main_mainstack) 816 if (coro->mainstack && coro->mainstack != main_mainstack)
790 { 817 {
818 assert (!(coro->flags & CF_RUNNING));
819
791 struct coro temp; 820 struct coro temp;
792 Zero (&temp, 1, struct coro); 821 Zero (&temp, 1, struct coro);
793 temp.save = CORO_SAVE_ALL; 822 temp.save = CORO_SAVE_ALL;
794 823
795 if (coro->flags & CF_RUNNING) 824 if (coro->flags & CF_RUNNING)
805 coro->mainstack = 0; 834 coro->mainstack = 0;
806 } 835 }
807 836
808 cctx_destroy (coro->cctx); 837 cctx_destroy (coro->cctx);
809 SvREFCNT_dec (coro->args); 838 SvREFCNT_dec (coro->args);
810 Safefree (coro); 839
840 return 1;
811} 841}
812 842
813static int 843static int
814coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 844coro_state_free (pTHX_ SV *sv, MAGIC *mg)
815{ 845{
816 struct coro *coro = (struct coro *)mg->mg_ptr; 846 struct coro *coro = (struct coro *)mg->mg_ptr;
817 mg->mg_ptr = 0; 847 mg->mg_ptr = 0;
818 848
849 if (--coro->refcnt < 0)
850 {
819 coro_state_destroy (coro); 851 coro_state_destroy (coro);
852 Safefree (coro);
853 }
820 854
821 return 0; 855 return 0;
822} 856}
823 857
824static int 858static int
831 return 0; 865 return 0;
832} 866}
833 867
834static MGVTBL coro_state_vtbl = { 868static MGVTBL coro_state_vtbl = {
835 0, 0, 0, 0, 869 0, 0, 0, 0,
836 coro_state_clear, 870 coro_state_free,
837 0, 871 0,
838#ifdef MGf_DUP 872#ifdef MGf_DUP
839 coro_state_dup, 873 coro_state_dup,
840#else 874#else
841# define MGf_DUP 0 875# define MGf_DUP 0
943 coro = SvSTATE (coro_sv); 977 coro = SvSTATE (coro_sv);
944 978
945 if (coro->flags & CF_READY) 979 if (coro->flags & CF_READY)
946 return 0; 980 return 0;
947 981
948#if 0 /* this is actually harmless */
949 if (coro->flags & CF_RUNNING)
950 croak ("Coro::ready called on currently running coroutine");
951#endif
952
953 coro->flags |= CF_READY; 982 coro->flags |= CF_READY;
954 983
955 LOCK; 984 LOCK;
956 coro_enq (SvREFCNT_inc (coro_sv)); 985 coro_enq (SvREFCNT_inc (coro_sv));
957 UNLOCK; 986 UNLOCK;
960} 989}
961 990
962static int 991static int
963api_is_ready (SV *coro_sv) 992api_is_ready (SV *coro_sv)
964{ 993{
965 return !!SvSTATE (coro_sv)->flags & CF_READY; 994 return !!(SvSTATE (coro_sv)->flags & CF_READY);
966} 995}
967 996
968static void 997static void
969prepare_schedule (struct transfer_args *ta) 998prepare_schedule (struct transfer_args *ta)
970{ 999{
971 SV *prev, *next; 1000 SV *prev_sv, *next_sv;
972 1001
973 for (;;) 1002 for (;;)
974 { 1003 {
975 LOCK; 1004 LOCK;
976 next = coro_deq (PRIO_MIN); 1005 next_sv = coro_deq (PRIO_MIN);
977 UNLOCK; 1006 UNLOCK;
978 1007
1008 /* nothing to schedule: call the idle handler */
979 if (next) 1009 if (!next_sv)
980 break;
981
982 { 1010 {
983 dSP; 1011 dSP;
984 1012
985 ENTER; 1013 ENTER;
986 SAVETMPS; 1014 SAVETMPS;
987 1015
988 PUSHMARK (SP); 1016 PUSHMARK (SP);
989 PUTBACK; 1017 PUTBACK;
990 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1018 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
991 1019
992 FREETMPS; 1020 FREETMPS;
993 LEAVE; 1021 LEAVE;
1022 continue;
994 } 1023 }
995 }
996 1024
997 prev = SvRV (coro_current); 1025 ta->next = SvSTATE (next_sv);
998 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 }
999 1036
1000 /* 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
1001 LOCK; 1045 LOCK;
1002 free_coro_mortal (); 1046 free_coro_mortal ();
1047 coro_mortal = prev_sv;
1003 UNLOCK; 1048 UNLOCK;
1004 coro_mortal = prev;
1005
1006 assert (!SvROK(prev));//D
1007 assert (!SvROK(next));//D
1008
1009 ta->prev = SvSTATE (prev);
1010 ta->next = SvSTATE (next);
1011
1012 assert (ta->next->flags & CF_READY);
1013 ta->next->flags &= ~CF_READY;
1014} 1049}
1015 1050
1016static void 1051static void
1017prepare_cede (struct transfer_args *ta) 1052prepare_cede (struct transfer_args *ta)
1018{ 1053{
1019 api_ready (coro_current); 1054 api_ready (coro_current);
1020
1021 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;
1022} 1070}
1023 1071
1024static void 1072static void
1025api_schedule (void) 1073api_schedule (void)
1026{ 1074{
1028 1076
1029 prepare_schedule (&ta); 1077 prepare_schedule (&ta);
1030 TRANSFER (ta); 1078 TRANSFER (ta);
1031} 1079}
1032 1080
1033static void 1081static int
1034api_cede (void) 1082api_cede (void)
1035{ 1083{
1036 struct transfer_args ta; 1084 struct transfer_args ta;
1037 1085
1038 prepare_cede (&ta); 1086 prepare_cede (&ta);
1087
1088 if (ta.prev != ta.next)
1089 {
1039 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;
1040} 1109}
1041 1110
1042MODULE = Coro::State PACKAGE = Coro::State 1111MODULE = Coro::State PACKAGE = Coro::State
1043 1112
1044PROTOTYPES: DISABLE 1113PROTOTYPES: DISABLE
1100_set_stacklevel (...) 1169_set_stacklevel (...)
1101 ALIAS: 1170 ALIAS:
1102 Coro::State::transfer = 1 1171 Coro::State::transfer = 1
1103 Coro::schedule = 2 1172 Coro::schedule = 2
1104 Coro::cede = 3 1173 Coro::cede = 3
1174 Coro::cede_notself = 4
1105 CODE: 1175 CODE:
1106{ 1176{
1107 struct transfer_args ta; 1177 struct transfer_args ta;
1108 1178
1109 switch (ix) 1179 switch (ix)
1125 break; 1195 break;
1126 1196
1127 case 3: 1197 case 3:
1128 prepare_cede (&ta); 1198 prepare_cede (&ta);
1129 break; 1199 break;
1200
1201 case 4:
1202 if (!prepare_cede_notself (&ta))
1203 XSRETURN_EMPTY;
1204
1205 break;
1130 } 1206 }
1131 1207
1208 BARRIER;
1132 TRANSFER (ta); 1209 TRANSFER (ta);
1133}
1134 1210
1135void 1211 if (GIMME_V != G_VOID && ta.next != ta.prev)
1136_clone_state_from (SV *dst, SV *src) 1212 XSRETURN_YES;
1213}
1214
1215bool
1216_destroy (SV *coro_sv)
1137 CODE: 1217 CODE:
1138{ 1218 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1139 struct coro *coro_src = SvSTATE (src); 1219 OUTPUT:
1140 1220 RETVAL
1141 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1142
1143 ++coro_src->refcnt;
1144 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1145}
1146 1221
1147void 1222void
1148_exit (code) 1223_exit (code)
1149 int code 1224 int code
1150 PROTOTYPE: $ 1225 PROTOTYPE: $
1187 coro_ready[i] = newAV (); 1262 coro_ready[i] = newAV ();
1188 1263
1189 { 1264 {
1190 SV *sv = perl_get_sv("Coro::API", 1); 1265 SV *sv = perl_get_sv("Coro::API", 1);
1191 1266
1192 coroapi.schedule = api_schedule; 1267 coroapi.schedule = api_schedule;
1193 coroapi.save = api_save; 1268 coroapi.save = api_save;
1194 coroapi.cede = api_cede; 1269 coroapi.cede = api_cede;
1270 coroapi.cede_notself = api_cede_notself;
1195 coroapi.ready = api_ready; 1271 coroapi.ready = api_ready;
1196 coroapi.is_ready = api_is_ready; 1272 coroapi.is_ready = api_is_ready;
1197 coroapi.nready = &coro_nready; 1273 coroapi.nready = &coro_nready;
1198 coroapi.current = coro_current; 1274 coroapi.current = coro_current;
1199 1275
1200 GCoroAPI = &coroapi; 1276 GCoroAPI = &coroapi;
1201 sv_setiv (sv, (IV)&coroapi); 1277 sv_setiv (sv, (IV)&coroapi);
1202 SvREADONLY_on (sv); 1278 SvREADONLY_on (sv);
1203 } 1279 }
1219 RETVAL = coro->prio; 1295 RETVAL = coro->prio;
1220 1296
1221 if (items > 1) 1297 if (items > 1)
1222 { 1298 {
1223 if (ix) 1299 if (ix)
1224 newprio += coro->prio; 1300 newprio = coro->prio - newprio;
1225 1301
1226 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1302 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1227 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1303 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1228 1304
1229 coro->prio = newprio; 1305 coro->prio = newprio;
1230 } 1306 }
1231} 1307}
1308 OUTPUT:
1309 RETVAL
1232 1310
1233SV * 1311SV *
1234ready (SV *self) 1312ready (SV *self)
1235 PROTOTYPE: $ 1313 PROTOTYPE: $
1236 CODE: 1314 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines