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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines