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.244 by root, Sun Sep 21 18:29:39 2008 UTC vs.
Revision 1.258 by root, Sun Nov 9 23:08:49 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>
51#else 52#else
52# define REGISTER_STACK(cctx,start,end) 53# define REGISTER_STACK(cctx,start,end)
53#endif 54#endif
54 55
55/* the maximum number of idle cctx that will be pooled */ 56/* the maximum number of idle cctx that will be pooled */
56#define MAX_IDLE_CCTX 8 57static int cctx_max_idle = 4;
57 58
58#define PERL_VERSION_ATLEAST(a,b,c) \ 59#define PERL_VERSION_ATLEAST(a,b,c) \
59 (PERL_REVISION > (a) \ 60 (PERL_REVISION > (a) \
60 || (PERL_REVISION == (a) \ 61 || (PERL_REVISION == (a) \
61 && (PERL_VERSION > (b) \ 62 && (PERL_VERSION > (b) \
80# ifndef IS_PADCONST 81# ifndef IS_PADCONST
81# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
82# endif 83# endif
83#endif 84#endif
84 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
85/* 5.8.8 */ 96/* 5.8.8 */
86#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
87# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
88#endif 99#endif
89#ifndef newSV 100#ifndef newSV
90# define newSV(l) NEWSV(0,l) 101# 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 102#endif
97 103
98/* 5.8.7 */ 104/* 5.8.7 */
99#ifndef SvRV_set 105#ifndef SvRV_set
100# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
136#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
137 143
138#include "CoroAPI.h" 144#include "CoroAPI.h"
139 145
140#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
141static perl_mutex coro_mutex; 148static perl_mutex coro_lock;
142# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
143# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD
152static void *coro_thx;
153# endif
154
144#else 155#else
156
145# define LOCK (void)0 157# define LOCK (void)0
146# define UNLOCK (void)0 158# define UNLOCK (void)0
159
147#endif 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
148 166
149/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
150struct io_state 168struct io_state
151{ 169{
170 AV *res;
152 int errorno; 171 int errorno;
153 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
154 int laststatval; 173 int laststatval;
155 Stat_t statcache; 174 Stat_t statcache;
156}; 175};
157 176
177static double (*nvtime)(); /* so why doesn't it take void? */
178
179static U32 cctx_gen;
158static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 183static JMPENV *main_top_env;
162static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
163static volatile SV *coro_mortal; /* will be freed after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
164 186
165static GV *irsgv; /* $/ */ 187static GV *irsgv; /* $/ */
166static GV *stdoutgv; /* *STDOUT */ 188static GV *stdoutgv; /* *STDOUT */
167static SV *rv_diehook; 189static SV *rv_diehook;
168static SV *rv_warnhook; 190static SV *rv_warnhook;
200 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 222 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 */ 223 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
202 JMPENV *top_env; 224 JMPENV *top_env;
203 coro_context cctx; 225 coro_context cctx;
204 226
227 U32 gen;
205#if CORO_USE_VALGRIND 228#if CORO_USE_VALGRIND
206 int valgrind_id; 229 int valgrind_id;
207#endif 230#endif
208 unsigned char flags; 231 unsigned char flags;
209} coro_cctx; 232} coro_cctx;
269#define PRIO_MIN -4 292#define PRIO_MIN -4
270 293
271/* for Coro.pm */ 294/* for Coro.pm */
272static SV *coro_current; 295static SV *coro_current;
273static SV *coro_readyhook; 296static SV *coro_readyhook;
274static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 297static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
275static int coro_nready; 298static int coro_nready;
276static struct coro *coro_first; 299static struct coro *coro_first;
277 300
278/** lowlevel stuff **********************************************************/ 301/** lowlevel stuff **********************************************************/
279 302
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 344 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 345#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 346 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 347 --AvFILLp (padlist);
325 348
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 349 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 350 av_store (newpadlist, 1, (SV *)newpad);
328 351
329 return newpadlist; 352 return newpadlist;
330} 353}
331 354
361 384
362 /* casting is fun. */ 385 /* casting is fun. */
363 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 386 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
364 free_padlist (aTHX_ padlist); 387 free_padlist (aTHX_ padlist);
365 388
366 SvREFCNT_dec (av); 389 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
367 390
368 return 0; 391 return 0;
369} 392}
370 393
371#define CORO_MAGIC_type_cv PERL_MAGIC_ext 394#define CORO_MAGIC_type_cv PERL_MAGIC_ext
374static MGVTBL coro_cv_vtbl = { 397static MGVTBL coro_cv_vtbl = {
375 0, 0, 0, 0, 398 0, 0, 0, 0,
376 coro_cv_free 399 coro_cv_free
377}; 400};
378 401
379#define CORO_MAGIC(sv,type) \ 402#define CORO_MAGIC(sv, type) \
380 SvMAGIC (sv) \ 403 SvMAGIC (sv) \
381 ? SvMAGIC (sv)->mg_type == type \ 404 ? SvMAGIC (sv)->mg_type == type \
382 ? SvMAGIC (sv) \ 405 ? SvMAGIC (sv) \
383 : mg_find (sv, type) \ 406 : mg_find (sv, type) \
384 : 0 407 : 0
385 408
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 409#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 410#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
388 411
389static struct coro * 412static struct coro *
619 642
620/* 643/*
621 * destroy the stacks, the callchain etc... 644 * destroy the stacks, the callchain etc...
622 */ 645 */
623static void 646static void
624coro_destroy_stacks (pTHX) 647coro_destruct_stacks (pTHX)
625{ 648{
626 while (PL_curstackinfo->si_next) 649 while (PL_curstackinfo->si_next)
627 PL_curstackinfo = PL_curstackinfo->si_next; 650 PL_curstackinfo = PL_curstackinfo->si_next;
628 651
629 while (PL_curstackinfo) 652 while (PL_curstackinfo)
666 #undef VAR 689 #undef VAR
667 } 690 }
668 else 691 else
669 slot = coro->slot; 692 slot = coro->slot;
670 693
694 if (slot)
695 {
671 rss += sizeof (slot->curstackinfo); 696 rss += sizeof (slot->curstackinfo);
672 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 697 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
673 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 698 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
674 rss += slot->tmps_max * sizeof (SV *); 699 rss += slot->tmps_max * sizeof (SV *);
675 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 700 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
676 rss += slot->scopestack_max * sizeof (I32); 701 rss += slot->scopestack_max * sizeof (I32);
677 rss += slot->savestack_max * sizeof (ANY); 702 rss += slot->savestack_max * sizeof (ANY);
678 703
679#if !PERL_VERSION_ATLEAST (5,10,0) 704#if !PERL_VERSION_ATLEAST (5,10,0)
680 rss += slot->retstack_max * sizeof (OP *); 705 rss += slot->retstack_max * sizeof (OP *);
681#endif 706#endif
707 }
682 } 708 }
683 709
684 return rss; 710 return rss;
685} 711}
686 712
804 GvSV (PL_defgv) = newSV (0); 830 GvSV (PL_defgv) = newSV (0);
805 GvAV (PL_defgv) = coro->args; coro->args = 0; 831 GvAV (PL_defgv) = coro->args; coro->args = 0;
806 GvSV (PL_errgv) = newSV (0); 832 GvSV (PL_errgv) = newSV (0);
807 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 833 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
808 PL_rs = newSVsv (GvSV (irsgv)); 834 PL_rs = newSVsv (GvSV (irsgv));
809 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 835 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
810 836
811 { 837 {
812 dSP; 838 dSP;
813 LOGOP myop; 839 LOGOP myop;
814 840
831 */ 857 */
832 ENTER; 858 ENTER;
833} 859}
834 860
835static void 861static void
836coro_destroy (pTHX_ struct coro *coro) 862coro_destruct (pTHX_ struct coro *coro)
837{ 863{
838 if (!IN_DESTRUCT) 864 if (!IN_DESTRUCT)
839 { 865 {
840 /* restore all saved variables and stuff */ 866 /* restore all saved variables and stuff */
841 LEAVE_SCOPE (0); 867 LEAVE_SCOPE (0);
863 SvREFCNT_dec (PL_warnhook); 889 SvREFCNT_dec (PL_warnhook);
864 890
865 SvREFCNT_dec (coro->saved_deffh); 891 SvREFCNT_dec (coro->saved_deffh);
866 SvREFCNT_dec (coro->throw); 892 SvREFCNT_dec (coro->throw);
867 893
868 coro_destroy_stacks (aTHX); 894 coro_destruct_stacks (aTHX);
869} 895}
870 896
871static void 897static void
872free_coro_mortal (pTHX) 898free_coro_mortal (pTHX)
873{ 899{
910 : cx->blk_gimme == G_SCALAR ? bot + 1 936 : cx->blk_gimme == G_SCALAR ? bot + 1
911 : bot; 937 : bot;
912 938
913 av_extend (av, top - bot); 939 av_extend (av, top - bot);
914 while (bot < top) 940 while (bot < top)
915 av_push (av, SvREFCNT_inc (*bot++)); 941 av_push (av, SvREFCNT_inc_NN (*bot++));
916 942
917 PL_runops = RUNOPS_DEFAULT; 943 PL_runops = RUNOPS_DEFAULT;
918 ENTER; 944 ENTER;
919 SAVETMPS; 945 SAVETMPS;
920 EXTEND (SP, 3); 946 EXTEND (SP, 3);
1034 * this is a _very_ stripped down perl interpreter ;) 1060 * this is a _very_ stripped down perl interpreter ;)
1035 */ 1061 */
1036static void 1062static void
1037cctx_run (void *arg) 1063cctx_run (void *arg)
1038{ 1064{
1065#ifdef USE_ITHREADS
1066# if CORO_PTHREAD
1067 PERL_SET_CONTEXT (coro_thx);
1068# endif
1069#endif
1070 {
1039 dTHX; 1071 dTHX;
1040 1072
1041 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1073 /* cctx_run is the alternative tail of transfer(), so unlock here. */
1042 UNLOCK; 1074 UNLOCK;
1043 1075
1044 /* we now skip the entersub that lead to transfer() */ 1076 /* we now skip the entersub that lead to transfer() */
1045 PL_op = PL_op->op_next; 1077 PL_op = PL_op->op_next;
1046 1078
1047 /* inject a fake subroutine call to cctx_init */ 1079 /* inject a fake subroutine call to cctx_init */
1048 cctx_prepare (aTHX_ (coro_cctx *)arg); 1080 cctx_prepare (aTHX_ (coro_cctx *)arg);
1049 1081
1050 /* somebody or something will hit me for both perl_run and PL_restartop */ 1082 /* somebody or something will hit me for both perl_run and PL_restartop */
1051 PL_restartop = PL_op; 1083 PL_restartop = PL_op;
1052 perl_run (PL_curinterp); 1084 perl_run (PL_curinterp);
1053 1085
1054 /* 1086 /*
1055 * If perl-run returns we assume exit() was being called or the coro 1087 * If perl-run returns we assume exit() was being called or the coro
1056 * fell off the end, which seems to be the only valid (non-bug) 1088 * fell off the end, which seems to be the only valid (non-bug)
1057 * reason for perl_run to return. We try to exit by jumping to the 1089 * reason for perl_run to return. We try to exit by jumping to the
1058 * bootstrap-time "top" top_env, as we cannot restore the "main" 1090 * bootstrap-time "top" top_env, as we cannot restore the "main"
1059 * coroutine as Coro has no such concept 1091 * coroutine as Coro has no such concept
1060 */ 1092 */
1061 PL_top_env = main_top_env; 1093 PL_top_env = main_top_env;
1062 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1094 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1095 }
1063} 1096}
1064 1097
1065static coro_cctx * 1098static coro_cctx *
1066cctx_new () 1099cctx_new ()
1067{ 1100{
1068 coro_cctx *cctx; 1101 coro_cctx *cctx;
1102
1103 ++cctx_count;
1104 New (0, cctx, 1, coro_cctx);
1105
1106 cctx->gen = cctx_gen;
1107 cctx->flags = 0;
1108
1109 return cctx;
1110}
1111
1112/* create a new cctx only suitable as source */
1113static coro_cctx *
1114cctx_new_empty ()
1115{
1116 coro_cctx *cctx = cctx_new ();
1117
1118 cctx->sptr = 0;
1119 cctx->idle_sp = 0; /* should never be a valid address */
1120 coro_create (&cctx->cctx, 0, 0, 0, 0);
1121
1122 return cctx;
1123}
1124
1125/* create a new cctx suitable as destination/running a perl interpreter */
1126static coro_cctx *
1127cctx_new_run ()
1128{
1129 coro_cctx *cctx = cctx_new ();
1069 void *stack_start; 1130 void *stack_start;
1070 size_t stack_size; 1131 size_t stack_size;
1071 1132
1072 ++cctx_count;
1073
1074 Newz (0, cctx, 1, coro_cctx);
1075
1076#if HAVE_MMAP 1133#if HAVE_MMAP
1077 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1134 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1078 /* mmap supposedly does allocate-on-write for us */ 1135 /* mmap supposedly does allocate-on-write for us */
1079 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1136 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1080 1137
1081 if (cctx->sptr != (void *)-1) 1138 if (cctx->sptr != (void *)-1)
1082 { 1139 {
1088 cctx->flags |= CC_MAPPED; 1145 cctx->flags |= CC_MAPPED;
1089 } 1146 }
1090 else 1147 else
1091#endif 1148#endif
1092 { 1149 {
1093 cctx->ssize = coro_stacksize * (long)sizeof (long); 1150 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1094 New (0, cctx->sptr, coro_stacksize, long); 1151 New (0, cctx->sptr, cctx_stacksize, long);
1095 1152
1096 if (!cctx->sptr) 1153 if (!cctx->sptr)
1097 { 1154 {
1098 perror ("FATAL: unable to allocate stack for coroutine"); 1155 perror ("FATAL: unable to allocate stack for coroutine");
1099 _exit (EXIT_FAILURE); 1156 _exit (EXIT_FAILURE);
1114{ 1171{
1115 if (!cctx) 1172 if (!cctx)
1116 return; 1173 return;
1117 1174
1118 --cctx_count; 1175 --cctx_count;
1176 coro_destroy (&cctx->cctx);
1119 1177
1178 /* coro_transfer creates new, empty cctx's */
1179 if (cctx->sptr)
1180 {
1120#if CORO_USE_VALGRIND 1181#if CORO_USE_VALGRIND
1121 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1182 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1122#endif 1183#endif
1123 1184
1124#if HAVE_MMAP 1185#if HAVE_MMAP
1125 if (cctx->flags & CC_MAPPED) 1186 if (cctx->flags & CC_MAPPED)
1126 munmap (cctx->sptr, cctx->ssize); 1187 munmap (cctx->sptr, cctx->ssize);
1127 else 1188 else
1128#endif 1189#endif
1129 Safefree (cctx->sptr); 1190 Safefree (cctx->sptr);
1191 }
1130 1192
1131 Safefree (cctx); 1193 Safefree (cctx);
1132} 1194}
1133 1195
1134/* wether this cctx should be destructed */ 1196/* wether this cctx should be destructed */
1135#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1197#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1136 1198
1137static coro_cctx * 1199static coro_cctx *
1138cctx_get (pTHX) 1200cctx_get (pTHX)
1139{ 1201{
1140 while (expect_true (cctx_first)) 1202 while (expect_true (cctx_first))
1147 return cctx; 1209 return cctx;
1148 1210
1149 cctx_destroy (cctx); 1211 cctx_destroy (cctx);
1150 } 1212 }
1151 1213
1152 return cctx_new (); 1214 return cctx_new_run ();
1153} 1215}
1154 1216
1155static void 1217static void
1156cctx_put (coro_cctx *cctx) 1218cctx_put (coro_cctx *cctx)
1157{ 1219{
1220 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1221
1158 /* free another cctx if overlimit */ 1222 /* free another cctx if overlimit */
1159 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1223 if (expect_false (cctx_idle >= cctx_max_idle))
1160 { 1224 {
1161 coro_cctx *first = cctx_first; 1225 coro_cctx *first = cctx_first;
1162 cctx_first = first->next; 1226 cctx_first = first->next;
1163 --cctx_idle; 1227 --cctx_idle;
1164 1228
1196/* always use the TRANSFER macro */ 1260/* always use the TRANSFER macro */
1197static void NOINLINE 1261static void NOINLINE
1198transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1262transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1199{ 1263{
1200 dSTACKLEVEL; 1264 dSTACKLEVEL;
1201 static volatile int has_throw;
1202 1265
1203 /* sometimes transfer is only called to set idle_sp */ 1266 /* sometimes transfer is only called to set idle_sp */
1204 if (expect_false (!next)) 1267 if (expect_false (!next))
1205 { 1268 {
1206 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1269 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1207 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1270 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1208 } 1271 }
1209 else if (expect_true (prev != next)) 1272 else if (expect_true (prev != next))
1210 { 1273 {
1274 static volatile int has_throw;
1211 coro_cctx *prev__cctx; 1275 coro_cctx *prev__cctx;
1212 1276
1213 if (expect_false (prev->flags & CF_NEW)) 1277 if (expect_false (prev->flags & CF_NEW))
1214 { 1278 {
1215 /* create a new empty context */ 1279 /* create a new empty/source context */
1216 Newz (0, prev->cctx, 1, coro_cctx); 1280 prev->cctx = cctx_new_empty ();
1217 prev->flags &= ~CF_NEW; 1281 prev->flags &= ~CF_NEW;
1218 prev->flags |= CF_RUNNING; 1282 prev->flags |= CF_RUNNING;
1219 } 1283 }
1220 1284
1221 prev->flags &= ~CF_RUNNING; 1285 prev->flags &= ~CF_RUNNING;
1328 croak ("FATAL: tried to destroy currently running coroutine"); 1392 croak ("FATAL: tried to destroy currently running coroutine");
1329 1393
1330 save_perl (aTHX_ &temp); 1394 save_perl (aTHX_ &temp);
1331 load_perl (aTHX_ coro); 1395 load_perl (aTHX_ coro);
1332 1396
1333 coro_destroy (aTHX_ coro); 1397 coro_destruct (aTHX_ coro);
1334 1398
1335 load_perl (aTHX_ &temp); 1399 load_perl (aTHX_ &temp);
1336 1400
1337 coro->slot = 0; 1401 coro->slot = 0;
1338 } 1402 }
1444 LOCK; 1508 LOCK;
1445 1509
1446 sv_hook = coro_nready ? 0 : coro_readyhook; 1510 sv_hook = coro_nready ? 0 : coro_readyhook;
1447 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1511 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1448 1512
1449 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1513 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1450 ++coro_nready; 1514 ++coro_nready;
1451 1515
1452 UNLOCK; 1516 UNLOCK;
1453 1517
1454 if (sv_hook) 1518 if (sv_hook)
1609 struct coro *coro = SvSTATE (coro_sv); 1673 struct coro *coro = SvSTATE (coro_sv);
1610 1674
1611 if (flags & CC_TRACE) 1675 if (flags & CC_TRACE)
1612 { 1676 {
1613 if (!coro->cctx) 1677 if (!coro->cctx)
1614 coro->cctx = cctx_new (); 1678 coro->cctx = cctx_new_run ();
1615 else if (!(coro->cctx->flags & CC_TRACE)) 1679 else if (!(coro->cctx->flags & CC_TRACE))
1616 croak ("cannot enable tracing on coroutine with custom stack"); 1680 croak ("cannot enable tracing on coroutine with custom stack");
1617 1681
1618 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1682 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1619 } 1683 }
1626 else 1690 else
1627 coro->slot->runops = RUNOPS_DEFAULT; 1691 coro->slot->runops = RUNOPS_DEFAULT;
1628 } 1692 }
1629} 1693}
1630 1694
1695#if 0
1631static int 1696static int
1632coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1697coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1633{ 1698{
1634 AV *padlist; 1699 AV *padlist;
1635 AV *av = (AV *)mg->mg_obj; 1700 AV *av = (AV *)mg->mg_obj;
1641 1706
1642static MGVTBL coro_gensub_vtbl = { 1707static MGVTBL coro_gensub_vtbl = {
1643 0, 0, 0, 0, 1708 0, 0, 0, 0,
1644 coro_gensub_free 1709 coro_gensub_free
1645}; 1710};
1711#endif
1712
1713/*****************************************************************************/
1714/* PerlIO::cede */
1715
1716typedef struct
1717{
1718 PerlIOBuf base;
1719 NV next, every;
1720} PerlIOCede;
1721
1722static IV
1723PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1724{
1725 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1726
1727 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1728 self->next = nvtime () + self->every;
1729
1730 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1731}
1732
1733static SV *
1734PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1735{
1736 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1737
1738 return newSVnv (self->every);
1739}
1740
1741static IV
1742PerlIOCede_flush (pTHX_ PerlIO *f)
1743{
1744 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1745 double now = nvtime ();
1746
1747 if (now >= self->next)
1748 {
1749 api_cede ();
1750 self->next = now + self->every;
1751 }
1752
1753 return PerlIOBuf_flush (aTHX_ f);
1754}
1755
1756static PerlIO_funcs PerlIO_cede =
1757{
1758 sizeof(PerlIO_funcs),
1759 "cede",
1760 sizeof(PerlIOCede),
1761 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1762 PerlIOCede_pushed,
1763 PerlIOBuf_popped,
1764 PerlIOBuf_open,
1765 PerlIOBase_binmode,
1766 PerlIOCede_getarg,
1767 PerlIOBase_fileno,
1768 PerlIOBuf_dup,
1769 PerlIOBuf_read,
1770 PerlIOBuf_unread,
1771 PerlIOBuf_write,
1772 PerlIOBuf_seek,
1773 PerlIOBuf_tell,
1774 PerlIOBuf_close,
1775 PerlIOCede_flush,
1776 PerlIOBuf_fill,
1777 PerlIOBase_eof,
1778 PerlIOBase_error,
1779 PerlIOBase_clearerr,
1780 PerlIOBase_setlinebuf,
1781 PerlIOBuf_get_base,
1782 PerlIOBuf_bufsiz,
1783 PerlIOBuf_get_ptr,
1784 PerlIOBuf_get_cnt,
1785 PerlIOBuf_set_ptrcnt,
1786};
1787
1646 1788
1647MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1789MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1648 1790
1649PROTOTYPES: DISABLE 1791PROTOTYPES: DISABLE
1650 1792
1651BOOT: 1793BOOT:
1652{ 1794{
1653#ifdef USE_ITHREADS 1795#ifdef USE_ITHREADS
1654 MUTEX_INIT (&coro_mutex); 1796 MUTEX_INIT (&coro_lock);
1797# if CORO_PTHREAD
1798 coro_thx = PERL_GET_CONTEXT;
1799# endif
1655#endif 1800#endif
1656 BOOT_PAGESIZE; 1801 BOOT_PAGESIZE;
1657 1802
1658 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1803 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1659 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1804 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1680 main_top_env = main_top_env->je_prev; 1825 main_top_env = main_top_env->je_prev;
1681 1826
1682 coroapi.ver = CORO_API_VERSION; 1827 coroapi.ver = CORO_API_VERSION;
1683 coroapi.rev = CORO_API_REVISION; 1828 coroapi.rev = CORO_API_REVISION;
1684 coroapi.transfer = api_transfer; 1829 coroapi.transfer = api_transfer;
1830
1831 {
1832 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1833
1834 if (!svp) croak ("Time::HiRes is required");
1835 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1836
1837 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1838 }
1685 1839
1686 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1840 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1687} 1841}
1688 1842
1689SV * 1843SV *
1737 ta.next = 0; 1891 ta.next = 0;
1738 break; 1892 break;
1739 1893
1740 case 1: 1894 case 1:
1741 if (items != 2) 1895 if (items != 2)
1742 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1896 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1743 1897
1744 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1898 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1745 break; 1899 break;
1746 1900
1747 case 2: 1901 case 2:
1781 _exit (code); 1935 _exit (code);
1782 1936
1783int 1937int
1784cctx_stacksize (int new_stacksize = 0) 1938cctx_stacksize (int new_stacksize = 0)
1785 CODE: 1939 CODE:
1786 RETVAL = coro_stacksize; 1940 RETVAL = cctx_stacksize;
1787 if (new_stacksize) 1941 if (new_stacksize)
1942 {
1788 coro_stacksize = new_stacksize; 1943 cctx_stacksize = new_stacksize;
1944 ++cctx_gen;
1945 }
1946 OUTPUT:
1947 RETVAL
1948
1949int
1950cctx_max_idle (int max_idle = 0)
1951 CODE:
1952 RETVAL = cctx_max_idle;
1953 if (max_idle > 1)
1954 cctx_max_idle = max_idle;
1789 OUTPUT: 1955 OUTPUT:
1790 RETVAL 1956 RETVAL
1791 1957
1792int 1958int
1793cctx_count () 1959cctx_count ()
1817call (Coro::State coro, SV *coderef) 1983call (Coro::State coro, SV *coderef)
1818 ALIAS: 1984 ALIAS:
1819 eval = 1 1985 eval = 1
1820 CODE: 1986 CODE:
1821{ 1987{
1822 if (coro->mainstack) 1988 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1823 { 1989 {
1824 struct coro temp; 1990 struct coro temp;
1825 1991
1826 if (!(coro->flags & CF_RUNNING)) 1992 if (!(coro->flags & CF_RUNNING))
1827 { 1993 {
1889 CODE: 2055 CODE:
1890 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2056 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1891 OUTPUT: 2057 OUTPUT:
1892 RETVAL 2058 RETVAL
1893 2059
1894IV 2060UV
1895rss (Coro::State coro) 2061rss (Coro::State coro)
1896 PROTOTYPE: $ 2062 PROTOTYPE: $
1897 ALIAS: 2063 ALIAS:
1898 usecount = 1 2064 usecount = 1
1899 CODE: 2065 CODE:
1910 CODE: 2076 CODE:
1911 struct coro *coro = SvSTATE (coro_current); 2077 struct coro *coro = SvSTATE (coro_current);
1912 coro->cctx->idle_sp = 0; 2078 coro->cctx->idle_sp = 0;
1913 2079
1914void 2080void
1915throw (Coro::State self, SV *throw = &PL_sv_undef)
1916 PROTOTYPE: $;$
1917 CODE:
1918 SvREFCNT_dec (self->throw);
1919 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1920
1921void
1922swap_defsv (Coro::State self) 2081swap_defsv (Coro::State self)
1923 PROTOTYPE: $ 2082 PROTOTYPE: $
1924 ALIAS: 2083 ALIAS:
1925 swap_defav = 1 2084 swap_defav = 1
1926 CODE: 2085 CODE:
1980void 2139void
1981_set_current (SV *current) 2140_set_current (SV *current)
1982 PROTOTYPE: $ 2141 PROTOTYPE: $
1983 CODE: 2142 CODE:
1984 SvREFCNT_dec (SvRV (coro_current)); 2143 SvREFCNT_dec (SvRV (coro_current));
1985 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2144 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
1986 2145
1987void 2146void
1988_set_readyhook (SV *hook) 2147_set_readyhook (SV *hook)
1989 PROTOTYPE: $ 2148 PROTOTYPE: $
1990 CODE: 2149 CODE:
2029 CODE: 2188 CODE:
2030 RETVAL = coro_nready; 2189 RETVAL = coro_nready;
2031 OUTPUT: 2190 OUTPUT:
2032 RETVAL 2191 RETVAL
2033 2192
2193void
2194throw (Coro::State self, SV *throw = &PL_sv_undef)
2195 PROTOTYPE: $;$
2196 CODE:
2197 SvREFCNT_dec (self->throw);
2198 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2199
2034# for async_pool speedup 2200# for async_pool speedup
2035void 2201void
2036_pool_1 (SV *cb) 2202_pool_1 (SV *cb)
2037 CODE: 2203 CODE:
2038{ 2204{
2050 SvREFCNT_dec (old); 2216 SvREFCNT_dec (old);
2051 croak ("\3async_pool terminate\2\n"); 2217 croak ("\3async_pool terminate\2\n");
2052 } 2218 }
2053 2219
2054 SvREFCNT_dec (coro->saved_deffh); 2220 SvREFCNT_dec (coro->saved_deffh);
2055 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2221 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2056 2222
2057 hv_store (hv, "desc", sizeof ("desc") - 1, 2223 hv_store (hv, "desc", sizeof ("desc") - 1,
2058 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2224 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2059 2225
2060 invoke_av = (AV *)SvRV (invoke); 2226 invoke_av = (AV *)SvRV (invoke);
2064 2230
2065 if (len > 0) 2231 if (len > 0)
2066 { 2232 {
2067 av_fill (defav, len - 1); 2233 av_fill (defav, len - 1);
2068 for (i = 0; i < len; ++i) 2234 for (i = 0; i < len; ++i)
2069 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2235 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2070 } 2236 }
2071 2237
2072 SvREFCNT_dec (invoke); 2238 SvREFCNT_dec (invoke);
2073} 2239}
2074 2240
2081 sv_setsv (cb, &PL_sv_undef); 2247 sv_setsv (cb, &PL_sv_undef);
2082 2248
2083 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2249 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2084 coro->saved_deffh = 0; 2250 coro->saved_deffh = 0;
2085 2251
2086 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2252 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2087 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2253 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2088 { 2254 {
2089 SV *old = PL_diehook; 2255 SV *old = PL_diehook;
2090 PL_diehook = 0; 2256 PL_diehook = 0;
2091 SvREFCNT_dec (old); 2257 SvREFCNT_dec (old);
2153#endif 2319#endif
2154 2320
2155 2321
2156MODULE = Coro::State PACKAGE = Coro::AIO 2322MODULE = Coro::State PACKAGE = Coro::AIO
2157 2323
2158SV * 2324void
2159_get_state () 2325_get_state (SV *self)
2160 CODE: 2326 PPCODE:
2161{ 2327{
2162 struct io_state *data; 2328 AV *defav = GvAV (PL_defgv);
2163 2329 AV *av = newAV ();
2330 int i;
2164 RETVAL = newSV (sizeof (struct io_state)); 2331 SV *data_sv = newSV (sizeof (struct io_state));
2165 data = (struct io_state *)SvPVX (RETVAL); 2332 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2166 SvCUR_set (RETVAL, sizeof (struct io_state)); 2333 SvCUR_set (data_sv, sizeof (struct io_state));
2167 SvPOK_only (RETVAL); 2334 SvPOK_only (data_sv);
2168 2335
2169 data->errorno = errno; 2336 data->errorno = errno;
2170 data->laststype = PL_laststype; 2337 data->laststype = PL_laststype;
2171 data->laststatval = PL_laststatval; 2338 data->laststatval = PL_laststatval;
2172 data->statcache = PL_statcache; 2339 data->statcache = PL_statcache;
2340
2341 av_extend (av, AvFILLp (defav) + 1 + 1);
2342
2343 for (i = 0; i <= AvFILLp (defav); ++i)
2344 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2345
2346 av_push (av, data_sv);
2347
2348 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2349
2350 api_ready (self);
2173} 2351}
2174 OUTPUT:
2175 RETVAL
2176 2352
2177void 2353void
2178_set_state (char *data_) 2354_set_state (SV *state)
2179 PROTOTYPE: $ 2355 PROTOTYPE: $
2180 CODE: 2356 PPCODE:
2181{ 2357{
2182 struct io_state *data = (void *)data_; 2358 AV *av = (AV *)SvRV (state);
2359 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2360 int i;
2183 2361
2184 errno = data->errorno; 2362 errno = data->errorno;
2185 PL_laststype = data->laststype; 2363 PL_laststype = data->laststype;
2186 PL_laststatval = data->laststatval; 2364 PL_laststatval = data->laststatval;
2187 PL_statcache = data->statcache; 2365 PL_statcache = data->statcache;
2366
2367 EXTEND (SP, AvFILLp (av));
2368 for (i = 0; i < AvFILLp (av); ++i)
2369 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2188} 2370}
2189 2371
2190 2372
2191MODULE = Coro::State PACKAGE = Coro::AnyEvent 2373MODULE = Coro::State PACKAGE = Coro::AnyEvent
2192 2374
2216 } 2398 }
2217 2399
2218 --incede; 2400 --incede;
2219} 2401}
2220 2402
2403
2404MODULE = Coro::State PACKAGE = PerlIO::cede
2405
2406BOOT:
2407 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines