… | |
… | |
3 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
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 | |
|
|
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
8 | |
12 | |
9 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
10 | (PERL_REVISION > (a) \ |
14 | (PERL_REVISION > (a) \ |
11 | || (PERL_REVISION == (a) \ |
15 | || (PERL_REVISION == (a) \ |
12 | && (PERL_VERSION > (b) \ |
16 | && (PERL_VERSION > (b) \ |
… | |
… | |
58 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
59 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
60 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
61 | static long pagesize; |
65 | static long pagesize; |
62 | # else |
66 | # else |
63 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
64 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
65 | #endif |
72 | #endif |
66 | |
73 | |
67 | /* 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 |
68 | * 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 |
69 | * and should be unique. */ |
76 | * and should be unique. */ |
… | |
… | |
96 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
97 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
98 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
99 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
100 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
101 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
102 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
103 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
104 | |
114 | |
105 | /* the stack */ |
115 | /* the stack */ |
106 | void *sptr; |
116 | void *sptr; |
107 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
108 | |
118 | |
109 | /* cpu state */ |
119 | /* cpu state */ |
110 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
111 | JMPENV *top_env; |
121 | JMPENV *top_env; |
112 | coro_context cctx; |
122 | coro_context cctx; |
113 | } coro_stack; |
123 | |
|
|
124 | #if USE_VALGRIND |
|
|
125 | int valgrind_id; |
|
|
126 | #endif |
|
|
127 | } coro_cctx; |
114 | |
128 | |
115 | /* this is a structure representing a perl-level coroutine */ |
129 | /* this is a structure representing a perl-level coroutine */ |
116 | struct coro { |
130 | struct coro { |
117 | /* the c coroutine allocated to this perl coroutine, if any */ |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
118 | coro_stack *stack; |
132 | coro_cctx *cctx; |
119 | |
133 | |
120 | /* data associated with this coroutine (initial args) */ |
134 | /* data associated with this coroutine (initial args) */ |
121 | AV *args; |
135 | AV *args; |
122 | int refcnt; |
136 | int refcnt; |
123 | |
137 | |
124 | /* optionally saved, might be zero */ |
138 | /* optionally saved, might be zero */ |
125 | AV *defav; |
139 | AV *defav; |
126 | SV *defsv; |
140 | SV *defsv; |
127 | SV *errsv; |
141 | SV *errsv; |
128 | |
142 | |
129 | /* saved global state not related to stacks */ |
143 | #define VAR(name,type) type name; |
130 | U8 dowarn; |
144 | # include "state.h" |
131 | I32 in_eval; |
145 | #undef VAR |
132 | |
|
|
133 | /* the stacks and related info (callchain etc..) */ |
|
|
134 | PERL_SI *curstackinfo; |
|
|
135 | AV *curstack; |
|
|
136 | AV *mainstack; |
|
|
137 | SV **stack_sp; |
|
|
138 | OP *op; |
|
|
139 | SV **curpad; |
|
|
140 | AV *comppad; |
|
|
141 | CV *compcv; |
|
|
142 | SV **stack_base; |
|
|
143 | SV **stack_max; |
|
|
144 | SV **tmps_stack; |
|
|
145 | I32 tmps_floor; |
|
|
146 | I32 tmps_ix; |
|
|
147 | I32 tmps_max; |
|
|
148 | I32 *markstack; |
|
|
149 | I32 *markstack_ptr; |
|
|
150 | I32 *markstack_max; |
|
|
151 | I32 *scopestack; |
|
|
152 | I32 scopestack_ix; |
|
|
153 | I32 scopestack_max; |
|
|
154 | ANY *savestack; |
|
|
155 | I32 savestack_ix; |
|
|
156 | I32 savestack_max; |
|
|
157 | OP **retstack; |
|
|
158 | I32 retstack_ix; |
|
|
159 | I32 retstack_max; |
|
|
160 | PMOP *curpm; |
|
|
161 | COP *curcop; |
|
|
162 | |
146 | |
163 | /* coro process data */ |
147 | /* coro process data */ |
164 | int prio; |
148 | int prio; |
165 | }; |
149 | }; |
166 | |
150 | |
… | |
… | |
279 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
263 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
280 | |
264 | |
281 | static void |
265 | static void |
282 | load_state(Coro__State c) |
266 | load_state(Coro__State c) |
283 | { |
267 | { |
284 | PL_dowarn = c->dowarn; |
268 | #define VAR(name,type) PL_ ## name = c->name; |
285 | PL_in_eval = c->in_eval; |
269 | # include "state.h" |
286 | |
270 | #undef VAR |
287 | PL_curstackinfo = c->curstackinfo; |
|
|
288 | PL_curstack = c->curstack; |
|
|
289 | PL_mainstack = c->mainstack; |
|
|
290 | PL_stack_sp = c->stack_sp; |
|
|
291 | PL_op = c->op; |
|
|
292 | PL_curpad = c->curpad; |
|
|
293 | PL_comppad = c->comppad; |
|
|
294 | PL_compcv = c->compcv; |
|
|
295 | PL_stack_base = c->stack_base; |
|
|
296 | PL_stack_max = c->stack_max; |
|
|
297 | PL_tmps_stack = c->tmps_stack; |
|
|
298 | PL_tmps_floor = c->tmps_floor; |
|
|
299 | PL_tmps_ix = c->tmps_ix; |
|
|
300 | PL_tmps_max = c->tmps_max; |
|
|
301 | PL_markstack = c->markstack; |
|
|
302 | PL_markstack_ptr = c->markstack_ptr; |
|
|
303 | PL_markstack_max = c->markstack_max; |
|
|
304 | PL_scopestack = c->scopestack; |
|
|
305 | PL_scopestack_ix = c->scopestack_ix; |
|
|
306 | PL_scopestack_max = c->scopestack_max; |
|
|
307 | PL_savestack = c->savestack; |
|
|
308 | PL_savestack_ix = c->savestack_ix; |
|
|
309 | PL_savestack_max = c->savestack_max; |
|
|
310 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
311 | PL_retstack = c->retstack; |
|
|
312 | PL_retstack_ix = c->retstack_ix; |
|
|
313 | PL_retstack_max = c->retstack_max; |
|
|
314 | #endif |
|
|
315 | PL_curpm = c->curpm; |
|
|
316 | PL_curcop = c->curcop; |
|
|
317 | |
271 | |
318 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
272 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
319 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
273 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
320 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
274 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
321 | |
275 | |
… | |
… | |
406 | |
360 | |
407 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
361 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
408 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
362 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
409 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
363 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
410 | |
364 | |
411 | c->dowarn = PL_dowarn; |
365 | #define VAR(name,type)c->name = PL_ ## name; |
412 | c->in_eval = PL_in_eval; |
366 | # include "state.h" |
413 | |
367 | #undef VAR |
414 | c->curstackinfo = PL_curstackinfo; |
|
|
415 | c->curstack = PL_curstack; |
|
|
416 | c->mainstack = PL_mainstack; |
|
|
417 | c->stack_sp = PL_stack_sp; |
|
|
418 | c->op = PL_op; |
|
|
419 | c->curpad = PL_curpad; |
|
|
420 | c->comppad = PL_comppad; |
|
|
421 | c->compcv = PL_compcv; |
|
|
422 | c->stack_base = PL_stack_base; |
|
|
423 | c->stack_max = PL_stack_max; |
|
|
424 | c->tmps_stack = PL_tmps_stack; |
|
|
425 | c->tmps_floor = PL_tmps_floor; |
|
|
426 | c->tmps_ix = PL_tmps_ix; |
|
|
427 | c->tmps_max = PL_tmps_max; |
|
|
428 | c->markstack = PL_markstack; |
|
|
429 | c->markstack_ptr = PL_markstack_ptr; |
|
|
430 | c->markstack_max = PL_markstack_max; |
|
|
431 | c->scopestack = PL_scopestack; |
|
|
432 | c->scopestack_ix = PL_scopestack_ix; |
|
|
433 | c->scopestack_max = PL_scopestack_max; |
|
|
434 | c->savestack = PL_savestack; |
|
|
435 | c->savestack_ix = PL_savestack_ix; |
|
|
436 | c->savestack_max = PL_savestack_max; |
|
|
437 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
438 | c->retstack = PL_retstack; |
|
|
439 | c->retstack_ix = PL_retstack_ix; |
|
|
440 | c->retstack_max = PL_retstack_max; |
|
|
441 | #endif |
|
|
442 | c->curpm = PL_curpm; |
|
|
443 | c->curcop = PL_curcop; |
|
|
444 | } |
368 | } |
445 | |
369 | |
446 | /* |
370 | /* |
447 | * allocate various perl stacks. This is an exact copy |
371 | * allocate various perl stacks. This is an exact copy |
448 | * of perl.c:init_stacks, except that it uses less memory |
372 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
491 | |
415 | |
492 | /* |
416 | /* |
493 | * destroy the stacks, the callchain etc... |
417 | * destroy the stacks, the callchain etc... |
494 | */ |
418 | */ |
495 | static void |
419 | static void |
496 | coro_destroy_stacks() |
420 | coro_destroy_stacks () |
497 | { |
421 | { |
498 | if (!IN_DESTRUCT) |
422 | if (!IN_DESTRUCT) |
499 | { |
423 | { |
500 | /* is this ugly, I ask? */ |
424 | /* is this ugly, I ask? */ |
501 | LEAVE_SCOPE (0); |
425 | LEAVE_SCOPE (0); |
… | |
… | |
567 | myop.op_flags = OPf_WANT_VOID; |
491 | myop.op_flags = OPf_WANT_VOID; |
568 | |
492 | |
569 | PL_op = (OP *)&myop; |
493 | PL_op = (OP *)&myop; |
570 | |
494 | |
571 | PUSHMARK (SP); |
495 | PUSHMARK (SP); |
572 | XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); |
496 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
573 | PUTBACK; |
497 | PUTBACK; |
574 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
498 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
575 | SPAGAIN; |
499 | SPAGAIN; |
576 | |
500 | |
577 | ENTER; /* necessary e.g. for dounwind */ |
501 | ENTER; /* necessary e.g. for dounwind */ |
… | |
… | |
587 | coro_mortal = 0; |
511 | coro_mortal = 0; |
588 | } |
512 | } |
589 | } |
513 | } |
590 | |
514 | |
591 | static void NOINLINE |
515 | static void NOINLINE |
592 | prepare_cctx (coro_stack *cctx) |
516 | prepare_cctx (coro_cctx *cctx) |
593 | { |
517 | { |
594 | dSP; |
518 | dSP; |
595 | LOGOP myop; |
519 | LOGOP myop; |
596 | |
520 | |
597 | Zero (&myop, 1, LOGOP); |
521 | Zero (&myop, 1, LOGOP); |
598 | myop.op_next = PL_op; |
522 | myop.op_next = PL_op; |
599 | myop.op_flags = OPf_WANT_VOID; |
523 | myop.op_flags = OPf_WANT_VOID; |
600 | |
524 | |
601 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); |
525 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
602 | |
526 | |
603 | PUSHMARK (SP); |
527 | PUSHMARK (SP); |
604 | XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
528 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
605 | PUTBACK; |
529 | PUTBACK; |
606 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
530 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
607 | SPAGAIN; |
531 | SPAGAIN; |
608 | } |
532 | } |
609 | |
533 | |
… | |
… | |
615 | |
539 | |
616 | /* |
540 | /* |
617 | * this is a _very_ stripped down perl interpreter ;) |
541 | * this is a _very_ stripped down perl interpreter ;) |
618 | */ |
542 | */ |
619 | PL_top_env = &PL_start_env; |
543 | PL_top_env = &PL_start_env; |
|
|
544 | |
620 | /* inject call to cctx_init */ |
545 | /* inject call to cctx_init */ |
621 | prepare_cctx ((coro_stack *)arg); |
546 | prepare_cctx ((coro_cctx *)arg); |
622 | |
547 | |
623 | /* somebody will hit me for both perl_run and PL_restartop */ |
548 | /* somebody will hit me for both perl_run and PL_restartop */ |
624 | perl_run (PL_curinterp); |
549 | perl_run (PL_curinterp); |
625 | |
550 | |
626 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
551 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
627 | abort (); |
552 | abort (); |
628 | } |
553 | } |
629 | |
554 | |
630 | static coro_stack * |
555 | static coro_cctx * |
631 | stack_new () |
556 | cctx_new () |
632 | { |
557 | { |
633 | coro_stack *stack; |
558 | coro_cctx *cctx; |
634 | |
559 | |
|
|
560 | ++cctx_count; |
|
|
561 | |
635 | New (0, stack, 1, coro_stack); |
562 | New (0, cctx, 1, coro_cctx); |
636 | |
563 | |
637 | #if HAVE_MMAP |
564 | #if HAVE_MMAP |
638 | |
565 | |
639 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
566 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
640 | /* mmap suppsedly does allocate-on-write for us */ |
567 | /* mmap suppsedly does allocate-on-write for us */ |
641 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
568 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
642 | |
569 | |
643 | if (stack->sptr == (void *)-1) |
570 | if (cctx->sptr == (void *)-1) |
644 | { |
571 | { |
645 | perror ("FATAL: unable to mmap stack for coroutine"); |
572 | perror ("FATAL: unable to mmap stack for coroutine"); |
646 | _exit (EXIT_FAILURE); |
573 | _exit (EXIT_FAILURE); |
647 | } |
574 | } |
648 | |
575 | |
649 | # if STACKGUARD |
576 | # if STACKGUARD |
650 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
577 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
651 | # endif |
578 | # endif |
652 | |
579 | |
653 | #else |
580 | #else |
654 | |
581 | |
655 | stack->ssize = STACKSIZE * (long)sizeof (long); |
582 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
656 | New (0, stack->sptr, STACKSIZE, long); |
583 | New (0, cctx->sptr, STACKSIZE, long); |
657 | |
584 | |
658 | if (!stack->sptr) |
585 | if (!cctx->sptr) |
659 | { |
586 | { |
660 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
587 | perror ("FATAL: unable to malloc stack for coroutine"); |
661 | _exit (EXIT_FAILURE); |
588 | _exit (EXIT_FAILURE); |
662 | } |
589 | } |
663 | |
590 | |
664 | #endif |
591 | #endif |
665 | |
592 | |
|
|
593 | #if USE_VALGRIND |
|
|
594 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
595 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
596 | cctx->ssize + (char *)cctx->sptr |
|
|
597 | ); |
|
|
598 | #endif |
|
|
599 | |
666 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
600 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
667 | |
601 | |
668 | return stack; |
602 | return cctx; |
669 | } |
603 | } |
670 | |
604 | |
671 | static void |
605 | static void |
672 | stack_free (coro_stack *stack) |
606 | cctx_free (coro_cctx *cctx) |
673 | { |
607 | { |
674 | if (!stack) |
608 | if (!cctx) |
675 | return; |
609 | return; |
676 | |
610 | |
|
|
611 | --cctx_count; |
|
|
612 | |
|
|
613 | #if USE_VALGRIND |
|
|
614 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
615 | #endif |
|
|
616 | |
677 | #if HAVE_MMAP |
617 | #if HAVE_MMAP |
678 | munmap (stack->sptr, stack->ssize); |
618 | munmap (cctx->sptr, cctx->ssize); |
679 | #else |
619 | #else |
680 | Safefree (stack->sptr); |
620 | Safefree (cctx->sptr); |
681 | #endif |
621 | #endif |
682 | |
622 | |
683 | Safefree (stack); |
623 | Safefree (cctx); |
684 | } |
624 | } |
685 | |
625 | |
686 | static coro_stack *stack_first; |
|
|
687 | static int cctx_count, cctx_idle; |
|
|
688 | |
|
|
689 | static coro_stack * |
626 | static coro_cctx * |
690 | stack_get () |
627 | cctx_get () |
691 | { |
628 | { |
692 | coro_stack *stack; |
629 | coro_cctx *cctx; |
693 | |
630 | |
694 | if (stack_first) |
631 | if (cctx_first) |
695 | { |
632 | { |
696 | --cctx_idle; |
633 | --cctx_idle; |
697 | stack = stack_first; |
634 | cctx = cctx_first; |
698 | stack_first = stack->next; |
635 | cctx_first = cctx->next; |
699 | } |
636 | } |
700 | else |
637 | else |
701 | { |
638 | { |
702 | ++cctx_count; |
639 | cctx = cctx_new (); |
703 | stack = stack_new (); |
|
|
704 | PL_op = PL_op->op_next; |
640 | PL_op = PL_op->op_next; |
705 | } |
641 | } |
706 | |
642 | |
707 | return stack; |
643 | return cctx; |
708 | } |
644 | } |
709 | |
645 | |
710 | static void |
646 | static void |
711 | stack_put (coro_stack *stack) |
647 | cctx_put (coro_cctx *cctx) |
712 | { |
648 | { |
713 | ++cctx_idle; |
649 | ++cctx_idle; |
714 | stack->next = stack_first; |
650 | cctx->next = cctx_first; |
715 | stack_first = stack; |
651 | cctx_first = cctx; |
716 | } |
652 | } |
717 | |
653 | |
718 | /* never call directly, always through the coro_state_transfer global variable */ |
654 | /* never call directly, always through the coro_state_transfer global variable */ |
719 | static void NOINLINE |
655 | static void NOINLINE |
720 | transfer (struct coro *prev, struct coro *next, int flags) |
656 | transfer (struct coro *prev, struct coro *next, int flags) |
721 | { |
657 | { |
722 | dSTACKLEVEL; |
658 | dSTACKLEVEL; |
723 | |
659 | |
724 | /* sometimes transfer is only called to set idle_sp */ |
660 | /* sometimes transfer is only called to set idle_sp */ |
725 | if (flags == TRANSFER_SET_STACKLEVEL) |
661 | if (flags == TRANSFER_SET_STACKLEVEL) |
726 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
662 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
727 | else if (prev != next) |
663 | else if (prev != next) |
728 | { |
664 | { |
729 | coro_stack *prev__stack; |
665 | coro_cctx *prev__cctx; |
730 | |
666 | |
731 | LOCK; |
667 | LOCK; |
732 | |
668 | |
733 | if (next->mainstack) |
669 | if (next->mainstack) |
734 | { |
670 | { |
… | |
… | |
742 | /* first get rid of the old state */ |
678 | /* first get rid of the old state */ |
743 | SAVE (prev, -1); |
679 | SAVE (prev, -1); |
744 | /* setup coroutine call */ |
680 | /* setup coroutine call */ |
745 | setup_coro (next); |
681 | setup_coro (next); |
746 | /* need a stack */ |
682 | /* need a stack */ |
747 | next->stack = 0; |
683 | next->cctx = 0; |
748 | } |
684 | } |
749 | |
685 | |
750 | if (!prev->stack) |
686 | if (!prev->cctx) |
751 | /* create a new empty context */ |
687 | /* create a new empty context */ |
752 | Newz (0, prev->stack, 1, coro_stack); |
688 | Newz (0, prev->cctx, 1, coro_cctx); |
753 | |
689 | |
754 | prev__stack = prev->stack; |
690 | prev__cctx = prev->cctx; |
755 | |
691 | |
756 | /* possibly "free" the stack */ |
692 | /* possibly "free" the cctx */ |
757 | if (prev__stack->idle_sp == STACKLEVEL) |
693 | if (prev__cctx->idle_sp == STACKLEVEL) |
758 | { |
694 | { |
759 | stack_put (prev__stack); |
695 | cctx_put (prev__cctx); |
760 | prev->stack = 0; |
696 | prev->cctx = 0; |
761 | } |
697 | } |
762 | |
698 | |
763 | if (!next->stack) |
699 | if (!next->cctx) |
764 | next->stack = stack_get (); |
700 | next->cctx = cctx_get (); |
765 | |
701 | |
766 | if (prev__stack != next->stack) |
702 | if (prev__cctx != next->cctx) |
767 | { |
703 | { |
768 | prev__stack->top_env = PL_top_env; |
704 | prev__cctx->top_env = PL_top_env; |
769 | PL_top_env = next->stack->top_env; |
705 | PL_top_env = next->cctx->top_env; |
770 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
706 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
771 | } |
707 | } |
772 | |
708 | |
773 | free_coro_mortal (); |
709 | free_coro_mortal (); |
774 | |
710 | |
775 | UNLOCK; |
711 | UNLOCK; |
… | |
… | |
802 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
738 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
803 | |
739 | |
804 | coro->mainstack = 0; |
740 | coro->mainstack = 0; |
805 | } |
741 | } |
806 | |
742 | |
807 | stack_free (coro->stack); |
743 | cctx_free (coro->cctx); |
808 | SvREFCNT_dec (coro->args); |
744 | SvREFCNT_dec (coro->args); |
809 | Safefree (coro); |
745 | Safefree (coro); |
810 | } |
746 | } |
811 | |
747 | |
812 | static int |
748 | static int |
… | |
… | |
1056 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
992 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1057 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
993 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1058 | |
994 | |
1059 | for (i = 1; i < items; i++) |
995 | for (i = 1; i < items; i++) |
1060 | av_push (coro->args, newSVsv (ST (i))); |
996 | av_push (coro->args, newSVsv (ST (i))); |
1061 | |
|
|
1062 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1063 | /*coro->stack = 0;*/ |
|
|
1064 | } |
997 | } |
1065 | OUTPUT: |
998 | OUTPUT: |
1066 | RETVAL |
999 | RETVAL |
1067 | |
1000 | |
1068 | void |
1001 | void |
… | |
… | |
1077 | struct transfer_args ta; |
1010 | struct transfer_args ta; |
1078 | |
1011 | |
1079 | switch (ix) |
1012 | switch (ix) |
1080 | { |
1013 | { |
1081 | case 0: |
1014 | case 0: |
1082 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1015 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1083 | ta.next = 0; |
1016 | ta.next = 0; |
1084 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1017 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1085 | break; |
1018 | break; |
1086 | |
1019 | |
1087 | case 1: |
1020 | case 1: |
… | |
… | |
1147 | int code |
1080 | int code |
1148 | PROTOTYPE: $ |
1081 | PROTOTYPE: $ |
1149 | CODE: |
1082 | CODE: |
1150 | _exit (code); |
1083 | _exit (code); |
1151 | |
1084 | |
|
|
1085 | int |
|
|
1086 | cctx_count () |
|
|
1087 | CODE: |
|
|
1088 | RETVAL = cctx_count; |
|
|
1089 | OUTPUT: |
|
|
1090 | RETVAL |
|
|
1091 | |
|
|
1092 | int |
|
|
1093 | cctx_idle () |
|
|
1094 | CODE: |
|
|
1095 | RETVAL = cctx_idle; |
|
|
1096 | OUTPUT: |
|
|
1097 | RETVAL |
|
|
1098 | |
1152 | MODULE = Coro::State PACKAGE = Coro |
1099 | MODULE = Coro::State PACKAGE = Coro |
1153 | |
1100 | |
1154 | BOOT: |
1101 | BOOT: |
1155 | { |
1102 | { |
1156 | int i; |
1103 | int i; |