ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.117 by root, Fri Dec 1 19:41:06 2006 UTC vs.
Revision 1.124 by root, Tue Dec 5 12:50:04 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines