… | |
… | |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if PERL_VERSION < 6 |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
14 | (PERL_REVISION > (a) \ |
|
|
15 | || (PERL_REVISION == (a) \ |
|
|
16 | && (PERL_VERSION > (b) \ |
|
|
17 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
18 | |
|
|
19 | #if !PERL_VERSION_ATLEAST (5,6,0) |
10 | # ifndef PL_ppaddr |
20 | # ifndef PL_ppaddr |
11 | # define PL_ppaddr ppaddr |
21 | # define PL_ppaddr ppaddr |
12 | # endif |
22 | # endif |
13 | # ifndef call_sv |
23 | # ifndef call_sv |
14 | # define call_sv perl_call_sv |
24 | # define call_sv perl_call_sv |
… | |
… | |
52 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
53 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
54 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
55 | static long pagesize; |
65 | static long pagesize; |
56 | # else |
66 | # else |
57 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
58 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
59 | #endif |
72 | #endif |
60 | |
73 | |
61 | /* The next macro should declare a variable stacklevel that contains and approximation |
74 | /* The next macro should declare a variable stacklevel that contains and approximation |
62 | * to the current C stack pointer. Its property is that it changes with each call |
75 | * to the current C stack pointer. Its property is that it changes with each call |
63 | * and should be unique. */ |
76 | * and should be unique. */ |
64 | #define dSTACKLEVEL int stacklevel |
77 | #define dSTACKLEVEL int stacklevel |
65 | #define STACKLEVEL ((void *)&stacklevel) |
78 | #define STACKLEVEL ((void *)&stacklevel) |
66 | |
79 | |
67 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
80 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
|
|
81 | |
|
|
82 | #if __GNUC__ >= 3 |
|
|
83 | # define attribute(x) __attribute__(x) |
|
|
84 | #else |
|
|
85 | # define attribute(x) |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | #define NOINLINE attribute ((noinline)) |
68 | |
89 | |
69 | #include "CoroAPI.h" |
90 | #include "CoroAPI.h" |
70 | |
91 | |
71 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
72 | |
93 | |
… | |
… | |
82 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
83 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
84 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
85 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
86 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
87 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
88 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
89 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
90 | |
114 | |
91 | /* the stack */ |
115 | /* the stack */ |
92 | void *sptr; |
116 | void *sptr; |
93 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
94 | |
118 | |
95 | /* cpu state */ |
119 | /* cpu state */ |
96 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
97 | JMPENV *top_env; |
121 | JMPENV *top_env; |
98 | coro_context cctx; |
122 | coro_context cctx; |
99 | } coro_stack; |
123 | |
|
|
124 | #if USE_VALGRIND |
|
|
125 | int valgrind_id; |
|
|
126 | #endif |
|
|
127 | } coro_cctx; |
100 | |
128 | |
101 | /* this is a structure representing a perl-level coroutine */ |
129 | /* this is a structure representing a perl-level coroutine */ |
102 | struct coro { |
130 | struct coro { |
103 | /* the c coroutine allocated to this perl coroutine, if any */ |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
104 | coro_stack *stack; |
132 | coro_cctx *cctx; |
105 | |
133 | |
106 | /* data associated with this coroutine (initial args) */ |
134 | /* data associated with this coroutine (initial args) */ |
107 | AV *args; |
135 | AV *args; |
108 | int refcnt; |
136 | int refcnt; |
109 | |
137 | |
… | |
… | |
159 | AV *padlist = CvPADLIST (cv); |
187 | AV *padlist = CvPADLIST (cv); |
160 | AV *newpadlist, *newpad; |
188 | AV *newpadlist, *newpad; |
161 | |
189 | |
162 | newpadlist = newAV (); |
190 | newpadlist = newAV (); |
163 | AvREAL_off (newpadlist); |
191 | AvREAL_off (newpadlist); |
164 | #if PERL_VERSION < 9 |
192 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
193 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
194 | #else |
165 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
195 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
166 | #else |
|
|
167 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
168 | #endif |
196 | #endif |
169 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
197 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
170 | --AvFILLp (padlist); |
198 | --AvFILLp (padlist); |
171 | |
199 | |
172 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
200 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
291 | PL_scopestack_ix = c->scopestack_ix; |
319 | PL_scopestack_ix = c->scopestack_ix; |
292 | PL_scopestack_max = c->scopestack_max; |
320 | PL_scopestack_max = c->scopestack_max; |
293 | PL_savestack = c->savestack; |
321 | PL_savestack = c->savestack; |
294 | PL_savestack_ix = c->savestack_ix; |
322 | PL_savestack_ix = c->savestack_ix; |
295 | PL_savestack_max = c->savestack_max; |
323 | PL_savestack_max = c->savestack_max; |
296 | #if PERL_VERSION < 9 |
324 | #if !PERL_VERSION_ATLEAST (5,9,0) |
297 | PL_retstack = c->retstack; |
325 | PL_retstack = c->retstack; |
298 | PL_retstack_ix = c->retstack_ix; |
326 | PL_retstack_ix = c->retstack_ix; |
299 | PL_retstack_max = c->retstack_max; |
327 | PL_retstack_max = c->retstack_max; |
300 | #endif |
328 | #endif |
301 | PL_curpm = c->curpm; |
329 | PL_curpm = c->curpm; |
… | |
… | |
418 | c->scopestack_ix = PL_scopestack_ix; |
446 | c->scopestack_ix = PL_scopestack_ix; |
419 | c->scopestack_max = PL_scopestack_max; |
447 | c->scopestack_max = PL_scopestack_max; |
420 | c->savestack = PL_savestack; |
448 | c->savestack = PL_savestack; |
421 | c->savestack_ix = PL_savestack_ix; |
449 | c->savestack_ix = PL_savestack_ix; |
422 | c->savestack_max = PL_savestack_max; |
450 | c->savestack_max = PL_savestack_max; |
423 | #if PERL_VERSION < 9 |
451 | #if !PERL_VERSION_ATLEAST (5,9,0) |
424 | c->retstack = PL_retstack; |
452 | c->retstack = PL_retstack; |
425 | c->retstack_ix = PL_retstack_ix; |
453 | c->retstack_ix = PL_retstack_ix; |
426 | c->retstack_max = PL_retstack_max; |
454 | c->retstack_max = PL_retstack_max; |
427 | #endif |
455 | #endif |
428 | c->curpm = PL_curpm; |
456 | c->curpm = PL_curpm; |
… | |
… | |
466 | |
494 | |
467 | New(54,PL_savestack,96,ANY); |
495 | New(54,PL_savestack,96,ANY); |
468 | PL_savestack_ix = 0; |
496 | PL_savestack_ix = 0; |
469 | PL_savestack_max = 96; |
497 | PL_savestack_max = 96; |
470 | |
498 | |
471 | #if PERL_VERSION < 9 |
499 | #if !PERL_VERSION_ATLEAST (5,9,0) |
472 | New(54,PL_retstack,8,OP*); |
500 | New(54,PL_retstack,8,OP*); |
473 | PL_retstack_ix = 0; |
501 | PL_retstack_ix = 0; |
474 | PL_retstack_max = 8; |
502 | PL_retstack_max = 8; |
475 | #endif |
503 | #endif |
476 | } |
504 | } |
477 | |
505 | |
478 | /* |
506 | /* |
479 | * destroy the stacks, the callchain etc... |
507 | * destroy the stacks, the callchain etc... |
480 | */ |
508 | */ |
481 | static void |
509 | static void |
482 | destroy_stacks() |
510 | coro_destroy_stacks () |
483 | { |
511 | { |
484 | if (!IN_DESTRUCT) |
512 | if (!IN_DESTRUCT) |
485 | { |
513 | { |
486 | /* is this ugly, I ask? */ |
514 | /* is this ugly, I ask? */ |
487 | LEAVE_SCOPE (0); |
515 | LEAVE_SCOPE (0); |
… | |
… | |
518 | |
546 | |
519 | Safefree (PL_tmps_stack); |
547 | Safefree (PL_tmps_stack); |
520 | Safefree (PL_markstack); |
548 | Safefree (PL_markstack); |
521 | Safefree (PL_scopestack); |
549 | Safefree (PL_scopestack); |
522 | Safefree (PL_savestack); |
550 | Safefree (PL_savestack); |
523 | #if PERL_VERSION < 9 |
551 | #if !PERL_VERSION_ATLEAST (5,9,0) |
524 | Safefree (PL_retstack); |
552 | Safefree (PL_retstack); |
525 | #endif |
553 | #endif |
526 | } |
554 | } |
527 | |
555 | |
528 | static void |
556 | static void |
529 | setup_coro (struct coro *coro) |
557 | setup_coro (struct coro *coro) |
530 | { |
558 | { |
531 | /* |
559 | /* |
532 | * emulate part of the perl startup here. |
560 | * emulate part of the perl startup here. |
533 | */ |
561 | */ |
534 | dTHX; |
|
|
535 | dSP; |
|
|
536 | UNOP myop; |
|
|
537 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
538 | |
562 | |
539 | coro_init_stacks (); |
563 | coro_init_stacks (); |
|
|
564 | |
540 | /*PL_curcop = 0;*/ |
565 | PL_curcop = 0; |
541 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
566 | PL_in_eval = 0; |
|
|
567 | PL_curpm = 0; |
|
|
568 | |
|
|
569 | { |
|
|
570 | dSP; |
|
|
571 | LOGOP myop; |
|
|
572 | |
|
|
573 | /* I have no idea why this is needed, but it is */ |
|
|
574 | PUSHMARK (SP); |
|
|
575 | |
542 | SvREFCNT_dec (GvAV (PL_defgv)); |
576 | SvREFCNT_dec (GvAV (PL_defgv)); |
543 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
577 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
544 | |
578 | |
545 | SPAGAIN; |
|
|
546 | |
|
|
547 | Zero (&myop, 1, UNOP); |
579 | Zero (&myop, 1, LOGOP); |
548 | myop.op_next = Nullop; |
580 | myop.op_next = Nullop; |
549 | myop.op_flags = OPf_WANT_VOID; |
581 | myop.op_flags = OPf_WANT_VOID; |
550 | |
582 | |
551 | PL_op = (OP *)&myop; |
583 | PL_op = (OP *)&myop; |
552 | |
584 | |
553 | PUSHMARK(SP); |
585 | PUSHMARK (SP); |
554 | XPUSHs (sub_init); |
586 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
555 | PUTBACK; |
587 | PUTBACK; |
556 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
588 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
557 | SPAGAIN; |
589 | SPAGAIN; |
558 | |
590 | |
559 | ENTER; /* necessary e.g. for dounwind */ |
591 | ENTER; /* necessary e.g. for dounwind */ |
|
|
592 | } |
560 | } |
593 | } |
561 | |
594 | |
562 | static void |
595 | static void |
563 | free_coro_mortal () |
596 | free_coro_mortal () |
564 | { |
597 | { |
… | |
… | |
567 | SvREFCNT_dec (coro_mortal); |
600 | SvREFCNT_dec (coro_mortal); |
568 | coro_mortal = 0; |
601 | coro_mortal = 0; |
569 | } |
602 | } |
570 | } |
603 | } |
571 | |
604 | |
572 | static void |
605 | static void NOINLINE |
573 | prepare_cctx (coro_stack *cctx) |
606 | prepare_cctx (coro_cctx *cctx) |
574 | { |
607 | { |
575 | dSP; |
608 | dSP; |
576 | UNOP myop; |
609 | LOGOP myop; |
577 | |
610 | |
578 | Zero (&myop, 1, UNOP); |
611 | Zero (&myop, 1, LOGOP); |
579 | myop.op_next = PL_op; |
612 | myop.op_next = PL_op; |
580 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
613 | myop.op_flags = OPf_WANT_VOID; |
581 | |
614 | |
|
|
615 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
616 | |
582 | PUSHMARK(SP); |
617 | PUSHMARK (SP); |
583 | EXTEND (SP, 2); |
|
|
584 | PUSHs (newSViv (PTR2IV (cctx))); |
|
|
585 | PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
618 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
586 | PUTBACK; |
619 | PUTBACK; |
587 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
620 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
588 | SPAGAIN; |
621 | SPAGAIN; |
589 | } |
622 | } |
590 | |
623 | |
591 | static void |
624 | static void |
592 | coro_run (void *arg) |
625 | coro_run (void *arg) |
593 | { |
626 | { |
|
|
627 | /* coro_run is the alternative epilogue of transfer() */ |
|
|
628 | UNLOCK; |
|
|
629 | |
594 | /* |
630 | /* |
595 | * this is a _very_ stripped down perl interpreter ;) |
631 | * this is a _very_ stripped down perl interpreter ;) |
596 | */ |
632 | */ |
597 | UNLOCK; |
|
|
598 | |
|
|
599 | PL_top_env = &PL_start_env; |
633 | PL_top_env = &PL_start_env; |
|
|
634 | |
|
|
635 | /* inject call to cctx_init */ |
600 | prepare_cctx ((coro_stack *)arg); |
636 | prepare_cctx ((coro_cctx *)arg); |
601 | |
637 | |
602 | /* somebody will hit me for both perl_run and PL_restartop */ |
638 | /* somebody will hit me for both perl_run and PL_restartop */ |
603 | perl_run (PERL_GET_CONTEXT); |
639 | perl_run (PL_curinterp); |
604 | |
640 | |
605 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
641 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
606 | abort (); |
642 | abort (); |
607 | } |
643 | } |
608 | |
644 | |
609 | static coro_stack * |
645 | static coro_cctx * |
610 | stack_new () |
646 | cctx_new () |
611 | { |
647 | { |
612 | coro_stack *stack; |
648 | coro_cctx *cctx; |
613 | |
649 | |
|
|
650 | ++cctx_count; |
|
|
651 | |
614 | New (0, stack, 1, coro_stack); |
652 | New (0, cctx, 1, coro_cctx); |
615 | |
653 | |
616 | #if HAVE_MMAP |
654 | #if HAVE_MMAP |
617 | |
655 | |
618 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
656 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
619 | /* mmap suppsedly does allocate-on-write for us */ |
657 | /* mmap suppsedly does allocate-on-write for us */ |
620 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
658 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
621 | |
659 | |
622 | if (stack->sptr == (void *)-1) |
660 | if (cctx->sptr == (void *)-1) |
623 | { |
661 | { |
624 | perror ("FATAL: unable to mmap stack for coroutine"); |
662 | perror ("FATAL: unable to mmap stack for coroutine"); |
625 | _exit (EXIT_FAILURE); |
663 | _exit (EXIT_FAILURE); |
626 | } |
664 | } |
627 | |
665 | |
628 | # if STACKGUARD |
666 | # if STACKGUARD |
629 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
667 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
630 | # endif |
668 | # endif |
631 | |
669 | |
632 | #else |
670 | #else |
633 | |
671 | |
634 | stack->ssize = STACKSIZE * (long)sizeof (long); |
672 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
635 | New (0, stack->sptr, STACKSIZE, long); |
673 | New (0, cctx->sptr, STACKSIZE, long); |
636 | |
674 | |
637 | if (!stack->sptr) |
675 | if (!cctx->sptr) |
638 | { |
676 | { |
639 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
677 | perror ("FATAL: unable to malloc stack for coroutine"); |
640 | _exit (EXIT_FAILURE); |
678 | _exit (EXIT_FAILURE); |
641 | } |
679 | } |
642 | |
680 | |
643 | #endif |
681 | #endif |
644 | |
682 | |
|
|
683 | #if USE_VALGRIND |
|
|
684 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
685 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
686 | cctx->ssize + (char *)cctx->sptr |
|
|
687 | ); |
|
|
688 | #endif |
|
|
689 | |
645 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
690 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
646 | |
691 | |
647 | return stack; |
692 | return cctx; |
648 | } |
693 | } |
649 | |
694 | |
650 | static void |
695 | static void |
651 | stack_free (coro_stack *stack) |
696 | cctx_free (coro_cctx *cctx) |
652 | { |
697 | { |
653 | if (!stack) |
698 | if (!cctx) |
654 | return; |
699 | return; |
655 | |
700 | |
|
|
701 | --cctx_count; |
|
|
702 | |
|
|
703 | #if USE_VALGRIND |
|
|
704 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
705 | #endif |
|
|
706 | |
656 | #if HAVE_MMAP |
707 | #if HAVE_MMAP |
657 | munmap (stack->sptr, stack->ssize); |
708 | munmap (cctx->sptr, cctx->ssize); |
658 | #else |
709 | #else |
659 | Safefree (stack->sptr); |
710 | Safefree (cctx->sptr); |
660 | #endif |
711 | #endif |
661 | |
712 | |
662 | Safefree (stack); |
713 | Safefree (cctx); |
663 | } |
714 | } |
664 | |
715 | |
665 | static coro_stack *stack_first; |
|
|
666 | |
|
|
667 | static coro_stack * |
716 | static coro_cctx * |
668 | stack_get () |
717 | cctx_get () |
669 | { |
718 | { |
670 | coro_stack *stack; |
719 | coro_cctx *cctx; |
671 | |
720 | |
672 | if (stack_first) |
721 | if (cctx_first) |
673 | { |
722 | { |
674 | stack = stack_first; |
723 | --cctx_idle; |
|
|
724 | cctx = cctx_first; |
675 | stack_first = stack->next; |
725 | cctx_first = cctx->next; |
676 | } |
726 | } |
677 | else |
727 | else |
678 | { |
728 | { |
679 | stack = stack_new (); |
729 | cctx = cctx_new (); |
680 | PL_op = PL_op->op_next; |
730 | PL_op = PL_op->op_next; |
681 | } |
731 | } |
682 | |
732 | |
683 | return stack; |
733 | return cctx; |
684 | } |
734 | } |
685 | |
735 | |
686 | static void |
736 | static void |
687 | stack_put (coro_stack *stack) |
737 | cctx_put (coro_cctx *cctx) |
688 | { |
738 | { |
689 | stack->next = stack_first; |
739 | ++cctx_idle; |
690 | stack_first = stack; |
740 | cctx->next = cctx_first; |
|
|
741 | cctx_first = cctx; |
691 | } |
742 | } |
692 | |
743 | |
693 | /* never call directly, always through the coro_state_transfer global variable */ |
744 | /* never call directly, always through the coro_state_transfer global variable */ |
694 | static void |
745 | static void NOINLINE |
695 | transfer_impl (struct coro *prev, struct coro *next, int flags) |
746 | transfer (struct coro *prev, struct coro *next, int flags) |
696 | { |
747 | { |
697 | dSTACKLEVEL; |
748 | dSTACKLEVEL; |
698 | |
749 | |
699 | /* sometimes transfer is only called to set idle_sp */ |
750 | /* sometimes transfer is only called to set idle_sp */ |
700 | if (flags == TRANSFER_SET_STACKLEVEL) |
751 | if (flags == TRANSFER_SET_STACKLEVEL) |
701 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
752 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
702 | else if (prev != next) |
753 | else if (prev != next) |
703 | { |
754 | { |
704 | coro_stack *prev__stack; |
755 | coro_cctx *prev__cctx; |
705 | |
756 | |
706 | LOCK; |
757 | LOCK; |
707 | |
758 | |
708 | if (next->mainstack) |
759 | if (next->mainstack) |
709 | { |
760 | { |
… | |
… | |
717 | /* first get rid of the old state */ |
768 | /* first get rid of the old state */ |
718 | SAVE (prev, -1); |
769 | SAVE (prev, -1); |
719 | /* setup coroutine call */ |
770 | /* setup coroutine call */ |
720 | setup_coro (next); |
771 | setup_coro (next); |
721 | /* need a stack */ |
772 | /* need a stack */ |
722 | next->stack = 0; |
773 | next->cctx = 0; |
723 | } |
774 | } |
724 | |
775 | |
725 | if (!prev->stack) |
776 | if (!prev->cctx) |
726 | /* create a new empty context */ |
777 | /* create a new empty context */ |
727 | Newz (0, prev->stack, 1, coro_stack); |
778 | Newz (0, prev->cctx, 1, coro_cctx); |
728 | |
779 | |
729 | prev__stack = prev->stack; |
780 | prev__cctx = prev->cctx; |
730 | |
781 | |
731 | /* possibly "free" the stack */ |
782 | /* possibly "free" the cctx */ |
732 | if (prev__stack->idle_sp == STACKLEVEL) |
783 | if (prev__cctx->idle_sp == STACKLEVEL) |
733 | { |
784 | { |
734 | stack_put (prev__stack); |
785 | cctx_put (prev__cctx); |
735 | prev->stack = 0; |
786 | prev->cctx = 0; |
736 | } |
787 | } |
737 | |
788 | |
738 | if (!next->stack) |
789 | if (!next->cctx) |
739 | next->stack = stack_get (); |
790 | next->cctx = cctx_get (); |
740 | |
791 | |
741 | if (prev__stack != next->stack) |
792 | if (prev__cctx != next->cctx) |
742 | { |
793 | { |
743 | prev__stack->top_env = PL_top_env; |
794 | prev__cctx->top_env = PL_top_env; |
744 | PL_top_env = next->stack->top_env; |
795 | PL_top_env = next->cctx->top_env; |
745 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
796 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
746 | } |
797 | } |
747 | |
798 | |
748 | free_coro_mortal (); |
799 | free_coro_mortal (); |
749 | |
800 | |
750 | UNLOCK; |
801 | UNLOCK; |
751 | } |
802 | } |
752 | } |
803 | } |
753 | |
|
|
754 | /* use this function pointer to call the above function */ |
|
|
755 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
756 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
|
|
757 | void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
758 | |
804 | |
759 | struct transfer_args |
805 | struct transfer_args |
760 | { |
806 | { |
761 | struct coro *prev, *next; |
807 | struct coro *prev, *next; |
762 | int flags; |
808 | int flags; |
763 | }; |
809 | }; |
764 | |
810 | |
765 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
811 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
766 | |
812 | |
767 | static void |
813 | static void |
768 | coro_state_destroy (struct coro *coro) |
814 | coro_state_destroy (struct coro *coro) |
769 | { |
815 | { |
770 | if (coro->refcnt--) |
816 | if (coro->refcnt--) |
… | |
… | |
775 | struct coro temp; |
821 | struct coro temp; |
776 | |
822 | |
777 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
823 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
778 | LOAD (coro); |
824 | LOAD (coro); |
779 | |
825 | |
780 | destroy_stacks (); |
826 | coro_destroy_stacks (); |
781 | |
827 | |
782 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
828 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
783 | |
829 | |
784 | coro->mainstack = 0; |
830 | coro->mainstack = 0; |
785 | } |
831 | } |
786 | |
832 | |
787 | stack_free (coro->stack); |
833 | cctx_free (coro->cctx); |
788 | SvREFCNT_dec (coro->args); |
834 | SvREFCNT_dec (coro->args); |
789 | Safefree (coro); |
835 | Safefree (coro); |
790 | } |
836 | } |
791 | |
837 | |
792 | static int |
838 | static int |
… | |
… | |
808 | ++coro->refcnt; |
854 | ++coro->refcnt; |
809 | |
855 | |
810 | return 0; |
856 | return 0; |
811 | } |
857 | } |
812 | |
858 | |
813 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
859 | static MGVTBL coro_state_vtbl = { |
|
|
860 | 0, 0, 0, 0, |
|
|
861 | coro_state_clear, |
|
|
862 | 0, |
|
|
863 | #ifdef MGf_DUP |
|
|
864 | coro_state_dup, |
|
|
865 | #else |
|
|
866 | # define MGf_DUP 0 |
|
|
867 | #endif |
|
|
868 | }; |
814 | |
869 | |
815 | static struct coro * |
870 | static struct coro * |
816 | SvSTATE (SV *coro) |
871 | SvSTATE (SV *coro) |
817 | { |
872 | { |
818 | HV *stash; |
873 | HV *stash; |
… | |
… | |
1027 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1082 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1028 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1083 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1029 | |
1084 | |
1030 | for (i = 1; i < items; i++) |
1085 | for (i = 1; i < items; i++) |
1031 | av_push (coro->args, newSVsv (ST (i))); |
1086 | av_push (coro->args, newSVsv (ST (i))); |
1032 | |
|
|
1033 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1034 | /*coro->stack = 0;*/ |
|
|
1035 | } |
1087 | } |
1036 | OUTPUT: |
1088 | OUTPUT: |
1037 | RETVAL |
1089 | RETVAL |
1038 | |
1090 | |
1039 | void |
1091 | void |
… | |
… | |
1048 | struct transfer_args ta; |
1100 | struct transfer_args ta; |
1049 | |
1101 | |
1050 | switch (ix) |
1102 | switch (ix) |
1051 | { |
1103 | { |
1052 | case 0: |
1104 | case 0: |
1053 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1105 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1054 | ta.next = 0; |
1106 | ta.next = 0; |
1055 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1107 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1056 | break; |
1108 | break; |
1057 | |
1109 | |
1058 | case 1: |
1110 | case 1: |
… | |
… | |
1112 | ++coro_src->refcnt; |
1164 | ++coro_src->refcnt; |
1113 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1165 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1114 | } |
1166 | } |
1115 | |
1167 | |
1116 | void |
1168 | void |
1117 | _nonlocal_goto (IV nextop) |
|
|
1118 | CODE: |
|
|
1119 | /* uuh, somebody will kill me again for this */ |
|
|
1120 | PL_op->op_next = INT2PTR (OP *, nextop); |
|
|
1121 | |
|
|
1122 | void |
|
|
1123 | _exit (code) |
1169 | _exit (code) |
1124 | int code |
1170 | int code |
1125 | PROTOTYPE: $ |
1171 | PROTOTYPE: $ |
1126 | CODE: |
1172 | CODE: |
1127 | _exit (code); |
1173 | _exit (code); |
|
|
1174 | |
|
|
1175 | int |
|
|
1176 | cctx_count () |
|
|
1177 | CODE: |
|
|
1178 | RETVAL = cctx_count; |
|
|
1179 | OUTPUT: |
|
|
1180 | RETVAL |
|
|
1181 | |
|
|
1182 | int |
|
|
1183 | cctx_idle () |
|
|
1184 | CODE: |
|
|
1185 | RETVAL = cctx_idle; |
|
|
1186 | OUTPUT: |
|
|
1187 | RETVAL |
1128 | |
1188 | |
1129 | MODULE = Coro::State PACKAGE = Coro |
1189 | MODULE = Coro::State PACKAGE = Coro |
1130 | |
1190 | |
1131 | BOOT: |
1191 | BOOT: |
1132 | { |
1192 | { |