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.208 by root, Wed Oct 10 00:53:18 2007 UTC vs.
Revision 1.209 by root, Wed Oct 10 02:58:17 2007 UTC

10#include "patchlevel.h" 10#include "patchlevel.h"
11 11
12#include <stdio.h> 12#include <stdio.h>
13#include <errno.h> 13#include <errno.h>
14#include <assert.h> 14#include <assert.h>
15#include <inttypes.h> /* portable stdint.h */
15 16
16#ifdef HAVE_MMAP 17#ifdef HAVE_MMAP
17# include <unistd.h> 18# include <unistd.h>
18# include <sys/mman.h> 19# include <sys/mman.h>
19# ifndef MAP_ANONYMOUS 20# ifndef MAP_ANONYMOUS
160/* async_pool helper stuff */ 161/* async_pool helper stuff */
161static SV *sv_pool_rss; 162static SV *sv_pool_rss;
162static SV *sv_pool_size; 163static SV *sv_pool_size;
163static AV *av_async_pool; 164static AV *av_async_pool;
164 165
165static struct coro_cctx *cctx_first; 166static struct coro_cctx *cctx_first[3]; /* index by GIMME_V type, void, scalar, array */
166static int cctx_count, cctx_idle; 167static int cctx_count, cctx_idle[3];
167 168
168enum { 169enum {
169 CC_MAPPED = 0x01, 170 CC_MAPPED = 0x01,
170 CC_NOREUSE = 0x02, /* throw this away after tracing */ 171 CC_NOREUSE = 0x02, /* throw this away after tracing */
171 CC_TRACE = 0x04, 172 CC_TRACE = 0x04,
216 217
217/* this is a structure representing a perl-level coroutine */ 218/* this is a structure representing a perl-level coroutine */
218struct coro { 219struct coro {
219 /* the c coroutine allocated to this perl coroutine, if any */ 220 /* the c coroutine allocated to this perl coroutine, if any */
220 coro_cctx *cctx; 221 coro_cctx *cctx;
222 int gimme;
221 223
222 /* process data */ 224 /* process data */
223 AV *mainstack; 225 AV *mainstack;
224 perl_slots *slot; /* basically the saved sp */ 226 perl_slots *slot; /* basically the saved sp */
225 227
710 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 712 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
711 PUTBACK; 713 PUTBACK;
712 PL_op = (OP *)&myop; 714 PL_op = (OP *)&myop;
713 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 715 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
714 SPAGAIN; 716 SPAGAIN;
717
718 /*
719 * now its very tricky. the "tail" of the next transfer might end up
720 * either in a new cctx, or an existing one.
721 * in case of an existing one we have to take care of whatever
722 * entersub and transfer do to the perl stack.
723 */
724 ENTER;
725 EXTEND (SP, 4);
726 PUSHs ((SV *)0); /* items */
727 PUSHs ((SV *)0); /* ix, set_stacklevel */
728 PUSHs ((SV *)(sp - PL_stack_base + 1)); /* ax */
729 PUSHs ((SV *)0); /* again */
730 PUTBACK;
715 } 731 }
716
717 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */
718} 732}
719 733
720static void 734static void
721coro_destroy (pTHX_ struct coro *coro) 735coro_destroy (pTHX_ struct coro *coro)
722{ 736{
1018 1032
1019/* wether this cctx should be destructed */ 1033/* wether this cctx should be destructed */
1020#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1034#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
1021 1035
1022static coro_cctx * 1036static coro_cctx *
1023cctx_get (pTHX) 1037cctx_get (pTHX_ int gimme)
1024{ 1038{
1025 while (expect_true (cctx_first)) 1039 while (expect_true (cctx_first[gimme]))
1026 { 1040 {
1027 coro_cctx *cctx = cctx_first; 1041 coro_cctx *cctx = cctx_first[gimme];
1028 cctx_first = cctx->next; 1042 cctx_first[gimme] = cctx->next;
1029 --cctx_idle; 1043 --cctx_idle[gimme];
1030 1044
1031 if (expect_true (!CCTX_EXPIRED (cctx))) 1045 if (expect_true (!CCTX_EXPIRED (cctx)))
1032 return cctx; 1046 return cctx;
1033 1047
1034 cctx_destroy (cctx); 1048 cctx_destroy (cctx);
1035 } 1049 }
1036 1050
1051 assert (!gimme);
1037 return cctx_new (); 1052 return cctx_new ();
1038} 1053}
1039 1054
1040static void 1055static void
1041cctx_put (coro_cctx *cctx) 1056cctx_put (coro_cctx *cctx, int gimme)
1042{ 1057{
1043 /* free another cctx if overlimit */ 1058 /* free another cctx if overlimit */
1044 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1059 if (expect_false (cctx_idle[gimme] >= MAX_IDLE_CCTX))
1045 { 1060 {
1046 coro_cctx *first = cctx_first; 1061 coro_cctx *first = cctx_first[gimme];
1047 cctx_first = first->next; 1062 cctx_first[gimme] = first->next;
1048 --cctx_idle; 1063 --cctx_idle[gimme];
1049 1064
1050 cctx_destroy (first); 1065 cctx_destroy (first);
1051 } 1066 }
1052 1067
1053 ++cctx_idle; 1068 ++cctx_idle[gimme];
1054 cctx->next = cctx_first; 1069 cctx->next = cctx_first[gimme];
1055 cctx_first = cctx; 1070 cctx_first[gimme] = cctx;
1056} 1071}
1057 1072
1058/** coroutine switching *****************************************************/ 1073/** coroutine switching *****************************************************/
1059 1074
1060static void 1075static void
1139 1154
1140 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1155 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1141 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1156 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1142 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1157 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1143 if (!next->cctx) 1158 if (!next->cctx)
1144 next->cctx = cctx_get (aTHX); 1159 next->cctx = cctx_get (aTHX_ next->gimme);
1145 1160
1146 cctx_put (prev__cctx); 1161 cctx_put (prev__cctx, prev->gimme);
1147 } 1162 }
1148 1163
1149 ++next->usecount; 1164 ++next->usecount;
1150 1165
1151 if (expect_true (!next->cctx)) 1166 if (expect_true (!next->cctx))
1152 next->cctx = cctx_get (aTHX); 1167 next->cctx = cctx_get (aTHX_ next->gimme);
1153 1168
1154 if (expect_false (prev__cctx != next->cctx)) 1169 if (expect_false (prev__cctx != next->cctx))
1155 { 1170 {
1156 prev__cctx->top_env = PL_top_env; 1171 prev__cctx->top_env = PL_top_env;
1157 PL_top_env = next->cctx->top_env; 1172 PL_top_env = next->cctx->top_env;
1585 1600
1586 case 1: 1601 case 1:
1587 if (items != 2) 1602 if (items != 2)
1588 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1603 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1589 1604
1590 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1605 prepare_transfer (aTHX_ &ta, ST(0), ST(1));
1591 break; 1606 break;
1592 1607
1593 case 2: 1608 case 2:
1594 prepare_schedule (aTHX_ &ta); 1609 prepare_schedule (aTHX_ &ta);
1595 break; 1610 break;
1627 prepare_schedule (aTHX_ &ta); 1642 prepare_schedule (aTHX_ &ta);
1628 again = 1; 1643 again = 1;
1629 break; 1644 break;
1630 } 1645 }
1631 1646
1647 /* our caller, entersub, caches *only* this value */
1648 ta.prev->gimme = GIMME_V == G_VOID ? 0
1649 : GIMME_V == G_SCALAR ? 1
1650 : 2;
1651
1652 /* we need to save all local variables, as we might execute a different coroutine when transfer returns */
1653 sp += 2; /* save args */
1654 EXTEND (SP, 4);
1655 PUSHs ((SV *)(intptr_t)items);
1656 PUSHs ((SV *)(intptr_t)ix);
1657 PUSHs ((SV *)(intptr_t)ax);
1658 PUSHs ((SV *)(intptr_t)again);
1659 PUTBACK;
1632 BARRIER; 1660 BARRIER;
1633 TRANSFER (ta); 1661 TRANSFER (ta);
1634 BARRIER; 1662 BARRIER;
1663 SPAGAIN;
1664 again = (intptr_t)POPs;
1665 ax = (intptr_t)POPs;
1666 ix = (intptr_t)POPs;
1667 items = (intptr_t)POPs;
1668 sp -= 2; /* restore args */
1635 } 1669 }
1636 while (again); 1670 while (again);
1637 1671
1638 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1672 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1639 XSRETURN_YES; 1673 XSRETURN_YES;
1640 1674
1641 XSRETURN_EMPTY; /* not understood why this is necessary, likely some stack handling bug */ 1675 XSRETURN_EMPTY; /* not understood why this is necessary, likely some stack handling bug */
1676}
1642 1677
1643bool 1678bool
1644_destroy (SV *coro_sv) 1679_destroy (SV *coro_sv)
1645 CODE: 1680 CODE:
1646 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1681 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1671 RETVAL 1706 RETVAL
1672 1707
1673int 1708int
1674cctx_idle () 1709cctx_idle ()
1675 CODE: 1710 CODE:
1676 RETVAL = cctx_idle; 1711 RETVAL = cctx_idle[0] + cctx_idle[1] + cctx_idle[2];
1677 OUTPUT: 1712 OUTPUT:
1678 RETVAL 1713 RETVAL
1679 1714
1680void 1715void
1681list () 1716list ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines