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.101 by root, Mon Nov 27 00:08:18 2006 UTC vs.
Revision 1.115 by root, Fri Dec 1 13:36:08 2006 UTC

3#include "EXTERN.h" 3#include "EXTERN.h"
4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8
9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
8 15
9#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
10 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
11 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
12 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
13 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
14 21
15#if !PERL_VERSION_ATLEAST (5,8,0) 22#if !PERL_VERSION_ATLEAST (5,6,0)
16# ifndef PL_ppaddr 23# ifndef PL_ppaddr
17# define PL_ppaddr ppaddr 24# define PL_ppaddr ppaddr
18# endif 25# endif
19# ifndef call_sv 26# ifndef call_sv
20# define call_sv perl_call_sv 27# define call_sv perl_call_sv
58# ifndef PAGESIZE 65# ifndef PAGESIZE
59# define PAGESIZE pagesize 66# define PAGESIZE pagesize
60# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 67# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
61static long pagesize; 68static long pagesize;
62# else 69# else
63# define BOOT_PAGESIZE 70# define BOOT_PAGESIZE (void)0
64# endif 71# endif
72#else
73# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0
65#endif 75#endif
66 76
67/* The next macro should declare a variable stacklevel that contains and approximation 77/* The next macro should declare a variable stacklevel that contains and approximation
68 * to the current C stack pointer. Its property is that it changes with each call 78 * to the current C stack pointer. Its property is that it changes with each call
69 * and should be unique. */ 79 * and should be unique. */
96static struct CoroAPI coroapi; 106static struct CoroAPI coroapi;
97static AV *main_mainstack; /* used to differentiate between $main and others */ 107static AV *main_mainstack; /* used to differentiate between $main and others */
98static HV *coro_state_stash, *coro_stash; 108static HV *coro_state_stash, *coro_stash;
99static SV *coro_mortal; /* will be freed after next transfer */ 109static SV *coro_mortal; /* will be freed after next transfer */
100 110
111static struct coro_cctx *cctx_first;
112static int cctx_count, cctx_idle;
113
101/* this is a structure representing a c-level coroutine */ 114/* this is a structure representing a c-level coroutine */
102typedef struct coro_stack { 115typedef struct coro_cctx {
103 struct coro_stack *next; 116 struct coro_cctx *next;
104 117
105 /* the stack */ 118 /* the stack */
106 void *sptr; 119 void *sptr;
107 long ssize; /* positive == mmap, otherwise malloc */ 120 long ssize; /* positive == mmap, otherwise malloc */
108 121
109 /* cpu state */ 122 /* cpu state */
110 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
111 JMPENV *top_env; 124 JMPENV *top_env;
112 coro_context cctx; 125 coro_context cctx;
113} coro_stack; 126
127 int inuse;
128
129#if USE_VALGRIND
130 int valgrind_id;
131#endif
132} coro_cctx;
133
134enum {
135 CF_RUNNING, /* coroutine is running */
136 CF_READY, /* coroutine is ready */
137};
114 138
115/* this is a structure representing a perl-level coroutine */ 139/* this is a structure representing a perl-level coroutine */
116struct coro { 140struct coro {
117 /* the c coroutine allocated to this perl coroutine, if any */ 141 /* the c coroutine allocated to this perl coroutine, if any */
118 coro_stack *stack; 142 coro_cctx *cctx;
119 143
120 /* data associated with this coroutine (initial args) */ 144 /* data associated with this coroutine (initial args) */
121 AV *args; 145 AV *args;
122 int refcnt; 146 int refcnt;
147 int flags;
123 148
124 /* optionally saved, might be zero */ 149 /* optionally saved, might be zero */
125 AV *defav; 150 AV *defav;
126 SV *defsv; 151 SV *defsv;
127 SV *errsv; 152 SV *errsv;
128 153
129 /* saved global state not related to stacks */ 154#define VAR(name,type) type name;
130 U8 dowarn; 155# include "state.h"
131 I32 in_eval; 156#undef VAR
132
133 /* the stacks and related info (callchain etc..) */
134 PERL_SI *curstackinfo;
135 AV *curstack;
136 AV *mainstack;
137 SV **stack_sp;
138 OP *op;
139 SV **curpad;
140 AV *comppad;
141 CV *compcv;
142 SV **stack_base;
143 SV **stack_max;
144 SV **tmps_stack;
145 I32 tmps_floor;
146 I32 tmps_ix;
147 I32 tmps_max;
148 I32 *markstack;
149 I32 *markstack_ptr;
150 I32 *markstack_max;
151 I32 *scopestack;
152 I32 scopestack_ix;
153 I32 scopestack_max;
154 ANY *savestack;
155 I32 savestack_ix;
156 I32 savestack_max;
157 OP **retstack;
158 I32 retstack_ix;
159 I32 retstack_max;
160 PMOP *curpm;
161 COP *curcop;
162 157
163 /* coro process data */ 158 /* coro process data */
164 int prio; 159 int prio;
165}; 160};
166 161
230 225
231#define PERL_MAGIC_coro PERL_MAGIC_ext 226#define PERL_MAGIC_coro PERL_MAGIC_ext
232 227
233static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 228static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
234 229
230#define CORO_MAGIC(cv) \
231 SvMAGIC (cv) \
232 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
233 ? SvMAGIC (cv) \
234 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
235 : 0
236
235/* the next two functions merely cache the padlists */ 237/* the next two functions merely cache the padlists */
236static void 238static void
237get_padlist (CV *cv) 239get_padlist (CV *cv)
238{ 240{
239 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 241 MAGIC *mg = CORO_MAGIC (cv);
242 AV *av;
240 243
241 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 244 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
242 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 245 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
243 else 246 else
244 { 247 {
245#if 0 248#if 0
246 /* this is probably cleaner, but also slower? */ 249 /* this is probably cleaner, but also slower? */
247 CV *cp = Perl_cv_clone (cv); 250 CV *cp = Perl_cv_clone (cv);
255} 258}
256 259
257static void 260static void
258put_padlist (CV *cv) 261put_padlist (CV *cv)
259{ 262{
260 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 263 MAGIC *mg = CORO_MAGIC (cv);
264 AV *av;
261 265
262 if (!mg) 266 if (!mg)
263 { 267 {
264 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 268 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
265 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 269 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
266 mg->mg_virtual = &vtbl_coro; 270 mg->mg_virtual = &vtbl_coro;
267 mg->mg_obj = (SV *)newAV (); 271 mg->mg_obj = (SV *)newAV ();
268 } 272 }
269 273
270 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 274 av = (AV *)mg->mg_obj;
275
276 if (AvFILLp (av) >= AvMAX (av))
277 av_extend (av, AvMAX (av) + 1);
278
279 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
271} 280}
272 281
273#define SB do { 282#define SB do {
274#define SE } while (0) 283#define SE } while (0)
275 284
279#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 288#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
280 289
281static void 290static void
282load_state(Coro__State c) 291load_state(Coro__State c)
283{ 292{
284 PL_dowarn = c->dowarn; 293#define VAR(name,type) PL_ ## name = c->name;
285 PL_in_eval = c->in_eval; 294# include "state.h"
286 295#undef VAR
287 PL_curstackinfo = c->curstackinfo;
288 PL_curstack = c->curstack;
289 PL_mainstack = c->mainstack;
290 PL_stack_sp = c->stack_sp;
291 PL_op = c->op;
292 PL_curpad = c->curpad;
293 PL_comppad = c->comppad;
294 PL_compcv = c->compcv;
295 PL_stack_base = c->stack_base;
296 PL_stack_max = c->stack_max;
297 PL_tmps_stack = c->tmps_stack;
298 PL_tmps_floor = c->tmps_floor;
299 PL_tmps_ix = c->tmps_ix;
300 PL_tmps_max = c->tmps_max;
301 PL_markstack = c->markstack;
302 PL_markstack_ptr = c->markstack_ptr;
303 PL_markstack_max = c->markstack_max;
304 PL_scopestack = c->scopestack;
305 PL_scopestack_ix = c->scopestack_ix;
306 PL_scopestack_max = c->scopestack_max;
307 PL_savestack = c->savestack;
308 PL_savestack_ix = c->savestack_ix;
309 PL_savestack_max = c->savestack_max;
310#if !PERL_VERSION_ATLEAST (5,9,0)
311 PL_retstack = c->retstack;
312 PL_retstack_ix = c->retstack_ix;
313 PL_retstack_max = c->retstack_max;
314#endif
315 PL_curpm = c->curpm;
316 PL_curcop = c->curcop;
317 296
318 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 297 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
319 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 298 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
320 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 299 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
321 300
324 CV *cv; 303 CV *cv;
325 304
326 /* now do the ugly restore mess */ 305 /* now do the ugly restore mess */
327 while ((cv = (CV *)POPs)) 306 while ((cv = (CV *)POPs))
328 { 307 {
329 AV *padlist = (AV *)POPs;
330
331 if (padlist)
332 {
333 put_padlist (cv); /* mark this padlist as available */ 308 put_padlist (cv); /* mark this padlist as available */
334 CvPADLIST(cv) = padlist; 309 CvDEPTH (cv) = PTR2IV (POPs);
335 } 310 CvPADLIST (cv) = (AV *)POPs;
336
337 ++CvDEPTH(cv);
338 } 311 }
339 312
340 PUTBACK; 313 PUTBACK;
341 } 314 }
342} 315}
364 PERL_CONTEXT *cx = &ccstk[cxix--]; 337 PERL_CONTEXT *cx = &ccstk[cxix--];
365 338
366 if (CxTYPE(cx) == CXt_SUB) 339 if (CxTYPE(cx) == CXt_SUB)
367 { 340 {
368 CV *cv = cx->blk_sub.cv; 341 CV *cv = cx->blk_sub.cv;
342
369 if (CvDEPTH(cv)) 343 if (CvDEPTH (cv))
370 { 344 {
371 EXTEND (SP, CvDEPTH(cv)*2); 345 EXTEND (SP, 3);
372
373 while (--CvDEPTH(cv))
374 {
375 /* this tells the restore code to increment CvDEPTH */
376 PUSHs (Nullsv);
377 PUSHs ((SV *)cv);
378 }
379 346
380 PUSHs ((SV *)CvPADLIST(cv)); 347 PUSHs ((SV *)CvPADLIST(cv));
348 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
381 PUSHs ((SV *)cv); 349 PUSHs ((SV *)cv);
382 350
351 CvDEPTH (cv) = 0;
383 get_padlist (cv); 352 get_padlist (cv);
384 } 353 }
385 } 354 }
386#ifdef CXt_FORMAT 355#ifdef CXt_FORMAT
387 else if (CxTYPE(cx) == CXt_FORMAT) 356 else if (CxTYPE(cx) == CXt_FORMAT)
406 375
407 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 376 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
408 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 377 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
409 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 378 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
410 379
411 c->dowarn = PL_dowarn; 380#define VAR(name,type)c->name = PL_ ## name;
412 c->in_eval = PL_in_eval; 381# include "state.h"
413 382#undef VAR
414 c->curstackinfo = PL_curstackinfo;
415 c->curstack = PL_curstack;
416 c->mainstack = PL_mainstack;
417 c->stack_sp = PL_stack_sp;
418 c->op = PL_op;
419 c->curpad = PL_curpad;
420 c->comppad = PL_comppad;
421 c->compcv = PL_compcv;
422 c->stack_base = PL_stack_base;
423 c->stack_max = PL_stack_max;
424 c->tmps_stack = PL_tmps_stack;
425 c->tmps_floor = PL_tmps_floor;
426 c->tmps_ix = PL_tmps_ix;
427 c->tmps_max = PL_tmps_max;
428 c->markstack = PL_markstack;
429 c->markstack_ptr = PL_markstack_ptr;
430 c->markstack_max = PL_markstack_max;
431 c->scopestack = PL_scopestack;
432 c->scopestack_ix = PL_scopestack_ix;
433 c->scopestack_max = PL_scopestack_max;
434 c->savestack = PL_savestack;
435 c->savestack_ix = PL_savestack_ix;
436 c->savestack_max = PL_savestack_max;
437#if !PERL_VERSION_ATLEAST (5,9,0)
438 c->retstack = PL_retstack;
439 c->retstack_ix = PL_retstack_ix;
440 c->retstack_max = PL_retstack_max;
441#endif
442 c->curpm = PL_curpm;
443 c->curcop = PL_curcop;
444} 383}
445 384
446/* 385/*
447 * allocate various perl stacks. This is an exact copy 386 * allocate various perl stacks. This is an exact copy
448 * of perl.c:init_stacks, except that it uses less memory 387 * of perl.c:init_stacks, except that it uses less memory
491 430
492/* 431/*
493 * destroy the stacks, the callchain etc... 432 * destroy the stacks, the callchain etc...
494 */ 433 */
495static void 434static void
496destroy_stacks() 435coro_destroy_stacks ()
497{ 436{
498 if (!IN_DESTRUCT) 437 if (!IN_DESTRUCT)
499 { 438 {
500 /* is this ugly, I ask? */ 439 /* is this ugly, I ask? */
501 LEAVE_SCOPE (0); 440 LEAVE_SCOPE (0);
543setup_coro (struct coro *coro) 482setup_coro (struct coro *coro)
544{ 483{
545 /* 484 /*
546 * emulate part of the perl startup here. 485 * emulate part of the perl startup here.
547 */ 486 */
548 dTHX;
549 dSP;
550 UNOP myop;
551 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
552 487
553 coro_init_stacks (); 488 coro_init_stacks ();
489
554 /*PL_curcop = 0;*/ 490 PL_curcop = 0;
555 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 491 PL_in_eval = 0;
492 PL_curpm = 0;
493
494 {
495 dSP;
496 LOGOP myop;
497
498 /* I have no idea why this is needed, but it is */
499 PUSHMARK (SP);
500
556 SvREFCNT_dec (GvAV (PL_defgv)); 501 SvREFCNT_dec (GvAV (PL_defgv));
557 GvAV (PL_defgv) = coro->args; coro->args = 0; 502 GvAV (PL_defgv) = coro->args; coro->args = 0;
558 503
559 SPAGAIN;
560
561 Zero (&myop, 1, UNOP); 504 Zero (&myop, 1, LOGOP);
562 myop.op_next = Nullop; 505 myop.op_next = Nullop;
563 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
564 507
565 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
566 509
567 PUSHMARK(SP); 510 PUSHMARK (SP);
568 XPUSHs (sub_init); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
569 PUTBACK; 512 PUTBACK;
570 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
571 SPAGAIN; 514 SPAGAIN;
572 515
573 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
517 }
574} 518}
575 519
576static void 520static void
577free_coro_mortal () 521free_coro_mortal ()
578{ 522{
582 coro_mortal = 0; 526 coro_mortal = 0;
583 } 527 }
584} 528}
585 529
586static void NOINLINE 530static void NOINLINE
587prepare_cctx (coro_stack *cctx) 531prepare_cctx (coro_cctx *cctx)
588{ 532{
589 dSP; 533 dSP;
590 UNOP myop; 534 LOGOP myop;
591 535
592 Zero (&myop, 1, UNOP); 536 Zero (&myop, 1, LOGOP);
593 myop.op_next = PL_op; 537 myop.op_next = PL_op;
594 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 538 myop.op_flags = OPf_WANT_VOID;
595 539
540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
541
596 PUSHMARK(SP); 542 PUSHMARK (SP);
597 EXTEND (SP, 2);
598 PUSHs (newSViv (PTR2IV (cctx)));
599 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
600 PUTBACK; 544 PUTBACK;
601 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 546 SPAGAIN;
603} 547}
604 548
605static void 549static void
606coro_run (void *arg) 550coro_run (void *arg)
607{ 551{
552 /* coro_run is the alternative epilogue of transfer() */
553 UNLOCK;
554
608 /* 555 /*
609 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
610 */ 557 */
611 UNLOCK;
612
613 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
559
560 /* inject call to cctx_init */
614 prepare_cctx ((coro_stack *)arg); 561 prepare_cctx ((coro_cctx *)arg);
615 562
616 /* somebody will hit me for both perl_run and PL_restartop */ 563 /* somebody will hit me for both perl_run and PL_restartop */
617 perl_run (PERL_GET_CONTEXT); 564 perl_run (PL_curinterp);
618 565
619 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 566 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
620 abort (); 567 abort ();
621} 568}
622 569
623static coro_stack * 570static coro_cctx *
624stack_new () 571cctx_new ()
625{ 572{
626 coro_stack *stack; 573 coro_cctx *cctx;
627 574
575 ++cctx_count;
576
628 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
629 578
630#if HAVE_MMAP 579#if HAVE_MMAP
631 580
632 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
633 /* mmap suppsedly does allocate-on-write for us */ 582 /* mmap suppsedly does allocate-on-write for us */
634 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 583 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
635 584
636 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
637 { 586 {
638 perror ("FATAL: unable to mmap stack for coroutine"); 587 perror ("FATAL: unable to mmap stack for coroutine");
639 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
640 } 589 }
641 590
642# if STACKGUARD 591# if STACKGUARD
643 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
644# endif 593# endif
645 594
646#else 595#else
647 596
648 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
649 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
650 599
651 if (!stack->sptr) 600 if (!cctx->sptr)
652 { 601 {
653 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 602 perror ("FATAL: unable to malloc stack for coroutine");
654 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
655 } 604 }
656 605
657#endif 606#endif
658 607
608#if USE_VALGRIND
609 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
610 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
611 cctx->ssize + (char *)cctx->sptr
612 );
613#endif
614
659 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 615 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
660 616
661 return stack; 617 return cctx;
662} 618}
663 619
664static void 620static void
665stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
666{ 622{
667 if (!stack) 623 if (!cctx)
668 return; 624 return;
669 625
626 --cctx_count;
627
628#if USE_VALGRIND
629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
630#endif
631
670#if HAVE_MMAP 632#if HAVE_MMAP
671 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
672#else 634#else
673 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
674#endif 636#endif
675 637
676 Safefree (stack); 638 Safefree (cctx);
677} 639}
678 640
679static coro_stack *stack_first;
680
681static coro_stack * 641static coro_cctx *
682stack_get () 642cctx_get ()
683{ 643{
684 coro_stack *stack; 644 coro_cctx *cctx;
685 645
686 if (stack_first) 646 if (cctx_first)
687 { 647 {
688 stack = stack_first; 648 cctx = cctx_first;
689 stack_first = stack->next; 649 cctx_first = cctx->next;
650 --cctx_idle;
690 } 651 }
691 else 652 else
692 { 653 {
693 stack = stack_new (); 654 cctx = cctx_new ();
694 PL_op = PL_op->op_next; 655 PL_op = PL_op->op_next;
695 } 656 }
696 657
697 return stack; 658 return cctx;
698} 659}
699 660
700static void 661static void
701stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
702{ 663{
703 stack->next = stack_first; 664 /* free another cctx if overlimit */
704 stack_first = stack; 665 if (cctx_idle >= MAX_IDLE_CCTX)
666 {
667 coro_cctx *first = cctx_first;
668 cctx_first = first->next;
669 --cctx_idle;
670
671 assert (!first->inuse);
672 cctx_destroy (first);
673 }
674
675 ++cctx_idle;
676 cctx->next = cctx_first;
677 cctx_first = cctx;
705} 678}
706 679
707/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
708static void NOINLINE 681static void NOINLINE
709transfer (struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
710{ 683{
711 dSTACKLEVEL; 684 dSTACKLEVEL;
712 685
713 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
714 if (flags == TRANSFER_SET_STACKLEVEL) 687 if (flags == TRANSFER_SET_STACKLEVEL)
715 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
716 else if (prev != next) 689 else if (prev != next)
717 { 690 {
718 coro_stack *prev__stack; 691 coro_cctx *prev__cctx;
692
693 if (!prev->cctx)
694 {
695 /* create a new empty context */
696 Newz (0, prev->cctx, 1, coro_cctx);
697 prev->cctx->inuse = 1;
698 prev->flags |= CF_RUNNING;
699 }
700
701 if (!prev->flags & CF_RUNNING)
702 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
703
704 if (next->flags & CF_RUNNING)
705 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
706
707 prev->flags &= ~CF_RUNNING;
708 next->flags |= CF_RUNNING;
719 709
720 LOCK; 710 LOCK;
721 711
722 if (next->mainstack) 712 if (next->mainstack)
723 { 713 {
730 /* need to start coroutine */ 720 /* need to start coroutine */
731 /* first get rid of the old state */ 721 /* first get rid of the old state */
732 SAVE (prev, -1); 722 SAVE (prev, -1);
733 /* setup coroutine call */ 723 /* setup coroutine call */
734 setup_coro (next); 724 setup_coro (next);
735 /* need a stack */ 725 /* need a new stack */
736 next->stack = 0; 726 assert (!next->stack);
737 } 727 }
738 728
739 if (!prev->stack)
740 /* create a new empty context */
741 Newz (0, prev->stack, 1, coro_stack);
742
743 prev__stack = prev->stack; 729 prev__cctx = prev->cctx;
744 730
745 /* possibly "free" the stack */ 731 /* possibly "free" the cctx */
746 if (prev__stack->idle_sp == STACKLEVEL) 732 if (prev__cctx->idle_sp == STACKLEVEL)
747 { 733 {
734 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
735 assert (PL_top_env == prev__cctx->top_env);
736
748 stack_put (prev__stack); 737 cctx_put (prev__cctx);
749 prev->stack = 0; 738 prev->cctx = 0;
750 } 739 }
751 740
752 if (!next->stack) 741 if (!next->cctx)
753 next->stack = stack_get (); 742 next->cctx = cctx_get ();
754 743
755 if (prev__stack != next->stack) 744 if (prev__cctx != next->cctx)
756 { 745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
757 prev__stack->top_env = PL_top_env; 752 prev__cctx->top_env = PL_top_env;
758 PL_top_env = next->stack->top_env; 753 PL_top_env = next->cctx->top_env;
759 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
760 } 755 }
761 756
762 free_coro_mortal (); 757 free_coro_mortal ();
763 758
764 UNLOCK; 759 UNLOCK;
777coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
778{ 773{
779 if (coro->refcnt--) 774 if (coro->refcnt--)
780 return; 775 return;
781 776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
779
782 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
783 { 781 {
784 struct coro temp; 782 struct coro temp;
785 783
786 SAVE ((&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
787 LOAD (coro); 785 LOAD (coro);
788 786
789 destroy_stacks (); 787 coro_destroy_stacks ();
790 788
791 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
792 790
793 coro->mainstack = 0; 791 coro->mainstack = 0;
794 } 792 }
795 793
796 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
797 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
798 Safefree (coro); 796 Safefree (coro);
799} 797}
800 798
801static int 799static int
823 0, 0, 0, 0, 821 0, 0, 0, 0,
824 coro_state_clear, 822 coro_state_clear,
825 0, 823 0,
826#ifdef MGf_DUP 824#ifdef MGf_DUP
827 coro_state_dup, 825 coro_state_dup,
826#else
827# define MGf_DUP 0
828#endif 828#endif
829}; 829};
830 830
831static struct coro * 831static struct coro *
832SvSTATE (SV *coro) 832SvSTATE (SV *coro)
881static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
883static int coro_nready; 883static int coro_nready;
884 884
885static void 885static void
886coro_enq (SV *sv) 886coro_enq (SV *coro_sv)
887{ 887{
888 int prio;
889
890 if (SvTYPE (sv) != SVt_PVHV)
891 croak ("Coro::ready tried to enqueue something that is not a coroutine");
892
893 prio = SvSTATE (sv)->prio;
894
895 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
896 coro_nready++; 889 coro_nready++;
897} 890}
898 891
899static SV * 892static SV *
900coro_deq (int min_prio) 893coro_deq (int min_prio)
913 } 906 }
914 907
915 return 0; 908 return 0;
916} 909}
917 910
918static void 911static int
919api_ready (SV *coro) 912api_ready (SV *coro_sv)
920{ 913{
921 dTHX; 914 struct coro *coro;
922 915
923 if (SvROK (coro)) 916 if (SvROK (coro_sv))
924 coro = SvRV (coro); 917 coro_sv = SvRV (coro_sv);
918
919 coro = SvSTATE (coro_sv);
920
921 if (coro->flags & CF_READY)
922 return 0;
923
924 if (coro->flags & CF_RUNNING)
925 croak ("Coro::ready called on currently running coroutine");
926
927 coro->flags |= CF_READY;
925 928
926 LOCK; 929 LOCK;
927 coro_enq (SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
928 UNLOCK; 931 UNLOCK;
932
933 return 1;
934}
935
936static int
937api_is_ready (SV *coro_sv)
938{
939 return !!SvSTATE (coro_sv)->flags & CF_READY;
929} 940}
930 941
931static void 942static void
932prepare_schedule (struct transfer_args *ta) 943prepare_schedule (struct transfer_args *ta)
933{ 944{
969 coro_mortal = prev; 980 coro_mortal = prev;
970 981
971 ta->prev = SvSTATE (prev); 982 ta->prev = SvSTATE (prev);
972 ta->next = SvSTATE (next); 983 ta->next = SvSTATE (next);
973 ta->flags = TRANSFER_SAVE_ALL; 984 ta->flags = TRANSFER_SAVE_ALL;
985
986 ta->next->flags &= ~CF_READY;
974} 987}
975 988
976static void 989static void
977prepare_cede (struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
978{ 991{
979 LOCK; 992 api_ready (GvSV (coro_current));
980 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
981 UNLOCK;
982 993
983 prepare_schedule (ta); 994 prepare_schedule (ta);
984} 995}
985 996
986static void 997static void
1043 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1054 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1044 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1045 1056
1046 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
1047 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
1048
1049 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1050 /*coro->stack = 0;*/
1051} 1059}
1052 OUTPUT: 1060 OUTPUT:
1053 RETVAL 1061 RETVAL
1054 1062
1055void 1063void
1064 struct transfer_args ta; 1072 struct transfer_args ta;
1065 1073
1066 switch (ix) 1074 switch (ix)
1067 { 1075 {
1068 case 0: 1076 case 0:
1069 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1070 ta.next = 0; 1078 ta.next = 0;
1071 ta.flags = TRANSFER_SET_STACKLEVEL; 1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1072 break; 1080 break;
1073 1081
1074 case 1: 1082 case 1:
1134 int code 1142 int code
1135 PROTOTYPE: $ 1143 PROTOTYPE: $
1136 CODE: 1144 CODE:
1137 _exit (code); 1145 _exit (code);
1138 1146
1147int
1148cctx_count ()
1149 CODE:
1150 RETVAL = cctx_count;
1151 OUTPUT:
1152 RETVAL
1153
1154int
1155cctx_idle ()
1156 CODE:
1157 RETVAL = cctx_idle;
1158 OUTPUT:
1159 RETVAL
1160
1139MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1140 1162
1141BOOT: 1163BOOT:
1142{ 1164{
1143 int i; 1165 int i;
1161 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1162 1184
1163 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1164 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1165 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1166 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1167 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1168 1191
1169 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1170 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1190 1213
1191 coro->prio = newprio; 1214 coro->prio = newprio;
1192 } 1215 }
1193} 1216}
1194 1217
1195void 1218SV *
1196ready (SV *self) 1219ready (SV *self)
1197 PROTOTYPE: $ 1220 PROTOTYPE: $
1198 CODE: 1221 CODE:
1199 api_ready (self); 1222 RETVAL = boolSV (api_ready (self));
1223 OUTPUT:
1224 RETVAL
1225
1226SV *
1227is_ready (SV *self)
1228 PROTOTYPE: $
1229 CODE:
1230 RETVAL = boolSV (api_is_ready (self));
1231 OUTPUT:
1232 RETVAL
1200 1233
1201int 1234int
1202nready (...) 1235nready (...)
1203 PROTOTYPE: 1236 PROTOTYPE:
1204 CODE: 1237 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines