… | |
… | |
101 | #else |
101 | #else |
102 | # define LOCK (void)0 |
102 | # define LOCK (void)0 |
103 | # define UNLOCK (void)0 |
103 | # define UNLOCK (void)0 |
104 | #endif |
104 | #endif |
105 | |
105 | |
|
|
106 | struct io_state |
|
|
107 | { |
|
|
108 | int errorno; |
|
|
109 | I32 laststype; |
|
|
110 | int laststatval; |
|
|
111 | Stat_t statcache; |
|
|
112 | }; |
|
|
113 | |
106 | static struct CoroAPI coroapi; |
114 | static struct CoroAPI coroapi; |
107 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
115 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
108 | static HV *coro_state_stash, *coro_stash; |
116 | static HV *coro_state_stash, *coro_stash; |
109 | static SV *coro_mortal; /* will be freed after next transfer */ |
117 | static SV *coro_mortal; /* will be freed after next transfer */ |
110 | |
118 | |
… | |
… | |
130 | int valgrind_id; |
138 | int valgrind_id; |
131 | #endif |
139 | #endif |
132 | } coro_cctx; |
140 | } coro_cctx; |
133 | |
141 | |
134 | enum { |
142 | enum { |
135 | CF_RUNNING, /* coroutine is running */ |
143 | CF_RUNNING = 0x0001, /* coroutine is running */ |
136 | CF_READY, /* coroutine is ready */ |
144 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
145 | CF_NEW = 0x0004, /* ahs never been switched to */ |
137 | }; |
146 | }; |
138 | |
147 | |
139 | /* this is a structure representing a perl-level coroutine */ |
148 | /* this is a structure representing a perl-level coroutine */ |
140 | struct coro { |
149 | struct coro { |
141 | /* the c coroutine allocated to this perl coroutine, if any */ |
150 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
280 | } |
289 | } |
281 | |
290 | |
282 | #define SB do { |
291 | #define SB do { |
283 | #define SE } while (0) |
292 | #define SE } while (0) |
284 | |
293 | |
285 | #define LOAD(state) load_state((state)); |
294 | #define LOAD(state) load_state ((state)) |
286 | #define SAVE(state,flags) save_state((state),(flags)); |
295 | #define SAVE(state,flags) save_state ((state),(flags)) |
287 | |
296 | |
288 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
297 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
289 | |
298 | |
290 | static void |
299 | static void |
291 | load_state(Coro__State c) |
300 | load_state (Coro__State c) |
292 | { |
301 | { |
293 | #define VAR(name,type) PL_ ## name = c->name; |
302 | #define VAR(name,type) PL_ ## name = c->name; |
294 | # include "state.h" |
303 | # include "state.h" |
295 | #undef VAR |
304 | #undef VAR |
296 | |
305 | |
… | |
… | |
313 | PUTBACK; |
322 | PUTBACK; |
314 | } |
323 | } |
315 | } |
324 | } |
316 | |
325 | |
317 | static void |
326 | static void |
318 | save_state(Coro__State c, int flags) |
327 | save_state (Coro__State c, int flags) |
319 | { |
328 | { |
320 | { |
329 | { |
321 | dSP; |
330 | dSP; |
322 | I32 cxix = cxstack_ix; |
331 | I32 cxix = cxstack_ix; |
323 | PERL_CONTEXT *ccstk = cxstack; |
332 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
334 | { |
343 | { |
335 | while (cxix >= 0) |
344 | while (cxix >= 0) |
336 | { |
345 | { |
337 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
346 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
338 | |
347 | |
339 | if (CxTYPE(cx) == CXt_SUB) |
348 | if (CxTYPE (cx) == CXt_SUB) |
340 | { |
349 | { |
341 | CV *cv = cx->blk_sub.cv; |
350 | CV *cv = cx->blk_sub.cv; |
342 | |
351 | |
343 | if (CvDEPTH (cv)) |
352 | if (CvDEPTH (cv)) |
344 | { |
353 | { |
345 | EXTEND (SP, 3); |
354 | EXTEND (SP, 3); |
346 | |
355 | |
347 | PUSHs ((SV *)CvPADLIST(cv)); |
356 | PUSHs ((SV *)CvPADLIST (cv)); |
348 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
357 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
349 | PUSHs ((SV *)cv); |
358 | PUSHs ((SV *)cv); |
350 | |
359 | |
351 | CvDEPTH (cv) = 0; |
360 | CvDEPTH (cv) = 0; |
352 | get_padlist (cv); |
361 | get_padlist (cv); |
353 | } |
362 | } |
354 | } |
363 | } |
355 | #ifdef CXt_FORMAT |
364 | #ifdef CXt_FORMAT |
356 | else if (CxTYPE(cx) == CXt_FORMAT) |
365 | else if (CxTYPE (cx) == CXt_FORMAT) |
357 | { |
366 | { |
358 | /* I never used formats, so how should I know how these are implemented? */ |
367 | /* I never used formats, so how should I know how these are implemented? */ |
359 | /* my bold guess is as a simple, plain sub... */ |
368 | /* my bold guess is as a simple, plain sub... */ |
360 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
369 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
361 | } |
370 | } |
… | |
… | |
485 | * emulate part of the perl startup here. |
494 | * emulate part of the perl startup here. |
486 | */ |
495 | */ |
487 | |
496 | |
488 | coro_init_stacks (); |
497 | coro_init_stacks (); |
489 | |
498 | |
490 | PL_curcop = 0; |
499 | PL_curcop = &PL_compiling; |
491 | PL_in_eval = 0; |
500 | PL_in_eval = EVAL_NULL; |
492 | PL_curpm = 0; |
501 | PL_curpm = 0; |
|
|
502 | PL_localizing = 0; |
|
|
503 | PL_dirty = 0; |
|
|
504 | PL_restartop = 0; |
493 | |
505 | |
494 | { |
506 | { |
495 | dSP; |
507 | dSP; |
496 | LOGOP myop; |
508 | LOGOP myop; |
497 | |
509 | |
498 | /* I have no idea why this is needed, but it is */ |
|
|
499 | PUSHMARK (SP); |
|
|
500 | |
|
|
501 | SvREFCNT_dec (GvAV (PL_defgv)); |
510 | SvREFCNT_dec (GvAV (PL_defgv)); |
502 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
511 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
503 | |
512 | |
504 | Zero (&myop, 1, LOGOP); |
513 | Zero (&myop, 1, LOGOP); |
505 | myop.op_next = Nullop; |
514 | myop.op_next = Nullop; |
506 | myop.op_flags = OPf_WANT_VOID; |
515 | myop.op_flags = OPf_WANT_VOID; |
507 | |
516 | |
508 | PL_op = (OP *)&myop; |
|
|
509 | |
|
|
510 | PUSHMARK (SP); |
517 | PUSHMARK (SP); |
511 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
518 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
512 | PUTBACK; |
519 | PUTBACK; |
|
|
520 | PL_op = (OP *)&myop; |
513 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
521 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
514 | SPAGAIN; |
522 | SPAGAIN; |
515 | |
|
|
516 | ENTER; /* necessary e.g. for dounwind */ |
|
|
517 | } |
523 | } |
|
|
524 | |
|
|
525 | ENTER; /* necessary e.g. for dounwind */ |
518 | } |
526 | } |
519 | |
527 | |
520 | static void |
528 | static void |
521 | free_coro_mortal () |
529 | free_coro_mortal () |
522 | { |
530 | { |
… | |
… | |
525 | SvREFCNT_dec (coro_mortal); |
533 | SvREFCNT_dec (coro_mortal); |
526 | coro_mortal = 0; |
534 | coro_mortal = 0; |
527 | } |
535 | } |
528 | } |
536 | } |
529 | |
537 | |
|
|
538 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
530 | static void NOINLINE |
539 | static void NOINLINE |
531 | prepare_cctx (coro_cctx *cctx) |
540 | prepare_cctx (coro_cctx *cctx) |
532 | { |
541 | { |
533 | dSP; |
542 | dSP; |
534 | LOGOP myop; |
543 | LOGOP myop; |
535 | |
544 | |
536 | Zero (&myop, 1, LOGOP); |
545 | Zero (&myop, 1, LOGOP); |
537 | myop.op_next = PL_op; |
546 | myop.op_next = PL_op; |
538 | myop.op_flags = OPf_WANT_VOID; |
547 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
539 | |
|
|
540 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
541 | |
548 | |
542 | PUSHMARK (SP); |
549 | PUSHMARK (SP); |
|
|
550 | EXTEND (SP, 2); |
|
|
551 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
543 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
552 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
544 | PUTBACK; |
553 | PUTBACK; |
|
|
554 | PL_op = (OP *)&myop; |
545 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
555 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
546 | SPAGAIN; |
556 | SPAGAIN; |
547 | } |
557 | } |
548 | |
558 | |
549 | static void |
559 | static void |
550 | coro_run (void *arg) |
560 | coro_run (void *arg) |
551 | { |
561 | { |
552 | /* coro_run is the alternative epilogue of transfer() */ |
562 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
553 | UNLOCK; |
563 | UNLOCK; |
554 | |
564 | |
555 | /* |
565 | /* |
556 | * this is a _very_ stripped down perl interpreter ;) |
566 | * this is a _very_ stripped down perl interpreter ;) |
557 | */ |
567 | */ |
… | |
… | |
559 | |
569 | |
560 | /* inject call to cctx_init */ |
570 | /* inject call to cctx_init */ |
561 | prepare_cctx ((coro_cctx *)arg); |
571 | prepare_cctx ((coro_cctx *)arg); |
562 | |
572 | |
563 | /* somebody will hit me for both perl_run and PL_restartop */ |
573 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
574 | PL_restartop = PL_op; |
564 | perl_run (PL_curinterp); |
575 | perl_run (PL_curinterp); |
565 | |
576 | |
566 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
577 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
567 | abort (); |
578 | abort (); |
568 | } |
579 | } |
… | |
… | |
577 | New (0, cctx, 1, coro_cctx); |
588 | New (0, cctx, 1, coro_cctx); |
578 | |
589 | |
579 | #if HAVE_MMAP |
590 | #if HAVE_MMAP |
580 | |
591 | |
581 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
592 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
582 | /* mmap suppsedly does allocate-on-write for us */ |
593 | /* mmap supposedly does allocate-on-write for us */ |
583 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
594 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
584 | |
595 | |
585 | if (cctx->sptr == (void *)-1) |
596 | if (cctx->sptr == (void *)-1) |
586 | { |
597 | { |
587 | perror ("FATAL: unable to mmap stack for coroutine"); |
598 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
688 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
699 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
689 | else if (prev != next) |
700 | else if (prev != next) |
690 | { |
701 | { |
691 | coro_cctx *prev__cctx; |
702 | coro_cctx *prev__cctx; |
692 | |
703 | |
693 | if (!prev->cctx) |
704 | if (prev->flags & CF_NEW) |
694 | { |
705 | { |
695 | /* create a new empty context */ |
706 | /* create a new empty context */ |
696 | Newz (0, prev->cctx, 1, coro_cctx); |
707 | Newz (0, prev->cctx, 1, coro_cctx); |
697 | prev->cctx->inuse = 1; |
708 | prev->cctx->inuse = 1; |
|
|
709 | prev->flags &= ~CF_NEW; |
698 | prev->flags |= CF_RUNNING; |
710 | prev->flags |= CF_RUNNING; |
699 | } |
711 | } |
700 | |
712 | |
|
|
713 | /*TODO: must not croak here */ |
701 | if (!prev->flags & CF_RUNNING) |
714 | if (!prev->flags & CF_RUNNING) |
702 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
715 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
703 | |
716 | |
704 | if (next->flags & CF_RUNNING) |
717 | if (next->flags & CF_RUNNING) |
705 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
718 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
… | |
… | |
707 | prev->flags &= ~CF_RUNNING; |
720 | prev->flags &= ~CF_RUNNING; |
708 | next->flags |= CF_RUNNING; |
721 | next->flags |= CF_RUNNING; |
709 | |
722 | |
710 | LOCK; |
723 | LOCK; |
711 | |
724 | |
712 | if (next->mainstack) |
725 | if (next->flags & CF_NEW) |
713 | { |
|
|
714 | /* coroutine already started */ |
|
|
715 | SAVE (prev, flags); |
|
|
716 | LOAD (next); |
|
|
717 | } |
|
|
718 | else |
|
|
719 | { |
726 | { |
720 | /* need to start coroutine */ |
727 | /* need to start coroutine */ |
|
|
728 | next->flags &= ~CF_NEW; |
721 | /* first get rid of the old state */ |
729 | /* first get rid of the old state */ |
722 | SAVE (prev, -1); |
730 | SAVE (prev, -1); |
723 | /* setup coroutine call */ |
731 | /* setup coroutine call */ |
724 | setup_coro (next); |
732 | setup_coro (next); |
725 | /* need a new stack */ |
733 | /* need a new stack */ |
726 | assert (!next->stack); |
734 | assert (!next->stack); |
727 | } |
735 | } |
|
|
736 | else |
|
|
737 | { |
|
|
738 | /* coroutine already started */ |
|
|
739 | SAVE (prev, flags); |
|
|
740 | LOAD (next); |
|
|
741 | } |
728 | |
742 | |
729 | prev__cctx = prev->cctx; |
743 | prev__cctx = prev->cctx; |
730 | |
744 | |
731 | /* possibly "free" the cctx */ |
745 | /* possibly "free" the cctx */ |
732 | if (prev__cctx->idle_sp == STACKLEVEL) |
746 | if (prev__cctx->idle_sp == STACKLEVEL) |
733 | { |
747 | { |
734 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
748 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
735 | assert (PL_top_env == prev__cctx->top_env); |
749 | assert (PL_top_env == prev__cctx->top_env); |
736 | |
750 | |
|
|
751 | prev->cctx = 0; |
|
|
752 | |
737 | cctx_put (prev__cctx); |
753 | cctx_put (prev__cctx); |
738 | prev->cctx = 0; |
754 | prev__cctx->inuse = 0; |
739 | } |
755 | } |
740 | |
756 | |
741 | if (!next->cctx) |
757 | if (!next->cctx) |
|
|
758 | { |
742 | next->cctx = cctx_get (); |
759 | next->cctx = cctx_get (); |
|
|
760 | assert (!next->cctx->inuse); |
|
|
761 | next->cctx->inuse = 1; |
|
|
762 | } |
743 | |
763 | |
744 | if (prev__cctx != next->cctx) |
764 | if (prev__cctx != next->cctx) |
745 | { |
765 | { |
746 | assert ( prev__cctx->inuse); |
|
|
747 | assert (!next->cctx->inuse); |
|
|
748 | |
|
|
749 | prev__cctx->inuse = 0; |
|
|
750 | next->cctx->inuse = 1; |
|
|
751 | |
|
|
752 | prev__cctx->top_env = PL_top_env; |
766 | prev__cctx->top_env = PL_top_env; |
753 | PL_top_env = next->cctx->top_env; |
767 | PL_top_env = next->cctx->top_env; |
754 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
768 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
755 | } |
769 | } |
756 | |
770 | |
… | |
… | |
772 | coro_state_destroy (struct coro *coro) |
786 | coro_state_destroy (struct coro *coro) |
773 | { |
787 | { |
774 | if (coro->refcnt--) |
788 | if (coro->refcnt--) |
775 | return; |
789 | return; |
776 | |
790 | |
777 | if (coro->flags & CF_RUNNING) |
|
|
778 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
779 | |
|
|
780 | if (coro->mainstack && coro->mainstack != main_mainstack) |
791 | if (coro->mainstack && coro->mainstack != main_mainstack) |
781 | { |
792 | { |
782 | struct coro temp; |
793 | struct coro temp; |
|
|
794 | |
|
|
795 | if (coro->flags & CF_RUNNING) |
|
|
796 | croak ("FATAL: tried to destroy currently running coroutine"); |
783 | |
797 | |
784 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
798 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
785 | LOAD (coro); |
799 | LOAD (coro); |
786 | |
800 | |
787 | coro_destroy_stacks (); |
801 | coro_destroy_stacks (); |
… | |
… | |
859 | } |
873 | } |
860 | |
874 | |
861 | static void |
875 | static void |
862 | api_transfer (SV *prev, SV *next, int flags) |
876 | api_transfer (SV *prev, SV *next, int flags) |
863 | { |
877 | { |
864 | dTHX; |
|
|
865 | struct transfer_args ta; |
878 | struct transfer_args ta; |
866 | |
879 | |
867 | prepare_transfer (&ta, prev, next, flags); |
880 | prepare_transfer (&ta, prev, next, flags); |
868 | TRANSFER (ta); |
881 | TRANSFER (ta); |
869 | } |
882 | } |
… | |
… | |
876 | #define PRIO_LOW -1 |
889 | #define PRIO_LOW -1 |
877 | #define PRIO_IDLE -3 |
890 | #define PRIO_IDLE -3 |
878 | #define PRIO_MIN -4 |
891 | #define PRIO_MIN -4 |
879 | |
892 | |
880 | /* for Coro.pm */ |
893 | /* for Coro.pm */ |
881 | static GV *coro_current, *coro_idle; |
894 | static SV *coro_current; |
882 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
895 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
883 | static int coro_nready; |
896 | static int coro_nready; |
884 | |
897 | |
885 | static void |
898 | static void |
886 | coro_enq (SV *coro_sv) |
899 | coro_enq (SV *coro_sv) |
… | |
… | |
919 | coro = SvSTATE (coro_sv); |
932 | coro = SvSTATE (coro_sv); |
920 | |
933 | |
921 | if (coro->flags & CF_READY) |
934 | if (coro->flags & CF_READY) |
922 | return 0; |
935 | return 0; |
923 | |
936 | |
|
|
937 | #if 0 /* this is actually harmless */ |
924 | if (coro->flags & CF_RUNNING) |
938 | if (coro->flags & CF_RUNNING) |
925 | croak ("Coro::ready called on currently running coroutine"); |
939 | croak ("Coro::ready called on currently running coroutine"); |
|
|
940 | #endif |
926 | |
941 | |
927 | coro->flags |= CF_READY; |
942 | coro->flags |= CF_READY; |
928 | |
943 | |
929 | LOCK; |
944 | LOCK; |
930 | coro_enq (SvREFCNT_inc (coro_sv)); |
945 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
940 | } |
955 | } |
941 | |
956 | |
942 | static void |
957 | static void |
943 | prepare_schedule (struct transfer_args *ta) |
958 | prepare_schedule (struct transfer_args *ta) |
944 | { |
959 | { |
945 | SV *current, *prev, *next; |
960 | SV *prev, *next; |
946 | |
|
|
947 | current = GvSV (coro_current); |
|
|
948 | |
961 | |
949 | for (;;) |
962 | for (;;) |
950 | { |
963 | { |
951 | LOCK; |
964 | LOCK; |
952 | next = coro_deq (PRIO_MIN); |
965 | next = coro_deq (PRIO_MIN); |
… | |
… | |
961 | ENTER; |
974 | ENTER; |
962 | SAVETMPS; |
975 | SAVETMPS; |
963 | |
976 | |
964 | PUSHMARK (SP); |
977 | PUSHMARK (SP); |
965 | PUTBACK; |
978 | PUTBACK; |
966 | call_sv (GvSV (coro_idle), G_DISCARD); |
979 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
967 | |
980 | |
968 | FREETMPS; |
981 | FREETMPS; |
969 | LEAVE; |
982 | LEAVE; |
970 | } |
983 | } |
971 | } |
984 | } |
972 | |
985 | |
973 | prev = SvRV (current); |
986 | prev = SvRV (coro_current); |
974 | SvRV (current) = next; |
987 | SvRV_set (coro_current, next); |
975 | |
988 | |
976 | /* free this only after the transfer */ |
989 | /* free this only after the transfer */ |
977 | LOCK; |
990 | LOCK; |
978 | free_coro_mortal (); |
991 | free_coro_mortal (); |
979 | UNLOCK; |
992 | UNLOCK; |
980 | coro_mortal = prev; |
993 | coro_mortal = prev; |
981 | |
994 | |
|
|
995 | assert (!SvROK(prev));//D |
|
|
996 | assert (!SvROK(next));//D |
|
|
997 | |
982 | ta->prev = SvSTATE (prev); |
998 | ta->prev = SvSTATE (prev); |
983 | ta->next = SvSTATE (next); |
999 | ta->next = SvSTATE (next); |
984 | ta->flags = TRANSFER_SAVE_ALL; |
1000 | ta->flags = TRANSFER_SAVE_ALL; |
985 | |
1001 | |
|
|
1002 | assert (ta->flags & CF_READY); |
986 | ta->next->flags &= ~CF_READY; |
1003 | ta->next->flags &= ~CF_READY; |
987 | } |
1004 | } |
988 | |
1005 | |
989 | static void |
1006 | static void |
990 | prepare_cede (struct transfer_args *ta) |
1007 | prepare_cede (struct transfer_args *ta) |
991 | { |
1008 | { |
992 | api_ready (GvSV (coro_current)); |
1009 | api_ready (coro_current); |
993 | |
1010 | |
994 | prepare_schedule (ta); |
1011 | prepare_schedule (ta); |
995 | } |
1012 | } |
996 | |
1013 | |
997 | static void |
1014 | static void |
998 | api_schedule (void) |
1015 | api_schedule (void) |
999 | { |
1016 | { |
1000 | dTHX; |
|
|
1001 | struct transfer_args ta; |
1017 | struct transfer_args ta; |
1002 | |
1018 | |
1003 | prepare_schedule (&ta); |
1019 | prepare_schedule (&ta); |
1004 | TRANSFER (ta); |
1020 | TRANSFER (ta); |
1005 | } |
1021 | } |
1006 | |
1022 | |
1007 | static void |
1023 | static void |
1008 | api_cede (void) |
1024 | api_cede (void) |
1009 | { |
1025 | { |
1010 | dTHX; |
|
|
1011 | struct transfer_args ta; |
1026 | struct transfer_args ta; |
1012 | |
1027 | |
1013 | prepare_cede (&ta); |
1028 | prepare_cede (&ta); |
1014 | TRANSFER (ta); |
1029 | TRANSFER (ta); |
1015 | } |
1030 | } |
… | |
… | |
1047 | HV *hv; |
1062 | HV *hv; |
1048 | int i; |
1063 | int i; |
1049 | |
1064 | |
1050 | Newz (0, coro, 1, struct coro); |
1065 | Newz (0, coro, 1, struct coro); |
1051 | coro->args = newAV (); |
1066 | coro->args = newAV (); |
|
|
1067 | coro->flags = CF_NEW; |
1052 | |
1068 | |
1053 | hv = newHV (); |
1069 | hv = newHV (); |
1054 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1070 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1055 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1071 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1056 | |
1072 | |
… | |
… | |
1064 | _set_stacklevel (...) |
1080 | _set_stacklevel (...) |
1065 | ALIAS: |
1081 | ALIAS: |
1066 | Coro::State::transfer = 1 |
1082 | Coro::State::transfer = 1 |
1067 | Coro::schedule = 2 |
1083 | Coro::schedule = 2 |
1068 | Coro::cede = 3 |
1084 | Coro::cede = 3 |
1069 | Coro::Cont::yield = 4 |
|
|
1070 | CODE: |
1085 | CODE: |
1071 | { |
1086 | { |
1072 | struct transfer_args ta; |
1087 | struct transfer_args ta; |
1073 | |
1088 | |
1074 | switch (ix) |
1089 | switch (ix) |
… | |
… | |
1079 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1094 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1080 | break; |
1095 | break; |
1081 | |
1096 | |
1082 | case 1: |
1097 | case 1: |
1083 | if (items != 3) |
1098 | if (items != 3) |
1084 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1099 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1085 | |
1100 | |
1086 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1101 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1087 | break; |
1102 | break; |
1088 | |
1103 | |
1089 | case 2: |
1104 | case 2: |
… | |
… | |
1091 | break; |
1106 | break; |
1092 | |
1107 | |
1093 | case 3: |
1108 | case 3: |
1094 | prepare_cede (&ta); |
1109 | prepare_cede (&ta); |
1095 | break; |
1110 | break; |
1096 | |
|
|
1097 | case 4: |
|
|
1098 | { |
|
|
1099 | SV *yieldstack; |
|
|
1100 | SV *sv; |
|
|
1101 | AV *defav = GvAV (PL_defgv); |
|
|
1102 | |
|
|
1103 | yieldstack = *hv_fetch ( |
|
|
1104 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1105 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1106 | 0 |
|
|
1107 | ); |
|
|
1108 | |
|
|
1109 | /* set up @_ -- ugly */ |
|
|
1110 | av_clear (defav); |
|
|
1111 | av_fill (defav, items - 1); |
|
|
1112 | while (items--) |
|
|
1113 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1114 | |
|
|
1115 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1116 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1117 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1118 | ta.flags = 0; |
|
|
1119 | SvREFCNT_dec (sv); |
|
|
1120 | } |
|
|
1121 | break; |
|
|
1122 | |
|
|
1123 | } |
1111 | } |
1124 | |
1112 | |
1125 | TRANSFER (ta); |
1113 | TRANSFER (ta); |
1126 | } |
1114 | } |
1127 | |
1115 | |
… | |
… | |
1171 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1159 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1172 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1160 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1173 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1161 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1174 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1162 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1175 | |
1163 | |
1176 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1164 | coro_current = get_sv ("Coro::current", FALSE); |
1177 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1165 | SvREADONLY_on (coro_current); |
1178 | |
1166 | |
1179 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1167 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1180 | coro_ready[i] = newAV (); |
1168 | coro_ready[i] = newAV (); |
1181 | |
1169 | |
1182 | { |
1170 | { |
… | |
… | |
1237 | CODE: |
1225 | CODE: |
1238 | RETVAL = coro_nready; |
1226 | RETVAL = coro_nready; |
1239 | OUTPUT: |
1227 | OUTPUT: |
1240 | RETVAL |
1228 | RETVAL |
1241 | |
1229 | |
|
|
1230 | void |
|
|
1231 | _set_current (SV *current) |
|
|
1232 | PROTOTYPE: $ |
|
|
1233 | CODE: |
|
|
1234 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1235 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1236 | |
1242 | MODULE = Coro::State PACKAGE = Coro::AIO |
1237 | MODULE = Coro::State PACKAGE = Coro::AIO |
1243 | |
1238 | |
1244 | SV * |
1239 | SV * |
1245 | _get_state () |
1240 | _get_state () |
1246 | CODE: |
1241 | CODE: |
1247 | { |
1242 | { |
1248 | struct { |
1243 | RETVAL = newSV (sizeof (struct io_state)); |
1249 | int errorno; |
1244 | struct io_state *data = (struct io_state *)SvPVX (RETVAL); |
1250 | int laststype; |
1245 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
1251 | int laststatval; |
1246 | SvPOK_only (RETVAL); |
1252 | Stat_t statcache; |
|
|
1253 | } data; |
|
|
1254 | |
1247 | |
1255 | data.errorno = errno; |
1248 | data->errorno = errno; |
1256 | data.laststype = PL_laststype; |
1249 | data->laststype = PL_laststype; |
1257 | data.laststatval = PL_laststatval; |
1250 | data->laststatval = PL_laststatval; |
1258 | data.statcache = PL_statcache; |
1251 | data->statcache = PL_statcache; |
1259 | |
|
|
1260 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1261 | } |
1252 | } |
1262 | OUTPUT: |
1253 | OUTPUT: |
1263 | RETVAL |
1254 | RETVAL |
1264 | |
1255 | |
1265 | void |
1256 | void |
1266 | _set_state (char *data_) |
1257 | _set_state (char *data_) |
1267 | PROTOTYPE: $ |
1258 | PROTOTYPE: $ |
1268 | CODE: |
1259 | CODE: |
1269 | { |
1260 | { |
1270 | struct { |
1261 | struct io_state *data = (void *)data_; |
1271 | int errorno; |
|
|
1272 | int laststype; |
|
|
1273 | int laststatval; |
|
|
1274 | Stat_t statcache; |
|
|
1275 | } *data = (void *)data_; |
|
|
1276 | |
1262 | |
1277 | errno = data->errorno; |
1263 | errno = data->errorno; |
1278 | PL_laststype = data->laststype; |
1264 | PL_laststype = data->laststype; |
1279 | PL_laststatval = data->laststatval; |
1265 | PL_laststatval = data->laststatval; |
1280 | PL_statcache = data->statcache; |
1266 | PL_statcache = data->statcache; |
1281 | } |
1267 | } |
|
|
1268 | |