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.146 by root, Sat Mar 17 19:51:57 2007 UTC vs.
Revision 1.164 by root, Tue Sep 25 01:47:23 2007 UTC

88/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
89#ifndef CORO_PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
90# define CORO_PREFER_PERL_FUNCTIONS 0 90# define CORO_PREFER_PERL_FUNCTIONS 0
91#endif 91#endif
92 92
93/* The next macro should declare a variable stacklevel that contains and approximation 93/* The next macros try to return the current stack pointer, in an as
94 * to the current C stack pointer. Its property is that it changes with each call 94 * portable way as possible. */
95 * and should be unique. */
96#define dSTACKLEVEL int stacklevel 95#define dSTACKLEVEL volatile char stacklevel
97#define STACKLEVEL ((void *)&stacklevel) 96#define STACKLEVEL ((void *)&stacklevel)
98 97
99#define IN_DESTRUCT (PL_main_cv == Nullcv) 98#define IN_DESTRUCT (PL_main_cv == Nullcv)
100 99
101#if __GNUC__ >= 3 100#if __GNUC__ >= 3
129}; 128};
130 129
131static size_t coro_stacksize = CORO_STACKSIZE; 130static size_t coro_stacksize = CORO_STACKSIZE;
132static struct CoroAPI coroapi; 131static struct CoroAPI coroapi;
133static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
136 141
137static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
138static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
139 144
140/* this is a structure representing a c-level coroutine */ 145/* this is a structure representing a c-level coroutine */
177 182
178 /* optionally saved, might be zero */ 183 /* optionally saved, might be zero */
179 AV *defav; /* @_ */ 184 AV *defav; /* @_ */
180 SV *defsv; /* $_ */ 185 SV *defsv; /* $_ */
181 SV *errsv; /* $@ */ 186 SV *errsv; /* $@ */
187 GV *deffh; /* default filehandle */
182 SV *irssv; /* $/ */ 188 SV *irssv; /* $/ */
183 SV *irssv_sv; /* real $/ cache */ 189 SV *irssv_sv; /* real $/ cache */
184 190
185#define VAR(name,type) type name; 191#define VAR(name,type) type name;
186# include "state.h" 192# include "state.h"
187#undef VAR 193#undef VAR
188 194
189 /* coro process data */ 195 /* coro process data */
190 int prio; 196 int prio;
197
198 /* linked list */
199 struct coro *next, *prev;
200 HV *hv; /* the perl hash associated with this coro, if any */
191}; 201};
192 202
193typedef struct coro *Coro__State; 203typedef struct coro *Coro__State;
194typedef struct coro *Coro__State_or_hashref; 204typedef struct coro *Coro__State_or_hashref;
195 205
204 214
205/* for Coro.pm */ 215/* for Coro.pm */
206static SV *coro_current; 216static SV *coro_current;
207static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 217static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
208static int coro_nready; 218static int coro_nready;
219static struct coro *coro_first;
209 220
210/** lowlevel stuff **********************************************************/ 221/** lowlevel stuff **********************************************************/
211 222
212static AV * 223static AV *
213coro_clone_padlist (pTHX_ CV *cv) 224coro_clone_padlist (pTHX_ CV *cv)
341#undef VAR 352#undef VAR
342 353
343 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 354 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
344 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 355 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
345 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 356 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
357 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
358
346 if (c->irssv) 359 if (c->irssv)
347 { 360 {
348 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 361 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
362 {
349 SvREFCNT_dec (c->irssv); 363 SvREFCNT_dec (c->irssv);
364 c->irssv = 0;
365 }
350 else 366 else
351 { 367 {
352 REPLACE_SV (PL_rs, c->irssv); 368 REPLACE_SV (PL_rs, c->irssv);
353 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 369 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
354 sv_setsv (c->irssv_sv, PL_rs); 370 sv_setsv (c->irssv_sv, PL_rs);
367 CvPADLIST (cv) = (AV *)POPs; 383 CvPADLIST (cv) = (AV *)POPs;
368 } 384 }
369 385
370 PUTBACK; 386 PUTBACK;
371 } 387 }
372 assert (!PL_comppad || AvARRAY (PL_comppad));//D
373} 388}
374 389
375static void 390static void
376save_perl (pTHX_ Coro__State c) 391save_perl (pTHX_ Coro__State c)
377{ 392{
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
379 { 393 {
380 dSP; 394 dSP;
381 I32 cxix = cxstack_ix; 395 I32 cxix = cxstack_ix;
382 PERL_CONTEXT *ccstk = cxstack; 396 PERL_CONTEXT *ccstk = cxstack;
383 PERL_SI *top_si = PL_curstackinfo; 397 PERL_SI *top_si = PL_curstackinfo;
385 /* 399 /*
386 * the worst thing you can imagine happens first - we have to save 400 * the worst thing you can imagine happens first - we have to save
387 * (and reinitialize) all cv's in the whole callchain :( 401 * (and reinitialize) all cv's in the whole callchain :(
388 */ 402 */
389 403
390 EXTEND (SP, 3 + 1);
391 PUSHs (Nullsv); 404 XPUSHs (Nullsv);
392 /* this loop was inspired by pp_caller */ 405 /* this loop was inspired by pp_caller */
393 for (;;) 406 for (;;)
394 { 407 {
395 while (cxix >= 0) 408 while (cxix >= 0)
396 { 409 {
397 PERL_CONTEXT *cx = &ccstk[cxix--]; 410 PERL_CONTEXT *cx = &ccstk[cxix--];
398 411
399 if (CxTYPE (cx) == CXt_SUB) 412 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
400 { 413 {
401 CV *cv = cx->blk_sub.cv; 414 CV *cv = cx->blk_sub.cv;
402 415
403 if (CvDEPTH (cv)) 416 if (CvDEPTH (cv))
404 { 417 {
415 428
416 if (top_si->si_type == PERLSI_MAIN) 429 if (top_si->si_type == PERLSI_MAIN)
417 break; 430 break;
418 431
419 top_si = top_si->si_prev; 432 top_si = top_si->si_prev;
420 ccstk = top_si->si_cxstack; 433 ccstk = top_si->si_cxstack;
421 cxix = top_si->si_cxix; 434 cxix = top_si->si_cxix;
422 } 435 }
423 436
424 PUTBACK; 437 PUTBACK;
425 } 438 }
426 439
427 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 440 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
428 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 441 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
429 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 442 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
443 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
430 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 444 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
431 445
432#define VAR(name,type)c->name = PL_ ## name; 446#define VAR(name,type)c->name = PL_ ## name;
433# include "state.h" 447# include "state.h"
434#undef VAR 448#undef VAR
444# define coro_init_stacks init_stacks 458# define coro_init_stacks init_stacks
445#else 459#else
446static void 460static void
447coro_init_stacks (pTHX) 461coro_init_stacks (pTHX)
448{ 462{
449 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 463 PL_curstackinfo = new_stackinfo(64, 6);
450 PL_curstackinfo->si_type = PERLSI_MAIN; 464 PL_curstackinfo->si_type = PERLSI_MAIN;
451 PL_curstack = PL_curstackinfo->si_stack; 465 PL_curstack = PL_curstackinfo->si_stack;
452 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 466 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
453 467
454 PL_stack_base = AvARRAY(PL_curstack); 468 PL_stack_base = AvARRAY(PL_curstack);
455 PL_stack_sp = PL_stack_base; 469 PL_stack_sp = PL_stack_base;
456 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 470 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
457 471
458 New(50,PL_tmps_stack,128,SV*); 472 New(50,PL_tmps_stack,64,SV*);
459 PL_tmps_floor = -1; 473 PL_tmps_floor = -1;
460 PL_tmps_ix = -1; 474 PL_tmps_ix = -1;
461 PL_tmps_max = 128; 475 PL_tmps_max = 64;
462 476
463 New(54,PL_markstack,32,I32); 477 New(54,PL_markstack,16,I32);
464 PL_markstack_ptr = PL_markstack; 478 PL_markstack_ptr = PL_markstack;
465 PL_markstack_max = PL_markstack + 32; 479 PL_markstack_max = PL_markstack + 16;
466 480
467#ifdef SET_MARK_OFFSET 481#ifdef SET_MARK_OFFSET
468 SET_MARK_OFFSET; 482 SET_MARK_OFFSET;
469#endif 483#endif
470 484
471 New(54,PL_scopestack,32,I32); 485 New(54,PL_scopestack,16,I32);
472 PL_scopestack_ix = 0; 486 PL_scopestack_ix = 0;
473 PL_scopestack_max = 32; 487 PL_scopestack_max = 16;
474 488
475 New(54,PL_savestack,64,ANY); 489 New(54,PL_savestack,64,ANY);
476 PL_savestack_ix = 0; 490 PL_savestack_ix = 0;
477 PL_savestack_max = 64; 491 PL_savestack_max = 64;
478 492
479#if !PERL_VERSION_ATLEAST (5,9,0) 493#if !PERL_VERSION_ATLEAST (5,9,0)
480 New(54,PL_retstack,16,OP*); 494 New(54,PL_retstack,4,OP*);
481 PL_retstack_ix = 0; 495 PL_retstack_ix = 0;
482 PL_retstack_max = 16; 496 PL_retstack_max = 4;
483#endif 497#endif
484} 498}
485#endif 499#endif
486 500
487/* 501/*
527 Safefree (PL_scopestack); 541 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 542 Safefree (PL_savestack);
529#if !PERL_VERSION_ATLEAST (5,9,0) 543#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 544 Safefree (PL_retstack);
531#endif 545#endif
546}
547
548static size_t
549coro_rss (struct coro *coro)
550{
551 size_t rss = sizeof (coro);
552
553 if (coro->mainstack)
554 {
555 if (coro->flags & CF_RUNNING)
556 {
557 #define VAR(name,type)coro->name = PL_ ## name;
558 # include "state.h"
559 #undef VAR
560 }
561
562 rss += sizeof (coro->curstackinfo);
563 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
564 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
565 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
566 rss += coro->tmps_max * sizeof (SV *);
567 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
568 rss += coro->scopestack_max * sizeof (I32);
569 rss += coro->savestack_max * sizeof (ANY);
570
571#if !PERL_VERSION_ATLEAST (5,9,0)
572 rss += coro->retstack_max * sizeof (OP *);
573#endif
574 }
575
576 return rss;
532} 577}
533 578
534/** coroutine stack handling ************************************************/ 579/** coroutine stack handling ************************************************/
535 580
536static void 581static void
559 Zero (&myop, 1, LOGOP); 604 Zero (&myop, 1, LOGOP);
560 myop.op_next = Nullop; 605 myop.op_next = Nullop;
561 myop.op_flags = OPf_WANT_VOID; 606 myop.op_flags = OPf_WANT_VOID;
562 607
563 PUSHMARK (SP); 608 PUSHMARK (SP);
564 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 609 XPUSHs (av_shift (GvAV (PL_defgv)));
565 PUTBACK; 610 PUTBACK;
566 PL_op = (OP *)&myop; 611 PL_op = (OP *)&myop;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 612 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 613 SPAGAIN;
569 } 614 }
580 coro_mortal = 0; 625 coro_mortal = 0;
581 } 626 }
582} 627}
583 628
584/* inject a fake call to Coro::State::_cctx_init into the execution */ 629/* inject a fake call to Coro::State::_cctx_init into the execution */
630/* _cctx_init should be careful, as it could be called at almost any time */
631/* during execution of a perl program */
585static void NOINLINE 632static void NOINLINE
586prepare_cctx (pTHX_ coro_cctx *cctx) 633prepare_cctx (pTHX_ coro_cctx *cctx)
587{ 634{
588 dSP; 635 dSP;
589 LOGOP myop; 636 LOGOP myop;
600 PL_op = (OP *)&myop; 647 PL_op = (OP *)&myop;
601 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 648 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 649 SPAGAIN;
603} 650}
604 651
652/*
653 * this is a _very_ stripped down perl interpreter ;)
654 */
605static void 655static void
606coro_run (void *arg) 656coro_run (void *arg)
607{ 657{
608 dTHX; 658 dTHX;
609 659
610 /* coro_run is the alternative tail of transfer(), so unlock here. */ 660 /* coro_run is the alternative tail of transfer(), so unlock here. */
611 UNLOCK; 661 UNLOCK;
612 662
613 /*
614 * this is a _very_ stripped down perl interpreter ;)
615 */
616 PL_top_env = &PL_start_env; 663 PL_top_env = &PL_start_env;
617 664
618 /* inject call to cctx_init */ 665 /* inject a fake subroutine call to cctx_init */
619 prepare_cctx (aTHX_ (coro_cctx *)arg); 666 prepare_cctx (aTHX_ (coro_cctx *)arg);
620 667
621 /* somebody will hit me for both perl_run and PL_restartop */ 668 /* somebody or something will hit me for both perl_run and PL_restartop */
622 PL_restartop = PL_op; 669 PL_restartop = PL_op;
623 perl_run (PL_curinterp); 670 perl_run (PL_curinterp);
624 671
625 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 672 /*
626 abort (); 673 * If perl-run returns we assume exit() was being called or the coro
674 * fell off the end, which seems to be the only valid (non-bug)
675 * reason for perl_run to return. We try to exit by jumping to the
676 * bootstrap-time "top" top_env, as we cannot restore the "main"
677 * coroutine as Coro has no such concept
678 */
679 PL_top_env = main_top_env;
680 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
627} 681}
628 682
629static coro_cctx * 683static coro_cctx *
630cctx_new () 684cctx_new ()
631{ 685{
878 } 932 }
879 933
880 cctx_destroy (coro->cctx); 934 cctx_destroy (coro->cctx);
881 SvREFCNT_dec (coro->args); 935 SvREFCNT_dec (coro->args);
882 936
937 if (coro->next) coro->next->prev = coro->prev;
938 if (coro->prev) coro->prev->next = coro->next;
939 if (coro == coro_first) coro_first = coro->next;
940
883 return 1; 941 return 1;
884} 942}
885 943
886static int 944static int
887coro_state_free (pTHX_ SV *sv, MAGIC *mg) 945coro_state_free (pTHX_ SV *sv, MAGIC *mg)
888{ 946{
889 struct coro *coro = (struct coro *)mg->mg_ptr; 947 struct coro *coro = (struct coro *)mg->mg_ptr;
890 mg->mg_ptr = 0; 948 mg->mg_ptr = 0;
949
950 coro->hv = 0;
891 951
892 if (--coro->refcnt < 0) 952 if (--coro->refcnt < 0)
893 { 953 {
894 coro_state_destroy (aTHX_ coro); 954 coro_state_destroy (aTHX_ coro);
895 Safefree (coro); 955 Safefree (coro);
1163 1223
1164 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1224 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1165 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1225 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1166 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1226 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1167 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1227 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1228 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1229 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1168 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1230 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1169 1231
1170 main_mainstack = PL_mainstack; 1232 main_mainstack = PL_mainstack;
1233 main_top_env = PL_top_env;
1234
1235 while (main_top_env->je_prev)
1236 main_top_env = main_top_env->je_prev;
1171 1237
1172 coroapi.ver = CORO_API_VERSION; 1238 coroapi.ver = CORO_API_VERSION;
1173 coroapi.transfer = api_transfer; 1239 coroapi.transfer = api_transfer;
1174 1240
1175 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1241 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1183 HV *hv; 1249 HV *hv;
1184 int i; 1250 int i;
1185 1251
1186 Newz (0, coro, 1, struct coro); 1252 Newz (0, coro, 1, struct coro);
1187 coro->args = newAV (); 1253 coro->args = newAV ();
1188 coro->save = CORO_SAVE_ALL; 1254 coro->save = CORO_SAVE_DEF;
1189 coro->flags = CF_NEW; 1255 coro->flags = CF_NEW;
1190 1256
1257 if (coro_first) coro_first->prev = coro;
1258 coro->next = coro_first;
1259 coro_first = coro;
1260
1191 hv = newHV (); 1261 coro->hv = hv = newHV ();
1192 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1262 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1193 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1263 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1194 1264
1195 for (i = 1; i < items; i++) 1265 for (i = 1; i < items; i++)
1196 av_push (coro->args, newSVsv (ST (i))); 1266 av_push (coro->args, newSVsv (ST (i)));
1200 1270
1201int 1271int
1202save (SV *coro, int new_save = -1) 1272save (SV *coro, int new_save = -1)
1203 CODE: 1273 CODE:
1204 RETVAL = api_save (coro, new_save); 1274 RETVAL = api_save (coro, new_save);
1275 OUTPUT:
1276 RETVAL
1277
1278int
1279save_also (SV *coro_sv, int save_also)
1280 CODE:
1281{
1282 struct coro *coro = SvSTATE (coro_sv);
1283 RETVAL = coro->save;
1284 coro->save |= save_also;
1285}
1205 OUTPUT: 1286 OUTPUT:
1206 RETVAL 1287 RETVAL
1207 1288
1208void 1289void
1209_set_stacklevel (...) 1290_set_stacklevel (...)
1287 CODE: 1368 CODE:
1288 RETVAL = cctx_idle; 1369 RETVAL = cctx_idle;
1289 OUTPUT: 1370 OUTPUT:
1290 RETVAL 1371 RETVAL
1291 1372
1373void
1374list ()
1375 PPCODE:
1376{
1377 struct coro *coro;
1378 for (coro = coro_first; coro; coro = coro->next)
1379 if (coro->hv)
1380 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1381}
1382
1383void
1384call (Coro::State coro, SV *coderef)
1385 ALIAS:
1386 eval = 1
1387 CODE:
1388{
1389 if (coro->mainstack)
1390 {
1391 struct coro temp;
1392 Zero (&temp, 1, struct coro);
1393 temp.save = CORO_SAVE_ALL;
1394
1395 if (!(coro->flags & CF_RUNNING))
1396 {
1397 save_perl (aTHX_ &temp);
1398 load_perl (aTHX_ coro);
1399 }
1400
1401 {
1402 dSP;
1403 ENTER;
1404 SAVETMPS;
1405 PUSHMARK (SP);
1406 PUTBACK;
1407 if (ix)
1408 eval_sv (coderef, 0);
1409 else
1410 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1411 SPAGAIN;
1412 FREETMPS;
1413 LEAVE;
1414 PUTBACK;
1415 }
1416
1417 if (!(coro->flags & CF_RUNNING))
1418 {
1419 save_perl (aTHX_ coro);
1420 load_perl (aTHX_ &temp);
1421 }
1422 }
1423}
1424
1425SV *
1426is_ready (Coro::State coro)
1427 PROTOTYPE: $
1428 ALIAS:
1429 is_ready = CF_READY
1430 is_running = CF_RUNNING
1431 is_new = CF_NEW
1432 is_destroyed = CF_DESTROYED
1433 CODE:
1434 RETVAL = boolSV (coro->flags & ix);
1435 OUTPUT:
1436 RETVAL
1437
1438SV *
1439has_stack (Coro::State coro)
1440 PROTOTYPE: $
1441 CODE:
1442 RETVAL = boolSV (!!coro->cctx);
1443 OUTPUT:
1444 RETVAL
1445
1446IV
1447rss (Coro::State coro)
1448 PROTOTYPE: $
1449 CODE:
1450 RETVAL = coro_rss (coro);
1451 OUTPUT:
1452 RETVAL
1453
1454
1292MODULE = Coro::State PACKAGE = Coro 1455MODULE = Coro::State PACKAGE = Coro
1293 1456
1294BOOT: 1457BOOT:
1295{ 1458{
1296 int i; 1459 int i;
1460
1461 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1462 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1463 av_async_pool = get_av ("Coro::async_pool", TRUE);
1464
1465 coro_current = get_sv ("Coro::current", FALSE);
1466 SvREADONLY_on (coro_current);
1297 1467
1298 coro_stash = gv_stashpv ("Coro", TRUE); 1468 coro_stash = gv_stashpv ("Coro", TRUE);
1299 1469
1300 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1470 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1301 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1471 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1302 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1472 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1303 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1473 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1304 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1474 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1305 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1475 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1306
1307 coro_current = get_sv ("Coro::current", FALSE);
1308 SvREADONLY_on (coro_current);
1309 1476
1310 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1477 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1311 coro_ready[i] = newAV (); 1478 coro_ready[i] = newAV ();
1312 1479
1313 { 1480 {
1363 CODE: 1530 CODE:
1364 RETVAL = boolSV (api_ready (self)); 1531 RETVAL = boolSV (api_ready (self));
1365 OUTPUT: 1532 OUTPUT:
1366 RETVAL 1533 RETVAL
1367 1534
1368SV *
1369is_ready (SV *self)
1370 PROTOTYPE: $
1371 CODE:
1372 RETVAL = boolSV (api_is_ready (self));
1373 OUTPUT:
1374 RETVAL
1375
1376int 1535int
1377nready (...) 1536nready (...)
1378 PROTOTYPE: 1537 PROTOTYPE:
1379 CODE: 1538 CODE:
1380 RETVAL = coro_nready; 1539 RETVAL = coro_nready;
1381 OUTPUT: 1540 OUTPUT:
1382 RETVAL 1541 RETVAL
1383 1542
1543# for async_pool speedup
1544void
1545_pool_1 (SV *cb)
1546 CODE:
1547{
1548 int i, len;
1549 HV *hv = (HV *)SvRV (coro_current);
1550 AV *defav = GvAV (PL_defgv);
1551 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1552 AV *invoke_av;
1553
1554 if (!invoke)
1555 croak ("\3terminate\2\n");
1556
1557 hv_store (hv, "desc", sizeof ("desc") - 1,
1558 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1559
1560 invoke_av = (AV *)SvRV (invoke);
1561 len = av_len (invoke_av);
1562
1563 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1564
1565 if (len > 0)
1566 {
1567 av_fill (defav, len - 1);
1568 for (i = 0; i < len; ++i)
1569 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1570 }
1571
1572 SvREFCNT_dec (invoke);
1573}
1574
1575void
1576_pool_2 (SV *cb)
1577 CODE:
1578{
1579 struct coro *coro = SvSTATE (coro_current);
1580
1581 sv_setsv (cb, &PL_sv_undef);
1582
1583 if (coro_rss (coro) > SvIV (sv_pool_rss)
1584 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1585 croak ("\3terminate\2\n");
1586
1587 av_clear (GvAV (PL_defgv));
1588 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1589 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1590 coro->save = CORO_SAVE_DEF;
1591 coro->prio = 0;
1592 av_push (av_async_pool, newSVsv (coro_current));
1593}
1594
1595
1384MODULE = Coro::State PACKAGE = Coro::AIO 1596MODULE = Coro::State PACKAGE = Coro::AIO
1385 1597
1386SV * 1598SV *
1387_get_state () 1599_get_state ()
1388 CODE: 1600 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines