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.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
188# include "state.h" 187# include "state.h"
189#undef VAR 188#undef VAR
190 189
191 /* coro process data */ 190 /* coro process data */
192 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 */
193}; 196};
194 197
195typedef struct coro *Coro__State; 198typedef struct coro *Coro__State;
196typedef struct coro *Coro__State_or_hashref; 199typedef struct coro *Coro__State_or_hashref;
197 200
206 209
207/* for Coro.pm */ 210/* for Coro.pm */
208static SV *coro_current; 211static SV *coro_current;
209static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 212static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
210static int coro_nready; 213static int coro_nready;
214static struct coro *coro_first;
211 215
212/** lowlevel stuff **********************************************************/ 216/** lowlevel stuff **********************************************************/
213 217
214static AV * 218static AV *
215coro_clone_padlist (pTHX_ CV *cv) 219coro_clone_padlist (pTHX_ CV *cv)
374 CvPADLIST (cv) = (AV *)POPs; 378 CvPADLIST (cv) = (AV *)POPs;
375 } 379 }
376 380
377 PUTBACK; 381 PUTBACK;
378 } 382 }
379 assert (!PL_comppad || AvARRAY (PL_comppad));//D
380} 383}
381 384
382static void 385static void
383save_perl (pTHX_ Coro__State c) 386save_perl (pTHX_ Coro__State c)
384{ 387{
385 assert (!PL_comppad || AvARRAY (PL_comppad));//D
386 { 388 {
387 dSP; 389 dSP;
388 I32 cxix = cxstack_ix; 390 I32 cxix = cxstack_ix;
389 PERL_CONTEXT *ccstk = cxstack; 391 PERL_CONTEXT *ccstk = cxstack;
390 PERL_SI *top_si = PL_curstackinfo; 392 PERL_SI *top_si = PL_curstackinfo;
392 /* 394 /*
393 * 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
394 * (and reinitialize) all cv's in the whole callchain :( 396 * (and reinitialize) all cv's in the whole callchain :(
395 */ 397 */
396 398
397 EXTEND (SP, 3 + 1);
398 PUSHs (Nullsv); 399 XPUSHs (Nullsv);
399 /* this loop was inspired by pp_caller */ 400 /* this loop was inspired by pp_caller */
400 for (;;) 401 for (;;)
401 { 402 {
402 while (cxix >= 0) 403 while (cxix >= 0)
403 { 404 {
404 PERL_CONTEXT *cx = &ccstk[cxix--]; 405 PERL_CONTEXT *cx = &ccstk[cxix--];
405 406
406 if (CxTYPE (cx) == CXt_SUB) 407 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
407 { 408 {
408 CV *cv = cx->blk_sub.cv; 409 CV *cv = cx->blk_sub.cv;
409 410
410 if (CvDEPTH (cv)) 411 if (CvDEPTH (cv))
411 { 412 {
422 423
423 if (top_si->si_type == PERLSI_MAIN) 424 if (top_si->si_type == PERLSI_MAIN)
424 break; 425 break;
425 426
426 top_si = top_si->si_prev; 427 top_si = top_si->si_prev;
427 ccstk = top_si->si_cxstack; 428 ccstk = top_si->si_cxstack;
428 cxix = top_si->si_cxix; 429 cxix = top_si->si_cxix;
429 } 430 }
430 431
431 PUTBACK; 432 PUTBACK;
432 } 433 }
433 434
452# define coro_init_stacks init_stacks 453# define coro_init_stacks init_stacks
453#else 454#else
454static void 455static void
455coro_init_stacks (pTHX) 456coro_init_stacks (pTHX)
456{ 457{
457 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 458 PL_curstackinfo = new_stackinfo(64, 6);
458 PL_curstackinfo->si_type = PERLSI_MAIN; 459 PL_curstackinfo->si_type = PERLSI_MAIN;
459 PL_curstack = PL_curstackinfo->si_stack; 460 PL_curstack = PL_curstackinfo->si_stack;
460 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 461 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
461 462
462 PL_stack_base = AvARRAY(PL_curstack); 463 PL_stack_base = AvARRAY(PL_curstack);
463 PL_stack_sp = PL_stack_base; 464 PL_stack_sp = PL_stack_base;
464 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 465 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
465 466
466 New(50,PL_tmps_stack,128,SV*); 467 New(50,PL_tmps_stack,64,SV*);
467 PL_tmps_floor = -1; 468 PL_tmps_floor = -1;
468 PL_tmps_ix = -1; 469 PL_tmps_ix = -1;
469 PL_tmps_max = 128; 470 PL_tmps_max = 64;
470 471
471 New(54,PL_markstack,32,I32); 472 New(54,PL_markstack,16,I32);
472 PL_markstack_ptr = PL_markstack; 473 PL_markstack_ptr = PL_markstack;
473 PL_markstack_max = PL_markstack + 32; 474 PL_markstack_max = PL_markstack + 16;
474 475
475#ifdef SET_MARK_OFFSET 476#ifdef SET_MARK_OFFSET
476 SET_MARK_OFFSET; 477 SET_MARK_OFFSET;
477#endif 478#endif
478 479
479 New(54,PL_scopestack,32,I32); 480 New(54,PL_scopestack,16,I32);
480 PL_scopestack_ix = 0; 481 PL_scopestack_ix = 0;
481 PL_scopestack_max = 32; 482 PL_scopestack_max = 16;
482 483
483 New(54,PL_savestack,64,ANY); 484 New(54,PL_savestack,64,ANY);
484 PL_savestack_ix = 0; 485 PL_savestack_ix = 0;
485 PL_savestack_max = 64; 486 PL_savestack_max = 64;
486 487
487#if !PERL_VERSION_ATLEAST (5,9,0) 488#if !PERL_VERSION_ATLEAST (5,9,0)
488 New(54,PL_retstack,16,OP*); 489 New(54,PL_retstack,4,OP*);
489 PL_retstack_ix = 0; 490 PL_retstack_ix = 0;
490 PL_retstack_max = 16; 491 PL_retstack_max = 4;
491#endif 492#endif
492} 493}
493#endif 494#endif
494 495
495/* 496/*
535 Safefree (PL_scopestack); 536 Safefree (PL_scopestack);
536 Safefree (PL_savestack); 537 Safefree (PL_savestack);
537#if !PERL_VERSION_ATLEAST (5,9,0) 538#if !PERL_VERSION_ATLEAST (5,9,0)
538 Safefree (PL_retstack); 539 Safefree (PL_retstack);
539#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;
540} 572}
541 573
542/** coroutine stack handling ************************************************/ 574/** coroutine stack handling ************************************************/
543 575
544static void 576static void
567 Zero (&myop, 1, LOGOP); 599 Zero (&myop, 1, LOGOP);
568 myop.op_next = Nullop; 600 myop.op_next = Nullop;
569 myop.op_flags = OPf_WANT_VOID; 601 myop.op_flags = OPf_WANT_VOID;
570 602
571 PUSHMARK (SP); 603 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 604 XPUSHs (av_shift (GvAV (PL_defgv)));
573 PUTBACK; 605 PUTBACK;
574 PL_op = (OP *)&myop; 606 PL_op = (OP *)&myop;
575 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 607 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
576 SPAGAIN; 608 SPAGAIN;
577 } 609 }
588 coro_mortal = 0; 620 coro_mortal = 0;
589 } 621 }
590} 622}
591 623
592/* 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 */
593/* _cctx_init shoukld be careful, as it could be called at almost any time */ 625/* _cctx_init should be careful, as it could be called at almost any time */
594/* during execution of a pelr program */ 626/* during execution of a perl program */
595static void NOINLINE 627static void NOINLINE
596prepare_cctx (pTHX_ coro_cctx *cctx) 628prepare_cctx (pTHX_ coro_cctx *cctx)
597{ 629{
598 dSP; 630 dSP;
599 LOGOP myop; 631 LOGOP myop;
630 662
631 /* somebody or something will hit me for both perl_run and PL_restartop */ 663 /* somebody or something will hit me for both perl_run and PL_restartop */
632 PL_restartop = PL_op; 664 PL_restartop = PL_op;
633 perl_run (PL_curinterp); 665 perl_run (PL_curinterp);
634 666
667 /*
635 /* If perl-run returns we assume exit() was being called, which */ 668 * 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. */ 669 * 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 */ 670 * 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 */ 671 * bootstrap-time "top" top_env, as we cannot restore the "main"
672 * coroutine as Coro has no such concept
673 */
639 PL_top_env = main_top_env; 674 PL_top_env = main_top_env;
640 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 675 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
641} 676}
642 677
643static coro_cctx * 678static coro_cctx *
874 struct coro temp; 909 struct coro temp;
875 910
876 assert (!(coro->flags & CF_RUNNING)); 911 assert (!(coro->flags & CF_RUNNING));
877 912
878 Zero (&temp, 1, struct coro); 913 Zero (&temp, 1, struct coro);
879 temp.save = CORO_SAVE_DEF; 914 temp.save = CORO_SAVE_ALL;
880 915
881 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
882 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
883 918
884 save_perl (aTHX_ &temp); 919 save_perl (aTHX_ &temp);
892 } 927 }
893 928
894 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
895 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
896 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
897 return 1; 936 return 1;
898} 937}
899 938
900static int 939static int
901coro_state_free (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
902{ 941{
903 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
904 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
944
945 coro->hv = 0;
905 946
906 if (--coro->refcnt < 0) 947 if (--coro->refcnt < 0)
907 { 948 {
908 coro_state_destroy (aTHX_ coro); 949 coro_state_destroy (aTHX_ coro);
909 Safefree (coro); 950 Safefree (coro);
1206 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1207 coro->args = newAV (); 1248 coro->args = newAV ();
1208 coro->save = CORO_SAVE_DEF; 1249 coro->save = CORO_SAVE_DEF;
1209 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1210 1251
1252 if (coro_first) coro_first->prev = coro;
1253 coro->next = coro_first;
1254 coro_first = coro;
1255
1211 hv = newHV (); 1256 coro->hv = hv = newHV ();
1212 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;
1213 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1214 1259
1215 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1216 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1318 CODE: 1363 CODE:
1319 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1320 OUTPUT: 1365 OUTPUT:
1321 RETVAL 1366 RETVAL
1322 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
1323MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1324 1438
1325BOOT: 1439BOOT:
1326{ 1440{
1327 int i; 1441 int i;
1394 CODE: 1508 CODE:
1395 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1396 OUTPUT: 1510 OUTPUT:
1397 RETVAL 1511 RETVAL
1398 1512
1399SV *
1400is_ready (SV *self)
1401 PROTOTYPE: $
1402 CODE:
1403 RETVAL = boolSV (api_is_ready (self));
1404 OUTPUT:
1405 RETVAL
1406
1407int 1513int
1408nready (...) 1514nready (...)
1409 PROTOTYPE: 1515 PROTOTYPE:
1410 CODE: 1516 CODE:
1411 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines