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.56 by pcg, Thu Apr 1 02:41:05 2004 UTC vs.
Revision 1.76 by root, Thu Oct 26 08:45:56 2006 UTC

1#define PERL_NO_GET_CONTEXT 1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c"
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
7#include "patchlevel.h" 9#include "patchlevel.h"
8 10
9#if PATCHLEVEL < 6 11#if PERL_VERSION < 6
10# ifndef PL_ppaddr 12# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 13# define PL_ppaddr ppaddr
12# endif 14# endif
13# ifndef call_sv 15# ifndef call_sv
14# define call_sv perl_call_sv 16# define call_sv perl_call_sv
25# ifndef IS_PADCONST 27# ifndef IS_PADCONST
26# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
27# endif 29# endif
28#endif 30#endif
29 31
30#include "libcoro/coro.c"
31
32#include <signal.h> 32#include <errno.h>
33 33
34#ifdef HAVE_MMAP 34#ifdef HAVE_MMAP
35# include <unistd.h> 35# include <unistd.h>
36# include <sys/mman.h> 36# include <sys/mman.h>
37# ifndef MAP_ANONYMOUS 37# ifndef MAP_ANONYMOUS
57 57
58#include "CoroAPI.h" 58#include "CoroAPI.h"
59 59
60#ifdef USE_ITHREADS 60#ifdef USE_ITHREADS
61static perl_mutex coro_mutex; 61static perl_mutex coro_mutex;
62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
64#else 64#else
65# define LOCK 0 65# define LOCK (void)0
66# define UNLOCK 0 66# define UNLOCK (void)0
67#endif 67#endif
68 68
69static struct CoroAPI coroapi; 69static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 70static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 71static HV *coro_state_stash;
72static SV *ucoro_state_sv; 72static SV *ucoro_state_sv;
73static U32 ucoro_state_hash; 73static U32 ucoro_state_hash;
74static __thread SV *coro_mortal; /* will be freed after next transfer */ 74static SV *coro_mortal; /* will be freed after next transfer */
75 75
76/* this is actually not only the c stack but also c registers etc... */ 76/* this is actually not only the c stack but also c registers etc... */
77typedef struct { 77typedef struct {
78 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
79 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
109 AV *mainstack; 109 AV *mainstack;
110 SV **stack_sp; 110 SV **stack_sp;
111 OP *op; 111 OP *op;
112 SV **curpad; 112 SV **curpad;
113 AV *comppad; 113 AV *comppad;
114 CV *compcv;
114 SV **stack_base; 115 SV **stack_base;
115 SV **stack_max; 116 SV **stack_max;
116 SV **tmps_stack; 117 SV **tmps_stack;
117 I32 tmps_floor; 118 I32 tmps_floor;
118 I32 tmps_ix; 119 I32 tmps_ix;
127 I32 savestack_ix; 128 I32 savestack_ix;
128 I32 savestack_max; 129 I32 savestack_max;
129 OP **retstack; 130 OP **retstack;
130 I32 retstack_ix; 131 I32 retstack_ix;
131 I32 retstack_max; 132 I32 retstack_max;
133 PMOP *curpm;
132 COP *curcop; 134 COP *curcop;
133 JMPENV *top_env; 135 JMPENV *top_env;
134 136
135 /* data associated with this coroutine (initial args) */ 137 /* data associated with this coroutine (initial args) */
136 AV *args; 138 AV *args;
168 av_store (newpadlist, 1, (SV *) newpad); 170 av_store (newpadlist, 1, (SV *) newpad);
169 171
170 av = newAV (); /* will be @_ */ 172 av = newAV (); /* will be @_ */
171 av_extend (av, 0); 173 av_extend (av, 0);
172 av_store (newpad, 0, (SV *) av); 174 av_store (newpad, 0, (SV *) av);
173 AvFLAGS (av) = AVf_REIFY; 175 AvREIFY_on (av);
174 176
175 for (ix = fpad; ix > 0; ix--) 177 for (ix = fpad; ix > 0; ix--)
176 { 178 {
177 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 179 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
178 180
270 AV *av = (AV *)mg->mg_obj; 272 AV *av = (AV *)mg->mg_obj;
271 273
272 /* casting is fun. */ 274 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 275 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist); 276 free_padlist (aTHX_ padlist);
277
278 SvREFCNT_dec (av);
279
280 return 0;
275} 281}
276 282
277#define PERL_MAGIC_coro PERL_MAGIC_ext 283#define PERL_MAGIC_coro PERL_MAGIC_ext
278 284
279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 285static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
325 PL_mainstack = c->mainstack; 331 PL_mainstack = c->mainstack;
326 PL_stack_sp = c->stack_sp; 332 PL_stack_sp = c->stack_sp;
327 PL_op = c->op; 333 PL_op = c->op;
328 PL_curpad = c->curpad; 334 PL_curpad = c->curpad;
329 PL_comppad = c->comppad; 335 PL_comppad = c->comppad;
336 PL_compcv = c->compcv;
330 PL_stack_base = c->stack_base; 337 PL_stack_base = c->stack_base;
331 PL_stack_max = c->stack_max; 338 PL_stack_max = c->stack_max;
332 PL_tmps_stack = c->tmps_stack; 339 PL_tmps_stack = c->tmps_stack;
333 PL_tmps_floor = c->tmps_floor; 340 PL_tmps_floor = c->tmps_floor;
334 PL_tmps_ix = c->tmps_ix; 341 PL_tmps_ix = c->tmps_ix;
340 PL_scopestack_ix = c->scopestack_ix; 347 PL_scopestack_ix = c->scopestack_ix;
341 PL_scopestack_max = c->scopestack_max; 348 PL_scopestack_max = c->scopestack_max;
342 PL_savestack = c->savestack; 349 PL_savestack = c->savestack;
343 PL_savestack_ix = c->savestack_ix; 350 PL_savestack_ix = c->savestack_ix;
344 PL_savestack_max = c->savestack_max; 351 PL_savestack_max = c->savestack_max;
352#if PERL_VERSION < 9
345 PL_retstack = c->retstack; 353 PL_retstack = c->retstack;
346 PL_retstack_ix = c->retstack_ix; 354 PL_retstack_ix = c->retstack_ix;
347 PL_retstack_max = c->retstack_max; 355 PL_retstack_max = c->retstack_max;
356#endif
357 PL_curpm = c->curpm;
348 PL_curcop = c->curcop; 358 PL_curcop = c->curcop;
349 PL_top_env = c->top_env; 359 PL_top_env = c->top_env;
350 360
351 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 361 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
352 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 362 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
449 c->mainstack = PL_mainstack; 459 c->mainstack = PL_mainstack;
450 c->stack_sp = PL_stack_sp; 460 c->stack_sp = PL_stack_sp;
451 c->op = PL_op; 461 c->op = PL_op;
452 c->curpad = PL_curpad; 462 c->curpad = PL_curpad;
453 c->comppad = PL_comppad; 463 c->comppad = PL_comppad;
464 c->compcv = PL_compcv;
454 c->stack_base = PL_stack_base; 465 c->stack_base = PL_stack_base;
455 c->stack_max = PL_stack_max; 466 c->stack_max = PL_stack_max;
456 c->tmps_stack = PL_tmps_stack; 467 c->tmps_stack = PL_tmps_stack;
457 c->tmps_floor = PL_tmps_floor; 468 c->tmps_floor = PL_tmps_floor;
458 c->tmps_ix = PL_tmps_ix; 469 c->tmps_ix = PL_tmps_ix;
464 c->scopestack_ix = PL_scopestack_ix; 475 c->scopestack_ix = PL_scopestack_ix;
465 c->scopestack_max = PL_scopestack_max; 476 c->scopestack_max = PL_scopestack_max;
466 c->savestack = PL_savestack; 477 c->savestack = PL_savestack;
467 c->savestack_ix = PL_savestack_ix; 478 c->savestack_ix = PL_savestack_ix;
468 c->savestack_max = PL_savestack_max; 479 c->savestack_max = PL_savestack_max;
480#if PERL_VERSION < 9
469 c->retstack = PL_retstack; 481 c->retstack = PL_retstack;
470 c->retstack_ix = PL_retstack_ix; 482 c->retstack_ix = PL_retstack_ix;
471 c->retstack_max = PL_retstack_max; 483 c->retstack_max = PL_retstack_max;
484#endif
485 c->curpm = PL_curpm;
472 c->curcop = PL_curcop; 486 c->curcop = PL_curcop;
473 c->top_env = PL_top_env; 487 c->top_env = PL_top_env;
474} 488}
475 489
476/* 490/*
512 526
513 New(54,PL_savestack,96,ANY); 527 New(54,PL_savestack,96,ANY);
514 PL_savestack_ix = 0; 528 PL_savestack_ix = 0;
515 PL_savestack_max = 96; 529 PL_savestack_max = 96;
516 530
531#if PERL_VERSION < 9
517 New(54,PL_retstack,8,OP*); 532 New(54,PL_retstack,8,OP*);
518 PL_retstack_ix = 0; 533 PL_retstack_ix = 0;
519 PL_retstack_max = 8; 534 PL_retstack_max = 8;
535#endif
520 536
521 UNLOCK; 537 UNLOCK;
522} 538}
523 539
524/* 540/*
532 /* is this ugly, I ask? */ 548 /* is this ugly, I ask? */
533 LEAVE_SCOPE (0); 549 LEAVE_SCOPE (0);
534 550
535 /* sure it is, but more important: is it correct?? :/ */ 551 /* sure it is, but more important: is it correct?? :/ */
536 FREETMPS; 552 FREETMPS;
553
554 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
537 } 555 }
538 556
539 while (PL_curstackinfo->si_next) 557 while (PL_curstackinfo->si_next)
540 PL_curstackinfo = PL_curstackinfo->si_next; 558 PL_curstackinfo = PL_curstackinfo->si_next;
541 559
542 while (PL_curstackinfo) 560 while (PL_curstackinfo)
543 { 561 {
544 PERL_SI *p = PL_curstackinfo->si_prev; 562 PERL_SI *p = PL_curstackinfo->si_prev;
545 563
546 { 564 { /*D*//*remove*/
547 dSP; 565 dSP;
548 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 566 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
549 PUTBACK; /* possibly superfluous */ 567 PUTBACK; /* possibly superfluous */
550 } 568 }
551 569
552 if (!IN_DESTRUCT) 570 if (!IN_DESTRUCT)
553 { 571 {
554 dounwind(-1); 572 dounwind (-1);/*D*//*remove*/
555 SvREFCNT_dec(PL_curstackinfo->si_stack); 573 SvREFCNT_dec (PL_curstackinfo->si_stack);
556 } 574 }
557 575
558 Safefree(PL_curstackinfo->si_cxstack); 576 Safefree (PL_curstackinfo->si_cxstack);
559 Safefree(PL_curstackinfo); 577 Safefree (PL_curstackinfo);
560 PL_curstackinfo = p; 578 PL_curstackinfo = p;
561 } 579 }
562 580
563 Safefree(PL_tmps_stack); 581 Safefree (PL_tmps_stack);
564 Safefree(PL_markstack); 582 Safefree (PL_markstack);
565 Safefree(PL_scopestack); 583 Safefree (PL_scopestack);
566 Safefree(PL_savestack); 584 Safefree (PL_savestack);
585#if PERL_VERSION < 9
567 Safefree(PL_retstack); 586 Safefree (PL_retstack);
587#endif
568} 588}
569 589
570static void 590static void
571allocate_stack (Coro__State ctx, int alloc) 591allocate_stack (Coro__State ctx, int alloc)
572{ 592{
579 stack->gencnt = ctx->gencnt = 0; 599 stack->gencnt = ctx->gencnt = 0;
580 600
581 if (alloc) 601 if (alloc)
582 { 602 {
583#if HAVE_MMAP 603#if HAVE_MMAP
584 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 604 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
585 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 605 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
586 if (stack->sptr == (void *)-1) 606 if (stack->sptr == (void *)-1)
587#endif 607#endif
588 { 608 {
589 /*FIXME*//*D*//* reasonable stack size! */
590 stack->ssize = - (8192 * sizeof (long)); 609 stack->ssize = - (STACKSIZE * (long)sizeof (long));
591 New (0, stack->sptr, 8192, long); 610 New (0, stack->sptr, STACKSIZE, long);
592 } 611 }
593 } 612 }
594 else 613 else
595 stack->sptr = 0; 614 stack->sptr = 0;
596 615
687 /* 706 /*
688 * this is a _very_ stripped down perl interpreter ;) 707 * this is a _very_ stripped down perl interpreter ;)
689 */ 708 */
690 dTHX; 709 dTHX;
691 Coro__State ctx = (Coro__State)arg; 710 Coro__State ctx = (Coro__State)arg;
692 JMPENV coro_start_env;
693 711
694 PL_top_env = &ctx->start_env; 712 PL_top_env = &ctx->start_env;
695 713
696 ctx->cursp = 0; 714 ctx->cursp = 0;
697 PL_op = PL_op->op_next; 715 PL_op = PL_op->op_next;
820 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 838 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
821 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 839 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
822 \ 840 \
823 } while(0) 841 } while(0)
824 842
825#define SvSTATE(sv) (struct coro *)SvIV (sv) 843#define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv))
826 844
827static void 845static void
828api_transfer(pTHX_ SV *prev, SV *next, int flags) 846api_transfer(pTHX_ SV *prev, SV *next, int flags)
829{ 847{
830 SV_CORO (prev, "Coro::transfer"); 848 SV_CORO (prev, "Coro::transfer");
848static int coro_nready; 866static int coro_nready;
849 867
850static void 868static void
851coro_enq (pTHX_ SV *sv) 869coro_enq (pTHX_ SV *sv)
852{ 870{
871 SV **xprio;
872 int prio;
873
853 if (SvTYPE (sv) == SVt_PVHV) 874 if (SvTYPE (sv) != SVt_PVHV)
854 {
855 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
856 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
857
858 prio = prio > PRIO_MAX ? PRIO_MAX
859 : prio < PRIO_MIN ? PRIO_MIN
860 : prio;
861
862 av_push (coro_ready [prio - PRIO_MIN], sv);
863 coro_nready++;
864
865 return;
866 }
867
868 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 875 croak ("Coro::ready tried to enqueue something that is not a coroutine");
876
877 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
878 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
879
880 prio = prio > PRIO_MAX ? PRIO_MAX
881 : prio < PRIO_MIN ? PRIO_MIN
882 : prio;
883
884 av_push (coro_ready [prio - PRIO_MIN], sv);
885 coro_nready++;
869} 886}
870 887
871static SV * 888static SV *
872coro_deq (pTHX_ int min_prio) 889coro_deq (pTHX_ int min_prio)
873{ 890{
977 croak ("Coro::State::_newprocess expects an arrayref"); 994 croak ("Coro::State::_newprocess expects an arrayref");
978 995
979 Newz (0, coro, 1, struct coro); 996 Newz (0, coro, 1, struct coro);
980 997
981 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 998 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
982 coro->mainstack = 0; /* actual work is done inside transfer */ 999 /*coro->mainstack = 0; *//*actual work is done inside transfer */
983 coro->stack = 0; 1000 /*coro->stack = 0;*/
984 1001
985 /* same as JMPENV_BOOTSTRAP */ 1002 /* same as JMPENV_BOOTSTRAP */
986 /* we might be able to recycle start_env, but safe is safe */ 1003 /* we might be able to recycle start_env, but safe is safe */
987 //Zero(&coro->start_env, 1, JMPENV); 1004 /*Zero(&coro->start_env, 1, JMPENV);*/
988 coro->start_env.je_ret = -1; 1005 coro->start_env.je_ret = -1;
989 coro->start_env.je_mustcatch = TRUE; 1006 coro->start_env.je_mustcatch = TRUE;
990 1007
991 RETVAL = coro; 1008 RETVAL = coro;
992 OUTPUT: 1009 OUTPUT:
1013 if (coro->mainstack && coro->mainstack != main_mainstack) 1030 if (coro->mainstack && coro->mainstack != main_mainstack)
1014 { 1031 {
1015 struct coro temp; 1032 struct coro temp;
1016 1033
1017 PUTBACK; 1034 PUTBACK;
1018 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1035 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1019 LOAD(aTHX_ coro); 1036 LOAD (aTHX_ coro);
1020 SPAGAIN; 1037 SPAGAIN;
1021 1038
1022 destroy_stacks (aTHX); 1039 destroy_stacks (aTHX);
1023 1040
1024 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1041 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1025 SPAGAIN; 1042 SPAGAIN;
1026 1043
1027 coro->mainstack = 0; 1044 coro->mainstack = 0;
1028 } 1045 }
1029 1046
1044 1061
1045void 1062void
1046yield(...) 1063yield(...)
1047 PROTOTYPE: @ 1064 PROTOTYPE: @
1048 CODE: 1065 CODE:
1049 static SV *returnstk; 1066 SV *yieldstack;
1050 SV *sv; 1067 SV *sv;
1051 AV *defav = GvAV (PL_defgv); 1068 AV *defav = GvAV (PL_defgv);
1052 struct coro *prev, *next; 1069 struct coro *prev, *next;
1053 1070
1054 if (!returnstk) 1071 yieldstack = *hv_fetch (
1055 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1072 (HV *)SvRV (GvSV (coro_current)),
1073 "yieldstack", sizeof ("yieldstack") - 1,
1074 0
1075 );
1056 1076
1057 /* set up @_ -- ugly */ 1077 /* set up @_ -- ugly */
1058 av_clear (defav); 1078 av_clear (defav);
1059 av_fill (defav, items - 1); 1079 av_fill (defav, items - 1);
1060 while (items--) 1080 while (items--)
1061 av_store (defav, items, SvREFCNT_inc (ST(items))); 1081 av_store (defav, items, SvREFCNT_inc (ST(items)));
1062 1082
1063 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1064 sv = av_pop ((AV *)SvRV (returnstk)); 1083 sv = av_pop ((AV *)SvRV (yieldstack));
1065 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1084 prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))));
1066 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1085 next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))));
1067 SvREFCNT_dec (sv); 1086 SvREFCNT_dec (sv);
1068 1087
1069 transfer(aTHX_ prev, next, 0); 1088 transfer (aTHX_ prev, next, 0);
1070 1089
1071MODULE = Coro::State PACKAGE = Coro 1090MODULE = Coro::State PACKAGE = Coro
1072 1091
1073# this is slightly dirty (should expose a c-level api) 1092# this is slightly dirty (should expose a c-level api)
1074 1093
1134cede(...) 1153cede(...)
1135 PROTOTYPE: 1154 PROTOTYPE:
1136 CODE: 1155 CODE:
1137 api_cede (); 1156 api_cede ();
1138 1157
1158# and these are hacks
1159SV *
1160_aio_get_state ()
1161 CODE:
1162{
1163 struct {
1164 int errorno;
1165 int laststype;
1166 int laststatval;
1167 Stat_t statcache;
1168 } data;
1169
1170 data.errorno = errno;
1171 data.laststype = PL_laststype;
1172 data.laststatval = PL_laststatval;
1173 data.statcache = PL_statcache;
1174
1175 RETVAL = newSVpvn ((char *)&data, sizeof data);
1176}
1177 OUTPUT:
1178 RETVAL
1179
1180void
1181_aio_set_state (char *data_)
1182 PROTOTYPE: $
1183 CODE:
1184{
1185 struct {
1186 int errorno;
1187 int laststype;
1188 int laststatval;
1189 Stat_t statcache;
1190 } *data = (void *)data_;
1191
1192 errno = data->errorno;
1193 PL_laststype = data->laststype;
1194 PL_laststatval = data->laststatval;
1195 PL_statcache = data->statcache;
1196}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines