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.103 by root, Mon Nov 27 01:33:30 2006 UTC vs.
Revision 1.116 by root, Fri Dec 1 14:01:43 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) \
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
450 * not usually need a lot of stackspace. 389 * not usually need a lot of stackspace.
451 */ 390 */
452static void 391static void
453coro_init_stacks () 392coro_init_stacks ()
454{ 393{
455 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 394 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
456 PL_curstackinfo->si_type = PERLSI_MAIN; 395 PL_curstackinfo->si_type = PERLSI_MAIN;
457 PL_curstack = PL_curstackinfo->si_stack; 396 PL_curstack = PL_curstackinfo->si_stack;
458 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 397 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
459 398
460 PL_stack_base = AvARRAY(PL_curstack); 399 PL_stack_base = AvARRAY(PL_curstack);
461 PL_stack_sp = PL_stack_base; 400 PL_stack_sp = PL_stack_base;
462 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 401 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
463 402
464 New(50,PL_tmps_stack,96,SV*); 403 New(50,PL_tmps_stack,128,SV*);
465 PL_tmps_floor = -1; 404 PL_tmps_floor = -1;
466 PL_tmps_ix = -1; 405 PL_tmps_ix = -1;
467 PL_tmps_max = 96; 406 PL_tmps_max = 128;
468 407
469 New(54,PL_markstack,16,I32); 408 New(54,PL_markstack,32,I32);
470 PL_markstack_ptr = PL_markstack; 409 PL_markstack_ptr = PL_markstack;
471 PL_markstack_max = PL_markstack + 16; 410 PL_markstack_max = PL_markstack + 32;
472 411
473#ifdef SET_MARK_OFFSET 412#ifdef SET_MARK_OFFSET
474 SET_MARK_OFFSET; 413 SET_MARK_OFFSET;
475#endif 414#endif
476 415
477 New(54,PL_scopestack,16,I32); 416 New(54,PL_scopestack,32,I32);
478 PL_scopestack_ix = 0; 417 PL_scopestack_ix = 0;
479 PL_scopestack_max = 16; 418 PL_scopestack_max = 32;
480 419
481 New(54,PL_savestack,96,ANY); 420 New(54,PL_savestack,64,ANY);
482 PL_savestack_ix = 0; 421 PL_savestack_ix = 0;
483 PL_savestack_max = 96; 422 PL_savestack_max = 64;
484 423
485#if !PERL_VERSION_ATLEAST (5,9,0) 424#if !PERL_VERSION_ATLEAST (5,9,0)
486 New(54,PL_retstack,8,OP*); 425 New(54,PL_retstack,16,OP*);
487 PL_retstack_ix = 0; 426 PL_retstack_ix = 0;
488 PL_retstack_max = 8; 427 PL_retstack_max = 16;
489#endif 428#endif
490} 429}
491 430
492/* 431/*
493 * destroy the stacks, the callchain etc... 432 * destroy the stacks, the callchain etc...
494 */ 433 */
495static void 434static void
496coro_destroy_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);
567 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
568 507
569 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
570 509
571 PUSHMARK (SP); 510 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
573 PUTBACK; 512 PUTBACK;
574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
575 SPAGAIN; 514 SPAGAIN;
576 515
577 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
587 coro_mortal = 0; 526 coro_mortal = 0;
588 } 527 }
589} 528}
590 529
591static void NOINLINE 530static void NOINLINE
592prepare_cctx (coro_stack *cctx) 531prepare_cctx (coro_cctx *cctx)
593{ 532{
594 dSP; 533 dSP;
595 LOGOP myop; 534 LOGOP myop;
596 535
597 Zero (&myop, 1, LOGOP); 536 Zero (&myop, 1, LOGOP);
598 myop.op_next = PL_op; 537 myop.op_next = PL_op;
599 myop.op_flags = OPf_WANT_VOID; 538 myop.op_flags = OPf_WANT_VOID;
600 539
601 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
602 541
603 PUSHMARK (SP); 542 PUSHMARK (SP);
604 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
605 PUTBACK; 544 PUTBACK;
606 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
607 SPAGAIN; 546 SPAGAIN;
608} 547}
609 548
615 554
616 /* 555 /*
617 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
618 */ 557 */
619 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
559
620 /* inject call to cctx_init */ 560 /* inject call to cctx_init */
621 prepare_cctx ((coro_stack *)arg); 561 prepare_cctx ((coro_cctx *)arg);
622 562
623 /* somebody will hit me for both perl_run and PL_restartop */ 563 /* somebody will hit me for both perl_run and PL_restartop */
624 perl_run (PL_curinterp); 564 perl_run (PL_curinterp);
625 565
626 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);
627 abort (); 567 abort ();
628} 568}
629 569
630static coro_stack * 570static coro_cctx *
631stack_new () 571cctx_new ()
632{ 572{
633 coro_stack *stack; 573 coro_cctx *cctx;
634 574
575 ++cctx_count;
576
635 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
636 578
637#if HAVE_MMAP 579#if HAVE_MMAP
638 580
639 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
640 /* mmap suppsedly does allocate-on-write for us */ 582 /* mmap suppsedly does allocate-on-write for us */
641 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);
642 584
643 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
644 { 586 {
645 perror ("FATAL: unable to mmap stack for coroutine"); 587 perror ("FATAL: unable to mmap stack for coroutine");
646 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
647 } 589 }
648 590
649# if STACKGUARD 591# if STACKGUARD
650 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
651# endif 593# endif
652 594
653#else 595#else
654 596
655 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
656 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
657 599
658 if (!stack->sptr) 600 if (!cctx->sptr)
659 { 601 {
660 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 602 perror ("FATAL: unable to malloc stack for coroutine");
661 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
662 } 604 }
663 605
664#endif 606#endif
665 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
666 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);
667 616
668 return stack; 617 return cctx;
669} 618}
670 619
671static void 620static void
672stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
673{ 622{
674 if (!stack) 623 if (!cctx)
675 return; 624 return;
676 625
626 --cctx_count;
627
628#if USE_VALGRIND
629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
630#endif
631
677#if HAVE_MMAP 632#if HAVE_MMAP
678 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
679#else 634#else
680 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
681#endif 636#endif
682 637
683 Safefree (stack); 638 Safefree (cctx);
684} 639}
685 640
686static coro_stack *stack_first;
687static int cctx_count, cctx_idle;
688
689static coro_stack * 641static coro_cctx *
690stack_get () 642cctx_get ()
691{ 643{
692 coro_stack *stack; 644 coro_cctx *cctx;
693 645
694 if (stack_first) 646 if (cctx_first)
695 { 647 {
648 cctx = cctx_first;
649 cctx_first = cctx->next;
696 --cctx_idle; 650 --cctx_idle;
697 stack = stack_first;
698 stack_first = stack->next;
699 } 651 }
700 else 652 else
701 { 653 {
702 ++cctx_count; 654 cctx = cctx_new ();
703 stack = stack_new ();
704 PL_op = PL_op->op_next; 655 PL_op = PL_op->op_next;
705 } 656 }
706 657
707 return stack; 658 return cctx;
708} 659}
709 660
710static void 661static void
711stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
712{ 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
713 ++cctx_idle; 675 ++cctx_idle;
714 stack->next = stack_first; 676 cctx->next = cctx_first;
715 stack_first = stack; 677 cctx_first = cctx;
716} 678}
717 679
718/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
719static void NOINLINE 681static void NOINLINE
720transfer (struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
721{ 683{
722 dSTACKLEVEL; 684 dSTACKLEVEL;
723 685
724 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
725 if (flags == TRANSFER_SET_STACKLEVEL) 687 if (flags == TRANSFER_SET_STACKLEVEL)
726 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
727 else if (prev != next) 689 else if (prev != next)
728 { 690 {
729 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;
730 709
731 LOCK; 710 LOCK;
732 711
733 if (next->mainstack) 712 if (next->mainstack)
734 { 713 {
741 /* need to start coroutine */ 720 /* need to start coroutine */
742 /* first get rid of the old state */ 721 /* first get rid of the old state */
743 SAVE (prev, -1); 722 SAVE (prev, -1);
744 /* setup coroutine call */ 723 /* setup coroutine call */
745 setup_coro (next); 724 setup_coro (next);
746 /* need a stack */ 725 /* need a new stack */
747 next->stack = 0; 726 assert (!next->stack);
748 } 727 }
749 728
750 if (!prev->stack)
751 /* create a new empty context */
752 Newz (0, prev->stack, 1, coro_stack);
753
754 prev__stack = prev->stack; 729 prev__cctx = prev->cctx;
755 730
756 /* possibly "free" the stack */ 731 /* possibly "free" the cctx */
757 if (prev__stack->idle_sp == STACKLEVEL) 732 if (prev__cctx->idle_sp == STACKLEVEL)
758 { 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
759 stack_put (prev__stack); 737 cctx_put (prev__cctx);
760 prev->stack = 0; 738 prev->cctx = 0;
761 } 739 }
762 740
763 if (!next->stack) 741 if (!next->cctx)
764 next->stack = stack_get (); 742 next->cctx = cctx_get ();
765 743
766 if (prev__stack != next->stack) 744 if (prev__cctx != next->cctx)
767 { 745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
768 prev__stack->top_env = PL_top_env; 752 prev__cctx->top_env = PL_top_env;
769 PL_top_env = next->stack->top_env; 753 PL_top_env = next->cctx->top_env;
770 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
771 } 755 }
772 756
773 free_coro_mortal (); 757 free_coro_mortal ();
774 758
775 UNLOCK; 759 UNLOCK;
788coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
789{ 773{
790 if (coro->refcnt--) 774 if (coro->refcnt--)
791 return; 775 return;
792 776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
779
793 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
794 { 781 {
795 struct coro temp; 782 struct coro temp;
796 783
797 SAVE ((&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
802 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
803 790
804 coro->mainstack = 0; 791 coro->mainstack = 0;
805 } 792 }
806 793
807 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
808 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
809 Safefree (coro); 796 Safefree (coro);
810} 797}
811 798
812static int 799static int
894static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
895static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
896static int coro_nready; 883static int coro_nready;
897 884
898static void 885static void
899coro_enq (SV *sv) 886coro_enq (SV *coro_sv)
900{ 887{
901 int prio;
902
903 if (SvTYPE (sv) != SVt_PVHV)
904 croak ("Coro::ready tried to enqueue something that is not a coroutine");
905
906 prio = SvSTATE (sv)->prio;
907
908 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
909 coro_nready++; 889 coro_nready++;
910} 890}
911 891
912static SV * 892static SV *
913coro_deq (int min_prio) 893coro_deq (int min_prio)
926 } 906 }
927 907
928 return 0; 908 return 0;
929} 909}
930 910
931static void 911static int
932api_ready (SV *coro) 912api_ready (SV *coro_sv)
933{ 913{
934 dTHX; 914 struct coro *coro;
935 915
936 if (SvROK (coro)) 916 if (SvROK (coro_sv))
937 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;
938 928
939 LOCK; 929 LOCK;
940 coro_enq (SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
941 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;
942} 940}
943 941
944static void 942static void
945prepare_schedule (struct transfer_args *ta) 943prepare_schedule (struct transfer_args *ta)
946{ 944{
982 coro_mortal = prev; 980 coro_mortal = prev;
983 981
984 ta->prev = SvSTATE (prev); 982 ta->prev = SvSTATE (prev);
985 ta->next = SvSTATE (next); 983 ta->next = SvSTATE (next);
986 ta->flags = TRANSFER_SAVE_ALL; 984 ta->flags = TRANSFER_SAVE_ALL;
985
986 ta->next->flags &= ~CF_READY;
987} 987}
988 988
989static void 989static void
990prepare_cede (struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
991{ 991{
992 LOCK; 992 api_ready (GvSV (coro_current));
993 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
994 UNLOCK;
995 993
996 prepare_schedule (ta); 994 prepare_schedule (ta);
997} 995}
998 996
999static void 997static void
1056 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;
1057 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1058 1056
1059 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
1060 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
1061
1062 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1063 /*coro->stack = 0;*/
1064} 1059}
1065 OUTPUT: 1060 OUTPUT:
1066 RETVAL 1061 RETVAL
1067 1062
1068void 1063void
1077 struct transfer_args ta; 1072 struct transfer_args ta;
1078 1073
1079 switch (ix) 1074 switch (ix)
1080 { 1075 {
1081 case 0: 1076 case 0:
1082 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1083 ta.next = 0; 1078 ta.next = 0;
1084 ta.flags = TRANSFER_SET_STACKLEVEL; 1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1085 break; 1080 break;
1086 1081
1087 case 1: 1082 case 1:
1147 int code 1142 int code
1148 PROTOTYPE: $ 1143 PROTOTYPE: $
1149 CODE: 1144 CODE:
1150 _exit (code); 1145 _exit (code);
1151 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
1152MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1153 1162
1154BOOT: 1163BOOT:
1155{ 1164{
1156 int i; 1165 int i;
1174 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1175 1184
1176 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1177 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1178 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1179 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1180 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1181 1191
1182 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1183 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1203 1213
1204 coro->prio = newprio; 1214 coro->prio = newprio;
1205 } 1215 }
1206} 1216}
1207 1217
1208void 1218SV *
1209ready (SV *self) 1219ready (SV *self)
1210 PROTOTYPE: $ 1220 PROTOTYPE: $
1211 CODE: 1221 CODE:
1212 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
1213 1233
1214int 1234int
1215nready (...) 1235nready (...)
1216 PROTOTYPE: 1236 PROTOTYPE:
1217 CODE: 1237 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines