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.148 by root, Fri Apr 13 12:56:55 2007 UTC vs.
Revision 1.155 by root, Fri Sep 21 01:09:36 2007 UTC

188# include "state.h" 188# include "state.h"
189#undef VAR 189#undef VAR
190 190
191 /* coro process data */ 191 /* coro process data */
192 int prio; 192 int prio;
193
194 /* linked list */
195 struct coro *next, *prev;
196 HV *hv; /* the perl hash associated with this coro, if any */
193}; 197};
194 198
195typedef struct coro *Coro__State; 199typedef struct coro *Coro__State;
196typedef struct coro *Coro__State_or_hashref; 200typedef struct coro *Coro__State_or_hashref;
197 201
206 210
207/* for Coro.pm */ 211/* for Coro.pm */
208static SV *coro_current; 212static SV *coro_current;
209static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 213static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
210static int coro_nready; 214static int coro_nready;
215static struct coro *coro_first;
211 216
212/** lowlevel stuff **********************************************************/ 217/** lowlevel stuff **********************************************************/
213 218
214static AV * 219static AV *
215coro_clone_padlist (pTHX_ CV *cv) 220coro_clone_padlist (pTHX_ CV *cv)
374 CvPADLIST (cv) = (AV *)POPs; 379 CvPADLIST (cv) = (AV *)POPs;
375 } 380 }
376 381
377 PUTBACK; 382 PUTBACK;
378 } 383 }
379 assert (!PL_comppad || AvARRAY (PL_comppad));//D
380} 384}
381 385
382static void 386static void
383save_perl (pTHX_ Coro__State c) 387save_perl (pTHX_ Coro__State c)
384{ 388{
385 assert (!PL_comppad || AvARRAY (PL_comppad));//D
386 { 389 {
387 dSP; 390 dSP;
388 I32 cxix = cxstack_ix; 391 I32 cxix = cxstack_ix;
389 PERL_CONTEXT *ccstk = cxstack; 392 PERL_CONTEXT *ccstk = cxstack;
390 PERL_SI *top_si = PL_curstackinfo; 393 PERL_SI *top_si = PL_curstackinfo;
401 { 404 {
402 while (cxix >= 0) 405 while (cxix >= 0)
403 { 406 {
404 PERL_CONTEXT *cx = &ccstk[cxix--]; 407 PERL_CONTEXT *cx = &ccstk[cxix--];
405 408
406 if (CxTYPE (cx) == CXt_SUB) 409 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
407 { 410 {
408 CV *cv = cx->blk_sub.cv; 411 CV *cv = cx->blk_sub.cv;
409 412
410 if (CvDEPTH (cv)) 413 if (CvDEPTH (cv))
411 { 414 {
452# define coro_init_stacks init_stacks 455# define coro_init_stacks init_stacks
453#else 456#else
454static void 457static void
455coro_init_stacks (pTHX) 458coro_init_stacks (pTHX)
456{ 459{
457 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 460 PL_curstackinfo = new_stackinfo(64, 4);
458 PL_curstackinfo->si_type = PERLSI_MAIN; 461 PL_curstackinfo->si_type = PERLSI_MAIN;
459 PL_curstack = PL_curstackinfo->si_stack; 462 PL_curstack = PL_curstackinfo->si_stack;
460 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 463 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
461 464
462 PL_stack_base = AvARRAY(PL_curstack); 465 PL_stack_base = AvARRAY(PL_curstack);
463 PL_stack_sp = PL_stack_base; 466 PL_stack_sp = PL_stack_base;
464 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 467 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
465 468
466 New(50,PL_tmps_stack,128,SV*); 469 New(50,PL_tmps_stack,64,SV*);
467 PL_tmps_floor = -1; 470 PL_tmps_floor = -1;
468 PL_tmps_ix = -1; 471 PL_tmps_ix = -1;
469 PL_tmps_max = 128; 472 PL_tmps_max = 64;
470 473
471 New(54,PL_markstack,32,I32); 474 New(54,PL_markstack,16,I32);
472 PL_markstack_ptr = PL_markstack; 475 PL_markstack_ptr = PL_markstack;
473 PL_markstack_max = PL_markstack + 32; 476 PL_markstack_max = PL_markstack + 16;
474 477
475#ifdef SET_MARK_OFFSET 478#ifdef SET_MARK_OFFSET
476 SET_MARK_OFFSET; 479 SET_MARK_OFFSET;
477#endif 480#endif
478 481
479 New(54,PL_scopestack,32,I32); 482 New(54,PL_scopestack,16,I32);
480 PL_scopestack_ix = 0; 483 PL_scopestack_ix = 0;
481 PL_scopestack_max = 32; 484 PL_scopestack_max = 16;
482 485
483 New(54,PL_savestack,64,ANY); 486 New(54,PL_savestack,64,ANY);
484 PL_savestack_ix = 0; 487 PL_savestack_ix = 0;
485 PL_savestack_max = 64; 488 PL_savestack_max = 64;
486 489
487#if !PERL_VERSION_ATLEAST (5,9,0) 490#if !PERL_VERSION_ATLEAST (5,9,0)
488 New(54,PL_retstack,16,OP*); 491 New(54,PL_retstack,8,OP*);
489 PL_retstack_ix = 0; 492 PL_retstack_ix = 0;
490 PL_retstack_max = 16; 493 PL_retstack_max = 8;
491#endif 494#endif
492} 495}
493#endif 496#endif
494 497
495/* 498/*
535 Safefree (PL_scopestack); 538 Safefree (PL_scopestack);
536 Safefree (PL_savestack); 539 Safefree (PL_savestack);
537#if !PERL_VERSION_ATLEAST (5,9,0) 540#if !PERL_VERSION_ATLEAST (5,9,0)
538 Safefree (PL_retstack); 541 Safefree (PL_retstack);
539#endif 542#endif
543}
544
545static size_t
546coro_rss (struct coro *coro)
547{
548 size_t rss = sizeof (coro);
549
550 if (coro->mainstack)
551 {
552 if (coro->flags & CF_RUNNING)
553 {
554 #define VAR(name,type)coro->name = PL_ ## name;
555 # include "state.h"
556 #undef VAR
557 }
558
559 rss += sizeof (coro->curstackinfo);
560 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
561 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
562 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
563 rss += coro->tmps_max * sizeof (SV *);
564 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
565 rss += coro->scopestack_max * sizeof (I32);
566 rss += coro->savestack_max * sizeof (ANY);
567
568#if !PERL_VERSION_ATLEAST (5,9,0)
569 rss += coro->retstack_max * sizeof (OP *);
570#endif
571 }
572
573 return rss;
540} 574}
541 575
542/** coroutine stack handling ************************************************/ 576/** coroutine stack handling ************************************************/
543 577
544static void 578static void
567 Zero (&myop, 1, LOGOP); 601 Zero (&myop, 1, LOGOP);
568 myop.op_next = Nullop; 602 myop.op_next = Nullop;
569 myop.op_flags = OPf_WANT_VOID; 603 myop.op_flags = OPf_WANT_VOID;
570 604
571 PUSHMARK (SP); 605 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 606 XPUSHs (av_shift (GvAV (PL_defgv)));
573 PUTBACK; 607 PUTBACK;
574 PL_op = (OP *)&myop; 608 PL_op = (OP *)&myop;
575 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 609 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
576 SPAGAIN; 610 SPAGAIN;
577 } 611 }
588 coro_mortal = 0; 622 coro_mortal = 0;
589 } 623 }
590} 624}
591 625
592/* inject a fake call to Coro::State::_cctx_init into the execution */ 626/* inject a fake call to Coro::State::_cctx_init into the execution */
593/* _cctx_init shoukld be careful, as it could be called at almost any time */ 627/* _cctx_init should be careful, as it could be called at almost any time */
594/* during execution of a pelr program */ 628/* during execution of a perl program */
595static void NOINLINE 629static void NOINLINE
596prepare_cctx (pTHX_ coro_cctx *cctx) 630prepare_cctx (pTHX_ coro_cctx *cctx)
597{ 631{
598 dSP; 632 dSP;
599 LOGOP myop; 633 LOGOP myop;
630 664
631 /* somebody or something will hit me for both perl_run and PL_restartop */ 665 /* somebody or something will hit me for both perl_run and PL_restartop */
632 PL_restartop = PL_op; 666 PL_restartop = PL_op;
633 perl_run (PL_curinterp); 667 perl_run (PL_curinterp);
634 668
669 /*
635 /* If perl-run returns we assume exit() was being called, which */ 670 * If perl-run returns we assume exit() was being called or the coro
636 /* seems to be the only valid (non-bug) reason for perl_run to return. */ 671 * fell off the end, which seems to be the only valid (non-bug)
637 /* We try to exit by jumping to the bootstrap-time "top" top_env, as */ 672 * reason for perl_run to return. We try to exit by jumping to the
638 /* we cannot restore the "main" coroutine as Coro has no such concept */ 673 * bootstrap-time "top" top_env, as we cannot restore the "main"
674 * coroutine as Coro has no such concept
675 */
639 PL_top_env = main_top_env; 676 PL_top_env = main_top_env;
640 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 677 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
641} 678}
642 679
643static coro_cctx * 680static coro_cctx *
874 struct coro temp; 911 struct coro temp;
875 912
876 assert (!(coro->flags & CF_RUNNING)); 913 assert (!(coro->flags & CF_RUNNING));
877 914
878 Zero (&temp, 1, struct coro); 915 Zero (&temp, 1, struct coro);
879 temp.save = CORO_SAVE_DEF; 916 temp.save = CORO_SAVE_ALL;
880 917
881 if (coro->flags & CF_RUNNING) 918 if (coro->flags & CF_RUNNING)
882 croak ("FATAL: tried to destroy currently running coroutine"); 919 croak ("FATAL: tried to destroy currently running coroutine");
883 920
884 save_perl (aTHX_ &temp); 921 save_perl (aTHX_ &temp);
892 } 929 }
893 930
894 cctx_destroy (coro->cctx); 931 cctx_destroy (coro->cctx);
895 SvREFCNT_dec (coro->args); 932 SvREFCNT_dec (coro->args);
896 933
934 if (coro->next) coro->next->prev = coro->prev;
935 if (coro->prev) coro->prev->next = coro->next;
936 if (coro == coro_first) coro_first = coro->next;
937
897 return 1; 938 return 1;
898} 939}
899 940
900static int 941static int
901coro_state_free (pTHX_ SV *sv, MAGIC *mg) 942coro_state_free (pTHX_ SV *sv, MAGIC *mg)
902{ 943{
903 struct coro *coro = (struct coro *)mg->mg_ptr; 944 struct coro *coro = (struct coro *)mg->mg_ptr;
904 mg->mg_ptr = 0; 945 mg->mg_ptr = 0;
946
947 coro->hv = 0;
905 948
906 if (--coro->refcnt < 0) 949 if (--coro->refcnt < 0)
907 { 950 {
908 coro_state_destroy (aTHX_ coro); 951 coro_state_destroy (aTHX_ coro);
909 Safefree (coro); 952 Safefree (coro);
1206 Newz (0, coro, 1, struct coro); 1249 Newz (0, coro, 1, struct coro);
1207 coro->args = newAV (); 1250 coro->args = newAV ();
1208 coro->save = CORO_SAVE_DEF; 1251 coro->save = CORO_SAVE_DEF;
1209 coro->flags = CF_NEW; 1252 coro->flags = CF_NEW;
1210 1253
1254 if (coro_first) coro_first->prev = coro;
1255 coro->next = coro_first;
1256 coro_first = coro;
1257
1211 hv = newHV (); 1258 coro->hv = hv = newHV ();
1212 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1259 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1213 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1260 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1214 1261
1215 for (i = 1; i < items; i++) 1262 for (i = 1; i < items; i++)
1216 av_push (coro->args, newSVsv (ST (i))); 1263 av_push (coro->args, newSVsv (ST (i)));
1318 CODE: 1365 CODE:
1319 RETVAL = cctx_idle; 1366 RETVAL = cctx_idle;
1320 OUTPUT: 1367 OUTPUT:
1321 RETVAL 1368 RETVAL
1322 1369
1370void
1371list ()
1372 PPCODE:
1373{
1374 struct coro *coro;
1375 for (coro = coro_first; coro; coro = coro->next)
1376 if (coro->hv)
1377 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1378}
1379
1380void
1381_eval (Coro::State coro, SV *coderef)
1382 CODE:
1383{
1384 if (coro->mainstack)
1385 {
1386 struct coro temp;
1387 Zero (&temp, 1, struct coro);
1388 temp.save = CORO_SAVE_ALL;
1389
1390 if (!(coro->flags & CF_RUNNING))
1391 {
1392 save_perl (aTHX_ &temp);
1393 load_perl (aTHX_ coro);
1394 }
1395
1396 {
1397 dSP;
1398 ENTER;
1399 SAVETMPS;
1400 PUSHMARK (SP);
1401 PUTBACK;
1402 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1403 SPAGAIN;
1404 FREETMPS;
1405 LEAVE;
1406 PUTBACK;
1407 }
1408
1409 if (!(coro->flags & CF_RUNNING))
1410 {
1411 save_perl (aTHX_ coro);
1412 load_perl (aTHX_ &temp);
1413 }
1414 }
1415}
1416
1417SV *
1418is_ready (Coro::State coro)
1419 PROTOTYPE: $
1420 ALIAS:
1421 is_ready = CF_READY
1422 is_running = CF_RUNNING
1423 is_new = CF_NEW
1424 is_destroyed = CF_DESTROYED
1425 CODE:
1426 RETVAL = boolSV (coro->flags & ix);
1427 OUTPUT:
1428 RETVAL
1429
1430IV
1431rss (Coro::State coro)
1432 PROTOTYPE: $
1433 CODE:
1434 RETVAL = coro_rss (coro);
1435 OUTPUT:
1436 RETVAL
1437
1438
1323MODULE = Coro::State PACKAGE = Coro 1439MODULE = Coro::State PACKAGE = Coro
1324 1440
1325BOOT: 1441BOOT:
1326{ 1442{
1327 int i; 1443 int i;
1394 CODE: 1510 CODE:
1395 RETVAL = boolSV (api_ready (self)); 1511 RETVAL = boolSV (api_ready (self));
1396 OUTPUT: 1512 OUTPUT:
1397 RETVAL 1513 RETVAL
1398 1514
1399SV *
1400is_ready (SV *self)
1401 PROTOTYPE: $
1402 CODE:
1403 RETVAL = boolSV (api_is_ready (self));
1404 OUTPUT:
1405 RETVAL
1406
1407int 1515int
1408nready (...) 1516nready (...)
1409 PROTOTYPE: 1517 PROTOTYPE:
1410 CODE: 1518 CODE:
1411 RETVAL = coro_nready; 1519 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines