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.193 by root, Fri Oct 5 23:38:40 2007 UTC vs.
Revision 1.199 by root, Sun Oct 7 13:53:37 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2 2
3#define PERL_NO_GET_CONTEXT 3#define PERL_NO_GET_CONTEXT
4#define PERL_EXT
4 5
5#include "EXTERN.h" 6#include "EXTERN.h"
6#include "perl.h" 7#include "perl.h"
7#include "XSUB.h" 8#include "XSUB.h"
8 9
106#define IN_DESTRUCT (PL_main_cv == Nullcv) 107#define IN_DESTRUCT (PL_main_cv == Nullcv)
107 108
108#if __GNUC__ >= 3 109#if __GNUC__ >= 3
109# define attribute(x) __attribute__(x) 110# define attribute(x) __attribute__(x)
110# define BARRIER __asm__ __volatile__ ("" : : : "memory") 111# define BARRIER __asm__ __volatile__ ("" : : : "memory")
112# define expect(expr,value) __builtin_expect ((expr),(value))
111#else 113#else
112# define attribute(x) 114# define attribute(x)
113# define BARRIER 115# define BARRIER
116# define expect(expr,value) (expr)
114#endif 117#endif
118
119#define expect_false(expr) expect ((expr) != 0, 0)
120#define expect_true(expr) expect ((expr) != 0, 1)
115 121
116#define NOINLINE attribute ((noinline)) 122#define NOINLINE attribute ((noinline))
117 123
118#include "CoroAPI.h" 124#include "CoroAPI.h"
119 125
142static HV *coro_state_stash, *coro_stash; 148static HV *coro_state_stash, *coro_stash;
143static SV *coro_mortal; /* will be freed after next transfer */ 149static SV *coro_mortal; /* will be freed after next transfer */
144 150
145static GV *irsgv; /* $/ */ 151static GV *irsgv; /* $/ */
146static GV *stdoutgv; /* *STDOUT */ 152static GV *stdoutgv; /* *STDOUT */
153static SV *sv_diehook;
154static SV *sv_warnhook;
147 155
148/* async_pool helper stuff */ 156/* async_pool helper stuff */
149static SV *sv_pool_rss; 157static SV *sv_pool_rss;
150static SV *sv_pool_size; 158static SV *sv_pool_size;
151static AV *av_async_pool; 159static AV *av_async_pool;
187 CF_READY = 0x0002, /* coroutine is ready */ 195 CF_READY = 0x0002, /* coroutine is ready */
188 CF_NEW = 0x0004, /* has never been switched to */ 196 CF_NEW = 0x0004, /* has never been switched to */
189 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 197 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
190}; 198};
191 199
200/* the structure where most of the perl state is stored, overlaid on the cxstack */
201typedef struct {
202 SV *defsv;
203 AV *defav;
204 SV *errsv;
205 SV *irsgv;
206#define VAR(name,type) type name;
207# include "state.h"
208#undef VAR
209} perl_slots;
210
211#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
212
192/* this is a structure representing a perl-level coroutine */ 213/* this is a structure representing a perl-level coroutine */
193struct coro { 214struct coro {
194 /* the c coroutine allocated to this perl coroutine, if any */ 215 /* the c coroutine allocated to this perl coroutine, if any */
195 coro_cctx *cctx; 216 coro_cctx *cctx;
196 217
218 /* process data */
219 AV *mainstack;
220 perl_slots *slot; /* basically the saved sp */
221
197 /* data associated with this coroutine (initial args) */ 222 /* data associated with this coroutine (initial args) */
198 AV *args; 223 AV *args;
199 int refcnt; 224 int refcnt;
200 int flags; /* CF_ flags */ 225 int flags; /* CF_ flags */
201 226
202 /* optionally saved, might be zero */
203 AV *defav; /* @_ */
204 SV *defsv; /* $_ */
205 SV *errsv; /* $@ */
206 SV *deffh; /* default filehandle */
207 SV *irssv; /* $/ */
208 SV *irssv_sv; /* real $/ cache */
209
210#define VAR(name,type) type name;
211# include "state.h"
212#undef VAR
213
214 /* statistics */ 227 /* statistics */
215 int usecount; /* number of transfers to this coro */ 228 int usecount; /* number of transfers to this coro */
216 229
217 /* coro process data */ 230 /* coro process data */
218 int prio; 231 int prio;
219 //SV *throw; 232 SV *throw;
220 233
221 /* async_pool */ 234 /* async_pool */
222 SV *saved_deffh; 235 SV *saved_deffh;
223 236
224 /* linked list */ 237 /* linked list */
243static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 256static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
244static int coro_nready; 257static int coro_nready;
245static struct coro *coro_first; 258static struct coro *coro_first;
246 259
247/** lowlevel stuff **********************************************************/ 260/** lowlevel stuff **********************************************************/
261
262static SV *
263coro_get_sv (const char *name, int create)
264{
265#if PERL_VERSION_ATLEAST (5,9,0)
266 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
267 get_sv (name, create);
268#endif
269 return get_sv (name, create);
270}
271
272static SV *
273coro_get_av (const char *name, int create)
274{
275#if PERL_VERSION_ATLEAST (5,9,0)
276 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
277 get_av (name, create);
278#endif
279 return get_av (name, create);
280}
248 281
249static AV * 282static AV *
250coro_clone_padlist (pTHX_ CV *cv) 283coro_clone_padlist (pTHX_ CV *cv)
251{ 284{
252 AV *padlist = CvPADLIST (cv); 285 AV *padlist = CvPADLIST (cv);
325 MAGIC *mg; 358 MAGIC *mg;
326 359
327 if (SvROK (coro)) 360 if (SvROK (coro))
328 coro = SvRV (coro); 361 coro = SvRV (coro);
329 362
330 if (SvTYPE (coro) != SVt_PVHV) 363 if (expect_false (SvTYPE (coro) != SVt_PVHV))
331 croak ("Coro::State object required"); 364 croak ("Coro::State object required");
332 365
333 stash = SvSTASH (coro); 366 stash = SvSTASH (coro);
334 if (stash != coro_stash && stash != coro_state_stash) 367 if (expect_false (stash != coro_stash && stash != coro_state_stash))
335 { 368 {
336 /* very slow, but rare, check */ 369 /* very slow, but rare, check */
337 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 370 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
338 croak ("Coro::State object required"); 371 croak ("Coro::State object required");
339 } 372 }
349get_padlist (pTHX_ CV *cv) 382get_padlist (pTHX_ CV *cv)
350{ 383{
351 MAGIC *mg = CORO_MAGIC (cv); 384 MAGIC *mg = CORO_MAGIC (cv);
352 AV *av; 385 AV *av;
353 386
354 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 387 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
355 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 388 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
356 else 389 else
357 { 390 {
358#if CORO_PREFER_PERL_FUNCTIONS 391#if CORO_PREFER_PERL_FUNCTIONS
359 /* this is probably cleaner, but also slower? */ 392 /* this is probably cleaner, but also slower? */
371put_padlist (pTHX_ CV *cv) 404put_padlist (pTHX_ CV *cv)
372{ 405{
373 MAGIC *mg = CORO_MAGIC (cv); 406 MAGIC *mg = CORO_MAGIC (cv);
374 AV *av; 407 AV *av;
375 408
376 if (!mg) 409 if (expect_false (!mg))
377 { 410 {
378 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 411 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
379 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 412 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
380 mg->mg_virtual = &vtbl_coro; 413 mg->mg_virtual = &vtbl_coro;
381 mg->mg_obj = (SV *)newAV (); 414 mg->mg_obj = (SV *)newAV ();
382 } 415 }
383 416
384 av = (AV *)mg->mg_obj; 417 av = (AV *)mg->mg_obj;
385 418
386 if (AvFILLp (av) >= AvMAX (av)) 419 if (expect_false (AvFILLp (av) >= AvMAX (av)))
387 av_extend (av, AvMAX (av) + 1); 420 av_extend (av, AvMAX (av) + 1);
388 421
389 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 422 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
390} 423}
391 424
392/** load & save, init *******************************************************/ 425/** load & save, init *******************************************************/
393 426
394static void 427static void
395load_perl (pTHX_ Coro__State c) 428load_perl (pTHX_ Coro__State c)
396{ 429{
430 perl_slots *slot = c->slot;
431 c->slot = 0;
432
433 PL_mainstack = c->mainstack;
434
435 GvSV (PL_defgv) = slot->defsv;
436 GvAV (PL_defgv) = slot->defav;
437 GvSV (PL_errgv) = slot->errsv;
438 GvSV (irsgv) = slot->irsgv;
439
397#define VAR(name,type) PL_ ## name = c->name; 440 #define VAR(name,type) PL_ ## name = slot->name;
398# include "state.h" 441 # include "state.h"
399#undef VAR 442 #undef VAR
400
401 GvSV (PL_defgv) = c->defsv;
402 GvAV (PL_defgv) = c->defav;
403 GvSV (PL_errgv) = c->errsv;
404 GvSV (irsgv) = c->irssv_sv;
405 443
406 { 444 {
407 dSP; 445 dSP;
446
408 CV *cv; 447 CV *cv;
409 448
410 /* now do the ugly restore mess */ 449 /* now do the ugly restore mess */
411 while ((cv = (CV *)POPs)) 450 while (expect_true (cv = (CV *)POPs))
412 { 451 {
413 put_padlist (aTHX_ cv); /* mark this padlist as available */ 452 put_padlist (aTHX_ cv); /* mark this padlist as available */
414 CvDEPTH (cv) = PTR2IV (POPs); 453 CvDEPTH (cv) = PTR2IV (POPs);
415 CvPADLIST (cv) = (AV *)POPs; 454 CvPADLIST (cv) = (AV *)POPs;
416 } 455 }
435 474
436 XPUSHs (Nullsv); 475 XPUSHs (Nullsv);
437 /* this loop was inspired by pp_caller */ 476 /* this loop was inspired by pp_caller */
438 for (;;) 477 for (;;)
439 { 478 {
440 while (cxix >= 0) 479 while (expect_true (cxix >= 0))
441 { 480 {
442 PERL_CONTEXT *cx = &ccstk[cxix--]; 481 PERL_CONTEXT *cx = &ccstk[cxix--];
443 482
444 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) 483 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT))
445 { 484 {
446 CV *cv = cx->blk_sub.cv; 485 CV *cv = cx->blk_sub.cv;
447 486
448 if (CvDEPTH (cv)) 487 if (expect_true (CvDEPTH (cv)))
449 { 488 {
450 EXTEND (SP, 3); 489 EXTEND (SP, 3);
451 PUSHs ((SV *)CvPADLIST (cv)); 490 PUSHs ((SV *)CvPADLIST (cv));
452 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 491 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
453 PUSHs ((SV *)cv); 492 PUSHs ((SV *)cv);
456 get_padlist (aTHX_ cv); 495 get_padlist (aTHX_ cv);
457 } 496 }
458 } 497 }
459 } 498 }
460 499
461 if (top_si->si_type == PERLSI_MAIN) 500 if (expect_true (top_si->si_type == PERLSI_MAIN))
462 break; 501 break;
463 502
464 top_si = top_si->si_prev; 503 top_si = top_si->si_prev;
465 ccstk = top_si->si_cxstack; 504 ccstk = top_si->si_cxstack;
466 cxix = top_si->si_cxix; 505 cxix = top_si->si_cxix;
467 } 506 }
468 507
469 PUTBACK; 508 PUTBACK;
470 } 509 }
471 510
511 /* allocate some space on the context stack for our purposes */
512 {
513 /* we manually unroll here, as usually 2 slots is enough */
514 int i;
515 if (SLOT_COUNT >= 1) CXINC;
516 if (SLOT_COUNT >= 2) CXINC;
517 if (SLOT_COUNT >= 3) CXINC;
518 for (i = 3; i < SLOT_COUNT; ++i)
519 CXINC;
520
521 cxstack_ix -= SLOT_COUNT; /* undo allocation */
522 }
523
524 c->mainstack = PL_mainstack;
525
526 {
527 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
528
472 c->defav = GvAV (PL_defgv); 529 slot->defav = GvAV (PL_defgv);
473 c->defsv = DEFSV; 530 slot->defsv = DEFSV;
474 c->errsv = ERRSV; 531 slot->errsv = ERRSV;
475 c->irssv_sv = GvSV (irsgv); 532 slot->irsgv = GvSV (irsgv);
476 533
477#define VAR(name,type)c->name = PL_ ## name; 534 #define VAR(name,type) slot->name = PL_ ## name;
478# include "state.h" 535 # include "state.h"
479#undef VAR 536 #undef VAR
537 }
480} 538}
481 539
482/* 540/*
483 * allocate various perl stacks. This is an exact copy 541 * allocate various perl stacks. This is an exact copy
484 * of perl.c:init_stacks, except that it uses less memory 542 * of perl.c:init_stacks, except that it uses less memory
489# define coro_init_stacks init_stacks 547# define coro_init_stacks init_stacks
490#else 548#else
491static void 549static void
492coro_init_stacks (pTHX) 550coro_init_stacks (pTHX)
493{ 551{
494 PL_curstackinfo = new_stackinfo(64, 6); 552 PL_curstackinfo = new_stackinfo(32, 8);
495 PL_curstackinfo->si_type = PERLSI_MAIN; 553 PL_curstackinfo->si_type = PERLSI_MAIN;
496 PL_curstack = PL_curstackinfo->si_stack; 554 PL_curstack = PL_curstackinfo->si_stack;
497 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 555 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
498 556
499 PL_stack_base = AvARRAY(PL_curstack); 557 PL_stack_base = AvARRAY(PL_curstack);
500 PL_stack_sp = PL_stack_base; 558 PL_stack_sp = PL_stack_base;
501 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 559 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
502 560
503 New(50,PL_tmps_stack,64,SV*); 561 New(50,PL_tmps_stack,32,SV*);
504 PL_tmps_floor = -1; 562 PL_tmps_floor = -1;
505 PL_tmps_ix = -1; 563 PL_tmps_ix = -1;
506 PL_tmps_max = 64; 564 PL_tmps_max = 32;
507 565
508 New(54,PL_markstack,16,I32); 566 New(54,PL_markstack,16,I32);
509 PL_markstack_ptr = PL_markstack; 567 PL_markstack_ptr = PL_markstack;
510 PL_markstack_max = PL_markstack + 16; 568 PL_markstack_max = PL_markstack + 16;
511 569
512#ifdef SET_MARK_OFFSET 570#ifdef SET_MARK_OFFSET
513 SET_MARK_OFFSET; 571 SET_MARK_OFFSET;
514#endif 572#endif
515 573
516 New(54,PL_scopestack,16,I32); 574 New(54,PL_scopestack,8,I32);
517 PL_scopestack_ix = 0; 575 PL_scopestack_ix = 0;
518 PL_scopestack_max = 16; 576 PL_scopestack_max = 8;
519 577
520 New(54,PL_savestack,64,ANY); 578 New(54,PL_savestack,24,ANY);
521 PL_savestack_ix = 0; 579 PL_savestack_ix = 0;
522 PL_savestack_max = 64; 580 PL_savestack_max = 24;
523 581
524#if !PERL_VERSION_ATLEAST (5,9,0) 582#if !PERL_VERSION_ATLEAST (5,9,0)
525 New(54,PL_retstack,4,OP*); 583 New(54,PL_retstack,4,OP*);
526 PL_retstack_ix = 0; 584 PL_retstack_ix = 0;
527 PL_retstack_max = 4; 585 PL_retstack_max = 4;
564{ 622{
565 size_t rss = sizeof (*coro); 623 size_t rss = sizeof (*coro);
566 624
567 if (coro->mainstack) 625 if (coro->mainstack)
568 { 626 {
627 perl_slots tmp_slot;
628 perl_slots *slot;
629
569 if (coro->flags & CF_RUNNING) 630 if (coro->flags & CF_RUNNING)
570 { 631 {
632 slot = &tmp_slot;
633
571 #define VAR(name,type)coro->name = PL_ ## name; 634 #define VAR(name,type) slot->name = PL_ ## name;
572 # include "state.h" 635 # include "state.h"
573 #undef VAR 636 #undef VAR
574 } 637 }
638 else
639 slot = coro->slot;
575 640
576 rss += sizeof (coro->curstackinfo); 641 rss += sizeof (slot->curstackinfo);
577 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
578 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 642 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
579 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 643 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
580 rss += coro->tmps_max * sizeof (SV *); 644 rss += slot->tmps_max * sizeof (SV *);
581 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 645 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
582 rss += coro->scopestack_max * sizeof (I32); 646 rss += slot->scopestack_max * sizeof (I32);
583 rss += coro->savestack_max * sizeof (ANY); 647 rss += slot->savestack_max * sizeof (ANY);
584 648
585#if !PERL_VERSION_ATLEAST (5,9,0) 649#if !PERL_VERSION_ATLEAST (5,9,0)
586 rss += coro->retstack_max * sizeof (OP *); 650 rss += slot->retstack_max * sizeof (OP *);
587#endif 651#endif
588 } 652 }
589 653
590 return rss; 654 return rss;
591} 655}
603 PL_runops = RUNOPS_DEFAULT; 667 PL_runops = RUNOPS_DEFAULT;
604 PL_curcop = &PL_compiling; 668 PL_curcop = &PL_compiling;
605 PL_in_eval = EVAL_NULL; 669 PL_in_eval = EVAL_NULL;
606 PL_comppad = 0; 670 PL_comppad = 0;
607 PL_curpm = 0; 671 PL_curpm = 0;
672 PL_curpad = 0;
608 PL_localizing = 0; 673 PL_localizing = 0;
609 PL_dirty = 0; 674 PL_dirty = 0;
610 PL_restartop = 0; 675 PL_restartop = 0;
676 PL_diehook = SvREFCNT_inc (sv_diehook);
677 PL_warnhook = SvREFCNT_inc (sv_warnhook);
611 678
612 GvSV (PL_defgv) = newSV (0); 679 GvSV (PL_defgv) = newSV (0);
613 GvAV (PL_defgv) = coro->args; coro->args = 0; 680 GvAV (PL_defgv) = coro->args; coro->args = 0;
614 GvSV (PL_errgv) = newSV (0); 681 GvSV (PL_errgv) = newSV (0);
615 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 682 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
616 PL_rs = newSVsv (GvSV (irsgv)); 683 PL_rs = newSVsv (GvSV (irsgv));
617 PL_defoutgv = SvREFCNT_inc (stdoutgv); 684 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv);
618
619 ENTER; /* necessary e.g. for dounwind */
620 685
621 { 686 {
622 dSP; 687 dSP;
623 LOGOP myop; 688 LOGOP myop;
624 689
631 PUTBACK; 696 PUTBACK;
632 PL_op = (OP *)&myop; 697 PL_op = (OP *)&myop;
633 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 698 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
634 SPAGAIN; 699 SPAGAIN;
635 } 700 }
701
702 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */
636} 703}
637 704
638static void 705static void
639coro_destroy (pTHX_ struct coro *coro) 706coro_destroy (pTHX_ struct coro *coro)
640{ 707{
660 SvREFCNT_dec (GvSV (PL_errgv)); 727 SvREFCNT_dec (GvSV (PL_errgv));
661 SvREFCNT_dec (PL_defoutgv); 728 SvREFCNT_dec (PL_defoutgv);
662 SvREFCNT_dec (PL_rs); 729 SvREFCNT_dec (PL_rs);
663 SvREFCNT_dec (GvSV (irsgv)); 730 SvREFCNT_dec (GvSV (irsgv));
664 731
732 SvREFCNT_dec (PL_diehook);
733 SvREFCNT_dec (PL_warnhook);
734
665 SvREFCNT_dec (coro->saved_deffh); 735 SvREFCNT_dec (coro->saved_deffh);
666 //SvREFCNT_dec (coro->throw); 736 SvREFCNT_dec (coro->throw);
667 737
668 coro_destroy_stacks (aTHX); 738 coro_destroy_stacks (aTHX);
669} 739}
670 740
671static void 741static void
672free_coro_mortal (pTHX) 742free_coro_mortal (pTHX)
673{ 743{
674 if (coro_mortal) 744 if (expect_true (coro_mortal))
675 { 745 {
676 SvREFCNT_dec (coro_mortal); 746 SvREFCNT_dec (coro_mortal);
677 coro_mortal = 0; 747 coro_mortal = 0;
678 } 748 }
679} 749}
936#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1006#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
937 1007
938static coro_cctx * 1008static coro_cctx *
939cctx_get (pTHX) 1009cctx_get (pTHX)
940{ 1010{
941 while (cctx_first) 1011 while (expect_true (cctx_first))
942 { 1012 {
943 coro_cctx *cctx = cctx_first; 1013 coro_cctx *cctx = cctx_first;
944 cctx_first = cctx->next; 1014 cctx_first = cctx->next;
945 --cctx_idle; 1015 --cctx_idle;
946 1016
947 if (!CCTX_EXPIRED (cctx)) 1017 if (expect_true (!CCTX_EXPIRED (cctx)))
948 return cctx; 1018 return cctx;
949 1019
950 cctx_destroy (cctx); 1020 cctx_destroy (cctx);
951 } 1021 }
952 1022
955 1025
956static void 1026static void
957cctx_put (coro_cctx *cctx) 1027cctx_put (coro_cctx *cctx)
958{ 1028{
959 /* free another cctx if overlimit */ 1029 /* free another cctx if overlimit */
960 if (cctx_idle >= MAX_IDLE_CCTX) 1030 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
961 { 1031 {
962 coro_cctx *first = cctx_first; 1032 coro_cctx *first = cctx_first;
963 cctx_first = first->next; 1033 cctx_first = first->next;
964 --cctx_idle; 1034 --cctx_idle;
965 1035
971 cctx_first = cctx; 1041 cctx_first = cctx;
972} 1042}
973 1043
974/** coroutine switching *****************************************************/ 1044/** coroutine switching *****************************************************/
975 1045
976static void NOINLINE 1046static void
977transfer_check (pTHX_ struct coro *prev, struct coro *next) 1047transfer_check (pTHX_ struct coro *prev, struct coro *next)
978{ 1048{
979 if (prev != next) 1049 if (expect_true (prev != next))
980 { 1050 {
981 if (!(prev->flags & (CF_RUNNING | CF_NEW))) 1051 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
982 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); 1052 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
983 1053
984 if (next->flags & CF_RUNNING) 1054 if (expect_false (next->flags & CF_RUNNING))
985 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1055 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
986 1056
987 if (next->flags & CF_DESTROYED) 1057 if (expect_false (next->flags & CF_DESTROYED))
988 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1058 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
989 1059
1060 if (
1061#if PERL_VERSION_ATLEAST (5,9,0)
1062 expect_false (PL_parser)
1063#else
990 if (PL_lex_state != LEX_NOTPARSING) 1064 expect_false (PL_lex_state != LEX_NOTPARSING)
1065#endif
1066 )
991 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1067 croak ("Coro::State::transfer called while parsing, but this is not supported");
992 } 1068 }
993} 1069}
994 1070
995/* always use the TRANSFER macro */ 1071/* always use the TRANSFER macro */
997transfer (pTHX_ struct coro *prev, struct coro *next) 1073transfer (pTHX_ struct coro *prev, struct coro *next)
998{ 1074{
999 dSTACKLEVEL; 1075 dSTACKLEVEL;
1000 1076
1001 /* sometimes transfer is only called to set idle_sp */ 1077 /* sometimes transfer is only called to set idle_sp */
1002 if (!next) 1078 if (expect_false (!next))
1003 { 1079 {
1004 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1080 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1005 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1081 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1006 } 1082 }
1007 else if (prev != next) 1083 else if (expect_true (prev != next))
1008 { 1084 {
1009 coro_cctx *prev__cctx; 1085 coro_cctx *prev__cctx;
1010 1086
1011 if (prev->flags & CF_NEW) 1087 if (expect_false (prev->flags & CF_NEW))
1012 { 1088 {
1013 /* create a new empty context */ 1089 /* create a new empty context */
1014 Newz (0, prev->cctx, 1, coro_cctx); 1090 Newz (0, prev->cctx, 1, coro_cctx);
1015 prev->flags &= ~CF_NEW; 1091 prev->flags &= ~CF_NEW;
1016 prev->flags |= CF_RUNNING; 1092 prev->flags |= CF_RUNNING;
1019 prev->flags &= ~CF_RUNNING; 1095 prev->flags &= ~CF_RUNNING;
1020 next->flags |= CF_RUNNING; 1096 next->flags |= CF_RUNNING;
1021 1097
1022 LOCK; 1098 LOCK;
1023 1099
1024 if (next->flags & CF_NEW) 1100 if (expect_false (next->flags & CF_NEW))
1025 { 1101 {
1026 /* need to start coroutine */ 1102 /* need to start coroutine */
1027 next->flags &= ~CF_NEW; 1103 next->flags &= ~CF_NEW;
1028 /* first get rid of the old state */ 1104 /* first get rid of the old state */
1029 save_perl (aTHX_ prev); 1105 save_perl (aTHX_ prev);
1038 } 1114 }
1039 1115
1040 prev__cctx = prev->cctx; 1116 prev__cctx = prev->cctx;
1041 1117
1042 /* possibly "free" the cctx */ 1118 /* possibly "free" the cctx */
1043 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) 1119 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1044 { 1120 {
1045 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1121 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1046 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1122 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1047 1123
1048 prev->cctx = 0; 1124 prev->cctx = 0;
1049 1125
1050 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1126 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1127 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1051 if (CCTX_EXPIRED (prev__cctx)) 1128 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1052 next->cctx = cctx_get (aTHX); 1129 next->cctx = cctx_get (aTHX);
1053 1130
1054 cctx_put (prev__cctx); 1131 cctx_put (prev__cctx);
1055 } 1132 }
1056 1133
1057 ++next->usecount; 1134 ++next->usecount;
1058 1135
1059 if (!next->cctx) 1136 if (expect_true (!next->cctx))
1060 next->cctx = cctx_get (aTHX); 1137 next->cctx = cctx_get (aTHX);
1061 1138
1062 if (prev__cctx != next->cctx) 1139 if (expect_false (prev__cctx != next->cctx))
1063 { 1140 {
1064 prev__cctx->top_env = PL_top_env; 1141 prev__cctx->top_env = PL_top_env;
1065 PL_top_env = next->cctx->top_env; 1142 PL_top_env = next->cctx->top_env;
1066 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1143 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1067 } 1144 }
1068 1145
1069 free_coro_mortal (aTHX); 1146 free_coro_mortal (aTHX);
1070 UNLOCK; 1147 UNLOCK;
1148
1149 if (expect_false (prev->throw || next->throw))
1150 {
1151 struct coro *coro = SvSTATE (coro_current);
1152
1153 if (coro->throw)
1154 {
1155 SV *exception = coro->throw;
1156 coro->throw = 0;
1157 sv_setsv (ERRSV, exception);
1158 croak (0);
1159 }
1160 }
1071 } 1161 }
1072} 1162}
1073 1163
1074struct transfer_args 1164struct transfer_args
1075{ 1165{
1110 save_perl (aTHX_ &temp); 1200 save_perl (aTHX_ &temp);
1111 load_perl (aTHX_ coro); 1201 load_perl (aTHX_ coro);
1112 1202
1113 coro_destroy (aTHX_ coro); 1203 coro_destroy (aTHX_ coro);
1114 1204
1115 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1205 load_perl (aTHX_ &temp);
1116 1206
1117 coro->mainstack = 0; 1207 coro->slot = 0;
1118 } 1208 }
1119 1209
1120 cctx_destroy (coro->cctx); 1210 cctx_destroy (coro->cctx);
1121 SvREFCNT_dec (coro->args); 1211 SvREFCNT_dec (coro->args);
1122 1212
1247 { 1337 {
1248 LOCK; 1338 LOCK;
1249 next_sv = coro_deq (aTHX_ PRIO_MIN); 1339 next_sv = coro_deq (aTHX_ PRIO_MIN);
1250 1340
1251 /* nothing to schedule: call the idle handler */ 1341 /* nothing to schedule: call the idle handler */
1252 if (!next_sv) 1342 if (expect_false (!next_sv))
1253 { 1343 {
1254 dSP; 1344 dSP;
1255 UNLOCK; 1345 UNLOCK;
1256 1346
1257 ENTER; 1347 ENTER;
1267 } 1357 }
1268 1358
1269 ta->next = SvSTATE (next_sv); 1359 ta->next = SvSTATE (next_sv);
1270 1360
1271 /* cannot transfer to destroyed coros, skip and look for next */ 1361 /* cannot transfer to destroyed coros, skip and look for next */
1272 if (ta->next->flags & CF_DESTROYED) 1362 if (expect_false (ta->next->flags & CF_DESTROYED))
1273 { 1363 {
1274 UNLOCK; 1364 UNLOCK;
1275 SvREFCNT_dec (next_sv); 1365 SvREFCNT_dec (next_sv);
1276 /* coro_nready is already taken care of by destroy */ 1366 /* coro_nready is already taken care of by destroy */
1277 continue; 1367 continue;
1333 dTHX; 1423 dTHX;
1334 struct transfer_args ta; 1424 struct transfer_args ta;
1335 1425
1336 prepare_cede (aTHX_ &ta); 1426 prepare_cede (aTHX_ &ta);
1337 1427
1338 if (ta.prev != ta.next) 1428 if (expect_true (ta.prev != ta.next))
1339 { 1429 {
1340 TRANSFER (ta); 1430 TRANSFER (ta);
1341 return 1; 1431 return 1;
1342 } 1432 }
1343 else 1433 else
1379 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); 1469 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1380 1470
1381 if (coro->flags & CF_RUNNING) 1471 if (coro->flags & CF_RUNNING)
1382 PL_runops = RUNOPS_DEFAULT; 1472 PL_runops = RUNOPS_DEFAULT;
1383 else 1473 else
1384 coro->runops = RUNOPS_DEFAULT; 1474 coro->slot->runops = RUNOPS_DEFAULT;
1385 } 1475 }
1386} 1476}
1387 1477
1388MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1478MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1389 1479
1396#endif 1486#endif
1397 BOOT_PAGESIZE; 1487 BOOT_PAGESIZE;
1398 1488
1399 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1489 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1400 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1490 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1491
1492 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE);
1493 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE);
1494
1495 if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook);
1496 if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook);
1401 1497
1402 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1498 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1403 1499
1404 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1500 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1405 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1501 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1489 } 1585 }
1490 1586
1491 BARRIER; 1587 BARRIER;
1492 TRANSFER (ta); 1588 TRANSFER (ta);
1493 1589
1494 if (GIMME_V != G_VOID && ta.next != ta.prev) 1590 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1495 XSRETURN_YES; 1591 XSRETURN_YES;
1496} 1592}
1497 1593
1498bool 1594bool
1499_destroy (SV *coro_sv) 1595_destroy (SV *coro_sv)
1549 CODE: 1645 CODE:
1550{ 1646{
1551 if (coro->mainstack) 1647 if (coro->mainstack)
1552 { 1648 {
1553 struct coro temp; 1649 struct coro temp;
1554 Zero (&temp, 1, struct coro);
1555 1650
1556 if (!(coro->flags & CF_RUNNING)) 1651 if (!(coro->flags & CF_RUNNING))
1557 { 1652 {
1558 save_perl (aTHX_ &temp); 1653 save_perl (aTHX_ &temp);
1559 load_perl (aTHX_ coro); 1654 load_perl (aTHX_ coro);
1563 dSP; 1658 dSP;
1564 ENTER; 1659 ENTER;
1565 SAVETMPS; 1660 SAVETMPS;
1566 PUSHMARK (SP); 1661 PUSHMARK (SP);
1567 PUTBACK; 1662 PUTBACK;
1663
1568 if (ix) 1664 if (ix)
1569 eval_sv (coderef, 0); 1665 eval_sv (coderef, 0);
1570 else 1666 else
1571 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1667 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1668
1572 SPAGAIN; 1669 SPAGAIN;
1573 FREETMPS; 1670 FREETMPS;
1574 LEAVE; 1671 LEAVE;
1575 PUTBACK; 1672 PUTBACK;
1576 } 1673 }
1634 1731
1635BOOT: 1732BOOT:
1636{ 1733{
1637 int i; 1734 int i;
1638 1735
1639 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1736 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1640 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1737 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1641 av_async_pool = get_av ("Coro::async_pool", TRUE); 1738 av_async_pool = coro_get_av ("Coro::async_pool", TRUE);
1642 1739
1643 coro_current = get_sv ("Coro::current", FALSE); 1740 coro_current = coro_get_sv ("Coro::current", FALSE);
1644 SvREADONLY_on (coro_current); 1741 SvREADONLY_on (coro_current);
1645 1742
1646 coro_stash = gv_stashpv ("Coro", TRUE); 1743 coro_stash = gv_stashpv ("Coro", TRUE);
1647 1744
1648 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1745 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1649 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1746 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1650 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1747 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1651 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1748 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1654 1751
1655 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1752 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1656 coro_ready[i] = newAV (); 1753 coro_ready[i] = newAV ();
1657 1754
1658 { 1755 {
1659 SV *sv = perl_get_sv("Coro::API", 1); 1756 SV *sv = perl_get_sv ("Coro::API", TRUE);
1757 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1660 1758
1661 coroapi.schedule = api_schedule; 1759 coroapi.schedule = api_schedule;
1662 coroapi.cede = api_cede; 1760 coroapi.cede = api_cede;
1663 coroapi.cede_notself = api_cede_notself; 1761 coroapi.cede_notself = api_cede_notself;
1664 coroapi.ready = api_ready; 1762 coroapi.ready = api_ready;
1715 CODE: 1813 CODE:
1716 RETVAL = coro_nready; 1814 RETVAL = coro_nready;
1717 OUTPUT: 1815 OUTPUT:
1718 RETVAL 1816 RETVAL
1719 1817
1818void
1819throw (Coro::State self, SV *throw = &PL_sv_undef)
1820 PROTOTYPE: $;$
1821 CODE:
1822 SvREFCNT_dec (self->throw);
1823 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1824
1720# for async_pool speedup 1825# for async_pool speedup
1721void 1826void
1722_pool_1 (SV *cb) 1827_pool_1 (SV *cb)
1723 CODE: 1828 CODE:
1724{ 1829{
1728 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1833 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1729 AV *invoke_av; 1834 AV *invoke_av;
1730 int i, len; 1835 int i, len;
1731 1836
1732 if (!invoke) 1837 if (!invoke)
1733 croak ("\3terminate\2\n"); 1838 croak ("\3async_pool terminate\2\n");
1734 1839
1735 SvREFCNT_dec (coro->saved_deffh); 1840 SvREFCNT_dec (coro->saved_deffh);
1736 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 1841 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1737 1842
1738 hv_store (hv, "desc", sizeof ("desc") - 1, 1843 hv_store (hv, "desc", sizeof ("desc") - 1,
1764 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 1869 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1765 coro->saved_deffh = 0; 1870 coro->saved_deffh = 0;
1766 1871
1767 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1872 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1768 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1873 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1769 croak ("\3terminate\2\n"); 1874 croak ("\3async_pool terminate\2\n");
1770 1875
1771 av_clear (GvAV (PL_defgv)); 1876 av_clear (GvAV (PL_defgv));
1772 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1877 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1773 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1878 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1774 1879

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines