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.142 by root, Tue Feb 13 19:21:29 2007 UTC vs.
Revision 1.196 by root, Sat Oct 6 01:11:01 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2
3#define PERL_NO_GET_CONTEXT
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
31#else 33#else
32# define PAGESIZE 0 34# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0 35# define BOOT_PAGESIZE (void)0
34#endif 36#endif
35 37
36#if USE_VALGRIND 38#if CORO_USE_VALGRIND
37# include <valgrind/valgrind.h> 39# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) 40# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else 41#else
40# define REGISTER_STACK(cctx,start,end) 42# define REGISTER_STACK(cctx,start,end)
41#endif 43#endif
73/* 5.8.7 */ 75/* 5.8.7 */
74#ifndef SvRV_set 76#ifndef SvRV_set
75# define SvRV_set(s,v) SvRV(s) = (v) 77# define SvRV_set(s,v) SvRV(s) = (v)
76#endif 78#endif
77 79
80/* 5.8.8 */
81#ifndef GV_NOTQUAL
82# define GV_NOTQUAL 0
83#endif
84#ifndef newSV
85# define newSV(l) NEWSV(0,l)
86#endif
87
78#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 88#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
79# undef STACKGUARD 89# undef CORO_STACKGUARD
80#endif 90#endif
81 91
82#ifndef STACKGUARD 92#ifndef CORO_STACKGUARD
83# define STACKGUARD 0 93# define CORO_STACKGUARD 0
84#endif 94#endif
85 95
86/* prefer perl internal functions over our own? */ 96/* prefer perl internal functions over our own? */
87#ifndef PREFER_PERL_FUNCTIONS 97#ifndef CORO_PREFER_PERL_FUNCTIONS
88# define PREFER_PERL_FUNCTIONS 0 98# define CORO_PREFER_PERL_FUNCTIONS 0
89#endif 99#endif
90 100
91/* The next macro should declare a variable stacklevel that contains and approximation 101/* The next macros try to return the current stack pointer, in an as
92 * to the current C stack pointer. Its property is that it changes with each call 102 * portable way as possible. */
93 * and should be unique. */
94#define dSTACKLEVEL int stacklevel 103#define dSTACKLEVEL volatile char stacklevel
95#define STACKLEVEL ((void *)&stacklevel) 104#define STACKLEVEL ((void *)&stacklevel)
96 105
97#define IN_DESTRUCT (PL_main_cv == Nullcv) 106#define IN_DESTRUCT (PL_main_cv == Nullcv)
98 107
99#if __GNUC__ >= 3 108#if __GNUC__ >= 3
100# define attribute(x) __attribute__(x) 109# define attribute(x) __attribute__(x)
101# define BARRIER __asm__ __volatile__ ("" : : : "memory") 110# define BARRIER __asm__ __volatile__ ("" : : : "memory")
111# define expect(expr,value) __builtin_expect ((expr),(value))
102#else 112#else
103# define attribute(x) 113# define attribute(x)
104# define BARRIER 114# define BARRIER
115# define expect(expr,value) (expr)
105#endif 116#endif
117
118#define expect_false(expr) expect ((expr) != 0, 0)
119#define expect_true(expr) expect ((expr) != 0, 1)
106 120
107#define NOINLINE attribute ((noinline)) 121#define NOINLINE attribute ((noinline))
108 122
109#include "CoroAPI.h" 123#include "CoroAPI.h"
110 124
124 I32 laststype; 138 I32 laststype;
125 int laststatval; 139 int laststatval;
126 Stat_t statcache; 140 Stat_t statcache;
127}; 141};
128 142
143static size_t coro_stacksize = CORO_STACKSIZE;
129static struct CoroAPI coroapi; 144static struct CoroAPI coroapi;
130static AV *main_mainstack; /* used to differentiate between $main and others */ 145static AV *main_mainstack; /* used to differentiate between $main and others */
146static JMPENV *main_top_env;
131static HV *coro_state_stash, *coro_stash; 147static HV *coro_state_stash, *coro_stash;
132static SV *coro_mortal; /* will be freed after next transfer */ 148static SV *coro_mortal; /* will be freed after next transfer */
133 149
150static GV *irsgv; /* $/ */
151static GV *stdoutgv; /* *STDOUT */
152
153/* async_pool helper stuff */
154static SV *sv_pool_rss;
155static SV *sv_pool_size;
156static AV *av_async_pool;
157
134static struct coro_cctx *cctx_first; 158static struct coro_cctx *cctx_first;
135static int cctx_count, cctx_idle; 159static int cctx_count, cctx_idle;
160
161enum {
162 CC_MAPPED = 0x01,
163 CC_NOREUSE = 0x02, /* throw this away after tracing */
164 CC_TRACE = 0x04,
165 CC_TRACE_SUB = 0x08, /* trace sub calls */
166 CC_TRACE_LINE = 0x10, /* trace each statement */
167 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
168};
136 169
137/* this is a structure representing a c-level coroutine */ 170/* this is a structure representing a c-level coroutine */
138typedef struct coro_cctx { 171typedef struct coro_cctx {
139 struct coro_cctx *next; 172 struct coro_cctx *next;
140 173
141 /* the stack */ 174 /* the stack */
142 void *sptr; 175 void *sptr;
143 ssize_t ssize; /* positive == mmap, otherwise malloc */ 176 size_t ssize;
144 177
145 /* cpu state */ 178 /* cpu state */
146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 179 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 180 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
148 JMPENV *top_env; 181 JMPENV *top_env;
149 coro_context cctx; 182 coro_context cctx;
150 183
151 int inuse;
152
153#if USE_VALGRIND 184#if CORO_USE_VALGRIND
154 int valgrind_id; 185 int valgrind_id;
155#endif 186#endif
187 unsigned char flags;
156} coro_cctx; 188} coro_cctx;
157 189
158enum { 190enum {
159 CF_RUNNING = 0x0001, /* coroutine is running */ 191 CF_RUNNING = 0x0001, /* coroutine is running */
160 CF_READY = 0x0002, /* coroutine is ready */ 192 CF_READY = 0x0002, /* coroutine is ready */
168 coro_cctx *cctx; 200 coro_cctx *cctx;
169 201
170 /* data associated with this coroutine (initial args) */ 202 /* data associated with this coroutine (initial args) */
171 AV *args; 203 AV *args;
172 int refcnt; 204 int refcnt;
173 int save; /* CORO_SAVE flags */
174 int flags; /* CF_ flags */ 205 int flags; /* CF_ flags */
175 206
176 /* optionally saved, might be zero */ 207 /* optionally saved, might be zero */
177 AV *defav; /* @_ */ 208 AV *defav; /* @_ */
178 SV *defsv; /* $_ */ 209 SV *defsv; /* $_ */
179 SV *errsv; /* $@ */ 210 SV *errsv; /* $@ */
211 SV *deffh; /* default filehandle */
180 SV *irssv; /* $/ */ 212 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */ 213 SV *irssv_sv; /* real $/ cache */
182 214
183#define VAR(name,type) type name; 215#define VAR(name,type) type name;
184# include "state.h" 216# include "state.h"
185#undef VAR 217#undef VAR
186 218
219 /* statistics */
220 int usecount; /* number of transfers to this coro */
221
187 /* coro process data */ 222 /* coro process data */
188 int prio; 223 int prio;
224 SV *throw;
225
226 /* async_pool */
227 SV *saved_deffh;
228
229 /* linked list */
230 struct coro *next, *prev;
231 HV *hv; /* the perl hash associated with this coro, if any */
189}; 232};
190 233
191typedef struct coro *Coro__State; 234typedef struct coro *Coro__State;
192typedef struct coro *Coro__State_or_hashref; 235typedef struct coro *Coro__State_or_hashref;
193 236
202 245
203/* for Coro.pm */ 246/* for Coro.pm */
204static SV *coro_current; 247static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 248static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready; 249static int coro_nready;
250static struct coro *coro_first;
207 251
208/** lowlevel stuff **********************************************************/ 252/** lowlevel stuff **********************************************************/
209 253
210static AV * 254static AV *
211coro_clone_padlist (CV *cv) 255coro_clone_padlist (pTHX_ CV *cv)
212{ 256{
213 AV *padlist = CvPADLIST (cv); 257 AV *padlist = CvPADLIST (cv);
214 AV *newpadlist, *newpad; 258 AV *newpadlist, *newpad;
215 259
216 newpadlist = newAV (); 260 newpadlist = newAV ();
228 272
229 return newpadlist; 273 return newpadlist;
230} 274}
231 275
232static void 276static void
233free_padlist (AV *padlist) 277free_padlist (pTHX_ AV *padlist)
234{ 278{
235 /* may be during global destruction */ 279 /* may be during global destruction */
236 if (SvREFCNT (padlist)) 280 if (SvREFCNT (padlist))
237 { 281 {
238 I32 i = AvFILLp (padlist); 282 I32 i = AvFILLp (padlist);
259 AV *padlist; 303 AV *padlist;
260 AV *av = (AV *)mg->mg_obj; 304 AV *av = (AV *)mg->mg_obj;
261 305
262 /* casting is fun. */ 306 /* casting is fun. */
263 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 307 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
264 free_padlist (padlist); 308 free_padlist (aTHX_ padlist);
265 309
266 SvREFCNT_dec (av); 310 SvREFCNT_dec (av);
267 311
268 return 0; 312 return 0;
269} 313}
277 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 321 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
278 ? SvMAGIC (cv) \ 322 ? SvMAGIC (cv) \
279 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 323 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
280 : 0 324 : 0
281 325
326static struct coro *
327SvSTATE_ (pTHX_ SV *coro)
328{
329 HV *stash;
330 MAGIC *mg;
331
332 if (SvROK (coro))
333 coro = SvRV (coro);
334
335 if (expect_false (SvTYPE (coro) != SVt_PVHV))
336 croak ("Coro::State object required");
337
338 stash = SvSTASH (coro);
339 if (expect_false (stash != coro_stash && stash != coro_state_stash))
340 {
341 /* very slow, but rare, check */
342 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
343 croak ("Coro::State object required");
344 }
345
346 mg = CORO_MAGIC (coro);
347 return (struct coro *)mg->mg_ptr;
348}
349
350#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
351
282/* the next two functions merely cache the padlists */ 352/* the next two functions merely cache the padlists */
283static void 353static void
284get_padlist (CV *cv) 354get_padlist (pTHX_ CV *cv)
285{ 355{
286 MAGIC *mg = CORO_MAGIC (cv); 356 MAGIC *mg = CORO_MAGIC (cv);
287 AV *av; 357 AV *av;
288 358
289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 359 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 360 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
291 else 361 else
292 { 362 {
293#if PREFER_PERL_FUNCTIONS 363#if CORO_PREFER_PERL_FUNCTIONS
294 /* this is probably cleaner, but also slower? */ 364 /* this is probably cleaner, but also slower? */
295 CV *cp = Perl_cv_clone (cv); 365 CV *cp = Perl_cv_clone (cv);
296 CvPADLIST (cv) = CvPADLIST (cp); 366 CvPADLIST (cv) = CvPADLIST (cp);
297 CvPADLIST (cp) = 0; 367 CvPADLIST (cp) = 0;
298 SvREFCNT_dec (cp); 368 SvREFCNT_dec (cp);
299#else 369#else
300 CvPADLIST (cv) = coro_clone_padlist (cv); 370 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
301#endif 371#endif
302 } 372 }
303} 373}
304 374
305static void 375static void
306put_padlist (CV *cv) 376put_padlist (pTHX_ CV *cv)
307{ 377{
308 MAGIC *mg = CORO_MAGIC (cv); 378 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av; 379 AV *av;
310 380
311 if (!mg) 381 if (expect_false (!mg))
312 { 382 {
313 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 383 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
314 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 384 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
315 mg->mg_virtual = &vtbl_coro; 385 mg->mg_virtual = &vtbl_coro;
316 mg->mg_obj = (SV *)newAV (); 386 mg->mg_obj = (SV *)newAV ();
317 } 387 }
318 388
319 av = (AV *)mg->mg_obj; 389 av = (AV *)mg->mg_obj;
320 390
321 if (AvFILLp (av) >= AvMAX (av)) 391 if (expect_false (AvFILLp (av) >= AvMAX (av)))
322 av_extend (av, AvMAX (av) + 1); 392 av_extend (av, AvMAX (av) + 1);
323 393
324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 394 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
325} 395}
326 396
327/** load & save, init *******************************************************/ 397/** load & save, init *******************************************************/
328 398
329#define SB do {
330#define SE } while (0)
331
332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
333
334static void 399static void
335load_perl (Coro__State c) 400load_perl (pTHX_ Coro__State c)
336{ 401{
337#define VAR(name,type) PL_ ## name = c->name; 402#define VAR(name,type) PL_ ## name = c->name;
338# include "state.h" 403# include "state.h"
339#undef VAR 404#undef VAR
340 405
341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 406 GvSV (PL_defgv) = c->defsv;
342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 407 GvAV (PL_defgv) = c->defav;
343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 408 GvSV (PL_errgv) = c->errsv;
344 if (c->irssv) 409 GvSV (irsgv) = c->irssv_sv;
345 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
347 SvREFCNT_dec (c->irssv);
348 else
349 {
350 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs);
353 }
354 }
355 410
356 { 411 {
357 dSP; 412 dSP;
358 CV *cv; 413 CV *cv;
359 414
360 /* now do the ugly restore mess */ 415 /* now do the ugly restore mess */
361 while ((cv = (CV *)POPs)) 416 while (expect_true (cv = (CV *)POPs))
362 { 417 {
363 put_padlist (cv); /* mark this padlist as available */ 418 put_padlist (aTHX_ cv); /* mark this padlist as available */
364 CvDEPTH (cv) = PTR2IV (POPs); 419 CvDEPTH (cv) = PTR2IV (POPs);
365 CvPADLIST (cv) = (AV *)POPs; 420 CvPADLIST (cv) = (AV *)POPs;
366 } 421 }
367 422
368 PUTBACK; 423 PUTBACK;
369 } 424 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D
371} 425}
372 426
373static void 427static void
374save_perl (Coro__State c) 428save_perl (pTHX_ Coro__State c)
375{ 429{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
377 { 430 {
378 dSP; 431 dSP;
379 I32 cxix = cxstack_ix; 432 I32 cxix = cxstack_ix;
380 PERL_CONTEXT *ccstk = cxstack; 433 PERL_CONTEXT *ccstk = cxstack;
381 PERL_SI *top_si = PL_curstackinfo; 434 PERL_SI *top_si = PL_curstackinfo;
383 /* 436 /*
384 * the worst thing you can imagine happens first - we have to save 437 * the worst thing you can imagine happens first - we have to save
385 * (and reinitialize) all cv's in the whole callchain :( 438 * (and reinitialize) all cv's in the whole callchain :(
386 */ 439 */
387 440
388 EXTEND (SP, 3 + 1);
389 PUSHs (Nullsv); 441 XPUSHs (Nullsv);
390 /* this loop was inspired by pp_caller */ 442 /* this loop was inspired by pp_caller */
391 for (;;) 443 for (;;)
392 { 444 {
393 while (cxix >= 0) 445 while (expect_true (cxix >= 0))
394 { 446 {
395 PERL_CONTEXT *cx = &ccstk[cxix--]; 447 PERL_CONTEXT *cx = &ccstk[cxix--];
396 448
397 if (CxTYPE (cx) == CXt_SUB) 449 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT))
398 { 450 {
399 CV *cv = cx->blk_sub.cv; 451 CV *cv = cx->blk_sub.cv;
400 452
401 if (CvDEPTH (cv)) 453 if (expect_true (CvDEPTH (cv)))
402 { 454 {
403 EXTEND (SP, 3); 455 EXTEND (SP, 3);
404 PUSHs ((SV *)CvPADLIST (cv)); 456 PUSHs ((SV *)CvPADLIST (cv));
405 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 457 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
406 PUSHs ((SV *)cv); 458 PUSHs ((SV *)cv);
407 459
408 CvDEPTH (cv) = 0; 460 CvDEPTH (cv) = 0;
409 get_padlist (cv); 461 get_padlist (aTHX_ cv);
410 } 462 }
411 } 463 }
412 } 464 }
413 465
414 if (top_si->si_type == PERLSI_MAIN) 466 if (expect_true (top_si->si_type == PERLSI_MAIN))
415 break; 467 break;
416 468
417 top_si = top_si->si_prev; 469 top_si = top_si->si_prev;
418 ccstk = top_si->si_cxstack; 470 ccstk = top_si->si_cxstack;
419 cxix = top_si->si_cxix; 471 cxix = top_si->si_cxix;
420 } 472 }
421 473
422 PUTBACK; 474 PUTBACK;
423 } 475 }
424 476
425 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 477 c->defav = GvAV (PL_defgv);
426 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 478 c->defsv = DEFSV;
427 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 479 c->errsv = ERRSV;
428 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 480 c->irssv_sv = GvSV (irsgv);
429 481
430#define VAR(name,type)c->name = PL_ ## name; 482#define VAR(name,type)c->name = PL_ ## name;
431# include "state.h" 483# include "state.h"
432#undef VAR 484#undef VAR
433} 485}
436 * allocate various perl stacks. This is an exact copy 488 * allocate various perl stacks. This is an exact copy
437 * of perl.c:init_stacks, except that it uses less memory 489 * of perl.c:init_stacks, except that it uses less memory
438 * on the (sometimes correct) assumption that coroutines do 490 * on the (sometimes correct) assumption that coroutines do
439 * not usually need a lot of stackspace. 491 * not usually need a lot of stackspace.
440 */ 492 */
441#if PREFER_PERL_FUNCTIONS 493#if CORO_PREFER_PERL_FUNCTIONS
442# define coro_init_stacks init_stacks 494# define coro_init_stacks init_stacks
443#else 495#else
444static void 496static void
445coro_init_stacks () 497coro_init_stacks (pTHX)
446{ 498{
447 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 499 PL_curstackinfo = new_stackinfo(64, 6);
448 PL_curstackinfo->si_type = PERLSI_MAIN; 500 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 501 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 502 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
451 503
452 PL_stack_base = AvARRAY(PL_curstack); 504 PL_stack_base = AvARRAY(PL_curstack);
453 PL_stack_sp = PL_stack_base; 505 PL_stack_sp = PL_stack_base;
454 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 506 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
455 507
456 New(50,PL_tmps_stack,128,SV*); 508 New(50,PL_tmps_stack,64,SV*);
457 PL_tmps_floor = -1; 509 PL_tmps_floor = -1;
458 PL_tmps_ix = -1; 510 PL_tmps_ix = -1;
459 PL_tmps_max = 128; 511 PL_tmps_max = 64;
460 512
461 New(54,PL_markstack,32,I32); 513 New(54,PL_markstack,16,I32);
462 PL_markstack_ptr = PL_markstack; 514 PL_markstack_ptr = PL_markstack;
463 PL_markstack_max = PL_markstack + 32; 515 PL_markstack_max = PL_markstack + 16;
464 516
465#ifdef SET_MARK_OFFSET 517#ifdef SET_MARK_OFFSET
466 SET_MARK_OFFSET; 518 SET_MARK_OFFSET;
467#endif 519#endif
468 520
469 New(54,PL_scopestack,32,I32); 521 New(54,PL_scopestack,16,I32);
470 PL_scopestack_ix = 0; 522 PL_scopestack_ix = 0;
471 PL_scopestack_max = 32; 523 PL_scopestack_max = 16;
472 524
473 New(54,PL_savestack,64,ANY); 525 New(54,PL_savestack,64,ANY);
474 PL_savestack_ix = 0; 526 PL_savestack_ix = 0;
475 PL_savestack_max = 64; 527 PL_savestack_max = 64;
476 528
477#if !PERL_VERSION_ATLEAST (5,9,0) 529#if !PERL_VERSION_ATLEAST (5,9,0)
478 New(54,PL_retstack,16,OP*); 530 New(54,PL_retstack,4,OP*);
479 PL_retstack_ix = 0; 531 PL_retstack_ix = 0;
480 PL_retstack_max = 16; 532 PL_retstack_max = 4;
481#endif 533#endif
482} 534}
483#endif 535#endif
484 536
485/* 537/*
486 * destroy the stacks, the callchain etc... 538 * destroy the stacks, the callchain etc...
487 */ 539 */
488static void 540static void
489coro_destroy_stacks () 541coro_destroy_stacks (pTHX)
490{ 542{
491 if (!IN_DESTRUCT)
492 {
493 /* restore all saved variables and stuff */
494 LEAVE_SCOPE (0);
495 assert (PL_tmps_floor == -1);
496
497 /* free all temporaries */
498 FREETMPS;
499 assert (PL_tmps_ix == -1);
500
501 /* unwind all extra stacks */
502 POPSTACK_TO (PL_mainstack);
503
504 /* unwind main stack */
505 dounwind (-1);
506 }
507
508 while (PL_curstackinfo->si_next) 543 while (PL_curstackinfo->si_next)
509 PL_curstackinfo = PL_curstackinfo->si_next; 544 PL_curstackinfo = PL_curstackinfo->si_next;
510 545
511 while (PL_curstackinfo) 546 while (PL_curstackinfo)
512 { 547 {
527#if !PERL_VERSION_ATLEAST (5,9,0) 562#if !PERL_VERSION_ATLEAST (5,9,0)
528 Safefree (PL_retstack); 563 Safefree (PL_retstack);
529#endif 564#endif
530} 565}
531 566
567static size_t
568coro_rss (pTHX_ struct coro *coro)
569{
570 size_t rss = sizeof (*coro);
571
572 if (coro->mainstack)
573 {
574 if (coro->flags & CF_RUNNING)
575 {
576 #define VAR(name,type)coro->name = PL_ ## name;
577 # include "state.h"
578 #undef VAR
579 }
580
581 rss += sizeof (coro->curstackinfo);
582 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
583 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
584 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
585 rss += coro->tmps_max * sizeof (SV *);
586 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
587 rss += coro->scopestack_max * sizeof (I32);
588 rss += coro->savestack_max * sizeof (ANY);
589
590#if !PERL_VERSION_ATLEAST (5,9,0)
591 rss += coro->retstack_max * sizeof (OP *);
592#endif
593 }
594
595 return rss;
596}
597
532/** coroutine stack handling ************************************************/ 598/** coroutine stack handling ************************************************/
533 599
534static void 600static void
535setup_coro (struct coro *coro) 601coro_setup (pTHX_ struct coro *coro)
536{ 602{
537 /* 603 /*
538 * emulate part of the perl startup here. 604 * emulate part of the perl startup here.
539 */ 605 */
540
541 coro_init_stacks (); 606 coro_init_stacks (aTHX);
542 607
608 PL_runops = RUNOPS_DEFAULT;
543 PL_curcop = &PL_compiling; 609 PL_curcop = &PL_compiling;
544 PL_in_eval = EVAL_NULL; 610 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0; 611 PL_comppad = 0;
546 PL_curpm = 0; 612 PL_curpm = 0;
613 PL_curpad = 0;
547 PL_localizing = 0; 614 PL_localizing = 0;
548 PL_dirty = 0; 615 PL_dirty = 0;
549 PL_restartop = 0; 616 PL_restartop = 0;
617
618 GvSV (PL_defgv) = newSV (0);
619 GvAV (PL_defgv) = coro->args; coro->args = 0;
620 GvSV (PL_errgv) = newSV (0);
621 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
622 PL_rs = newSVsv (GvSV (irsgv));
623 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv);
550 624
551 { 625 {
552 dSP; 626 dSP;
553 LOGOP myop; 627 LOGOP myop;
554 628
555 SvREFCNT_dec (GvAV (PL_defgv));
556 GvAV (PL_defgv) = coro->args; coro->args = 0;
557
558 Zero (&myop, 1, LOGOP); 629 Zero (&myop, 1, LOGOP);
559 myop.op_next = Nullop; 630 myop.op_next = Nullop;
560 myop.op_flags = OPf_WANT_VOID; 631 myop.op_flags = OPf_WANT_VOID;
561 632
562 PUSHMARK (SP); 633 PUSHMARK (SP);
563 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 634 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
564 PUTBACK; 635 PUTBACK;
565 PL_op = (OP *)&myop; 636 PL_op = (OP *)&myop;
566 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 637 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
567 SPAGAIN; 638 SPAGAIN;
568 } 639 }
569 640
570 ENTER; /* necessary e.g. for dounwind */ 641 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */
571} 642}
572 643
573static void 644static void
645coro_destroy (pTHX_ struct coro *coro)
646{
647 if (!IN_DESTRUCT)
648 {
649 /* restore all saved variables and stuff */
650 LEAVE_SCOPE (0);
651 assert (PL_tmps_floor == -1);
652
653 /* free all temporaries */
654 FREETMPS;
655 assert (PL_tmps_ix == -1);
656
657 /* unwind all extra stacks */
658 POPSTACK_TO (PL_mainstack);
659
660 /* unwind main stack */
661 dounwind (-1);
662 }
663
664 SvREFCNT_dec (GvSV (PL_defgv));
665 SvREFCNT_dec (GvAV (PL_defgv));
666 SvREFCNT_dec (GvSV (PL_errgv));
667 SvREFCNT_dec (PL_defoutgv);
668 SvREFCNT_dec (PL_rs);
669 SvREFCNT_dec (GvSV (irsgv));
670
671 SvREFCNT_dec (coro->saved_deffh);
672 SvREFCNT_dec (coro->throw);
673
674 coro_destroy_stacks (aTHX);
675}
676
677static void
574free_coro_mortal () 678free_coro_mortal (pTHX)
575{ 679{
576 if (coro_mortal) 680 if (expect_true (coro_mortal))
577 { 681 {
578 SvREFCNT_dec (coro_mortal); 682 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 683 coro_mortal = 0;
580 } 684 }
581} 685}
582 686
687static int
688runops_trace (pTHX)
689{
690 COP *oldcop = 0;
691 int oldcxix = -2;
692 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
693 coro_cctx *cctx = coro->cctx;
694
695 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
696 {
697 PERL_ASYNC_CHECK ();
698
699 if (cctx->flags & CC_TRACE_ALL)
700 {
701 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
702 {
703 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
704 SV **bot, **top;
705 AV *av = newAV (); /* return values */
706 SV **cb;
707 dSP;
708
709 GV *gv = CvGV (cx->blk_sub.cv);
710 SV *fullname = sv_2mortal (newSV (0));
711 if (isGV (gv))
712 gv_efullname3 (fullname, gv, 0);
713
714 bot = PL_stack_base + cx->blk_oldsp + 1;
715 top = cx->blk_gimme == G_ARRAY ? SP + 1
716 : cx->blk_gimme == G_SCALAR ? bot + 1
717 : bot;
718
719 av_extend (av, top - bot);
720 while (bot < top)
721 av_push (av, SvREFCNT_inc (*bot++));
722
723 PL_runops = RUNOPS_DEFAULT;
724 ENTER;
725 SAVETMPS;
726 EXTEND (SP, 3);
727 PUSHMARK (SP);
728 PUSHs (&PL_sv_no);
729 PUSHs (fullname);
730 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
731 PUTBACK;
732 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
733 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
734 SPAGAIN;
735 FREETMPS;
736 LEAVE;
737 PL_runops = runops_trace;
738 }
739
740 if (oldcop != PL_curcop)
741 {
742 oldcop = PL_curcop;
743
744 if (PL_curcop != &PL_compiling)
745 {
746 SV **cb;
747
748 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
749 {
750 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
751
752 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
753 {
754 runops_proc_t old_runops = PL_runops;
755 dSP;
756 GV *gv = CvGV (cx->blk_sub.cv);
757 SV *fullname = sv_2mortal (newSV (0));
758
759 if (isGV (gv))
760 gv_efullname3 (fullname, gv, 0);
761
762 PL_runops = RUNOPS_DEFAULT;
763 ENTER;
764 SAVETMPS;
765 EXTEND (SP, 3);
766 PUSHMARK (SP);
767 PUSHs (&PL_sv_yes);
768 PUSHs (fullname);
769 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
770 PUTBACK;
771 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
772 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
773 SPAGAIN;
774 FREETMPS;
775 LEAVE;
776 PL_runops = runops_trace;
777 }
778
779 oldcxix = cxstack_ix;
780 }
781
782 if (cctx->flags & CC_TRACE_LINE)
783 {
784 dSP;
785
786 PL_runops = RUNOPS_DEFAULT;
787 ENTER;
788 SAVETMPS;
789 EXTEND (SP, 3);
790 PL_runops = RUNOPS_DEFAULT;
791 PUSHMARK (SP);
792 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
793 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
794 PUTBACK;
795 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
796 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
797 SPAGAIN;
798 FREETMPS;
799 LEAVE;
800 PL_runops = runops_trace;
801 }
802 }
803 }
804 }
805 }
806
807 TAINT_NOT;
808 return 0;
809}
810
583/* inject a fake call to Coro::State::_cctx_init into the execution */ 811/* inject a fake call to Coro::State::_cctx_init into the execution */
812/* _cctx_init should be careful, as it could be called at almost any time */
813/* during execution of a perl program */
584static void NOINLINE 814static void NOINLINE
585prepare_cctx (coro_cctx *cctx) 815prepare_cctx (pTHX_ coro_cctx *cctx)
586{ 816{
587 dSP; 817 dSP;
588 LOGOP myop; 818 LOGOP myop;
819
820 PL_top_env = &PL_start_env;
821
822 if (cctx->flags & CC_TRACE)
823 PL_runops = runops_trace;
589 824
590 Zero (&myop, 1, LOGOP); 825 Zero (&myop, 1, LOGOP);
591 myop.op_next = PL_op; 826 myop.op_next = PL_op;
592 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 827 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
593 828
599 PL_op = (OP *)&myop; 834 PL_op = (OP *)&myop;
600 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 835 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
601 SPAGAIN; 836 SPAGAIN;
602} 837}
603 838
839/*
840 * this is a _very_ stripped down perl interpreter ;)
841 */
604static void 842static void
605coro_run (void *arg) 843coro_run (void *arg)
606{ 844{
845 dTHX;
846
607 /* coro_run is the alternative tail of transfer(), so unlock here. */ 847 /* coro_run is the alternative tail of transfer(), so unlock here. */
608 UNLOCK; 848 UNLOCK;
609 849
610 /* 850 /* we now skip the entersub that lead to transfer() */
611 * this is a _very_ stripped down perl interpreter ;) 851 PL_op = PL_op->op_next;
612 */
613 PL_top_env = &PL_start_env;
614 852
615 /* inject call to cctx_init */ 853 /* inject a fake subroutine call to cctx_init */
616 prepare_cctx ((coro_cctx *)arg); 854 prepare_cctx (aTHX_ (coro_cctx *)arg);
617 855
618 /* somebody will hit me for both perl_run and PL_restartop */ 856 /* somebody or something will hit me for both perl_run and PL_restartop */
619 PL_restartop = PL_op; 857 PL_restartop = PL_op;
620 perl_run (PL_curinterp); 858 perl_run (PL_curinterp);
621 859
622 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 860 /*
623 abort (); 861 * If perl-run returns we assume exit() was being called or the coro
862 * fell off the end, which seems to be the only valid (non-bug)
863 * reason for perl_run to return. We try to exit by jumping to the
864 * bootstrap-time "top" top_env, as we cannot restore the "main"
865 * coroutine as Coro has no such concept
866 */
867 PL_top_env = main_top_env;
868 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
624} 869}
625 870
626static coro_cctx * 871static coro_cctx *
627cctx_new () 872cctx_new ()
628{ 873{
629 coro_cctx *cctx; 874 coro_cctx *cctx;
875 void *stack_start;
876 size_t stack_size;
630 877
631 ++cctx_count; 878 ++cctx_count;
632 879
633 Newz (0, cctx, 1, coro_cctx); 880 Newz (0, cctx, 1, coro_cctx);
634 881
635#if HAVE_MMAP 882#if HAVE_MMAP
636 883
637 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 884 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
638 /* mmap supposedly does allocate-on-write for us */ 885 /* mmap supposedly does allocate-on-write for us */
639 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 886 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
640 887
641 if (cctx->sptr != (void *)-1) 888 if (cctx->sptr != (void *)-1)
642 { 889 {
643# if STACKGUARD 890# if CORO_STACKGUARD
644 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 891 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
645# endif 892# endif
646 REGISTER_STACK (
647 cctx,
648 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 893 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
649 cctx->ssize + (char *)cctx->sptr 894 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
650 ); 895 cctx->flags |= CC_MAPPED;
651
652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 } 896 }
654 else 897 else
655#endif 898#endif
656 { 899 {
657 cctx->ssize = -STACKSIZE * (long)sizeof (long); 900 cctx->ssize = coro_stacksize * (long)sizeof (long);
658 New (0, cctx->sptr, STACKSIZE, long); 901 New (0, cctx->sptr, coro_stacksize, long);
659 902
660 if (!cctx->sptr) 903 if (!cctx->sptr)
661 { 904 {
662 perror ("FATAL: unable to allocate stack for coroutine"); 905 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE); 906 _exit (EXIT_FAILURE);
664 } 907 }
665 908
666 REGISTER_STACK ( 909 stack_start = cctx->sptr;
667 cctx, 910 stack_size = cctx->ssize;
668 (char *)cctx->sptr, 911 }
669 (char *)cctx->sptr - cctx->ssize
670 );
671 912
913 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
672 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); 914 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size);
673 }
674 915
675 return cctx; 916 return cctx;
676} 917}
677 918
678static void 919static void
681 if (!cctx) 922 if (!cctx)
682 return; 923 return;
683 924
684 --cctx_count; 925 --cctx_count;
685 926
686#if USE_VALGRIND 927#if CORO_USE_VALGRIND
687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 928 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
688#endif 929#endif
689 930
690#if HAVE_MMAP 931#if HAVE_MMAP
691 if (cctx->ssize > 0) 932 if (cctx->flags & CC_MAPPED)
692 munmap (cctx->sptr, cctx->ssize); 933 munmap (cctx->sptr, cctx->ssize);
693 else 934 else
694#endif 935#endif
695 Safefree (cctx->sptr); 936 Safefree (cctx->sptr);
696 937
697 Safefree (cctx); 938 Safefree (cctx);
698} 939}
699 940
941/* wether this cctx should be destructed */
942#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
943
700static coro_cctx * 944static coro_cctx *
701cctx_get () 945cctx_get (pTHX)
702{ 946{
703 coro_cctx *cctx; 947 while (expect_true (cctx_first))
704
705 if (cctx_first)
706 { 948 {
707 cctx = cctx_first; 949 coro_cctx *cctx = cctx_first;
708 cctx_first = cctx->next; 950 cctx_first = cctx->next;
709 --cctx_idle; 951 --cctx_idle;
710 } 952
711 else 953 if (expect_true (!CCTX_EXPIRED (cctx)))
712 { 954 return cctx;
713 cctx = cctx_new (); 955
714 PL_op = PL_op->op_next; 956 cctx_destroy (cctx);
715 } 957 }
716 958
717 return cctx; 959 return cctx_new ();
718} 960}
719 961
720static void 962static void
721cctx_put (coro_cctx *cctx) 963cctx_put (coro_cctx *cctx)
722{ 964{
723 /* free another cctx if overlimit */ 965 /* free another cctx if overlimit */
724 if (cctx_idle >= MAX_IDLE_CCTX) 966 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
725 { 967 {
726 coro_cctx *first = cctx_first; 968 coro_cctx *first = cctx_first;
727 cctx_first = first->next; 969 cctx_first = first->next;
728 --cctx_idle; 970 --cctx_idle;
729 971
730 assert (!first->inuse);
731 cctx_destroy (first); 972 cctx_destroy (first);
732 } 973 }
733 974
734 ++cctx_idle; 975 ++cctx_idle;
735 cctx->next = cctx_first; 976 cctx->next = cctx_first;
736 cctx_first = cctx; 977 cctx_first = cctx;
737} 978}
738 979
739/** coroutine switching *****************************************************/ 980/** coroutine switching *****************************************************/
740 981
741/* never call directly, always through the coro_state_transfer global variable */ 982static void
983transfer_check (pTHX_ struct coro *prev, struct coro *next)
984{
985 if (expect_true (prev != next))
986 {
987 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
988 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
989
990 if (expect_false (next->flags & CF_RUNNING))
991 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
992
993 if (expect_false (next->flags & CF_DESTROYED))
994 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
995
996 if (expect_false (PL_lex_state != LEX_NOTPARSING))
997 croak ("Coro::State::transfer called while parsing, but this is not supported");
998 }
999}
1000
1001/* always use the TRANSFER macro */
742static void NOINLINE 1002static void NOINLINE
743transfer (struct coro *prev, struct coro *next) 1003transfer (pTHX_ struct coro *prev, struct coro *next)
744{ 1004{
745 dSTACKLEVEL; 1005 dSTACKLEVEL;
746 1006
747 /* sometimes transfer is only called to set idle_sp */ 1007 /* sometimes transfer is only called to set idle_sp */
748 if (!next) 1008 if (expect_false (!next))
749 { 1009 {
750 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1010 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
751 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1011 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
752 } 1012 }
753 else if (prev != next) 1013 else if (expect_true (prev != next))
754 { 1014 {
755 coro_cctx *prev__cctx; 1015 coro_cctx *prev__cctx;
756 1016
757 if (prev->flags & CF_NEW) 1017 if (expect_false (prev->flags & CF_NEW))
758 { 1018 {
759 /* create a new empty context */ 1019 /* create a new empty context */
760 Newz (0, prev->cctx, 1, coro_cctx); 1020 Newz (0, prev->cctx, 1, coro_cctx);
761 prev->cctx->inuse = 1;
762 prev->flags &= ~CF_NEW; 1021 prev->flags &= ~CF_NEW;
763 prev->flags |= CF_RUNNING; 1022 prev->flags |= CF_RUNNING;
764 } 1023 }
765 1024
766 /*TODO: must not croak here */
767 if (!prev->flags & CF_RUNNING)
768 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
769
770 if (next->flags & CF_RUNNING)
771 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
772
773 if (next->flags & CF_DESTROYED)
774 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
775
776 prev->flags &= ~CF_RUNNING; 1025 prev->flags &= ~CF_RUNNING;
777 next->flags |= CF_RUNNING; 1026 next->flags |= CF_RUNNING;
778 1027
779 LOCK; 1028 LOCK;
780 1029
781 if (next->flags & CF_NEW) 1030 if (expect_false (next->flags & CF_NEW))
782 { 1031 {
783 /* need to start coroutine */ 1032 /* need to start coroutine */
784 next->flags &= ~CF_NEW; 1033 next->flags &= ~CF_NEW;
785 /* first get rid of the old state */ 1034 /* first get rid of the old state */
786 save_perl (prev); 1035 save_perl (aTHX_ prev);
787 /* setup coroutine call */ 1036 /* setup coroutine call */
788 setup_coro (next); 1037 coro_setup (aTHX_ next);
789 /* need a new stack */
790 assert (!next->cctx);
791 } 1038 }
792 else 1039 else
793 { 1040 {
794 /* coroutine already started */ 1041 /* coroutine already started */
795 save_perl (prev); 1042 save_perl (aTHX_ prev);
796 load_perl (next); 1043 load_perl (aTHX_ next);
797 } 1044 }
798 1045
799 prev__cctx = prev->cctx; 1046 prev__cctx = prev->cctx;
800 1047
801 /* possibly "free" the cctx */ 1048 /* possibly "free" the cctx */
802 if (prev__cctx->idle_sp == STACKLEVEL) 1049 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
803 { 1050 {
804 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1051 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
805 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1052 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
806 1053
807 prev->cctx = 0; 1054 prev->cctx = 0;
808 1055
1056 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1057 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1058 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1059 next->cctx = cctx_get (aTHX);
1060
809 cctx_put (prev__cctx); 1061 cctx_put (prev__cctx);
810 prev__cctx->inuse = 0;
811 } 1062 }
812 1063
1064 ++next->usecount;
1065
813 if (!next->cctx) 1066 if (expect_true (!next->cctx))
814 {
815 next->cctx = cctx_get (); 1067 next->cctx = cctx_get (aTHX);
816 assert (!next->cctx->inuse);
817 next->cctx->inuse = 1;
818 }
819 1068
820 if (prev__cctx != next->cctx) 1069 if (expect_false (prev__cctx != next->cctx))
821 { 1070 {
822 prev__cctx->top_env = PL_top_env; 1071 prev__cctx->top_env = PL_top_env;
823 PL_top_env = next->cctx->top_env; 1072 PL_top_env = next->cctx->top_env;
824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1073 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
825 } 1074 }
826 1075
827 free_coro_mortal (); 1076 free_coro_mortal (aTHX);
828 UNLOCK; 1077 UNLOCK;
1078
1079 if (expect_false (prev->throw || next->throw))
1080 {
1081 struct coro *coro = SvSTATE (coro_current);
1082
1083 if (coro->throw)
1084 {
1085 SV *exception = coro->throw;
1086 coro->throw = 0;
1087 sv_setsv (ERRSV, exception);
1088 croak (0);
1089 }
1090 }
829 } 1091 }
830} 1092}
831 1093
832struct transfer_args 1094struct transfer_args
833{ 1095{
834 struct coro *prev, *next; 1096 struct coro *prev, *next;
835}; 1097};
836 1098
837#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 1099#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1100#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
838 1101
839/** high level stuff ********************************************************/ 1102/** high level stuff ********************************************************/
840 1103
841static int 1104static int
842coro_state_destroy (struct coro *coro) 1105coro_state_destroy (pTHX_ struct coro *coro)
843{ 1106{
844 if (coro->flags & CF_DESTROYED) 1107 if (coro->flags & CF_DESTROYED)
845 return 0; 1108 return 0;
846 1109
847 coro->flags |= CF_DESTROYED; 1110 coro->flags |= CF_DESTROYED;
859 1122
860 if (coro->mainstack && coro->mainstack != main_mainstack) 1123 if (coro->mainstack && coro->mainstack != main_mainstack)
861 { 1124 {
862 struct coro temp; 1125 struct coro temp;
863 1126
864 assert (!(coro->flags & CF_RUNNING));
865
866 Zero (&temp, 1, struct coro);
867 temp.save = CORO_SAVE_ALL;
868
869 if (coro->flags & CF_RUNNING) 1127 if (coro->flags & CF_RUNNING)
870 croak ("FATAL: tried to destroy currently running coroutine"); 1128 croak ("FATAL: tried to destroy currently running coroutine");
871 1129
872 save_perl (&temp); 1130 save_perl (aTHX_ &temp);
873 load_perl (coro); 1131 load_perl (aTHX_ coro);
874 1132
875 coro_destroy_stacks (); 1133 coro_destroy (aTHX_ coro);
876 1134
877 load_perl (&temp); /* this will get rid of defsv etc.. */ 1135 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
878 1136
879 coro->mainstack = 0; 1137 coro->mainstack = 0;
880 } 1138 }
881 1139
882 cctx_destroy (coro->cctx); 1140 cctx_destroy (coro->cctx);
883 SvREFCNT_dec (coro->args); 1141 SvREFCNT_dec (coro->args);
884 1142
1143 if (coro->next) coro->next->prev = coro->prev;
1144 if (coro->prev) coro->prev->next = coro->next;
1145 if (coro == coro_first) coro_first = coro->next;
1146
885 return 1; 1147 return 1;
886} 1148}
887 1149
888static int 1150static int
889coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1151coro_state_free (pTHX_ SV *sv, MAGIC *mg)
890{ 1152{
891 struct coro *coro = (struct coro *)mg->mg_ptr; 1153 struct coro *coro = (struct coro *)mg->mg_ptr;
892 mg->mg_ptr = 0; 1154 mg->mg_ptr = 0;
893 1155
1156 coro->hv = 0;
1157
894 if (--coro->refcnt < 0) 1158 if (--coro->refcnt < 0)
895 { 1159 {
896 coro_state_destroy (coro); 1160 coro_state_destroy (aTHX_ coro);
897 Safefree (coro); 1161 Safefree (coro);
898 } 1162 }
899 1163
900 return 0; 1164 return 0;
901} 1165}
919#else 1183#else
920# define MGf_DUP 0 1184# define MGf_DUP 0
921#endif 1185#endif
922}; 1186};
923 1187
924static struct coro *
925SvSTATE (SV *coro)
926{
927 HV *stash;
928 MAGIC *mg;
929
930 if (SvROK (coro))
931 coro = SvRV (coro);
932
933 stash = SvSTASH (coro);
934 if (stash != coro_stash && stash != coro_state_stash)
935 {
936 /* very slow, but rare, check */
937 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
938 croak ("Coro::State object required");
939 }
940
941 mg = SvMAGIC (coro);
942 assert (mg->mg_type == PERL_MAGIC_ext);
943 return (struct coro *)mg->mg_ptr;
944}
945
946static void 1188static void
947prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1189prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
948{ 1190{
949 ta->prev = SvSTATE (prev_sv); 1191 ta->prev = SvSTATE (prev_sv);
950 ta->next = SvSTATE (next_sv); 1192 ta->next = SvSTATE (next_sv);
1193 TRANSFER_CHECK (*ta);
951} 1194}
952 1195
953static void 1196static void
954api_transfer (SV *prev_sv, SV *next_sv) 1197api_transfer (SV *prev_sv, SV *next_sv)
955{ 1198{
1199 dTHX;
956 struct transfer_args ta; 1200 struct transfer_args ta;
957 1201
958 prepare_transfer (&ta, prev_sv, next_sv); 1202 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
959 TRANSFER (ta); 1203 TRANSFER (ta);
960} 1204}
961 1205
962static int
963api_save (SV *coro_sv, int new_save)
964{
965 struct coro *coro = SvSTATE (coro_sv);
966 int old_save = coro->save;
967
968 if (new_save >= 0)
969 coro->save = new_save;
970
971 return old_save;
972}
973
974/** Coro ********************************************************************/ 1206/** Coro ********************************************************************/
975 1207
976static void 1208static void
977coro_enq (SV *coro_sv) 1209coro_enq (pTHX_ SV *coro_sv)
978{ 1210{
979 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1211 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
980} 1212}
981 1213
982static SV * 1214static SV *
983coro_deq (int min_prio) 1215coro_deq (pTHX_ int min_prio)
984{ 1216{
985 int prio = PRIO_MAX - PRIO_MIN; 1217 int prio = PRIO_MAX - PRIO_MIN;
986 1218
987 min_prio -= PRIO_MIN; 1219 min_prio -= PRIO_MIN;
988 if (min_prio < 0) 1220 if (min_prio < 0)
996} 1228}
997 1229
998static int 1230static int
999api_ready (SV *coro_sv) 1231api_ready (SV *coro_sv)
1000{ 1232{
1233 dTHX;
1001 struct coro *coro; 1234 struct coro *coro;
1002 1235
1003 if (SvROK (coro_sv)) 1236 if (SvROK (coro_sv))
1004 coro_sv = SvRV (coro_sv); 1237 coro_sv = SvRV (coro_sv);
1005 1238
1009 return 0; 1242 return 0;
1010 1243
1011 coro->flags |= CF_READY; 1244 coro->flags |= CF_READY;
1012 1245
1013 LOCK; 1246 LOCK;
1014 coro_enq (SvREFCNT_inc (coro_sv)); 1247 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1015 ++coro_nready; 1248 ++coro_nready;
1016 UNLOCK; 1249 UNLOCK;
1017 1250
1018 return 1; 1251 return 1;
1019} 1252}
1020 1253
1021static int 1254static int
1022api_is_ready (SV *coro_sv) 1255api_is_ready (SV *coro_sv)
1023{ 1256{
1257 dTHX;
1024 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1258 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1025} 1259}
1026 1260
1027static void 1261static void
1028prepare_schedule (struct transfer_args *ta) 1262prepare_schedule (pTHX_ struct transfer_args *ta)
1029{ 1263{
1030 SV *prev_sv, *next_sv; 1264 SV *prev_sv, *next_sv;
1031 1265
1032 for (;;) 1266 for (;;)
1033 { 1267 {
1034 LOCK; 1268 LOCK;
1035 next_sv = coro_deq (PRIO_MIN); 1269 next_sv = coro_deq (aTHX_ PRIO_MIN);
1036 1270
1037 /* nothing to schedule: call the idle handler */ 1271 /* nothing to schedule: call the idle handler */
1038 if (!next_sv) 1272 if (expect_false (!next_sv))
1039 { 1273 {
1040 dSP; 1274 dSP;
1041 UNLOCK; 1275 UNLOCK;
1042 1276
1043 ENTER; 1277 ENTER;
1053 } 1287 }
1054 1288
1055 ta->next = SvSTATE (next_sv); 1289 ta->next = SvSTATE (next_sv);
1056 1290
1057 /* cannot transfer to destroyed coros, skip and look for next */ 1291 /* cannot transfer to destroyed coros, skip and look for next */
1058 if (ta->next->flags & CF_DESTROYED) 1292 if (expect_false (ta->next->flags & CF_DESTROYED))
1059 { 1293 {
1060 UNLOCK; 1294 UNLOCK;
1061 SvREFCNT_dec (next_sv); 1295 SvREFCNT_dec (next_sv);
1062 /* coro_nready is already taken care of by destroy */ 1296 /* coro_nready is already taken care of by destroy */
1063 continue; 1297 continue;
1068 break; 1302 break;
1069 } 1303 }
1070 1304
1071 /* free this only after the transfer */ 1305 /* free this only after the transfer */
1072 prev_sv = SvRV (coro_current); 1306 prev_sv = SvRV (coro_current);
1073 SvRV_set (coro_current, next_sv);
1074 ta->prev = SvSTATE (prev_sv); 1307 ta->prev = SvSTATE (prev_sv);
1075 1308 TRANSFER_CHECK (*ta);
1076 assert (ta->next->flags & CF_READY); 1309 assert (ta->next->flags & CF_READY);
1077 ta->next->flags &= ~CF_READY; 1310 ta->next->flags &= ~CF_READY;
1311 SvRV_set (coro_current, next_sv);
1078 1312
1079 LOCK; 1313 LOCK;
1080 free_coro_mortal (); 1314 free_coro_mortal (aTHX);
1081 coro_mortal = prev_sv; 1315 coro_mortal = prev_sv;
1082 UNLOCK; 1316 UNLOCK;
1083} 1317}
1084 1318
1085static void 1319static void
1086prepare_cede (struct transfer_args *ta) 1320prepare_cede (pTHX_ struct transfer_args *ta)
1087{ 1321{
1088 api_ready (coro_current); 1322 api_ready (coro_current);
1089 prepare_schedule (ta); 1323 prepare_schedule (aTHX_ ta);
1090} 1324}
1091 1325
1092static int 1326static int
1093prepare_cede_notself (struct transfer_args *ta) 1327prepare_cede_notself (pTHX_ struct transfer_args *ta)
1094{ 1328{
1095 if (coro_nready) 1329 if (coro_nready)
1096 { 1330 {
1097 SV *prev = SvRV (coro_current); 1331 SV *prev = SvRV (coro_current);
1098 prepare_schedule (ta); 1332 prepare_schedule (aTHX_ ta);
1099 api_ready (prev); 1333 api_ready (prev);
1100 return 1; 1334 return 1;
1101 } 1335 }
1102 else 1336 else
1103 return 0; 1337 return 0;
1104} 1338}
1105 1339
1106static void 1340static void
1107api_schedule (void) 1341api_schedule (void)
1108{ 1342{
1343 dTHX;
1109 struct transfer_args ta; 1344 struct transfer_args ta;
1110 1345
1111 prepare_schedule (&ta); 1346 prepare_schedule (aTHX_ &ta);
1112 TRANSFER (ta); 1347 TRANSFER (ta);
1113} 1348}
1114 1349
1115static int 1350static int
1116api_cede (void) 1351api_cede (void)
1117{ 1352{
1353 dTHX;
1118 struct transfer_args ta; 1354 struct transfer_args ta;
1119 1355
1120 prepare_cede (&ta); 1356 prepare_cede (aTHX_ &ta);
1121 1357
1122 if (ta.prev != ta.next) 1358 if (expect_true (ta.prev != ta.next))
1123 { 1359 {
1124 TRANSFER (ta); 1360 TRANSFER (ta);
1125 return 1; 1361 return 1;
1126 } 1362 }
1127 else 1363 else
1129} 1365}
1130 1366
1131static int 1367static int
1132api_cede_notself (void) 1368api_cede_notself (void)
1133{ 1369{
1370 dTHX;
1134 struct transfer_args ta; 1371 struct transfer_args ta;
1135 1372
1136 if (prepare_cede_notself (&ta)) 1373 if (prepare_cede_notself (aTHX_ &ta))
1137 { 1374 {
1138 TRANSFER (ta); 1375 TRANSFER (ta);
1139 return 1; 1376 return 1;
1140 } 1377 }
1141 else 1378 else
1142 return 0; 1379 return 0;
1143} 1380}
1144 1381
1382static void
1383api_trace (SV *coro_sv, int flags)
1384{
1385 dTHX;
1386 struct coro *coro = SvSTATE (coro_sv);
1387
1388 if (flags & CC_TRACE)
1389 {
1390 if (!coro->cctx)
1391 coro->cctx = cctx_new ();
1392 else if (!(coro->cctx->flags & CC_TRACE))
1393 croak ("cannot enable tracing on coroutine with custom stack");
1394
1395 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1396 }
1397 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1398 {
1399 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1400
1401 if (coro->flags & CF_RUNNING)
1402 PL_runops = RUNOPS_DEFAULT;
1403 else
1404 coro->runops = RUNOPS_DEFAULT;
1405 }
1406}
1407
1145MODULE = Coro::State PACKAGE = Coro::State 1408MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1146 1409
1147PROTOTYPES: DISABLE 1410PROTOTYPES: DISABLE
1148 1411
1149BOOT: 1412BOOT:
1150{ 1413{
1151#ifdef USE_ITHREADS 1414#ifdef USE_ITHREADS
1152 MUTEX_INIT (&coro_mutex); 1415 MUTEX_INIT (&coro_mutex);
1153#endif 1416#endif
1154 BOOT_PAGESIZE; 1417 BOOT_PAGESIZE;
1155 1418
1419 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1420 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1421
1156 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1422 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1157 1423
1424 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1425 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1158 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1426 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1159 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1160 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1161 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1162 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1427 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1163 1428
1164 main_mainstack = PL_mainstack; 1429 main_mainstack = PL_mainstack;
1430 main_top_env = PL_top_env;
1431
1432 while (main_top_env->je_prev)
1433 main_top_env = main_top_env->je_prev;
1165 1434
1166 coroapi.ver = CORO_API_VERSION; 1435 coroapi.ver = CORO_API_VERSION;
1167 coroapi.transfer = api_transfer; 1436 coroapi.transfer = api_transfer;
1168 1437
1169 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1438 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1176 struct coro *coro; 1445 struct coro *coro;
1177 HV *hv; 1446 HV *hv;
1178 int i; 1447 int i;
1179 1448
1180 Newz (0, coro, 1, struct coro); 1449 Newz (0, coro, 1, struct coro);
1181 coro->args = newAV (); 1450 coro->args = newAV ();
1182 coro->save = CORO_SAVE_ALL;
1183 coro->flags = CF_NEW; 1451 coro->flags = CF_NEW;
1184 1452
1453 if (coro_first) coro_first->prev = coro;
1454 coro->next = coro_first;
1455 coro_first = coro;
1456
1185 hv = newHV (); 1457 coro->hv = hv = newHV ();
1186 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1458 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1187 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1459 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1188 1460
1461 av_extend (coro->args, items - 1);
1189 for (i = 1; i < items; i++) 1462 for (i = 1; i < items; i++)
1190 av_push (coro->args, newSVsv (ST (i))); 1463 av_push (coro->args, newSVsv (ST (i)));
1191} 1464}
1192 OUTPUT: 1465 OUTPUT:
1193 RETVAL 1466 RETVAL
1194 1467
1195int 1468# these not obviously related functions are all rolled into the same xs
1196save (SV *coro, int new_save = -1) 1469# function to increase chances that they all will call transfer with the same
1197 CODE: 1470# stack offset
1198 RETVAL = api_save (coro, new_save);
1199 OUTPUT:
1200 RETVAL
1201
1202void 1471void
1203_set_stacklevel (...) 1472_set_stacklevel (...)
1204 ALIAS: 1473 ALIAS:
1205 Coro::State::transfer = 1 1474 Coro::State::transfer = 1
1206 Coro::schedule = 2 1475 Coro::schedule = 2
1219 1488
1220 case 1: 1489 case 1:
1221 if (items != 2) 1490 if (items != 2)
1222 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1491 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1223 1492
1224 prepare_transfer (&ta, ST (0), ST (1)); 1493 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1225 break; 1494 break;
1226 1495
1227 case 2: 1496 case 2:
1228 prepare_schedule (&ta); 1497 prepare_schedule (aTHX_ &ta);
1229 break; 1498 break;
1230 1499
1231 case 3: 1500 case 3:
1232 prepare_cede (&ta); 1501 prepare_cede (aTHX_ &ta);
1233 break; 1502 break;
1234 1503
1235 case 4: 1504 case 4:
1236 if (!prepare_cede_notself (&ta)) 1505 if (!prepare_cede_notself (aTHX_ &ta))
1237 XSRETURN_EMPTY; 1506 XSRETURN_EMPTY;
1238 1507
1239 break; 1508 break;
1240 } 1509 }
1241 1510
1242 BARRIER; 1511 BARRIER;
1243 TRANSFER (ta); 1512 TRANSFER (ta);
1244 1513
1245 if (GIMME_V != G_VOID && ta.next != ta.prev) 1514 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1246 XSRETURN_YES; 1515 XSRETURN_YES;
1247} 1516}
1248 1517
1249bool 1518bool
1250_destroy (SV *coro_sv) 1519_destroy (SV *coro_sv)
1251 CODE: 1520 CODE:
1252 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1521 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1253 OUTPUT: 1522 OUTPUT:
1254 RETVAL 1523 RETVAL
1255 1524
1256void 1525void
1257_exit (code) 1526_exit (code)
1259 PROTOTYPE: $ 1528 PROTOTYPE: $
1260 CODE: 1529 CODE:
1261 _exit (code); 1530 _exit (code);
1262 1531
1263int 1532int
1533cctx_stacksize (int new_stacksize = 0)
1534 CODE:
1535 RETVAL = coro_stacksize;
1536 if (new_stacksize)
1537 coro_stacksize = new_stacksize;
1538 OUTPUT:
1539 RETVAL
1540
1541int
1264cctx_count () 1542cctx_count ()
1265 CODE: 1543 CODE:
1266 RETVAL = cctx_count; 1544 RETVAL = cctx_count;
1267 OUTPUT: 1545 OUTPUT:
1268 RETVAL 1546 RETVAL
1272 CODE: 1550 CODE:
1273 RETVAL = cctx_idle; 1551 RETVAL = cctx_idle;
1274 OUTPUT: 1552 OUTPUT:
1275 RETVAL 1553 RETVAL
1276 1554
1555void
1556list ()
1557 PPCODE:
1558{
1559 struct coro *coro;
1560 for (coro = coro_first; coro; coro = coro->next)
1561 if (coro->hv)
1562 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1563}
1564
1565void
1566call (Coro::State coro, SV *coderef)
1567 ALIAS:
1568 eval = 1
1569 CODE:
1570{
1571 if (coro->mainstack)
1572 {
1573 struct coro temp;
1574 Zero (&temp, 1, struct coro);
1575
1576 if (!(coro->flags & CF_RUNNING))
1577 {
1578 save_perl (aTHX_ &temp);
1579 load_perl (aTHX_ coro);
1580 }
1581
1582 {
1583 dSP;
1584 ENTER;
1585 SAVETMPS;
1586 PUSHMARK (SP);
1587 PUTBACK;
1588 if (ix)
1589 eval_sv (coderef, 0);
1590 else
1591 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1592 SPAGAIN;
1593 FREETMPS;
1594 LEAVE;
1595 PUTBACK;
1596 }
1597
1598 if (!(coro->flags & CF_RUNNING))
1599 {
1600 save_perl (aTHX_ coro);
1601 load_perl (aTHX_ &temp);
1602 }
1603 }
1604}
1605
1606SV *
1607is_ready (Coro::State coro)
1608 PROTOTYPE: $
1609 ALIAS:
1610 is_ready = CF_READY
1611 is_running = CF_RUNNING
1612 is_new = CF_NEW
1613 is_destroyed = CF_DESTROYED
1614 CODE:
1615 RETVAL = boolSV (coro->flags & ix);
1616 OUTPUT:
1617 RETVAL
1618
1619void
1620api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1621
1622SV *
1623has_stack (Coro::State coro)
1624 PROTOTYPE: $
1625 CODE:
1626 RETVAL = boolSV (!!coro->cctx);
1627 OUTPUT:
1628 RETVAL
1629
1630int
1631is_traced (Coro::State coro)
1632 PROTOTYPE: $
1633 CODE:
1634 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1635 OUTPUT:
1636 RETVAL
1637
1638IV
1639rss (Coro::State coro)
1640 PROTOTYPE: $
1641 ALIAS:
1642 usecount = 1
1643 CODE:
1644 switch (ix)
1645 {
1646 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1647 case 1: RETVAL = coro->usecount; break;
1648 }
1649 OUTPUT:
1650 RETVAL
1651
1652
1277MODULE = Coro::State PACKAGE = Coro 1653MODULE = Coro::State PACKAGE = Coro
1278 1654
1279BOOT: 1655BOOT:
1280{ 1656{
1281 int i; 1657 int i;
1658
1659 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1660 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1661 av_async_pool = get_av ("Coro::async_pool", TRUE);
1662
1663 coro_current = get_sv ("Coro::current", FALSE);
1664 SvREADONLY_on (coro_current);
1282 1665
1283 coro_stash = gv_stashpv ("Coro", TRUE); 1666 coro_stash = gv_stashpv ("Coro", TRUE);
1284 1667
1285 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1668 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1286 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1669 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1287 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1670 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1288 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1671 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1289 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1672 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1290 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1673 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1291 1674
1292 coro_current = get_sv ("Coro::current", FALSE);
1293 SvREADONLY_on (coro_current);
1294
1295 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1675 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1296 coro_ready[i] = newAV (); 1676 coro_ready[i] = newAV ();
1297 1677
1298 { 1678 {
1299 SV *sv = perl_get_sv("Coro::API", 1); 1679 SV *sv = perl_get_sv("Coro::API", 1);
1300 1680
1301 coroapi.schedule = api_schedule; 1681 coroapi.schedule = api_schedule;
1302 coroapi.save = api_save;
1303 coroapi.cede = api_cede; 1682 coroapi.cede = api_cede;
1304 coroapi.cede_notself = api_cede_notself; 1683 coroapi.cede_notself = api_cede_notself;
1305 coroapi.ready = api_ready; 1684 coroapi.ready = api_ready;
1306 coroapi.is_ready = api_is_ready; 1685 coroapi.is_ready = api_is_ready;
1307 coroapi.nready = &coro_nready; 1686 coroapi.nready = &coro_nready;
1348 CODE: 1727 CODE:
1349 RETVAL = boolSV (api_ready (self)); 1728 RETVAL = boolSV (api_ready (self));
1350 OUTPUT: 1729 OUTPUT:
1351 RETVAL 1730 RETVAL
1352 1731
1353SV *
1354is_ready (SV *self)
1355 PROTOTYPE: $
1356 CODE:
1357 RETVAL = boolSV (api_is_ready (self));
1358 OUTPUT:
1359 RETVAL
1360
1361int 1732int
1362nready (...) 1733nready (...)
1363 PROTOTYPE: 1734 PROTOTYPE:
1364 CODE: 1735 CODE:
1365 RETVAL = coro_nready; 1736 RETVAL = coro_nready;
1366 OUTPUT: 1737 OUTPUT:
1367 RETVAL 1738 RETVAL
1368 1739
1740void
1741throw (Coro::State self, SV *throw = &PL_sv_undef)
1742 PROTOTYPE: $;$
1743 CODE:
1744 SvREFCNT_dec (self->throw);
1745 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1746
1747# for async_pool speedup
1748void
1749_pool_1 (SV *cb)
1750 CODE:
1751{
1752 struct coro *coro = SvSTATE (coro_current);
1753 HV *hv = (HV *)SvRV (coro_current);
1754 AV *defav = GvAV (PL_defgv);
1755 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1756 AV *invoke_av;
1757 int i, len;
1758
1759 if (!invoke)
1760 croak ("\3terminate\2\n");
1761
1762 SvREFCNT_dec (coro->saved_deffh);
1763 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1764
1765 hv_store (hv, "desc", sizeof ("desc") - 1,
1766 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1767
1768 invoke_av = (AV *)SvRV (invoke);
1769 len = av_len (invoke_av);
1770
1771 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1772
1773 if (len > 0)
1774 {
1775 av_fill (defav, len - 1);
1776 for (i = 0; i < len; ++i)
1777 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1778 }
1779
1780 SvREFCNT_dec (invoke);
1781}
1782
1783void
1784_pool_2 (SV *cb)
1785 CODE:
1786{
1787 struct coro *coro = SvSTATE (coro_current);
1788
1789 sv_setsv (cb, &PL_sv_undef);
1790
1791 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1792 coro->saved_deffh = 0;
1793
1794 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1795 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1796 croak ("\3terminate\2\n");
1797
1798 av_clear (GvAV (PL_defgv));
1799 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1800 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1801
1802 coro->prio = 0;
1803
1804 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1805 api_trace (coro_current, 0);
1806
1807 av_push (av_async_pool, newSVsv (coro_current));
1808}
1809
1810
1369MODULE = Coro::State PACKAGE = Coro::AIO 1811MODULE = Coro::State PACKAGE = Coro::AIO
1370 1812
1371SV * 1813SV *
1372_get_state () 1814_get_state ()
1373 CODE: 1815 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines