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