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.124 by root, Tue Dec 5 12:50:04 2006 UTC vs.
Revision 1.134 by root, Thu Jan 4 23:49:27 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#include <assert.h> 11#include <assert.h>
46
47#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
48# undef STACKGUARD
49#endif
50
51#ifndef STACKGUARD
52# define STACKGUARD 0
53#endif
54 12
55#ifdef HAVE_MMAP 13#ifdef HAVE_MMAP
56# include <unistd.h> 14# include <unistd.h>
57# include <sys/mman.h> 15# include <sys/mman.h>
58# ifndef MAP_ANONYMOUS 16# ifndef MAP_ANONYMOUS
73#else 31#else
74# define PAGESIZE 0 32# define PAGESIZE 0
75# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
76#endif 34#endif
77 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
78/* 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
79 * 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
80 * and should be unique. */ 90 * and should be unique. */
81#define dSTACKLEVEL int stacklevel 91#define dSTACKLEVEL int stacklevel
82#define STACKLEVEL ((void *)&stacklevel) 92#define STACKLEVEL ((void *)&stacklevel)
83 93
84#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
85 95
86#if __GNUC__ >= 3 96#if __GNUC__ >= 3
87# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
88#else 99#else
89# define attribute(x) 100# define attribute(x)
101# define BARRIER
90#endif 102#endif
91 103
92#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
93 105
94#include "CoroAPI.h" 106#include "CoroAPI.h"
125 /* the stack */ 137 /* the stack */
126 void *sptr; 138 void *sptr;
127 long ssize; /* positive == mmap, otherwise malloc */ 139 long ssize; /* positive == mmap, otherwise malloc */
128 140
129 /* cpu state */ 141 /* cpu state */
130 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 */
131 JMPENV *top_env; 144 JMPENV *top_env;
132 coro_context cctx; 145 coro_context cctx;
133 146
134 int inuse; 147 int inuse;
135 148
137 int valgrind_id; 150 int valgrind_id;
138#endif 151#endif
139} coro_cctx; 152} coro_cctx;
140 153
141enum { 154enum {
142 CF_RUNNING = 0x0001, /* coroutine is running */ 155 CF_RUNNING = 0x0001, /* coroutine is running */
143 CF_READY = 0x0002, /* coroutine is ready */ 156 CF_READY = 0x0002, /* coroutine is ready */
144 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 */
145}; 159};
146 160
147/* this is a structure representing a perl-level coroutine */ 161/* this is a structure representing a perl-level coroutine */
148struct coro { 162struct coro {
149 /* the c coroutine allocated to this perl coroutine, if any */ 163 /* the c coroutine allocated to this perl coroutine, if any */
254 268
255 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 269 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
256 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 270 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
257 else 271 else
258 { 272 {
259#if 0 273#if PREFER_PERL_FUNCTIONS
260 /* this is probably cleaner, but also slower? */ 274 /* this is probably cleaner, but also slower? */
261 CV *cp = Perl_cv_clone (cv); 275 CV *cp = Perl_cv_clone (cv);
262 CvPADLIST (cv) = CvPADLIST (cp); 276 CvPADLIST (cv) = CvPADLIST (cp);
263 CvPADLIST (cp) = 0; 277 CvPADLIST (cp) = 0;
264 SvREFCNT_dec (cp); 278 SvREFCNT_dec (cp);
345 /* 359 /*
346 * 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
347 * (and reinitialize) all cv's in the whole callchain :( 361 * (and reinitialize) all cv's in the whole callchain :(
348 */ 362 */
349 363
364 EXTEND (SP, 3 + 1);
350 PUSHs (Nullsv); 365 PUSHs (Nullsv);
351 /* this loop was inspired by pp_caller */ 366 /* this loop was inspired by pp_caller */
352 for (;;) 367 for (;;)
353 { 368 {
354 while (cxix >= 0) 369 while (cxix >= 0)
360 CV *cv = cx->blk_sub.cv; 375 CV *cv = cx->blk_sub.cv;
361 376
362 if (CvDEPTH (cv)) 377 if (CvDEPTH (cv))
363 { 378 {
364 EXTEND (SP, 3); 379 EXTEND (SP, 3);
365
366 PUSHs ((SV *)CvPADLIST (cv)); 380 PUSHs ((SV *)CvPADLIST (cv));
367 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 381 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
368 PUSHs ((SV *)cv); 382 PUSHs ((SV *)cv);
369 383
370 CvDEPTH (cv) = 0; 384 CvDEPTH (cv) = 0;
371 get_padlist (cv); 385 get_padlist (cv);
372 } 386 }
373 } 387 }
374#ifdef CXt_FORMAT
375 else if (CxTYPE (cx) == CXt_FORMAT)
376 {
377 /* I never used formats, so how should I know how these are implemented? */
378 /* my bold guess is as a simple, plain sub... */
379 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
380 }
381#endif
382 } 388 }
383 389
384 if (top_si->si_type == PERLSI_MAIN) 390 if (top_si->si_type == PERLSI_MAIN)
385 break; 391 break;
386 392
406 * allocate various perl stacks. This is an exact copy 412 * allocate various perl stacks. This is an exact copy
407 * of perl.c:init_stacks, except that it uses less memory 413 * of perl.c:init_stacks, except that it uses less memory
408 * on the (sometimes correct) assumption that coroutines do 414 * on the (sometimes correct) assumption that coroutines do
409 * not usually need a lot of stackspace. 415 * not usually need a lot of stackspace.
410 */ 416 */
417#if PREFER_PERL_FUNCTIONS
418# define coro_init_stacks init_stacks
419#else
411static void 420static void
412coro_init_stacks () 421coro_init_stacks ()
413{ 422{
414 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 423 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
415 PL_curstackinfo->si_type = PERLSI_MAIN; 424 PL_curstackinfo->si_type = PERLSI_MAIN;
445 New(54,PL_retstack,16,OP*); 454 New(54,PL_retstack,16,OP*);
446 PL_retstack_ix = 0; 455 PL_retstack_ix = 0;
447 PL_retstack_max = 16; 456 PL_retstack_max = 16;
448#endif 457#endif
449} 458}
459#endif
450 460
451/* 461/*
452 * destroy the stacks, the callchain etc... 462 * destroy the stacks, the callchain etc...
453 */ 463 */
454static void 464static void
455coro_destroy_stacks () 465coro_destroy_stacks ()
456{ 466{
457 if (!IN_DESTRUCT) 467 if (!IN_DESTRUCT)
458 { 468 {
459 /* is this ugly, I ask? */ 469 /* restore all saved variables and stuff */
460 LEAVE_SCOPE (0); 470 LEAVE_SCOPE (0);
471 assert (PL_tmps_floor == -1);
461 472
462 /* sure it is, but more important: is it correct?? :/ */ 473 /* free all temporaries */
463 FREETMPS; 474 FREETMPS;
475 assert (PL_tmps_ix == -1);
464 476
477 /* unwind all extra stacks */
465 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 478 POPSTACK_TO (PL_mainstack);
479
480 /* unwind main stack */
481 dounwind (-1);
466 } 482 }
467 483
468 while (PL_curstackinfo->si_next) 484 while (PL_curstackinfo->si_next)
469 PL_curstackinfo = PL_curstackinfo->si_next; 485 PL_curstackinfo = PL_curstackinfo->si_next;
470 486
471 while (PL_curstackinfo) 487 while (PL_curstackinfo)
472 { 488 {
473 PERL_SI *p = PL_curstackinfo->si_prev; 489 PERL_SI *p = PL_curstackinfo->si_prev;
474 490
475 { /*D*//*remove*/
476 dSP;
477 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
478 PUTBACK; /* possibly superfluous */
479 }
480
481 if (!IN_DESTRUCT) 491 if (!IN_DESTRUCT)
482 {
483 dounwind (-1);/*D*//*remove*/
484 SvREFCNT_dec (PL_curstackinfo->si_stack); 492 SvREFCNT_dec (PL_curstackinfo->si_stack);
485 }
486 493
487 Safefree (PL_curstackinfo->si_cxstack); 494 Safefree (PL_curstackinfo->si_cxstack);
488 Safefree (PL_curstackinfo); 495 Safefree (PL_curstackinfo);
489 PL_curstackinfo = p; 496 PL_curstackinfo = p;
490 } 497 }
707 714
708 /* sometimes transfer is only called to set idle_sp */ 715 /* sometimes transfer is only called to set idle_sp */
709 if (!next) 716 if (!next)
710 { 717 {
711 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 718 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
712 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ 719 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
713 } 720 }
714 else if (prev != next) 721 else if (prev != next)
715 { 722 {
716 coro_cctx *prev__cctx; 723 coro_cctx *prev__cctx;
717 724
728 if (!prev->flags & CF_RUNNING) 735 if (!prev->flags & CF_RUNNING)
729 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");
730 737
731 if (next->flags & CF_RUNNING) 738 if (next->flags & CF_RUNNING)
732 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");
733 743
734 prev->flags &= ~CF_RUNNING; 744 prev->flags &= ~CF_RUNNING;
735 next->flags |= CF_RUNNING; 745 next->flags |= CF_RUNNING;
736 746
737 LOCK; 747 LOCK;
758 768
759 /* possibly "free" the cctx */ 769 /* possibly "free" the cctx */
760 if (prev__cctx->idle_sp == STACKLEVEL) 770 if (prev__cctx->idle_sp == STACKLEVEL)
761 { 771 {
762 /* 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 */
763 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));
764 774
765 prev->cctx = 0; 775 prev->cctx = 0;
766 776
767 cctx_put (prev__cctx); 777 cctx_put (prev__cctx);
768 prev__cctx->inuse = 0; 778 prev__cctx->inuse = 0;
781 PL_top_env = next->cctx->top_env; 791 PL_top_env = next->cctx->top_env;
782 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
783 } 793 }
784 794
785 free_coro_mortal (); 795 free_coro_mortal ();
786
787 UNLOCK; 796 UNLOCK;
788 } 797 }
789} 798}
790 799
791struct transfer_args 800struct transfer_args
793 struct coro *prev, *next; 802 struct coro *prev, *next;
794}; 803};
795 804
796#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 805#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
797 806
798static void 807static int
799coro_state_destroy (struct coro *coro) 808coro_state_destroy (struct coro *coro)
800{ 809{
801 if (coro->refcnt--) 810 if (coro->flags & CF_DESTROYED)
802 return; 811 return 0;
812
813 coro->flags |= CF_DESTROYED;
803 814
804 if (coro->mainstack && coro->mainstack != main_mainstack) 815 if (coro->mainstack && coro->mainstack != main_mainstack)
805 { 816 {
817 assert (!(coro->flags & CF_RUNNING));
818
806 struct coro temp; 819 struct coro temp;
807 Zero (&temp, 1, struct coro); 820 Zero (&temp, 1, struct coro);
808 temp.save = CORO_SAVE_ALL; 821 temp.save = CORO_SAVE_ALL;
809 822
810 if (coro->flags & CF_RUNNING) 823 if (coro->flags & CF_RUNNING)
820 coro->mainstack = 0; 833 coro->mainstack = 0;
821 } 834 }
822 835
823 cctx_destroy (coro->cctx); 836 cctx_destroy (coro->cctx);
824 SvREFCNT_dec (coro->args); 837 SvREFCNT_dec (coro->args);
825 Safefree (coro); 838
839 return 1;
826} 840}
827 841
828static int 842static int
829coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 843coro_state_free (pTHX_ SV *sv, MAGIC *mg)
830{ 844{
831 struct coro *coro = (struct coro *)mg->mg_ptr; 845 struct coro *coro = (struct coro *)mg->mg_ptr;
832 mg->mg_ptr = 0; 846 mg->mg_ptr = 0;
833 847
848 if (--coro->refcnt < 0)
849 {
834 coro_state_destroy (coro); 850 coro_state_destroy (coro);
851 Safefree (coro);
852 }
835 853
836 return 0; 854 return 0;
837} 855}
838 856
839static int 857static int
846 return 0; 864 return 0;
847} 865}
848 866
849static MGVTBL coro_state_vtbl = { 867static MGVTBL coro_state_vtbl = {
850 0, 0, 0, 0, 868 0, 0, 0, 0,
851 coro_state_clear, 869 coro_state_free,
852 0, 870 0,
853#ifdef MGf_DUP 871#ifdef MGf_DUP
854 coro_state_dup, 872 coro_state_dup,
855#else 873#else
856# define MGf_DUP 0 874# define MGf_DUP 0
958 coro = SvSTATE (coro_sv); 976 coro = SvSTATE (coro_sv);
959 977
960 if (coro->flags & CF_READY) 978 if (coro->flags & CF_READY)
961 return 0; 979 return 0;
962 980
963#if 0 /* this is actually harmless */
964 if (coro->flags & CF_RUNNING)
965 croak ("Coro::ready called on currently running coroutine");
966#endif
967
968 coro->flags |= CF_READY; 981 coro->flags |= CF_READY;
969 982
970 LOCK; 983 LOCK;
971 coro_enq (SvREFCNT_inc (coro_sv)); 984 coro_enq (SvREFCNT_inc (coro_sv));
972 UNLOCK; 985 UNLOCK;
975} 988}
976 989
977static int 990static int
978api_is_ready (SV *coro_sv) 991api_is_ready (SV *coro_sv)
979{ 992{
980 return !!SvSTATE (coro_sv)->flags & CF_READY; 993 return !!(SvSTATE (coro_sv)->flags & CF_READY);
981} 994}
982 995
983static void 996static void
984prepare_schedule (struct transfer_args *ta) 997prepare_schedule (struct transfer_args *ta)
985{ 998{
986 SV *prev, *next; 999 SV *prev_sv, *next_sv;
987 1000
988 for (;;) 1001 for (;;)
989 { 1002 {
990 LOCK; 1003 LOCK;
991 next = coro_deq (PRIO_MIN); 1004 next_sv = coro_deq (PRIO_MIN);
992 UNLOCK; 1005 UNLOCK;
993 1006
1007 /* nothing to schedule: call the idle handler */
994 if (next) 1008 if (!next_sv)
995 break;
996
997 { 1009 {
998 dSP; 1010 dSP;
999 1011
1000 ENTER; 1012 ENTER;
1001 SAVETMPS; 1013 SAVETMPS;
1002 1014
1003 PUSHMARK (SP); 1015 PUSHMARK (SP);
1004 PUTBACK; 1016 PUTBACK;
1005 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1017 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1006 1018
1007 FREETMPS; 1019 FREETMPS;
1008 LEAVE; 1020 LEAVE;
1021 continue;
1009 } 1022 }
1010 }
1011 1023
1012 prev = SvRV (coro_current); 1024 ta->next = SvSTATE (next_sv);
1013 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 }
1014 1035
1015 /* 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
1016 LOCK; 1044 LOCK;
1017 free_coro_mortal (); 1045 free_coro_mortal ();
1046 coro_mortal = prev_sv;
1018 UNLOCK; 1047 UNLOCK;
1019 coro_mortal = prev;
1020
1021 assert (!SvROK(prev));//D
1022 assert (!SvROK(next));//D
1023
1024 ta->prev = SvSTATE (prev);
1025 ta->next = SvSTATE (next);
1026
1027 assert (ta->next->flags & CF_READY);
1028 ta->next->flags &= ~CF_READY;
1029} 1048}
1030 1049
1031static void 1050static void
1032prepare_cede (struct transfer_args *ta) 1051prepare_cede (struct transfer_args *ta)
1033{ 1052{
1034 api_ready (coro_current); 1053 api_ready (coro_current);
1035
1036 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;
1037} 1069}
1038 1070
1039static void 1071static void
1040api_schedule (void) 1072api_schedule (void)
1041{ 1073{
1043 1075
1044 prepare_schedule (&ta); 1076 prepare_schedule (&ta);
1045 TRANSFER (ta); 1077 TRANSFER (ta);
1046} 1078}
1047 1079
1048static void 1080static int
1049api_cede (void) 1081api_cede (void)
1050{ 1082{
1051 struct transfer_args ta; 1083 struct transfer_args ta;
1052 1084
1053 prepare_cede (&ta); 1085 prepare_cede (&ta);
1086
1087 if (ta.prev != ta.next)
1088 {
1054 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;
1055} 1108}
1056 1109
1057MODULE = Coro::State PACKAGE = Coro::State 1110MODULE = Coro::State PACKAGE = Coro::State
1058 1111
1059PROTOTYPES: DISABLE 1112PROTOTYPES: DISABLE
1115_set_stacklevel (...) 1168_set_stacklevel (...)
1116 ALIAS: 1169 ALIAS:
1117 Coro::State::transfer = 1 1170 Coro::State::transfer = 1
1118 Coro::schedule = 2 1171 Coro::schedule = 2
1119 Coro::cede = 3 1172 Coro::cede = 3
1173 Coro::cede_notself = 4
1120 CODE: 1174 CODE:
1121{ 1175{
1122 struct transfer_args ta; 1176 struct transfer_args ta;
1123 1177
1124 switch (ix) 1178 switch (ix)
1140 break; 1194 break;
1141 1195
1142 case 3: 1196 case 3:
1143 prepare_cede (&ta); 1197 prepare_cede (&ta);
1144 break; 1198 break;
1199
1200 case 4:
1201 if (!prepare_cede_notself (&ta))
1202 XSRETURN_EMPTY;
1203
1204 break;
1145 } 1205 }
1146 1206
1207 BARRIER;
1147 TRANSFER (ta); 1208 TRANSFER (ta);
1148} 1209}
1149 1210
1150void 1211bool
1151_clone_state_from (SV *dst, SV *src) 1212_destroy (SV *coro_sv)
1152 CODE: 1213 CODE:
1153{ 1214 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1154 struct coro *coro_src = SvSTATE (src); 1215 OUTPUT:
1155 1216 RETVAL
1156 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1157
1158 ++coro_src->refcnt;
1159 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1160}
1161 1217
1162void 1218void
1163_exit (code) 1219_exit (code)
1164 int code 1220 int code
1165 PROTOTYPE: $ 1221 PROTOTYPE: $
1202 coro_ready[i] = newAV (); 1258 coro_ready[i] = newAV ();
1203 1259
1204 { 1260 {
1205 SV *sv = perl_get_sv("Coro::API", 1); 1261 SV *sv = perl_get_sv("Coro::API", 1);
1206 1262
1207 coroapi.schedule = api_schedule; 1263 coroapi.schedule = api_schedule;
1208 coroapi.save = api_save; 1264 coroapi.save = api_save;
1209 coroapi.cede = api_cede; 1265 coroapi.cede = api_cede;
1266 coroapi.cede_notself = api_cede_notself;
1210 coroapi.ready = api_ready; 1267 coroapi.ready = api_ready;
1211 coroapi.is_ready = api_is_ready; 1268 coroapi.is_ready = api_is_ready;
1212 coroapi.nready = &coro_nready; 1269 coroapi.nready = &coro_nready;
1213 coroapi.current = coro_current; 1270 coroapi.current = coro_current;
1214 1271
1215 GCoroAPI = &coroapi; 1272 GCoroAPI = &coroapi;
1216 sv_setiv (sv, (IV)&coroapi); 1273 sv_setiv (sv, (IV)&coroapi);
1217 SvREADONLY_on (sv); 1274 SvREADONLY_on (sv);
1218 } 1275 }
1234 RETVAL = coro->prio; 1291 RETVAL = coro->prio;
1235 1292
1236 if (items > 1) 1293 if (items > 1)
1237 { 1294 {
1238 if (ix) 1295 if (ix)
1239 newprio += coro->prio; 1296 newprio = coro->prio - newprio;
1240 1297
1241 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1298 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1242 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1299 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1243 1300
1244 coro->prio = newprio; 1301 coro->prio = newprio;
1245 } 1302 }
1246} 1303}
1304 OUTPUT:
1305 RETVAL
1247 1306
1248SV * 1307SV *
1249ready (SV *self) 1308ready (SV *self)
1250 PROTOTYPE: $ 1309 PROTOTYPE: $
1251 CODE: 1310 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines