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.53 by pcg, Sat Feb 14 12:39:41 2004 UTC vs.
Revision 1.67 by root, Mon Dec 12 20:07:44 2005 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c"
4
1#include "EXTERN.h" 5#include "EXTERN.h"
2#include "perl.h" 6#include "perl.h"
3#include "XSUB.h" 7#include "XSUB.h"
4 8
5#include "patchlevel.h" 9#include "patchlevel.h"
6 10
7#if PATCHLEVEL < 6 11// threaded perls might need much more, 65536 or more,
12// which is 0.5 to 1MB
13#define STACKSIZE 16384
14
15#if PERL_VERSION < 6
8# ifndef PL_ppaddr 16# ifndef PL_ppaddr
9# define PL_ppaddr ppaddr 17# define PL_ppaddr ppaddr
10# endif 18# endif
11# ifndef call_sv 19# ifndef call_sv
12# define call_sv perl_call_sv 20# define call_sv perl_call_sv
23# ifndef IS_PADCONST 31# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0 32# define IS_PADCONST(v) 0
25# endif 33# endif
26#endif 34#endif
27 35
28#include "libcoro/coro.c"
29
30#include <signal.h> 36#include <signal.h>
31 37
32#ifdef HAVE_MMAP 38#ifdef HAVE_MMAP
33# include <unistd.h> 39# include <unistd.h>
34# include <sys/mman.h> 40# include <sys/mman.h>
53 59
54#define labs(l) ((l) >= 0 ? (l) : -(l)) 60#define labs(l) ((l) >= 0 ? (l) : -(l))
55 61
56#include "CoroAPI.h" 62#include "CoroAPI.h"
57 63
64#ifdef USE_ITHREADS
65static perl_mutex coro_mutex;
66# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
67# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
68#else
69# define LOCK 0
70# define UNLOCK 0
71#endif
72
58static struct CoroAPI coroapi; 73static struct CoroAPI coroapi;
74static AV *main_mainstack; /* used to differentiate between $main and others */
75static HV *coro_state_stash;
76static SV *ucoro_state_sv;
77static U32 ucoro_state_hash;
78static SV *coro_mortal; /* will be freed after next transfer */
59 79
60/* this is actually not only the c stack but also c registers etc... */ 80/* this is actually not only the c stack but also c registers etc... */
61typedef struct { 81typedef struct {
62 int refcnt; /* pointer reference counter */ 82 int refcnt; /* pointer reference counter */
63 int usecnt; /* shared by how many coroutines */ 83 int usecnt; /* shared by how many coroutines */
93 AV *mainstack; 113 AV *mainstack;
94 SV **stack_sp; 114 SV **stack_sp;
95 OP *op; 115 OP *op;
96 SV **curpad; 116 SV **curpad;
97 AV *comppad; 117 AV *comppad;
118 CV *compcv;
98 SV **stack_base; 119 SV **stack_base;
99 SV **stack_max; 120 SV **stack_max;
100 SV **tmps_stack; 121 SV **tmps_stack;
101 I32 tmps_floor; 122 I32 tmps_floor;
102 I32 tmps_ix; 123 I32 tmps_ix;
111 I32 savestack_ix; 132 I32 savestack_ix;
112 I32 savestack_max; 133 I32 savestack_max;
113 OP **retstack; 134 OP **retstack;
114 I32 retstack_ix; 135 I32 retstack_ix;
115 I32 retstack_max; 136 I32 retstack_max;
137 PMOP *curpm;
116 COP *curcop; 138 COP *curcop;
117 JMPENV *top_env; 139 JMPENV *top_env;
118 140
119 /* data associated with this coroutine (initial args) */ 141 /* data associated with this coroutine (initial args) */
120 AV *args; 142 AV *args;
121}; 143};
122 144
123typedef struct coro *Coro__State; 145typedef struct coro *Coro__State;
124typedef struct coro *Coro__State_or_hashref; 146typedef struct coro *Coro__State_or_hashref;
125 147
126static AV *main_mainstack; /* used to differentiate between $main and others */
127static HV *coro_state_stash;
128static SV *ucoro_state_sv;
129static U32 ucoro_state_hash;
130static HV *padlist_cache;
131static SV *coro_mortal; /* will be freed after next transfer */
132
133/* mostly copied from op.c:cv_clone2 */ 148/* mostly copied from op.c:cv_clone2 */
134STATIC AV * 149STATIC AV *
135clone_padlist (AV *protopadlist) 150clone_padlist (pTHX_ AV *protopadlist)
136{ 151{
137 AV *av; 152 AV *av;
138 I32 ix; 153 I32 ix;
139 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 154 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
140 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 155 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
164 AvFLAGS (av) = AVf_REIFY; 179 AvFLAGS (av) = AVf_REIFY;
165 180
166 for (ix = fpad; ix > 0; ix--) 181 for (ix = fpad; ix > 0; ix--)
167 { 182 {
168 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 183 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
184
169 if (namesv && namesv != &PL_sv_undef) 185 if (namesv && namesv != &PL_sv_undef)
170 { 186 {
171 char *name = SvPVX (namesv); /* XXX */ 187 char *name = SvPVX (namesv); /* XXX */
188
172 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 189 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
173 { /* lexical from outside? */ 190 { /* lexical from outside? */
174 npad[ix] = SvREFCNT_inc (ppad[ix]); 191 npad[ix] = SvREFCNT_inc (ppad[ix]);
175 } 192 }
176 else 193 else
182 sv = (SV *) newAV (); 199 sv = (SV *) newAV ();
183 else if (*name == '%') 200 else if (*name == '%')
184 sv = (SV *) newHV (); 201 sv = (SV *) newHV ();
185 else 202 else
186 sv = NEWSV (0, 0); 203 sv = NEWSV (0, 0);
204
187#ifdef SvPADBUSY 205#ifdef SvPADBUSY
188 if (!SvPADBUSY (sv)) 206 if (!SvPADBUSY (sv))
189#endif 207#endif
190 SvPADMY_on (sv); 208 SvPADMY_on (sv);
209
191 npad[ix] = sv; 210 npad[ix] = sv;
192 } 211 }
193 } 212 }
194 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 213 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
195 { 214 {
225 244
226 return newpadlist; 245 return newpadlist;
227} 246}
228 247
229STATIC void 248STATIC void
230free_padlist (AV *padlist) 249free_padlist (pTHX_ AV *padlist)
231{ 250{
232 /* may be during global destruction */ 251 /* may be during global destruction */
233 if (SvREFCNT (padlist)) 252 if (SvREFCNT (padlist))
234 { 253 {
235 I32 i = AvFILLp (padlist); 254 I32 i = AvFILLp (padlist);
249 SvREFCNT_dec ((SV*)padlist); 268 SvREFCNT_dec ((SV*)padlist);
250 } 269 }
251} 270}
252 271
253STATIC int 272STATIC int
254coro_cv_free (SV *sv, MAGIC *mg) 273coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
255{ 274{
256 AV *padlist; 275 AV *padlist;
257 AV *av = (AV *)mg->mg_obj; 276 AV *av = (AV *)mg->mg_obj;
258 277
259 /* casting is fun. */ 278 /* casting is fun. */
260 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 279 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
261 free_padlist (padlist); 280 free_padlist (aTHX_ padlist);
281
282 SvREFCNT_dec (av);
262} 283}
263 284
264#define PERL_MAGIC_coro PERL_MAGIC_ext 285#define PERL_MAGIC_coro PERL_MAGIC_ext
265 286
266static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 287static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
267 288
268/* the next two functions merely cache the padlists */ 289/* the next two functions merely cache the padlists */
269STATIC void 290STATIC void
270get_padlist (CV *cv) 291get_padlist (pTHX_ CV *cv)
271{ 292{
272 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 293 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
273 294
274 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 295 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
275 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 296 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
276 else 297 else
277 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 298 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
278} 299}
279 300
280STATIC void 301STATIC void
281put_padlist (CV *cv) 302put_padlist (pTHX_ CV *cv)
282{ 303{
283 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 304 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
284 305
285 if (!mg) 306 if (!mg)
286 { 307 {
312 PL_mainstack = c->mainstack; 333 PL_mainstack = c->mainstack;
313 PL_stack_sp = c->stack_sp; 334 PL_stack_sp = c->stack_sp;
314 PL_op = c->op; 335 PL_op = c->op;
315 PL_curpad = c->curpad; 336 PL_curpad = c->curpad;
316 PL_comppad = c->comppad; 337 PL_comppad = c->comppad;
338 PL_compcv = c->compcv;
317 PL_stack_base = c->stack_base; 339 PL_stack_base = c->stack_base;
318 PL_stack_max = c->stack_max; 340 PL_stack_max = c->stack_max;
319 PL_tmps_stack = c->tmps_stack; 341 PL_tmps_stack = c->tmps_stack;
320 PL_tmps_floor = c->tmps_floor; 342 PL_tmps_floor = c->tmps_floor;
321 PL_tmps_ix = c->tmps_ix; 343 PL_tmps_ix = c->tmps_ix;
330 PL_savestack_ix = c->savestack_ix; 352 PL_savestack_ix = c->savestack_ix;
331 PL_savestack_max = c->savestack_max; 353 PL_savestack_max = c->savestack_max;
332 PL_retstack = c->retstack; 354 PL_retstack = c->retstack;
333 PL_retstack_ix = c->retstack_ix; 355 PL_retstack_ix = c->retstack_ix;
334 PL_retstack_max = c->retstack_max; 356 PL_retstack_max = c->retstack_max;
357 PL_curpm = c->curpm;
335 PL_curcop = c->curcop; 358 PL_curcop = c->curcop;
336 PL_top_env = c->top_env; 359 PL_top_env = c->top_env;
337 360
338 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 361 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
339 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 362 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
348 { 371 {
349 AV *padlist = (AV *)POPs; 372 AV *padlist = (AV *)POPs;
350 373
351 if (padlist) 374 if (padlist)
352 { 375 {
353 put_padlist (cv); /* mark this padlist as available */ 376 put_padlist (aTHX_ cv); /* mark this padlist as available */
354 CvPADLIST(cv) = padlist; 377 CvPADLIST(cv) = padlist;
355#ifdef USE_THREADS
356 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
357#endif
358 } 378 }
359 379
360 ++CvDEPTH(cv); 380 ++CvDEPTH(cv);
361 } 381 }
362 382
389 if (CxTYPE(cx) == CXt_SUB) 409 if (CxTYPE(cx) == CXt_SUB)
390 { 410 {
391 CV *cv = cx->blk_sub.cv; 411 CV *cv = cx->blk_sub.cv;
392 if (CvDEPTH(cv)) 412 if (CvDEPTH(cv))
393 { 413 {
394#ifdef USE_THREADS
395 /*XPUSHs ((SV *)CvOWNER(cv));*/
396 /*CvOWNER(cv) = 0;*/
397 /*error must unlock this cv etc.. etc...*/
398#endif
399 EXTEND (SP, CvDEPTH(cv)*2); 414 EXTEND (SP, CvDEPTH(cv)*2);
400 415
401 while (--CvDEPTH(cv)) 416 while (--CvDEPTH(cv))
402 { 417 {
403 /* this tells the restore code to increment CvDEPTH */ 418 /* this tells the restore code to increment CvDEPTH */
406 } 421 }
407 422
408 PUSHs ((SV *)CvPADLIST(cv)); 423 PUSHs ((SV *)CvPADLIST(cv));
409 PUSHs ((SV *)cv); 424 PUSHs ((SV *)cv);
410 425
411 get_padlist (cv); /* this is a monster */ 426 get_padlist (aTHX_ cv); /* this is a monster */
412 } 427 }
413 } 428 }
414#ifdef CXt_FORMAT 429#ifdef CXt_FORMAT
415 else if (CxTYPE(cx) == CXt_FORMAT) 430 else if (CxTYPE(cx) == CXt_FORMAT)
416 { 431 {
444 c->mainstack = PL_mainstack; 459 c->mainstack = PL_mainstack;
445 c->stack_sp = PL_stack_sp; 460 c->stack_sp = PL_stack_sp;
446 c->op = PL_op; 461 c->op = PL_op;
447 c->curpad = PL_curpad; 462 c->curpad = PL_curpad;
448 c->comppad = PL_comppad; 463 c->comppad = PL_comppad;
464 c->compcv = PL_compcv;
449 c->stack_base = PL_stack_base; 465 c->stack_base = PL_stack_base;
450 c->stack_max = PL_stack_max; 466 c->stack_max = PL_stack_max;
451 c->tmps_stack = PL_tmps_stack; 467 c->tmps_stack = PL_tmps_stack;
452 c->tmps_floor = PL_tmps_floor; 468 c->tmps_floor = PL_tmps_floor;
453 c->tmps_ix = PL_tmps_ix; 469 c->tmps_ix = PL_tmps_ix;
462 c->savestack_ix = PL_savestack_ix; 478 c->savestack_ix = PL_savestack_ix;
463 c->savestack_max = PL_savestack_max; 479 c->savestack_max = PL_savestack_max;
464 c->retstack = PL_retstack; 480 c->retstack = PL_retstack;
465 c->retstack_ix = PL_retstack_ix; 481 c->retstack_ix = PL_retstack_ix;
466 c->retstack_max = PL_retstack_max; 482 c->retstack_max = PL_retstack_max;
483 c->curpm = PL_curpm;
467 c->curcop = PL_curcop; 484 c->curcop = PL_curcop;
468 c->top_env = PL_top_env; 485 c->top_env = PL_top_env;
469} 486}
470 487
471/* 488/*
475 * not usually need a lot of stackspace. 492 * not usually need a lot of stackspace.
476 */ 493 */
477STATIC void 494STATIC void
478coro_init_stacks (pTHX) 495coro_init_stacks (pTHX)
479{ 496{
497 LOCK;
498
480 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 499 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
481 PL_curstackinfo->si_type = PERLSI_MAIN; 500 PL_curstackinfo->si_type = PERLSI_MAIN;
482 PL_curstack = PL_curstackinfo->si_stack; 501 PL_curstack = PL_curstackinfo->si_stack;
483 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 502 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 503
508 PL_savestack_max = 96; 527 PL_savestack_max = 96;
509 528
510 New(54,PL_retstack,8,OP*); 529 New(54,PL_retstack,8,OP*);
511 PL_retstack_ix = 0; 530 PL_retstack_ix = 0;
512 PL_retstack_max = 8; 531 PL_retstack_max = 8;
532
533 UNLOCK;
513} 534}
514 535
515/* 536/*
516 * destroy the stacks, the callchain etc... 537 * destroy the stacks, the callchain etc...
517 */ 538 */
523 /* is this ugly, I ask? */ 544 /* is this ugly, I ask? */
524 LEAVE_SCOPE (0); 545 LEAVE_SCOPE (0);
525 546
526 /* sure it is, but more important: is it correct?? :/ */ 547 /* sure it is, but more important: is it correct?? :/ */
527 FREETMPS; 548 FREETMPS;
549
550 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
528 } 551 }
529 552
530 while (PL_curstackinfo->si_next) 553 while (PL_curstackinfo->si_next)
531 PL_curstackinfo = PL_curstackinfo->si_next; 554 PL_curstackinfo = PL_curstackinfo->si_next;
532 555
533 while (PL_curstackinfo) 556 while (PL_curstackinfo)
534 { 557 {
535 PERL_SI *p = PL_curstackinfo->si_prev; 558 PERL_SI *p = PL_curstackinfo->si_prev;
536 559
537 { 560 { /*D*//*remove*/
538 dSP; 561 dSP;
539 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 562 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
540 PUTBACK; /* possibly superfluous */ 563 PUTBACK; /* possibly superfluous */
541 } 564 }
542 565
543 if (!IN_DESTRUCT) 566 if (!IN_DESTRUCT)
544 { 567 {
545 dounwind(-1); 568 dounwind (-1);/*D*//*remove*/
546 SvREFCNT_dec(PL_curstackinfo->si_stack); 569 SvREFCNT_dec (PL_curstackinfo->si_stack);
547 } 570 }
548 571
549 Safefree(PL_curstackinfo->si_cxstack); 572 Safefree (PL_curstackinfo->si_cxstack);
550 Safefree(PL_curstackinfo); 573 Safefree (PL_curstackinfo);
551 PL_curstackinfo = p; 574 PL_curstackinfo = p;
552 } 575 }
553 576
554 Safefree(PL_tmps_stack); 577 Safefree (PL_tmps_stack);
555 Safefree(PL_markstack); 578 Safefree (PL_markstack);
556 Safefree(PL_scopestack); 579 Safefree (PL_scopestack);
557 Safefree(PL_savestack); 580 Safefree (PL_savestack);
558 Safefree(PL_retstack); 581 Safefree (PL_retstack);
559} 582}
560 583
561static void 584static void
562allocate_stack (Coro__State ctx, int alloc) 585allocate_stack (Coro__State ctx, int alloc)
563{ 586{
566 New (0, stack, 1, coro_stack); 589 New (0, stack, 1, coro_stack);
567 590
568 stack->refcnt = 1; 591 stack->refcnt = 1;
569 stack->usecnt = 1; 592 stack->usecnt = 1;
570 stack->gencnt = ctx->gencnt = 0; 593 stack->gencnt = ctx->gencnt = 0;
594
571 if (alloc) 595 if (alloc)
572 { 596 {
573#if HAVE_MMAP 597#if HAVE_MMAP
574 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 598 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
575 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 599 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
576 if (stack->sptr == (void *)-1) 600 if (stack->sptr == (void *)-1)
577#endif 601#endif
578 { 602 {
579 /*FIXME*//*D*//* reasonable stack size! */ 603 /*FIXME*//*D*//* reasonable stack size! */
580 stack->ssize = - (8192 * sizeof (long)); 604 stack->ssize = - (STACKSIZE * sizeof (long));
581 New (0, stack->sptr, 8192, long); 605 New (0, stack->sptr, STACKSIZE, long);
582 } 606 }
583 } 607 }
584 else 608 else
585 stack->sptr = 0; 609 stack->sptr = 0;
586 610
616setup_coro (void *arg) 640setup_coro (void *arg)
617{ 641{
618 /* 642 /*
619 * emulate part of the perl startup here. 643 * emulate part of the perl startup here.
620 */ 644 */
645 dTHX;
621 dSP; 646 dSP;
622 Coro__State ctx = (Coro__State)arg; 647 Coro__State ctx = (Coro__State)arg;
623 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 648 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
624 649
625 coro_init_stacks (aTHX); 650 coro_init_stacks (aTHX);
674continue_coro (void *arg) 699continue_coro (void *arg)
675{ 700{
676 /* 701 /*
677 * this is a _very_ stripped down perl interpreter ;) 702 * this is a _very_ stripped down perl interpreter ;)
678 */ 703 */
704 dTHX;
679 Coro__State ctx = (Coro__State)arg; 705 Coro__State ctx = (Coro__State)arg;
680 JMPENV coro_start_env; 706 JMPENV coro_start_env;
681 707
682 PL_top_env = &ctx->start_env; 708 PL_top_env = &ctx->start_env;
683 709
687 713
688 abort (); 714 abort ();
689} 715}
690 716
691STATIC void 717STATIC void
692transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 718transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
693{ 719{
694 dSTACKLEVEL; 720 dSTACKLEVEL;
695 static struct coro *xnext;
696 721
697 if (prev != next) 722 if (prev != next)
698 { 723 {
699 xnext = next;
700
701 if (next->mainstack) 724 if (next->mainstack)
702 { 725 {
726 LOCK;
703 SAVE (prev, flags); 727 SAVE (prev, flags);
704 LOAD (next); 728 LOAD (next);
729 UNLOCK;
705 730
706 /* mark this state as in-use */ 731 /* mark this state as in-use */
707 next->mainstack = 0; 732 next->mainstack = 0;
708 next->tmps_ix = -2; 733 next->tmps_ix = -2;
709 734
728 continue_coro, (void *)next, 753 continue_coro, (void *)next,
729 next->stack->sptr, labs (next->stack->ssize)); 754 next->stack->sptr, labs (next->stack->ssize));
730 } 755 }
731 756
732 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 757 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
758 prev->cursp = stacklevel;
733 /* don't add any code here */ 759 /* don't add any code here */
734 } 760 }
735 761 else
762 next->cursp = stacklevel;
736 } 763 }
737 else if (next->tmps_ix == -2) 764 else if (next->tmps_ix == -2)
738 croak ("tried to transfer to running coroutine"); 765 croak ("tried to transfer to running coroutine");
739 else 766 else
740 { 767 {
768 LOCK;
741 SAVE (prev, -1); /* first get rid of the old state */ 769 SAVE (prev, -1); /* first get rid of the old state */
770 UNLOCK;
742 771
743 if (flags & TRANSFER_SAVE_CCTXT) 772 if (flags & TRANSFER_SAVE_CCTXT)
744 { 773 {
745 if (!prev->stack) 774 if (!prev->stack)
746 allocate_stack (prev, 0); 775 allocate_stack (prev, 0);
748 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 777 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
749 { 778 {
750 PL_top_env = &next->start_env; 779 PL_top_env = &next->start_env;
751 780
752 setup_coro (next); 781 setup_coro (next);
782 next->cursp = stacklevel;
753 783
754 prev->stack->refcnt++; 784 prev->stack->refcnt++;
755 prev->stack->usecnt++; 785 prev->stack->usecnt++;
756 next->stack = prev->stack; 786 next->stack = prev->stack;
757 next->gencnt = prev->gencnt; 787 next->gencnt = prev->gencnt;
762 allocate_stack (next, 1); 792 allocate_stack (next, 1);
763 coro_create (&(next->stack->cctx), 793 coro_create (&(next->stack->cctx),
764 setup_coro, (void *)next, 794 setup_coro, (void *)next,
765 next->stack->sptr, labs (next->stack->ssize)); 795 next->stack->sptr, labs (next->stack->ssize));
766 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 796 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
797 prev->cursp = stacklevel;
767 /* don't add any code here */ 798 /* don't add any code here */
768 } 799 }
769 } 800 }
770 else 801 else
802 {
771 setup_coro (next); 803 setup_coro (next);
804 next->cursp = stacklevel;
805 }
772 } 806 }
773
774 /*
775 * xnext is now either prev or next, depending on wether
776 * we switched the c stack or not. that's why I use a global
777 * variable, that should become thread-specific at one point.
778 */
779 xnext->cursp = stacklevel;
780 } 807 }
781 808
809 LOCK;
782 if (coro_mortal) 810 if (coro_mortal)
783 { 811 {
784 SvREFCNT_dec (coro_mortal); 812 SvREFCNT_dec (coro_mortal);
785 coro_mortal = 0; 813 coro_mortal = 0;
786 } 814 }
815 UNLOCK;
787} 816}
788 817
789#define SV_CORO(sv,func) \ 818#define SV_CORO(sv,func) \
790 do { \ 819 do { \
791 if (SvROK (sv)) \ 820 if (SvROK (sv)) \
792 sv = SvRV (sv); \ 821 sv = SvRV (sv); \
793 \ 822 \
794 if (SvTYPE(sv) == SVt_PVHV) \ 823 if (SvTYPE (sv) == SVt_PVHV) \
795 { \ 824 { \
796 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 825 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
797 \ 826 \
798 if (!he) \ 827 if (!he) \
799 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 828 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
800 \ 829 \
801 (sv) = SvRV (HeVAL(he)); \ 830 (sv) = SvRV (HeVAL(he)); \
802 } \ 831 } \
803 \ 832 \
804 /* must also be changed inside Coro::Cont::yield */ \ 833 /* must also be changed inside Coro::Cont::yield */ \
805 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 834 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
806 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 835 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
807 \ 836 \
808 } while(0) 837 } while(0)
809 838
810#define SvSTATE(sv) (struct coro *)SvIV (sv) 839#define SvSTATE(sv) (struct coro *)SvIV (sv)
813api_transfer(pTHX_ SV *prev, SV *next, int flags) 842api_transfer(pTHX_ SV *prev, SV *next, int flags)
814{ 843{
815 SV_CORO (prev, "Coro::transfer"); 844 SV_CORO (prev, "Coro::transfer");
816 SV_CORO (next, "Coro::transfer"); 845 SV_CORO (next, "Coro::transfer");
817 846
818 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 847 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
819} 848}
820 849
821/** Coro ********************************************************************/ 850/** Coro ********************************************************************/
822 851
823#define PRIO_MAX 3 852#define PRIO_MAX 3
831static GV *coro_current, *coro_idle; 860static GV *coro_current, *coro_idle;
832static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 861static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
833static int coro_nready; 862static int coro_nready;
834 863
835static void 864static void
836coro_enq (SV *sv) 865coro_enq (pTHX_ SV *sv)
837{ 866{
838 if (SvTYPE (sv) == SVt_PVHV) 867 if (SvTYPE (sv) == SVt_PVHV)
839 { 868 {
840 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 869 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
841 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 870 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
852 881
853 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 882 croak ("Coro::ready tried to enqueue something that is not a coroutine");
854} 883}
855 884
856static SV * 885static SV *
857coro_deq (int min_prio) 886coro_deq (pTHX_ int min_prio)
858{ 887{
859 int prio = PRIO_MAX - PRIO_MIN; 888 int prio = PRIO_MAX - PRIO_MIN;
860 889
861 min_prio -= PRIO_MIN; 890 min_prio -= PRIO_MIN;
862 if (min_prio < 0) 891 if (min_prio < 0)
873} 902}
874 903
875static void 904static void
876api_ready (SV *coro) 905api_ready (SV *coro)
877{ 906{
907 dTHX;
908
878 if (SvROK (coro)) 909 if (SvROK (coro))
879 coro = SvRV (coro); 910 coro = SvRV (coro);
880 911
912 LOCK;
881 coro_enq (SvREFCNT_inc (coro)); 913 coro_enq (aTHX_ SvREFCNT_inc (coro));
914 UNLOCK;
882} 915}
883 916
884static void 917static void
885api_schedule (void) 918api_schedule (void)
886{ 919{
920 dTHX;
921
887 SV *prev, *next; 922 SV *prev, *next;
888 923
924 LOCK;
925
889 prev = SvRV (GvSV (coro_current)); 926 prev = SvRV (GvSV (coro_current));
890 next = coro_deq (PRIO_MIN); 927 next = coro_deq (aTHX_ PRIO_MIN);
891 928
892 if (!next) 929 if (!next)
893 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 930 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
894 931
895 /* free this only after the transfer */ 932 /* free this only after the transfer */
898 935
899 SvRV (GvSV (coro_current)) = next; 936 SvRV (GvSV (coro_current)) = next;
900 937
901 SV_CORO (next, "Coro::schedule"); 938 SV_CORO (next, "Coro::schedule");
902 939
940 UNLOCK;
941
903 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 942 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
904 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 943 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
905} 944}
906 945
907static void 946static void
908api_cede (void) 947api_cede (void)
909{ 948{
949 dTHX;
950
951 LOCK;
910 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 952 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
953 UNLOCK;
911 954
912 api_schedule (); 955 api_schedule ();
913} 956}
914 957
915MODULE = Coro::State PACKAGE = Coro::State 958MODULE = Coro::State PACKAGE = Coro::State
916 959
917PROTOTYPES: ENABLE 960PROTOTYPES: ENABLE
918 961
919BOOT: 962BOOT:
920{ /* {} necessary for stoopid perl-5.6.x */ 963{ /* {} necessary for stoopid perl-5.6.x */
964#ifdef USE_ITHREADS
965 MUTEX_INIT (&coro_mutex);
966#endif
967
921 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 968 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
922 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 969 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
923 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 970 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
924 971
925 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 972 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
926 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 973 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
927 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 974 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
928 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 975 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
929
930 if (!padlist_cache)
931 padlist_cache = newHV ();
932 976
933 main_mainstack = PL_mainstack; 977 main_mainstack = PL_mainstack;
934 978
935 coroapi.ver = CORO_API_VERSION; 979 coroapi.ver = CORO_API_VERSION;
936 coroapi.transfer = api_transfer; 980 coroapi.transfer = api_transfer;
947 croak ("Coro::State::_newprocess expects an arrayref"); 991 croak ("Coro::State::_newprocess expects an arrayref");
948 992
949 Newz (0, coro, 1, struct coro); 993 Newz (0, coro, 1, struct coro);
950 994
951 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 995 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
952 coro->mainstack = 0; /* actual work is done inside transfer */ 996 /*coro->mainstack = 0; *//*actual work is done inside transfer */
953 coro->stack = 0; 997 /*coro->stack = 0;*/
954 998
955 /* same as JMPENV_BOOTSTRAP */ 999 /* same as JMPENV_BOOTSTRAP */
956 /* we might be able to recycle start_env, but safe is safe */ 1000 /* we might be able to recycle start_env, but safe is safe */
957 //Zero(&coro->start_env, 1, JMPENV); 1001 /*Zero(&coro->start_env, 1, JMPENV);*/
958 coro->start_env.je_ret = -1; 1002 coro->start_env.je_ret = -1;
959 coro->start_env.je_mustcatch = TRUE; 1003 coro->start_env.je_mustcatch = TRUE;
960 1004
961 RETVAL = coro; 1005 RETVAL = coro;
962 OUTPUT: 1006 OUTPUT:
983 if (coro->mainstack && coro->mainstack != main_mainstack) 1027 if (coro->mainstack && coro->mainstack != main_mainstack)
984 { 1028 {
985 struct coro temp; 1029 struct coro temp;
986 1030
987 PUTBACK; 1031 PUTBACK;
988 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1032 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
989 LOAD(aTHX_ coro); 1033 LOAD (aTHX_ coro);
990 SPAGAIN; 1034 SPAGAIN;
991 1035
992 destroy_stacks (aTHX); 1036 destroy_stacks (aTHX);
993 1037
994 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1038 LOAD ((&temp)); /* this will get rid of defsv etc.. */
995 SPAGAIN; 1039 SPAGAIN;
996 1040
997 coro->mainstack = 0; 1041 coro->mainstack = 0;
998 } 1042 }
999 1043
1014 1058
1015void 1059void
1016yield(...) 1060yield(...)
1017 PROTOTYPE: @ 1061 PROTOTYPE: @
1018 CODE: 1062 CODE:
1019 static SV *returnstk; 1063 SV *yieldstack;
1020 SV *sv; 1064 SV *sv;
1021 AV *defav = GvAV (PL_defgv); 1065 AV *defav = GvAV (PL_defgv);
1022 struct coro *prev, *next; 1066 struct coro *prev, *next;
1023 1067
1024 if (!returnstk) 1068 yieldstack = *hv_fetch (
1025 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1069 (HV *)SvRV (GvSV (coro_current)),
1070 "yieldstack", sizeof ("yieldstack") - 1,
1071 0
1072 );
1026 1073
1027 /* set up @_ -- ugly */ 1074 /* set up @_ -- ugly */
1028 av_clear (defav); 1075 av_clear (defav);
1029 av_fill (defav, items - 1); 1076 av_fill (defav, items - 1);
1030 while (items--) 1077 while (items--)
1031 av_store (defav, items, SvREFCNT_inc (ST(items))); 1078 av_store (defav, items, SvREFCNT_inc (ST(items)));
1032 1079
1033 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1034 sv = av_pop ((AV *)SvRV (returnstk)); 1080 sv = av_pop ((AV *)SvRV (yieldstack));
1035 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1081 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1036 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1082 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1037 SvREFCNT_dec (sv); 1083 SvREFCNT_dec (sv);
1038 1084
1039 transfer(aTHX_ prev, next, 0); 1085 transfer (aTHX_ prev, next, 0);
1040 1086
1041MODULE = Coro::State PACKAGE = Coro 1087MODULE = Coro::State PACKAGE = Coro
1042 1088
1043# this is slightly dirty (should expose a c-level api) 1089# this is slightly dirty (should expose a c-level api)
1044 1090

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines