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.102 by root, Mon Nov 27 01:28:03 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);
546 * emulate part of the perl startup here. 485 * emulate part of the perl startup here.
547 */ 486 */
548 487
549 coro_init_stacks (); 488 coro_init_stacks ();
550 489
490 PL_curcop = 0;
491 PL_in_eval = 0;
492 PL_curpm = 0;
493
551 { 494 {
552 dSP; 495 dSP;
553 LOGOP myop; 496 LOGOP myop;
554 497
555 /*PL_curcop = 0;*/ 498 /* I have no idea why this is needed, but it is */
556 PL_in_eval = 0; 499 PUSHMARK (SP);
500
557 SvREFCNT_dec (GvAV (PL_defgv)); 501 SvREFCNT_dec (GvAV (PL_defgv));
558 GvAV (PL_defgv) = coro->args; coro->args = 0; 502 GvAV (PL_defgv) = coro->args; coro->args = 0;
559
560 SPAGAIN;
561 503
562 Zero (&myop, 1, LOGOP); 504 Zero (&myop, 1, LOGOP);
563 myop.op_next = Nullop; 505 myop.op_next = Nullop;
564 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
565 507
566 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
567 509
568 PUSHMARK (SP); 510 PUSHMARK (SP);
569 PUSHMARK (SP);
570 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
571 PUTBACK; 512 PUTBACK;
572 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
573 SPAGAIN; 514 SPAGAIN;
574 515
575 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
585 coro_mortal = 0; 526 coro_mortal = 0;
586 } 527 }
587} 528}
588 529
589static void NOINLINE 530static void NOINLINE
590prepare_cctx (coro_stack *cctx) 531prepare_cctx (coro_cctx *cctx)
591{ 532{
592 dSP; 533 dSP;
593 LOGOP myop; 534 LOGOP myop;
594 535
595 Zero (&myop, 1, LOGOP); 536 Zero (&myop, 1, LOGOP);
596 myop.op_next = PL_op; 537 myop.op_next = PL_op;
597 myop.op_flags = OPf_WANT_VOID; 538 myop.op_flags = OPf_WANT_VOID;
598 539
599 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
600 541
601 PUSHMARK (SP); 542 PUSHMARK (SP);
602 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
603 PUTBACK; 544 PUTBACK;
604 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
605 SPAGAIN; 546 SPAGAIN;
606} 547}
607 548
613 554
614 /* 555 /*
615 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
616 */ 557 */
617 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
559
618 /* inject call to cctx_init */ 560 /* inject call to cctx_init */
619 prepare_cctx ((coro_stack *)arg); 561 prepare_cctx ((coro_cctx *)arg);
620 562
621 /* somebody will hit me for both perl_run and PL_restartop */ 563 /* somebody will hit me for both perl_run and PL_restartop */
622 perl_run (PL_curinterp); 564 perl_run (PL_curinterp);
623 565
624 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);
625 abort (); 567 abort ();
626} 568}
627 569
628static coro_stack * 570static coro_cctx *
629stack_new () 571cctx_new ()
630{ 572{
631 coro_stack *stack; 573 coro_cctx *cctx;
632 574
575 ++cctx_count;
576
633 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
634 578
635#if HAVE_MMAP 579#if HAVE_MMAP
636 580
637 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
638 /* mmap suppsedly does allocate-on-write for us */ 582 /* mmap suppsedly does allocate-on-write for us */
639 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);
640 584
641 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
642 { 586 {
643 perror ("FATAL: unable to mmap stack for coroutine"); 587 perror ("FATAL: unable to mmap stack for coroutine");
644 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
645 } 589 }
646 590
647# if STACKGUARD 591# if STACKGUARD
648 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
649# endif 593# endif
650 594
651#else 595#else
652 596
653 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
654 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
655 599
656 if (!stack->sptr) 600 if (!cctx->sptr)
657 { 601 {
658 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 602 perror ("FATAL: unable to malloc stack for coroutine");
659 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
660 } 604 }
661 605
662#endif 606#endif
663 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
664 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);
665 616
666 return stack; 617 return cctx;
667} 618}
668 619
669static void 620static void
670stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
671{ 622{
672 if (!stack) 623 if (!cctx)
673 return; 624 return;
674 625
626 --cctx_count;
627
628#if USE_VALGRIND
629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
630#endif
631
675#if HAVE_MMAP 632#if HAVE_MMAP
676 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
677#else 634#else
678 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
679#endif 636#endif
680 637
681 Safefree (stack); 638 Safefree (cctx);
682} 639}
683 640
684static coro_stack *stack_first;
685static int cctx_count, cctx_idle;
686
687static coro_stack * 641static coro_cctx *
688stack_get () 642cctx_get ()
689{ 643{
690 coro_stack *stack; 644 coro_cctx *cctx;
691 645
692 if (stack_first) 646 if (cctx_first)
693 { 647 {
648 cctx = cctx_first;
649 cctx_first = cctx->next;
694 --cctx_idle; 650 --cctx_idle;
695 stack = stack_first;
696 stack_first = stack->next;
697 } 651 }
698 else 652 else
699 { 653 {
700 ++cctx_count; 654 cctx = cctx_new ();
701 stack = stack_new ();
702 PL_op = PL_op->op_next; 655 PL_op = PL_op->op_next;
703 } 656 }
704 657
705 return stack; 658 return cctx;
706} 659}
707 660
708static void 661static void
709stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
710{ 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
711 ++cctx_idle; 675 ++cctx_idle;
712 stack->next = stack_first; 676 cctx->next = cctx_first;
713 stack_first = stack; 677 cctx_first = cctx;
714} 678}
715 679
716/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
717static void NOINLINE 681static void NOINLINE
718transfer (struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
719{ 683{
720 dSTACKLEVEL; 684 dSTACKLEVEL;
721 685
722 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
723 if (flags == TRANSFER_SET_STACKLEVEL) 687 if (flags == TRANSFER_SET_STACKLEVEL)
724 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
725 else if (prev != next) 689 else if (prev != next)
726 { 690 {
727 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;
728 709
729 LOCK; 710 LOCK;
730 711
731 if (next->mainstack) 712 if (next->mainstack)
732 { 713 {
739 /* need to start coroutine */ 720 /* need to start coroutine */
740 /* first get rid of the old state */ 721 /* first get rid of the old state */
741 SAVE (prev, -1); 722 SAVE (prev, -1);
742 /* setup coroutine call */ 723 /* setup coroutine call */
743 setup_coro (next); 724 setup_coro (next);
744 /* need a stack */ 725 /* need a new stack */
745 next->stack = 0; 726 assert (!next->stack);
746 } 727 }
747 728
748 if (!prev->stack)
749 /* create a new empty context */
750 Newz (0, prev->stack, 1, coro_stack);
751
752 prev__stack = prev->stack; 729 prev__cctx = prev->cctx;
753 730
754 /* possibly "free" the stack */ 731 /* possibly "free" the cctx */
755 if (prev__stack->idle_sp == STACKLEVEL) 732 if (prev__cctx->idle_sp == STACKLEVEL)
756 { 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
757 stack_put (prev__stack); 737 cctx_put (prev__cctx);
758 prev->stack = 0; 738 prev->cctx = 0;
759 } 739 }
760 740
761 if (!next->stack) 741 if (!next->cctx)
762 next->stack = stack_get (); 742 next->cctx = cctx_get ();
763 743
764 if (prev__stack != next->stack) 744 if (prev__cctx != next->cctx)
765 { 745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
766 prev__stack->top_env = PL_top_env; 752 prev__cctx->top_env = PL_top_env;
767 PL_top_env = next->stack->top_env; 753 PL_top_env = next->cctx->top_env;
768 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
769 } 755 }
770 756
771 free_coro_mortal (); 757 free_coro_mortal ();
772 758
773 UNLOCK; 759 UNLOCK;
786coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
787{ 773{
788 if (coro->refcnt--) 774 if (coro->refcnt--)
789 return; 775 return;
790 776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
779
791 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
792 { 781 {
793 struct coro temp; 782 struct coro temp;
794 783
795 SAVE ((&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
800 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
801 790
802 coro->mainstack = 0; 791 coro->mainstack = 0;
803 } 792 }
804 793
805 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
806 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
807 Safefree (coro); 796 Safefree (coro);
808} 797}
809 798
810static int 799static int
892static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
893static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
894static int coro_nready; 883static int coro_nready;
895 884
896static void 885static void
897coro_enq (SV *sv) 886coro_enq (SV *coro_sv)
898{ 887{
899 int prio;
900
901 if (SvTYPE (sv) != SVt_PVHV)
902 croak ("Coro::ready tried to enqueue something that is not a coroutine");
903
904 prio = SvSTATE (sv)->prio;
905
906 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
907 coro_nready++; 889 coro_nready++;
908} 890}
909 891
910static SV * 892static SV *
911coro_deq (int min_prio) 893coro_deq (int min_prio)
924 } 906 }
925 907
926 return 0; 908 return 0;
927} 909}
928 910
929static void 911static int
930api_ready (SV *coro) 912api_ready (SV *coro_sv)
931{ 913{
932 dTHX; 914 struct coro *coro;
933 915
934 if (SvROK (coro)) 916 if (SvROK (coro_sv))
935 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;
936 928
937 LOCK; 929 LOCK;
938 coro_enq (SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
939 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;
940} 940}
941 941
942static void 942static void
943prepare_schedule (struct transfer_args *ta) 943prepare_schedule (struct transfer_args *ta)
944{ 944{
980 coro_mortal = prev; 980 coro_mortal = prev;
981 981
982 ta->prev = SvSTATE (prev); 982 ta->prev = SvSTATE (prev);
983 ta->next = SvSTATE (next); 983 ta->next = SvSTATE (next);
984 ta->flags = TRANSFER_SAVE_ALL; 984 ta->flags = TRANSFER_SAVE_ALL;
985
986 ta->next->flags &= ~CF_READY;
985} 987}
986 988
987static void 989static void
988prepare_cede (struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
989{ 991{
990 LOCK; 992 api_ready (GvSV (coro_current));
991 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
992 UNLOCK;
993 993
994 prepare_schedule (ta); 994 prepare_schedule (ta);
995} 995}
996 996
997static void 997static void
1054 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;
1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1056 1056
1057 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
1058 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
1059
1060 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1061 /*coro->stack = 0;*/
1062} 1059}
1063 OUTPUT: 1060 OUTPUT:
1064 RETVAL 1061 RETVAL
1065 1062
1066void 1063void
1075 struct transfer_args ta; 1072 struct transfer_args ta;
1076 1073
1077 switch (ix) 1074 switch (ix)
1078 { 1075 {
1079 case 0: 1076 case 0:
1080 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1081 ta.next = 0; 1078 ta.next = 0;
1082 ta.flags = TRANSFER_SET_STACKLEVEL; 1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1083 break; 1080 break;
1084 1081
1085 case 1: 1082 case 1:
1145 int code 1142 int code
1146 PROTOTYPE: $ 1143 PROTOTYPE: $
1147 CODE: 1144 CODE:
1148 _exit (code); 1145 _exit (code);
1149 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
1150MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1151 1162
1152BOOT: 1163BOOT:
1153{ 1164{
1154 int i; 1165 int i;
1172 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1173 1184
1174 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1175 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1176 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1177 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1178 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1179 1191
1180 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1181 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1201 1213
1202 coro->prio = newprio; 1214 coro->prio = newprio;
1203 } 1215 }
1204} 1216}
1205 1217
1206void 1218SV *
1207ready (SV *self) 1219ready (SV *self)
1208 PROTOTYPE: $ 1220 PROTOTYPE: $
1209 CODE: 1221 CODE:
1210 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
1211 1233
1212int 1234int
1213nready (...) 1235nready (...)
1214 PROTOTYPE: 1236 PROTOTYPE:
1215 CODE: 1237 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines