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.106 by root, Mon Nov 27 02:08:55 2006 UTC vs.
Revision 1.117 by root, Fri Dec 1 19:41:06 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) \
103static struct CoroAPI coroapi; 106static struct CoroAPI coroapi;
104static AV *main_mainstack; /* used to differentiate between $main and others */ 107static AV *main_mainstack; /* used to differentiate between $main and others */
105static HV *coro_state_stash, *coro_stash; 108static HV *coro_state_stash, *coro_stash;
106static SV *coro_mortal; /* will be freed after next transfer */ 109static SV *coro_mortal; /* will be freed after next transfer */
107 110
111static struct coro_cctx *cctx_first;
112static int cctx_count, cctx_idle;
113
108/* this is a structure representing a c-level coroutine */ 114/* this is a structure representing a c-level coroutine */
109typedef struct coro_cctx { 115typedef struct coro_cctx {
110 struct coro_cctx *next; 116 struct coro_cctx *next;
111 117
112 /* the stack */ 118 /* the stack */
116 /* cpu state */ 122 /* cpu state */
117 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
118 JMPENV *top_env; 124 JMPENV *top_env;
119 coro_context cctx; 125 coro_context cctx;
120 126
127 int inuse;
128
121#if USE_VALGRIND 129#if USE_VALGRIND
122 int valgrind_id; 130 int valgrind_id;
123#endif 131#endif
124} coro_cctx; 132} coro_cctx;
133
134enum {
135 CF_RUNNING = 0x0001, /* coroutine is running */
136 CF_READY = 0x0002, /* coroutine is ready */
137 CF_NEW = 0x0004, /* ahs never been switched to */
138};
125 139
126/* this is a structure representing a perl-level coroutine */ 140/* this is a structure representing a perl-level coroutine */
127struct coro { 141struct coro {
128 /* the c coroutine allocated to this perl coroutine, if any */ 142 /* the c coroutine allocated to this perl coroutine, if any */
129 coro_cctx *cctx; 143 coro_cctx *cctx;
130 144
131 /* data associated with this coroutine (initial args) */ 145 /* data associated with this coroutine (initial args) */
132 AV *args; 146 AV *args;
133 int refcnt; 147 int refcnt;
148 int flags;
134 149
135 /* optionally saved, might be zero */ 150 /* optionally saved, might be zero */
136 AV *defav; 151 AV *defav;
137 SV *defsv; 152 SV *defsv;
138 SV *errsv; 153 SV *errsv;
139 154
140 /* saved global state not related to stacks */ 155#define VAR(name,type) type name;
141 U8 dowarn; 156# include "state.h"
142 I32 in_eval; 157#undef VAR
143
144 /* the stacks and related info (callchain etc..) */
145 PERL_SI *curstackinfo;
146 AV *curstack;
147 AV *mainstack;
148 SV **stack_sp;
149 OP *op;
150 SV **curpad;
151 AV *comppad;
152 CV *compcv;
153 SV **stack_base;
154 SV **stack_max;
155 SV **tmps_stack;
156 I32 tmps_floor;
157 I32 tmps_ix;
158 I32 tmps_max;
159 I32 *markstack;
160 I32 *markstack_ptr;
161 I32 *markstack_max;
162 I32 *scopestack;
163 I32 scopestack_ix;
164 I32 scopestack_max;
165 ANY *savestack;
166 I32 savestack_ix;
167 I32 savestack_max;
168 OP **retstack;
169 I32 retstack_ix;
170 I32 retstack_max;
171 PMOP *curpm;
172 COP *curcop;
173 158
174 /* coro process data */ 159 /* coro process data */
175 int prio; 160 int prio;
176}; 161};
177 162
241 226
242#define PERL_MAGIC_coro PERL_MAGIC_ext 227#define PERL_MAGIC_coro PERL_MAGIC_ext
243 228
244static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 229static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
245 230
231#define CORO_MAGIC(cv) \
232 SvMAGIC (cv) \
233 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
234 ? SvMAGIC (cv) \
235 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
236 : 0
237
246/* the next two functions merely cache the padlists */ 238/* the next two functions merely cache the padlists */
247static void 239static void
248get_padlist (CV *cv) 240get_padlist (CV *cv)
249{ 241{
250 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 242 MAGIC *mg = CORO_MAGIC (cv);
243 AV *av;
251 244
252 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 245 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
253 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 246 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
254 else 247 else
255 { 248 {
256#if 0 249#if 0
257 /* this is probably cleaner, but also slower? */ 250 /* this is probably cleaner, but also slower? */
258 CV *cp = Perl_cv_clone (cv); 251 CV *cp = Perl_cv_clone (cv);
266} 259}
267 260
268static void 261static void
269put_padlist (CV *cv) 262put_padlist (CV *cv)
270{ 263{
271 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 264 MAGIC *mg = CORO_MAGIC (cv);
265 AV *av;
272 266
273 if (!mg) 267 if (!mg)
274 { 268 {
275 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 269 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
276 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 270 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
277 mg->mg_virtual = &vtbl_coro; 271 mg->mg_virtual = &vtbl_coro;
278 mg->mg_obj = (SV *)newAV (); 272 mg->mg_obj = (SV *)newAV ();
279 } 273 }
280 274
281 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 275 av = (AV *)mg->mg_obj;
276
277 if (AvFILLp (av) >= AvMAX (av))
278 av_extend (av, AvMAX (av) + 1);
279
280 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
282} 281}
283 282
284#define SB do { 283#define SB do {
285#define SE } while (0) 284#define SE } while (0)
286 285
287#define LOAD(state) load_state((state)); 286#define LOAD(state) load_state ((state))
288#define SAVE(state,flags) save_state((state),(flags)); 287#define SAVE(state,flags) save_state ((state),(flags))
289 288
290#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 289#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
291 290
292static void 291static void
293load_state(Coro__State c) 292load_state (Coro__State c)
294{ 293{
295 PL_dowarn = c->dowarn; 294#define VAR(name,type) PL_ ## name = c->name;
296 PL_in_eval = c->in_eval; 295# include "state.h"
297 296#undef VAR
298 PL_curstackinfo = c->curstackinfo;
299 PL_curstack = c->curstack;
300 PL_mainstack = c->mainstack;
301 PL_stack_sp = c->stack_sp;
302 PL_op = c->op;
303 PL_curpad = c->curpad;
304 PL_comppad = c->comppad;
305 PL_compcv = c->compcv;
306 PL_stack_base = c->stack_base;
307 PL_stack_max = c->stack_max;
308 PL_tmps_stack = c->tmps_stack;
309 PL_tmps_floor = c->tmps_floor;
310 PL_tmps_ix = c->tmps_ix;
311 PL_tmps_max = c->tmps_max;
312 PL_markstack = c->markstack;
313 PL_markstack_ptr = c->markstack_ptr;
314 PL_markstack_max = c->markstack_max;
315 PL_scopestack = c->scopestack;
316 PL_scopestack_ix = c->scopestack_ix;
317 PL_scopestack_max = c->scopestack_max;
318 PL_savestack = c->savestack;
319 PL_savestack_ix = c->savestack_ix;
320 PL_savestack_max = c->savestack_max;
321#if !PERL_VERSION_ATLEAST (5,9,0)
322 PL_retstack = c->retstack;
323 PL_retstack_ix = c->retstack_ix;
324 PL_retstack_max = c->retstack_max;
325#endif
326 PL_curpm = c->curpm;
327 PL_curcop = c->curcop;
328 297
329 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 298 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
330 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 299 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
331 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 300 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
332 301
335 CV *cv; 304 CV *cv;
336 305
337 /* now do the ugly restore mess */ 306 /* now do the ugly restore mess */
338 while ((cv = (CV *)POPs)) 307 while ((cv = (CV *)POPs))
339 { 308 {
340 AV *padlist = (AV *)POPs;
341
342 if (padlist)
343 {
344 put_padlist (cv); /* mark this padlist as available */ 309 put_padlist (cv); /* mark this padlist as available */
345 CvPADLIST(cv) = padlist; 310 CvDEPTH (cv) = PTR2IV (POPs);
346 } 311 CvPADLIST (cv) = (AV *)POPs;
347
348 ++CvDEPTH(cv);
349 } 312 }
350 313
351 PUTBACK; 314 PUTBACK;
352 } 315 }
353} 316}
354 317
355static void 318static void
356save_state(Coro__State c, int flags) 319save_state (Coro__State c, int flags)
357{ 320{
358 { 321 {
359 dSP; 322 dSP;
360 I32 cxix = cxstack_ix; 323 I32 cxix = cxstack_ix;
361 PERL_CONTEXT *ccstk = cxstack; 324 PERL_CONTEXT *ccstk = cxstack;
372 { 335 {
373 while (cxix >= 0) 336 while (cxix >= 0)
374 { 337 {
375 PERL_CONTEXT *cx = &ccstk[cxix--]; 338 PERL_CONTEXT *cx = &ccstk[cxix--];
376 339
377 if (CxTYPE(cx) == CXt_SUB) 340 if (CxTYPE (cx) == CXt_SUB)
378 { 341 {
379 CV *cv = cx->blk_sub.cv; 342 CV *cv = cx->blk_sub.cv;
343
380 if (CvDEPTH(cv)) 344 if (CvDEPTH (cv))
381 { 345 {
382 EXTEND (SP, CvDEPTH(cv)*2); 346 EXTEND (SP, 3);
383 347
384 while (--CvDEPTH(cv))
385 {
386 /* this tells the restore code to increment CvDEPTH */
387 PUSHs (Nullsv);
388 PUSHs ((SV *)cv);
389 }
390
391 PUSHs ((SV *)CvPADLIST(cv)); 348 PUSHs ((SV *)CvPADLIST (cv));
349 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
392 PUSHs ((SV *)cv); 350 PUSHs ((SV *)cv);
393 351
352 CvDEPTH (cv) = 0;
394 get_padlist (cv); 353 get_padlist (cv);
395 } 354 }
396 } 355 }
397#ifdef CXt_FORMAT 356#ifdef CXt_FORMAT
398 else if (CxTYPE(cx) == CXt_FORMAT) 357 else if (CxTYPE (cx) == CXt_FORMAT)
399 { 358 {
400 /* I never used formats, so how should I know how these are implemented? */ 359 /* I never used formats, so how should I know how these are implemented? */
401 /* my bold guess is as a simple, plain sub... */ 360 /* my bold guess is as a simple, plain sub... */
402 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 361 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
403 } 362 }
417 376
418 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 377 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
419 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 378 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
420 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 379 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
421 380
422 c->dowarn = PL_dowarn; 381#define VAR(name,type)c->name = PL_ ## name;
423 c->in_eval = PL_in_eval; 382# include "state.h"
424 383#undef VAR
425 c->curstackinfo = PL_curstackinfo;
426 c->curstack = PL_curstack;
427 c->mainstack = PL_mainstack;
428 c->stack_sp = PL_stack_sp;
429 c->op = PL_op;
430 c->curpad = PL_curpad;
431 c->comppad = PL_comppad;
432 c->compcv = PL_compcv;
433 c->stack_base = PL_stack_base;
434 c->stack_max = PL_stack_max;
435 c->tmps_stack = PL_tmps_stack;
436 c->tmps_floor = PL_tmps_floor;
437 c->tmps_ix = PL_tmps_ix;
438 c->tmps_max = PL_tmps_max;
439 c->markstack = PL_markstack;
440 c->markstack_ptr = PL_markstack_ptr;
441 c->markstack_max = PL_markstack_max;
442 c->scopestack = PL_scopestack;
443 c->scopestack_ix = PL_scopestack_ix;
444 c->scopestack_max = PL_scopestack_max;
445 c->savestack = PL_savestack;
446 c->savestack_ix = PL_savestack_ix;
447 c->savestack_max = PL_savestack_max;
448#if !PERL_VERSION_ATLEAST (5,9,0)
449 c->retstack = PL_retstack;
450 c->retstack_ix = PL_retstack_ix;
451 c->retstack_max = PL_retstack_max;
452#endif
453 c->curpm = PL_curpm;
454 c->curcop = PL_curcop;
455} 384}
456 385
457/* 386/*
458 * allocate various perl stacks. This is an exact copy 387 * allocate various perl stacks. This is an exact copy
459 * of perl.c:init_stacks, except that it uses less memory 388 * of perl.c:init_stacks, except that it uses less memory
461 * not usually need a lot of stackspace. 390 * not usually need a lot of stackspace.
462 */ 391 */
463static void 392static void
464coro_init_stacks () 393coro_init_stacks ()
465{ 394{
466 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 395 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
467 PL_curstackinfo->si_type = PERLSI_MAIN; 396 PL_curstackinfo->si_type = PERLSI_MAIN;
468 PL_curstack = PL_curstackinfo->si_stack; 397 PL_curstack = PL_curstackinfo->si_stack;
469 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 398 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
470 399
471 PL_stack_base = AvARRAY(PL_curstack); 400 PL_stack_base = AvARRAY(PL_curstack);
472 PL_stack_sp = PL_stack_base; 401 PL_stack_sp = PL_stack_base;
473 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 402 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
474 403
475 New(50,PL_tmps_stack,96,SV*); 404 New(50,PL_tmps_stack,128,SV*);
476 PL_tmps_floor = -1; 405 PL_tmps_floor = -1;
477 PL_tmps_ix = -1; 406 PL_tmps_ix = -1;
478 PL_tmps_max = 96; 407 PL_tmps_max = 128;
479 408
480 New(54,PL_markstack,16,I32); 409 New(54,PL_markstack,32,I32);
481 PL_markstack_ptr = PL_markstack; 410 PL_markstack_ptr = PL_markstack;
482 PL_markstack_max = PL_markstack + 16; 411 PL_markstack_max = PL_markstack + 32;
483 412
484#ifdef SET_MARK_OFFSET 413#ifdef SET_MARK_OFFSET
485 SET_MARK_OFFSET; 414 SET_MARK_OFFSET;
486#endif 415#endif
487 416
488 New(54,PL_scopestack,16,I32); 417 New(54,PL_scopestack,32,I32);
489 PL_scopestack_ix = 0; 418 PL_scopestack_ix = 0;
490 PL_scopestack_max = 16; 419 PL_scopestack_max = 32;
491 420
492 New(54,PL_savestack,96,ANY); 421 New(54,PL_savestack,64,ANY);
493 PL_savestack_ix = 0; 422 PL_savestack_ix = 0;
494 PL_savestack_max = 96; 423 PL_savestack_max = 64;
495 424
496#if !PERL_VERSION_ATLEAST (5,9,0) 425#if !PERL_VERSION_ATLEAST (5,9,0)
497 New(54,PL_retstack,8,OP*); 426 New(54,PL_retstack,16,OP*);
498 PL_retstack_ix = 0; 427 PL_retstack_ix = 0;
499 PL_retstack_max = 8; 428 PL_retstack_max = 16;
500#endif 429#endif
501} 430}
502 431
503/* 432/*
504 * destroy the stacks, the callchain etc... 433 * destroy the stacks, the callchain etc...
557 * emulate part of the perl startup here. 486 * emulate part of the perl startup here.
558 */ 487 */
559 488
560 coro_init_stacks (); 489 coro_init_stacks ();
561 490
562 PL_curcop = 0; 491 PL_curcop = &PL_compiling;
563 PL_in_eval = 0; 492 PL_in_eval = EVAL_NULL;
564 PL_curpm = 0; 493 PL_curpm = 0;
494 PL_localizing = 0;
495 PL_dirty = 0;
496 PL_restartop = 0;
565 497
566 { 498 {
567 dSP; 499 dSP;
568 LOGOP myop; 500 LOGOP myop;
569 501
578 myop.op_flags = OPf_WANT_VOID; 510 myop.op_flags = OPf_WANT_VOID;
579 511
580 PL_op = (OP *)&myop; 512 PL_op = (OP *)&myop;
581 513
582 PUSHMARK (SP); 514 PUSHMARK (SP);
583 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 515 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
584 PUTBACK; 516 PUTBACK;
585 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 517 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
586 SPAGAIN; 518 SPAGAIN;
587
588 ENTER; /* necessary e.g. for dounwind */
589 } 519 }
520
521 ENTER; /* necessary e.g. for dounwind */
590} 522}
591 523
592static void 524static void
593free_coro_mortal () 525free_coro_mortal ()
594{ 526{
607 539
608 Zero (&myop, 1, LOGOP); 540 Zero (&myop, 1, LOGOP);
609 myop.op_next = PL_op; 541 myop.op_next = PL_op;
610 myop.op_flags = OPf_WANT_VOID; 542 myop.op_flags = OPf_WANT_VOID;
611 543
612 sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx)); 544 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
613 545
614 PUSHMARK (SP); 546 PUSHMARK (SP);
615 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 547 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
616 PUTBACK; 548 PUTBACK;
617 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 549 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
618 SPAGAIN; 550 SPAGAIN;
619} 551}
620 552
626 558
627 /* 559 /*
628 * this is a _very_ stripped down perl interpreter ;) 560 * this is a _very_ stripped down perl interpreter ;)
629 */ 561 */
630 PL_top_env = &PL_start_env; 562 PL_top_env = &PL_start_env;
563
631 /* inject call to cctx_init */ 564 /* inject call to cctx_init */
632 prepare_cctx ((coro_cctx *)arg); 565 prepare_cctx ((coro_cctx *)arg);
633 566
634 /* somebody will hit me for both perl_run and PL_restartop */ 567 /* somebody will hit me for both perl_run and PL_restartop */
635 perl_run (PL_curinterp); 568 perl_run (PL_curinterp);
640 573
641static coro_cctx * 574static coro_cctx *
642cctx_new () 575cctx_new ()
643{ 576{
644 coro_cctx *cctx; 577 coro_cctx *cctx;
578
579 ++cctx_count;
645 580
646 New (0, cctx, 1, coro_cctx); 581 New (0, cctx, 1, coro_cctx);
647 582
648#if HAVE_MMAP 583#if HAVE_MMAP
649 584
685 620
686 return cctx; 621 return cctx;
687} 622}
688 623
689static void 624static void
690cctx_free (coro_cctx *cctx) 625cctx_destroy (coro_cctx *cctx)
691{ 626{
692 if (!cctx) 627 if (!cctx)
693 return; 628 return;
629
630 --cctx_count;
694 631
695#if USE_VALGRIND 632#if USE_VALGRIND
696 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 633 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
697#endif 634#endif
698 635
703#endif 640#endif
704 641
705 Safefree (cctx); 642 Safefree (cctx);
706} 643}
707 644
708static coro_cctx *cctx_first;
709static int cctx_count, cctx_idle;
710
711static coro_cctx * 645static coro_cctx *
712cctx_get () 646cctx_get ()
713{ 647{
714 coro_cctx *cctx; 648 coro_cctx *cctx;
715 649
716 if (cctx_first) 650 if (cctx_first)
717 { 651 {
718 --cctx_idle;
719 cctx = cctx_first; 652 cctx = cctx_first;
720 cctx_first = cctx->next; 653 cctx_first = cctx->next;
654 --cctx_idle;
721 } 655 }
722 else 656 else
723 { 657 {
724 ++cctx_count;
725 cctx = cctx_new (); 658 cctx = cctx_new ();
726 PL_op = PL_op->op_next; 659 PL_op = PL_op->op_next;
727 } 660 }
728 661
729 return cctx; 662 return cctx;
730} 663}
731 664
732static void 665static void
733cctx_put (coro_cctx *cctx) 666cctx_put (coro_cctx *cctx)
734{ 667{
668 /* free another cctx if overlimit */
669 if (cctx_idle >= MAX_IDLE_CCTX)
670 {
671 coro_cctx *first = cctx_first;
672 cctx_first = first->next;
673 --cctx_idle;
674
675 assert (!first->inuse);
676 cctx_destroy (first);
677 }
678
735 ++cctx_idle; 679 ++cctx_idle;
736 cctx->next = cctx_first; 680 cctx->next = cctx_first;
737 cctx_first = cctx; 681 cctx_first = cctx;
738} 682}
739 683
747 if (flags == TRANSFER_SET_STACKLEVEL) 691 if (flags == TRANSFER_SET_STACKLEVEL)
748 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 692 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
749 else if (prev != next) 693 else if (prev != next)
750 { 694 {
751 coro_cctx *prev__cctx; 695 coro_cctx *prev__cctx;
696
697 if (prev->flags & CF_NEW)
698 {
699 /* create a new empty context */
700 Newz (0, prev->cctx, 1, coro_cctx);
701 prev->cctx->inuse = 1;
702 prev->flags &= ~CF_NEW;
703 prev->flags |= CF_RUNNING;
704 }
705
706 /*TODO: must not croak here */
707 if (!prev->flags & CF_RUNNING)
708 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
709
710 if (next->flags & CF_RUNNING)
711 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
712
713 prev->flags &= ~CF_RUNNING;
714 next->flags |= CF_RUNNING;
752 715
753 LOCK; 716 LOCK;
754 717
755 if (next->mainstack) 718 if (next->mainstack)
756 { 719 {
759 LOAD (next); 722 LOAD (next);
760 } 723 }
761 else 724 else
762 { 725 {
763 /* need to start coroutine */ 726 /* need to start coroutine */
727 assert (next->flags & CF_NEW);
728 next->flags &= ~CF_NEW;
764 /* first get rid of the old state */ 729 /* first get rid of the old state */
765 SAVE (prev, -1); 730 SAVE (prev, -1);
766 /* setup coroutine call */ 731 /* setup coroutine call */
767 setup_coro (next); 732 setup_coro (next);
768 /* need a stack */ 733 /* need a new stack */
769 next->cctx = 0; 734 assert (!next->stack);
770 } 735 }
771 736
772 if (!prev->cctx)
773 /* create a new empty context */
774 Newz (0, prev->cctx, 1, coro_cctx);
775
776 prev__cctx = prev->cctx; 737 prev__cctx = prev->cctx;
777 738
778 /* possibly "free" the cctx */ 739 /* possibly "free" the cctx */
779 if (prev__cctx->idle_sp == STACKLEVEL) 740 if (prev__cctx->idle_sp == STACKLEVEL && 0)
780 { 741 {
742 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
743 assert (PL_top_env == prev__cctx->top_env);
744
745 prev->cctx = 0;
746
781 cctx_put (prev__cctx); 747 cctx_put (prev__cctx);
782 prev->cctx = 0; 748 prev__cctx->inuse = 0;
783 } 749 }
784 750
785 if (!next->cctx) 751 if (!next->cctx)
752 {
786 next->cctx = cctx_get (); 753 next->cctx = cctx_get ();
754 assert (!next->cctx->inuse);
755 next->cctx->inuse = 1;
756 }
787 757
788 if (prev__cctx != next->cctx) 758 if (prev__cctx != next->cctx)
789 { 759 {
790 prev__cctx->top_env = PL_top_env; 760 prev__cctx->top_env = PL_top_env;
791 PL_top_env = next->cctx->top_env; 761 PL_top_env = next->cctx->top_env;
814 784
815 if (coro->mainstack && coro->mainstack != main_mainstack) 785 if (coro->mainstack && coro->mainstack != main_mainstack)
816 { 786 {
817 struct coro temp; 787 struct coro temp;
818 788
789 if (coro->flags & CF_RUNNING)
790 croak ("FATAL: tried to destroy currently running coroutine");
791
819 SAVE ((&temp), TRANSFER_SAVE_ALL); 792 SAVE ((&temp), TRANSFER_SAVE_ALL);
820 LOAD (coro); 793 LOAD (coro);
821 794
822 coro_destroy_stacks (); 795 coro_destroy_stacks ();
823 796
824 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 797 LOAD ((&temp)); /* this will get rid of defsv etc.. */
825 798
826 coro->mainstack = 0; 799 coro->mainstack = 0;
827 } 800 }
828 801
829 cctx_free (coro->cctx); 802 cctx_destroy (coro->cctx);
830 SvREFCNT_dec (coro->args); 803 SvREFCNT_dec (coro->args);
831 Safefree (coro); 804 Safefree (coro);
832} 805}
833 806
834static int 807static int
894} 867}
895 868
896static void 869static void
897api_transfer (SV *prev, SV *next, int flags) 870api_transfer (SV *prev, SV *next, int flags)
898{ 871{
899 dTHX;
900 struct transfer_args ta; 872 struct transfer_args ta;
901 873
902 prepare_transfer (&ta, prev, next, flags); 874 prepare_transfer (&ta, prev, next, flags);
903 TRANSFER (ta); 875 TRANSFER (ta);
904} 876}
911#define PRIO_LOW -1 883#define PRIO_LOW -1
912#define PRIO_IDLE -3 884#define PRIO_IDLE -3
913#define PRIO_MIN -4 885#define PRIO_MIN -4
914 886
915/* for Coro.pm */ 887/* for Coro.pm */
916static GV *coro_current, *coro_idle; 888static SV *coro_current;
917static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 889static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
918static int coro_nready; 890static int coro_nready;
919 891
920static void 892static void
921coro_enq (SV *sv) 893coro_enq (SV *coro_sv)
922{ 894{
923 int prio;
924
925 if (SvTYPE (sv) != SVt_PVHV)
926 croak ("Coro::ready tried to enqueue something that is not a coroutine");
927
928 prio = SvSTATE (sv)->prio;
929
930 av_push (coro_ready [prio - PRIO_MIN], sv); 895 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
931 coro_nready++; 896 coro_nready++;
932} 897}
933 898
934static SV * 899static SV *
935coro_deq (int min_prio) 900coro_deq (int min_prio)
948 } 913 }
949 914
950 return 0; 915 return 0;
951} 916}
952 917
953static void 918static int
954api_ready (SV *coro) 919api_ready (SV *coro_sv)
955{ 920{
956 dTHX; 921 struct coro *coro;
957 922
958 if (SvROK (coro)) 923 if (SvROK (coro_sv))
959 coro = SvRV (coro); 924 coro_sv = SvRV (coro_sv);
925
926 coro = SvSTATE (coro_sv);
927
928 if (coro->flags & CF_READY)
929 return 0;
930
931#if 0 /* this is actually harmless */
932 if (coro->flags & CF_RUNNING)
933 croak ("Coro::ready called on currently running coroutine");
934#endif
935
936 coro->flags |= CF_READY;
960 937
961 LOCK; 938 LOCK;
962 coro_enq (SvREFCNT_inc (coro)); 939 coro_enq (SvREFCNT_inc (coro_sv));
963 UNLOCK; 940 UNLOCK;
941
942 return 1;
943}
944
945static int
946api_is_ready (SV *coro_sv)
947{
948 return !!SvSTATE (coro_sv)->flags & CF_READY;
964} 949}
965 950
966static void 951static void
967prepare_schedule (struct transfer_args *ta) 952prepare_schedule (struct transfer_args *ta)
968{ 953{
969 SV *current, *prev, *next; 954 SV *prev, *next;
970
971 current = GvSV (coro_current);
972 955
973 for (;;) 956 for (;;)
974 { 957 {
975 LOCK; 958 LOCK;
976 next = coro_deq (PRIO_MIN); 959 next = coro_deq (PRIO_MIN);
985 ENTER; 968 ENTER;
986 SAVETMPS; 969 SAVETMPS;
987 970
988 PUSHMARK (SP); 971 PUSHMARK (SP);
989 PUTBACK; 972 PUTBACK;
990 call_sv (GvSV (coro_idle), G_DISCARD); 973 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
991 974
992 FREETMPS; 975 FREETMPS;
993 LEAVE; 976 LEAVE;
994 } 977 }
995 } 978 }
996 979
997 prev = SvRV (current); 980 prev = SvRV (coro_current);
998 SvRV (current) = next; 981 SvRV_set (coro_current, next);
999 982
1000 /* free this only after the transfer */ 983 /* free this only after the transfer */
1001 LOCK; 984 LOCK;
1002 free_coro_mortal (); 985 free_coro_mortal ();
1003 UNLOCK; 986 UNLOCK;
1004 coro_mortal = prev; 987 coro_mortal = prev;
1005 988
989 assert (!SvROK(prev));//D
990 assert (!SvROK(next));//D
991
1006 ta->prev = SvSTATE (prev); 992 ta->prev = SvSTATE (prev);
1007 ta->next = SvSTATE (next); 993 ta->next = SvSTATE (next);
1008 ta->flags = TRANSFER_SAVE_ALL; 994 ta->flags = TRANSFER_SAVE_ALL;
995
996 assert (ta->flags & CF_READY);
997 ta->next->flags &= ~CF_READY;
1009} 998}
1010 999
1011static void 1000static void
1012prepare_cede (struct transfer_args *ta) 1001prepare_cede (struct transfer_args *ta)
1013{ 1002{
1014 LOCK; 1003 api_ready (coro_current);
1015 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
1016 UNLOCK;
1017 1004
1018 prepare_schedule (ta); 1005 prepare_schedule (ta);
1019} 1006}
1020 1007
1021static void 1008static void
1022api_schedule (void) 1009api_schedule (void)
1023{ 1010{
1024 dTHX;
1025 struct transfer_args ta; 1011 struct transfer_args ta;
1026 1012
1027 prepare_schedule (&ta); 1013 prepare_schedule (&ta);
1028 TRANSFER (ta); 1014 TRANSFER (ta);
1029} 1015}
1030 1016
1031static void 1017static void
1032api_cede (void) 1018api_cede (void)
1033{ 1019{
1034 dTHX;
1035 struct transfer_args ta; 1020 struct transfer_args ta;
1036 1021
1037 prepare_cede (&ta); 1022 prepare_cede (&ta);
1038 TRANSFER (ta); 1023 TRANSFER (ta);
1039} 1024}
1071 HV *hv; 1056 HV *hv;
1072 int i; 1057 int i;
1073 1058
1074 Newz (0, coro, 1, struct coro); 1059 Newz (0, coro, 1, struct coro);
1075 coro->args = newAV (); 1060 coro->args = newAV ();
1061 coro->flags = CF_NEW;
1076 1062
1077 hv = newHV (); 1063 hv = newHV ();
1078 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1064 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1079 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1065 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1080 1066
1103 ta.flags = TRANSFER_SET_STACKLEVEL; 1089 ta.flags = TRANSFER_SET_STACKLEVEL;
1104 break; 1090 break;
1105 1091
1106 case 1: 1092 case 1:
1107 if (items != 3) 1093 if (items != 3)
1108 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1094 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items);
1109 1095
1110 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1096 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1111 break; 1097 break;
1112 1098
1113 case 2: 1099 case 2:
1123 SV *yieldstack; 1109 SV *yieldstack;
1124 SV *sv; 1110 SV *sv;
1125 AV *defav = GvAV (PL_defgv); 1111 AV *defav = GvAV (PL_defgv);
1126 1112
1127 yieldstack = *hv_fetch ( 1113 yieldstack = *hv_fetch (
1128 (HV *)SvRV (GvSV (coro_current)), 1114 (HV *)SvRV (coro_current),
1129 "yieldstack", sizeof ("yieldstack") - 1, 1115 "yieldstack", sizeof ("yieldstack") - 1,
1130 0 1116 0
1131 ); 1117 );
1132 1118
1133 /* set up @_ -- ugly */ 1119 /* set up @_ -- ugly */
1195 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1181 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1196 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1182 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1197 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1183 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1198 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1184 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1199 1185
1200 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1186 coro_current = get_sv ("Coro::current", FALSE);
1201 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1187 SvREADONLY_on (coro_current);
1202 1188
1203 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1189 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1204 coro_ready[i] = newAV (); 1190 coro_ready[i] = newAV ();
1205 1191
1206 { 1192 {
1207 SV *sv = perl_get_sv("Coro::API", 1); 1193 SV *sv = perl_get_sv("Coro::API", 1);
1208 1194
1209 coroapi.schedule = api_schedule; 1195 coroapi.schedule = api_schedule;
1210 coroapi.cede = api_cede; 1196 coroapi.cede = api_cede;
1211 coroapi.ready = api_ready; 1197 coroapi.ready = api_ready;
1198 coroapi.is_ready = api_is_ready;
1212 coroapi.nready = &coro_nready; 1199 coroapi.nready = &coro_nready;
1213 coroapi.current = coro_current; 1200 coroapi.current = coro_current;
1214 1201
1215 GCoroAPI = &coroapi; 1202 GCoroAPI = &coroapi;
1216 sv_setiv (sv, (IV)&coroapi); 1203 sv_setiv (sv, (IV)&coroapi);
1236 1223
1237 coro->prio = newprio; 1224 coro->prio = newprio;
1238 } 1225 }
1239} 1226}
1240 1227
1241void 1228SV *
1242ready (SV *self) 1229ready (SV *self)
1243 PROTOTYPE: $ 1230 PROTOTYPE: $
1244 CODE: 1231 CODE:
1245 api_ready (self); 1232 RETVAL = boolSV (api_ready (self));
1233 OUTPUT:
1234 RETVAL
1235
1236SV *
1237is_ready (SV *self)
1238 PROTOTYPE: $
1239 CODE:
1240 RETVAL = boolSV (api_is_ready (self));
1241 OUTPUT:
1242 RETVAL
1246 1243
1247int 1244int
1248nready (...) 1245nready (...)
1249 PROTOTYPE: 1246 PROTOTYPE:
1250 CODE: 1247 CODE:
1251 RETVAL = coro_nready; 1248 RETVAL = coro_nready;
1252 OUTPUT: 1249 OUTPUT:
1253 RETVAL 1250 RETVAL
1254 1251
1252void
1253_set_current (SV *current)
1254 PROTOTYPE: $
1255 CODE:
1256 SvREFCNT_dec (SvRV (coro_current));
1257 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1258
1255MODULE = Coro::State PACKAGE = Coro::AIO 1259MODULE = Coro::State PACKAGE = Coro::AIO
1256 1260
1257SV * 1261SV *
1258_get_state () 1262_get_state ()
1259 CODE: 1263 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines