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.107 by root, Mon Nov 27 02:15:51 2006 UTC vs.
Revision 1.119 by root, Sat Dec 2 15:41:28 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) \
119 /* cpu state */ 122 /* cpu state */
120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 JMPENV *top_env; 124 JMPENV *top_env;
122 coro_context cctx; 125 coro_context cctx;
123 126
127 int inuse;
128
124#if USE_VALGRIND 129#if USE_VALGRIND
125 int valgrind_id; 130 int valgrind_id;
126#endif 131#endif
127} 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};
128 139
129/* this is a structure representing a perl-level coroutine */ 140/* this is a structure representing a perl-level coroutine */
130struct coro { 141struct coro {
131 /* the c coroutine allocated to this perl coroutine, if any */ 142 /* the c coroutine allocated to this perl coroutine, if any */
132 coro_cctx *cctx; 143 coro_cctx *cctx;
133 144
134 /* data associated with this coroutine (initial args) */ 145 /* data associated with this coroutine (initial args) */
135 AV *args; 146 AV *args;
136 int refcnt; 147 int refcnt;
148 int flags;
137 149
138 /* optionally saved, might be zero */ 150 /* optionally saved, might be zero */
139 AV *defav; 151 AV *defav;
140 SV *defsv; 152 SV *defsv;
141 SV *errsv; 153 SV *errsv;
142 154
143 /* saved global state not related to stacks */ 155#define VAR(name,type) type name;
144 U8 dowarn; 156# include "state.h"
145 I32 in_eval; 157#undef VAR
146
147 /* the stacks and related info (callchain etc..) */
148 PERL_SI *curstackinfo;
149 AV *curstack;
150 AV *mainstack;
151 SV **stack_sp;
152 OP *op;
153 SV **curpad;
154 AV *comppad;
155 CV *compcv;
156 SV **stack_base;
157 SV **stack_max;
158 SV **tmps_stack;
159 I32 tmps_floor;
160 I32 tmps_ix;
161 I32 tmps_max;
162 I32 *markstack;
163 I32 *markstack_ptr;
164 I32 *markstack_max;
165 I32 *scopestack;
166 I32 scopestack_ix;
167 I32 scopestack_max;
168 ANY *savestack;
169 I32 savestack_ix;
170 I32 savestack_max;
171 OP **retstack;
172 I32 retstack_ix;
173 I32 retstack_max;
174 PMOP *curpm;
175 COP *curcop;
176 158
177 /* coro process data */ 159 /* coro process data */
178 int prio; 160 int prio;
179}; 161};
180 162
244 226
245#define PERL_MAGIC_coro PERL_MAGIC_ext 227#define PERL_MAGIC_coro PERL_MAGIC_ext
246 228
247static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 229static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
248 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
249/* the next two functions merely cache the padlists */ 238/* the next two functions merely cache the padlists */
250static void 239static void
251get_padlist (CV *cv) 240get_padlist (CV *cv)
252{ 241{
253 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 242 MAGIC *mg = CORO_MAGIC (cv);
243 AV *av;
254 244
255 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 245 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
256 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 246 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
257 else 247 else
258 { 248 {
259#if 0 249#if 0
260 /* this is probably cleaner, but also slower? */ 250 /* this is probably cleaner, but also slower? */
261 CV *cp = Perl_cv_clone (cv); 251 CV *cp = Perl_cv_clone (cv);
269} 259}
270 260
271static void 261static void
272put_padlist (CV *cv) 262put_padlist (CV *cv)
273{ 263{
274 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 264 MAGIC *mg = CORO_MAGIC (cv);
265 AV *av;
275 266
276 if (!mg) 267 if (!mg)
277 { 268 {
278 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 269 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
279 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 270 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
280 mg->mg_virtual = &vtbl_coro; 271 mg->mg_virtual = &vtbl_coro;
281 mg->mg_obj = (SV *)newAV (); 272 mg->mg_obj = (SV *)newAV ();
282 } 273 }
283 274
284 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);
285} 281}
286 282
287#define SB do { 283#define SB do {
288#define SE } while (0) 284#define SE } while (0)
289 285
290#define LOAD(state) load_state((state)); 286#define LOAD(state) load_state ((state))
291#define SAVE(state,flags) save_state((state),(flags)); 287#define SAVE(state,flags) save_state ((state),(flags))
292 288
293#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
294 290
295static void 291static void
296load_state(Coro__State c) 292load_state (Coro__State c)
297{ 293{
298 PL_dowarn = c->dowarn; 294#define VAR(name,type) PL_ ## name = c->name;
299 PL_in_eval = c->in_eval; 295# include "state.h"
300 296#undef VAR
301 PL_curstackinfo = c->curstackinfo;
302 PL_curstack = c->curstack;
303 PL_mainstack = c->mainstack;
304 PL_stack_sp = c->stack_sp;
305 PL_op = c->op;
306 PL_curpad = c->curpad;
307 PL_comppad = c->comppad;
308 PL_compcv = c->compcv;
309 PL_stack_base = c->stack_base;
310 PL_stack_max = c->stack_max;
311 PL_tmps_stack = c->tmps_stack;
312 PL_tmps_floor = c->tmps_floor;
313 PL_tmps_ix = c->tmps_ix;
314 PL_tmps_max = c->tmps_max;
315 PL_markstack = c->markstack;
316 PL_markstack_ptr = c->markstack_ptr;
317 PL_markstack_max = c->markstack_max;
318 PL_scopestack = c->scopestack;
319 PL_scopestack_ix = c->scopestack_ix;
320 PL_scopestack_max = c->scopestack_max;
321 PL_savestack = c->savestack;
322 PL_savestack_ix = c->savestack_ix;
323 PL_savestack_max = c->savestack_max;
324#if !PERL_VERSION_ATLEAST (5,9,0)
325 PL_retstack = c->retstack;
326 PL_retstack_ix = c->retstack_ix;
327 PL_retstack_max = c->retstack_max;
328#endif
329 PL_curpm = c->curpm;
330 PL_curcop = c->curcop;
331 297
332 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 298 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
333 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 299 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
334 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 300 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
335 301
338 CV *cv; 304 CV *cv;
339 305
340 /* now do the ugly restore mess */ 306 /* now do the ugly restore mess */
341 while ((cv = (CV *)POPs)) 307 while ((cv = (CV *)POPs))
342 { 308 {
343 AV *padlist = (AV *)POPs;
344
345 if (padlist)
346 {
347 put_padlist (cv); /* mark this padlist as available */ 309 put_padlist (cv); /* mark this padlist as available */
348 CvPADLIST(cv) = padlist; 310 CvDEPTH (cv) = PTR2IV (POPs);
349 } 311 CvPADLIST (cv) = (AV *)POPs;
350
351 ++CvDEPTH(cv);
352 } 312 }
353 313
354 PUTBACK; 314 PUTBACK;
355 } 315 }
356} 316}
357 317
358static void 318static void
359save_state(Coro__State c, int flags) 319save_state (Coro__State c, int flags)
360{ 320{
361 { 321 {
362 dSP; 322 dSP;
363 I32 cxix = cxstack_ix; 323 I32 cxix = cxstack_ix;
364 PERL_CONTEXT *ccstk = cxstack; 324 PERL_CONTEXT *ccstk = cxstack;
375 { 335 {
376 while (cxix >= 0) 336 while (cxix >= 0)
377 { 337 {
378 PERL_CONTEXT *cx = &ccstk[cxix--]; 338 PERL_CONTEXT *cx = &ccstk[cxix--];
379 339
380 if (CxTYPE(cx) == CXt_SUB) 340 if (CxTYPE (cx) == CXt_SUB)
381 { 341 {
382 CV *cv = cx->blk_sub.cv; 342 CV *cv = cx->blk_sub.cv;
343
383 if (CvDEPTH(cv)) 344 if (CvDEPTH (cv))
384 { 345 {
385 EXTEND (SP, CvDEPTH(cv)*2); 346 EXTEND (SP, 3);
386 347
387 while (--CvDEPTH(cv))
388 {
389 /* this tells the restore code to increment CvDEPTH */
390 PUSHs (Nullsv);
391 PUSHs ((SV *)cv);
392 }
393
394 PUSHs ((SV *)CvPADLIST(cv)); 348 PUSHs ((SV *)CvPADLIST (cv));
349 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
395 PUSHs ((SV *)cv); 350 PUSHs ((SV *)cv);
396 351
352 CvDEPTH (cv) = 0;
397 get_padlist (cv); 353 get_padlist (cv);
398 } 354 }
399 } 355 }
400#ifdef CXt_FORMAT 356#ifdef CXt_FORMAT
401 else if (CxTYPE(cx) == CXt_FORMAT) 357 else if (CxTYPE (cx) == CXt_FORMAT)
402 { 358 {
403 /* 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? */
404 /* my bold guess is as a simple, plain sub... */ 360 /* my bold guess is as a simple, plain sub... */
405 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");
406 } 362 }
420 376
421 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;
422 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 378 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
423 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 379 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
424 380
425 c->dowarn = PL_dowarn; 381#define VAR(name,type)c->name = PL_ ## name;
426 c->in_eval = PL_in_eval; 382# include "state.h"
427 383#undef VAR
428 c->curstackinfo = PL_curstackinfo;
429 c->curstack = PL_curstack;
430 c->mainstack = PL_mainstack;
431 c->stack_sp = PL_stack_sp;
432 c->op = PL_op;
433 c->curpad = PL_curpad;
434 c->comppad = PL_comppad;
435 c->compcv = PL_compcv;
436 c->stack_base = PL_stack_base;
437 c->stack_max = PL_stack_max;
438 c->tmps_stack = PL_tmps_stack;
439 c->tmps_floor = PL_tmps_floor;
440 c->tmps_ix = PL_tmps_ix;
441 c->tmps_max = PL_tmps_max;
442 c->markstack = PL_markstack;
443 c->markstack_ptr = PL_markstack_ptr;
444 c->markstack_max = PL_markstack_max;
445 c->scopestack = PL_scopestack;
446 c->scopestack_ix = PL_scopestack_ix;
447 c->scopestack_max = PL_scopestack_max;
448 c->savestack = PL_savestack;
449 c->savestack_ix = PL_savestack_ix;
450 c->savestack_max = PL_savestack_max;
451#if !PERL_VERSION_ATLEAST (5,9,0)
452 c->retstack = PL_retstack;
453 c->retstack_ix = PL_retstack_ix;
454 c->retstack_max = PL_retstack_max;
455#endif
456 c->curpm = PL_curpm;
457 c->curcop = PL_curcop;
458} 384}
459 385
460/* 386/*
461 * allocate various perl stacks. This is an exact copy 387 * allocate various perl stacks. This is an exact copy
462 * of perl.c:init_stacks, except that it uses less memory 388 * of perl.c:init_stacks, except that it uses less memory
464 * not usually need a lot of stackspace. 390 * not usually need a lot of stackspace.
465 */ 391 */
466static void 392static void
467coro_init_stacks () 393coro_init_stacks ()
468{ 394{
469 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 395 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
470 PL_curstackinfo->si_type = PERLSI_MAIN; 396 PL_curstackinfo->si_type = PERLSI_MAIN;
471 PL_curstack = PL_curstackinfo->si_stack; 397 PL_curstack = PL_curstackinfo->si_stack;
472 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 398 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
473 399
474 PL_stack_base = AvARRAY(PL_curstack); 400 PL_stack_base = AvARRAY(PL_curstack);
475 PL_stack_sp = PL_stack_base; 401 PL_stack_sp = PL_stack_base;
476 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 402 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
477 403
478 New(50,PL_tmps_stack,96,SV*); 404 New(50,PL_tmps_stack,128,SV*);
479 PL_tmps_floor = -1; 405 PL_tmps_floor = -1;
480 PL_tmps_ix = -1; 406 PL_tmps_ix = -1;
481 PL_tmps_max = 96; 407 PL_tmps_max = 128;
482 408
483 New(54,PL_markstack,16,I32); 409 New(54,PL_markstack,32,I32);
484 PL_markstack_ptr = PL_markstack; 410 PL_markstack_ptr = PL_markstack;
485 PL_markstack_max = PL_markstack + 16; 411 PL_markstack_max = PL_markstack + 32;
486 412
487#ifdef SET_MARK_OFFSET 413#ifdef SET_MARK_OFFSET
488 SET_MARK_OFFSET; 414 SET_MARK_OFFSET;
489#endif 415#endif
490 416
491 New(54,PL_scopestack,16,I32); 417 New(54,PL_scopestack,32,I32);
492 PL_scopestack_ix = 0; 418 PL_scopestack_ix = 0;
493 PL_scopestack_max = 16; 419 PL_scopestack_max = 32;
494 420
495 New(54,PL_savestack,96,ANY); 421 New(54,PL_savestack,64,ANY);
496 PL_savestack_ix = 0; 422 PL_savestack_ix = 0;
497 PL_savestack_max = 96; 423 PL_savestack_max = 64;
498 424
499#if !PERL_VERSION_ATLEAST (5,9,0) 425#if !PERL_VERSION_ATLEAST (5,9,0)
500 New(54,PL_retstack,8,OP*); 426 New(54,PL_retstack,16,OP*);
501 PL_retstack_ix = 0; 427 PL_retstack_ix = 0;
502 PL_retstack_max = 8; 428 PL_retstack_max = 16;
503#endif 429#endif
504} 430}
505 431
506/* 432/*
507 * destroy the stacks, the callchain etc... 433 * destroy the stacks, the callchain etc...
560 * emulate part of the perl startup here. 486 * emulate part of the perl startup here.
561 */ 487 */
562 488
563 coro_init_stacks (); 489 coro_init_stacks ();
564 490
565 PL_curcop = 0; 491 PL_curcop = &PL_compiling;
566 PL_in_eval = 0; 492 PL_in_eval = EVAL_NULL;
567 PL_curpm = 0; 493 PL_curpm = 0;
494 PL_localizing = 0;
495 PL_dirty = 0;
496 PL_restartop = 0;
568 497
569 { 498 {
570 dSP; 499 dSP;
571 LOGOP myop; 500 LOGOP myop;
572 501
585 PUSHMARK (SP); 514 PUSHMARK (SP);
586 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 515 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
587 PUTBACK; 516 PUTBACK;
588 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 517 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
589 SPAGAIN; 518 SPAGAIN;
590
591 ENTER; /* necessary e.g. for dounwind */
592 } 519 }
520
521 ENTER; /* necessary e.g. for dounwind */
593} 522}
594 523
595static void 524static void
596free_coro_mortal () 525free_coro_mortal ()
597{ 526{
691 620
692 return cctx; 621 return cctx;
693} 622}
694 623
695static void 624static void
696cctx_free (coro_cctx *cctx) 625cctx_destroy (coro_cctx *cctx)
697{ 626{
698 if (!cctx) 627 if (!cctx)
699 return; 628 return;
700 629
701 --cctx_count; 630 --cctx_count;
718{ 647{
719 coro_cctx *cctx; 648 coro_cctx *cctx;
720 649
721 if (cctx_first) 650 if (cctx_first)
722 { 651 {
723 --cctx_idle;
724 cctx = cctx_first; 652 cctx = cctx_first;
725 cctx_first = cctx->next; 653 cctx_first = cctx->next;
654 --cctx_idle;
726 } 655 }
727 else 656 else
728 { 657 {
729 cctx = cctx_new (); 658 cctx = cctx_new ();
730 PL_op = PL_op->op_next; 659 PL_op = PL_op->op_next;
734} 663}
735 664
736static void 665static void
737cctx_put (coro_cctx *cctx) 666cctx_put (coro_cctx *cctx)
738{ 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
739 ++cctx_idle; 679 ++cctx_idle;
740 cctx->next = cctx_first; 680 cctx->next = cctx_first;
741 cctx_first = cctx; 681 cctx_first = cctx;
742} 682}
743 683
752 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 692 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
753 else if (prev != next) 693 else if (prev != next)
754 { 694 {
755 coro_cctx *prev__cctx; 695 coro_cctx *prev__cctx;
756 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;
715
757 LOCK; 716 LOCK;
758 717
759 if (next->mainstack) 718 if (next->flags & CF_NEW)
719 {
720 /* need to start coroutine */
721 next->flags &= ~CF_NEW;
722 /* first get rid of the old state */
723 SAVE (prev, -1);
724 /* setup coroutine call */
725 setup_coro (next);
726 /* need a new stack */
727 assert (!next->stack);
728 }
729 else
760 { 730 {
761 /* coroutine already started */ 731 /* coroutine already started */
762 SAVE (prev, flags); 732 SAVE (prev, flags);
763 LOAD (next); 733 LOAD (next);
764 } 734 }
765 else
766 {
767 /* need to start coroutine */
768 /* first get rid of the old state */
769 SAVE (prev, -1);
770 /* setup coroutine call */
771 setup_coro (next);
772 /* need a stack */
773 next->cctx = 0;
774 }
775
776 if (!prev->cctx)
777 /* create a new empty context */
778 Newz (0, prev->cctx, 1, coro_cctx);
779 735
780 prev__cctx = prev->cctx; 736 prev__cctx = prev->cctx;
781 737
782 /* possibly "free" the cctx */ 738 /* possibly "free" the cctx */
783 if (prev__cctx->idle_sp == STACKLEVEL) 739 if (prev__cctx->idle_sp == STACKLEVEL)
784 { 740 {
741 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
742 assert (PL_top_env == prev__cctx->top_env);
743
744 prev->cctx = 0;
745
785 cctx_put (prev__cctx); 746 cctx_put (prev__cctx);
786 prev->cctx = 0; 747 prev__cctx->inuse = 0;
787 } 748 }
788 749
789 if (!next->cctx) 750 if (!next->cctx)
751 {
790 next->cctx = cctx_get (); 752 next->cctx = cctx_get ();
753 assert (!next->cctx->inuse);
754 next->cctx->inuse = 1;
755 }
791 756
792 if (prev__cctx != next->cctx) 757 if (prev__cctx != next->cctx)
793 { 758 {
794 prev__cctx->top_env = PL_top_env; 759 prev__cctx->top_env = PL_top_env;
795 PL_top_env = next->cctx->top_env; 760 PL_top_env = next->cctx->top_env;
818 783
819 if (coro->mainstack && coro->mainstack != main_mainstack) 784 if (coro->mainstack && coro->mainstack != main_mainstack)
820 { 785 {
821 struct coro temp; 786 struct coro temp;
822 787
788 if (coro->flags & CF_RUNNING)
789 croak ("FATAL: tried to destroy currently running coroutine");
790
823 SAVE ((&temp), TRANSFER_SAVE_ALL); 791 SAVE ((&temp), TRANSFER_SAVE_ALL);
824 LOAD (coro); 792 LOAD (coro);
825 793
826 coro_destroy_stacks (); 794 coro_destroy_stacks ();
827 795
828 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 796 LOAD ((&temp)); /* this will get rid of defsv etc.. */
829 797
830 coro->mainstack = 0; 798 coro->mainstack = 0;
831 } 799 }
832 800
833 cctx_free (coro->cctx); 801 cctx_destroy (coro->cctx);
834 SvREFCNT_dec (coro->args); 802 SvREFCNT_dec (coro->args);
835 Safefree (coro); 803 Safefree (coro);
836} 804}
837 805
838static int 806static int
898} 866}
899 867
900static void 868static void
901api_transfer (SV *prev, SV *next, int flags) 869api_transfer (SV *prev, SV *next, int flags)
902{ 870{
903 dTHX;
904 struct transfer_args ta; 871 struct transfer_args ta;
905 872
906 prepare_transfer (&ta, prev, next, flags); 873 prepare_transfer (&ta, prev, next, flags);
907 TRANSFER (ta); 874 TRANSFER (ta);
908} 875}
915#define PRIO_LOW -1 882#define PRIO_LOW -1
916#define PRIO_IDLE -3 883#define PRIO_IDLE -3
917#define PRIO_MIN -4 884#define PRIO_MIN -4
918 885
919/* for Coro.pm */ 886/* for Coro.pm */
920static GV *coro_current, *coro_idle; 887static SV *coro_current;
921static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 888static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
922static int coro_nready; 889static int coro_nready;
923 890
924static void 891static void
925coro_enq (SV *sv) 892coro_enq (SV *coro_sv)
926{ 893{
927 int prio;
928
929 if (SvTYPE (sv) != SVt_PVHV)
930 croak ("Coro::ready tried to enqueue something that is not a coroutine");
931
932 prio = SvSTATE (sv)->prio;
933
934 av_push (coro_ready [prio - PRIO_MIN], sv); 894 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
935 coro_nready++; 895 coro_nready++;
936} 896}
937 897
938static SV * 898static SV *
939coro_deq (int min_prio) 899coro_deq (int min_prio)
952 } 912 }
953 913
954 return 0; 914 return 0;
955} 915}
956 916
957static void 917static int
958api_ready (SV *coro) 918api_ready (SV *coro_sv)
959{ 919{
960 dTHX; 920 struct coro *coro;
961 921
962 if (SvROK (coro)) 922 if (SvROK (coro_sv))
963 coro = SvRV (coro); 923 coro_sv = SvRV (coro_sv);
924
925 coro = SvSTATE (coro_sv);
926
927 if (coro->flags & CF_READY)
928 return 0;
929
930#if 0 /* this is actually harmless */
931 if (coro->flags & CF_RUNNING)
932 croak ("Coro::ready called on currently running coroutine");
933#endif
934
935 coro->flags |= CF_READY;
964 936
965 LOCK; 937 LOCK;
966 coro_enq (SvREFCNT_inc (coro)); 938 coro_enq (SvREFCNT_inc (coro_sv));
967 UNLOCK; 939 UNLOCK;
940
941 return 1;
942}
943
944static int
945api_is_ready (SV *coro_sv)
946{
947 return !!SvSTATE (coro_sv)->flags & CF_READY;
968} 948}
969 949
970static void 950static void
971prepare_schedule (struct transfer_args *ta) 951prepare_schedule (struct transfer_args *ta)
972{ 952{
973 SV *current, *prev, *next; 953 SV *prev, *next;
974
975 current = GvSV (coro_current);
976 954
977 for (;;) 955 for (;;)
978 { 956 {
979 LOCK; 957 LOCK;
980 next = coro_deq (PRIO_MIN); 958 next = coro_deq (PRIO_MIN);
989 ENTER; 967 ENTER;
990 SAVETMPS; 968 SAVETMPS;
991 969
992 PUSHMARK (SP); 970 PUSHMARK (SP);
993 PUTBACK; 971 PUTBACK;
994 call_sv (GvSV (coro_idle), G_DISCARD); 972 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
995 973
996 FREETMPS; 974 FREETMPS;
997 LEAVE; 975 LEAVE;
998 } 976 }
999 } 977 }
1000 978
1001 prev = SvRV (current); 979 prev = SvRV (coro_current);
1002 SvRV (current) = next; 980 SvRV_set (coro_current, next);
1003 981
1004 /* free this only after the transfer */ 982 /* free this only after the transfer */
1005 LOCK; 983 LOCK;
1006 free_coro_mortal (); 984 free_coro_mortal ();
1007 UNLOCK; 985 UNLOCK;
1008 coro_mortal = prev; 986 coro_mortal = prev;
1009 987
988 assert (!SvROK(prev));//D
989 assert (!SvROK(next));//D
990
1010 ta->prev = SvSTATE (prev); 991 ta->prev = SvSTATE (prev);
1011 ta->next = SvSTATE (next); 992 ta->next = SvSTATE (next);
1012 ta->flags = TRANSFER_SAVE_ALL; 993 ta->flags = TRANSFER_SAVE_ALL;
994
995 assert (ta->flags & CF_READY);
996 ta->next->flags &= ~CF_READY;
1013} 997}
1014 998
1015static void 999static void
1016prepare_cede (struct transfer_args *ta) 1000prepare_cede (struct transfer_args *ta)
1017{ 1001{
1018 LOCK; 1002 api_ready (coro_current);
1019 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
1020 UNLOCK;
1021 1003
1022 prepare_schedule (ta); 1004 prepare_schedule (ta);
1023} 1005}
1024 1006
1025static void 1007static void
1026api_schedule (void) 1008api_schedule (void)
1027{ 1009{
1028 dTHX;
1029 struct transfer_args ta; 1010 struct transfer_args ta;
1030 1011
1031 prepare_schedule (&ta); 1012 prepare_schedule (&ta);
1032 TRANSFER (ta); 1013 TRANSFER (ta);
1033} 1014}
1034 1015
1035static void 1016static void
1036api_cede (void) 1017api_cede (void)
1037{ 1018{
1038 dTHX;
1039 struct transfer_args ta; 1019 struct transfer_args ta;
1040 1020
1041 prepare_cede (&ta); 1021 prepare_cede (&ta);
1042 TRANSFER (ta); 1022 TRANSFER (ta);
1043} 1023}
1075 HV *hv; 1055 HV *hv;
1076 int i; 1056 int i;
1077 1057
1078 Newz (0, coro, 1, struct coro); 1058 Newz (0, coro, 1, struct coro);
1079 coro->args = newAV (); 1059 coro->args = newAV ();
1060 coro->flags = CF_NEW;
1080 1061
1081 hv = newHV (); 1062 hv = newHV ();
1082 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1063 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1083 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1064 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1084 1065
1092_set_stacklevel (...) 1073_set_stacklevel (...)
1093 ALIAS: 1074 ALIAS:
1094 Coro::State::transfer = 1 1075 Coro::State::transfer = 1
1095 Coro::schedule = 2 1076 Coro::schedule = 2
1096 Coro::cede = 3 1077 Coro::cede = 3
1097 Coro::Cont::yield = 4
1098 CODE: 1078 CODE:
1099{ 1079{
1100 struct transfer_args ta; 1080 struct transfer_args ta;
1101 1081
1102 switch (ix) 1082 switch (ix)
1107 ta.flags = TRANSFER_SET_STACKLEVEL; 1087 ta.flags = TRANSFER_SET_STACKLEVEL;
1108 break; 1088 break;
1109 1089
1110 case 1: 1090 case 1:
1111 if (items != 3) 1091 if (items != 3)
1112 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1092 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items);
1113 1093
1114 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1094 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1115 break; 1095 break;
1116 1096
1117 case 2: 1097 case 2:
1119 break; 1099 break;
1120 1100
1121 case 3: 1101 case 3:
1122 prepare_cede (&ta); 1102 prepare_cede (&ta);
1123 break; 1103 break;
1124
1125 case 4:
1126 {
1127 SV *yieldstack;
1128 SV *sv;
1129 AV *defav = GvAV (PL_defgv);
1130
1131 yieldstack = *hv_fetch (
1132 (HV *)SvRV (GvSV (coro_current)),
1133 "yieldstack", sizeof ("yieldstack") - 1,
1134 0
1135 );
1136
1137 /* set up @_ -- ugly */
1138 av_clear (defav);
1139 av_fill (defav, items - 1);
1140 while (items--)
1141 av_store (defav, items, SvREFCNT_inc (ST(items)));
1142
1143 sv = av_pop ((AV *)SvRV (yieldstack));
1144 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1145 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1146 ta.flags = 0;
1147 SvREFCNT_dec (sv);
1148 }
1149 break;
1150
1151 } 1104 }
1152 1105
1153 TRANSFER (ta); 1106 TRANSFER (ta);
1154} 1107}
1155 1108
1199 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1152 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1200 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1153 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1201 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1154 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1202 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1155 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1203 1156
1204 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1157 coro_current = get_sv ("Coro::current", FALSE);
1205 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1158 SvREADONLY_on (coro_current);
1206 1159
1207 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1160 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1208 coro_ready[i] = newAV (); 1161 coro_ready[i] = newAV ();
1209 1162
1210 { 1163 {
1211 SV *sv = perl_get_sv("Coro::API", 1); 1164 SV *sv = perl_get_sv("Coro::API", 1);
1212 1165
1213 coroapi.schedule = api_schedule; 1166 coroapi.schedule = api_schedule;
1214 coroapi.cede = api_cede; 1167 coroapi.cede = api_cede;
1215 coroapi.ready = api_ready; 1168 coroapi.ready = api_ready;
1169 coroapi.is_ready = api_is_ready;
1216 coroapi.nready = &coro_nready; 1170 coroapi.nready = &coro_nready;
1217 coroapi.current = coro_current; 1171 coroapi.current = coro_current;
1218 1172
1219 GCoroAPI = &coroapi; 1173 GCoroAPI = &coroapi;
1220 sv_setiv (sv, (IV)&coroapi); 1174 sv_setiv (sv, (IV)&coroapi);
1240 1194
1241 coro->prio = newprio; 1195 coro->prio = newprio;
1242 } 1196 }
1243} 1197}
1244 1198
1245void 1199SV *
1246ready (SV *self) 1200ready (SV *self)
1247 PROTOTYPE: $ 1201 PROTOTYPE: $
1248 CODE: 1202 CODE:
1249 api_ready (self); 1203 RETVAL = boolSV (api_ready (self));
1204 OUTPUT:
1205 RETVAL
1206
1207SV *
1208is_ready (SV *self)
1209 PROTOTYPE: $
1210 CODE:
1211 RETVAL = boolSV (api_is_ready (self));
1212 OUTPUT:
1213 RETVAL
1250 1214
1251int 1215int
1252nready (...) 1216nready (...)
1253 PROTOTYPE: 1217 PROTOTYPE:
1254 CODE: 1218 CODE:
1255 RETVAL = coro_nready; 1219 RETVAL = coro_nready;
1256 OUTPUT: 1220 OUTPUT:
1257 RETVAL 1221 RETVAL
1258 1222
1223void
1224_set_current (SV *current)
1225 PROTOTYPE: $
1226 CODE:
1227 SvREFCNT_dec (SvRV (coro_current));
1228 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1229
1259MODULE = Coro::State PACKAGE = Coro::AIO 1230MODULE = Coro::State PACKAGE = Coro::AIO
1260 1231
1261SV * 1232SV *
1262_get_state () 1233_get_state ()
1263 CODE: 1234 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines