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.240 by root, Sun Jun 29 00:28:17 2008 UTC vs.
Revision 1.263 by root, Wed Nov 12 04:49:06 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
45# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
46#endif 47#endif
47 48
48#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
49# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
50# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
51#else
52# define REGISTER_STACK(cctx,start,end)
53#endif 51#endif
54 52
55/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
56#define MAX_IDLE_CCTX 8 54static int cctx_max_idle = 4;
57 55
58#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
59 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
60 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
61 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
80# ifndef IS_PADCONST 78# ifndef IS_PADCONST
81# define IS_PADCONST(v) 0 79# define IS_PADCONST(v) 0
82# endif 80# endif
83#endif 81#endif
84 82
83/* 5.11 */
84#ifndef CxHASARGS
85# define CxHASARGS(cx) (cx)->blk_sub.hasargs
86#endif
87
88/* 5.10.0 */
89#ifndef SvREFCNT_inc_NN
90# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
91#endif
92
85/* 5.8.8 */ 93/* 5.8.8 */
86#ifndef GV_NOTQUAL 94#ifndef GV_NOTQUAL
87# define GV_NOTQUAL 0 95# define GV_NOTQUAL 0
88#endif 96#endif
89#ifndef newSV 97#ifndef newSV
90# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
91#endif
92
93/* 5.11 */
94#ifndef CxHASARGS
95# define CxHASARGS(cx) (cx)->blk_sub.hasargs
96#endif 99#endif
97 100
98/* 5.8.7 */ 101/* 5.8.7 */
99#ifndef SvRV_set 102#ifndef SvRV_set
100# define SvRV_set(s,v) SvRV(s) = (v) 103# define SvRV_set(s,v) SvRV(s) = (v)
122 125
123#if __GNUC__ >= 3 126#if __GNUC__ >= 3
124# define attribute(x) __attribute__(x) 127# define attribute(x) __attribute__(x)
125# define BARRIER __asm__ __volatile__ ("" : : : "memory") 128# define BARRIER __asm__ __volatile__ ("" : : : "memory")
126# define expect(expr,value) __builtin_expect ((expr),(value)) 129# define expect(expr,value) __builtin_expect ((expr),(value))
130# define INLINE static inline
127#else 131#else
128# define attribute(x) 132# define attribute(x)
129# define BARRIER 133# define BARRIER
130# define expect(expr,value) (expr) 134# define expect(expr,value) (expr)
135# define INLINE static
131#endif 136#endif
132 137
133#define expect_false(expr) expect ((expr) != 0, 0) 138#define expect_false(expr) expect ((expr) != 0, 0)
134#define expect_true(expr) expect ((expr) != 0, 1) 139#define expect_true(expr) expect ((expr) != 0, 1)
135 140
136#define NOINLINE attribute ((noinline)) 141#define NOINLINE attribute ((noinline))
137 142
138#include "CoroAPI.h" 143#include "CoroAPI.h"
139 144
140#ifdef USE_ITHREADS 145#ifdef USE_ITHREADS
146
141static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
142# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
143# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
150# if CORO_PTHREAD
151static void *coro_thx;
152# endif
153
144#else 154#else
155
145# define LOCK (void)0 156# define LOCK (void)0
146# define UNLOCK (void)0 157# define UNLOCK (void)0
158
147#endif 159#endif
160
161# undef LOCK
162# define LOCK (void)0
163# undef UNLOCK
164# define UNLOCK (void)0
148 165
149/* helper storage struct for Coro::AIO */ 166/* helper storage struct for Coro::AIO */
150struct io_state 167struct io_state
151{ 168{
169 AV *res;
152 int errorno; 170 int errorno;
153 I32 laststype; 171 I32 laststype; /* U16 in 5.10.0 */
154 int laststatval; 172 int laststatval;
155 Stat_t statcache; 173 Stat_t statcache;
156}; 174};
157 175
176static double (*nvtime)(); /* so why doesn't it take void? */
177
178static U32 cctx_gen;
158static size_t coro_stacksize = CORO_STACKSIZE; 179static size_t cctx_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 180static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 181static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 182static JMPENV *main_top_env;
162static HV *coro_state_stash, *coro_stash; 183static HV *coro_state_stash, *coro_stash;
163static volatile SV *coro_mortal; /* will be freed after next transfer */ 184static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
185static volatile struct coro *transfer_next;
186
187struct transfer_args
188{
189 struct coro *prev, *next;
190};
164 191
165static GV *irsgv; /* $/ */ 192static GV *irsgv; /* $/ */
166static GV *stdoutgv; /* *STDOUT */ 193static GV *stdoutgv; /* *STDOUT */
167static SV *rv_diehook; 194static SV *rv_diehook;
168static SV *rv_warnhook; 195static SV *rv_warnhook;
200 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 227 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
201 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 228 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
202 JMPENV *top_env; 229 JMPENV *top_env;
203 coro_context cctx; 230 coro_context cctx;
204 231
232 U32 gen;
205#if CORO_USE_VALGRIND 233#if CORO_USE_VALGRIND
206 int valgrind_id; 234 int valgrind_id;
207#endif 235#endif
208 unsigned char flags; 236 unsigned char flags;
209} coro_cctx; 237} coro_cctx;
269#define PRIO_MIN -4 297#define PRIO_MIN -4
270 298
271/* for Coro.pm */ 299/* for Coro.pm */
272static SV *coro_current; 300static SV *coro_current;
273static SV *coro_readyhook; 301static SV *coro_readyhook;
274static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 302static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
275static int coro_nready; 303static int coro_nready;
276static struct coro *coro_first; 304static struct coro *coro_first;
277 305
278/** lowlevel stuff **********************************************************/ 306/** lowlevel stuff **********************************************************/
279 307
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 349 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 350#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 351 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 352 --AvFILLp (padlist);
325 353
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 354 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 355 av_store (newpadlist, 1, (SV *)newpad);
328 356
329 return newpadlist; 357 return newpadlist;
330} 358}
331 359
361 389
362 /* casting is fun. */ 390 /* casting is fun. */
363 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 391 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
364 free_padlist (aTHX_ padlist); 392 free_padlist (aTHX_ padlist);
365 393
394 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
395
366 return 0; 396 return 0;
367} 397}
368 398
369#define CORO_MAGIC_type_cv PERL_MAGIC_ext 399#define CORO_MAGIC_type_cv PERL_MAGIC_ext
370#define CORO_MAGIC_type_state PERL_MAGIC_ext 400#define CORO_MAGIC_type_state PERL_MAGIC_ext
372static MGVTBL coro_cv_vtbl = { 402static MGVTBL coro_cv_vtbl = {
373 0, 0, 0, 0, 403 0, 0, 0, 0,
374 coro_cv_free 404 coro_cv_free
375}; 405};
376 406
377#define CORO_MAGIC(sv,type) \ 407#define CORO_MAGIC(sv, type) \
378 SvMAGIC (sv) \ 408 SvMAGIC (sv) \
379 ? SvMAGIC (sv)->mg_type == type \ 409 ? SvMAGIC (sv)->mg_type == type \
380 ? SvMAGIC (sv) \ 410 ? SvMAGIC (sv) \
381 : mg_find (sv, type) \ 411 : mg_find (sv, type) \
382 : 0 412 : 0
383 413
384#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 414#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
385#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 415#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
386 416
387static struct coro * 417INLINE struct coro *
388SvSTATE_ (pTHX_ SV *coro) 418SvSTATE_ (pTHX_ SV *coro)
389{ 419{
390 HV *stash; 420 HV *stash;
391 MAGIC *mg; 421 MAGIC *mg;
392 422
420 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 450 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
421 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 451 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
422 else 452 else
423 { 453 {
424#if CORO_PREFER_PERL_FUNCTIONS 454#if CORO_PREFER_PERL_FUNCTIONS
425 /* this is probably cleaner, but also slower? */ 455 /* this is probably cleaner? but also slower! */
456 /* in practise, it seems to be less stable */
426 CV *cp = Perl_cv_clone (cv); 457 CV *cp = Perl_cv_clone (cv);
427 CvPADLIST (cv) = CvPADLIST (cp); 458 CvPADLIST (cv) = CvPADLIST (cp);
428 CvPADLIST (cp) = 0; 459 CvPADLIST (cp) = 0;
429 SvREFCNT_dec (cp); 460 SvREFCNT_dec (cp);
430#else 461#else
616 647
617/* 648/*
618 * destroy the stacks, the callchain etc... 649 * destroy the stacks, the callchain etc...
619 */ 650 */
620static void 651static void
621coro_destroy_stacks (pTHX) 652coro_destruct_stacks (pTHX)
622{ 653{
623 while (PL_curstackinfo->si_next) 654 while (PL_curstackinfo->si_next)
624 PL_curstackinfo = PL_curstackinfo->si_next; 655 PL_curstackinfo = PL_curstackinfo->si_next;
625 656
626 while (PL_curstackinfo) 657 while (PL_curstackinfo)
663 #undef VAR 694 #undef VAR
664 } 695 }
665 else 696 else
666 slot = coro->slot; 697 slot = coro->slot;
667 698
699 if (slot)
700 {
668 rss += sizeof (slot->curstackinfo); 701 rss += sizeof (slot->curstackinfo);
669 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 702 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
670 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 703 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
671 rss += slot->tmps_max * sizeof (SV *); 704 rss += slot->tmps_max * sizeof (SV *);
672 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 705 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
673 rss += slot->scopestack_max * sizeof (I32); 706 rss += slot->scopestack_max * sizeof (I32);
674 rss += slot->savestack_max * sizeof (ANY); 707 rss += slot->savestack_max * sizeof (ANY);
675 708
676#if !PERL_VERSION_ATLEAST (5,10,0) 709#if !PERL_VERSION_ATLEAST (5,10,0)
677 rss += slot->retstack_max * sizeof (OP *); 710 rss += slot->retstack_max * sizeof (OP *);
678#endif 711#endif
712 }
679 } 713 }
680 714
681 return rss; 715 return rss;
682} 716}
683 717
686static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 720static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
687static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 721static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
688static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 722static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
689 723
690/* apparently < 5.8.8 */ 724/* apparently < 5.8.8 */
691#undef MgPV_nolen_const
692#ifndef MgPV_nolen_const 725#ifndef MgPV_nolen_const
693#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 726#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
694 SvPV_nolen_const((SV*)((mg)->mg_ptr)) : \ 727 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
695 (const char*)(mg)->mg_ptr) 728 (const char*)(mg)->mg_ptr)
696#endif 729#endif
730
731/* we sometimes need to create the effect of entersub calling us */
732#define ENTERSUB_HEAD ENTER; SAVETMPS
733/* we somtimes need to create the effect of leaving via entersub */
734#define ENTERSUB_TAIL LEAVE
697 735
698/* 736/*
699 * This overrides the default magic get method of %SIG elements. 737 * This overrides the default magic get method of %SIG elements.
700 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 738 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
701 * and instead of tryign to save and restore the hash elements, we just provide 739 * and instead of tryign to save and restore the hash elements, we just provide
802 GvSV (PL_defgv) = newSV (0); 840 GvSV (PL_defgv) = newSV (0);
803 GvAV (PL_defgv) = coro->args; coro->args = 0; 841 GvAV (PL_defgv) = coro->args; coro->args = 0;
804 GvSV (PL_errgv) = newSV (0); 842 GvSV (PL_errgv) = newSV (0);
805 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 843 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
806 PL_rs = newSVsv (GvSV (irsgv)); 844 PL_rs = newSVsv (GvSV (irsgv));
807 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 845 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
808 846
809 { 847 {
810 dSP; 848 dSP;
811 LOGOP myop; 849 UNOP myop;
812 850
813 Zero (&myop, 1, LOGOP); 851 Zero (&myop, 1, UNOP);
814 myop.op_next = Nullop; 852 myop.op_next = Nullop;
815 myop.op_flags = OPf_WANT_VOID; 853 myop.op_flags = OPf_WANT_VOID;
816 854
817 PUSHMARK (SP); 855 PUSHMARK (SP);
818 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 856 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
822 SPAGAIN; 860 SPAGAIN;
823 } 861 }
824 862
825 /* this newly created coroutine might be run on an existing cctx which most 863 /* this newly created coroutine might be run on an existing cctx which most
826 * likely was suspended in set_stacklevel, called from entersub. 864 * likely was suspended in set_stacklevel, called from entersub.
827 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 865 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
828 * so we ENTER here for symmetry 866 * so we ENTER here for symmetry.
829 */ 867 */
830 ENTER; 868 ENTERSUB_HEAD;
831} 869}
832 870
833static void 871static void
834coro_destroy (pTHX_ struct coro *coro) 872coro_destruct (pTHX_ struct coro *coro)
835{ 873{
836 if (!IN_DESTRUCT) 874 if (!IN_DESTRUCT)
837 { 875 {
838 /* restore all saved variables and stuff */ 876 /* restore all saved variables and stuff */
839 LEAVE_SCOPE (0); 877 LEAVE_SCOPE (0);
861 SvREFCNT_dec (PL_warnhook); 899 SvREFCNT_dec (PL_warnhook);
862 900
863 SvREFCNT_dec (coro->saved_deffh); 901 SvREFCNT_dec (coro->saved_deffh);
864 SvREFCNT_dec (coro->throw); 902 SvREFCNT_dec (coro->throw);
865 903
866 coro_destroy_stacks (aTHX); 904 coro_destruct_stacks (aTHX);
867} 905}
868 906
869static void 907INLINE void
870free_coro_mortal (pTHX) 908free_coro_mortal (pTHX)
871{ 909{
872 if (expect_true (coro_mortal)) 910 if (expect_true (coro_mortal))
873 { 911 {
874 SvREFCNT_dec (coro_mortal); 912 SvREFCNT_dec (coro_mortal);
908 : cx->blk_gimme == G_SCALAR ? bot + 1 946 : cx->blk_gimme == G_SCALAR ? bot + 1
909 : bot; 947 : bot;
910 948
911 av_extend (av, top - bot); 949 av_extend (av, top - bot);
912 while (bot < top) 950 while (bot < top)
913 av_push (av, SvREFCNT_inc (*bot++)); 951 av_push (av, SvREFCNT_inc_NN (*bot++));
914 952
915 PL_runops = RUNOPS_DEFAULT; 953 PL_runops = RUNOPS_DEFAULT;
916 ENTER; 954 ENTER;
917 SAVETMPS; 955 SAVETMPS;
918 EXTEND (SP, 3); 956 EXTEND (SP, 3);
998 1036
999 TAINT_NOT; 1037 TAINT_NOT;
1000 return 0; 1038 return 0;
1001} 1039}
1002 1040
1041static void
1042prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1043{
1044 ta->prev = (struct coro *)cctx;
1045 ta->next = 0;
1046}
1047
1003/* inject a fake call to Coro::State::_cctx_init into the execution */ 1048/* inject a fake call to Coro::State::_cctx_init into the execution */
1004/* _cctx_init should be careful, as it could be called at almost any time */ 1049/* _cctx_init should be careful, as it could be called at almost any time */
1005/* during execution of a perl program */ 1050/* during execution of a perl program */
1051/* also initialises PL_top_env */
1006static void NOINLINE 1052static void NOINLINE
1007cctx_prepare (pTHX_ coro_cctx *cctx) 1053cctx_prepare (pTHX_ coro_cctx *cctx)
1008{ 1054{
1009 dSP; 1055 dSP;
1010 LOGOP myop; 1056 UNOP myop;
1011 1057
1012 PL_top_env = &PL_start_env; 1058 PL_top_env = &PL_start_env;
1013 1059
1014 if (cctx->flags & CC_TRACE) 1060 if (cctx->flags & CC_TRACE)
1015 PL_runops = runops_trace; 1061 PL_runops = runops_trace;
1016 1062
1017 Zero (&myop, 1, LOGOP); 1063 Zero (&myop, 1, UNOP);
1018 myop.op_next = PL_op; 1064 myop.op_next = PL_op;
1019 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1065 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1020 1066
1021 PUSHMARK (SP); 1067 PUSHMARK (SP);
1022 EXTEND (SP, 2); 1068 EXTEND (SP, 2);
1023 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1069 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1024 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1070 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1025 PUTBACK; 1071 PUTBACK;
1026 PL_op = (OP *)&myop; 1072 PL_op = (OP *)&myop;
1027 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1073 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1028 SPAGAIN; 1074 SPAGAIN;
1029} 1075}
1030 1076
1077/* the tail of transfer: execute stuff we can only do after a transfer */
1078INLINE void
1079transfer_tail (pTHX)
1080{
1081 struct coro *next = (struct coro *)transfer_next;
1082 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1083 assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next));
1084
1085 free_coro_mortal (aTHX);
1086 UNLOCK;
1087
1088 if (expect_false (next->throw))
1089 {
1090 SV *exception = sv_2mortal (next->throw);
1091
1092 next->throw = 0;
1093 sv_setsv (ERRSV, exception);
1094 croak (0);
1095 }
1096}
1097
1031/* 1098/*
1032 * this is a _very_ stripped down perl interpreter ;) 1099 * this is a _very_ stripped down perl interpreter ;)
1033 */ 1100 */
1034static void 1101static void
1035cctx_run (void *arg) 1102cctx_run (void *arg)
1036{ 1103{
1104#ifdef USE_ITHREADS
1105# if CORO_PTHREAD
1106 PERL_SET_CONTEXT (coro_thx);
1107# endif
1108#endif
1109 {
1037 dTHX; 1110 dTHX;
1038 1111
1039 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1112 /* entersub called ENTER, but we never 'returned', undo that here */
1040 UNLOCK; 1113 ENTERSUB_TAIL;
1041 1114
1042 /* we now skip the entersub that lead to transfer() */ 1115 /* we now skip the entersub that did lead to transfer() */
1043 PL_op = PL_op->op_next; 1116 PL_op = PL_op->op_next;
1044 1117
1045 /* inject a fake subroutine call to cctx_init */ 1118 /* inject a fake subroutine call to cctx_init */
1046 cctx_prepare (aTHX_ (coro_cctx *)arg); 1119 cctx_prepare (aTHX_ (coro_cctx *)arg);
1047 1120
1121 /* cctx_run is the alternative tail of transfer() */
1122 transfer_tail (aTHX);
1123
1048 /* somebody or something will hit me for both perl_run and PL_restartop */ 1124 /* somebody or something will hit me for both perl_run and PL_restartop */
1049 PL_restartop = PL_op; 1125 PL_restartop = PL_op;
1050 perl_run (PL_curinterp); 1126 perl_run (PL_curinterp);
1051 1127
1052 /* 1128 /*
1053 * If perl-run returns we assume exit() was being called or the coro 1129 * If perl-run returns we assume exit() was being called or the coro
1054 * fell off the end, which seems to be the only valid (non-bug) 1130 * fell off the end, which seems to be the only valid (non-bug)
1055 * reason for perl_run to return. We try to exit by jumping to the 1131 * reason for perl_run to return. We try to exit by jumping to the
1056 * bootstrap-time "top" top_env, as we cannot restore the "main" 1132 * bootstrap-time "top" top_env, as we cannot restore the "main"
1057 * coroutine as Coro has no such concept 1133 * coroutine as Coro has no such concept
1058 */ 1134 */
1059 PL_top_env = main_top_env; 1135 PL_top_env = main_top_env;
1060 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1136 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1137 }
1061} 1138}
1062 1139
1063static coro_cctx * 1140static coro_cctx *
1064cctx_new () 1141cctx_new ()
1065{ 1142{
1066 coro_cctx *cctx; 1143 coro_cctx *cctx;
1144
1145 ++cctx_count;
1146 New (0, cctx, 1, coro_cctx);
1147
1148 cctx->gen = cctx_gen;
1149 cctx->flags = 0;
1150 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1151
1152 return cctx;
1153}
1154
1155/* create a new cctx only suitable as source */
1156static coro_cctx *
1157cctx_new_empty ()
1158{
1159 coro_cctx *cctx = cctx_new ();
1160
1161 cctx->sptr = 0;
1162 coro_create (&cctx->cctx, 0, 0, 0, 0);
1163
1164 return cctx;
1165}
1166
1167/* create a new cctx suitable as destination/running a perl interpreter */
1168static coro_cctx *
1169cctx_new_run ()
1170{
1171 coro_cctx *cctx = cctx_new ();
1067 void *stack_start; 1172 void *stack_start;
1068 size_t stack_size; 1173 size_t stack_size;
1069 1174
1070 ++cctx_count;
1071
1072 Newz (0, cctx, 1, coro_cctx);
1073
1074#if HAVE_MMAP 1175#if HAVE_MMAP
1075 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1176 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1076 /* mmap supposedly does allocate-on-write for us */ 1177 /* mmap supposedly does allocate-on-write for us */
1077 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1178 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1078 1179
1079 if (cctx->sptr != (void *)-1) 1180 if (cctx->sptr != (void *)-1)
1080 { 1181 {
1081# if CORO_STACKGUARD 1182 #if CORO_STACKGUARD
1082 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1183 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1083# endif 1184 #endif
1084 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1185 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1085 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1186 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1086 cctx->flags |= CC_MAPPED; 1187 cctx->flags |= CC_MAPPED;
1087 } 1188 }
1088 else 1189 else
1089#endif 1190#endif
1090 { 1191 {
1091 cctx->ssize = coro_stacksize * (long)sizeof (long); 1192 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1092 New (0, cctx->sptr, coro_stacksize, long); 1193 New (0, cctx->sptr, cctx_stacksize, long);
1093 1194
1094 if (!cctx->sptr) 1195 if (!cctx->sptr)
1095 { 1196 {
1096 perror ("FATAL: unable to allocate stack for coroutine"); 1197 perror ("FATAL: unable to allocate stack for coroutine");
1097 _exit (EXIT_FAILURE); 1198 _exit (EXIT_FAILURE);
1099 1200
1100 stack_start = cctx->sptr; 1201 stack_start = cctx->sptr;
1101 stack_size = cctx->ssize; 1202 stack_size = cctx->ssize;
1102 } 1203 }
1103 1204
1104 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1205 #if CORO_USE_VALGRIND
1206 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1207 #endif
1208
1105 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1209 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1106 1210
1107 return cctx; 1211 return cctx;
1108} 1212}
1109 1213
1112{ 1216{
1113 if (!cctx) 1217 if (!cctx)
1114 return; 1218 return;
1115 1219
1116 --cctx_count; 1220 --cctx_count;
1221 coro_destroy (&cctx->cctx);
1117 1222
1223 /* coro_transfer creates new, empty cctx's */
1224 if (cctx->sptr)
1225 {
1118#if CORO_USE_VALGRIND 1226 #if CORO_USE_VALGRIND
1119 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1227 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1120#endif 1228 #endif
1121 1229
1122#if HAVE_MMAP 1230#if HAVE_MMAP
1123 if (cctx->flags & CC_MAPPED) 1231 if (cctx->flags & CC_MAPPED)
1124 munmap (cctx->sptr, cctx->ssize); 1232 munmap (cctx->sptr, cctx->ssize);
1125 else 1233 else
1126#endif 1234#endif
1127 Safefree (cctx->sptr); 1235 Safefree (cctx->sptr);
1236 }
1128 1237
1129 Safefree (cctx); 1238 Safefree (cctx);
1130} 1239}
1131 1240
1132/* wether this cctx should be destructed */ 1241/* wether this cctx should be destructed */
1133#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1242#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1134 1243
1135static coro_cctx * 1244static coro_cctx *
1136cctx_get (pTHX) 1245cctx_get (pTHX)
1137{ 1246{
1138 while (expect_true (cctx_first)) 1247 while (expect_true (cctx_first))
1145 return cctx; 1254 return cctx;
1146 1255
1147 cctx_destroy (cctx); 1256 cctx_destroy (cctx);
1148 } 1257 }
1149 1258
1150 return cctx_new (); 1259 return cctx_new_run ();
1151} 1260}
1152 1261
1153static void 1262static void
1154cctx_put (coro_cctx *cctx) 1263cctx_put (coro_cctx *cctx)
1155{ 1264{
1265 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1266
1156 /* free another cctx if overlimit */ 1267 /* free another cctx if overlimit */
1157 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1268 if (expect_false (cctx_idle >= cctx_max_idle))
1158 { 1269 {
1159 coro_cctx *first = cctx_first; 1270 coro_cctx *first = cctx_first;
1160 cctx_first = first->next; 1271 cctx_first = first->next;
1161 --cctx_idle; 1272 --cctx_idle;
1162 1273
1194/* always use the TRANSFER macro */ 1305/* always use the TRANSFER macro */
1195static void NOINLINE 1306static void NOINLINE
1196transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1307transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1197{ 1308{
1198 dSTACKLEVEL; 1309 dSTACKLEVEL;
1199 static volatile int has_throw;
1200 1310
1201 /* sometimes transfer is only called to set idle_sp */ 1311 /* sometimes transfer is only called to set idle_sp */
1202 if (expect_false (!next)) 1312 if (expect_false (!next))
1203 { 1313 {
1204 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1314 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1208 { 1318 {
1209 coro_cctx *prev__cctx; 1319 coro_cctx *prev__cctx;
1210 1320
1211 if (expect_false (prev->flags & CF_NEW)) 1321 if (expect_false (prev->flags & CF_NEW))
1212 { 1322 {
1213 /* create a new empty context */ 1323 /* create a new empty/source context */
1214 Newz (0, prev->cctx, 1, coro_cctx); 1324 prev->cctx = cctx_new_empty ();
1215 prev->flags &= ~CF_NEW; 1325 prev->flags &= ~CF_NEW;
1216 prev->flags |= CF_RUNNING; 1326 prev->flags |= CF_RUNNING;
1217 } 1327 }
1218 1328
1219 prev->flags &= ~CF_RUNNING; 1329 prev->flags &= ~CF_RUNNING;
1234 else 1344 else
1235 load_perl (aTHX_ next); 1345 load_perl (aTHX_ next);
1236 1346
1237 prev__cctx = prev->cctx; 1347 prev__cctx = prev->cctx;
1238 1348
1239 /* possibly "free" the cctx */ 1349 /* possibly untie and reuse the cctx */
1240 if (expect_true ( 1350 if (expect_true (
1241 prev__cctx->idle_sp == STACKLEVEL 1351 prev__cctx->idle_sp == STACKLEVEL
1242 && !(prev__cctx->flags & CC_TRACE) 1352 && !(prev__cctx->flags & CC_TRACE)
1243 && !force_cctx 1353 && !force_cctx
1244 )) 1354 ))
1245 { 1355 {
1246 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1356 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1247 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1357 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1248 1358
1249 prev->cctx = 0; 1359 prev->cctx = 0;
1250 1360
1251 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1361 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1252 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1362 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1260 ++next->usecount; 1370 ++next->usecount;
1261 1371
1262 if (expect_true (!next->cctx)) 1372 if (expect_true (!next->cctx))
1263 next->cctx = cctx_get (aTHX); 1373 next->cctx = cctx_get (aTHX);
1264 1374
1265 has_throw = !!next->throw; 1375 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1376 transfer_next = next;
1266 1377
1267 if (expect_false (prev__cctx != next->cctx)) 1378 if (expect_false (prev__cctx != next->cctx))
1268 { 1379 {
1269 prev__cctx->top_env = PL_top_env; 1380 prev__cctx->top_env = PL_top_env;
1270 PL_top_env = next->cctx->top_env; 1381 PL_top_env = next->cctx->top_env;
1271 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1382 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1272 } 1383 }
1273 1384
1274 free_coro_mortal (aTHX); 1385 transfer_tail (aTHX);
1275 UNLOCK;
1276
1277 if (expect_false (has_throw))
1278 {
1279 struct coro *coro = SvSTATE (coro_current);
1280
1281 if (coro->throw)
1282 {
1283 SV *exception = coro->throw;
1284 coro->throw = 0;
1285 sv_setsv (ERRSV, exception);
1286 croak (0);
1287 }
1288 }
1289 } 1386 }
1290} 1387}
1291
1292struct transfer_args
1293{
1294 struct coro *prev, *next;
1295};
1296 1388
1297#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1389#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1298#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1390#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1299 1391
1300/** high level stuff ********************************************************/ 1392/** high level stuff ********************************************************/
1326 croak ("FATAL: tried to destroy currently running coroutine"); 1418 croak ("FATAL: tried to destroy currently running coroutine");
1327 1419
1328 save_perl (aTHX_ &temp); 1420 save_perl (aTHX_ &temp);
1329 load_perl (aTHX_ coro); 1421 load_perl (aTHX_ coro);
1330 1422
1331 coro_destroy (aTHX_ coro); 1423 coro_destruct (aTHX_ coro);
1332 1424
1333 load_perl (aTHX_ &temp); 1425 load_perl (aTHX_ &temp);
1334 1426
1335 coro->slot = 0; 1427 coro->slot = 0;
1336 } 1428 }
1442 LOCK; 1534 LOCK;
1443 1535
1444 sv_hook = coro_nready ? 0 : coro_readyhook; 1536 sv_hook = coro_nready ? 0 : coro_readyhook;
1445 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1537 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1446 1538
1447 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1539 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1448 ++coro_nready; 1540 ++coro_nready;
1449 1541
1450 UNLOCK; 1542 UNLOCK;
1451 1543
1452 if (sv_hook) 1544 if (sv_hook)
1473 1565
1474static int 1566static int
1475api_is_ready (SV *coro_sv) 1567api_is_ready (SV *coro_sv)
1476{ 1568{
1477 dTHX; 1569 dTHX;
1570
1478 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1571 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1479} 1572}
1480 1573
1481static void 1574INLINE void
1482prepare_schedule (pTHX_ struct transfer_args *ta) 1575prepare_schedule (pTHX_ struct transfer_args *ta)
1483{ 1576{
1484 SV *prev_sv, *next_sv; 1577 SV *prev_sv, *next_sv;
1485 1578
1486 for (;;) 1579 for (;;)
1512 /* cannot transfer to destroyed coros, skip and look for next */ 1605 /* cannot transfer to destroyed coros, skip and look for next */
1513 if (expect_false (ta->next->flags & CF_DESTROYED)) 1606 if (expect_false (ta->next->flags & CF_DESTROYED))
1514 { 1607 {
1515 UNLOCK; 1608 UNLOCK;
1516 SvREFCNT_dec (next_sv); 1609 SvREFCNT_dec (next_sv);
1517 /* coro_nready is already taken care of by destroy */ 1610 /* coro_nready has already been taken care of by destroy */
1518 continue; 1611 continue;
1519 } 1612 }
1520 1613
1521 --coro_nready; 1614 --coro_nready;
1522 UNLOCK; 1615 UNLOCK;
1525 1618
1526 /* free this only after the transfer */ 1619 /* free this only after the transfer */
1527 prev_sv = SvRV (coro_current); 1620 prev_sv = SvRV (coro_current);
1528 ta->prev = SvSTATE (prev_sv); 1621 ta->prev = SvSTATE (prev_sv);
1529 TRANSFER_CHECK (*ta); 1622 TRANSFER_CHECK (*ta);
1530 assert (ta->next->flags & CF_READY); 1623 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1531 ta->next->flags &= ~CF_READY; 1624 ta->next->flags &= ~CF_READY;
1532 SvRV_set (coro_current, next_sv); 1625 SvRV_set (coro_current, next_sv);
1533 1626
1534 LOCK; 1627 LOCK;
1535 free_coro_mortal (aTHX); 1628 free_coro_mortal (aTHX);
1536 coro_mortal = prev_sv; 1629 coro_mortal = prev_sv;
1537 UNLOCK; 1630 UNLOCK;
1538} 1631}
1539 1632
1540static void 1633INLINE void
1541prepare_cede (pTHX_ struct transfer_args *ta) 1634prepare_cede (pTHX_ struct transfer_args *ta)
1542{ 1635{
1543 api_ready (coro_current); 1636 api_ready (coro_current);
1544 prepare_schedule (aTHX_ ta); 1637 prepare_schedule (aTHX_ ta);
1545} 1638}
1607 struct coro *coro = SvSTATE (coro_sv); 1700 struct coro *coro = SvSTATE (coro_sv);
1608 1701
1609 if (flags & CC_TRACE) 1702 if (flags & CC_TRACE)
1610 { 1703 {
1611 if (!coro->cctx) 1704 if (!coro->cctx)
1612 coro->cctx = cctx_new (); 1705 coro->cctx = cctx_new_run ();
1613 else if (!(coro->cctx->flags & CC_TRACE)) 1706 else if (!(coro->cctx->flags & CC_TRACE))
1614 croak ("cannot enable tracing on coroutine with custom stack"); 1707 croak ("cannot enable tracing on coroutine with custom stack");
1615 1708
1616 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1709 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1617 } 1710 }
1624 else 1717 else
1625 coro->slot->runops = RUNOPS_DEFAULT; 1718 coro->slot->runops = RUNOPS_DEFAULT;
1626 } 1719 }
1627} 1720}
1628 1721
1722#if 0
1723static int
1724coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1725{
1726 AV *padlist;
1727 AV *av = (AV *)mg->mg_obj;
1728
1729 abort ();
1730
1731 return 0;
1732}
1733
1734static MGVTBL coro_gensub_vtbl = {
1735 0, 0, 0, 0,
1736 coro_gensub_free
1737};
1738#endif
1739
1740/*****************************************************************************/
1741/* PerlIO::cede */
1742
1743typedef struct
1744{
1745 PerlIOBuf base;
1746 NV next, every;
1747} PerlIOCede;
1748
1749static IV
1750PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1751{
1752 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1753
1754 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1755 self->next = nvtime () + self->every;
1756
1757 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1758}
1759
1760static SV *
1761PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1762{
1763 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1764
1765 return newSVnv (self->every);
1766}
1767
1768static IV
1769PerlIOCede_flush (pTHX_ PerlIO *f)
1770{
1771 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1772 double now = nvtime ();
1773
1774 if (now >= self->next)
1775 {
1776 api_cede ();
1777 self->next = now + self->every;
1778 }
1779
1780 return PerlIOBuf_flush (aTHX_ f);
1781}
1782
1783static PerlIO_funcs PerlIO_cede =
1784{
1785 sizeof(PerlIO_funcs),
1786 "cede",
1787 sizeof(PerlIOCede),
1788 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1789 PerlIOCede_pushed,
1790 PerlIOBuf_popped,
1791 PerlIOBuf_open,
1792 PerlIOBase_binmode,
1793 PerlIOCede_getarg,
1794 PerlIOBase_fileno,
1795 PerlIOBuf_dup,
1796 PerlIOBuf_read,
1797 PerlIOBuf_unread,
1798 PerlIOBuf_write,
1799 PerlIOBuf_seek,
1800 PerlIOBuf_tell,
1801 PerlIOBuf_close,
1802 PerlIOCede_flush,
1803 PerlIOBuf_fill,
1804 PerlIOBase_eof,
1805 PerlIOBase_error,
1806 PerlIOBase_clearerr,
1807 PerlIOBase_setlinebuf,
1808 PerlIOBuf_get_base,
1809 PerlIOBuf_bufsiz,
1810 PerlIOBuf_get_ptr,
1811 PerlIOBuf_get_cnt,
1812 PerlIOBuf_set_ptrcnt,
1813};
1814
1815
1629MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1816MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1630 1817
1631PROTOTYPES: DISABLE 1818PROTOTYPES: DISABLE
1632 1819
1633BOOT: 1820BOOT:
1634{ 1821{
1635#ifdef USE_ITHREADS 1822#ifdef USE_ITHREADS
1636 MUTEX_INIT (&coro_mutex); 1823 MUTEX_INIT (&coro_lock);
1824# if CORO_PTHREAD
1825 coro_thx = PERL_GET_CONTEXT;
1826# endif
1637#endif 1827#endif
1638 BOOT_PAGESIZE; 1828 BOOT_PAGESIZE;
1639 1829
1640 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1830 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1641 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1831 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1662 main_top_env = main_top_env->je_prev; 1852 main_top_env = main_top_env->je_prev;
1663 1853
1664 coroapi.ver = CORO_API_VERSION; 1854 coroapi.ver = CORO_API_VERSION;
1665 coroapi.rev = CORO_API_REVISION; 1855 coroapi.rev = CORO_API_REVISION;
1666 coroapi.transfer = api_transfer; 1856 coroapi.transfer = api_transfer;
1857
1858 {
1859 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1860
1861 if (!svp) croak ("Time::HiRes is required");
1862 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1863
1864 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1865 }
1667 1866
1668 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1867 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1669} 1868}
1670 1869
1671SV * 1870SV *
1713 1912
1714 PUTBACK; 1913 PUTBACK;
1715 switch (ix) 1914 switch (ix)
1716 { 1915 {
1717 case 0: 1916 case 0:
1718 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1917 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1719 ta.next = 0;
1720 break; 1918 break;
1721 1919
1722 case 1: 1920 case 1:
1723 if (items != 2) 1921 if (items != 2)
1724 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1922 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1725 1923
1726 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1924 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1727 break; 1925 break;
1728 1926
1729 case 2: 1927 case 2:
1763 _exit (code); 1961 _exit (code);
1764 1962
1765int 1963int
1766cctx_stacksize (int new_stacksize = 0) 1964cctx_stacksize (int new_stacksize = 0)
1767 CODE: 1965 CODE:
1768 RETVAL = coro_stacksize; 1966 RETVAL = cctx_stacksize;
1769 if (new_stacksize) 1967 if (new_stacksize)
1968 {
1770 coro_stacksize = new_stacksize; 1969 cctx_stacksize = new_stacksize;
1970 ++cctx_gen;
1971 }
1972 OUTPUT:
1973 RETVAL
1974
1975int
1976cctx_max_idle (int max_idle = 0)
1977 CODE:
1978 RETVAL = cctx_max_idle;
1979 if (max_idle > 1)
1980 cctx_max_idle = max_idle;
1771 OUTPUT: 1981 OUTPUT:
1772 RETVAL 1982 RETVAL
1773 1983
1774int 1984int
1775cctx_count () 1985cctx_count ()
1799call (Coro::State coro, SV *coderef) 2009call (Coro::State coro, SV *coderef)
1800 ALIAS: 2010 ALIAS:
1801 eval = 1 2011 eval = 1
1802 CODE: 2012 CODE:
1803{ 2013{
1804 if (coro->mainstack) 2014 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1805 { 2015 {
1806 struct coro temp; 2016 struct coro temp;
1807 2017
1808 if (!(coro->flags & CF_RUNNING)) 2018 if (!(coro->flags & CF_RUNNING))
1809 { 2019 {
1853 RETVAL = boolSV (coro->flags & ix); 2063 RETVAL = boolSV (coro->flags & ix);
1854 OUTPUT: 2064 OUTPUT:
1855 RETVAL 2065 RETVAL
1856 2066
1857void 2067void
2068throw (Coro::State self, SV *throw = &PL_sv_undef)
2069 PROTOTYPE: $;$
2070 CODE:
2071 SvREFCNT_dec (self->throw);
2072 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2073
2074void
1858api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2075api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1859 2076
1860SV * 2077SV *
1861has_cctx (Coro::State coro) 2078has_cctx (Coro::State coro)
1862 PROTOTYPE: $ 2079 PROTOTYPE: $
1871 CODE: 2088 CODE:
1872 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2089 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1873 OUTPUT: 2090 OUTPUT:
1874 RETVAL 2091 RETVAL
1875 2092
1876IV 2093UV
1877rss (Coro::State coro) 2094rss (Coro::State coro)
1878 PROTOTYPE: $ 2095 PROTOTYPE: $
1879 ALIAS: 2096 ALIAS:
1880 usecount = 1 2097 usecount = 1
1881 CODE: 2098 CODE:
1891force_cctx () 2108force_cctx ()
1892 CODE: 2109 CODE:
1893 struct coro *coro = SvSTATE (coro_current); 2110 struct coro *coro = SvSTATE (coro_current);
1894 coro->cctx->idle_sp = 0; 2111 coro->cctx->idle_sp = 0;
1895 2112
1896MODULE = Coro::State PACKAGE = Coro
1897
1898BOOT:
1899{
1900 int i;
1901
1902 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1903 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1904 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1905
1906 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1907 SvREADONLY_on (coro_current);
1908
1909 coro_stash = gv_stashpv ("Coro", TRUE);
1910
1911 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1912 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1913 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1914 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1915 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1916 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1917
1918 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1919 coro_ready[i] = newAV ();
1920
1921 {
1922 SV *sv = perl_get_sv ("Coro::API", TRUE);
1923 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1924
1925 coroapi.schedule = api_schedule;
1926 coroapi.cede = api_cede;
1927 coroapi.cede_notself = api_cede_notself;
1928 coroapi.ready = api_ready;
1929 coroapi.is_ready = api_is_ready;
1930 coroapi.nready = &coro_nready;
1931 coroapi.current = coro_current;
1932
1933 GCoroAPI = &coroapi;
1934 sv_setiv (sv, (IV)&coroapi);
1935 SvREADONLY_on (sv);
1936 }
1937}
1938
1939void
1940_set_current (SV *current)
1941 PROTOTYPE: $
1942 CODE:
1943 SvREFCNT_dec (SvRV (coro_current));
1944 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1945
1946void
1947_set_readyhook (SV *hook)
1948 PROTOTYPE: $
1949 CODE:
1950 LOCK;
1951 SvREFCNT_dec (coro_readyhook);
1952 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
1953 UNLOCK;
1954
1955int
1956prio (Coro::State coro, int newprio = 0)
1957 ALIAS:
1958 nice = 1
1959 CODE:
1960{
1961 RETVAL = coro->prio;
1962
1963 if (items > 1)
1964 {
1965 if (ix)
1966 newprio = coro->prio - newprio;
1967
1968 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1969 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1970
1971 coro->prio = newprio;
1972 }
1973}
1974 OUTPUT:
1975 RETVAL
1976
1977SV *
1978ready (SV *self)
1979 PROTOTYPE: $
1980 CODE:
1981 RETVAL = boolSV (api_ready (self));
1982 OUTPUT:
1983 RETVAL
1984
1985int
1986nready (...)
1987 PROTOTYPE:
1988 CODE:
1989 RETVAL = coro_nready;
1990 OUTPUT:
1991 RETVAL
1992
1993void
1994throw (Coro::State self, SV *throw = &PL_sv_undef)
1995 PROTOTYPE: $;$
1996 CODE:
1997 SvREFCNT_dec (self->throw);
1998 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1999
2000void 2113void
2001swap_defsv (Coro::State self) 2114swap_defsv (Coro::State self)
2002 PROTOTYPE: $ 2115 PROTOTYPE: $
2003 ALIAS: 2116 ALIAS:
2004 swap_defav = 1 2117 swap_defav = 1
2011 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2124 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2012 2125
2013 SV *tmp = *src; *src = *dst; *dst = tmp; 2126 SV *tmp = *src; *src = *dst; *dst = tmp;
2014 } 2127 }
2015 2128
2129MODULE = Coro::State PACKAGE = Coro
2130
2131BOOT:
2132{
2133 int i;
2134
2135 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2136 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2137 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2138
2139 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2140 SvREADONLY_on (coro_current);
2141
2142 coro_stash = gv_stashpv ("Coro", TRUE);
2143
2144 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2145 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2146 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2147 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2148 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2149 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2150
2151 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2152 coro_ready[i] = newAV ();
2153
2154 {
2155 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2156
2157 coroapi.schedule = api_schedule;
2158 coroapi.cede = api_cede;
2159 coroapi.cede_notself = api_cede_notself;
2160 coroapi.ready = api_ready;
2161 coroapi.is_ready = api_is_ready;
2162 coroapi.nready = &coro_nready;
2163 coroapi.current = coro_current;
2164
2165 GCoroAPI = &coroapi;
2166 sv_setiv (sv, (IV)&coroapi);
2167 SvREADONLY_on (sv);
2168 }
2169}
2170
2171void
2172_set_current (SV *current)
2173 PROTOTYPE: $
2174 CODE:
2175 SvREFCNT_dec (SvRV (coro_current));
2176 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2177
2178void
2179_set_readyhook (SV *hook)
2180 PROTOTYPE: $
2181 CODE:
2182 LOCK;
2183 SvREFCNT_dec (coro_readyhook);
2184 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2185 UNLOCK;
2186
2187int
2188prio (Coro::State coro, int newprio = 0)
2189 ALIAS:
2190 nice = 1
2191 CODE:
2192{
2193 RETVAL = coro->prio;
2194
2195 if (items > 1)
2196 {
2197 if (ix)
2198 newprio = coro->prio - newprio;
2199
2200 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2201 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2202
2203 coro->prio = newprio;
2204 }
2205}
2206 OUTPUT:
2207 RETVAL
2208
2209SV *
2210ready (SV *self)
2211 PROTOTYPE: $
2212 CODE:
2213 RETVAL = boolSV (api_ready (self));
2214 OUTPUT:
2215 RETVAL
2216
2217int
2218nready (...)
2219 PROTOTYPE:
2220 CODE:
2221 RETVAL = coro_nready;
2222 OUTPUT:
2223 RETVAL
2224
2016# for async_pool speedup 2225# for async_pool speedup
2017void 2226void
2018_pool_1 (SV *cb) 2227_pool_1 (SV *cb)
2019 CODE: 2228 CODE:
2020{ 2229{
2032 SvREFCNT_dec (old); 2241 SvREFCNT_dec (old);
2033 croak ("\3async_pool terminate\2\n"); 2242 croak ("\3async_pool terminate\2\n");
2034 } 2243 }
2035 2244
2036 SvREFCNT_dec (coro->saved_deffh); 2245 SvREFCNT_dec (coro->saved_deffh);
2037 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2246 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2038 2247
2039 hv_store (hv, "desc", sizeof ("desc") - 1, 2248 hv_store (hv, "desc", sizeof ("desc") - 1,
2040 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2249 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2041 2250
2042 invoke_av = (AV *)SvRV (invoke); 2251 invoke_av = (AV *)SvRV (invoke);
2046 2255
2047 if (len > 0) 2256 if (len > 0)
2048 { 2257 {
2049 av_fill (defav, len - 1); 2258 av_fill (defav, len - 1);
2050 for (i = 0; i < len; ++i) 2259 for (i = 0; i < len; ++i)
2051 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2260 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2052 } 2261 }
2053 2262
2054 SvREFCNT_dec (invoke); 2263 SvREFCNT_dec (invoke);
2055} 2264}
2056 2265
2063 sv_setsv (cb, &PL_sv_undef); 2272 sv_setsv (cb, &PL_sv_undef);
2064 2273
2065 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2274 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2066 coro->saved_deffh = 0; 2275 coro->saved_deffh = 0;
2067 2276
2068 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2277 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2069 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2278 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2070 { 2279 {
2071 SV *old = PL_diehook; 2280 SV *old = PL_diehook;
2072 PL_diehook = 0; 2281 PL_diehook = 0;
2073 SvREFCNT_dec (old); 2282 SvREFCNT_dec (old);
2084 api_trace (coro_current, 0); 2293 api_trace (coro_current, 0);
2085 2294
2086 av_push (av_async_pool, newSVsv (coro_current)); 2295 av_push (av_async_pool, newSVsv (coro_current));
2087} 2296}
2088 2297
2298#if 0
2299
2300void
2301_generator_call (...)
2302 PROTOTYPE: @
2303 PPCODE:
2304 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2305 xxxx
2306 abort ();
2307
2308SV *
2309gensub (SV *sub, ...)
2310 PROTOTYPE: &;@
2311 CODE:
2312{
2313 struct coro *coro;
2314 MAGIC *mg;
2315 CV *xcv;
2316 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2317 int i;
2318
2319 CvGV (ncv) = CvGV (cv);
2320 CvFILE (ncv) = CvFILE (cv);
2321
2322 Newz (0, coro, 1, struct coro);
2323 coro->args = newAV ();
2324 coro->flags = CF_NEW;
2325
2326 av_extend (coro->args, items - 1);
2327 for (i = 1; i < items; i++)
2328 av_push (coro->args, newSVsv (ST (i)));
2329
2330 CvISXSUB_on (ncv);
2331 CvXSUBANY (ncv).any_ptr = (void *)coro;
2332
2333 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2334
2335 CvXSUB (ncv) = CvXSUB (xcv);
2336 CvANON_on (ncv);
2337
2338 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2339 RETVAL = newRV_noinc ((SV *)ncv);
2340}
2341 OUTPUT:
2342 RETVAL
2343
2344#endif
2345
2089 2346
2090MODULE = Coro::State PACKAGE = Coro::AIO 2347MODULE = Coro::State PACKAGE = Coro::AIO
2091 2348
2092SV * 2349void
2093_get_state () 2350_get_state (SV *self)
2094 CODE: 2351 PPCODE:
2095{ 2352{
2096 struct io_state *data; 2353 AV *defav = GvAV (PL_defgv);
2097 2354 AV *av = newAV ();
2355 int i;
2098 RETVAL = newSV (sizeof (struct io_state)); 2356 SV *data_sv = newSV (sizeof (struct io_state));
2099 data = (struct io_state *)SvPVX (RETVAL); 2357 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2100 SvCUR_set (RETVAL, sizeof (struct io_state)); 2358 SvCUR_set (data_sv, sizeof (struct io_state));
2101 SvPOK_only (RETVAL); 2359 SvPOK_only (data_sv);
2102 2360
2103 data->errorno = errno; 2361 data->errorno = errno;
2104 data->laststype = PL_laststype; 2362 data->laststype = PL_laststype;
2105 data->laststatval = PL_laststatval; 2363 data->laststatval = PL_laststatval;
2106 data->statcache = PL_statcache; 2364 data->statcache = PL_statcache;
2365
2366 av_extend (av, AvFILLp (defav) + 1 + 1);
2367
2368 for (i = 0; i <= AvFILLp (defav); ++i)
2369 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2370
2371 av_push (av, data_sv);
2372
2373 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2374
2375 api_ready (self);
2107} 2376}
2108 OUTPUT:
2109 RETVAL
2110 2377
2111void 2378void
2112_set_state (char *data_) 2379_set_state (SV *state)
2113 PROTOTYPE: $ 2380 PROTOTYPE: $
2114 CODE: 2381 PPCODE:
2115{ 2382{
2116 struct io_state *data = (void *)data_; 2383 AV *av = (AV *)SvRV (state);
2384 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2385 int i;
2117 2386
2118 errno = data->errorno; 2387 errno = data->errorno;
2119 PL_laststype = data->laststype; 2388 PL_laststype = data->laststype;
2120 PL_laststatval = data->laststatval; 2389 PL_laststatval = data->laststatval;
2121 PL_statcache = data->statcache; 2390 PL_statcache = data->statcache;
2391
2392 EXTEND (SP, AvFILLp (av));
2393 for (i = 0; i < AvFILLp (av); ++i)
2394 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2122} 2395}
2123 2396
2124 2397
2125MODULE = Coro::State PACKAGE = Coro::AnyEvent 2398MODULE = Coro::State PACKAGE = Coro::AnyEvent
2126 2399
2150 } 2423 }
2151 2424
2152 --incede; 2425 --incede;
2153} 2426}
2154 2427
2428
2429MODULE = Coro::State PACKAGE = PerlIO::cede
2430
2431BOOT:
2432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2433

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines