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.104 by root, Mon Nov 27 01:51:02 2006 UTC vs.
Revision 1.116 by root, Fri Dec 1 14:01:43 2006 UTC

7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND 9#if USE_VALGRIND
10# include <valgrind/valgrind.h> 10# include <valgrind/valgrind.h>
11#endif 11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
12 15
13#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
62# ifndef PAGESIZE 65# ifndef PAGESIZE
63# define PAGESIZE pagesize 66# define PAGESIZE pagesize
64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 67# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
65static long pagesize; 68static long pagesize;
66# else 69# else
67# define BOOT_PAGESIZE 70# define BOOT_PAGESIZE (void)0
68# endif 71# endif
72#else
73# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0
69#endif 75#endif
70 76
71/* 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
72 * 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
73 * and should be unique. */ 79 * and should be unique. */
100static struct CoroAPI coroapi; 106static struct CoroAPI coroapi;
101static AV *main_mainstack; /* used to differentiate between $main and others */ 107static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash, *coro_stash; 108static HV *coro_state_stash, *coro_stash;
103static SV *coro_mortal; /* will be freed after next transfer */ 109static SV *coro_mortal; /* will be freed after next transfer */
104 110
111static struct coro_cctx *cctx_first;
112static int cctx_count, cctx_idle;
113
105/* this is a structure representing a c-level coroutine */ 114/* this is a structure representing a c-level coroutine */
106typedef struct coro_stack { 115typedef struct coro_cctx {
107 struct coro_stack *next; 116 struct coro_cctx *next;
108 117
109 /* the stack */ 118 /* the stack */
110 void *sptr; 119 void *sptr;
111 long ssize; /* positive == mmap, otherwise malloc */ 120 long ssize; /* positive == mmap, otherwise malloc */
112 121
113 /* cpu state */ 122 /* cpu state */
114 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
115 JMPENV *top_env; 124 JMPENV *top_env;
116 coro_context cctx; 125 coro_context cctx;
117 126
127 int inuse;
128
118#if USE_VALGRIND 129#if USE_VALGRIND
119 int valgrind_id; 130 int valgrind_id;
120#endif 131#endif
121} coro_stack; 132} coro_cctx;
133
134enum {
135 CF_RUNNING, /* coroutine is running */
136 CF_READY, /* coroutine is ready */
137};
122 138
123/* this is a structure representing a perl-level coroutine */ 139/* this is a structure representing a perl-level coroutine */
124struct coro { 140struct coro {
125 /* the c coroutine allocated to this perl coroutine, if any */ 141 /* the c coroutine allocated to this perl coroutine, if any */
126 coro_stack *stack; 142 coro_cctx *cctx;
127 143
128 /* data associated with this coroutine (initial args) */ 144 /* data associated with this coroutine (initial args) */
129 AV *args; 145 AV *args;
130 int refcnt; 146 int refcnt;
147 int flags;
131 148
132 /* optionally saved, might be zero */ 149 /* optionally saved, might be zero */
133 AV *defav; 150 AV *defav;
134 SV *defsv; 151 SV *defsv;
135 SV *errsv; 152 SV *errsv;
136 153
137 /* saved global state not related to stacks */ 154#define VAR(name,type) type name;
138 U8 dowarn; 155# include "state.h"
139 I32 in_eval; 156#undef VAR
140
141 /* the stacks and related info (callchain etc..) */
142 PERL_SI *curstackinfo;
143 AV *curstack;
144 AV *mainstack;
145 SV **stack_sp;
146 OP *op;
147 SV **curpad;
148 AV *comppad;
149 CV *compcv;
150 SV **stack_base;
151 SV **stack_max;
152 SV **tmps_stack;
153 I32 tmps_floor;
154 I32 tmps_ix;
155 I32 tmps_max;
156 I32 *markstack;
157 I32 *markstack_ptr;
158 I32 *markstack_max;
159 I32 *scopestack;
160 I32 scopestack_ix;
161 I32 scopestack_max;
162 ANY *savestack;
163 I32 savestack_ix;
164 I32 savestack_max;
165 OP **retstack;
166 I32 retstack_ix;
167 I32 retstack_max;
168 PMOP *curpm;
169 COP *curcop;
170 157
171 /* coro process data */ 158 /* coro process data */
172 int prio; 159 int prio;
173}; 160};
174 161
238 225
239#define PERL_MAGIC_coro PERL_MAGIC_ext 226#define PERL_MAGIC_coro PERL_MAGIC_ext
240 227
241static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 228static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
242 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
243/* the next two functions merely cache the padlists */ 237/* the next two functions merely cache the padlists */
244static void 238static void
245get_padlist (CV *cv) 239get_padlist (CV *cv)
246{ 240{
247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 241 MAGIC *mg = CORO_MAGIC (cv);
242 AV *av;
248 243
249 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 244 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
250 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 245 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
251 else 246 else
252 { 247 {
253#if 0 248#if 0
254 /* this is probably cleaner, but also slower? */ 249 /* this is probably cleaner, but also slower? */
255 CV *cp = Perl_cv_clone (cv); 250 CV *cp = Perl_cv_clone (cv);
263} 258}
264 259
265static void 260static void
266put_padlist (CV *cv) 261put_padlist (CV *cv)
267{ 262{
268 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 263 MAGIC *mg = CORO_MAGIC (cv);
264 AV *av;
269 265
270 if (!mg) 266 if (!mg)
271 { 267 {
272 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 268 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
273 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 269 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
274 mg->mg_virtual = &vtbl_coro; 270 mg->mg_virtual = &vtbl_coro;
275 mg->mg_obj = (SV *)newAV (); 271 mg->mg_obj = (SV *)newAV ();
276 } 272 }
277 273
278 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);
279} 280}
280 281
281#define SB do { 282#define SB do {
282#define SE } while (0) 283#define SE } while (0)
283 284
287#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
288 289
289static void 290static void
290load_state(Coro__State c) 291load_state(Coro__State c)
291{ 292{
292 PL_dowarn = c->dowarn; 293#define VAR(name,type) PL_ ## name = c->name;
293 PL_in_eval = c->in_eval; 294# include "state.h"
294 295#undef VAR
295 PL_curstackinfo = c->curstackinfo;
296 PL_curstack = c->curstack;
297 PL_mainstack = c->mainstack;
298 PL_stack_sp = c->stack_sp;
299 PL_op = c->op;
300 PL_curpad = c->curpad;
301 PL_comppad = c->comppad;
302 PL_compcv = c->compcv;
303 PL_stack_base = c->stack_base;
304 PL_stack_max = c->stack_max;
305 PL_tmps_stack = c->tmps_stack;
306 PL_tmps_floor = c->tmps_floor;
307 PL_tmps_ix = c->tmps_ix;
308 PL_tmps_max = c->tmps_max;
309 PL_markstack = c->markstack;
310 PL_markstack_ptr = c->markstack_ptr;
311 PL_markstack_max = c->markstack_max;
312 PL_scopestack = c->scopestack;
313 PL_scopestack_ix = c->scopestack_ix;
314 PL_scopestack_max = c->scopestack_max;
315 PL_savestack = c->savestack;
316 PL_savestack_ix = c->savestack_ix;
317 PL_savestack_max = c->savestack_max;
318#if !PERL_VERSION_ATLEAST (5,9,0)
319 PL_retstack = c->retstack;
320 PL_retstack_ix = c->retstack_ix;
321 PL_retstack_max = c->retstack_max;
322#endif
323 PL_curpm = c->curpm;
324 PL_curcop = c->curcop;
325 296
326 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 297 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
327 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 298 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
328 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 299 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
329 300
332 CV *cv; 303 CV *cv;
333 304
334 /* now do the ugly restore mess */ 305 /* now do the ugly restore mess */
335 while ((cv = (CV *)POPs)) 306 while ((cv = (CV *)POPs))
336 { 307 {
337 AV *padlist = (AV *)POPs;
338
339 if (padlist)
340 {
341 put_padlist (cv); /* mark this padlist as available */ 308 put_padlist (cv); /* mark this padlist as available */
342 CvPADLIST(cv) = padlist; 309 CvDEPTH (cv) = PTR2IV (POPs);
343 } 310 CvPADLIST (cv) = (AV *)POPs;
344
345 ++CvDEPTH(cv);
346 } 311 }
347 312
348 PUTBACK; 313 PUTBACK;
349 } 314 }
350} 315}
372 PERL_CONTEXT *cx = &ccstk[cxix--]; 337 PERL_CONTEXT *cx = &ccstk[cxix--];
373 338
374 if (CxTYPE(cx) == CXt_SUB) 339 if (CxTYPE(cx) == CXt_SUB)
375 { 340 {
376 CV *cv = cx->blk_sub.cv; 341 CV *cv = cx->blk_sub.cv;
342
377 if (CvDEPTH(cv)) 343 if (CvDEPTH (cv))
378 { 344 {
379 EXTEND (SP, CvDEPTH(cv)*2); 345 EXTEND (SP, 3);
380
381 while (--CvDEPTH(cv))
382 {
383 /* this tells the restore code to increment CvDEPTH */
384 PUSHs (Nullsv);
385 PUSHs ((SV *)cv);
386 }
387 346
388 PUSHs ((SV *)CvPADLIST(cv)); 347 PUSHs ((SV *)CvPADLIST(cv));
348 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
389 PUSHs ((SV *)cv); 349 PUSHs ((SV *)cv);
390 350
351 CvDEPTH (cv) = 0;
391 get_padlist (cv); 352 get_padlist (cv);
392 } 353 }
393 } 354 }
394#ifdef CXt_FORMAT 355#ifdef CXt_FORMAT
395 else if (CxTYPE(cx) == CXt_FORMAT) 356 else if (CxTYPE(cx) == CXt_FORMAT)
414 375
415 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;
416 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 377 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
417 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 378 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
418 379
419 c->dowarn = PL_dowarn; 380#define VAR(name,type)c->name = PL_ ## name;
420 c->in_eval = PL_in_eval; 381# include "state.h"
421 382#undef VAR
422 c->curstackinfo = PL_curstackinfo;
423 c->curstack = PL_curstack;
424 c->mainstack = PL_mainstack;
425 c->stack_sp = PL_stack_sp;
426 c->op = PL_op;
427 c->curpad = PL_curpad;
428 c->comppad = PL_comppad;
429 c->compcv = PL_compcv;
430 c->stack_base = PL_stack_base;
431 c->stack_max = PL_stack_max;
432 c->tmps_stack = PL_tmps_stack;
433 c->tmps_floor = PL_tmps_floor;
434 c->tmps_ix = PL_tmps_ix;
435 c->tmps_max = PL_tmps_max;
436 c->markstack = PL_markstack;
437 c->markstack_ptr = PL_markstack_ptr;
438 c->markstack_max = PL_markstack_max;
439 c->scopestack = PL_scopestack;
440 c->scopestack_ix = PL_scopestack_ix;
441 c->scopestack_max = PL_scopestack_max;
442 c->savestack = PL_savestack;
443 c->savestack_ix = PL_savestack_ix;
444 c->savestack_max = PL_savestack_max;
445#if !PERL_VERSION_ATLEAST (5,9,0)
446 c->retstack = PL_retstack;
447 c->retstack_ix = PL_retstack_ix;
448 c->retstack_max = PL_retstack_max;
449#endif
450 c->curpm = PL_curpm;
451 c->curcop = PL_curcop;
452} 383}
453 384
454/* 385/*
455 * allocate various perl stacks. This is an exact copy 386 * allocate various perl stacks. This is an exact copy
456 * of perl.c:init_stacks, except that it uses less memory 387 * of perl.c:init_stacks, except that it uses less memory
458 * not usually need a lot of stackspace. 389 * not usually need a lot of stackspace.
459 */ 390 */
460static void 391static void
461coro_init_stacks () 392coro_init_stacks ()
462{ 393{
463 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 394 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
464 PL_curstackinfo->si_type = PERLSI_MAIN; 395 PL_curstackinfo->si_type = PERLSI_MAIN;
465 PL_curstack = PL_curstackinfo->si_stack; 396 PL_curstack = PL_curstackinfo->si_stack;
466 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 397 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
467 398
468 PL_stack_base = AvARRAY(PL_curstack); 399 PL_stack_base = AvARRAY(PL_curstack);
469 PL_stack_sp = PL_stack_base; 400 PL_stack_sp = PL_stack_base;
470 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 401 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
471 402
472 New(50,PL_tmps_stack,96,SV*); 403 New(50,PL_tmps_stack,128,SV*);
473 PL_tmps_floor = -1; 404 PL_tmps_floor = -1;
474 PL_tmps_ix = -1; 405 PL_tmps_ix = -1;
475 PL_tmps_max = 96; 406 PL_tmps_max = 128;
476 407
477 New(54,PL_markstack,16,I32); 408 New(54,PL_markstack,32,I32);
478 PL_markstack_ptr = PL_markstack; 409 PL_markstack_ptr = PL_markstack;
479 PL_markstack_max = PL_markstack + 16; 410 PL_markstack_max = PL_markstack + 32;
480 411
481#ifdef SET_MARK_OFFSET 412#ifdef SET_MARK_OFFSET
482 SET_MARK_OFFSET; 413 SET_MARK_OFFSET;
483#endif 414#endif
484 415
485 New(54,PL_scopestack,16,I32); 416 New(54,PL_scopestack,32,I32);
486 PL_scopestack_ix = 0; 417 PL_scopestack_ix = 0;
487 PL_scopestack_max = 16; 418 PL_scopestack_max = 32;
488 419
489 New(54,PL_savestack,96,ANY); 420 New(54,PL_savestack,64,ANY);
490 PL_savestack_ix = 0; 421 PL_savestack_ix = 0;
491 PL_savestack_max = 96; 422 PL_savestack_max = 64;
492 423
493#if !PERL_VERSION_ATLEAST (5,9,0) 424#if !PERL_VERSION_ATLEAST (5,9,0)
494 New(54,PL_retstack,8,OP*); 425 New(54,PL_retstack,16,OP*);
495 PL_retstack_ix = 0; 426 PL_retstack_ix = 0;
496 PL_retstack_max = 8; 427 PL_retstack_max = 16;
497#endif 428#endif
498} 429}
499 430
500/* 431/*
501 * destroy the stacks, the callchain etc... 432 * destroy the stacks, the callchain etc...
502 */ 433 */
503static void 434static void
504coro_destroy_stacks() 435coro_destroy_stacks ()
505{ 436{
506 if (!IN_DESTRUCT) 437 if (!IN_DESTRUCT)
507 { 438 {
508 /* is this ugly, I ask? */ 439 /* is this ugly, I ask? */
509 LEAVE_SCOPE (0); 440 LEAVE_SCOPE (0);
575 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
576 507
577 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
578 509
579 PUSHMARK (SP); 510 PUSHMARK (SP);
580 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
581 PUTBACK; 512 PUTBACK;
582 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
583 SPAGAIN; 514 SPAGAIN;
584 515
585 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
595 coro_mortal = 0; 526 coro_mortal = 0;
596 } 527 }
597} 528}
598 529
599static void NOINLINE 530static void NOINLINE
600prepare_cctx (coro_stack *cctx) 531prepare_cctx (coro_cctx *cctx)
601{ 532{
602 dSP; 533 dSP;
603 LOGOP myop; 534 LOGOP myop;
604 535
605 Zero (&myop, 1, LOGOP); 536 Zero (&myop, 1, LOGOP);
606 myop.op_next = PL_op; 537 myop.op_next = PL_op;
607 myop.op_flags = OPf_WANT_VOID; 538 myop.op_flags = OPf_WANT_VOID;
608 539
609 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
610 541
611 PUSHMARK (SP); 542 PUSHMARK (SP);
612 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
613 PUTBACK; 544 PUTBACK;
614 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
615 SPAGAIN; 546 SPAGAIN;
616} 547}
617 548
623 554
624 /* 555 /*
625 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
626 */ 557 */
627 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
559
628 /* inject call to cctx_init */ 560 /* inject call to cctx_init */
629 prepare_cctx ((coro_stack *)arg); 561 prepare_cctx ((coro_cctx *)arg);
630 562
631 /* somebody will hit me for both perl_run and PL_restartop */ 563 /* somebody will hit me for both perl_run and PL_restartop */
632 perl_run (PL_curinterp); 564 perl_run (PL_curinterp);
633 565
634 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);
635 abort (); 567 abort ();
636} 568}
637 569
638static coro_stack * 570static coro_cctx *
639stack_new () 571cctx_new ()
640{ 572{
641 coro_stack *stack; 573 coro_cctx *cctx;
642 574
575 ++cctx_count;
576
643 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
644 578
645#if HAVE_MMAP 579#if HAVE_MMAP
646 580
647 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
648 /* mmap suppsedly does allocate-on-write for us */ 582 /* mmap suppsedly does allocate-on-write for us */
649 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);
650 584
651 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
652 { 586 {
653 perror ("FATAL: unable to mmap stack for coroutine"); 587 perror ("FATAL: unable to mmap stack for coroutine");
654 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
655 } 589 }
656 590
657# if STACKGUARD 591# if STACKGUARD
658 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
659# endif 593# endif
660 594
661#else 595#else
662 596
663 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
664 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
665 599
666 if (!stack->sptr) 600 if (!cctx->sptr)
667 { 601 {
668 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 602 perror ("FATAL: unable to malloc stack for coroutine");
669 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
670 } 604 }
671 605
672#endif 606#endif
673 607
674#if USE_VALGRIND 608#if USE_VALGRIND
675 stack->valgrind_id = VALGRIND_STACK_REGISTER ( 609 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
676 STACKGUARD * PAGESIZE + (char *)stack->sptr, 610 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
677 stack->ssize + (char *)stack->sptr 611 cctx->ssize + (char *)cctx->sptr
678 ); 612 );
679#endif 613#endif
680 614
681 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);
682 616
683 return stack; 617 return cctx;
684} 618}
685 619
686static void 620static void
687stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
688{ 622{
689 if (!stack) 623 if (!cctx)
690 return; 624 return;
691 625
626 --cctx_count;
627
692#if USE_VALGRIND 628#if USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id); 629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
694#endif 630#endif
695 631
696#if HAVE_MMAP 632#if HAVE_MMAP
697 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
698#else 634#else
699 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
700#endif 636#endif
701 637
702 Safefree (stack); 638 Safefree (cctx);
703} 639}
704 640
705static coro_stack *stack_first;
706static int cctx_count, cctx_idle;
707
708static coro_stack * 641static coro_cctx *
709stack_get () 642cctx_get ()
710{ 643{
711 coro_stack *stack; 644 coro_cctx *cctx;
712 645
713 if (stack_first) 646 if (cctx_first)
714 { 647 {
648 cctx = cctx_first;
649 cctx_first = cctx->next;
715 --cctx_idle; 650 --cctx_idle;
716 stack = stack_first;
717 stack_first = stack->next;
718 } 651 }
719 else 652 else
720 { 653 {
721 ++cctx_count; 654 cctx = cctx_new ();
722 stack = stack_new ();
723 PL_op = PL_op->op_next; 655 PL_op = PL_op->op_next;
724 } 656 }
725 657
726 return stack; 658 return cctx;
727} 659}
728 660
729static void 661static void
730stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
731{ 663{
664 /* free another cctx if overlimit */
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
732 ++cctx_idle; 675 ++cctx_idle;
733 stack->next = stack_first; 676 cctx->next = cctx_first;
734 stack_first = stack; 677 cctx_first = cctx;
735} 678}
736 679
737/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
738static void NOINLINE 681static void NOINLINE
739transfer (struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
740{ 683{
741 dSTACKLEVEL; 684 dSTACKLEVEL;
742 685
743 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
744 if (flags == TRANSFER_SET_STACKLEVEL) 687 if (flags == TRANSFER_SET_STACKLEVEL)
745 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
746 else if (prev != next) 689 else if (prev != next)
747 { 690 {
748 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;
749 709
750 LOCK; 710 LOCK;
751 711
752 if (next->mainstack) 712 if (next->mainstack)
753 { 713 {
760 /* need to start coroutine */ 720 /* need to start coroutine */
761 /* first get rid of the old state */ 721 /* first get rid of the old state */
762 SAVE (prev, -1); 722 SAVE (prev, -1);
763 /* setup coroutine call */ 723 /* setup coroutine call */
764 setup_coro (next); 724 setup_coro (next);
765 /* need a stack */ 725 /* need a new stack */
766 next->stack = 0; 726 assert (!next->stack);
767 } 727 }
768 728
769 if (!prev->stack)
770 /* create a new empty context */
771 Newz (0, prev->stack, 1, coro_stack);
772
773 prev__stack = prev->stack; 729 prev__cctx = prev->cctx;
774 730
775 /* possibly "free" the stack */ 731 /* possibly "free" the cctx */
776 if (prev__stack->idle_sp == STACKLEVEL) 732 if (prev__cctx->idle_sp == STACKLEVEL)
777 { 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
778 stack_put (prev__stack); 737 cctx_put (prev__cctx);
779 prev->stack = 0; 738 prev->cctx = 0;
780 } 739 }
781 740
782 if (!next->stack) 741 if (!next->cctx)
783 next->stack = stack_get (); 742 next->cctx = cctx_get ();
784 743
785 if (prev__stack != next->stack) 744 if (prev__cctx != next->cctx)
786 { 745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
787 prev__stack->top_env = PL_top_env; 752 prev__cctx->top_env = PL_top_env;
788 PL_top_env = next->stack->top_env; 753 PL_top_env = next->cctx->top_env;
789 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
790 } 755 }
791 756
792 free_coro_mortal (); 757 free_coro_mortal ();
793 758
794 UNLOCK; 759 UNLOCK;
807coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
808{ 773{
809 if (coro->refcnt--) 774 if (coro->refcnt--)
810 return; 775 return;
811 776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
779
812 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
813 { 781 {
814 struct coro temp; 782 struct coro temp;
815 783
816 SAVE ((&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822 790
823 coro->mainstack = 0; 791 coro->mainstack = 0;
824 } 792 }
825 793
826 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
827 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
828 Safefree (coro); 796 Safefree (coro);
829} 797}
830 798
831static int 799static int
913static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
914static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
915static int coro_nready; 883static int coro_nready;
916 884
917static void 885static void
918coro_enq (SV *sv) 886coro_enq (SV *coro_sv)
919{ 887{
920 int prio;
921
922 if (SvTYPE (sv) != SVt_PVHV)
923 croak ("Coro::ready tried to enqueue something that is not a coroutine");
924
925 prio = SvSTATE (sv)->prio;
926
927 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
928 coro_nready++; 889 coro_nready++;
929} 890}
930 891
931static SV * 892static SV *
932coro_deq (int min_prio) 893coro_deq (int min_prio)
945 } 906 }
946 907
947 return 0; 908 return 0;
948} 909}
949 910
950static void 911static int
951api_ready (SV *coro) 912api_ready (SV *coro_sv)
952{ 913{
953 dTHX; 914 struct coro *coro;
954 915
955 if (SvROK (coro)) 916 if (SvROK (coro_sv))
956 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;
957 928
958 LOCK; 929 LOCK;
959 coro_enq (SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
960 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;
961} 940}
962 941
963static void 942static void
964prepare_schedule (struct transfer_args *ta) 943prepare_schedule (struct transfer_args *ta)
965{ 944{
1001 coro_mortal = prev; 980 coro_mortal = prev;
1002 981
1003 ta->prev = SvSTATE (prev); 982 ta->prev = SvSTATE (prev);
1004 ta->next = SvSTATE (next); 983 ta->next = SvSTATE (next);
1005 ta->flags = TRANSFER_SAVE_ALL; 984 ta->flags = TRANSFER_SAVE_ALL;
985
986 ta->next->flags &= ~CF_READY;
1006} 987}
1007 988
1008static void 989static void
1009prepare_cede (struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
1010{ 991{
1011 LOCK; 992 api_ready (GvSV (coro_current));
1012 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
1013 UNLOCK;
1014 993
1015 prepare_schedule (ta); 994 prepare_schedule (ta);
1016} 995}
1017 996
1018static void 997static void
1075 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;
1076 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1077 1056
1078 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
1079 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
1080
1081 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1082 /*coro->stack = 0;*/
1083} 1059}
1084 OUTPUT: 1060 OUTPUT:
1085 RETVAL 1061 RETVAL
1086 1062
1087void 1063void
1096 struct transfer_args ta; 1072 struct transfer_args ta;
1097 1073
1098 switch (ix) 1074 switch (ix)
1099 { 1075 {
1100 case 0: 1076 case 0:
1101 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1102 ta.next = 0; 1078 ta.next = 0;
1103 ta.flags = TRANSFER_SET_STACKLEVEL; 1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1104 break; 1080 break;
1105 1081
1106 case 1: 1082 case 1:
1166 int code 1142 int code
1167 PROTOTYPE: $ 1143 PROTOTYPE: $
1168 CODE: 1144 CODE:
1169 _exit (code); 1145 _exit (code);
1170 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
1171MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1172 1162
1173BOOT: 1163BOOT:
1174{ 1164{
1175 int i; 1165 int i;
1193 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1194 1184
1195 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1196 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1197 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1198 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1199 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1200 1191
1201 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1202 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1222 1213
1223 coro->prio = newprio; 1214 coro->prio = newprio;
1224 } 1215 }
1225} 1216}
1226 1217
1227void 1218SV *
1228ready (SV *self) 1219ready (SV *self)
1229 PROTOTYPE: $ 1220 PROTOTYPE: $
1230 CODE: 1221 CODE:
1231 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
1232 1233
1233int 1234int
1234nready (...) 1235nready (...)
1235 PROTOTYPE: 1236 PROTOTYPE:
1236 CODE: 1237 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines