… | |
… | |
38 | # ifndef IS_PADCONST |
38 | # ifndef IS_PADCONST |
39 | # define IS_PADCONST(v) 0 |
39 | # define IS_PADCONST(v) 0 |
40 | # endif |
40 | # endif |
41 | #endif |
41 | #endif |
42 | |
42 | |
|
|
43 | /* 5.8.7 */ |
|
|
44 | #ifndef SvRV_set |
|
|
45 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
46 | #endif |
|
|
47 | |
43 | #include <stdio.h> |
48 | #include <stdio.h> |
44 | #include <errno.h> |
49 | #include <errno.h> |
|
|
50 | #include <assert.h> |
45 | |
51 | |
46 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
52 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
47 | # undef STACKGUARD |
53 | # undef STACKGUARD |
48 | #endif |
54 | #endif |
49 | |
55 | |
… | |
… | |
90 | |
96 | |
91 | #define NOINLINE attribute ((noinline)) |
97 | #define NOINLINE attribute ((noinline)) |
92 | |
98 | |
93 | #include "CoroAPI.h" |
99 | #include "CoroAPI.h" |
94 | |
100 | |
95 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
96 | |
|
|
97 | #ifdef USE_ITHREADS |
101 | #ifdef USE_ITHREADS |
98 | static perl_mutex coro_mutex; |
102 | static perl_mutex coro_mutex; |
99 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
103 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
100 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
104 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
101 | #else |
105 | #else |
102 | # define LOCK (void)0 |
106 | # define LOCK (void)0 |
103 | # define UNLOCK (void)0 |
107 | # define UNLOCK (void)0 |
104 | #endif |
108 | #endif |
105 | |
109 | |
|
|
110 | struct io_state |
|
|
111 | { |
|
|
112 | int errorno; |
|
|
113 | I32 laststype; |
|
|
114 | int laststatval; |
|
|
115 | Stat_t statcache; |
|
|
116 | }; |
|
|
117 | |
106 | static struct CoroAPI coroapi; |
118 | static struct CoroAPI coroapi; |
107 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
119 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
108 | static HV *coro_state_stash, *coro_stash; |
120 | static HV *coro_state_stash, *coro_stash; |
109 | static SV *coro_mortal; /* will be freed after next transfer */ |
121 | static SV *coro_mortal; /* will be freed after next transfer */ |
110 | |
122 | |
… | |
… | |
143 | coro_cctx *cctx; |
155 | coro_cctx *cctx; |
144 | |
156 | |
145 | /* data associated with this coroutine (initial args) */ |
157 | /* data associated with this coroutine (initial args) */ |
146 | AV *args; |
158 | AV *args; |
147 | int refcnt; |
159 | int refcnt; |
148 | int flags; |
160 | int save; /* CORO_SAVE flags */ |
|
|
161 | int flags; /* CF_ flags */ |
149 | |
162 | |
150 | /* optionally saved, might be zero */ |
163 | /* optionally saved, might be zero */ |
151 | AV *defav; |
164 | AV *defav; /* @_ */ |
152 | SV *defsv; |
165 | SV *defsv; /* $_ */ |
153 | SV *errsv; |
166 | SV *errsv; /* $@ */ |
|
|
167 | SV *irssv; /* $/ */ |
|
|
168 | SV *irssv_sv; /* real $/ cache */ |
154 | |
169 | |
155 | #define VAR(name,type) type name; |
170 | #define VAR(name,type) type name; |
156 | # include "state.h" |
171 | # include "state.h" |
157 | #undef VAR |
172 | #undef VAR |
158 | |
173 | |
… | |
… | |
281 | } |
296 | } |
282 | |
297 | |
283 | #define SB do { |
298 | #define SB do { |
284 | #define SE } while (0) |
299 | #define SE } while (0) |
285 | |
300 | |
286 | #define LOAD(state) load_state ((state)) |
|
|
287 | #define SAVE(state,flags) save_state ((state),(flags)) |
|
|
288 | |
|
|
289 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
301 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
290 | |
302 | |
291 | static void |
303 | static void |
292 | load_state (Coro__State c) |
304 | load_perl (Coro__State c) |
293 | { |
305 | { |
294 | #define VAR(name,type) PL_ ## name = c->name; |
306 | #define VAR(name,type) PL_ ## name = c->name; |
295 | # include "state.h" |
307 | # include "state.h" |
296 | #undef VAR |
308 | #undef VAR |
297 | |
309 | |
298 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
310 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
299 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
311 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
300 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
312 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
313 | if (c->irssv) |
|
|
314 | { |
|
|
315 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
316 | SvREFCNT_dec (c->irssv); |
|
|
317 | else |
|
|
318 | { |
|
|
319 | REPLACE_SV (PL_rs, c->irssv); |
|
|
320 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
321 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
322 | } |
|
|
323 | } |
301 | |
324 | |
302 | { |
325 | { |
303 | dSP; |
326 | dSP; |
304 | CV *cv; |
327 | CV *cv; |
305 | |
328 | |
… | |
… | |
314 | PUTBACK; |
337 | PUTBACK; |
315 | } |
338 | } |
316 | } |
339 | } |
317 | |
340 | |
318 | static void |
341 | static void |
319 | save_state (Coro__State c, int flags) |
342 | save_perl (Coro__State c) |
320 | { |
343 | { |
321 | { |
344 | { |
322 | dSP; |
345 | dSP; |
323 | I32 cxix = cxstack_ix; |
346 | I32 cxix = cxstack_ix; |
324 | PERL_CONTEXT *ccstk = cxstack; |
347 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
372 | } |
395 | } |
373 | |
396 | |
374 | PUTBACK; |
397 | PUTBACK; |
375 | } |
398 | } |
376 | |
399 | |
377 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
400 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
378 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
401 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
379 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
402 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
403 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
380 | |
404 | |
381 | #define VAR(name,type)c->name = PL_ ## name; |
405 | #define VAR(name,type)c->name = PL_ ## name; |
382 | # include "state.h" |
406 | # include "state.h" |
383 | #undef VAR |
407 | #undef VAR |
384 | } |
408 | } |
… | |
… | |
497 | |
521 | |
498 | { |
522 | { |
499 | dSP; |
523 | dSP; |
500 | LOGOP myop; |
524 | LOGOP myop; |
501 | |
525 | |
502 | /* I have no idea why this is needed, but it is */ |
|
|
503 | PUSHMARK (SP); |
|
|
504 | |
|
|
505 | SvREFCNT_dec (GvAV (PL_defgv)); |
526 | SvREFCNT_dec (GvAV (PL_defgv)); |
506 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
527 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
507 | |
528 | |
508 | Zero (&myop, 1, LOGOP); |
529 | Zero (&myop, 1, LOGOP); |
509 | myop.op_next = Nullop; |
530 | myop.op_next = Nullop; |
510 | myop.op_flags = OPf_WANT_VOID; |
531 | myop.op_flags = OPf_WANT_VOID; |
511 | |
532 | |
512 | PL_op = (OP *)&myop; |
|
|
513 | |
|
|
514 | PUSHMARK (SP); |
533 | PUSHMARK (SP); |
515 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
534 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
516 | PUTBACK; |
535 | PUTBACK; |
|
|
536 | PL_op = (OP *)&myop; |
517 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
537 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
518 | SPAGAIN; |
538 | SPAGAIN; |
519 | } |
539 | } |
520 | |
540 | |
521 | ENTER; /* necessary e.g. for dounwind */ |
541 | ENTER; /* necessary e.g. for dounwind */ |
… | |
… | |
529 | SvREFCNT_dec (coro_mortal); |
549 | SvREFCNT_dec (coro_mortal); |
530 | coro_mortal = 0; |
550 | coro_mortal = 0; |
531 | } |
551 | } |
532 | } |
552 | } |
533 | |
553 | |
|
|
554 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
534 | static void NOINLINE |
555 | static void NOINLINE |
535 | prepare_cctx (coro_cctx *cctx) |
556 | prepare_cctx (coro_cctx *cctx) |
536 | { |
557 | { |
537 | dSP; |
558 | dSP; |
538 | LOGOP myop; |
559 | LOGOP myop; |
539 | |
560 | |
540 | Zero (&myop, 1, LOGOP); |
561 | Zero (&myop, 1, LOGOP); |
541 | myop.op_next = PL_op; |
562 | myop.op_next = PL_op; |
542 | myop.op_flags = OPf_WANT_VOID; |
563 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
543 | |
|
|
544 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
545 | |
564 | |
546 | PUSHMARK (SP); |
565 | PUSHMARK (SP); |
|
|
566 | EXTEND (SP, 2); |
|
|
567 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
547 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
568 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
548 | PUTBACK; |
569 | PUTBACK; |
|
|
570 | PL_op = (OP *)&myop; |
549 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
571 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
550 | SPAGAIN; |
572 | SPAGAIN; |
551 | } |
573 | } |
552 | |
574 | |
553 | static void |
575 | static void |
554 | coro_run (void *arg) |
576 | coro_run (void *arg) |
555 | { |
577 | { |
556 | /* coro_run is the alternative epilogue of transfer() */ |
578 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
557 | UNLOCK; |
579 | UNLOCK; |
558 | |
580 | |
559 | /* |
581 | /* |
560 | * this is a _very_ stripped down perl interpreter ;) |
582 | * this is a _very_ stripped down perl interpreter ;) |
561 | */ |
583 | */ |
… | |
… | |
563 | |
585 | |
564 | /* inject call to cctx_init */ |
586 | /* inject call to cctx_init */ |
565 | prepare_cctx ((coro_cctx *)arg); |
587 | prepare_cctx ((coro_cctx *)arg); |
566 | |
588 | |
567 | /* somebody will hit me for both perl_run and PL_restartop */ |
589 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
590 | PL_restartop = PL_op; |
568 | perl_run (PL_curinterp); |
591 | perl_run (PL_curinterp); |
569 | |
592 | |
570 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
593 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
571 | abort (); |
594 | abort (); |
572 | } |
595 | } |
… | |
… | |
576 | { |
599 | { |
577 | coro_cctx *cctx; |
600 | coro_cctx *cctx; |
578 | |
601 | |
579 | ++cctx_count; |
602 | ++cctx_count; |
580 | |
603 | |
581 | New (0, cctx, 1, coro_cctx); |
604 | Newz (0, cctx, 1, coro_cctx); |
582 | |
605 | |
583 | #if HAVE_MMAP |
606 | #if HAVE_MMAP |
584 | |
607 | |
585 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
608 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
586 | /* mmap suppsedly does allocate-on-write for us */ |
609 | /* mmap supposedly does allocate-on-write for us */ |
587 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
610 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
588 | |
611 | |
589 | if (cctx->sptr == (void *)-1) |
612 | if (cctx->sptr == (void *)-1) |
590 | { |
613 | { |
591 | perror ("FATAL: unable to mmap stack for coroutine"); |
614 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
681 | cctx_first = cctx; |
704 | cctx_first = cctx; |
682 | } |
705 | } |
683 | |
706 | |
684 | /* never call directly, always through the coro_state_transfer global variable */ |
707 | /* never call directly, always through the coro_state_transfer global variable */ |
685 | static void NOINLINE |
708 | static void NOINLINE |
686 | transfer (struct coro *prev, struct coro *next, int flags) |
709 | transfer (struct coro *prev, struct coro *next) |
687 | { |
710 | { |
688 | dSTACKLEVEL; |
711 | dSTACKLEVEL; |
689 | |
712 | |
690 | /* sometimes transfer is only called to set idle_sp */ |
713 | /* sometimes transfer is only called to set idle_sp */ |
691 | if (flags == TRANSFER_SET_STACKLEVEL) |
714 | if (!next) |
|
|
715 | { |
692 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
716 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
717 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
|
|
718 | } |
693 | else if (prev != next) |
719 | else if (prev != next) |
694 | { |
720 | { |
695 | coro_cctx *prev__cctx; |
721 | coro_cctx *prev__cctx; |
696 | |
722 | |
697 | if (prev->flags & CF_NEW) |
723 | if (prev->flags & CF_NEW) |
… | |
… | |
718 | if (next->flags & CF_NEW) |
744 | if (next->flags & CF_NEW) |
719 | { |
745 | { |
720 | /* need to start coroutine */ |
746 | /* need to start coroutine */ |
721 | next->flags &= ~CF_NEW; |
747 | next->flags &= ~CF_NEW; |
722 | /* first get rid of the old state */ |
748 | /* first get rid of the old state */ |
723 | SAVE (prev, -1); |
749 | save_perl (prev); |
724 | /* setup coroutine call */ |
750 | /* setup coroutine call */ |
725 | setup_coro (next); |
751 | setup_coro (next); |
726 | /* need a new stack */ |
752 | /* need a new stack */ |
727 | assert (!next->stack); |
753 | assert (!next->cctx); |
728 | } |
754 | } |
729 | else |
755 | else |
730 | { |
756 | { |
731 | /* coroutine already started */ |
757 | /* coroutine already started */ |
732 | SAVE (prev, flags); |
758 | save_perl (prev); |
733 | LOAD (next); |
759 | load_perl (next); |
734 | } |
760 | } |
735 | |
761 | |
736 | prev__cctx = prev->cctx; |
762 | prev__cctx = prev->cctx; |
737 | |
763 | |
738 | /* possibly "free" the cctx */ |
764 | /* possibly "free" the cctx */ |
739 | if (prev__cctx->idle_sp == STACKLEVEL && 0) |
765 | if (prev__cctx->idle_sp == STACKLEVEL) |
740 | { |
766 | { |
741 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
767 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
742 | assert (PL_top_env == prev__cctx->top_env); |
768 | assert (PL_top_env == prev__cctx->top_env); |
743 | |
769 | |
744 | prev->cctx = 0; |
770 | prev->cctx = 0; |
… | |
… | |
768 | } |
794 | } |
769 | |
795 | |
770 | struct transfer_args |
796 | struct transfer_args |
771 | { |
797 | { |
772 | struct coro *prev, *next; |
798 | struct coro *prev, *next; |
773 | int flags; |
|
|
774 | }; |
799 | }; |
775 | |
800 | |
776 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
801 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
777 | |
802 | |
778 | static void |
803 | static void |
779 | coro_state_destroy (struct coro *coro) |
804 | coro_state_destroy (struct coro *coro) |
780 | { |
805 | { |
781 | if (coro->refcnt--) |
806 | if (coro->refcnt--) |
782 | return; |
807 | return; |
783 | |
808 | |
784 | if (coro->mainstack && coro->mainstack != main_mainstack) |
809 | if (coro->mainstack && coro->mainstack != main_mainstack) |
785 | { |
810 | { |
786 | struct coro temp; |
811 | struct coro temp; |
|
|
812 | Zero (&temp, 1, struct coro); |
|
|
813 | temp.save = CORO_SAVE_ALL; |
787 | |
814 | |
788 | if (coro->flags & CF_RUNNING) |
815 | if (coro->flags & CF_RUNNING) |
789 | croak ("FATAL: tried to destroy currently running coroutine"); |
816 | croak ("FATAL: tried to destroy currently running coroutine"); |
790 | |
817 | |
791 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
818 | save_perl (&temp); |
792 | LOAD (coro); |
819 | load_perl (coro); |
793 | |
820 | |
794 | coro_destroy_stacks (); |
821 | coro_destroy_stacks (); |
795 | |
822 | |
796 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
823 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
797 | |
824 | |
798 | coro->mainstack = 0; |
825 | coro->mainstack = 0; |
799 | } |
826 | } |
800 | |
827 | |
801 | cctx_destroy (coro->cctx); |
828 | cctx_destroy (coro->cctx); |
… | |
… | |
856 | assert (mg->mg_type == PERL_MAGIC_ext); |
883 | assert (mg->mg_type == PERL_MAGIC_ext); |
857 | return (struct coro *)mg->mg_ptr; |
884 | return (struct coro *)mg->mg_ptr; |
858 | } |
885 | } |
859 | |
886 | |
860 | static void |
887 | static void |
861 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
888 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
862 | { |
889 | { |
863 | ta->prev = SvSTATE (prev); |
890 | ta->prev = SvSTATE (prev_sv); |
864 | ta->next = SvSTATE (next); |
891 | ta->next = SvSTATE (next_sv); |
865 | ta->flags = flags; |
|
|
866 | } |
892 | } |
867 | |
893 | |
868 | static void |
894 | static void |
869 | api_transfer (SV *prev, SV *next, int flags) |
895 | api_transfer (SV *prev_sv, SV *next_sv) |
870 | { |
896 | { |
871 | struct transfer_args ta; |
897 | struct transfer_args ta; |
872 | |
898 | |
873 | prepare_transfer (&ta, prev, next, flags); |
899 | prepare_transfer (&ta, prev_sv, next_sv); |
874 | TRANSFER (ta); |
900 | TRANSFER (ta); |
|
|
901 | } |
|
|
902 | |
|
|
903 | static int |
|
|
904 | api_save (SV *coro_sv, int new_save) |
|
|
905 | { |
|
|
906 | struct coro *coro = SvSTATE (coro_sv); |
|
|
907 | int old_save = coro->save; |
|
|
908 | |
|
|
909 | if (new_save >= 0) |
|
|
910 | coro->save = new_save; |
|
|
911 | |
|
|
912 | return old_save; |
875 | } |
913 | } |
876 | |
914 | |
877 | /** Coro ********************************************************************/ |
915 | /** Coro ********************************************************************/ |
878 | |
916 | |
879 | #define PRIO_MAX 3 |
917 | #define PRIO_MAX 3 |
… | |
… | |
988 | assert (!SvROK(prev));//D |
1026 | assert (!SvROK(prev));//D |
989 | assert (!SvROK(next));//D |
1027 | assert (!SvROK(next));//D |
990 | |
1028 | |
991 | ta->prev = SvSTATE (prev); |
1029 | ta->prev = SvSTATE (prev); |
992 | ta->next = SvSTATE (next); |
1030 | ta->next = SvSTATE (next); |
993 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
994 | |
1031 | |
995 | assert (ta->flags & CF_READY); |
1032 | assert (ta->next->flags & CF_READY); |
996 | ta->next->flags &= ~CF_READY; |
1033 | ta->next->flags &= ~CF_READY; |
997 | } |
1034 | } |
998 | |
1035 | |
999 | static void |
1036 | static void |
1000 | prepare_cede (struct transfer_args *ta) |
1037 | prepare_cede (struct transfer_args *ta) |
… | |
… | |
1033 | #endif |
1070 | #endif |
1034 | BOOT_PAGESIZE; |
1071 | BOOT_PAGESIZE; |
1035 | |
1072 | |
1036 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1073 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1037 | |
1074 | |
1038 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1075 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1039 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1076 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1040 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1077 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1078 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1079 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1041 | |
1080 | |
1042 | main_mainstack = PL_mainstack; |
1081 | main_mainstack = PL_mainstack; |
1043 | |
1082 | |
1044 | coroapi.ver = CORO_API_VERSION; |
1083 | coroapi.ver = CORO_API_VERSION; |
1045 | coroapi.transfer = api_transfer; |
1084 | coroapi.transfer = api_transfer; |
… | |
… | |
1055 | HV *hv; |
1094 | HV *hv; |
1056 | int i; |
1095 | int i; |
1057 | |
1096 | |
1058 | Newz (0, coro, 1, struct coro); |
1097 | Newz (0, coro, 1, struct coro); |
1059 | coro->args = newAV (); |
1098 | coro->args = newAV (); |
|
|
1099 | coro->save = CORO_SAVE_ALL; |
1060 | coro->flags = CF_NEW; |
1100 | coro->flags = CF_NEW; |
1061 | |
1101 | |
1062 | hv = newHV (); |
1102 | hv = newHV (); |
1063 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1103 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1064 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1104 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1065 | |
1105 | |
1066 | for (i = 1; i < items; i++) |
1106 | for (i = 1; i < items; i++) |
1067 | av_push (coro->args, newSVsv (ST (i))); |
1107 | av_push (coro->args, newSVsv (ST (i))); |
1068 | } |
1108 | } |
1069 | OUTPUT: |
1109 | OUTPUT: |
|
|
1110 | RETVAL |
|
|
1111 | |
|
|
1112 | int |
|
|
1113 | save (SV *coro, int new_save = -1) |
|
|
1114 | CODE: |
|
|
1115 | RETVAL = api_save (coro, new_save); |
|
|
1116 | OUTPUT: |
1070 | RETVAL |
1117 | RETVAL |
1071 | |
1118 | |
1072 | void |
1119 | void |
1073 | _set_stacklevel (...) |
1120 | _set_stacklevel (...) |
1074 | ALIAS: |
1121 | ALIAS: |
… | |
… | |
1082 | switch (ix) |
1129 | switch (ix) |
1083 | { |
1130 | { |
1084 | case 0: |
1131 | case 0: |
1085 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1132 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1086 | ta.next = 0; |
1133 | ta.next = 0; |
1087 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1088 | break; |
1134 | break; |
1089 | |
1135 | |
1090 | case 1: |
1136 | case 1: |
1091 | if (items != 3) |
1137 | if (items != 2) |
1092 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1138 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1093 | |
1139 | |
1094 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1140 | prepare_transfer (&ta, ST (0), ST (1)); |
1095 | break; |
1141 | break; |
1096 | |
1142 | |
1097 | case 2: |
1143 | case 2: |
1098 | prepare_schedule (&ta); |
1144 | prepare_schedule (&ta); |
1099 | break; |
1145 | break; |
… | |
… | |
1162 | |
1208 | |
1163 | { |
1209 | { |
1164 | SV *sv = perl_get_sv("Coro::API", 1); |
1210 | SV *sv = perl_get_sv("Coro::API", 1); |
1165 | |
1211 | |
1166 | coroapi.schedule = api_schedule; |
1212 | coroapi.schedule = api_schedule; |
|
|
1213 | coroapi.save = api_save; |
1167 | coroapi.cede = api_cede; |
1214 | coroapi.cede = api_cede; |
1168 | coroapi.ready = api_ready; |
1215 | coroapi.ready = api_ready; |
1169 | coroapi.is_ready = api_is_ready; |
1216 | coroapi.is_ready = api_is_ready; |
1170 | coroapi.nready = &coro_nready; |
1217 | coroapi.nready = &coro_nready; |
1171 | coroapi.current = coro_current; |
1218 | coroapi.current = coro_current; |
… | |
… | |
1173 | GCoroAPI = &coroapi; |
1220 | GCoroAPI = &coroapi; |
1174 | sv_setiv (sv, (IV)&coroapi); |
1221 | sv_setiv (sv, (IV)&coroapi); |
1175 | SvREADONLY_on (sv); |
1222 | SvREADONLY_on (sv); |
1176 | } |
1223 | } |
1177 | } |
1224 | } |
|
|
1225 | |
|
|
1226 | void |
|
|
1227 | _set_current (SV *current) |
|
|
1228 | PROTOTYPE: $ |
|
|
1229 | CODE: |
|
|
1230 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1231 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1178 | |
1232 | |
1179 | int |
1233 | int |
1180 | prio (Coro::State coro, int newprio = 0) |
1234 | prio (Coro::State coro, int newprio = 0) |
1181 | ALIAS: |
1235 | ALIAS: |
1182 | nice = 1 |
1236 | nice = 1 |
… | |
… | |
1218 | CODE: |
1272 | CODE: |
1219 | RETVAL = coro_nready; |
1273 | RETVAL = coro_nready; |
1220 | OUTPUT: |
1274 | OUTPUT: |
1221 | RETVAL |
1275 | RETVAL |
1222 | |
1276 | |
1223 | void |
|
|
1224 | _set_current (SV *current) |
|
|
1225 | PROTOTYPE: $ |
|
|
1226 | CODE: |
|
|
1227 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1228 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1229 | |
|
|
1230 | MODULE = Coro::State PACKAGE = Coro::AIO |
1277 | MODULE = Coro::State PACKAGE = Coro::AIO |
1231 | |
1278 | |
1232 | SV * |
1279 | SV * |
1233 | _get_state () |
1280 | _get_state () |
1234 | CODE: |
1281 | CODE: |
1235 | { |
1282 | { |
1236 | struct { |
1283 | struct io_state *data; |
1237 | int errorno; |
|
|
1238 | int laststype; |
|
|
1239 | int laststatval; |
|
|
1240 | Stat_t statcache; |
|
|
1241 | } data; |
|
|
1242 | |
1284 | |
|
|
1285 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1286 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1287 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1288 | SvPOK_only (RETVAL); |
|
|
1289 | |
1243 | data.errorno = errno; |
1290 | data->errorno = errno; |
1244 | data.laststype = PL_laststype; |
1291 | data->laststype = PL_laststype; |
1245 | data.laststatval = PL_laststatval; |
1292 | data->laststatval = PL_laststatval; |
1246 | data.statcache = PL_statcache; |
1293 | data->statcache = PL_statcache; |
1247 | |
|
|
1248 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1249 | } |
1294 | } |
1250 | OUTPUT: |
1295 | OUTPUT: |
1251 | RETVAL |
1296 | RETVAL |
1252 | |
1297 | |
1253 | void |
1298 | void |
1254 | _set_state (char *data_) |
1299 | _set_state (char *data_) |
1255 | PROTOTYPE: $ |
1300 | PROTOTYPE: $ |
1256 | CODE: |
1301 | CODE: |
1257 | { |
1302 | { |
1258 | struct { |
1303 | struct io_state *data = (void *)data_; |
1259 | int errorno; |
|
|
1260 | int laststype; |
|
|
1261 | int laststatval; |
|
|
1262 | Stat_t statcache; |
|
|
1263 | } *data = (void *)data_; |
|
|
1264 | |
1304 | |
1265 | errno = data->errorno; |
1305 | errno = data->errorno; |
1266 | PL_laststype = data->laststype; |
1306 | PL_laststype = data->laststype; |
1267 | PL_laststatval = data->laststatval; |
1307 | PL_laststatval = data->laststatval; |
1268 | PL_statcache = data->statcache; |
1308 | PL_statcache = data->statcache; |
1269 | } |
1309 | } |
|
|
1310 | |