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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines