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.67 by root, Mon Dec 12 20:07:44 2005 UTC vs.
Revision 1.87 by root, Fri Nov 24 13:40:36 2006 UTC

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