1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #if 1 |
5 | #include "libcoro/coro.c" |
6 | # define CHK(x) (void *)0 |
6 | |
|
|
7 | #ifdef HAVE_MMAP |
|
|
8 | # include <unistd.h> |
|
|
9 | # include <sys/mman.h> |
|
|
10 | # ifndef MAP_ANON |
|
|
11 | # ifdef MAP_ANONYMOUS |
|
|
12 | # define MAP_ANON MAP_ANONYMOUS |
|
|
13 | # else |
|
|
14 | # undef HAVE_MMAP |
|
|
15 | # endif |
|
|
16 | # endif |
|
|
17 | #endif |
|
|
18 | |
|
|
19 | #define MAY_FLUSH /* increases codesize */ |
|
|
20 | |
|
|
21 | /* perl-related */ |
|
|
22 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
|
|
23 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
|
|
24 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
|
|
25 | /* c-related */ |
|
|
26 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
|
|
27 | #ifdef CORO_LAZY_STACK |
|
|
28 | # define TRANSFER_LAZY_STACK 0x00000010 |
7 | #else |
29 | #else |
8 | # define CHK(x) if (!(x)) croak("FATAL, CHK: " #x) |
30 | # define TRANSFER_LAZY_STACK 0x00000000 |
9 | #endif |
31 | #endif |
10 | |
32 | |
11 | #define MAY_FLUSH /* increases codesize */ |
33 | #define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \ |
|
|
34 | |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT) |
12 | |
35 | |
13 | #define SAVE_DEFAV 0x00000001 |
36 | #define SUB_INIT "Coro::State::initialize" |
14 | #define SAVE_DEFSV 0x00000002 |
37 | #define UCORO_STATE "_coro_state" |
15 | #define SAVE_ERRSV 0x00000004 |
|
|
16 | |
38 | |
17 | #define SAVE_ALL -1 |
39 | /* The next macro should delcare a variable stacklevel that contains and approximation |
|
|
40 | * to the current C stack pointer. It's property is that it changes with each call |
|
|
41 | * and should be unique. */ |
|
|
42 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
|
|
43 | |
|
|
44 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
45 | |
|
|
46 | /* this is actually not only the c stack but also c registers etc... */ |
|
|
47 | typedef struct { |
|
|
48 | int refcnt; /* pointer reference counter */ |
|
|
49 | int usecnt; /* shared by how many coroutines */ |
|
|
50 | int gencnt; /* generation counter */ |
|
|
51 | |
|
|
52 | coro_context cctx; |
|
|
53 | |
|
|
54 | void *sptr; |
|
|
55 | long ssize; /* positive == mmap, otherwise malloc */ |
|
|
56 | } coro_stack; |
18 | |
57 | |
19 | struct coro { |
58 | struct coro { |
|
|
59 | /* the optional C context */ |
|
|
60 | coro_stack *stack; |
|
|
61 | void *cursp; |
|
|
62 | int gencnt; |
|
|
63 | |
20 | /* optionally saved, might be zero */ |
64 | /* optionally saved, might be zero */ |
21 | AV *defav; |
65 | AV *defav; |
22 | SV *defsv; |
66 | SV *defsv; |
23 | SV *errsv; |
67 | SV *errsv; |
24 | |
68 | |
… | |
… | |
49 | I32 savestack_max; |
93 | I32 savestack_max; |
50 | OP **retstack; |
94 | OP **retstack; |
51 | I32 retstack_ix; |
95 | I32 retstack_ix; |
52 | I32 retstack_max; |
96 | I32 retstack_max; |
53 | COP *curcop; |
97 | COP *curcop; |
|
|
98 | JMPENV start_env; |
|
|
99 | JMPENV *top_env; |
54 | |
100 | |
55 | /* data associated with this coroutine (initial args) */ |
101 | /* data associated with this coroutine (initial args) */ |
56 | AV *args; |
102 | AV *args; |
57 | }; |
103 | }; |
58 | |
104 | |
59 | typedef struct coro *Coro__State; |
105 | typedef struct coro *Coro__State; |
60 | typedef struct coro *Coro__State_or_hashref; |
106 | typedef struct coro *Coro__State_or_hashref; |
61 | |
107 | |
|
|
108 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
109 | static HV *coro_state_stash; |
|
|
110 | static SV *ucoro_state_sv; |
|
|
111 | static U32 ucoro_state_hash; |
62 | static HV *padlist_cache; |
112 | static HV *padlist_cache; |
63 | |
113 | |
64 | /* mostly copied from op.c:cv_clone2 */ |
114 | /* mostly copied from op.c:cv_clone2 */ |
65 | STATIC AV * |
115 | STATIC AV * |
66 | clone_padlist (AV *protopadlist) |
116 | clone_padlist (AV *protopadlist) |
… | |
… | |
229 | #endif |
279 | #endif |
230 | |
280 | |
231 | #define SB do { |
281 | #define SB do { |
232 | #define SE } while (0) |
282 | #define SE } while (0) |
233 | |
283 | |
234 | #define LOAD(state) SB load_state(aTHX_ state); SPAGAIN; SE |
284 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
235 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ state,flags); SE |
285 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
236 | |
286 | |
237 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
287 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
238 | |
288 | |
239 | static void |
289 | static void |
240 | load_state(pTHX_ Coro__State c) |
290 | load_state(pTHX_ Coro__State c) |
… | |
… | |
264 | PL_savestack_max = c->savestack_max; |
314 | PL_savestack_max = c->savestack_max; |
265 | PL_retstack = c->retstack; |
315 | PL_retstack = c->retstack; |
266 | PL_retstack_ix = c->retstack_ix; |
316 | PL_retstack_ix = c->retstack_ix; |
267 | PL_retstack_max = c->retstack_max; |
317 | PL_retstack_max = c->retstack_max; |
268 | PL_curcop = c->curcop; |
318 | PL_curcop = c->curcop; |
|
|
319 | PL_start_env = c->start_env; |
|
|
320 | PL_top_env = c->top_env; |
269 | |
321 | |
270 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
322 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
271 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
323 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
272 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
324 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
273 | |
325 | |
… | |
… | |
300 | save_state(pTHX_ Coro__State c, int flags) |
352 | save_state(pTHX_ Coro__State c, int flags) |
301 | { |
353 | { |
302 | { |
354 | { |
303 | dSP; |
355 | dSP; |
304 | I32 cxix = cxstack_ix; |
356 | I32 cxix = cxstack_ix; |
|
|
357 | PERL_CONTEXT *ccstk = cxstack; |
305 | PERL_SI *top_si = PL_curstackinfo; |
358 | PERL_SI *top_si = PL_curstackinfo; |
306 | PERL_CONTEXT *ccstk = cxstack; |
|
|
307 | |
359 | |
308 | /* |
360 | /* |
309 | * the worst thing you can imagine happens first - we have to save |
361 | * the worst thing you can imagine happens first - we have to save |
310 | * (and reinitialize) all cv's in the whole callchain :( |
362 | * (and reinitialize) all cv's in the whole callchain :( |
311 | */ |
363 | */ |
312 | |
364 | |
313 | PUSHs (Nullsv); |
365 | PUSHs (Nullsv); |
314 | /* this loop was inspired by pp_caller */ |
366 | /* this loop was inspired by pp_caller */ |
315 | for (;;) |
367 | for (;;) |
316 | { |
368 | { |
317 | do |
369 | do |
318 | { |
370 | { |
319 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
371 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
320 | |
372 | |
321 | if (CxTYPE(cx) == CXt_SUB) |
373 | if (CxTYPE(cx) == CXt_SUB) |
322 | { |
374 | { |
… | |
… | |
361 | } |
413 | } |
362 | |
414 | |
363 | PUTBACK; |
415 | PUTBACK; |
364 | } |
416 | } |
365 | |
417 | |
366 | c->defav = flags & SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
418 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
367 | c->defsv = flags & SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
419 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
368 | c->errsv = flags & SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
420 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
421 | |
|
|
422 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
423 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
424 | if (c->defav) |
|
|
425 | av_reify (c->defav); |
369 | |
426 | |
370 | c->dowarn = PL_dowarn; |
427 | c->dowarn = PL_dowarn; |
371 | |
428 | |
372 | c->curstackinfo = PL_curstackinfo; |
429 | c->curstackinfo = PL_curstackinfo; |
373 | c->curstack = PL_curstack; |
430 | c->curstack = PL_curstack; |
… | |
… | |
392 | c->savestack_max = PL_savestack_max; |
449 | c->savestack_max = PL_savestack_max; |
393 | c->retstack = PL_retstack; |
450 | c->retstack = PL_retstack; |
394 | c->retstack_ix = PL_retstack_ix; |
451 | c->retstack_ix = PL_retstack_ix; |
395 | c->retstack_max = PL_retstack_max; |
452 | c->retstack_max = PL_retstack_max; |
396 | c->curcop = PL_curcop; |
453 | c->curcop = PL_curcop; |
|
|
454 | c->start_env = PL_start_env; |
|
|
455 | c->top_env = PL_top_env; |
|
|
456 | } |
|
|
457 | |
|
|
458 | /* |
|
|
459 | * allocate various perl stacks. This is an exact copy |
|
|
460 | * of perl.c:init_stacks, except that it uses less memory |
|
|
461 | * on the assumption that coroutines do not usually need |
|
|
462 | * a lot of stackspace. |
|
|
463 | */ |
|
|
464 | STATIC void |
|
|
465 | coro_init_stacks (pTHX) |
|
|
466 | { |
|
|
467 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
|
|
468 | PL_curstackinfo->si_type = PERLSI_MAIN; |
|
|
469 | PL_curstack = PL_curstackinfo->si_stack; |
|
|
470 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
|
|
471 | |
|
|
472 | PL_stack_base = AvARRAY(PL_curstack); |
|
|
473 | PL_stack_sp = PL_stack_base; |
|
|
474 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
|
|
475 | |
|
|
476 | New(50,PL_tmps_stack,96,SV*); |
|
|
477 | PL_tmps_floor = -1; |
|
|
478 | PL_tmps_ix = -1; |
|
|
479 | PL_tmps_max = 96; |
|
|
480 | |
|
|
481 | New(54,PL_markstack,16,I32); |
|
|
482 | PL_markstack_ptr = PL_markstack; |
|
|
483 | PL_markstack_max = PL_markstack + 16; |
|
|
484 | |
|
|
485 | SET_MARK_OFFSET; |
|
|
486 | |
|
|
487 | New(54,PL_scopestack,16,I32); |
|
|
488 | PL_scopestack_ix = 0; |
|
|
489 | PL_scopestack_max = 16; |
|
|
490 | |
|
|
491 | New(54,PL_savestack,96,ANY); |
|
|
492 | PL_savestack_ix = 0; |
|
|
493 | PL_savestack_max = 96; |
|
|
494 | |
|
|
495 | New(54,PL_retstack,8,OP*); |
|
|
496 | PL_retstack_ix = 0; |
|
|
497 | PL_retstack_max = 8; |
397 | } |
498 | } |
398 | |
499 | |
399 | /* |
500 | /* |
400 | * destroy the stacks, the callchain etc... |
501 | * destroy the stacks, the callchain etc... |
401 | * still there is a memleak of 128 bytes... |
502 | * still there is a memleak of 128 bytes... |
… | |
… | |
437 | Safefree(PL_scopestack); |
538 | Safefree(PL_scopestack); |
438 | Safefree(PL_savestack); |
539 | Safefree(PL_savestack); |
439 | Safefree(PL_retstack); |
540 | Safefree(PL_retstack); |
440 | } |
541 | } |
441 | |
542 | |
442 | #define SUB_INIT "Coro::State::_newcoro" |
543 | static void |
|
|
544 | allocate_stack (Coro__State ctx, int alloc) |
|
|
545 | { |
|
|
546 | coro_stack *stack; |
|
|
547 | |
|
|
548 | New (0, stack, 1, coro_stack); |
|
|
549 | |
|
|
550 | stack->refcnt = 1; |
|
|
551 | stack->usecnt = 1; |
|
|
552 | stack->gencnt = ctx->gencnt = 0; |
|
|
553 | if (alloc) |
|
|
554 | { |
|
|
555 | #ifdef HAVE_MMAP |
|
|
556 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
|
|
557 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
|
|
558 | if (stack->sptr == (void *)-1) |
|
|
559 | #endif |
|
|
560 | { |
|
|
561 | /*FIXME*//*D*//* reasonable stack size! */ |
|
|
562 | stack->ssize = -4096 * sizeof (long); |
|
|
563 | New (0, stack->sptr, 4096, long); |
|
|
564 | } |
|
|
565 | } |
|
|
566 | else |
|
|
567 | stack->sptr = 0; |
|
|
568 | |
|
|
569 | ctx->stack = stack; |
|
|
570 | } |
|
|
571 | |
|
|
572 | static void |
|
|
573 | deallocate_stack (Coro__State ctx) |
|
|
574 | { |
|
|
575 | coro_stack *stack = ctx->stack; |
|
|
576 | |
|
|
577 | ctx->stack = 0; |
|
|
578 | |
|
|
579 | if (stack) |
|
|
580 | { |
|
|
581 | if (!--stack->refcnt) |
|
|
582 | { |
|
|
583 | #ifdef HAVE_MMAP |
|
|
584 | if (stack->ssize > 0 && stack->sptr) |
|
|
585 | munmap (stack->sptr, stack->ssize); |
|
|
586 | else |
|
|
587 | #else |
|
|
588 | Safefree (stack->sptr); |
|
|
589 | #endif |
|
|
590 | Safefree (stack); |
|
|
591 | } |
|
|
592 | else if (ctx->gencnt == stack->gencnt) |
|
|
593 | --stack->usecnt; |
|
|
594 | } |
|
|
595 | } |
|
|
596 | |
|
|
597 | static void |
|
|
598 | setup_coro (void *arg) |
|
|
599 | { |
|
|
600 | /* |
|
|
601 | * emulate part of the perl startup here. |
|
|
602 | */ |
|
|
603 | dSP; |
|
|
604 | Coro__State ctx = (Coro__State)arg; |
|
|
605 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
|
|
606 | |
|
|
607 | coro_init_stacks (aTHX); |
|
|
608 | JMPENV_BOOTSTRAP; |
|
|
609 | SPAGAIN; |
|
|
610 | |
|
|
611 | /*PL_curcop = 0;*/ |
|
|
612 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
613 | GvAV (PL_defgv) = ctx->args; |
|
|
614 | |
|
|
615 | if (ctx->stack) |
|
|
616 | { |
|
|
617 | ctx->cursp = 0; |
|
|
618 | |
|
|
619 | PUSHMARK(SP); |
|
|
620 | PUTBACK; |
|
|
621 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
|
|
622 | croak ("FATAL: CCTXT coroutine returned!"); |
|
|
623 | } |
|
|
624 | else |
|
|
625 | { |
|
|
626 | UNOP myop; |
|
|
627 | |
|
|
628 | PL_op = (OP *)&myop; |
|
|
629 | |
|
|
630 | Zero(&myop, 1, UNOP); |
|
|
631 | myop.op_next = Nullop; |
|
|
632 | myop.op_flags = OPf_WANT_VOID; |
|
|
633 | |
|
|
634 | PUSHMARK(SP); |
|
|
635 | XPUSHs (sub_init); |
|
|
636 | /* |
|
|
637 | * the next line is slightly wrong, as PL_op->op_next |
|
|
638 | * is actually being executed so we skip the first op. |
|
|
639 | * that doesn't matter, though, since it is only |
|
|
640 | * pp_nextstate and we never return... |
|
|
641 | * ah yes, and I don't care anyways ;) |
|
|
642 | */ |
|
|
643 | PUTBACK; |
|
|
644 | PL_op = pp_entersub(); |
|
|
645 | SPAGAIN; |
|
|
646 | |
|
|
647 | ENTER; /* necessary e.g. for dounwind */ |
|
|
648 | } |
|
|
649 | } |
|
|
650 | |
|
|
651 | static void |
|
|
652 | continue_coro (void *arg) |
|
|
653 | { |
|
|
654 | /* |
|
|
655 | * this is a _very_ stripped down perl interpreter ;) |
|
|
656 | */ |
|
|
657 | Coro__State ctx = (Coro__State)arg; |
|
|
658 | |
|
|
659 | ctx->cursp = 0; |
|
|
660 | PL_op = PL_op->op_next; |
|
|
661 | CALLRUNOPS(aTHX); |
|
|
662 | /*NORETURN*/ |
|
|
663 | abort (); |
|
|
664 | } |
|
|
665 | |
|
|
666 | STATIC void |
|
|
667 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
|
|
668 | { |
|
|
669 | dSP; |
|
|
670 | dSTACKLEVEL; |
|
|
671 | |
|
|
672 | if (prev != next) |
|
|
673 | { |
|
|
674 | if (next->mainstack) |
|
|
675 | { |
|
|
676 | SAVE (prev, flags); |
|
|
677 | LOAD (next); |
|
|
678 | |
|
|
679 | /* mark this state as in-use */ |
|
|
680 | next->mainstack = 0; |
|
|
681 | next->tmps_ix = -2; |
|
|
682 | |
|
|
683 | /* stacklevel changed? if yes, grab the stack for us! */ |
|
|
684 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
685 | { |
|
|
686 | if (!prev->stack) |
|
|
687 | allocate_stack (prev, 0); |
|
|
688 | else if (prev->cursp != stacklevel |
|
|
689 | && prev->stack->usecnt > 1) |
|
|
690 | { |
|
|
691 | prev->gencnt = ++prev->stack->gencnt; |
|
|
692 | prev->stack->usecnt = 1; |
|
|
693 | } |
|
|
694 | |
|
|
695 | /* has our stack been invalidated? */ |
|
|
696 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
697 | { |
|
|
698 | deallocate_stack (next); |
|
|
699 | allocate_stack (next, 1); |
|
|
700 | coro_create (&(next->stack->cctx), |
|
|
701 | continue_coro, (void *)next, |
|
|
702 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
703 | } |
|
|
704 | |
|
|
705 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
706 | } |
|
|
707 | |
|
|
708 | } |
|
|
709 | else if (next->tmps_ix == -2) |
|
|
710 | croak ("tried to transfer to running coroutine"); |
|
|
711 | else |
|
|
712 | { |
|
|
713 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
714 | |
|
|
715 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
716 | { |
|
|
717 | if (!prev->stack) |
|
|
718 | allocate_stack (prev, 0); |
|
|
719 | |
|
|
720 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
|
|
721 | { |
|
|
722 | setup_coro (next); |
|
|
723 | |
|
|
724 | prev->stack->refcnt++; |
|
|
725 | prev->stack->usecnt++; |
|
|
726 | next->stack = prev->stack; |
|
|
727 | next->gencnt = prev->gencnt; |
|
|
728 | } |
|
|
729 | else |
|
|
730 | { |
|
|
731 | allocate_stack (next, 1); |
|
|
732 | coro_create (&(next->stack->cctx), |
|
|
733 | setup_coro, (void *)next, |
|
|
734 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
735 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
736 | } |
|
|
737 | } |
|
|
738 | else |
|
|
739 | setup_coro (next); |
|
|
740 | } |
|
|
741 | } |
|
|
742 | |
|
|
743 | next->cursp = stacklevel; |
|
|
744 | } |
443 | |
745 | |
444 | MODULE = Coro::State PACKAGE = Coro::State |
746 | MODULE = Coro::State PACKAGE = Coro::State |
445 | |
747 | |
446 | PROTOTYPES: ENABLE |
748 | PROTOTYPES: ENABLE |
447 | |
749 | |
448 | BOOT: |
750 | BOOT: |
|
|
751 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
752 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
|
|
753 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
449 | HV * stash = gv_stashpvn("Coro::State", 10, TRUE); |
754 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
450 | |
755 | |
451 | newCONSTSUB (stash, "SAVE_DEFAV", newSViv (SAVE_DEFAV)); |
756 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
452 | newCONSTSUB (stash, "SAVE_DEFSV", newSViv (SAVE_DEFSV)); |
757 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
453 | newCONSTSUB (stash, "SAVE_ERRSV", newSViv (SAVE_ERRSV)); |
758 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
|
|
759 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
454 | |
760 | |
455 | if (!padlist_cache) |
761 | if (!padlist_cache) |
456 | padlist_cache = newHV (); |
762 | padlist_cache = newHV (); |
|
|
763 | |
|
|
764 | main_mainstack = PL_mainstack; |
|
|
765 | } |
457 | |
766 | |
458 | Coro::State |
767 | Coro::State |
459 | _newprocess(args) |
768 | _newprocess(args) |
460 | SV * args |
769 | SV * args |
461 | PROTOTYPE: $ |
770 | PROTOTYPE: $ |
… | |
… | |
465 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
774 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
466 | croak ("Coro::State::_newprocess expects an arrayref"); |
775 | croak ("Coro::State::_newprocess expects an arrayref"); |
467 | |
776 | |
468 | New (0, coro, 1, struct coro); |
777 | New (0, coro, 1, struct coro); |
469 | |
778 | |
|
|
779 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
470 | coro->mainstack = 0; /* actual work is done inside transfer */ |
780 | coro->mainstack = 0; /* actual work is done inside transfer */ |
471 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
781 | coro->stack = 0; |
472 | |
782 | |
473 | RETVAL = coro; |
783 | RETVAL = coro; |
474 | OUTPUT: |
784 | OUTPUT: |
475 | RETVAL |
785 | RETVAL |
476 | |
786 | |
477 | void |
787 | void |
478 | transfer(prev, next, flags = SAVE_DEFAV) |
788 | transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) |
479 | Coro::State_or_hashref prev |
789 | Coro::State_or_hashref prev |
480 | Coro::State_or_hashref next |
790 | Coro::State_or_hashref next |
481 | int flags |
791 | int flags |
|
|
792 | PROTOTYPE: @ |
482 | CODE: |
793 | CODE: |
483 | |
794 | transfer (aTHX_ prev, next, flags); |
484 | if (prev != next) |
|
|
485 | { |
|
|
486 | /* |
|
|
487 | * this could be done in newprocess which would lead to |
|
|
488 | * extremely elegant and fast (just SAVE/LOAD) |
|
|
489 | * code here, but lazy allocation of stacks has also |
|
|
490 | * some virtues and the overhead of the if() is nil. |
|
|
491 | */ |
|
|
492 | if (next->mainstack) |
|
|
493 | { |
|
|
494 | SAVE (prev, flags); |
|
|
495 | LOAD (next); |
|
|
496 | /* mark this state as in-use */ |
|
|
497 | next->mainstack = 0; |
|
|
498 | next->tmps_ix = -2; |
|
|
499 | } |
|
|
500 | else if (next->tmps_ix == -2) |
|
|
501 | { |
|
|
502 | croak ("tried to transfer to running coroutine"); |
|
|
503 | } |
|
|
504 | else |
|
|
505 | { |
|
|
506 | /* |
|
|
507 | * emulate part of the perl startup here. |
|
|
508 | */ |
|
|
509 | UNOP myop; |
|
|
510 | |
|
|
511 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
512 | |
|
|
513 | init_stacks (); /* from perl.c */ |
|
|
514 | SPAGAIN; |
|
|
515 | |
|
|
516 | PL_op = (OP *)&myop; |
|
|
517 | /*PL_curcop = 0;*/ |
|
|
518 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
519 | GvAV (PL_defgv) = next->args; |
|
|
520 | |
|
|
521 | Zero(&myop, 1, UNOP); |
|
|
522 | myop.op_next = Nullop; |
|
|
523 | myop.op_flags = OPf_WANT_VOID; |
|
|
524 | |
|
|
525 | PUSHMARK(SP); |
|
|
526 | XPUSHs ((SV*)get_cv(SUB_INIT, TRUE)); |
|
|
527 | /* |
|
|
528 | * the next line is slightly wrong, as PL_op->op_next |
|
|
529 | * is actually being executed so we skip the first op. |
|
|
530 | * that doesn't matter, though, since it is only |
|
|
531 | * pp_nextstate and we never return... |
|
|
532 | * ah yes, and I don't care anyways ;) |
|
|
533 | */ |
|
|
534 | PUTBACK; |
|
|
535 | PL_op = pp_entersub(aTHX); |
|
|
536 | SPAGAIN; |
|
|
537 | |
|
|
538 | ENTER; /* necessary e.g. for dounwind */ |
|
|
539 | } |
|
|
540 | } |
|
|
541 | |
795 | |
542 | void |
796 | void |
543 | DESTROY(coro) |
797 | DESTROY(coro) |
544 | Coro::State coro |
798 | Coro::State coro |
545 | CODE: |
799 | CODE: |
546 | |
800 | |
547 | if (coro->mainstack) |
801 | if (coro->mainstack && coro->mainstack != main_mainstack) |
548 | { |
802 | { |
549 | struct coro temp; |
803 | struct coro temp; |
550 | |
804 | |
551 | SAVE(aTHX_ (&temp), SAVE_ALL); |
805 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
552 | LOAD(aTHX_ coro); |
806 | LOAD(aTHX_ coro); |
553 | |
807 | |
554 | destroy_stacks (); |
808 | destroy_stacks (aTHX); |
555 | |
809 | |
556 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
810 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
811 | |
|
|
812 | coro->mainstack = 0; |
557 | } |
813 | } |
|
|
814 | |
|
|
815 | deallocate_stack (coro); |
558 | |
816 | |
559 | Safefree (coro); |
817 | Safefree (coro); |
560 | |
818 | |
561 | void |
819 | void |
562 | flush() |
820 | flush() |
563 | CODE: |
821 | CODE: |
564 | #ifdef MAY_FLUSH |
822 | #ifdef MAY_FLUSH |
565 | flush_padlist_cache (); |
823 | flush_padlist_cache (); |
566 | #endif |
824 | #endif |
567 | |
825 | |
|
|
826 | MODULE = Coro::State PACKAGE = Coro::Cont |
568 | |
827 | |
|
|
828 | # this is slightly dirty |
|
|
829 | |
|
|
830 | void |
|
|
831 | yield(...) |
|
|
832 | PROTOTYPE: @ |
|
|
833 | CODE: |
|
|
834 | static SV *returnstk; |
|
|
835 | SV *sv; |
|
|
836 | AV *defav = GvAV (PL_defgv); |
|
|
837 | struct coro *prev, *next; |
|
|
838 | |
|
|
839 | if (!returnstk) |
|
|
840 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
|
|
841 | |
|
|
842 | /* set up @_ -- ugly */ |
|
|
843 | av_clear (defav); |
|
|
844 | av_fill (defav, items - 1); |
|
|
845 | while (items--) |
|
|
846 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
847 | |
|
|
848 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
849 | sv = av_pop ((AV *)SvRV (returnstk)); |
|
|
850 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
|
|
851 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
|
|
852 | SvREFCNT_dec (sv); |
|
|
853 | |
|
|
854 | transfer(aTHX_ prev, next, 0); |
|
|
855 | |