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.109 by root, Mon Nov 27 18:45:29 2006 UTC vs.
Revision 1.123 by root, Mon Dec 4 21:56:00 2006 UTC

7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND 9#if USE_VALGRIND
10# include <valgrind/valgrind.h> 10# include <valgrind/valgrind.h>
11#endif 11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
12 15
13#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
87 90
88#define NOINLINE attribute ((noinline)) 91#define NOINLINE attribute ((noinline))
89 92
90#include "CoroAPI.h" 93#include "CoroAPI.h"
91 94
92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
93
94#ifdef USE_ITHREADS 95#ifdef USE_ITHREADS
95static perl_mutex coro_mutex; 96static perl_mutex coro_mutex;
96# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 97# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
97# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 98# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
98#else 99#else
99# define LOCK (void)0 100# define LOCK (void)0
100# define UNLOCK (void)0 101# define UNLOCK (void)0
101#endif 102#endif
102 103
104struct io_state
105{
106 int errorno;
107 I32 laststype;
108 int laststatval;
109 Stat_t statcache;
110};
111
103static struct CoroAPI coroapi; 112static struct CoroAPI coroapi;
104static AV *main_mainstack; /* used to differentiate between $main and others */ 113static AV *main_mainstack; /* used to differentiate between $main and others */
105static HV *coro_state_stash, *coro_stash; 114static HV *coro_state_stash, *coro_stash;
106static SV *coro_mortal; /* will be freed after next transfer */ 115static SV *coro_mortal; /* will be freed after next transfer */
107 116
119 /* cpu state */ 128 /* cpu state */
120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 129 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 JMPENV *top_env; 130 JMPENV *top_env;
122 coro_context cctx; 131 coro_context cctx;
123 132
133 int inuse;
134
124#if USE_VALGRIND 135#if USE_VALGRIND
125 int valgrind_id; 136 int valgrind_id;
126#endif 137#endif
127} coro_cctx; 138} coro_cctx;
139
140enum {
141 CF_RUNNING = 0x0001, /* coroutine is running */
142 CF_READY = 0x0002, /* coroutine is ready */
143 CF_NEW = 0x0004, /* ahs never been switched to */
144};
128 145
129/* this is a structure representing a perl-level coroutine */ 146/* this is a structure representing a perl-level coroutine */
130struct coro { 147struct coro {
131 /* the c coroutine allocated to this perl coroutine, if any */ 148 /* the c coroutine allocated to this perl coroutine, if any */
132 coro_cctx *cctx; 149 coro_cctx *cctx;
133 150
134 /* data associated with this coroutine (initial args) */ 151 /* data associated with this coroutine (initial args) */
135 AV *args; 152 AV *args;
136 int refcnt; 153 int refcnt;
154 int save; /* CORO_SAVE flags */
155 int flags; /* CF_ flags */
137 156
138 /* optionally saved, might be zero */ 157 /* optionally saved, might be zero */
139 AV *defav; 158 AV *defav; /* @_ */
140 SV *defsv; 159 SV *defsv; /* $_ */
141 SV *errsv; 160 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */
162 SV *irssv_sv; /* real $/ cache */
142 163
143#define VAR(name,type) type name; 164#define VAR(name,type) type name;
144# include "state.h" 165# include "state.h"
145#undef VAR 166#undef VAR
146 167
269} 290}
270 291
271#define SB do { 292#define SB do {
272#define SE } while (0) 293#define SE } while (0)
273 294
274#define LOAD(state) load_state((state));
275#define SAVE(state,flags) save_state((state),(flags));
276
277#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 295#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
278 296
279static void 297static void
280load_state(Coro__State c) 298load_perl (Coro__State c)
281{ 299{
282#define VAR(name,type) PL_ ## name = c->name; 300#define VAR(name,type) PL_ ## name = c->name;
283# include "state.h" 301# include "state.h"
284#undef VAR 302#undef VAR
285 303
286 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
287 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
288 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
307 if (c->irssv)
308 {
309 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
310 SvREFCNT_dec (c->irssv);
311 else
312 {
313 REPLACE_SV (PL_rs, c->irssv);
314 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
315 sv_setsv (c->irssv_sv, PL_rs);
316 }
317 }
289 318
290 { 319 {
291 dSP; 320 dSP;
292 CV *cv; 321 CV *cv;
293 322
302 PUTBACK; 331 PUTBACK;
303 } 332 }
304} 333}
305 334
306static void 335static void
307save_state(Coro__State c, int flags) 336save_perl (Coro__State c)
308{ 337{
309 { 338 {
310 dSP; 339 dSP;
311 I32 cxix = cxstack_ix; 340 I32 cxix = cxstack_ix;
312 PERL_CONTEXT *ccstk = cxstack; 341 PERL_CONTEXT *ccstk = cxstack;
323 { 352 {
324 while (cxix >= 0) 353 while (cxix >= 0)
325 { 354 {
326 PERL_CONTEXT *cx = &ccstk[cxix--]; 355 PERL_CONTEXT *cx = &ccstk[cxix--];
327 356
328 if (CxTYPE(cx) == CXt_SUB) 357 if (CxTYPE (cx) == CXt_SUB)
329 { 358 {
330 CV *cv = cx->blk_sub.cv; 359 CV *cv = cx->blk_sub.cv;
331 360
332 if (CvDEPTH (cv)) 361 if (CvDEPTH (cv))
333 { 362 {
334 EXTEND (SP, 3); 363 EXTEND (SP, 3);
335 364
336 PUSHs ((SV *)CvPADLIST(cv)); 365 PUSHs ((SV *)CvPADLIST (cv));
337 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 366 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
338 PUSHs ((SV *)cv); 367 PUSHs ((SV *)cv);
339 368
340 CvDEPTH (cv) = 0; 369 CvDEPTH (cv) = 0;
341 get_padlist (cv); 370 get_padlist (cv);
342 } 371 }
343 } 372 }
344#ifdef CXt_FORMAT 373#ifdef CXt_FORMAT
345 else if (CxTYPE(cx) == CXt_FORMAT) 374 else if (CxTYPE (cx) == CXt_FORMAT)
346 { 375 {
347 /* I never used formats, so how should I know how these are implemented? */ 376 /* I never used formats, so how should I know how these are implemented? */
348 /* my bold guess is as a simple, plain sub... */ 377 /* my bold guess is as a simple, plain sub... */
349 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 378 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
350 } 379 }
360 } 389 }
361 390
362 PUTBACK; 391 PUTBACK;
363 } 392 }
364 393
365 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 394 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
366 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 395 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
367 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 396 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
397 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
368 398
369#define VAR(name,type)c->name = PL_ ## name; 399#define VAR(name,type)c->name = PL_ ## name;
370# include "state.h" 400# include "state.h"
371#undef VAR 401#undef VAR
372} 402}
378 * not usually need a lot of stackspace. 408 * not usually need a lot of stackspace.
379 */ 409 */
380static void 410static void
381coro_init_stacks () 411coro_init_stacks ()
382{ 412{
383 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 413 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
384 PL_curstackinfo->si_type = PERLSI_MAIN; 414 PL_curstackinfo->si_type = PERLSI_MAIN;
385 PL_curstack = PL_curstackinfo->si_stack; 415 PL_curstack = PL_curstackinfo->si_stack;
386 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 416 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
387 417
388 PL_stack_base = AvARRAY(PL_curstack); 418 PL_stack_base = AvARRAY(PL_curstack);
389 PL_stack_sp = PL_stack_base; 419 PL_stack_sp = PL_stack_base;
390 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 420 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
391 421
392 New(50,PL_tmps_stack,96,SV*); 422 New(50,PL_tmps_stack,128,SV*);
393 PL_tmps_floor = -1; 423 PL_tmps_floor = -1;
394 PL_tmps_ix = -1; 424 PL_tmps_ix = -1;
395 PL_tmps_max = 96; 425 PL_tmps_max = 128;
396 426
397 New(54,PL_markstack,16,I32); 427 New(54,PL_markstack,32,I32);
398 PL_markstack_ptr = PL_markstack; 428 PL_markstack_ptr = PL_markstack;
399 PL_markstack_max = PL_markstack + 16; 429 PL_markstack_max = PL_markstack + 32;
400 430
401#ifdef SET_MARK_OFFSET 431#ifdef SET_MARK_OFFSET
402 SET_MARK_OFFSET; 432 SET_MARK_OFFSET;
403#endif 433#endif
404 434
405 New(54,PL_scopestack,16,I32); 435 New(54,PL_scopestack,32,I32);
406 PL_scopestack_ix = 0; 436 PL_scopestack_ix = 0;
407 PL_scopestack_max = 16; 437 PL_scopestack_max = 32;
408 438
409 New(54,PL_savestack,96,ANY); 439 New(54,PL_savestack,64,ANY);
410 PL_savestack_ix = 0; 440 PL_savestack_ix = 0;
411 PL_savestack_max = 96; 441 PL_savestack_max = 64;
412 442
413#if !PERL_VERSION_ATLEAST (5,9,0) 443#if !PERL_VERSION_ATLEAST (5,9,0)
414 New(54,PL_retstack,8,OP*); 444 New(54,PL_retstack,16,OP*);
415 PL_retstack_ix = 0; 445 PL_retstack_ix = 0;
416 PL_retstack_max = 8; 446 PL_retstack_max = 16;
417#endif 447#endif
418} 448}
419 449
420/* 450/*
421 * destroy the stacks, the callchain etc... 451 * destroy the stacks, the callchain etc...
474 * emulate part of the perl startup here. 504 * emulate part of the perl startup here.
475 */ 505 */
476 506
477 coro_init_stacks (); 507 coro_init_stacks ();
478 508
479 PL_curcop = 0; 509 PL_curcop = &PL_compiling;
480 PL_in_eval = 0; 510 PL_in_eval = EVAL_NULL;
481 PL_curpm = 0; 511 PL_curpm = 0;
512 PL_localizing = 0;
513 PL_dirty = 0;
514 PL_restartop = 0;
482 515
483 { 516 {
484 dSP; 517 dSP;
485 LOGOP myop; 518 LOGOP myop;
486 519
487 /* I have no idea why this is needed, but it is */
488 PUSHMARK (SP);
489
490 SvREFCNT_dec (GvAV (PL_defgv)); 520 SvREFCNT_dec (GvAV (PL_defgv));
491 GvAV (PL_defgv) = coro->args; coro->args = 0; 521 GvAV (PL_defgv) = coro->args; coro->args = 0;
492 522
493 Zero (&myop, 1, LOGOP); 523 Zero (&myop, 1, LOGOP);
494 myop.op_next = Nullop; 524 myop.op_next = Nullop;
495 myop.op_flags = OPf_WANT_VOID; 525 myop.op_flags = OPf_WANT_VOID;
496 526
497 PL_op = (OP *)&myop;
498
499 PUSHMARK (SP); 527 PUSHMARK (SP);
500 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 528 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
501 PUTBACK; 529 PUTBACK;
530 PL_op = (OP *)&myop;
502 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 531 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
503 SPAGAIN; 532 SPAGAIN;
504
505 ENTER; /* necessary e.g. for dounwind */
506 } 533 }
534
535 ENTER; /* necessary e.g. for dounwind */
507} 536}
508 537
509static void 538static void
510free_coro_mortal () 539free_coro_mortal ()
511{ 540{
514 SvREFCNT_dec (coro_mortal); 543 SvREFCNT_dec (coro_mortal);
515 coro_mortal = 0; 544 coro_mortal = 0;
516 } 545 }
517} 546}
518 547
548/* inject a fake call to Coro::State::_cctx_init into the execution */
519static void NOINLINE 549static void NOINLINE
520prepare_cctx (coro_cctx *cctx) 550prepare_cctx (coro_cctx *cctx)
521{ 551{
522 dSP; 552 dSP;
523 LOGOP myop; 553 LOGOP myop;
524 554
525 Zero (&myop, 1, LOGOP); 555 Zero (&myop, 1, LOGOP);
526 myop.op_next = PL_op; 556 myop.op_next = PL_op;
527 myop.op_flags = OPf_WANT_VOID; 557 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
528
529 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
530 558
531 PUSHMARK (SP); 559 PUSHMARK (SP);
560 EXTEND (SP, 2);
561 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
532 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 562 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
533 PUTBACK; 563 PUTBACK;
564 PL_op = (OP *)&myop;
534 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
535 SPAGAIN; 566 SPAGAIN;
536} 567}
537 568
538static void 569static void
539coro_run (void *arg) 570coro_run (void *arg)
540{ 571{
541 /* coro_run is the alternative epilogue of transfer() */ 572 /* coro_run is the alternative tail of transfer(), so unlock here. */
542 UNLOCK; 573 UNLOCK;
543 574
544 /* 575 /*
545 * this is a _very_ stripped down perl interpreter ;) 576 * this is a _very_ stripped down perl interpreter ;)
546 */ 577 */
548 579
549 /* inject call to cctx_init */ 580 /* inject call to cctx_init */
550 prepare_cctx ((coro_cctx *)arg); 581 prepare_cctx ((coro_cctx *)arg);
551 582
552 /* somebody will hit me for both perl_run and PL_restartop */ 583 /* somebody will hit me for both perl_run and PL_restartop */
584 PL_restartop = PL_op;
553 perl_run (PL_curinterp); 585 perl_run (PL_curinterp);
554 586
555 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 587 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
556 abort (); 588 abort ();
557} 589}
566 New (0, cctx, 1, coro_cctx); 598 New (0, cctx, 1, coro_cctx);
567 599
568#if HAVE_MMAP 600#if HAVE_MMAP
569 601
570 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
571 /* mmap suppsedly does allocate-on-write for us */ 603 /* mmap supposedly does allocate-on-write for us */
572 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 604 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
573 605
574 if (cctx->sptr == (void *)-1) 606 if (cctx->sptr == (void *)-1)
575 { 607 {
576 perror ("FATAL: unable to mmap stack for coroutine"); 608 perror ("FATAL: unable to mmap stack for coroutine");
605 637
606 return cctx; 638 return cctx;
607} 639}
608 640
609static void 641static void
610cctx_free (coro_cctx *cctx) 642cctx_destroy (coro_cctx *cctx)
611{ 643{
612 if (!cctx) 644 if (!cctx)
613 return; 645 return;
614 646
615 --cctx_count; 647 --cctx_count;
632{ 664{
633 coro_cctx *cctx; 665 coro_cctx *cctx;
634 666
635 if (cctx_first) 667 if (cctx_first)
636 { 668 {
637 --cctx_idle;
638 cctx = cctx_first; 669 cctx = cctx_first;
639 cctx_first = cctx->next; 670 cctx_first = cctx->next;
671 --cctx_idle;
640 } 672 }
641 else 673 else
642 { 674 {
643 cctx = cctx_new (); 675 cctx = cctx_new ();
644 PL_op = PL_op->op_next; 676 PL_op = PL_op->op_next;
648} 680}
649 681
650static void 682static void
651cctx_put (coro_cctx *cctx) 683cctx_put (coro_cctx *cctx)
652{ 684{
685 /* free another cctx if overlimit */
686 if (cctx_idle >= MAX_IDLE_CCTX)
687 {
688 coro_cctx *first = cctx_first;
689 cctx_first = first->next;
690 --cctx_idle;
691
692 assert (!first->inuse);
693 cctx_destroy (first);
694 }
695
653 ++cctx_idle; 696 ++cctx_idle;
654 cctx->next = cctx_first; 697 cctx->next = cctx_first;
655 cctx_first = cctx; 698 cctx_first = cctx;
656} 699}
657 700
658/* never call directly, always through the coro_state_transfer global variable */ 701/* never call directly, always through the coro_state_transfer global variable */
659static void NOINLINE 702static void NOINLINE
660transfer (struct coro *prev, struct coro *next, int flags) 703transfer (struct coro *prev, struct coro *next)
661{ 704{
662 dSTACKLEVEL; 705 dSTACKLEVEL;
663 706
664 /* sometimes transfer is only called to set idle_sp */ 707 /* sometimes transfer is only called to set idle_sp */
665 if (flags == TRANSFER_SET_STACKLEVEL) 708 if (!next)
666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 709 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
667 else if (prev != next) 710 else if (prev != next)
668 { 711 {
669 coro_cctx *prev__cctx; 712 coro_cctx *prev__cctx;
670 713
714 if (prev->flags & CF_NEW)
715 {
716 /* create a new empty context */
717 Newz (0, prev->cctx, 1, coro_cctx);
718 prev->cctx->inuse = 1;
719 prev->flags &= ~CF_NEW;
720 prev->flags |= CF_RUNNING;
721 }
722
723 /*TODO: must not croak here */
724 if (!prev->flags & CF_RUNNING)
725 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
726
727 if (next->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
729
730 prev->flags &= ~CF_RUNNING;
731 next->flags |= CF_RUNNING;
732
671 LOCK; 733 LOCK;
672 734
673 if (next->mainstack) 735 if (next->flags & CF_NEW)
674 { 736 {
675 /* coroutine already started */ 737 /* need to start coroutine */
676 SAVE (prev, flags); 738 next->flags &= ~CF_NEW;
677 LOAD (next); 739 /* first get rid of the old state */
740 save_perl (prev);
741 /* setup coroutine call */
742 setup_coro (next);
743 /* need a new stack */
744 assert (!next->stack);
678 } 745 }
679 else 746 else
680 { 747 {
681 /* need to start coroutine */ 748 /* coroutine already started */
682 /* first get rid of the old state */ 749 save_perl (prev);
683 SAVE (prev, -1); 750 load_perl (next);
684 /* setup coroutine call */
685 setup_coro (next);
686 /* need a stack */
687 next->cctx = 0;
688 } 751 }
689
690 if (!prev->cctx)
691 /* create a new empty context */
692 Newz (0, prev->cctx, 1, coro_cctx);
693 752
694 prev__cctx = prev->cctx; 753 prev__cctx = prev->cctx;
695 754
696 /* possibly "free" the cctx */ 755 /* possibly "free" the cctx */
697 if (prev__cctx->idle_sp == STACKLEVEL) 756 if (prev__cctx->idle_sp == STACKLEVEL)
698 { 757 {
758 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
759 assert (PL_top_env == prev__cctx->top_env);
760
761 prev->cctx = 0;
762
699 cctx_put (prev__cctx); 763 cctx_put (prev__cctx);
700 prev->cctx = 0; 764 prev__cctx->inuse = 0;
701 } 765 }
702 766
703 if (!next->cctx) 767 if (!next->cctx)
768 {
704 next->cctx = cctx_get (); 769 next->cctx = cctx_get ();
770 assert (!next->cctx->inuse);
771 next->cctx->inuse = 1;
772 }
705 773
706 if (prev__cctx != next->cctx) 774 if (prev__cctx != next->cctx)
707 { 775 {
708 prev__cctx->top_env = PL_top_env; 776 prev__cctx->top_env = PL_top_env;
709 PL_top_env = next->cctx->top_env; 777 PL_top_env = next->cctx->top_env;
717} 785}
718 786
719struct transfer_args 787struct transfer_args
720{ 788{
721 struct coro *prev, *next; 789 struct coro *prev, *next;
722 int flags;
723}; 790};
724 791
725#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 792#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
726 793
727static void 794static void
728coro_state_destroy (struct coro *coro) 795coro_state_destroy (struct coro *coro)
729{ 796{
730 if (coro->refcnt--) 797 if (coro->refcnt--)
731 return; 798 return;
732 799
733 if (coro->mainstack && coro->mainstack != main_mainstack) 800 if (coro->mainstack && coro->mainstack != main_mainstack)
734 { 801 {
735 struct coro temp; 802 struct coro temp;
803 Zero (&temp, 1, struct coro);
804 temp.save = CORO_SAVE_ALL;
736 805
737 SAVE ((&temp), TRANSFER_SAVE_ALL); 806 if (coro->flags & CF_RUNNING)
738 LOAD (coro); 807 croak ("FATAL: tried to destroy currently running coroutine");
808
809 save_perl (&temp);
810 load_perl (coro);
739 811
740 coro_destroy_stacks (); 812 coro_destroy_stacks ();
741 813
742 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 814 load_perl (&temp); /* this will get rid of defsv etc.. */
743 815
744 coro->mainstack = 0; 816 coro->mainstack = 0;
745 } 817 }
746 818
747 cctx_free (coro->cctx); 819 cctx_destroy (coro->cctx);
748 SvREFCNT_dec (coro->args); 820 SvREFCNT_dec (coro->args);
749 Safefree (coro); 821 Safefree (coro);
750} 822}
751 823
752static int 824static int
802 assert (mg->mg_type == PERL_MAGIC_ext); 874 assert (mg->mg_type == PERL_MAGIC_ext);
803 return (struct coro *)mg->mg_ptr; 875 return (struct coro *)mg->mg_ptr;
804} 876}
805 877
806static void 878static void
807prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 879prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
808{ 880{
809 ta->prev = SvSTATE (prev); 881 ta->prev = SvSTATE (prev_sv);
810 ta->next = SvSTATE (next); 882 ta->next = SvSTATE (next_sv);
811 ta->flags = flags;
812} 883}
813 884
814static void 885static void
815api_transfer (SV *prev, SV *next, int flags) 886api_transfer (SV *prev_sv, SV *next_sv)
816{ 887{
817 dTHX;
818 struct transfer_args ta; 888 struct transfer_args ta;
819 889
820 prepare_transfer (&ta, prev, next, flags); 890 prepare_transfer (&ta, prev_sv, next_sv);
821 TRANSFER (ta); 891 TRANSFER (ta);
892}
893
894static int
895api_save (SV *coro_sv, int new_save)
896{
897 struct coro *coro = SvSTATE (coro_sv);
898 int old_save = coro->save;
899
900 if (new_save >= 0)
901 coro->save = new_save;
902
903 return old_save;
822} 904}
823 905
824/** Coro ********************************************************************/ 906/** Coro ********************************************************************/
825 907
826#define PRIO_MAX 3 908#define PRIO_MAX 3
829#define PRIO_LOW -1 911#define PRIO_LOW -1
830#define PRIO_IDLE -3 912#define PRIO_IDLE -3
831#define PRIO_MIN -4 913#define PRIO_MIN -4
832 914
833/* for Coro.pm */ 915/* for Coro.pm */
834static GV *coro_current, *coro_idle; 916static SV *coro_current;
835static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 917static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
836static int coro_nready; 918static int coro_nready;
837 919
838static void 920static void
839coro_enq (SV *sv) 921coro_enq (SV *coro_sv)
840{ 922{
841 int prio;
842
843 if (SvTYPE (sv) != SVt_PVHV)
844 croak ("Coro::ready tried to enqueue something that is not a coroutine");
845
846 prio = SvSTATE (sv)->prio;
847
848 av_push (coro_ready [prio - PRIO_MIN], sv); 923 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
849 coro_nready++; 924 coro_nready++;
850} 925}
851 926
852static SV * 927static SV *
853coro_deq (int min_prio) 928coro_deq (int min_prio)
866 } 941 }
867 942
868 return 0; 943 return 0;
869} 944}
870 945
871static void 946static int
872api_ready (SV *coro) 947api_ready (SV *coro_sv)
873{ 948{
874 dTHX; 949 struct coro *coro;
875 950
876 if (SvROK (coro)) 951 if (SvROK (coro_sv))
877 coro = SvRV (coro); 952 coro_sv = SvRV (coro_sv);
953
954 coro = SvSTATE (coro_sv);
955
956 if (coro->flags & CF_READY)
957 return 0;
958
959#if 0 /* this is actually harmless */
960 if (coro->flags & CF_RUNNING)
961 croak ("Coro::ready called on currently running coroutine");
962#endif
963
964 coro->flags |= CF_READY;
878 965
879 LOCK; 966 LOCK;
880 coro_enq (SvREFCNT_inc (coro)); 967 coro_enq (SvREFCNT_inc (coro_sv));
881 UNLOCK; 968 UNLOCK;
969
970 return 1;
971}
972
973static int
974api_is_ready (SV *coro_sv)
975{
976 return !!SvSTATE (coro_sv)->flags & CF_READY;
882} 977}
883 978
884static void 979static void
885prepare_schedule (struct transfer_args *ta) 980prepare_schedule (struct transfer_args *ta)
886{ 981{
887 SV *current, *prev, *next; 982 SV *prev, *next;
888
889 current = GvSV (coro_current);
890 983
891 for (;;) 984 for (;;)
892 { 985 {
893 LOCK; 986 LOCK;
894 next = coro_deq (PRIO_MIN); 987 next = coro_deq (PRIO_MIN);
903 ENTER; 996 ENTER;
904 SAVETMPS; 997 SAVETMPS;
905 998
906 PUSHMARK (SP); 999 PUSHMARK (SP);
907 PUTBACK; 1000 PUTBACK;
908 call_sv (GvSV (coro_idle), G_DISCARD); 1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
909 1002
910 FREETMPS; 1003 FREETMPS;
911 LEAVE; 1004 LEAVE;
912 } 1005 }
913 } 1006 }
914 1007
915 prev = SvRV (current); 1008 prev = SvRV (coro_current);
916 SvRV (current) = next; 1009 SvRV_set (coro_current, next);
917 1010
918 /* free this only after the transfer */ 1011 /* free this only after the transfer */
919 LOCK; 1012 LOCK;
920 free_coro_mortal (); 1013 free_coro_mortal ();
921 UNLOCK; 1014 UNLOCK;
922 coro_mortal = prev; 1015 coro_mortal = prev;
923 1016
1017 assert (!SvROK(prev));//D
1018 assert (!SvROK(next));//D
1019
924 ta->prev = SvSTATE (prev); 1020 ta->prev = SvSTATE (prev);
925 ta->next = SvSTATE (next); 1021 ta->next = SvSTATE (next);
926 ta->flags = TRANSFER_SAVE_ALL; 1022
1023 assert (ta->next->flags & CF_READY);
1024 ta->next->flags &= ~CF_READY;
927} 1025}
928 1026
929static void 1027static void
930prepare_cede (struct transfer_args *ta) 1028prepare_cede (struct transfer_args *ta)
931{ 1029{
932 LOCK; 1030 api_ready (coro_current);
933 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
934 UNLOCK;
935 1031
936 prepare_schedule (ta); 1032 prepare_schedule (ta);
937} 1033}
938 1034
939static void 1035static void
940api_schedule (void) 1036api_schedule (void)
941{ 1037{
942 dTHX;
943 struct transfer_args ta; 1038 struct transfer_args ta;
944 1039
945 prepare_schedule (&ta); 1040 prepare_schedule (&ta);
946 TRANSFER (ta); 1041 TRANSFER (ta);
947} 1042}
948 1043
949static void 1044static void
950api_cede (void) 1045api_cede (void)
951{ 1046{
952 dTHX;
953 struct transfer_args ta; 1047 struct transfer_args ta;
954 1048
955 prepare_cede (&ta); 1049 prepare_cede (&ta);
956 TRANSFER (ta); 1050 TRANSFER (ta);
957} 1051}
967#endif 1061#endif
968 BOOT_PAGESIZE; 1062 BOOT_PAGESIZE;
969 1063
970 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1064 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
971 1065
972 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1066 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
973 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1067 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
974 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1068 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1069 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1070 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
975 1071
976 main_mainstack = PL_mainstack; 1072 main_mainstack = PL_mainstack;
977 1073
978 coroapi.ver = CORO_API_VERSION; 1074 coroapi.ver = CORO_API_VERSION;
979 coroapi.transfer = api_transfer; 1075 coroapi.transfer = api_transfer;
989 HV *hv; 1085 HV *hv;
990 int i; 1086 int i;
991 1087
992 Newz (0, coro, 1, struct coro); 1088 Newz (0, coro, 1, struct coro);
993 coro->args = newAV (); 1089 coro->args = newAV ();
1090 coro->save = CORO_SAVE_ALL;
1091 coro->flags = CF_NEW;
994 1092
995 hv = newHV (); 1093 hv = newHV ();
996 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1094 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
997 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1095 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
998 1096
999 for (i = 1; i < items; i++) 1097 for (i = 1; i < items; i++)
1000 av_push (coro->args, newSVsv (ST (i))); 1098 av_push (coro->args, newSVsv (ST (i)));
1001} 1099}
1002 OUTPUT: 1100 OUTPUT:
1101 RETVAL
1102
1103int
1104save (SV *coro, int new_save = -1)
1105 CODE:
1106 RETVAL = api_save (coro, new_save);
1107 OUTPUT:
1003 RETVAL 1108 RETVAL
1004 1109
1005void 1110void
1006_set_stacklevel (...) 1111_set_stacklevel (...)
1007 ALIAS: 1112 ALIAS:
1008 Coro::State::transfer = 1 1113 Coro::State::transfer = 1
1009 Coro::schedule = 2 1114 Coro::schedule = 2
1010 Coro::cede = 3 1115 Coro::cede = 3
1011 Coro::Cont::yield = 4
1012 CODE: 1116 CODE:
1013{ 1117{
1014 struct transfer_args ta; 1118 struct transfer_args ta;
1015 1119
1016 switch (ix) 1120 switch (ix)
1017 { 1121 {
1018 case 0: 1122 case 0:
1019 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1123 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1020 ta.next = 0; 1124 ta.next = 0;
1021 ta.flags = TRANSFER_SET_STACKLEVEL;
1022 break; 1125 break;
1023 1126
1024 case 1: 1127 case 1:
1025 if (items != 3) 1128 if (items != 2)
1026 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1129 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1027 1130
1028 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1131 prepare_transfer (&ta, ST (0), ST (1));
1029 break; 1132 break;
1030 1133
1031 case 2: 1134 case 2:
1032 prepare_schedule (&ta); 1135 prepare_schedule (&ta);
1033 break; 1136 break;
1034 1137
1035 case 3: 1138 case 3:
1036 prepare_cede (&ta); 1139 prepare_cede (&ta);
1037 break; 1140 break;
1038
1039 case 4:
1040 {
1041 SV *yieldstack;
1042 SV *sv;
1043 AV *defav = GvAV (PL_defgv);
1044
1045 yieldstack = *hv_fetch (
1046 (HV *)SvRV (GvSV (coro_current)),
1047 "yieldstack", sizeof ("yieldstack") - 1,
1048 0
1049 );
1050
1051 /* set up @_ -- ugly */
1052 av_clear (defav);
1053 av_fill (defav, items - 1);
1054 while (items--)
1055 av_store (defav, items, SvREFCNT_inc (ST(items)));
1056
1057 sv = av_pop ((AV *)SvRV (yieldstack));
1058 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1059 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1060 ta.flags = 0;
1061 SvREFCNT_dec (sv);
1062 }
1063 break;
1064
1065 } 1141 }
1066 1142
1067 TRANSFER (ta); 1143 TRANSFER (ta);
1068} 1144}
1069 1145
1113 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1189 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1114 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1190 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1115 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1191 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1116 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1192 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1117 1193
1118 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1194 coro_current = get_sv ("Coro::current", FALSE);
1119 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1195 SvREADONLY_on (coro_current);
1120 1196
1121 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1197 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1122 coro_ready[i] = newAV (); 1198 coro_ready[i] = newAV ();
1123 1199
1124 { 1200 {
1125 SV *sv = perl_get_sv("Coro::API", 1); 1201 SV *sv = perl_get_sv("Coro::API", 1);
1126 1202
1127 coroapi.schedule = api_schedule; 1203 coroapi.schedule = api_schedule;
1204 coroapi.save = api_save;
1128 coroapi.cede = api_cede; 1205 coroapi.cede = api_cede;
1129 coroapi.ready = api_ready; 1206 coroapi.ready = api_ready;
1207 coroapi.is_ready = api_is_ready;
1130 coroapi.nready = &coro_nready; 1208 coroapi.nready = &coro_nready;
1131 coroapi.current = coro_current; 1209 coroapi.current = coro_current;
1132 1210
1133 GCoroAPI = &coroapi; 1211 GCoroAPI = &coroapi;
1134 sv_setiv (sv, (IV)&coroapi); 1212 sv_setiv (sv, (IV)&coroapi);
1135 SvREADONLY_on (sv); 1213 SvREADONLY_on (sv);
1136 } 1214 }
1137} 1215}
1216
1217void
1218_set_current (SV *current)
1219 PROTOTYPE: $
1220 CODE:
1221 SvREFCNT_dec (SvRV (coro_current));
1222 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1138 1223
1139int 1224int
1140prio (Coro::State coro, int newprio = 0) 1225prio (Coro::State coro, int newprio = 0)
1141 ALIAS: 1226 ALIAS:
1142 nice = 1 1227 nice = 1
1154 1239
1155 coro->prio = newprio; 1240 coro->prio = newprio;
1156 } 1241 }
1157} 1242}
1158 1243
1159void 1244SV *
1160ready (SV *self) 1245ready (SV *self)
1161 PROTOTYPE: $ 1246 PROTOTYPE: $
1162 CODE: 1247 CODE:
1163 api_ready (self); 1248 RETVAL = boolSV (api_ready (self));
1249 OUTPUT:
1250 RETVAL
1251
1252SV *
1253is_ready (SV *self)
1254 PROTOTYPE: $
1255 CODE:
1256 RETVAL = boolSV (api_is_ready (self));
1257 OUTPUT:
1258 RETVAL
1164 1259
1165int 1260int
1166nready (...) 1261nready (...)
1167 PROTOTYPE: 1262 PROTOTYPE:
1168 CODE: 1263 CODE:
1174 1269
1175SV * 1270SV *
1176_get_state () 1271_get_state ()
1177 CODE: 1272 CODE:
1178{ 1273{
1179 struct { 1274 struct io_state *data;
1180 int errorno;
1181 int laststype;
1182 int laststatval;
1183 Stat_t statcache;
1184 } data;
1185 1275
1276 RETVAL = newSV (sizeof (struct io_state));
1277 data = (struct io_state *)SvPVX (RETVAL);
1278 SvCUR_set (RETVAL, sizeof (struct io_state));
1279 SvPOK_only (RETVAL);
1280
1186 data.errorno = errno; 1281 data->errorno = errno;
1187 data.laststype = PL_laststype; 1282 data->laststype = PL_laststype;
1188 data.laststatval = PL_laststatval; 1283 data->laststatval = PL_laststatval;
1189 data.statcache = PL_statcache; 1284 data->statcache = PL_statcache;
1190
1191 RETVAL = newSVpvn ((char *)&data, sizeof data);
1192} 1285}
1193 OUTPUT: 1286 OUTPUT:
1194 RETVAL 1287 RETVAL
1195 1288
1196void 1289void
1197_set_state (char *data_) 1290_set_state (char *data_)
1198 PROTOTYPE: $ 1291 PROTOTYPE: $
1199 CODE: 1292 CODE:
1200{ 1293{
1201 struct { 1294 struct io_state *data = (void *)data_;
1202 int errorno;
1203 int laststype;
1204 int laststatval;
1205 Stat_t statcache;
1206 } *data = (void *)data_;
1207 1295
1208 errno = data->errorno; 1296 errno = data->errorno;
1209 PL_laststype = data->laststype; 1297 PL_laststype = data->laststype;
1210 PL_laststatval = data->laststatval; 1298 PL_laststatval = data->laststatval;
1211 PL_statcache = data->statcache; 1299 PL_statcache = data->statcache;
1212} 1300}
1301

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines