… | |
… | |
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 | 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); |
… | |
… | |
543 | setup_coro (struct coro *coro) |
467 | setup_coro (struct coro *coro) |
544 | { |
468 | { |
545 | /* |
469 | /* |
546 | * emulate part of the perl startup here. |
470 | * emulate part of the perl startup here. |
547 | */ |
471 | */ |
548 | dTHX; |
|
|
549 | dSP; |
|
|
550 | UNOP myop; |
|
|
551 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
552 | |
472 | |
553 | coro_init_stacks (); |
473 | coro_init_stacks (); |
|
|
474 | |
554 | /*PL_curcop = 0;*/ |
475 | PL_curcop = 0; |
555 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
476 | PL_in_eval = 0; |
|
|
477 | PL_curpm = 0; |
|
|
478 | |
|
|
479 | { |
|
|
480 | dSP; |
|
|
481 | LOGOP myop; |
|
|
482 | |
|
|
483 | /* I have no idea why this is needed, but it is */ |
|
|
484 | PUSHMARK (SP); |
|
|
485 | |
556 | SvREFCNT_dec (GvAV (PL_defgv)); |
486 | SvREFCNT_dec (GvAV (PL_defgv)); |
557 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
487 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
558 | |
488 | |
559 | SPAGAIN; |
|
|
560 | |
|
|
561 | Zero (&myop, 1, UNOP); |
489 | Zero (&myop, 1, LOGOP); |
562 | myop.op_next = Nullop; |
490 | myop.op_next = Nullop; |
563 | myop.op_flags = OPf_WANT_VOID; |
491 | myop.op_flags = OPf_WANT_VOID; |
564 | |
492 | |
565 | PL_op = (OP *)&myop; |
493 | PL_op = (OP *)&myop; |
566 | |
494 | |
567 | PUSHMARK(SP); |
495 | PUSHMARK (SP); |
568 | XPUSHs (sub_init); |
496 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
569 | PUTBACK; |
497 | PUTBACK; |
570 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
498 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
571 | SPAGAIN; |
499 | SPAGAIN; |
572 | |
500 | |
573 | ENTER; /* necessary e.g. for dounwind */ |
501 | ENTER; /* necessary e.g. for dounwind */ |
|
|
502 | } |
574 | } |
503 | } |
575 | |
504 | |
576 | static void |
505 | static void |
577 | free_coro_mortal () |
506 | free_coro_mortal () |
578 | { |
507 | { |
… | |
… | |
582 | coro_mortal = 0; |
511 | coro_mortal = 0; |
583 | } |
512 | } |
584 | } |
513 | } |
585 | |
514 | |
586 | static void NOINLINE |
515 | static void NOINLINE |
587 | prepare_cctx (coro_stack *cctx) |
516 | prepare_cctx (coro_cctx *cctx) |
588 | { |
517 | { |
589 | dSP; |
518 | dSP; |
590 | UNOP myop; |
519 | LOGOP myop; |
591 | |
520 | |
592 | Zero (&myop, 1, UNOP); |
521 | Zero (&myop, 1, LOGOP); |
593 | myop.op_next = PL_op; |
522 | myop.op_next = PL_op; |
594 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
523 | myop.op_flags = OPf_WANT_VOID; |
595 | |
524 | |
|
|
525 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
526 | |
596 | PUSHMARK(SP); |
527 | PUSHMARK (SP); |
597 | EXTEND (SP, 2); |
|
|
598 | PUSHs (newSViv (PTR2IV (cctx))); |
|
|
599 | PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
528 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
600 | PUTBACK; |
529 | PUTBACK; |
601 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
530 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
602 | SPAGAIN; |
531 | SPAGAIN; |
603 | } |
532 | } |
604 | |
533 | |
605 | static void |
534 | static void |
606 | coro_run (void *arg) |
535 | coro_run (void *arg) |
607 | { |
536 | { |
|
|
537 | /* coro_run is the alternative epilogue of transfer() */ |
|
|
538 | UNLOCK; |
|
|
539 | |
608 | /* |
540 | /* |
609 | * this is a _very_ stripped down perl interpreter ;) |
541 | * this is a _very_ stripped down perl interpreter ;) |
610 | */ |
542 | */ |
611 | UNLOCK; |
|
|
612 | |
|
|
613 | PL_top_env = &PL_start_env; |
543 | PL_top_env = &PL_start_env; |
|
|
544 | |
|
|
545 | /* inject call to cctx_init */ |
614 | prepare_cctx ((coro_stack *)arg); |
546 | prepare_cctx ((coro_cctx *)arg); |
615 | |
547 | |
616 | /* somebody will hit me for both perl_run and PL_restartop */ |
548 | /* somebody will hit me for both perl_run and PL_restartop */ |
617 | perl_run (PERL_GET_CONTEXT); |
549 | perl_run (PL_curinterp); |
618 | |
550 | |
619 | 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); |
620 | abort (); |
552 | abort (); |
621 | } |
553 | } |
622 | |
554 | |
623 | static coro_stack * |
555 | static coro_cctx * |
624 | stack_new () |
556 | cctx_new () |
625 | { |
557 | { |
626 | coro_stack *stack; |
558 | coro_cctx *cctx; |
627 | |
559 | |
|
|
560 | ++cctx_count; |
|
|
561 | |
628 | New (0, stack, 1, coro_stack); |
562 | New (0, cctx, 1, coro_cctx); |
629 | |
563 | |
630 | #if HAVE_MMAP |
564 | #if HAVE_MMAP |
631 | |
565 | |
632 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
566 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
633 | /* mmap suppsedly does allocate-on-write for us */ |
567 | /* mmap suppsedly does allocate-on-write for us */ |
634 | 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); |
635 | |
569 | |
636 | if (stack->sptr == (void *)-1) |
570 | if (cctx->sptr == (void *)-1) |
637 | { |
571 | { |
638 | perror ("FATAL: unable to mmap stack for coroutine"); |
572 | perror ("FATAL: unable to mmap stack for coroutine"); |
639 | _exit (EXIT_FAILURE); |
573 | _exit (EXIT_FAILURE); |
640 | } |
574 | } |
641 | |
575 | |
642 | # if STACKGUARD |
576 | # if STACKGUARD |
643 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
577 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
644 | # endif |
578 | # endif |
645 | |
579 | |
646 | #else |
580 | #else |
647 | |
581 | |
648 | stack->ssize = STACKSIZE * (long)sizeof (long); |
582 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
649 | New (0, stack->sptr, STACKSIZE, long); |
583 | New (0, cctx->sptr, STACKSIZE, long); |
650 | |
584 | |
651 | if (!stack->sptr) |
585 | if (!cctx->sptr) |
652 | { |
586 | { |
653 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
587 | perror ("FATAL: unable to malloc stack for coroutine"); |
654 | _exit (EXIT_FAILURE); |
588 | _exit (EXIT_FAILURE); |
655 | } |
589 | } |
656 | |
590 | |
657 | #endif |
591 | #endif |
658 | |
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 | |
659 | 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); |
660 | |
601 | |
661 | return stack; |
602 | return cctx; |
662 | } |
603 | } |
663 | |
604 | |
664 | static void |
605 | static void |
665 | stack_free (coro_stack *stack) |
606 | cctx_free (coro_cctx *cctx) |
666 | { |
607 | { |
667 | if (!stack) |
608 | if (!cctx) |
668 | return; |
609 | return; |
669 | |
610 | |
|
|
611 | --cctx_count; |
|
|
612 | |
|
|
613 | #if USE_VALGRIND |
|
|
614 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
615 | #endif |
|
|
616 | |
670 | #if HAVE_MMAP |
617 | #if HAVE_MMAP |
671 | munmap (stack->sptr, stack->ssize); |
618 | munmap (cctx->sptr, cctx->ssize); |
672 | #else |
619 | #else |
673 | Safefree (stack->sptr); |
620 | Safefree (cctx->sptr); |
674 | #endif |
621 | #endif |
675 | |
622 | |
676 | Safefree (stack); |
623 | Safefree (cctx); |
677 | } |
624 | } |
678 | |
625 | |
679 | static coro_stack *stack_first; |
|
|
680 | |
|
|
681 | static coro_stack * |
626 | static coro_cctx * |
682 | stack_get () |
627 | cctx_get () |
683 | { |
628 | { |
684 | coro_stack *stack; |
629 | coro_cctx *cctx; |
685 | |
630 | |
686 | if (stack_first) |
631 | if (cctx_first) |
687 | { |
632 | { |
688 | stack = stack_first; |
633 | --cctx_idle; |
|
|
634 | cctx = cctx_first; |
689 | stack_first = stack->next; |
635 | cctx_first = cctx->next; |
690 | } |
636 | } |
691 | else |
637 | else |
692 | { |
638 | { |
693 | stack = stack_new (); |
639 | cctx = cctx_new (); |
694 | PL_op = PL_op->op_next; |
640 | PL_op = PL_op->op_next; |
695 | } |
641 | } |
696 | |
642 | |
697 | return stack; |
643 | return cctx; |
698 | } |
644 | } |
699 | |
645 | |
700 | static void |
646 | static void |
701 | stack_put (coro_stack *stack) |
647 | cctx_put (coro_cctx *cctx) |
702 | { |
648 | { |
703 | stack->next = stack_first; |
649 | ++cctx_idle; |
704 | stack_first = stack; |
650 | cctx->next = cctx_first; |
|
|
651 | cctx_first = cctx; |
705 | } |
652 | } |
706 | |
653 | |
707 | /* never call directly, always through the coro_state_transfer global variable */ |
654 | /* never call directly, always through the coro_state_transfer global variable */ |
708 | static void NOINLINE |
655 | static void NOINLINE |
709 | transfer (struct coro *prev, struct coro *next, int flags) |
656 | transfer (struct coro *prev, struct coro *next, int flags) |
710 | { |
657 | { |
711 | dSTACKLEVEL; |
658 | dSTACKLEVEL; |
712 | |
659 | |
713 | /* sometimes transfer is only called to set idle_sp */ |
660 | /* sometimes transfer is only called to set idle_sp */ |
714 | if (flags == TRANSFER_SET_STACKLEVEL) |
661 | if (flags == TRANSFER_SET_STACKLEVEL) |
715 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
662 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
716 | else if (prev != next) |
663 | else if (prev != next) |
717 | { |
664 | { |
718 | coro_stack *prev__stack; |
665 | coro_cctx *prev__cctx; |
719 | |
666 | |
720 | LOCK; |
667 | LOCK; |
721 | |
668 | |
722 | if (next->mainstack) |
669 | if (next->mainstack) |
723 | { |
670 | { |
… | |
… | |
731 | /* first get rid of the old state */ |
678 | /* first get rid of the old state */ |
732 | SAVE (prev, -1); |
679 | SAVE (prev, -1); |
733 | /* setup coroutine call */ |
680 | /* setup coroutine call */ |
734 | setup_coro (next); |
681 | setup_coro (next); |
735 | /* need a stack */ |
682 | /* need a stack */ |
736 | next->stack = 0; |
683 | next->cctx = 0; |
737 | } |
684 | } |
738 | |
685 | |
739 | if (!prev->stack) |
686 | if (!prev->cctx) |
740 | /* create a new empty context */ |
687 | /* create a new empty context */ |
741 | Newz (0, prev->stack, 1, coro_stack); |
688 | Newz (0, prev->cctx, 1, coro_cctx); |
742 | |
689 | |
743 | prev__stack = prev->stack; |
690 | prev__cctx = prev->cctx; |
744 | |
691 | |
745 | /* possibly "free" the stack */ |
692 | /* possibly "free" the cctx */ |
746 | if (prev__stack->idle_sp == STACKLEVEL) |
693 | if (prev__cctx->idle_sp == STACKLEVEL) |
747 | { |
694 | { |
748 | stack_put (prev__stack); |
695 | cctx_put (prev__cctx); |
749 | prev->stack = 0; |
696 | prev->cctx = 0; |
750 | } |
697 | } |
751 | |
698 | |
752 | if (!next->stack) |
699 | if (!next->cctx) |
753 | next->stack = stack_get (); |
700 | next->cctx = cctx_get (); |
754 | |
701 | |
755 | if (prev__stack != next->stack) |
702 | if (prev__cctx != next->cctx) |
756 | { |
703 | { |
757 | prev__stack->top_env = PL_top_env; |
704 | prev__cctx->top_env = PL_top_env; |
758 | PL_top_env = next->stack->top_env; |
705 | PL_top_env = next->cctx->top_env; |
759 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
706 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
760 | } |
707 | } |
761 | |
708 | |
762 | free_coro_mortal (); |
709 | free_coro_mortal (); |
763 | |
710 | |
764 | UNLOCK; |
711 | UNLOCK; |
… | |
… | |
784 | struct coro temp; |
731 | struct coro temp; |
785 | |
732 | |
786 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
733 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
787 | LOAD (coro); |
734 | LOAD (coro); |
788 | |
735 | |
789 | destroy_stacks (); |
736 | coro_destroy_stacks (); |
790 | |
737 | |
791 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
738 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
792 | |
739 | |
793 | coro->mainstack = 0; |
740 | coro->mainstack = 0; |
794 | } |
741 | } |
795 | |
742 | |
796 | stack_free (coro->stack); |
743 | cctx_free (coro->cctx); |
797 | SvREFCNT_dec (coro->args); |
744 | SvREFCNT_dec (coro->args); |
798 | Safefree (coro); |
745 | Safefree (coro); |
799 | } |
746 | } |
800 | |
747 | |
801 | static int |
748 | static int |
… | |
… | |
823 | 0, 0, 0, 0, |
770 | 0, 0, 0, 0, |
824 | coro_state_clear, |
771 | coro_state_clear, |
825 | 0, |
772 | 0, |
826 | #ifdef MGf_DUP |
773 | #ifdef MGf_DUP |
827 | coro_state_dup, |
774 | coro_state_dup, |
|
|
775 | #else |
|
|
776 | # define MGf_DUP 0 |
828 | #endif |
777 | #endif |
829 | }; |
778 | }; |
830 | |
779 | |
831 | static struct coro * |
780 | static struct coro * |
832 | SvSTATE (SV *coro) |
781 | SvSTATE (SV *coro) |
… | |
… | |
1043 | 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; |
1044 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
993 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1045 | |
994 | |
1046 | for (i = 1; i < items; i++) |
995 | for (i = 1; i < items; i++) |
1047 | av_push (coro->args, newSVsv (ST (i))); |
996 | av_push (coro->args, newSVsv (ST (i))); |
1048 | |
|
|
1049 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1050 | /*coro->stack = 0;*/ |
|
|
1051 | } |
997 | } |
1052 | OUTPUT: |
998 | OUTPUT: |
1053 | RETVAL |
999 | RETVAL |
1054 | |
1000 | |
1055 | void |
1001 | void |
… | |
… | |
1064 | struct transfer_args ta; |
1010 | struct transfer_args ta; |
1065 | |
1011 | |
1066 | switch (ix) |
1012 | switch (ix) |
1067 | { |
1013 | { |
1068 | case 0: |
1014 | case 0: |
1069 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1015 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1070 | ta.next = 0; |
1016 | ta.next = 0; |
1071 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1017 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1072 | break; |
1018 | break; |
1073 | |
1019 | |
1074 | case 1: |
1020 | case 1: |
… | |
… | |
1134 | int code |
1080 | int code |
1135 | PROTOTYPE: $ |
1081 | PROTOTYPE: $ |
1136 | CODE: |
1082 | CODE: |
1137 | _exit (code); |
1083 | _exit (code); |
1138 | |
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 | |
1139 | MODULE = Coro::State PACKAGE = Coro |
1099 | MODULE = Coro::State PACKAGE = Coro |
1140 | |
1100 | |
1141 | BOOT: |
1101 | BOOT: |
1142 | { |
1102 | { |
1143 | int i; |
1103 | int i; |