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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines