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.156 by root, Fri Sep 21 01:10:55 2007 UTC vs.
Revision 1.160 by root, Sat Sep 22 22:39:15 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
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 */
134static JMPENV *main_top_env; 133static JMPENV *main_top_env;
135static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
136static 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;
137 141
138static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
139static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
140 144
141/* this is a structure representing a c-level coroutine */ 145/* this is a structure representing a c-level coroutine */
395 /* 399 /*
396 * 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
397 * (and reinitialize) all cv's in the whole callchain :( 401 * (and reinitialize) all cv's in the whole callchain :(
398 */ 402 */
399 403
400 EXTEND (SP, 3 + 1);
401 PUSHs (Nullsv); 404 XPUSHs (Nullsv);
402 /* this loop was inspired by pp_caller */ 405 /* this loop was inspired by pp_caller */
403 for (;;) 406 for (;;)
404 { 407 {
405 while (cxix >= 0) 408 while (cxix >= 0)
406 { 409 {
425 428
426 if (top_si->si_type == PERLSI_MAIN) 429 if (top_si->si_type == PERLSI_MAIN)
427 break; 430 break;
428 431
429 top_si = top_si->si_prev; 432 top_si = top_si->si_prev;
430 ccstk = top_si->si_cxstack; 433 ccstk = top_si->si_cxstack;
431 cxix = top_si->si_cxix; 434 cxix = top_si->si_cxix;
432 } 435 }
433 436
434 PUTBACK; 437 PUTBACK;
435 } 438 }
436 439
1440 1443
1441BOOT: 1444BOOT:
1442{ 1445{
1443 int i; 1446 int i;
1444 1447
1448 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1449 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1450 av_async_pool = get_av ("Coro::async_pool", TRUE);
1451
1452 coro_current = get_sv ("Coro::current", FALSE);
1453 SvREADONLY_on (coro_current);
1454
1445 coro_stash = gv_stashpv ("Coro", TRUE); 1455 coro_stash = gv_stashpv ("Coro", TRUE);
1446 1456
1447 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1457 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1448 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1458 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1449 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1459 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1450 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1460 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1451 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1461 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1452 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1462 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1453
1454 coro_current = get_sv ("Coro::current", FALSE);
1455 SvREADONLY_on (coro_current);
1456 1463
1457 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1464 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1458 coro_ready[i] = newAV (); 1465 coro_ready[i] = newAV ();
1459 1466
1460 { 1467 {
1518 CODE: 1525 CODE:
1519 RETVAL = coro_nready; 1526 RETVAL = coro_nready;
1520 OUTPUT: 1527 OUTPUT:
1521 RETVAL 1528 RETVAL
1522 1529
1530# for async_pool speedup
1531SV *
1532_pool_1 (...)
1533 CODE:
1534{
1535 int i, len;
1536 HV *hv = (HV *)SvRV (coro_current);
1537 AV *defav = GvAV (PL_defgv);
1538 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1539 AV *invoke_av;
1540
1541 if (!invoke)
1542 XSRETURN_EMPTY;
1543
1544 hv_store (hv, "desc", sizeof ("desc") - 1,
1545 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1546
1547 invoke_av = (AV *)SvRV (invoke);
1548 len = av_len (invoke_av);
1549
1550 RETVAL = SvREFCNT_inc (AvARRAY (invoke_av)[0]);
1551
1552 if (len > 0)
1553 {
1554 av_clear (defav);
1555 av_extend (defav, len);
1556 for (i = 0; i < len; ++i)
1557 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1558 }
1559
1560 SvREFCNT_dec (invoke);
1561}
1562 OUTPUT:
1563 RETVAL
1564
1565void
1566_pool_2 (...)
1567 CODE:
1568{
1569 struct coro *coro = SvSTATE (coro_current);
1570
1571 if (coro_rss (coro) > SvIV (sv_pool_rss)
1572 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1573 XSRETURN_YES;
1574
1575 av_clear (GvAV (PL_defgv));
1576 hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1,
1577 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1578 coro->save = CORO_SAVE_DEF;
1579 coro->prio = 0;
1580 av_push (av_async_pool, newSVsv (coro_current));
1581
1582 XSRETURN_NO;
1583}
1584
1585
1523MODULE = Coro::State PACKAGE = Coro::AIO 1586MODULE = Coro::State PACKAGE = Coro::AIO
1524 1587
1525SV * 1588SV *
1526_get_state () 1589_get_state ()
1527 CODE: 1590 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines