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.64 by root, Mon Mar 21 15:06:51 2005 UTC vs.
Revision 1.87 by root, Fri Nov 24 13:40:36 2006 UTC

6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8 8
9#include "patchlevel.h" 9#include "patchlevel.h"
10 10
11#if PATCHLEVEL < 6 11#if PERL_VERSION < 6
12# ifndef PL_ppaddr 12# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 13# define PL_ppaddr ppaddr
14# endif 14# endif
15# ifndef call_sv 15# ifndef call_sv
16# define call_sv perl_call_sv 16# define call_sv perl_call_sv
27# ifndef IS_PADCONST 27# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
29# endif 29# endif
30#endif 30#endif
31 31
32#include <signal.h> 32#include <errno.h>
33
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD
36#endif
37
38#ifndef STACKGUARD
39# define STACKGUARD 0
40#endif
33 41
34#ifdef HAVE_MMAP 42#ifdef HAVE_MMAP
35# include <unistd.h> 43# include <unistd.h>
36# include <sys/mman.h> 44# include <sys/mman.h>
37# ifndef MAP_ANONYMOUS 45# ifndef MAP_ANONYMOUS
39# define MAP_ANONYMOUS MAP_ANON 47# define MAP_ANONYMOUS MAP_ANON
40# else 48# else
41# undef HAVE_MMAP 49# undef HAVE_MMAP
42# endif 50# endif
43# endif 51# endif
52# include <limits.h>
53# ifndef PAGESIZE
54# define PAGESIZE pagesize
55# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
56static long pagesize;
57# else
58# define BOOT_PAGESIZE
59# endif
44#endif 60#endif
45 61
46#define SUB_INIT "Coro::State::initialize" 62#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state" 63#define UCORO_STATE "_coro_state"
48 64
57 73
58#include "CoroAPI.h" 74#include "CoroAPI.h"
59 75
60#ifdef USE_ITHREADS 76#ifdef USE_ITHREADS
61static perl_mutex coro_mutex; 77static perl_mutex coro_mutex;
62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 78# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 79# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
64#else 80#else
65# define LOCK 0 81# define LOCK (void)0
66# define UNLOCK 0 82# define UNLOCK (void)0
67#endif 83#endif
68 84
69static struct CoroAPI coroapi; 85static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 86static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 87static HV *coro_state_stash;
84 void *sptr; 100 void *sptr;
85 long ssize; /* positive == mmap, otherwise malloc */ 101 long ssize; /* positive == mmap, otherwise malloc */
86} coro_stack; 102} coro_stack;
87 103
88struct coro { 104struct coro {
89 /* the top-level JMPENV for each coroutine, needed to catch dies. */
90 JMPENV start_env;
91
92 /* the optional C context */ 105 /* the optional C context */
93 coro_stack *stack; 106 coro_stack *stack;
94 void *cursp; 107 void *cursp;
95 int gencnt; 108 int gencnt;
96 109
128 I32 savestack_ix; 141 I32 savestack_ix;
129 I32 savestack_max; 142 I32 savestack_max;
130 OP **retstack; 143 OP **retstack;
131 I32 retstack_ix; 144 I32 retstack_ix;
132 I32 retstack_max; 145 I32 retstack_max;
146 PMOP *curpm;
133 COP *curcop; 147 COP *curcop;
134 JMPENV *top_env; 148 JMPENV *top_env;
135 149
150 /* coro process data */
151 int prio;
152
136 /* data associated with this coroutine (initial args) */ 153 /* data associated with this coroutine (initial args) */
137 AV *args; 154 AV *args;
155 int refcnt;
138}; 156};
139 157
140typedef struct coro *Coro__State; 158typedef struct coro *Coro__State;
141typedef struct coro *Coro__State_or_hashref; 159typedef struct coro *Coro__State_or_hashref;
142 160
143/* mostly copied from op.c:cv_clone2 */ 161static AV *
144STATIC AV * 162coro_clone_padlist (pTHX_ CV *cv)
145clone_padlist (pTHX_ AV *protopadlist)
146{ 163{
147 AV *av; 164 AV *padlist = CvPADLIST (cv);
148 I32 ix;
149 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
150 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
151 SV **pname = AvARRAY (protopad_name);
152 SV **ppad = AvARRAY (protopad);
153 I32 fname = AvFILLp (protopad_name);
154 I32 fpad = AvFILLp (protopad);
155 AV *newpadlist, *newpad_name, *newpad; 165 AV *newpadlist, *newpad;
156 SV **npad;
157
158 newpad_name = newAV ();
159 for (ix = fname; ix >= 0; ix--)
160 av_store (newpad_name, ix, SvREFCNT_inc (pname[ix]));
161
162 newpad = newAV ();
163 av_fill (newpad, AvFILLp (protopad));
164 npad = AvARRAY (newpad);
165 166
166 newpadlist = newAV (); 167 newpadlist = newAV ();
167 AvREAL_off (newpadlist); 168 AvREAL_off (newpadlist);
168 av_store (newpadlist, 0, (SV *) newpad_name); 169#if PERL_VERSION < 9
170 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
171#else
172 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
173#endif
174 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
175 --AvFILLp (padlist);
176
177 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
169 av_store (newpadlist, 1, (SV *) newpad); 178 av_store (newpadlist, 1, (SV *)newpad);
170
171 av = newAV (); /* will be @_ */
172 av_extend (av, 0);
173 av_store (newpad, 0, (SV *) av);
174 AvFLAGS (av) = AVf_REIFY;
175
176 for (ix = fpad; ix > 0; ix--)
177 {
178 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
179
180 if (namesv && namesv != &PL_sv_undef)
181 {
182 char *name = SvPVX (namesv); /* XXX */
183
184 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
185 { /* lexical from outside? */
186 npad[ix] = SvREFCNT_inc (ppad[ix]);
187 }
188 else
189 { /* our own lexical */
190 SV *sv;
191 if (*name == '&')
192 sv = SvREFCNT_inc (ppad[ix]);
193 else if (*name == '@')
194 sv = (SV *) newAV ();
195 else if (*name == '%')
196 sv = (SV *) newHV ();
197 else
198 sv = NEWSV (0, 0);
199
200#ifdef SvPADBUSY
201 if (!SvPADBUSY (sv))
202#endif
203 SvPADMY_on (sv);
204
205 npad[ix] = sv;
206 }
207 }
208 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
209 {
210 npad[ix] = SvREFCNT_inc (ppad[ix]);
211 }
212 else
213 {
214 SV *sv = NEWSV (0, 0);
215 SvPADTMP_on (sv);
216 npad[ix] = sv;
217 }
218 }
219
220#if 0 /* return -ENOTUNDERSTOOD */
221 /* Now that vars are all in place, clone nested closures. */
222
223 for (ix = fpad; ix > 0; ix--) {
224 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
225 if (namesv
226 && namesv != &PL_sv_undef
227 && !(SvFLAGS(namesv) & SVf_FAKE)
228 && *SvPVX(namesv) == '&'
229 && CvCLONE(ppad[ix]))
230 {
231 CV *kid = cv_clone((CV*)ppad[ix]);
232 SvREFCNT_dec(ppad[ix]);
233 CvCLONE_on(kid);
234 SvPADMY_on(kid);
235 npad[ix] = (SV*)kid;
236 }
237 }
238#endif
239 179
240 return newpadlist; 180 return newpadlist;
241} 181}
242 182
243STATIC void 183static void
244free_padlist (pTHX_ AV *padlist) 184free_padlist (pTHX_ AV *padlist)
245{ 185{
246 /* may be during global destruction */ 186 /* may be during global destruction */
247 if (SvREFCNT (padlist)) 187 if (SvREFCNT (padlist))
248 { 188 {
262 202
263 SvREFCNT_dec ((SV*)padlist); 203 SvREFCNT_dec ((SV*)padlist);
264 } 204 }
265} 205}
266 206
267STATIC int 207static int
268coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 208coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
269{ 209{
270 AV *padlist; 210 AV *padlist;
271 AV *av = (AV *)mg->mg_obj; 211 AV *av = (AV *)mg->mg_obj;
272 212
273 /* casting is fun. */ 213 /* casting is fun. */
274 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 214 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
275 free_padlist (aTHX_ padlist); 215 free_padlist (aTHX_ padlist);
276 216
277 SvREFCNT_dec (av); 217 SvREFCNT_dec (av);
218
219 return 0;
278} 220}
279 221
280#define PERL_MAGIC_coro PERL_MAGIC_ext 222#define PERL_MAGIC_coro PERL_MAGIC_ext
281 223
282static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 224static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
283 225
284/* the next two functions merely cache the padlists */ 226/* the next two functions merely cache the padlists */
285STATIC void 227static void
286get_padlist (pTHX_ CV *cv) 228get_padlist (pTHX_ CV *cv)
287{ 229{
288 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 230 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
289 231
290 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 232 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
291 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 233 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
292 else 234 else
235 {
236#if 0
237 /* this should work - but it doesn't :( */
238 CV *cp = Perl_cv_clone (aTHX_ cv);
239 CvPADLIST (cv) = CvPADLIST (cp);
240 CvPADLIST (cp) = 0;
241 SvREFCNT_dec (cp);
242#else
293 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); 243 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
244#endif
245 }
294} 246}
295 247
296STATIC void 248static void
297put_padlist (pTHX_ CV *cv) 249put_padlist (pTHX_ CV *cv)
298{ 250{
299 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 251 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
300 252
301 if (!mg) 253 if (!mg)
344 PL_scopestack_ix = c->scopestack_ix; 296 PL_scopestack_ix = c->scopestack_ix;
345 PL_scopestack_max = c->scopestack_max; 297 PL_scopestack_max = c->scopestack_max;
346 PL_savestack = c->savestack; 298 PL_savestack = c->savestack;
347 PL_savestack_ix = c->savestack_ix; 299 PL_savestack_ix = c->savestack_ix;
348 PL_savestack_max = c->savestack_max; 300 PL_savestack_max = c->savestack_max;
301#if PERL_VERSION < 9
349 PL_retstack = c->retstack; 302 PL_retstack = c->retstack;
350 PL_retstack_ix = c->retstack_ix; 303 PL_retstack_ix = c->retstack_ix;
351 PL_retstack_max = c->retstack_max; 304 PL_retstack_max = c->retstack_max;
305#endif
306 PL_curpm = c->curpm;
352 PL_curcop = c->curcop; 307 PL_curcop = c->curcop;
353 PL_top_env = c->top_env; 308 PL_top_env = c->top_env;
354 309
355 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
356 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
415 } 370 }
416 371
417 PUSHs ((SV *)CvPADLIST(cv)); 372 PUSHs ((SV *)CvPADLIST(cv));
418 PUSHs ((SV *)cv); 373 PUSHs ((SV *)cv);
419 374
420 get_padlist (aTHX_ cv); /* this is a monster */ 375 get_padlist (aTHX_ cv);
421 } 376 }
422 } 377 }
423#ifdef CXt_FORMAT 378#ifdef CXt_FORMAT
424 else if (CxTYPE(cx) == CXt_FORMAT) 379 else if (CxTYPE(cx) == CXt_FORMAT)
425 { 380 {
469 c->scopestack_ix = PL_scopestack_ix; 424 c->scopestack_ix = PL_scopestack_ix;
470 c->scopestack_max = PL_scopestack_max; 425 c->scopestack_max = PL_scopestack_max;
471 c->savestack = PL_savestack; 426 c->savestack = PL_savestack;
472 c->savestack_ix = PL_savestack_ix; 427 c->savestack_ix = PL_savestack_ix;
473 c->savestack_max = PL_savestack_max; 428 c->savestack_max = PL_savestack_max;
429#if PERL_VERSION < 9
474 c->retstack = PL_retstack; 430 c->retstack = PL_retstack;
475 c->retstack_ix = PL_retstack_ix; 431 c->retstack_ix = PL_retstack_ix;
476 c->retstack_max = PL_retstack_max; 432 c->retstack_max = PL_retstack_max;
433#endif
434 c->curpm = PL_curpm;
477 c->curcop = PL_curcop; 435 c->curcop = PL_curcop;
478 c->top_env = PL_top_env; 436 c->top_env = PL_top_env;
479} 437}
480 438
481/* 439/*
482 * allocate various perl stacks. This is an exact copy 440 * allocate various perl stacks. This is an exact copy
483 * of perl.c:init_stacks, except that it uses less memory 441 * of perl.c:init_stacks, except that it uses less memory
484 * on the (sometimes correct) assumption that coroutines do 442 * on the (sometimes correct) assumption that coroutines do
485 * not usually need a lot of stackspace. 443 * not usually need a lot of stackspace.
486 */ 444 */
487STATIC void 445static void
488coro_init_stacks (pTHX) 446coro_init_stacks (pTHX)
489{ 447{
490 LOCK; 448 LOCK;
491 449
492 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 450 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
517 475
518 New(54,PL_savestack,96,ANY); 476 New(54,PL_savestack,96,ANY);
519 PL_savestack_ix = 0; 477 PL_savestack_ix = 0;
520 PL_savestack_max = 96; 478 PL_savestack_max = 96;
521 479
480#if PERL_VERSION < 9
522 New(54,PL_retstack,8,OP*); 481 New(54,PL_retstack,8,OP*);
523 PL_retstack_ix = 0; 482 PL_retstack_ix = 0;
524 PL_retstack_max = 8; 483 PL_retstack_max = 8;
484#endif
525 485
526 UNLOCK; 486 UNLOCK;
527} 487}
528 488
529/* 489/*
530 * destroy the stacks, the callchain etc... 490 * destroy the stacks, the callchain etc...
531 */ 491 */
532STATIC void 492static void
533destroy_stacks(pTHX) 493destroy_stacks(pTHX)
534{ 494{
535 if (!IN_DESTRUCT) 495 if (!IN_DESTRUCT)
536 { 496 {
537 /* is this ugly, I ask? */ 497 /* is this ugly, I ask? */
569 529
570 Safefree (PL_tmps_stack); 530 Safefree (PL_tmps_stack);
571 Safefree (PL_markstack); 531 Safefree (PL_markstack);
572 Safefree (PL_scopestack); 532 Safefree (PL_scopestack);
573 Safefree (PL_savestack); 533 Safefree (PL_savestack);
534#if PERL_VERSION < 9
574 Safefree (PL_retstack); 535 Safefree (PL_retstack);
536#endif
575} 537}
576 538
577static void 539static void
578allocate_stack (Coro__State ctx, int alloc) 540allocate_stack (Coro__State ctx, int alloc)
579{ 541{
586 stack->gencnt = ctx->gencnt = 0; 548 stack->gencnt = ctx->gencnt = 0;
587 549
588 if (alloc) 550 if (alloc)
589 { 551 {
590#if HAVE_MMAP 552#if HAVE_MMAP
591 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 553 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
592 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 554 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
593 if (stack->sptr == (void *)-1) 555 if (stack->sptr != (void *)-1)
594#endif
595 { 556 {
596 /*FIXME*//*D*//* reasonable stack size! */ 557# if STACKGUARD
558 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
559# endif
560 }
561 else
562#endif
563 {
597 stack->ssize = - (8192 * sizeof (long)); 564 stack->ssize = - (STACKSIZE * (long)sizeof (long));
598 New (0, stack->sptr, 8192, long); 565 New (0, stack->sptr, STACKSIZE, long);
599 } 566 }
600 } 567 }
601 else 568 else
602 stack->sptr = 0; 569 stack->sptr = 0;
603 570
663 } 630 }
664 else 631 else
665 { 632 {
666 UNOP myop; 633 UNOP myop;
667 634
668 PL_op = (OP *)&myop;
669
670 Zero(&myop, 1, UNOP); 635 Zero(&myop, 1, UNOP);
671 myop.op_next = Nullop; 636 myop.op_next = Nullop;
672 myop.op_flags = OPf_WANT_VOID; 637 myop.op_flags = OPf_WANT_VOID;
638
639 PL_op = (OP *)&myop;
673 640
674 PUSHMARK(SP); 641 PUSHMARK(SP);
675 XPUSHs (sub_init); 642 XPUSHs (sub_init);
676 /* 643 /*
677 * the next line is slightly wrong, as PL_op->op_next 644 * the next line is slightly wrong, as PL_op->op_next
694 /* 661 /*
695 * this is a _very_ stripped down perl interpreter ;) 662 * this is a _very_ stripped down perl interpreter ;)
696 */ 663 */
697 dTHX; 664 dTHX;
698 Coro__State ctx = (Coro__State)arg; 665 Coro__State ctx = (Coro__State)arg;
699 JMPENV coro_start_env;
700 666
701 PL_top_env = &ctx->start_env; 667 PL_top_env = &PL_start_env;
702 668
703 ctx->cursp = 0; 669 ctx->cursp = 0;
704 PL_op = PL_op->op_next; 670 PL_op = PL_op->op_next;
705 CALLRUNOPS(aTHX); 671 CALLRUNOPS(aTHX);
706 672
707 abort (); 673 abort ();
708} 674}
709 675
710STATIC void 676/* never call directly, always through the coro_state_transfer global variable */
677static void
711transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 678transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
712{ 679{
713 dSTACKLEVEL; 680 dSTACKLEVEL;
714 681
715 if (prev != next) 682 if (prev != next)
716 { 683 {
684 /* has this coro been created yet? */
717 if (next->mainstack) 685 if (next->mainstack)
718 { 686 {
719 LOCK; 687 LOCK;
720 SAVE (prev, flags); 688 SAVE (prev, flags);
721 LOAD (next); 689 LOAD (next);
760 { 728 {
761 LOCK; 729 LOCK;
762 SAVE (prev, -1); /* first get rid of the old state */ 730 SAVE (prev, -1); /* first get rid of the old state */
763 UNLOCK; 731 UNLOCK;
764 732
733 /* create the coroutine for the first time */
765 if (flags & TRANSFER_SAVE_CCTXT) 734 if (flags & TRANSFER_SAVE_CCTXT)
766 { 735 {
767 if (!prev->stack) 736 if (!prev->stack)
768 allocate_stack (prev, 0); 737 allocate_stack (prev, 0);
769 738
739 /* the new coroutine starts with start_env again */
740 PL_top_env = &PL_start_env;
741
770 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 742 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
771 { 743 {
772 PL_top_env = &next->start_env;
773
774 setup_coro (next); 744 setup_coro (next);
775 next->cursp = stacklevel; 745 next->cursp = stacklevel;
776 746
777 prev->stack->refcnt++; 747 prev->stack->refcnt++;
778 prev->stack->usecnt++; 748 prev->stack->usecnt++;
806 coro_mortal = 0; 776 coro_mortal = 0;
807 } 777 }
808 UNLOCK; 778 UNLOCK;
809} 779}
810 780
781/* use this function pointer to call the above function */
782/* this is done to increase chances of the compiler not inlining the call */
783void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
784
811#define SV_CORO(sv,func) \ 785#define SV_CORO(sv,func) \
812 do { \ 786 do { \
813 if (SvROK (sv)) \ 787 if (SvROK (sv)) \
814 sv = SvRV (sv); \ 788 sv = SvRV (sv); \
815 \ 789 \
816 if (SvTYPE (sv) == SVt_PVHV) \ 790 if (SvTYPE (sv) == SVt_PVHV && SvSTASH (sv) != coro_state_stash) \
817 { \ 791 { \
818 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 792 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
819 \ 793 \
820 if (!he) \ 794 if (!he) \
821 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 795 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
827 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 801 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
828 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 802 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
829 \ 803 \
830 } while(0) 804 } while(0)
831 805
832#define SvSTATE(sv) (struct coro *)SvIV (sv) 806static void
807coro_state_destroy (struct coro *coro)
808{
809 if (coro->refcnt--)
810 return;
833 811
812 if (coro->mainstack && coro->mainstack != main_mainstack)
813 {
814 struct coro temp;
815
816 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
817 LOAD (aTHX_ coro);
818
819 destroy_stacks (aTHX);
820
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822
823 coro->mainstack = 0;
824 }
825
826 deallocate_stack (coro);
827 SvREFCNT_dec (coro->args);
828 Safefree (coro);
829}
830
834static void 831static int
832coro_state_clear (SV *sv, MAGIC *mg)
833{
834 struct coro *coro = (struct coro *)mg->mg_ptr;
835 mg->mg_ptr = 0;
836
837 coro_state_destroy (coro);
838
839 return 0;
840}
841
842static int
843coro_state_dup (MAGIC *mg, CLONE_PARAMS *params)
844{
845 struct coro *coro = (struct coro *)mg->mg_ptr;
846
847 ++coro->refcnt;
848
849 return 0;
850}
851
852static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 };
853
854static struct coro *
855SvSTATE (SV *coro)
856{
857 MAGIC *mg = SvMAGIC (SvROK (coro) ? SvRV (coro) : coro);
858 assert (mg->mg_type == PERL_MAGIC_ext);
859 return (struct coro *)mg->mg_ptr;
860}
861
862static void
835api_transfer(pTHX_ SV *prev, SV *next, int flags) 863api_transfer (pTHX_ SV *prev, SV *next, int flags)
836{ 864{
837 SV_CORO (prev, "Coro::transfer"); 865 SV_CORO (prev, "Coro::transfer");
838 SV_CORO (next, "Coro::transfer"); 866 SV_CORO (next, "Coro::transfer");
839 867
840 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 868 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
841} 869}
842 870
843/** Coro ********************************************************************/ 871/** Coro ********************************************************************/
844 872
845#define PRIO_MAX 3 873#define PRIO_MAX 3
849#define PRIO_IDLE -3 877#define PRIO_IDLE -3
850#define PRIO_MIN -4 878#define PRIO_MIN -4
851 879
852/* for Coro.pm */ 880/* for Coro.pm */
853static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
854static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
855static int coro_nready; 883static int coro_nready;
856 884
857static void 885static void
858coro_enq (pTHX_ SV *sv) 886coro_enq (pTHX_ SV *sv)
859{ 887{
888 int prio;
889
860 if (SvTYPE (sv) == SVt_PVHV) 890 if (SvTYPE (sv) != SVt_PVHV)
861 {
862 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
863 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
864
865 prio = prio > PRIO_MAX ? PRIO_MAX
866 : prio < PRIO_MIN ? PRIO_MIN
867 : prio;
868
869 av_push (coro_ready [prio - PRIO_MIN], sv);
870 coro_nready++;
871
872 return;
873 }
874
875 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 891 croak ("Coro::ready tried to enqueue something that is not a coroutine");
892
893 {
894 SV *coro = sv;
895 SV_CORO (coro, "omg");
896 prio = SvSTATE (coro)->prio;
897 }
898
899 av_push (coro_ready [prio - PRIO_MIN], sv);
900 coro_nready++;
876} 901}
877 902
878static SV * 903static SV *
879coro_deq (pTHX_ int min_prio) 904coro_deq (pTHX_ int min_prio)
880{ 905{
883 min_prio -= PRIO_MIN; 908 min_prio -= PRIO_MIN;
884 if (min_prio < 0) 909 if (min_prio < 0)
885 min_prio = 0; 910 min_prio = 0;
886 911
887 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 912 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
888 if (av_len (coro_ready[prio]) >= 0) 913 if (AvFILLp (coro_ready [prio]) >= 0)
889 { 914 {
890 coro_nready--; 915 coro_nready--;
891 return av_shift (coro_ready[prio]); 916 return av_shift (coro_ready [prio]);
892 } 917 }
893 918
894 return 0; 919 return 0;
895} 920}
896 921
930 955
931 SV_CORO (next, "Coro::schedule"); 956 SV_CORO (next, "Coro::schedule");
932 957
933 UNLOCK; 958 UNLOCK;
934 959
935 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 960 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
936 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 961 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
937} 962}
938 963
939static void 964static void
940api_cede (void) 965api_cede (void)
941{ 966{
948 api_schedule (); 973 api_schedule ();
949} 974}
950 975
951MODULE = Coro::State PACKAGE = Coro::State 976MODULE = Coro::State PACKAGE = Coro::State
952 977
953PROTOTYPES: ENABLE 978PROTOTYPES: DISABLE
954 979
955BOOT: 980BOOT:
956{ /* {} necessary for stoopid perl-5.6.x */ 981{ /* {} necessary for stoopid perl-5.6.x */
957#ifdef USE_ITHREADS 982#ifdef USE_ITHREADS
958 MUTEX_INIT (&coro_mutex); 983 MUTEX_INIT (&coro_mutex);
959#endif 984#endif
985 BOOT_PAGESIZE;
960 986
961 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 987 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
962 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 988 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
963 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 989 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
964 990
969 995
970 main_mainstack = PL_mainstack; 996 main_mainstack = PL_mainstack;
971 997
972 coroapi.ver = CORO_API_VERSION; 998 coroapi.ver = CORO_API_VERSION;
973 coroapi.transfer = api_transfer; 999 coroapi.transfer = api_transfer;
974}
975 1000
976Coro::State 1001 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
977_newprocess(args) 1002}
978 SV * args 1003
979 PROTOTYPE: $ 1004SV *
1005new (char *klass, ...)
980 CODE: 1006 CODE:
981 Coro__State coro; 1007{
1008 struct coro *coro;
1009 HV *hv;
1010 int i;
982 1011
983 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
984 croak ("Coro::State::_newprocess expects an arrayref");
985
986 Newz (0, coro, 1, struct coro); 1012 Newz (0, coro, 1, struct coro);
1013 coro->args = newAV ();
987 1014
988 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 1015 for (i = 1; i < items; i++)
1016 av_push (coro->args, newSVsv (ST (i)));
1017
989 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1018 /*coro->mainstack = 0; *//*actual work is done inside transfer */
990 /*coro->stack = 0;*/ 1019 /*coro->stack = 0;*/
991 1020
992 /* same as JMPENV_BOOTSTRAP */ 1021 hv = newHV ();
993 /* we might be able to recycle start_env, but safe is safe */ 1022 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
994 /*Zero(&coro->start_env, 1, JMPENV);*/ 1023 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
995 coro->start_env.je_ret = -1; 1024}
996 coro->start_env.je_mustcatch = TRUE;
997
998 RETVAL = coro;
999 OUTPUT: 1025 OUTPUT:
1000 RETVAL 1026 RETVAL
1001 1027
1002void 1028void
1003transfer(prev, next, flags) 1029transfer(prev, next, flags)
1004 SV *prev 1030 SV *prev
1005 SV *next 1031 SV *next
1006 int flags 1032 int flags
1007 PROTOTYPE: @
1008 CODE: 1033 CODE:
1009 PUTBACK; 1034 PUTBACK;
1010 SV_CORO (next, "Coro::transfer"); 1035 SV_CORO (next, "Coro::transfer");
1011 SV_CORO (prev, "Coro::transfer"); 1036 SV_CORO (prev, "Coro::transfer");
1012 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 1037 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1013 SPAGAIN; 1038 SPAGAIN;
1014 1039
1040int
1041prio (Coro::State coro, int newprio = 0)
1042 ALIAS:
1043 nice = 1
1044 CODE:
1045{
1046 RETVAL = coro->prio;
1047
1048 if (items > 1)
1049 {
1050 if (ix)
1051 newprio += coro->prio;
1052
1053 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1054 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1055
1056 coro->prio = newprio;
1057 }
1058}
1059
1015void 1060void
1016DESTROY(coro) 1061_clone_state_from (SV *dst, SV *src)
1017 Coro::State coro 1062 CODE:
1018 CODE: 1063{
1064 struct coro *coro_src = SvSTATE (src);
1019 1065
1020 if (coro->mainstack && coro->mainstack != main_mainstack) 1066 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1021 {
1022 struct coro temp;
1023 1067
1024 PUTBACK; 1068 ++coro_src->refcnt;
1025 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 1069 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1026 LOAD (aTHX_ coro); 1070}
1027 SPAGAIN;
1028
1029 destroy_stacks (aTHX);
1030
1031 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1032 SPAGAIN;
1033
1034 coro->mainstack = 0;
1035 }
1036
1037 deallocate_stack (coro);
1038 SvREFCNT_dec (coro->args);
1039 Safefree (coro);
1040 1071
1041void 1072void
1042_exit(code) 1073_exit (code)
1043 int code 1074 int code
1044 PROTOTYPE: $ 1075 PROTOTYPE: $
1045 CODE: 1076 CODE:
1046 _exit (code); 1077 _exit (code);
1047 1078
1048MODULE = Coro::State PACKAGE = Coro::Cont 1079MODULE = Coro::State PACKAGE = Coro::Cont
1049 1080
1050# this is slightly dirty (should expose a c-level api)
1051
1052void 1081void
1053yield(...) 1082yield (...)
1054 PROTOTYPE: @ 1083 PROTOTYPE: @
1055 CODE: 1084 CODE:
1056 SV *yieldstack; 1085 SV *yieldstack;
1057 SV *sv; 1086 SV *sv;
1058 AV *defav = GvAV (PL_defgv); 1087 AV *defav = GvAV (PL_defgv);
1069 av_fill (defav, items - 1); 1098 av_fill (defav, items - 1);
1070 while (items--) 1099 while (items--)
1071 av_store (defav, items, SvREFCNT_inc (ST(items))); 1100 av_store (defav, items, SvREFCNT_inc (ST(items)));
1072 1101
1073 sv = av_pop ((AV *)SvRV (yieldstack)); 1102 sv = av_pop ((AV *)SvRV (yieldstack));
1074 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1103 prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1075 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1104 next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1076 SvREFCNT_dec (sv); 1105 SvREFCNT_dec (sv);
1077 1106
1078 transfer (aTHX_ prev, next, 0); 1107 coro_state_transfer (aTHX_ prev, next, 0);
1079 1108
1080MODULE = Coro::State PACKAGE = Coro 1109MODULE = Coro::State PACKAGE = Coro
1081
1082# this is slightly dirty (should expose a c-level api)
1083 1110
1084BOOT: 1111BOOT:
1085{ 1112{
1086 int i; 1113 int i;
1087 HV *stash = gv_stashpv ("Coro", TRUE); 1114 HV *stash = gv_stashpv ("Coro", TRUE);
1107 coroapi.ready = api_ready; 1134 coroapi.ready = api_ready;
1108 coroapi.nready = &coro_nready; 1135 coroapi.nready = &coro_nready;
1109 coroapi.current = coro_current; 1136 coroapi.current = coro_current;
1110 1137
1111 GCoroAPI = &coroapi; 1138 GCoroAPI = &coroapi;
1112 sv_setiv(sv, (IV)&coroapi); 1139 sv_setiv (sv, (IV)&coroapi);
1113 SvREADONLY_on(sv); 1140 SvREADONLY_on (sv);
1114 } 1141 }
1115} 1142}
1116 1143
1117#if !PERL_MICRO 1144#if !PERL_MICRO
1118 1145
1119void 1146void
1120ready(self) 1147ready (self)
1121 SV * self 1148 SV * self
1122 PROTOTYPE: $ 1149 PROTOTYPE: $
1123 CODE: 1150 CODE:
1124 api_ready (self); 1151 api_ready (self);
1125 1152
1126#endif 1153#endif
1127 1154
1128int 1155int
1129nready(...) 1156nready (...)
1130 PROTOTYPE: 1157 PROTOTYPE:
1131 CODE: 1158 CODE:
1132 RETVAL = coro_nready; 1159 RETVAL = coro_nready;
1133 OUTPUT: 1160 OUTPUT:
1134 RETVAL 1161 RETVAL
1135 1162
1136void 1163void
1137schedule(...) 1164schedule (...)
1138 PROTOTYPE: 1165 PROTOTYPE:
1139 CODE: 1166 CODE:
1140 api_schedule (); 1167 api_schedule ();
1141 1168
1142void 1169void
1143cede(...) 1170cede (...)
1144 PROTOTYPE: 1171 PROTOTYPE:
1145 CODE: 1172 CODE:
1146 api_cede (); 1173 api_cede ();
1147 1174
1175# and these are hacks
1176SV *
1177_aio_get_state ()
1178 CODE:
1179{
1180 struct {
1181 int errorno;
1182 int laststype;
1183 int laststatval;
1184 Stat_t statcache;
1185 } data;
1186
1187 data.errorno = errno;
1188 data.laststype = PL_laststype;
1189 data.laststatval = PL_laststatval;
1190 data.statcache = PL_statcache;
1191
1192 RETVAL = newSVpvn ((char *)&data, sizeof data);
1193}
1194 OUTPUT:
1195 RETVAL
1196
1197void
1198_aio_set_state (char *data_)
1199 PROTOTYPE: $
1200 CODE:
1201{
1202 struct {
1203 int errorno;
1204 int laststype;
1205 int laststatval;
1206 Stat_t statcache;
1207 } *data = (void *)data_;
1208
1209 errno = data->errorno;
1210 PL_laststype = data->laststype;
1211 PL_laststatval = data->laststatval;
1212 PL_statcache = data->statcache;
1213}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines