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.95 by root, Sun Nov 26 17:35:42 2006 UTC vs.
Revision 1.106 by root, Mon Nov 27 02:08:55 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#if PERL_VERSION < 6 9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \
17 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
18
19#if !PERL_VERSION_ATLEAST (5,6,0)
12# ifndef PL_ppaddr 20# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 21# define PL_ppaddr ppaddr
14# endif 22# endif
15# ifndef call_sv 23# ifndef call_sv
16# define call_sv perl_call_sv 24# define call_sv perl_call_sv
54# ifndef PAGESIZE 62# ifndef PAGESIZE
55# define PAGESIZE pagesize 63# define PAGESIZE pagesize
56# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
57static long pagesize; 65static long pagesize;
58# else 66# else
59# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
60# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
61#endif 72#endif
62 73
63/* The next macro should declare a variable stacklevel that contains and approximation 74/* The next macro should declare a variable stacklevel that contains and approximation
64 * to the current C stack pointer. Its property is that it changes with each call 75 * to the current C stack pointer. Its property is that it changes with each call
65 * and should be unique. */ 76 * and should be unique. */
66#define dSTACKLEVEL int stacklevel 77#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 78#define STACKLEVEL ((void *)&stacklevel)
68 79
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 80#define IN_DESTRUCT (PL_main_cv == Nullcv)
81
82#if __GNUC__ >= 3
83# define attribute(x) __attribute__(x)
84#else
85# define attribute(x)
86#endif
87
88#define NOINLINE attribute ((noinline))
70 89
71#include "CoroAPI.h" 90#include "CoroAPI.h"
72 91
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 93
85static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
88 107
89/* this is a structure representing a c-level coroutine */ 108/* this is a structure representing a c-level coroutine */
90typedef struct coro_stack { 109typedef struct coro_cctx {
91 struct coro_stack *next; 110 struct coro_cctx *next;
92 111
93 /* the stack */ 112 /* the stack */
94 void *sptr; 113 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 114 long ssize; /* positive == mmap, otherwise malloc */
96 115
97 /* cpu state */ 116 /* cpu state */
98 void *idle_sp; /* original stacklevel when coroutine was created */ 117 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
99 JMPENV *top_env; 118 JMPENV *top_env;
100 coro_context cctx; 119 coro_context cctx;
101} coro_stack; 120
121#if USE_VALGRIND
122 int valgrind_id;
123#endif
124} coro_cctx;
102 125
103/* this is a structure representing a perl-level coroutine */ 126/* this is a structure representing a perl-level coroutine */
104struct coro { 127struct coro {
105 /* the c coroutine allocated to this perl coroutine, if any */ 128 /* the c coroutine allocated to this perl coroutine, if any */
106 coro_stack *stack; 129 coro_cctx *cctx;
107 130
108 /* data associated with this coroutine (initial args) */ 131 /* data associated with this coroutine (initial args) */
109 AV *args; 132 AV *args;
110 int refcnt; 133 int refcnt;
111 134
154 177
155typedef struct coro *Coro__State; 178typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 179typedef struct coro *Coro__State_or_hashref;
157 180
158static AV * 181static AV *
159coro_clone_padlist (pTHX_ CV *cv) 182coro_clone_padlist (CV *cv)
160{ 183{
161 AV *padlist = CvPADLIST (cv); 184 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 185 AV *newpadlist, *newpad;
163 186
164 newpadlist = newAV (); 187 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 188 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9 189#if PERL_VERSION_ATLEAST (5,9,0)
190 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
191#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 192 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif 193#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 194 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist); 195 --AvFILLp (padlist);
173 196
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 197 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
176 199
177 return newpadlist; 200 return newpadlist;
178} 201}
179 202
180static void 203static void
181free_padlist (pTHX_ AV *padlist) 204free_padlist (AV *padlist)
182{ 205{
183 /* may be during global destruction */ 206 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 207 if (SvREFCNT (padlist))
185 { 208 {
186 I32 i = AvFILLp (padlist); 209 I32 i = AvFILLp (padlist);
207 AV *padlist; 230 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 231 AV *av = (AV *)mg->mg_obj;
209 232
210 /* casting is fun. */ 233 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 234 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 235 free_padlist (padlist);
213 236
214 SvREFCNT_dec (av); 237 SvREFCNT_dec (av);
215 238
216 return 0; 239 return 0;
217} 240}
220 243
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 244static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 245
223/* the next two functions merely cache the padlists */ 246/* the next two functions merely cache the padlists */
224static void 247static void
225get_padlist (pTHX_ CV *cv) 248get_padlist (CV *cv)
226{ 249{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 250 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
228 251
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 252 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 253 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
231 else 254 else
232 { 255 {
233#if 0 256#if 0
234 /* this should work - but it doesn't :( */ 257 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 258 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 259 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 260 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 261 SvREFCNT_dec (cp);
239#else 262#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 263 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 264#endif
242 } 265 }
243} 266}
244 267
245static void 268static void
246put_padlist (pTHX_ CV *cv) 269put_padlist (CV *cv)
247{ 270{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 271 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
249 272
250 if (!mg) 273 if (!mg)
251 { 274 {
259} 282}
260 283
261#define SB do { 284#define SB do {
262#define SE } while (0) 285#define SE } while (0)
263 286
264#define LOAD(state) load_state(aTHX_ (state)); 287#define LOAD(state) load_state((state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 288#define SAVE(state,flags) save_state((state),(flags));
266 289
267#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 290#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
268 291
269static void 292static void
270load_state(pTHX_ Coro__State c) 293load_state(Coro__State c)
271{ 294{
272 PL_dowarn = c->dowarn; 295 PL_dowarn = c->dowarn;
273 PL_in_eval = c->in_eval; 296 PL_in_eval = c->in_eval;
274 297
275 PL_curstackinfo = c->curstackinfo; 298 PL_curstackinfo = c->curstackinfo;
293 PL_scopestack_ix = c->scopestack_ix; 316 PL_scopestack_ix = c->scopestack_ix;
294 PL_scopestack_max = c->scopestack_max; 317 PL_scopestack_max = c->scopestack_max;
295 PL_savestack = c->savestack; 318 PL_savestack = c->savestack;
296 PL_savestack_ix = c->savestack_ix; 319 PL_savestack_ix = c->savestack_ix;
297 PL_savestack_max = c->savestack_max; 320 PL_savestack_max = c->savestack_max;
298#if PERL_VERSION < 9 321#if !PERL_VERSION_ATLEAST (5,9,0)
299 PL_retstack = c->retstack; 322 PL_retstack = c->retstack;
300 PL_retstack_ix = c->retstack_ix; 323 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 324 PL_retstack_max = c->retstack_max;
302#endif 325#endif
303 PL_curpm = c->curpm; 326 PL_curpm = c->curpm;
316 { 339 {
317 AV *padlist = (AV *)POPs; 340 AV *padlist = (AV *)POPs;
318 341
319 if (padlist) 342 if (padlist)
320 { 343 {
321 put_padlist (aTHX_ cv); /* mark this padlist as available */ 344 put_padlist (cv); /* mark this padlist as available */
322 CvPADLIST(cv) = padlist; 345 CvPADLIST(cv) = padlist;
323 } 346 }
324 347
325 ++CvDEPTH(cv); 348 ++CvDEPTH(cv);
326 } 349 }
328 PUTBACK; 351 PUTBACK;
329 } 352 }
330} 353}
331 354
332static void 355static void
333save_state(pTHX_ Coro__State c, int flags) 356save_state(Coro__State c, int flags)
334{ 357{
335 { 358 {
336 dSP; 359 dSP;
337 I32 cxix = cxstack_ix; 360 I32 cxix = cxstack_ix;
338 PERL_CONTEXT *ccstk = cxstack; 361 PERL_CONTEXT *ccstk = cxstack;
366 } 389 }
367 390
368 PUSHs ((SV *)CvPADLIST(cv)); 391 PUSHs ((SV *)CvPADLIST(cv));
369 PUSHs ((SV *)cv); 392 PUSHs ((SV *)cv);
370 393
371 get_padlist (aTHX_ cv); 394 get_padlist (cv);
372 } 395 }
373 } 396 }
374#ifdef CXt_FORMAT 397#ifdef CXt_FORMAT
375 else if (CxTYPE(cx) == CXt_FORMAT) 398 else if (CxTYPE(cx) == CXt_FORMAT)
376 { 399 {
420 c->scopestack_ix = PL_scopestack_ix; 443 c->scopestack_ix = PL_scopestack_ix;
421 c->scopestack_max = PL_scopestack_max; 444 c->scopestack_max = PL_scopestack_max;
422 c->savestack = PL_savestack; 445 c->savestack = PL_savestack;
423 c->savestack_ix = PL_savestack_ix; 446 c->savestack_ix = PL_savestack_ix;
424 c->savestack_max = PL_savestack_max; 447 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9 448#if !PERL_VERSION_ATLEAST (5,9,0)
426 c->retstack = PL_retstack; 449 c->retstack = PL_retstack;
427 c->retstack_ix = PL_retstack_ix; 450 c->retstack_ix = PL_retstack_ix;
428 c->retstack_max = PL_retstack_max; 451 c->retstack_max = PL_retstack_max;
429#endif 452#endif
430 c->curpm = PL_curpm; 453 c->curpm = PL_curpm;
436 * of perl.c:init_stacks, except that it uses less memory 459 * of perl.c:init_stacks, except that it uses less memory
437 * on the (sometimes correct) assumption that coroutines do 460 * on the (sometimes correct) assumption that coroutines do
438 * not usually need a lot of stackspace. 461 * not usually need a lot of stackspace.
439 */ 462 */
440static void 463static void
441coro_init_stacks (pTHX) 464coro_init_stacks ()
442{ 465{
443 LOCK;
444
445 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 466 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
446 PL_curstackinfo->si_type = PERLSI_MAIN; 467 PL_curstackinfo->si_type = PERLSI_MAIN;
447 PL_curstack = PL_curstackinfo->si_stack; 468 PL_curstack = PL_curstackinfo->si_stack;
448 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 469 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
449 470
470 491
471 New(54,PL_savestack,96,ANY); 492 New(54,PL_savestack,96,ANY);
472 PL_savestack_ix = 0; 493 PL_savestack_ix = 0;
473 PL_savestack_max = 96; 494 PL_savestack_max = 96;
474 495
475#if PERL_VERSION < 9 496#if !PERL_VERSION_ATLEAST (5,9,0)
476 New(54,PL_retstack,8,OP*); 497 New(54,PL_retstack,8,OP*);
477 PL_retstack_ix = 0; 498 PL_retstack_ix = 0;
478 PL_retstack_max = 8; 499 PL_retstack_max = 8;
479#endif 500#endif
480
481 UNLOCK;
482} 501}
483 502
484/* 503/*
485 * destroy the stacks, the callchain etc... 504 * destroy the stacks, the callchain etc...
486 */ 505 */
487static void 506static void
488destroy_stacks(pTHX) 507coro_destroy_stacks ()
489{ 508{
490 if (!IN_DESTRUCT) 509 if (!IN_DESTRUCT)
491 { 510 {
492 /* is this ugly, I ask? */ 511 /* is this ugly, I ask? */
493 LEAVE_SCOPE (0); 512 LEAVE_SCOPE (0);
524 543
525 Safefree (PL_tmps_stack); 544 Safefree (PL_tmps_stack);
526 Safefree (PL_markstack); 545 Safefree (PL_markstack);
527 Safefree (PL_scopestack); 546 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 547 Safefree (PL_savestack);
529#if PERL_VERSION < 9 548#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 549 Safefree (PL_retstack);
531#endif 550#endif
532} 551}
533 552
534static void 553static void
535setup_coro (struct coro *coro) 554setup_coro (struct coro *coro)
536{ 555{
537 /* 556 /*
538 * emulate part of the perl startup here. 557 * emulate part of the perl startup here.
539 */ 558 */
540 dTHX;
541 dSP;
542 UNOP myop;
543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
544 559
545 coro_init_stacks (aTHX); 560 coro_init_stacks ();
561
546 /*PL_curcop = 0;*/ 562 PL_curcop = 0;
547 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 563 PL_in_eval = 0;
564 PL_curpm = 0;
565
566 {
567 dSP;
568 LOGOP myop;
569
570 /* I have no idea why this is needed, but it is */
571 PUSHMARK (SP);
572
548 SvREFCNT_dec (GvAV (PL_defgv)); 573 SvREFCNT_dec (GvAV (PL_defgv));
549 GvAV (PL_defgv) = coro->args; coro->args = 0; 574 GvAV (PL_defgv) = coro->args; coro->args = 0;
550 575
551 SPAGAIN;
552
553 Zero (&myop, 1, UNOP); 576 Zero (&myop, 1, LOGOP);
554 myop.op_next = Nullop; 577 myop.op_next = Nullop;
555 myop.op_flags = OPf_WANT_VOID; 578 myop.op_flags = OPf_WANT_VOID;
556 579
557 PL_op = (OP *)&myop; 580 PL_op = (OP *)&myop;
558 581
559 PUSHMARK(SP); 582 PUSHMARK (SP);
560 XPUSHs (sub_init); 583 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
561 PUTBACK; 584 PUTBACK;
562 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 585 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
563 SPAGAIN; 586 SPAGAIN;
564 587
565 ENTER; /* necessary e.g. for dounwind */ 588 ENTER; /* necessary e.g. for dounwind */
589 }
566} 590}
567 591
568static void 592static void
569free_coro_mortal () 593free_coro_mortal ()
570{ 594{
573 SvREFCNT_dec (coro_mortal); 597 SvREFCNT_dec (coro_mortal);
574 coro_mortal = 0; 598 coro_mortal = 0;
575 } 599 }
576} 600}
577 601
602static void NOINLINE
603prepare_cctx (coro_cctx *cctx)
604{
605 dSP;
606 LOGOP myop;
607
608 Zero (&myop, 1, LOGOP);
609 myop.op_next = PL_op;
610 myop.op_flags = OPf_WANT_VOID;
611
612 sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx));
613
614 PUSHMARK (SP);
615 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
616 PUTBACK;
617 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
618 SPAGAIN;
619}
620
578static void 621static void
579coro_run (void *arg) 622coro_run (void *arg)
580{ 623{
624 /* coro_run is the alternative epilogue of transfer() */
625 UNLOCK;
626
581 /* 627 /*
582 * this is a _very_ stripped down perl interpreter ;) 628 * this is a _very_ stripped down perl interpreter ;)
583 */ 629 */
584 dTHX;
585 int ret;
586
587 UNLOCK;
588
589 PL_top_env = &PL_start_env; 630 PL_top_env = &PL_start_env;
590 631 /* inject call to cctx_init */
591 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 632 prepare_cctx ((coro_cctx *)arg);
592 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
593
594 /* continue at cctx_init, without entersub */
595 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
596 633
597 /* somebody will hit me for both perl_run and PL_restartop */ 634 /* somebody will hit me for both perl_run and PL_restartop */
598 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 635 perl_run (PL_curinterp);
599 printf ("ret %d\n", ret);//D
600 636
601 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 637 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
602 abort (); 638 abort ();
603} 639}
604 640
605static coro_stack * 641static coro_cctx *
606stack_new () 642cctx_new ()
607{ 643{
608 coro_stack *stack; 644 coro_cctx *cctx;
609 645
610 New (0, stack, 1, coro_stack); 646 New (0, cctx, 1, coro_cctx);
611 647
612#if HAVE_MMAP 648#if HAVE_MMAP
613 649
614 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 650 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
615 /* mmap suppsedly does allocate-on-write for us */ 651 /* mmap suppsedly does allocate-on-write for us */
616 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 652 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
617 653
618 if (stack->sptr == (void *)-1) 654 if (cctx->sptr == (void *)-1)
619 { 655 {
620 perror ("FATAL: unable to mmap stack for coroutine"); 656 perror ("FATAL: unable to mmap stack for coroutine");
621 _exit (EXIT_FAILURE); 657 _exit (EXIT_FAILURE);
622 } 658 }
623 659
624# if STACKGUARD 660# if STACKGUARD
625 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 661 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
626# endif 662# endif
627 663
628#else 664#else
629 665
630 stack->ssize = STACKSIZE * (long)sizeof (long); 666 cctx->ssize = STACKSIZE * (long)sizeof (long);
631 New (0, stack->sptr, STACKSIZE, long); 667 New (0, cctx->sptr, STACKSIZE, long);
632 668
633 if (!stack->sptr) 669 if (!cctx->sptr)
634 { 670 {
635 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 671 perror ("FATAL: unable to malloc stack for coroutine");
636 _exit (EXIT_FAILURE); 672 _exit (EXIT_FAILURE);
637 } 673 }
638 674
639#endif 675#endif
640 676
677#if USE_VALGRIND
678 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
679 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
680 cctx->ssize + (char *)cctx->sptr
681 );
682#endif
683
641 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 684 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
642 685
643 return stack; 686 return cctx;
644} 687}
645 688
646static void 689static void
647stack_free (coro_stack *stack) 690cctx_free (coro_cctx *cctx)
648{ 691{
649 if (!stack) 692 if (!cctx)
650 return; 693 return;
651 694
695#if USE_VALGRIND
696 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
697#endif
698
652#if HAVE_MMAP 699#if HAVE_MMAP
653 munmap (stack->sptr, stack->ssize); 700 munmap (cctx->sptr, cctx->ssize);
654#else 701#else
655 Safefree (stack->sptr); 702 Safefree (cctx->sptr);
656#endif 703#endif
657 704
658 Safefree (stack); 705 Safefree (cctx);
659} 706}
660 707
661static coro_stack *stack_first; 708static coro_cctx *cctx_first;
709static int cctx_count, cctx_idle;
662 710
663static coro_stack * 711static coro_cctx *
664stack_get () 712cctx_get ()
665{ 713{
666 coro_stack *stack; 714 coro_cctx *cctx;
667 715
668 if (stack_first) 716 if (cctx_first)
669 { 717 {
670 stack = stack_first; 718 --cctx_idle;
719 cctx = cctx_first;
671 stack_first = stack->next; 720 cctx_first = cctx->next;
672 } 721 }
673 else 722 else
674 { 723 {
675 stack = stack_new (); 724 ++cctx_count;
725 cctx = cctx_new ();
676 PL_op = PL_op->op_next; 726 PL_op = PL_op->op_next;
677 } 727 }
678 728
679 return stack; 729 return cctx;
680} 730}
681 731
682static void 732static void
683stack_put (coro_stack *stack) 733cctx_put (coro_cctx *cctx)
684{ 734{
685 stack->next = stack_first; 735 ++cctx_idle;
686 stack_first = stack; 736 cctx->next = cctx_first;
737 cctx_first = cctx;
687} 738}
688 739
689/* never call directly, always through the coro_state_transfer global variable */ 740/* never call directly, always through the coro_state_transfer global variable */
690static void 741static void NOINLINE
691transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 742transfer (struct coro *prev, struct coro *next, int flags)
692{ 743{
693 dSTACKLEVEL; 744 dSTACKLEVEL;
694 745
695 /* sometimes transfer is only called to set idle_sp */ 746 /* sometimes transfer is only called to set idle_sp */
696 if (flags == TRANSFER_SET_STACKLEVEL) 747 if (flags == TRANSFER_SET_STACKLEVEL)
697 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 748 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
698 else if (prev != next) 749 else if (prev != next)
699 { 750 {
700 coro_stack *prev__stack; 751 coro_cctx *prev__cctx;
701 752
702 LOCK; 753 LOCK;
703 754
704 if (next->mainstack) 755 if (next->mainstack)
705 { 756 {
713 /* first get rid of the old state */ 764 /* first get rid of the old state */
714 SAVE (prev, -1); 765 SAVE (prev, -1);
715 /* setup coroutine call */ 766 /* setup coroutine call */
716 setup_coro (next); 767 setup_coro (next);
717 /* need a stack */ 768 /* need a stack */
718 next->stack = 0; 769 next->cctx = 0;
719 } 770 }
720 771
721 if (!prev->stack) 772 if (!prev->cctx)
722 /* create a new empty context */ 773 /* create a new empty context */
723 Newz (0, prev->stack, 1, coro_stack); 774 Newz (0, prev->cctx, 1, coro_cctx);
724 775
725 prev__stack = prev->stack; 776 prev__cctx = prev->cctx;
726 777
727 /* possibly "free" the stack */ 778 /* possibly "free" the cctx */
728 if (prev__stack->idle_sp == STACKLEVEL) 779 if (prev__cctx->idle_sp == STACKLEVEL)
729 { 780 {
730 stack_put (prev__stack); 781 cctx_put (prev__cctx);
731 prev->stack = 0; 782 prev->cctx = 0;
732 } 783 }
733 784
734 if (!next->stack) 785 if (!next->cctx)
735 next->stack = stack_get (); 786 next->cctx = cctx_get ();
736 787
737 if (prev__stack != next->stack) 788 if (prev__cctx != next->cctx)
738 { 789 {
739 prev__stack->top_env = PL_top_env; 790 prev__cctx->top_env = PL_top_env;
740 PL_top_env = next->stack->top_env; 791 PL_top_env = next->cctx->top_env;
741 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
742 } 793 }
743 794
744 free_coro_mortal (); 795 free_coro_mortal ();
745 796
746 UNLOCK; 797 UNLOCK;
747 } 798 }
748} 799}
749
750/* use this function pointer to call the above function */
751/* this is done to increase chances of the compiler not inlining the call */
752/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
753void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
754 800
755struct transfer_args 801struct transfer_args
756{ 802{
757 struct coro *prev, *next; 803 struct coro *prev, *next;
758 int flags; 804 int flags;
759}; 805};
760 806
761#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 807#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
762 808
763static void 809static void
764coro_state_destroy (struct coro *coro) 810coro_state_destroy (struct coro *coro)
765{ 811{
766 if (coro->refcnt--) 812 if (coro->refcnt--)
768 814
769 if (coro->mainstack && coro->mainstack != main_mainstack) 815 if (coro->mainstack && coro->mainstack != main_mainstack)
770 { 816 {
771 struct coro temp; 817 struct coro temp;
772 818
773 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 819 SAVE ((&temp), TRANSFER_SAVE_ALL);
774 LOAD (aTHX_ coro); 820 LOAD (coro);
775 821
776 destroy_stacks (aTHX); 822 coro_destroy_stacks ();
777 823
778 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 824 LOAD ((&temp)); /* this will get rid of defsv etc.. */
779 825
780 coro->mainstack = 0; 826 coro->mainstack = 0;
781 } 827 }
782 828
783 stack_free (coro->stack); 829 cctx_free (coro->cctx);
784 SvREFCNT_dec (coro->args); 830 SvREFCNT_dec (coro->args);
785 Safefree (coro); 831 Safefree (coro);
786} 832}
787 833
788static int 834static int
789coro_state_clear (SV *sv, MAGIC *mg) 835coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
790{ 836{
791 struct coro *coro = (struct coro *)mg->mg_ptr; 837 struct coro *coro = (struct coro *)mg->mg_ptr;
792 mg->mg_ptr = 0; 838 mg->mg_ptr = 0;
793 839
794 coro_state_destroy (coro); 840 coro_state_destroy (coro);
795 841
796 return 0; 842 return 0;
797} 843}
798 844
799static int 845static int
800coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 846coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
801{ 847{
802 struct coro *coro = (struct coro *)mg->mg_ptr; 848 struct coro *coro = (struct coro *)mg->mg_ptr;
803 849
804 ++coro->refcnt; 850 ++coro->refcnt;
805 851
806 return 0; 852 return 0;
807} 853}
808 854
809static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 855static MGVTBL coro_state_vtbl = {
856 0, 0, 0, 0,
857 coro_state_clear,
858 0,
859#ifdef MGf_DUP
860 coro_state_dup,
861#else
862# define MGf_DUP 0
863#endif
864};
810 865
811static struct coro * 866static struct coro *
812SvSTATE (SV *coro) 867SvSTATE (SV *coro)
813{ 868{
814 HV *stash; 869 HV *stash;
829 assert (mg->mg_type == PERL_MAGIC_ext); 884 assert (mg->mg_type == PERL_MAGIC_ext);
830 return (struct coro *)mg->mg_ptr; 885 return (struct coro *)mg->mg_ptr;
831} 886}
832 887
833static void 888static void
834prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 889prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
835{ 890{
836 ta->prev = SvSTATE (prev); 891 ta->prev = SvSTATE (prev);
837 ta->next = SvSTATE (next); 892 ta->next = SvSTATE (next);
838 ta->flags = flags; 893 ta->flags = flags;
839} 894}
842api_transfer (SV *prev, SV *next, int flags) 897api_transfer (SV *prev, SV *next, int flags)
843{ 898{
844 dTHX; 899 dTHX;
845 struct transfer_args ta; 900 struct transfer_args ta;
846 901
847 prepare_transfer (aTHX_ &ta, prev, next, flags); 902 prepare_transfer (&ta, prev, next, flags);
848 TRANSFER (ta); 903 TRANSFER (ta);
849} 904}
850 905
851/** Coro ********************************************************************/ 906/** Coro ********************************************************************/
852 907
861static GV *coro_current, *coro_idle; 916static GV *coro_current, *coro_idle;
862static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 917static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
863static int coro_nready; 918static int coro_nready;
864 919
865static void 920static void
866coro_enq (pTHX_ SV *sv) 921coro_enq (SV *sv)
867{ 922{
868 int prio; 923 int prio;
869 924
870 if (SvTYPE (sv) != SVt_PVHV) 925 if (SvTYPE (sv) != SVt_PVHV)
871 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 926 croak ("Coro::ready tried to enqueue something that is not a coroutine");
875 av_push (coro_ready [prio - PRIO_MIN], sv); 930 av_push (coro_ready [prio - PRIO_MIN], sv);
876 coro_nready++; 931 coro_nready++;
877} 932}
878 933
879static SV * 934static SV *
880coro_deq (pTHX_ int min_prio) 935coro_deq (int min_prio)
881{ 936{
882 int prio = PRIO_MAX - PRIO_MIN; 937 int prio = PRIO_MAX - PRIO_MIN;
883 938
884 min_prio -= PRIO_MIN; 939 min_prio -= PRIO_MIN;
885 if (min_prio < 0) 940 if (min_prio < 0)
902 957
903 if (SvROK (coro)) 958 if (SvROK (coro))
904 coro = SvRV (coro); 959 coro = SvRV (coro);
905 960
906 LOCK; 961 LOCK;
907 coro_enq (aTHX_ SvREFCNT_inc (coro)); 962 coro_enq (SvREFCNT_inc (coro));
908 UNLOCK; 963 UNLOCK;
909} 964}
910 965
911static void 966static void
912prepare_schedule (aTHX_ struct transfer_args *ta) 967prepare_schedule (struct transfer_args *ta)
913{ 968{
914 SV *current, *prev, *next; 969 SV *current, *prev, *next;
915
916 LOCK;
917 970
918 current = GvSV (coro_current); 971 current = GvSV (coro_current);
919 972
920 for (;;) 973 for (;;)
921 { 974 {
922 LOCK; 975 LOCK;
923
924 next = coro_deq (aTHX_ PRIO_MIN); 976 next = coro_deq (PRIO_MIN);
977 UNLOCK;
925 978
926 if (next) 979 if (next)
927 break; 980 break;
928
929 UNLOCK;
930 981
931 { 982 {
932 dSP; 983 dSP;
933 984
934 ENTER; 985 ENTER;
945 996
946 prev = SvRV (current); 997 prev = SvRV (current);
947 SvRV (current) = next; 998 SvRV (current) = next;
948 999
949 /* free this only after the transfer */ 1000 /* free this only after the transfer */
1001 LOCK;
950 free_coro_mortal (); 1002 free_coro_mortal ();
1003 UNLOCK;
951 coro_mortal = prev; 1004 coro_mortal = prev;
952 1005
953 ta->prev = SvSTATE (prev); 1006 ta->prev = SvSTATE (prev);
954 ta->next = SvSTATE (next); 1007 ta->next = SvSTATE (next);
955 ta->flags = TRANSFER_SAVE_ALL; 1008 ta->flags = TRANSFER_SAVE_ALL;
956
957 UNLOCK;
958} 1009}
959 1010
960static void 1011static void
961prepare_cede (aTHX_ struct transfer_args *ta) 1012prepare_cede (struct transfer_args *ta)
962{ 1013{
963 LOCK; 1014 LOCK;
964 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 1015 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
965 UNLOCK; 1016 UNLOCK;
966 1017
967 prepare_schedule (ta); 1018 prepare_schedule (ta);
968} 1019}
969 1020
1027 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1078 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1028 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1079 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1029 1080
1030 for (i = 1; i < items; i++) 1081 for (i = 1; i < items; i++)
1031 av_push (coro->args, newSVsv (ST (i))); 1082 av_push (coro->args, newSVsv (ST (i)));
1032
1033 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1034 /*coro->stack = 0;*/
1035} 1083}
1036 OUTPUT: 1084 OUTPUT:
1037 RETVAL 1085 RETVAL
1038 1086
1039void 1087void
1048 struct transfer_args ta; 1096 struct transfer_args ta;
1049 1097
1050 switch (ix) 1098 switch (ix)
1051 { 1099 {
1052 case 0: 1100 case 0:
1053 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1101 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1054 ta.next = 0; 1102 ta.next = 0;
1055 ta.flags = TRANSFER_SET_STACKLEVEL; 1103 ta.flags = TRANSFER_SET_STACKLEVEL;
1056 break; 1104 break;
1057 1105
1058 case 1: 1106 case 1:
1112 ++coro_src->refcnt; 1160 ++coro_src->refcnt;
1113 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1161 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1114} 1162}
1115 1163
1116void 1164void
1117_nonlocal_goto (IV nextop)
1118 CODE:
1119 /* uuh, somebody will kill me again for this */
1120 PL_op->op_next = INT2PTR (OP *, nextop);
1121
1122void
1123_exit (code) 1165_exit (code)
1124 int code 1166 int code
1125 PROTOTYPE: $ 1167 PROTOTYPE: $
1126 CODE: 1168 CODE:
1127 _exit (code); 1169 _exit (code);
1170
1171int
1172cctx_count ()
1173 CODE:
1174 RETVAL = cctx_count;
1175 OUTPUT:
1176 RETVAL
1177
1178int
1179cctx_idle ()
1180 CODE:
1181 RETVAL = cctx_idle;
1182 OUTPUT:
1183 RETVAL
1128 1184
1129MODULE = Coro::State PACKAGE = Coro 1185MODULE = Coro::State PACKAGE = Coro
1130 1186
1131BOOT: 1187BOOT:
1132{ 1188{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines