… | |
… | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
9 | #if USE_VALGRIND |
10 | # include <valgrind/valgrind.h> |
10 | # include <valgrind/valgrind.h> |
11 | #endif |
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
12 | |
15 | |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
14 | (PERL_REVISION > (a) \ |
17 | (PERL_REVISION > (a) \ |
15 | || (PERL_REVISION == (a) \ |
18 | || (PERL_REVISION == (a) \ |
16 | && (PERL_VERSION > (b) \ |
19 | && (PERL_VERSION > (b) \ |
… | |
… | |
127 | int valgrind_id; |
130 | int valgrind_id; |
128 | #endif |
131 | #endif |
129 | } coro_cctx; |
132 | } coro_cctx; |
130 | |
133 | |
131 | enum { |
134 | enum { |
132 | CF_RUNNING, /* coroutine is running */ |
135 | CF_RUNNING = 0x0001, /* coroutine is running */ |
133 | CF_READY, /* coroutine is ready */ |
136 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
137 | CF_NEW = 0x0004, /* ahs never been switched to */ |
134 | }; |
138 | }; |
135 | |
139 | |
136 | /* this is a structure representing a perl-level coroutine */ |
140 | /* this is a structure representing a perl-level coroutine */ |
137 | struct coro { |
141 | struct coro { |
138 | /* the c coroutine allocated to this perl coroutine, if any */ |
142 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
277 | } |
281 | } |
278 | |
282 | |
279 | #define SB do { |
283 | #define SB do { |
280 | #define SE } while (0) |
284 | #define SE } while (0) |
281 | |
285 | |
282 | #define LOAD(state) load_state((state)); |
286 | #define LOAD(state) load_state ((state)) |
283 | #define SAVE(state,flags) save_state((state),(flags)); |
287 | #define SAVE(state,flags) save_state ((state),(flags)) |
284 | |
288 | |
285 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
289 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
286 | |
290 | |
287 | static void |
291 | static void |
288 | load_state(Coro__State c) |
292 | load_state (Coro__State c) |
289 | { |
293 | { |
290 | #define VAR(name,type) PL_ ## name = c->name; |
294 | #define VAR(name,type) PL_ ## name = c->name; |
291 | # include "state.h" |
295 | # include "state.h" |
292 | #undef VAR |
296 | #undef VAR |
293 | |
297 | |
… | |
… | |
310 | PUTBACK; |
314 | PUTBACK; |
311 | } |
315 | } |
312 | } |
316 | } |
313 | |
317 | |
314 | static void |
318 | static void |
315 | save_state(Coro__State c, int flags) |
319 | save_state (Coro__State c, int flags) |
316 | { |
320 | { |
317 | { |
321 | { |
318 | dSP; |
322 | dSP; |
319 | I32 cxix = cxstack_ix; |
323 | I32 cxix = cxstack_ix; |
320 | PERL_CONTEXT *ccstk = cxstack; |
324 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
331 | { |
335 | { |
332 | while (cxix >= 0) |
336 | while (cxix >= 0) |
333 | { |
337 | { |
334 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
338 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
335 | |
339 | |
336 | if (CxTYPE(cx) == CXt_SUB) |
340 | if (CxTYPE (cx) == CXt_SUB) |
337 | { |
341 | { |
338 | CV *cv = cx->blk_sub.cv; |
342 | CV *cv = cx->blk_sub.cv; |
339 | |
343 | |
340 | if (CvDEPTH (cv)) |
344 | if (CvDEPTH (cv)) |
341 | { |
345 | { |
342 | EXTEND (SP, 3); |
346 | EXTEND (SP, 3); |
343 | |
347 | |
344 | PUSHs ((SV *)CvPADLIST(cv)); |
348 | PUSHs ((SV *)CvPADLIST (cv)); |
345 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
349 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
346 | PUSHs ((SV *)cv); |
350 | PUSHs ((SV *)cv); |
347 | |
351 | |
348 | CvDEPTH (cv) = 0; |
352 | CvDEPTH (cv) = 0; |
349 | get_padlist (cv); |
353 | get_padlist (cv); |
350 | } |
354 | } |
351 | } |
355 | } |
352 | #ifdef CXt_FORMAT |
356 | #ifdef CXt_FORMAT |
353 | else if (CxTYPE(cx) == CXt_FORMAT) |
357 | else if (CxTYPE (cx) == CXt_FORMAT) |
354 | { |
358 | { |
355 | /* I never used formats, so how should I know how these are implemented? */ |
359 | /* I never used formats, so how should I know how these are implemented? */ |
356 | /* my bold guess is as a simple, plain sub... */ |
360 | /* my bold guess is as a simple, plain sub... */ |
357 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
361 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
358 | } |
362 | } |
… | |
… | |
386 | * not usually need a lot of stackspace. |
390 | * not usually need a lot of stackspace. |
387 | */ |
391 | */ |
388 | static void |
392 | static void |
389 | coro_init_stacks () |
393 | coro_init_stacks () |
390 | { |
394 | { |
391 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
395 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
392 | PL_curstackinfo->si_type = PERLSI_MAIN; |
396 | PL_curstackinfo->si_type = PERLSI_MAIN; |
393 | PL_curstack = PL_curstackinfo->si_stack; |
397 | PL_curstack = PL_curstackinfo->si_stack; |
394 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
398 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
395 | |
399 | |
396 | PL_stack_base = AvARRAY(PL_curstack); |
400 | PL_stack_base = AvARRAY(PL_curstack); |
397 | PL_stack_sp = PL_stack_base; |
401 | PL_stack_sp = PL_stack_base; |
398 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
402 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
399 | |
403 | |
400 | New(50,PL_tmps_stack,96,SV*); |
404 | New(50,PL_tmps_stack,128,SV*); |
401 | PL_tmps_floor = -1; |
405 | PL_tmps_floor = -1; |
402 | PL_tmps_ix = -1; |
406 | PL_tmps_ix = -1; |
403 | PL_tmps_max = 96; |
407 | PL_tmps_max = 128; |
404 | |
408 | |
405 | New(54,PL_markstack,16,I32); |
409 | New(54,PL_markstack,32,I32); |
406 | PL_markstack_ptr = PL_markstack; |
410 | PL_markstack_ptr = PL_markstack; |
407 | PL_markstack_max = PL_markstack + 16; |
411 | PL_markstack_max = PL_markstack + 32; |
408 | |
412 | |
409 | #ifdef SET_MARK_OFFSET |
413 | #ifdef SET_MARK_OFFSET |
410 | SET_MARK_OFFSET; |
414 | SET_MARK_OFFSET; |
411 | #endif |
415 | #endif |
412 | |
416 | |
413 | New(54,PL_scopestack,16,I32); |
417 | New(54,PL_scopestack,32,I32); |
414 | PL_scopestack_ix = 0; |
418 | PL_scopestack_ix = 0; |
415 | PL_scopestack_max = 16; |
419 | PL_scopestack_max = 32; |
416 | |
420 | |
417 | New(54,PL_savestack,96,ANY); |
421 | New(54,PL_savestack,64,ANY); |
418 | PL_savestack_ix = 0; |
422 | PL_savestack_ix = 0; |
419 | PL_savestack_max = 96; |
423 | PL_savestack_max = 64; |
420 | |
424 | |
421 | #if !PERL_VERSION_ATLEAST (5,9,0) |
425 | #if !PERL_VERSION_ATLEAST (5,9,0) |
422 | New(54,PL_retstack,8,OP*); |
426 | New(54,PL_retstack,16,OP*); |
423 | PL_retstack_ix = 0; |
427 | PL_retstack_ix = 0; |
424 | PL_retstack_max = 8; |
428 | PL_retstack_max = 16; |
425 | #endif |
429 | #endif |
426 | } |
430 | } |
427 | |
431 | |
428 | /* |
432 | /* |
429 | * destroy the stacks, the callchain etc... |
433 | * destroy the stacks, the callchain etc... |
… | |
… | |
482 | * emulate part of the perl startup here. |
486 | * emulate part of the perl startup here. |
483 | */ |
487 | */ |
484 | |
488 | |
485 | coro_init_stacks (); |
489 | coro_init_stacks (); |
486 | |
490 | |
487 | PL_curcop = 0; |
491 | PL_curcop = &PL_compiling; |
488 | PL_in_eval = 0; |
492 | PL_in_eval = EVAL_NULL; |
489 | PL_curpm = 0; |
493 | PL_curpm = 0; |
|
|
494 | PL_localizing = 0; |
|
|
495 | PL_dirty = 0; |
|
|
496 | PL_restartop = 0; |
490 | |
497 | |
491 | { |
498 | { |
492 | dSP; |
499 | dSP; |
493 | LOGOP myop; |
500 | LOGOP myop; |
494 | |
501 | |
… | |
… | |
507 | PUSHMARK (SP); |
514 | PUSHMARK (SP); |
508 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
515 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
509 | PUTBACK; |
516 | PUTBACK; |
510 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
517 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
511 | SPAGAIN; |
518 | SPAGAIN; |
512 | |
|
|
513 | ENTER; /* necessary e.g. for dounwind */ |
|
|
514 | } |
519 | } |
|
|
520 | |
|
|
521 | ENTER; /* necessary e.g. for dounwind */ |
515 | } |
522 | } |
516 | |
523 | |
517 | static void |
524 | static void |
518 | free_coro_mortal () |
525 | free_coro_mortal () |
519 | { |
526 | { |
… | |
… | |
613 | |
620 | |
614 | return cctx; |
621 | return cctx; |
615 | } |
622 | } |
616 | |
623 | |
617 | static void |
624 | static void |
618 | cctx_free (coro_cctx *cctx) |
625 | cctx_destroy (coro_cctx *cctx) |
619 | { |
626 | { |
620 | if (!cctx) |
627 | if (!cctx) |
621 | return; |
628 | return; |
622 | |
629 | |
623 | --cctx_count; |
630 | --cctx_count; |
… | |
… | |
640 | { |
647 | { |
641 | coro_cctx *cctx; |
648 | coro_cctx *cctx; |
642 | |
649 | |
643 | if (cctx_first) |
650 | if (cctx_first) |
644 | { |
651 | { |
645 | --cctx_idle; |
|
|
646 | cctx = cctx_first; |
652 | cctx = cctx_first; |
647 | cctx_first = cctx->next; |
653 | cctx_first = cctx->next; |
|
|
654 | --cctx_idle; |
648 | } |
655 | } |
649 | else |
656 | else |
650 | { |
657 | { |
651 | cctx = cctx_new (); |
658 | cctx = cctx_new (); |
652 | PL_op = PL_op->op_next; |
659 | PL_op = PL_op->op_next; |
… | |
… | |
656 | } |
663 | } |
657 | |
664 | |
658 | static void |
665 | static void |
659 | cctx_put (coro_cctx *cctx) |
666 | cctx_put (coro_cctx *cctx) |
660 | { |
667 | { |
|
|
668 | /* free another cctx if overlimit */ |
|
|
669 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
670 | { |
|
|
671 | coro_cctx *first = cctx_first; |
|
|
672 | cctx_first = first->next; |
|
|
673 | --cctx_idle; |
|
|
674 | |
|
|
675 | assert (!first->inuse); |
|
|
676 | cctx_destroy (first); |
|
|
677 | } |
|
|
678 | |
661 | ++cctx_idle; |
679 | ++cctx_idle; |
662 | cctx->next = cctx_first; |
680 | cctx->next = cctx_first; |
663 | cctx_first = cctx; |
681 | cctx_first = cctx; |
664 | } |
682 | } |
665 | |
683 | |
… | |
… | |
674 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
692 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
675 | else if (prev != next) |
693 | else if (prev != next) |
676 | { |
694 | { |
677 | coro_cctx *prev__cctx; |
695 | coro_cctx *prev__cctx; |
678 | |
696 | |
679 | if (!prev->cctx) |
697 | if (prev->flags & CF_NEW) |
680 | { |
698 | { |
681 | /* create a new empty context */ |
699 | /* create a new empty context */ |
682 | Newz (0, prev->cctx, 1, coro_cctx); |
700 | Newz (0, prev->cctx, 1, coro_cctx); |
683 | prev->cctx->inuse = 1; |
701 | prev->cctx->inuse = 1; |
|
|
702 | prev->flags &= ~CF_NEW; |
684 | prev->flags |= CF_RUNNING; |
703 | prev->flags |= CF_RUNNING; |
685 | } |
704 | } |
686 | |
705 | |
|
|
706 | /*TODO: must not croak here */ |
687 | if (!prev->flags & CF_RUNNING) |
707 | if (!prev->flags & CF_RUNNING) |
688 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
708 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
689 | |
709 | |
690 | if (next->flags & CF_RUNNING) |
710 | if (next->flags & CF_RUNNING) |
691 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
711 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
… | |
… | |
702 | LOAD (next); |
722 | LOAD (next); |
703 | } |
723 | } |
704 | else |
724 | else |
705 | { |
725 | { |
706 | /* need to start coroutine */ |
726 | /* need to start coroutine */ |
|
|
727 | assert (next->flags & CF_NEW); |
|
|
728 | next->flags &= ~CF_NEW; |
707 | /* first get rid of the old state */ |
729 | /* first get rid of the old state */ |
708 | SAVE (prev, -1); |
730 | SAVE (prev, -1); |
709 | /* setup coroutine call */ |
731 | /* setup coroutine call */ |
710 | setup_coro (next); |
732 | setup_coro (next); |
711 | /* need a new stack */ |
733 | /* need a new stack */ |
… | |
… | |
713 | } |
735 | } |
714 | |
736 | |
715 | prev__cctx = prev->cctx; |
737 | prev__cctx = prev->cctx; |
716 | |
738 | |
717 | /* possibly "free" the cctx */ |
739 | /* possibly "free" the cctx */ |
718 | if (prev__cctx->idle_sp == STACKLEVEL) |
740 | if (prev__cctx->idle_sp == STACKLEVEL && 0) |
719 | { |
741 | { |
720 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
742 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
721 | assert (PL_top_env == prev__cctx->top_env); |
743 | assert (PL_top_env == prev__cctx->top_env); |
722 | |
744 | |
|
|
745 | prev->cctx = 0; |
|
|
746 | |
723 | cctx_put (prev__cctx); |
747 | cctx_put (prev__cctx); |
724 | prev->cctx = 0; |
748 | prev__cctx->inuse = 0; |
725 | } |
749 | } |
726 | |
750 | |
727 | if (!next->cctx) |
751 | if (!next->cctx) |
|
|
752 | { |
728 | next->cctx = cctx_get (); |
753 | next->cctx = cctx_get (); |
|
|
754 | assert (!next->cctx->inuse); |
|
|
755 | next->cctx->inuse = 1; |
|
|
756 | } |
729 | |
757 | |
730 | if (prev__cctx != next->cctx) |
758 | if (prev__cctx != next->cctx) |
731 | { |
759 | { |
732 | assert ( prev__cctx->inuse); |
|
|
733 | assert (!next->cctx->inuse); |
|
|
734 | |
|
|
735 | prev__cctx->inuse = 0; |
|
|
736 | next->cctx->inuse = 1; |
|
|
737 | |
|
|
738 | prev__cctx->top_env = PL_top_env; |
760 | prev__cctx->top_env = PL_top_env; |
739 | PL_top_env = next->cctx->top_env; |
761 | PL_top_env = next->cctx->top_env; |
740 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
762 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
741 | } |
763 | } |
742 | |
764 | |
… | |
… | |
758 | coro_state_destroy (struct coro *coro) |
780 | coro_state_destroy (struct coro *coro) |
759 | { |
781 | { |
760 | if (coro->refcnt--) |
782 | if (coro->refcnt--) |
761 | return; |
783 | return; |
762 | |
784 | |
763 | if (coro->flags & CF_RUNNING) |
|
|
764 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
765 | |
|
|
766 | if (coro->mainstack && coro->mainstack != main_mainstack) |
785 | if (coro->mainstack && coro->mainstack != main_mainstack) |
767 | { |
786 | { |
768 | struct coro temp; |
787 | struct coro temp; |
769 | |
788 | |
|
|
789 | if (coro->flags & CF_RUNNING) |
|
|
790 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
791 | |
770 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
792 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
771 | LOAD (coro); |
793 | LOAD (coro); |
772 | |
794 | |
773 | coro_destroy_stacks (); |
795 | coro_destroy_stacks (); |
774 | |
796 | |
775 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
797 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
776 | |
798 | |
777 | coro->mainstack = 0; |
799 | coro->mainstack = 0; |
778 | } |
800 | } |
779 | |
801 | |
780 | cctx_free (coro->cctx); |
802 | cctx_destroy (coro->cctx); |
781 | SvREFCNT_dec (coro->args); |
803 | SvREFCNT_dec (coro->args); |
782 | Safefree (coro); |
804 | Safefree (coro); |
783 | } |
805 | } |
784 | |
806 | |
785 | static int |
807 | static int |
… | |
… | |
845 | } |
867 | } |
846 | |
868 | |
847 | static void |
869 | static void |
848 | api_transfer (SV *prev, SV *next, int flags) |
870 | api_transfer (SV *prev, SV *next, int flags) |
849 | { |
871 | { |
850 | dTHX; |
|
|
851 | struct transfer_args ta; |
872 | struct transfer_args ta; |
852 | |
873 | |
853 | prepare_transfer (&ta, prev, next, flags); |
874 | prepare_transfer (&ta, prev, next, flags); |
854 | TRANSFER (ta); |
875 | TRANSFER (ta); |
855 | } |
876 | } |
… | |
… | |
862 | #define PRIO_LOW -1 |
883 | #define PRIO_LOW -1 |
863 | #define PRIO_IDLE -3 |
884 | #define PRIO_IDLE -3 |
864 | #define PRIO_MIN -4 |
885 | #define PRIO_MIN -4 |
865 | |
886 | |
866 | /* for Coro.pm */ |
887 | /* for Coro.pm */ |
867 | static GV *coro_current, *coro_idle; |
888 | static SV *coro_current; |
868 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
889 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
869 | static int coro_nready; |
890 | static int coro_nready; |
870 | |
891 | |
871 | static void |
892 | static void |
872 | coro_enq (SV *coro_sv) |
893 | coro_enq (SV *coro_sv) |
… | |
… | |
905 | coro = SvSTATE (coro_sv); |
926 | coro = SvSTATE (coro_sv); |
906 | |
927 | |
907 | if (coro->flags & CF_READY) |
928 | if (coro->flags & CF_READY) |
908 | return 0; |
929 | return 0; |
909 | |
930 | |
|
|
931 | #if 0 /* this is actually harmless */ |
910 | if (coro->flags & CF_RUNNING) |
932 | if (coro->flags & CF_RUNNING) |
911 | croak ("Coro::ready called on currently running coroutine"); |
933 | croak ("Coro::ready called on currently running coroutine"); |
|
|
934 | #endif |
912 | |
935 | |
913 | coro->flags |= CF_READY; |
936 | coro->flags |= CF_READY; |
914 | |
937 | |
915 | LOCK; |
938 | LOCK; |
916 | coro_enq (SvREFCNT_inc (coro_sv)); |
939 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
926 | } |
949 | } |
927 | |
950 | |
928 | static void |
951 | static void |
929 | prepare_schedule (struct transfer_args *ta) |
952 | prepare_schedule (struct transfer_args *ta) |
930 | { |
953 | { |
931 | SV *current, *prev, *next; |
954 | SV *prev, *next; |
932 | |
|
|
933 | current = GvSV (coro_current); |
|
|
934 | |
955 | |
935 | for (;;) |
956 | for (;;) |
936 | { |
957 | { |
937 | LOCK; |
958 | LOCK; |
938 | next = coro_deq (PRIO_MIN); |
959 | next = coro_deq (PRIO_MIN); |
… | |
… | |
947 | ENTER; |
968 | ENTER; |
948 | SAVETMPS; |
969 | SAVETMPS; |
949 | |
970 | |
950 | PUSHMARK (SP); |
971 | PUSHMARK (SP); |
951 | PUTBACK; |
972 | PUTBACK; |
952 | call_sv (GvSV (coro_idle), G_DISCARD); |
973 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
953 | |
974 | |
954 | FREETMPS; |
975 | FREETMPS; |
955 | LEAVE; |
976 | LEAVE; |
956 | } |
977 | } |
957 | } |
978 | } |
958 | |
979 | |
959 | prev = SvRV (current); |
980 | prev = SvRV (coro_current); |
960 | SvRV (current) = next; |
981 | SvRV_set (coro_current, next); |
961 | |
982 | |
962 | /* free this only after the transfer */ |
983 | /* free this only after the transfer */ |
963 | LOCK; |
984 | LOCK; |
964 | free_coro_mortal (); |
985 | free_coro_mortal (); |
965 | UNLOCK; |
986 | UNLOCK; |
966 | coro_mortal = prev; |
987 | coro_mortal = prev; |
967 | |
988 | |
|
|
989 | assert (!SvROK(prev));//D |
|
|
990 | assert (!SvROK(next));//D |
|
|
991 | |
968 | ta->prev = SvSTATE (prev); |
992 | ta->prev = SvSTATE (prev); |
969 | ta->next = SvSTATE (next); |
993 | ta->next = SvSTATE (next); |
970 | ta->flags = TRANSFER_SAVE_ALL; |
994 | ta->flags = TRANSFER_SAVE_ALL; |
971 | |
995 | |
|
|
996 | assert (ta->flags & CF_READY); |
972 | ta->next->flags &= ~CF_READY; |
997 | ta->next->flags &= ~CF_READY; |
973 | } |
998 | } |
974 | |
999 | |
975 | static void |
1000 | static void |
976 | prepare_cede (struct transfer_args *ta) |
1001 | prepare_cede (struct transfer_args *ta) |
977 | { |
1002 | { |
978 | api_ready (GvSV (coro_current)); |
1003 | api_ready (coro_current); |
979 | |
1004 | |
980 | prepare_schedule (ta); |
1005 | prepare_schedule (ta); |
981 | } |
1006 | } |
982 | |
1007 | |
983 | static void |
1008 | static void |
984 | api_schedule (void) |
1009 | api_schedule (void) |
985 | { |
1010 | { |
986 | dTHX; |
|
|
987 | struct transfer_args ta; |
1011 | struct transfer_args ta; |
988 | |
1012 | |
989 | prepare_schedule (&ta); |
1013 | prepare_schedule (&ta); |
990 | TRANSFER (ta); |
1014 | TRANSFER (ta); |
991 | } |
1015 | } |
992 | |
1016 | |
993 | static void |
1017 | static void |
994 | api_cede (void) |
1018 | api_cede (void) |
995 | { |
1019 | { |
996 | dTHX; |
|
|
997 | struct transfer_args ta; |
1020 | struct transfer_args ta; |
998 | |
1021 | |
999 | prepare_cede (&ta); |
1022 | prepare_cede (&ta); |
1000 | TRANSFER (ta); |
1023 | TRANSFER (ta); |
1001 | } |
1024 | } |
… | |
… | |
1033 | HV *hv; |
1056 | HV *hv; |
1034 | int i; |
1057 | int i; |
1035 | |
1058 | |
1036 | Newz (0, coro, 1, struct coro); |
1059 | Newz (0, coro, 1, struct coro); |
1037 | coro->args = newAV (); |
1060 | coro->args = newAV (); |
|
|
1061 | coro->flags = CF_NEW; |
1038 | |
1062 | |
1039 | hv = newHV (); |
1063 | hv = newHV (); |
1040 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1064 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1041 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1065 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1042 | |
1066 | |
… | |
… | |
1065 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1089 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1066 | break; |
1090 | break; |
1067 | |
1091 | |
1068 | case 1: |
1092 | case 1: |
1069 | if (items != 3) |
1093 | if (items != 3) |
1070 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1094 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1071 | |
1095 | |
1072 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1096 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1073 | break; |
1097 | break; |
1074 | |
1098 | |
1075 | case 2: |
1099 | case 2: |
… | |
… | |
1085 | SV *yieldstack; |
1109 | SV *yieldstack; |
1086 | SV *sv; |
1110 | SV *sv; |
1087 | AV *defav = GvAV (PL_defgv); |
1111 | AV *defav = GvAV (PL_defgv); |
1088 | |
1112 | |
1089 | yieldstack = *hv_fetch ( |
1113 | yieldstack = *hv_fetch ( |
1090 | (HV *)SvRV (GvSV (coro_current)), |
1114 | (HV *)SvRV (coro_current), |
1091 | "yieldstack", sizeof ("yieldstack") - 1, |
1115 | "yieldstack", sizeof ("yieldstack") - 1, |
1092 | 0 |
1116 | 0 |
1093 | ); |
1117 | ); |
1094 | |
1118 | |
1095 | /* set up @_ -- ugly */ |
1119 | /* set up @_ -- ugly */ |
… | |
… | |
1157 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1181 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1158 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1182 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1159 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1183 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1160 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1184 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1161 | |
1185 | |
1162 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1186 | coro_current = get_sv ("Coro::current", FALSE); |
1163 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1187 | SvREADONLY_on (coro_current); |
1164 | |
1188 | |
1165 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1189 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1166 | coro_ready[i] = newAV (); |
1190 | coro_ready[i] = newAV (); |
1167 | |
1191 | |
1168 | { |
1192 | { |
… | |
… | |
1223 | CODE: |
1247 | CODE: |
1224 | RETVAL = coro_nready; |
1248 | RETVAL = coro_nready; |
1225 | OUTPUT: |
1249 | OUTPUT: |
1226 | RETVAL |
1250 | RETVAL |
1227 | |
1251 | |
|
|
1252 | void |
|
|
1253 | _set_current (SV *current) |
|
|
1254 | PROTOTYPE: $ |
|
|
1255 | CODE: |
|
|
1256 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1257 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1258 | |
1228 | MODULE = Coro::State PACKAGE = Coro::AIO |
1259 | MODULE = Coro::State PACKAGE = Coro::AIO |
1229 | |
1260 | |
1230 | SV * |
1261 | SV * |
1231 | _get_state () |
1262 | _get_state () |
1232 | CODE: |
1263 | CODE: |