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.58 by pcg, Fri May 14 13:25:09 2004 UTC vs.
Revision 1.74 by root, Mon Oct 23 22:49:09 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" 32#include <errno.h>
31
32#include <signal.h> 33#include <signal.h>
33 34
34#ifdef HAVE_MMAP 35#ifdef HAVE_MMAP
35# include <unistd.h> 36# include <unistd.h>
36# include <sys/mman.h> 37# include <sys/mman.h>
57 58
58#include "CoroAPI.h" 59#include "CoroAPI.h"
59 60
60#ifdef USE_ITHREADS 61#ifdef USE_ITHREADS
61static perl_mutex coro_mutex; 62static perl_mutex coro_mutex;
62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 63# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 64# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
64#else 65#else
65# define LOCK 0 66# define LOCK (void)0
66# define UNLOCK 0 67# define UNLOCK (void)0
67#endif 68#endif
68 69
69static struct CoroAPI coroapi; 70static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 71static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 72static HV *coro_state_stash;
109 AV *mainstack; 110 AV *mainstack;
110 SV **stack_sp; 111 SV **stack_sp;
111 OP *op; 112 OP *op;
112 SV **curpad; 113 SV **curpad;
113 AV *comppad; 114 AV *comppad;
115 CV *compcv;
114 SV **stack_base; 116 SV **stack_base;
115 SV **stack_max; 117 SV **stack_max;
116 SV **tmps_stack; 118 SV **tmps_stack;
117 I32 tmps_floor; 119 I32 tmps_floor;
118 I32 tmps_ix; 120 I32 tmps_ix;
127 I32 savestack_ix; 129 I32 savestack_ix;
128 I32 savestack_max; 130 I32 savestack_max;
129 OP **retstack; 131 OP **retstack;
130 I32 retstack_ix; 132 I32 retstack_ix;
131 I32 retstack_max; 133 I32 retstack_max;
134 PMOP *curpm;
132 COP *curcop; 135 COP *curcop;
133 JMPENV *top_env; 136 JMPENV *top_env;
134 137
135 /* data associated with this coroutine (initial args) */ 138 /* data associated with this coroutine (initial args) */
136 AV *args; 139 AV *args;
168 av_store (newpadlist, 1, (SV *) newpad); 171 av_store (newpadlist, 1, (SV *) newpad);
169 172
170 av = newAV (); /* will be @_ */ 173 av = newAV (); /* will be @_ */
171 av_extend (av, 0); 174 av_extend (av, 0);
172 av_store (newpad, 0, (SV *) av); 175 av_store (newpad, 0, (SV *) av);
173 AvFLAGS (av) = AVf_REIFY; 176 AvREIFY_on (av);
174 177
175 for (ix = fpad; ix > 0; ix--) 178 for (ix = fpad; ix > 0; ix--)
176 { 179 {
177 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 180 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
178 181
270 AV *av = (AV *)mg->mg_obj; 273 AV *av = (AV *)mg->mg_obj;
271 274
272 /* casting is fun. */ 275 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 276 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist); 277 free_padlist (aTHX_ padlist);
278
279 SvREFCNT_dec (av);
275} 280}
276 281
277#define PERL_MAGIC_coro PERL_MAGIC_ext 282#define PERL_MAGIC_coro PERL_MAGIC_ext
278 283
279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 284static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
325 PL_mainstack = c->mainstack; 330 PL_mainstack = c->mainstack;
326 PL_stack_sp = c->stack_sp; 331 PL_stack_sp = c->stack_sp;
327 PL_op = c->op; 332 PL_op = c->op;
328 PL_curpad = c->curpad; 333 PL_curpad = c->curpad;
329 PL_comppad = c->comppad; 334 PL_comppad = c->comppad;
335 PL_compcv = c->compcv;
330 PL_stack_base = c->stack_base; 336 PL_stack_base = c->stack_base;
331 PL_stack_max = c->stack_max; 337 PL_stack_max = c->stack_max;
332 PL_tmps_stack = c->tmps_stack; 338 PL_tmps_stack = c->tmps_stack;
333 PL_tmps_floor = c->tmps_floor; 339 PL_tmps_floor = c->tmps_floor;
334 PL_tmps_ix = c->tmps_ix; 340 PL_tmps_ix = c->tmps_ix;
340 PL_scopestack_ix = c->scopestack_ix; 346 PL_scopestack_ix = c->scopestack_ix;
341 PL_scopestack_max = c->scopestack_max; 347 PL_scopestack_max = c->scopestack_max;
342 PL_savestack = c->savestack; 348 PL_savestack = c->savestack;
343 PL_savestack_ix = c->savestack_ix; 349 PL_savestack_ix = c->savestack_ix;
344 PL_savestack_max = c->savestack_max; 350 PL_savestack_max = c->savestack_max;
351#if PERL_VERSION < 9
345 PL_retstack = c->retstack; 352 PL_retstack = c->retstack;
346 PL_retstack_ix = c->retstack_ix; 353 PL_retstack_ix = c->retstack_ix;
347 PL_retstack_max = c->retstack_max; 354 PL_retstack_max = c->retstack_max;
355#endif
356 PL_curpm = c->curpm;
348 PL_curcop = c->curcop; 357 PL_curcop = c->curcop;
349 PL_top_env = c->top_env; 358 PL_top_env = c->top_env;
350 359
351 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 360 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
352 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 361 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
449 c->mainstack = PL_mainstack; 458 c->mainstack = PL_mainstack;
450 c->stack_sp = PL_stack_sp; 459 c->stack_sp = PL_stack_sp;
451 c->op = PL_op; 460 c->op = PL_op;
452 c->curpad = PL_curpad; 461 c->curpad = PL_curpad;
453 c->comppad = PL_comppad; 462 c->comppad = PL_comppad;
463 c->compcv = PL_compcv;
454 c->stack_base = PL_stack_base; 464 c->stack_base = PL_stack_base;
455 c->stack_max = PL_stack_max; 465 c->stack_max = PL_stack_max;
456 c->tmps_stack = PL_tmps_stack; 466 c->tmps_stack = PL_tmps_stack;
457 c->tmps_floor = PL_tmps_floor; 467 c->tmps_floor = PL_tmps_floor;
458 c->tmps_ix = PL_tmps_ix; 468 c->tmps_ix = PL_tmps_ix;
464 c->scopestack_ix = PL_scopestack_ix; 474 c->scopestack_ix = PL_scopestack_ix;
465 c->scopestack_max = PL_scopestack_max; 475 c->scopestack_max = PL_scopestack_max;
466 c->savestack = PL_savestack; 476 c->savestack = PL_savestack;
467 c->savestack_ix = PL_savestack_ix; 477 c->savestack_ix = PL_savestack_ix;
468 c->savestack_max = PL_savestack_max; 478 c->savestack_max = PL_savestack_max;
479#if PERL_VERSION < 9
469 c->retstack = PL_retstack; 480 c->retstack = PL_retstack;
470 c->retstack_ix = PL_retstack_ix; 481 c->retstack_ix = PL_retstack_ix;
471 c->retstack_max = PL_retstack_max; 482 c->retstack_max = PL_retstack_max;
483#endif
484 c->curpm = PL_curpm;
472 c->curcop = PL_curcop; 485 c->curcop = PL_curcop;
473 c->top_env = PL_top_env; 486 c->top_env = PL_top_env;
474} 487}
475 488
476/* 489/*
512 525
513 New(54,PL_savestack,96,ANY); 526 New(54,PL_savestack,96,ANY);
514 PL_savestack_ix = 0; 527 PL_savestack_ix = 0;
515 PL_savestack_max = 96; 528 PL_savestack_max = 96;
516 529
530#if PERL_VERSION < 9
517 New(54,PL_retstack,8,OP*); 531 New(54,PL_retstack,8,OP*);
518 PL_retstack_ix = 0; 532 PL_retstack_ix = 0;
519 PL_retstack_max = 8; 533 PL_retstack_max = 8;
534#endif
520 535
521 UNLOCK; 536 UNLOCK;
522} 537}
523 538
524/* 539/*
564 579
565 Safefree (PL_tmps_stack); 580 Safefree (PL_tmps_stack);
566 Safefree (PL_markstack); 581 Safefree (PL_markstack);
567 Safefree (PL_scopestack); 582 Safefree (PL_scopestack);
568 Safefree (PL_savestack); 583 Safefree (PL_savestack);
584#if PERL_VERSION < 9
569 Safefree (PL_retstack); 585 Safefree (PL_retstack);
586#endif
570} 587}
571 588
572static void 589static void
573allocate_stack (Coro__State ctx, int alloc) 590allocate_stack (Coro__State ctx, int alloc)
574{ 591{
581 stack->gencnt = ctx->gencnt = 0; 598 stack->gencnt = ctx->gencnt = 0;
582 599
583 if (alloc) 600 if (alloc)
584 { 601 {
585#if HAVE_MMAP 602#if HAVE_MMAP
586 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 603 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
587 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 604 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
588 if (stack->sptr == (void *)-1) 605 if (stack->sptr == (void *)-1)
589#endif 606#endif
590 { 607 {
591 /*FIXME*//*D*//* reasonable stack size! */ 608 /*FIXME*//*D*//* reasonable stack size! */
592 stack->ssize = - (8192 * sizeof (long)); 609 stack->ssize = - (STACKSIZE * sizeof (long));
593 New (0, stack->sptr, 8192, long); 610 New (0, stack->sptr, STACKSIZE, long);
594 } 611 }
595 } 612 }
596 else 613 else
597 stack->sptr = 0; 614 stack->sptr = 0;
598 615
850static int coro_nready; 867static int coro_nready;
851 868
852static void 869static void
853coro_enq (pTHX_ SV *sv) 870coro_enq (pTHX_ SV *sv)
854{ 871{
872 SV **xprio;
873 int prio;
874
855 if (SvTYPE (sv) == SVt_PVHV) 875 if (SvTYPE (sv) != SVt_PVHV)
856 {
857 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
858 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
859
860 prio = prio > PRIO_MAX ? PRIO_MAX
861 : prio < PRIO_MIN ? PRIO_MIN
862 : prio;
863
864 av_push (coro_ready [prio - PRIO_MIN], sv);
865 coro_nready++;
866
867 return;
868 }
869
870 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 876 croak ("Coro::ready tried to enqueue something that is not a coroutine");
877
878 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
879 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
880
881 prio = prio > PRIO_MAX ? PRIO_MAX
882 : prio < PRIO_MIN ? PRIO_MIN
883 : prio;
884
885 av_push (coro_ready [prio - PRIO_MIN], sv);
886 coro_nready++;
871} 887}
872 888
873static SV * 889static SV *
874coro_deq (pTHX_ int min_prio) 890coro_deq (pTHX_ int min_prio)
875{ 891{
979 croak ("Coro::State::_newprocess expects an arrayref"); 995 croak ("Coro::State::_newprocess expects an arrayref");
980 996
981 Newz (0, coro, 1, struct coro); 997 Newz (0, coro, 1, struct coro);
982 998
983 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 999 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
984 coro->mainstack = 0; /* actual work is done inside transfer */ 1000 /*coro->mainstack = 0; *//*actual work is done inside transfer */
985 coro->stack = 0; 1001 /*coro->stack = 0;*/
986 1002
987 /* same as JMPENV_BOOTSTRAP */ 1003 /* same as JMPENV_BOOTSTRAP */
988 /* we might be able to recycle start_env, but safe is safe */ 1004 /* we might be able to recycle start_env, but safe is safe */
989 //Zero(&coro->start_env, 1, JMPENV); 1005 /*Zero(&coro->start_env, 1, JMPENV);*/
990 coro->start_env.je_ret = -1; 1006 coro->start_env.je_ret = -1;
991 coro->start_env.je_mustcatch = TRUE; 1007 coro->start_env.je_mustcatch = TRUE;
992 1008
993 RETVAL = coro; 1009 RETVAL = coro;
994 OUTPUT: 1010 OUTPUT:
1015 if (coro->mainstack && coro->mainstack != main_mainstack) 1031 if (coro->mainstack && coro->mainstack != main_mainstack)
1016 { 1032 {
1017 struct coro temp; 1033 struct coro temp;
1018 1034
1019 PUTBACK; 1035 PUTBACK;
1020 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1036 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1021 LOAD(aTHX_ coro); 1037 LOAD (aTHX_ coro);
1022 SPAGAIN; 1038 SPAGAIN;
1023 1039
1024 destroy_stacks (aTHX); 1040 destroy_stacks (aTHX);
1025 1041
1026 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1042 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1027 SPAGAIN; 1043 SPAGAIN;
1028 1044
1029 coro->mainstack = 0; 1045 coro->mainstack = 0;
1030 } 1046 }
1031 1047
1046 1062
1047void 1063void
1048yield(...) 1064yield(...)
1049 PROTOTYPE: @ 1065 PROTOTYPE: @
1050 CODE: 1066 CODE:
1051 static SV *returnstk; 1067 SV *yieldstack;
1052 SV *sv; 1068 SV *sv;
1053 AV *defav = GvAV (PL_defgv); 1069 AV *defav = GvAV (PL_defgv);
1054 struct coro *prev, *next; 1070 struct coro *prev, *next;
1055 1071
1056 if (!returnstk) 1072 yieldstack = *hv_fetch (
1057 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1073 (HV *)SvRV (GvSV (coro_current)),
1074 "yieldstack", sizeof ("yieldstack") - 1,
1075 0
1076 );
1058 1077
1059 /* set up @_ -- ugly */ 1078 /* set up @_ -- ugly */
1060 av_clear (defav); 1079 av_clear (defav);
1061 av_fill (defav, items - 1); 1080 av_fill (defav, items - 1);
1062 while (items--) 1081 while (items--)
1063 av_store (defav, items, SvREFCNT_inc (ST(items))); 1082 av_store (defav, items, SvREFCNT_inc (ST(items)));
1064 1083
1065 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1066 sv = av_pop ((AV *)SvRV (returnstk)); 1084 sv = av_pop ((AV *)SvRV (yieldstack));
1067 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1085 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1068 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1086 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1069 SvREFCNT_dec (sv); 1087 SvREFCNT_dec (sv);
1070 1088
1071 transfer(aTHX_ prev, next, 0); 1089 transfer (aTHX_ prev, next, 0);
1072 1090
1073MODULE = Coro::State PACKAGE = Coro 1091MODULE = Coro::State PACKAGE = Coro
1074 1092
1075# this is slightly dirty (should expose a c-level api) 1093# this is slightly dirty (should expose a c-level api)
1076 1094
1136cede(...) 1154cede(...)
1137 PROTOTYPE: 1155 PROTOTYPE:
1138 CODE: 1156 CODE:
1139 api_cede (); 1157 api_cede ();
1140 1158
1159# and these are hacks
1160SV *
1161_aio_get_state ()
1162 CODE:
1163{
1164 struct {
1165 int errorno;
1166 int laststype;
1167 int laststatval;
1168 Stat_t statcache;
1169 } data;
1170
1171 data.errorno = errno;
1172 data.laststype = PL_laststype;
1173 data.laststatval = PL_laststatval;
1174 data.statcache = PL_statcache;
1175
1176 RETVAL = newSVpvn ((char *)&data, sizeof data);
1177}
1178 OUTPUT:
1179 RETVAL
1180
1181void
1182_aio_set_state (char *data_)
1183 PROTOTYPE: $
1184 CODE:
1185{
1186 struct {
1187 int errorno;
1188 int laststype;
1189 int laststatval;
1190 Stat_t statcache;
1191 } *data = (void *)data_;
1192
1193 errno = data->errorno;
1194 PL_laststype = data->laststype;
1195 PL_laststatval = data->laststatval;
1196 PL_statcache = data->statcache;
1197}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines