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.147 by root, Mon Mar 19 15:50:48 2007 UTC vs.
Revision 1.158 by root, Fri Sep 21 02:23:48 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 136
137static struct coro_cctx *cctx_first; 137static struct coro_cctx *cctx_first;
138static int cctx_count, cctx_idle; 138static int cctx_count, cctx_idle;
187# include "state.h" 187# include "state.h"
188#undef VAR 188#undef VAR
189 189
190 /* coro process data */ 190 /* coro process data */
191 int prio; 191 int prio;
192
193 /* linked list */
194 struct coro *next, *prev;
195 HV *hv; /* the perl hash associated with this coro, if any */
192}; 196};
193 197
194typedef struct coro *Coro__State; 198typedef struct coro *Coro__State;
195typedef struct coro *Coro__State_or_hashref; 199typedef struct coro *Coro__State_or_hashref;
196 200
205 209
206/* for Coro.pm */ 210/* for Coro.pm */
207static SV *coro_current; 211static SV *coro_current;
208static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 212static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
209static int coro_nready; 213static int coro_nready;
214static struct coro *coro_first;
210 215
211/** lowlevel stuff **********************************************************/ 216/** lowlevel stuff **********************************************************/
212 217
213static AV * 218static AV *
214coro_clone_padlist (pTHX_ CV *cv) 219coro_clone_padlist (pTHX_ CV *cv)
373 CvPADLIST (cv) = (AV *)POPs; 378 CvPADLIST (cv) = (AV *)POPs;
374 } 379 }
375 380
376 PUTBACK; 381 PUTBACK;
377 } 382 }
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
379} 383}
380 384
381static void 385static void
382save_perl (pTHX_ Coro__State c) 386save_perl (pTHX_ Coro__State c)
383{ 387{
384 assert (!PL_comppad || AvARRAY (PL_comppad));//D
385 { 388 {
386 dSP; 389 dSP;
387 I32 cxix = cxstack_ix; 390 I32 cxix = cxstack_ix;
388 PERL_CONTEXT *ccstk = cxstack; 391 PERL_CONTEXT *ccstk = cxstack;
389 PERL_SI *top_si = PL_curstackinfo; 392 PERL_SI *top_si = PL_curstackinfo;
391 /* 394 /*
392 * the worst thing you can imagine happens first - we have to save 395 * the worst thing you can imagine happens first - we have to save
393 * (and reinitialize) all cv's in the whole callchain :( 396 * (and reinitialize) all cv's in the whole callchain :(
394 */ 397 */
395 398
396 EXTEND (SP, 3 + 1);
397 PUSHs (Nullsv); 399 XPUSHs (Nullsv);
398 /* this loop was inspired by pp_caller */ 400 /* this loop was inspired by pp_caller */
399 for (;;) 401 for (;;)
400 { 402 {
401 while (cxix >= 0) 403 while (cxix >= 0)
402 { 404 {
403 PERL_CONTEXT *cx = &ccstk[cxix--]; 405 PERL_CONTEXT *cx = &ccstk[cxix--];
404 406
405 if (CxTYPE (cx) == CXt_SUB) 407 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
406 { 408 {
407 CV *cv = cx->blk_sub.cv; 409 CV *cv = cx->blk_sub.cv;
408 410
409 if (CvDEPTH (cv)) 411 if (CvDEPTH (cv))
410 { 412 {
421 423
422 if (top_si->si_type == PERLSI_MAIN) 424 if (top_si->si_type == PERLSI_MAIN)
423 break; 425 break;
424 426
425 top_si = top_si->si_prev; 427 top_si = top_si->si_prev;
426 ccstk = top_si->si_cxstack; 428 ccstk = top_si->si_cxstack;
427 cxix = top_si->si_cxix; 429 cxix = top_si->si_cxix;
428 } 430 }
429 431
430 PUTBACK; 432 PUTBACK;
431 } 433 }
432 434
451# define coro_init_stacks init_stacks 453# define coro_init_stacks init_stacks
452#else 454#else
453static void 455static void
454coro_init_stacks (pTHX) 456coro_init_stacks (pTHX)
455{ 457{
456 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 458 PL_curstackinfo = new_stackinfo(64, 6);
457 PL_curstackinfo->si_type = PERLSI_MAIN; 459 PL_curstackinfo->si_type = PERLSI_MAIN;
458 PL_curstack = PL_curstackinfo->si_stack; 460 PL_curstack = PL_curstackinfo->si_stack;
459 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 461 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
460 462
461 PL_stack_base = AvARRAY(PL_curstack); 463 PL_stack_base = AvARRAY(PL_curstack);
462 PL_stack_sp = PL_stack_base; 464 PL_stack_sp = PL_stack_base;
463 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 465 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
464 466
465 New(50,PL_tmps_stack,128,SV*); 467 New(50,PL_tmps_stack,64,SV*);
466 PL_tmps_floor = -1; 468 PL_tmps_floor = -1;
467 PL_tmps_ix = -1; 469 PL_tmps_ix = -1;
468 PL_tmps_max = 128; 470 PL_tmps_max = 64;
469 471
470 New(54,PL_markstack,32,I32); 472 New(54,PL_markstack,16,I32);
471 PL_markstack_ptr = PL_markstack; 473 PL_markstack_ptr = PL_markstack;
472 PL_markstack_max = PL_markstack + 32; 474 PL_markstack_max = PL_markstack + 16;
473 475
474#ifdef SET_MARK_OFFSET 476#ifdef SET_MARK_OFFSET
475 SET_MARK_OFFSET; 477 SET_MARK_OFFSET;
476#endif 478#endif
477 479
478 New(54,PL_scopestack,32,I32); 480 New(54,PL_scopestack,16,I32);
479 PL_scopestack_ix = 0; 481 PL_scopestack_ix = 0;
480 PL_scopestack_max = 32; 482 PL_scopestack_max = 16;
481 483
482 New(54,PL_savestack,64,ANY); 484 New(54,PL_savestack,64,ANY);
483 PL_savestack_ix = 0; 485 PL_savestack_ix = 0;
484 PL_savestack_max = 64; 486 PL_savestack_max = 64;
485 487
486#if !PERL_VERSION_ATLEAST (5,9,0) 488#if !PERL_VERSION_ATLEAST (5,9,0)
487 New(54,PL_retstack,16,OP*); 489 New(54,PL_retstack,4,OP*);
488 PL_retstack_ix = 0; 490 PL_retstack_ix = 0;
489 PL_retstack_max = 16; 491 PL_retstack_max = 4;
490#endif 492#endif
491} 493}
492#endif 494#endif
493 495
494/* 496/*
534 Safefree (PL_scopestack); 536 Safefree (PL_scopestack);
535 Safefree (PL_savestack); 537 Safefree (PL_savestack);
536#if !PERL_VERSION_ATLEAST (5,9,0) 538#if !PERL_VERSION_ATLEAST (5,9,0)
537 Safefree (PL_retstack); 539 Safefree (PL_retstack);
538#endif 540#endif
541}
542
543static size_t
544coro_rss (struct coro *coro)
545{
546 size_t rss = sizeof (coro);
547
548 if (coro->mainstack)
549 {
550 if (coro->flags & CF_RUNNING)
551 {
552 #define VAR(name,type)coro->name = PL_ ## name;
553 # include "state.h"
554 #undef VAR
555 }
556
557 rss += sizeof (coro->curstackinfo);
558 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
559 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
560 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
561 rss += coro->tmps_max * sizeof (SV *);
562 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
563 rss += coro->scopestack_max * sizeof (I32);
564 rss += coro->savestack_max * sizeof (ANY);
565
566#if !PERL_VERSION_ATLEAST (5,9,0)
567 rss += coro->retstack_max * sizeof (OP *);
568#endif
569 }
570
571 return rss;
539} 572}
540 573
541/** coroutine stack handling ************************************************/ 574/** coroutine stack handling ************************************************/
542 575
543static void 576static void
566 Zero (&myop, 1, LOGOP); 599 Zero (&myop, 1, LOGOP);
567 myop.op_next = Nullop; 600 myop.op_next = Nullop;
568 myop.op_flags = OPf_WANT_VOID; 601 myop.op_flags = OPf_WANT_VOID;
569 602
570 PUSHMARK (SP); 603 PUSHMARK (SP);
571 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 604 XPUSHs (av_shift (GvAV (PL_defgv)));
572 PUTBACK; 605 PUTBACK;
573 PL_op = (OP *)&myop; 606 PL_op = (OP *)&myop;
574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 607 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
575 SPAGAIN; 608 SPAGAIN;
576 } 609 }
587 coro_mortal = 0; 620 coro_mortal = 0;
588 } 621 }
589} 622}
590 623
591/* inject a fake call to Coro::State::_cctx_init into the execution */ 624/* inject a fake call to Coro::State::_cctx_init into the execution */
625/* _cctx_init should be careful, as it could be called at almost any time */
626/* during execution of a perl program */
592static void NOINLINE 627static void NOINLINE
593prepare_cctx (pTHX_ coro_cctx *cctx) 628prepare_cctx (pTHX_ coro_cctx *cctx)
594{ 629{
595 dSP; 630 dSP;
596 LOGOP myop; 631 LOGOP myop;
607 PL_op = (OP *)&myop; 642 PL_op = (OP *)&myop;
608 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 643 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
609 SPAGAIN; 644 SPAGAIN;
610} 645}
611 646
647/*
648 * this is a _very_ stripped down perl interpreter ;)
649 */
612static void 650static void
613coro_run (void *arg) 651coro_run (void *arg)
614{ 652{
615 dTHX; 653 dTHX;
616 654
617 /* coro_run is the alternative tail of transfer(), so unlock here. */ 655 /* coro_run is the alternative tail of transfer(), so unlock here. */
618 UNLOCK; 656 UNLOCK;
619 657
620 /*
621 * this is a _very_ stripped down perl interpreter ;)
622 */
623 PL_top_env = &PL_start_env; 658 PL_top_env = &PL_start_env;
624 659
625 /* inject call to cctx_init */ 660 /* inject a fake subroutine call to cctx_init */
626 prepare_cctx (aTHX_ (coro_cctx *)arg); 661 prepare_cctx (aTHX_ (coro_cctx *)arg);
627 662
628 /* somebody will hit me for both perl_run and PL_restartop */ 663 /* somebody or something will hit me for both perl_run and PL_restartop */
629 PL_restartop = PL_op; 664 PL_restartop = PL_op;
630 perl_run (PL_curinterp); 665 perl_run (PL_curinterp);
631 666
632 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 667 /*
633 abort (); 668 * If perl-run returns we assume exit() was being called or the coro
669 * fell off the end, which seems to be the only valid (non-bug)
670 * reason for perl_run to return. We try to exit by jumping to the
671 * bootstrap-time "top" top_env, as we cannot restore the "main"
672 * coroutine as Coro has no such concept
673 */
674 PL_top_env = main_top_env;
675 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
634} 676}
635 677
636static coro_cctx * 678static coro_cctx *
637cctx_new () 679cctx_new ()
638{ 680{
867 struct coro temp; 909 struct coro temp;
868 910
869 assert (!(coro->flags & CF_RUNNING)); 911 assert (!(coro->flags & CF_RUNNING));
870 912
871 Zero (&temp, 1, struct coro); 913 Zero (&temp, 1, struct coro);
872 temp.save = CORO_SAVE_DEF; 914 temp.save = CORO_SAVE_ALL;
873 915
874 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
875 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
876 918
877 save_perl (aTHX_ &temp); 919 save_perl (aTHX_ &temp);
885 } 927 }
886 928
887 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
888 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
889 931
932 if (coro->next) coro->next->prev = coro->prev;
933 if (coro->prev) coro->prev->next = coro->next;
934 if (coro == coro_first) coro_first = coro->next;
935
890 return 1; 936 return 1;
891} 937}
892 938
893static int 939static int
894coro_state_free (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
895{ 941{
896 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
897 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
944
945 coro->hv = 0;
898 946
899 if (--coro->refcnt < 0) 947 if (--coro->refcnt < 0)
900 { 948 {
901 coro_state_destroy (aTHX_ coro); 949 coro_state_destroy (aTHX_ coro);
902 Safefree (coro); 950 Safefree (coro);
1175 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1223 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1176 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); 1224 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1177 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1225 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1178 1226
1179 main_mainstack = PL_mainstack; 1227 main_mainstack = PL_mainstack;
1228 main_top_env = PL_top_env;
1229
1230 while (main_top_env->je_prev)
1231 main_top_env = main_top_env->je_prev;
1180 1232
1181 coroapi.ver = CORO_API_VERSION; 1233 coroapi.ver = CORO_API_VERSION;
1182 coroapi.transfer = api_transfer; 1234 coroapi.transfer = api_transfer;
1183 1235
1184 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1236 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1195 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1196 coro->args = newAV (); 1248 coro->args = newAV ();
1197 coro->save = CORO_SAVE_DEF; 1249 coro->save = CORO_SAVE_DEF;
1198 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1199 1251
1252 if (coro_first) coro_first->prev = coro;
1253 coro->next = coro_first;
1254 coro_first = coro;
1255
1200 hv = newHV (); 1256 coro->hv = hv = newHV ();
1201 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1257 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1202 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1203 1259
1204 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1205 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1307 CODE: 1363 CODE:
1308 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1309 OUTPUT: 1365 OUTPUT:
1310 RETVAL 1366 RETVAL
1311 1367
1368void
1369list ()
1370 PPCODE:
1371{
1372 struct coro *coro;
1373 for (coro = coro_first; coro; coro = coro->next)
1374 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376}
1377
1378void
1379_eval (Coro::State coro, SV *coderef)
1380 CODE:
1381{
1382 if (coro->mainstack)
1383 {
1384 struct coro temp;
1385 Zero (&temp, 1, struct coro);
1386 temp.save = CORO_SAVE_ALL;
1387
1388 if (!(coro->flags & CF_RUNNING))
1389 {
1390 save_perl (aTHX_ &temp);
1391 load_perl (aTHX_ coro);
1392 }
1393
1394 {
1395 dSP;
1396 ENTER;
1397 SAVETMPS;
1398 PUSHMARK (SP);
1399 PUTBACK;
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN;
1402 FREETMPS;
1403 LEAVE;
1404 PUTBACK;
1405 }
1406
1407 if (!(coro->flags & CF_RUNNING))
1408 {
1409 save_perl (aTHX_ coro);
1410 load_perl (aTHX_ &temp);
1411 }
1412 }
1413}
1414
1415SV *
1416is_ready (Coro::State coro)
1417 PROTOTYPE: $
1418 ALIAS:
1419 is_ready = CF_READY
1420 is_running = CF_RUNNING
1421 is_new = CF_NEW
1422 is_destroyed = CF_DESTROYED
1423 CODE:
1424 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT:
1426 RETVAL
1427
1428IV
1429rss (Coro::State coro)
1430 PROTOTYPE: $
1431 CODE:
1432 RETVAL = coro_rss (coro);
1433 OUTPUT:
1434 RETVAL
1435
1436
1312MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1313 1438
1314BOOT: 1439BOOT:
1315{ 1440{
1316 int i; 1441 int i;
1383 CODE: 1508 CODE:
1384 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1385 OUTPUT: 1510 OUTPUT:
1386 RETVAL 1511 RETVAL
1387 1512
1388SV *
1389is_ready (SV *self)
1390 PROTOTYPE: $
1391 CODE:
1392 RETVAL = boolSV (api_is_ready (self));
1393 OUTPUT:
1394 RETVAL
1395
1396int 1513int
1397nready (...) 1514nready (...)
1398 PROTOTYPE: 1515 PROTOTYPE:
1399 CODE: 1516 CODE:
1400 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines