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.205 by root, Tue Oct 9 14:07:02 2007 UTC vs.
Revision 1.210 by root, Wed Oct 10 03:24:40 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 732
717 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 733 /* this newly created coroutine might be run on an existing cctx which most
734 * likely was suspended in set_stacklevel, called from entersub.
735 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
736 * so we ENTER here for symmetry
737 */
738 ENTER;
718} 739}
719 740
720static void 741static void
721coro_destroy (pTHX_ struct coro *coro) 742coro_destroy (pTHX_ struct coro *coro)
722{ 743{
953 coro_cctx *cctx; 974 coro_cctx *cctx;
954 void *stack_start; 975 void *stack_start;
955 size_t stack_size; 976 size_t stack_size;
956 977
957 ++cctx_count; 978 ++cctx_count;
958 fprintf (stderr, "bp1\n");//D
959 979
960 Newz (0, cctx, 1, coro_cctx); 980 Newz (0, cctx, 1, coro_cctx);
961 981
962#if HAVE_MMAP 982#if HAVE_MMAP
963 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 983 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1019 1039
1020/* wether this cctx should be destructed */ 1040/* wether this cctx should be destructed */
1021#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1041#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
1022 1042
1023static coro_cctx * 1043static coro_cctx *
1024cctx_get (pTHX) 1044cctx_get (pTHX_ int gimme)
1025{ 1045{
1026 while (expect_true (cctx_first)) 1046 while (expect_true (cctx_first[gimme]))
1027 { 1047 {
1028 coro_cctx *cctx = cctx_first; 1048 coro_cctx *cctx = cctx_first[gimme];
1029 cctx_first = cctx->next; 1049 cctx_first[gimme] = cctx->next;
1030 --cctx_idle; 1050 --cctx_idle[gimme];
1031 1051
1032 if (expect_true (!CCTX_EXPIRED (cctx))) 1052 if (expect_true (!CCTX_EXPIRED (cctx)))
1033 return cctx; 1053 return cctx;
1034 1054
1035 cctx_destroy (cctx); 1055 cctx_destroy (cctx);
1036 } 1056 }
1037 1057
1058 assert (!gimme);
1038 return cctx_new (); 1059 return cctx_new ();
1039} 1060}
1040 1061
1041static void 1062static void
1042cctx_put (coro_cctx *cctx) 1063cctx_put (coro_cctx *cctx, int gimme)
1043{ 1064{
1044 /* free another cctx if overlimit */ 1065 /* free another cctx if overlimit */
1045 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1066 if (expect_false (cctx_idle[gimme] >= MAX_IDLE_CCTX))
1046 { 1067 {
1047 coro_cctx *first = cctx_first; 1068 coro_cctx *first = cctx_first[gimme];
1048 cctx_first = first->next; 1069 cctx_first[gimme] = first->next;
1049 --cctx_idle; 1070 --cctx_idle[gimme];
1050 1071
1051 cctx_destroy (first); 1072 cctx_destroy (first);
1052 } 1073 }
1053 1074
1054 ++cctx_idle; 1075 ++cctx_idle[gimme];
1055 cctx->next = cctx_first; 1076 cctx->next = cctx_first[gimme];
1056 cctx_first = cctx; 1077 cctx_first[gimme] = cctx;
1057} 1078}
1058 1079
1059/** coroutine switching *****************************************************/ 1080/** coroutine switching *****************************************************/
1060 1081
1061static void 1082static void
1140 1161
1141 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1162 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1142 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1163 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1143 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1164 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1144 if (!next->cctx) 1165 if (!next->cctx)
1145 next->cctx = cctx_get (aTHX); 1166 next->cctx = cctx_get (aTHX_ next->gimme);
1146 1167
1147 cctx_put (prev__cctx); 1168 cctx_put (prev__cctx, prev->gimme);
1148 } 1169 }
1149 1170
1150 ++next->usecount; 1171 ++next->usecount;
1151 1172
1152 if (expect_true (!next->cctx)) 1173 if (expect_true (!next->cctx))
1153 next->cctx = cctx_get (aTHX); 1174 next->cctx = cctx_get (aTHX_ next->gimme);
1154 1175
1155 if (expect_false (prev__cctx != next->cctx)) 1176 if (expect_false (prev__cctx != next->cctx))
1156 { 1177 {
1157 prev__cctx->top_env = PL_top_env; 1178 prev__cctx->top_env = PL_top_env;
1158 PL_top_env = next->cctx->top_env; 1179 PL_top_env = next->cctx->top_env;
1524 1545
1525 while (main_top_env->je_prev) 1546 while (main_top_env->je_prev)
1526 main_top_env = main_top_env->je_prev; 1547 main_top_env = main_top_env->je_prev;
1527 1548
1528 coroapi.ver = CORO_API_VERSION; 1549 coroapi.ver = CORO_API_VERSION;
1550 coroapi.rev = CORO_API_REVISION;
1529 coroapi.transfer = api_transfer; 1551 coroapi.transfer = api_transfer;
1530 1552
1531 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1553 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1532} 1554}
1533 1555
1600 1622
1601 break; 1623 break;
1602 } 1624 }
1603 1625
1604 BARRIER; 1626 BARRIER;
1627 PUTBACK;
1605 TRANSFER (ta); 1628 TRANSFER (ta);
1606 1629 SPAGAIN; /* might be the sp of a different coroutine now */
1607 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1630 /* be extra careful not to ever do anything after TRANSFER */
1608 XSRETURN_YES;
1609} 1631}
1610 1632
1611bool 1633bool
1612_destroy (SV *coro_sv) 1634_destroy (SV *coro_sv)
1613 CODE: 1635 CODE:
1639 RETVAL 1661 RETVAL
1640 1662
1641int 1663int
1642cctx_idle () 1664cctx_idle ()
1643 CODE: 1665 CODE:
1644 RETVAL = cctx_idle; 1666 RETVAL = cctx_idle[0] + cctx_idle[1] + cctx_idle[2];
1645 OUTPUT: 1667 OUTPUT:
1646 RETVAL 1668 RETVAL
1647 1669
1648void 1670void
1649list () 1671list ()
1673 1695
1674 { 1696 {
1675 dSP; 1697 dSP;
1676 ENTER; 1698 ENTER;
1677 SAVETMPS; 1699 SAVETMPS;
1700 PUTBACK;
1701 PUSHSTACK;
1678 PUSHMARK (SP); 1702 PUSHMARK (SP);
1679 PUTBACK;
1680 1703
1681 if (ix) 1704 if (ix)
1682 eval_sv (coderef, 0); 1705 eval_sv (coderef, 0);
1683 else 1706 else
1684 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1707 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1685 1708
1686 SPAGAIN; 1709 POPSTACK;
1687 FREETMPS; 1710 FREETMPS;
1688 LEAVE; 1711 LEAVE;
1689 PUTBACK; 1712 PUTBACK;
1690 } 1713 }
1691 1714

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines