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.246 by root, Wed Sep 24 21:20:05 2008 UTC vs.
Revision 1.255 by root, Sat Nov 8 04:52:01 2008 UTC

52#else 52#else
53# define REGISTER_STACK(cctx,start,end) 53# define REGISTER_STACK(cctx,start,end)
54#endif 54#endif
55 55
56/* the maximum number of idle cctx that will be pooled */ 56/* the maximum number of idle cctx that will be pooled */
57#define MAX_IDLE_CCTX 8 57static int cctx_max_idle = 4;
58 58
59#define PERL_VERSION_ATLEAST(a,b,c) \ 59#define PERL_VERSION_ATLEAST(a,b,c) \
60 (PERL_REVISION > (a) \ 60 (PERL_REVISION > (a) \
61 || (PERL_REVISION == (a) \ 61 || (PERL_REVISION == (a) \
62 && (PERL_VERSION > (b) \ 62 && (PERL_VERSION > (b) \
81# ifndef IS_PADCONST 81# ifndef IS_PADCONST
82# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
83# endif 83# endif
84#endif 84#endif
85 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
86/* 5.8.8 */ 96/* 5.8.8 */
87#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
88# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
89#endif 99#endif
90#ifndef newSV 100#ifndef newSV
91# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
92#endif
93
94/* 5.11 */
95#ifndef CxHASARGS
96# define CxHASARGS(cx) (cx)->blk_sub.hasargs
97#endif 102#endif
98 103
99/* 5.8.7 */ 104/* 5.8.7 */
100#ifndef SvRV_set 105#ifndef SvRV_set
101# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
137#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
138 143
139#include "CoroAPI.h" 144#include "CoroAPI.h"
140 145
141#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
142static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
143# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
144# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
145#else 150#else
146# define LOCK (void)0 151# define LOCK (void)0
147# define UNLOCK (void)0 152# define UNLOCK (void)0
148#endif 153#endif
149 154
150/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
151struct io_state 156struct io_state
152{ 157{
158 AV *res;
153 int errorno; 159 int errorno;
154 I32 laststype; 160 I32 laststype; /* U16 in 5.10.0 */
155 int laststatval; 161 int laststatval;
156 Stat_t statcache; 162 Stat_t statcache;
157}; 163};
158 164
159static double (*nvtime)(); /* so why doesn't it take void? */ 165static double (*nvtime)(); /* so why doesn't it take void? */
160 166
167static U32 cctx_gen;
161static size_t coro_stacksize = CORO_STACKSIZE; 168static size_t cctx_stacksize = CORO_STACKSIZE;
162static struct CoroAPI coroapi; 169static struct CoroAPI coroapi;
163static AV *main_mainstack; /* used to differentiate between $main and others */ 170static AV *main_mainstack; /* used to differentiate between $main and others */
164static JMPENV *main_top_env; 171static JMPENV *main_top_env;
165static HV *coro_state_stash, *coro_stash; 172static HV *coro_state_stash, *coro_stash;
166static volatile SV *coro_mortal; /* will be freed after next transfer */ 173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
167 174
168static GV *irsgv; /* $/ */ 175static GV *irsgv; /* $/ */
169static GV *stdoutgv; /* *STDOUT */ 176static GV *stdoutgv; /* *STDOUT */
170static SV *rv_diehook; 177static SV *rv_diehook;
171static SV *rv_warnhook; 178static SV *rv_warnhook;
203 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 210 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
204 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 211 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
205 JMPENV *top_env; 212 JMPENV *top_env;
206 coro_context cctx; 213 coro_context cctx;
207 214
215 U32 gen;
208#if CORO_USE_VALGRIND 216#if CORO_USE_VALGRIND
209 int valgrind_id; 217 int valgrind_id;
210#endif 218#endif
211 unsigned char flags; 219 unsigned char flags;
212} coro_cctx; 220} coro_cctx;
324 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 332 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
325#endif 333#endif
326 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 334 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
327 --AvFILLp (padlist); 335 --AvFILLp (padlist);
328 336
329 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 337 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
330 av_store (newpadlist, 1, (SV *)newpad); 338 av_store (newpadlist, 1, (SV *)newpad);
331 339
332 return newpadlist; 340 return newpadlist;
333} 341}
334 342
377static MGVTBL coro_cv_vtbl = { 385static MGVTBL coro_cv_vtbl = {
378 0, 0, 0, 0, 386 0, 0, 0, 0,
379 coro_cv_free 387 coro_cv_free
380}; 388};
381 389
382#define CORO_MAGIC(sv,type) \ 390#define CORO_MAGIC(sv, type) \
383 SvMAGIC (sv) \ 391 SvMAGIC (sv) \
384 ? SvMAGIC (sv)->mg_type == type \ 392 ? SvMAGIC (sv)->mg_type == type \
385 ? SvMAGIC (sv) \ 393 ? SvMAGIC (sv) \
386 : mg_find (sv, type) \ 394 : mg_find (sv, type) \
387 : 0 395 : 0
388 396
389#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 397#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
390#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 398#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
391 399
392static struct coro * 400static struct coro *
622 630
623/* 631/*
624 * destroy the stacks, the callchain etc... 632 * destroy the stacks, the callchain etc...
625 */ 633 */
626static void 634static void
627coro_destroy_stacks (pTHX) 635coro_destruct_stacks (pTHX)
628{ 636{
629 while (PL_curstackinfo->si_next) 637 while (PL_curstackinfo->si_next)
630 PL_curstackinfo = PL_curstackinfo->si_next; 638 PL_curstackinfo = PL_curstackinfo->si_next;
631 639
632 while (PL_curstackinfo) 640 while (PL_curstackinfo)
810 GvSV (PL_defgv) = newSV (0); 818 GvSV (PL_defgv) = newSV (0);
811 GvAV (PL_defgv) = coro->args; coro->args = 0; 819 GvAV (PL_defgv) = coro->args; coro->args = 0;
812 GvSV (PL_errgv) = newSV (0); 820 GvSV (PL_errgv) = newSV (0);
813 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 821 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
814 PL_rs = newSVsv (GvSV (irsgv)); 822 PL_rs = newSVsv (GvSV (irsgv));
815 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 823 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
816 824
817 { 825 {
818 dSP; 826 dSP;
819 LOGOP myop; 827 LOGOP myop;
820 828
837 */ 845 */
838 ENTER; 846 ENTER;
839} 847}
840 848
841static void 849static void
842coro_destroy (pTHX_ struct coro *coro) 850coro_destruct (pTHX_ struct coro *coro)
843{ 851{
844 if (!IN_DESTRUCT) 852 if (!IN_DESTRUCT)
845 { 853 {
846 /* restore all saved variables and stuff */ 854 /* restore all saved variables and stuff */
847 LEAVE_SCOPE (0); 855 LEAVE_SCOPE (0);
869 SvREFCNT_dec (PL_warnhook); 877 SvREFCNT_dec (PL_warnhook);
870 878
871 SvREFCNT_dec (coro->saved_deffh); 879 SvREFCNT_dec (coro->saved_deffh);
872 SvREFCNT_dec (coro->throw); 880 SvREFCNT_dec (coro->throw);
873 881
874 coro_destroy_stacks (aTHX); 882 coro_destruct_stacks (aTHX);
875} 883}
876 884
877static void 885static void
878free_coro_mortal (pTHX) 886free_coro_mortal (pTHX)
879{ 887{
916 : cx->blk_gimme == G_SCALAR ? bot + 1 924 : cx->blk_gimme == G_SCALAR ? bot + 1
917 : bot; 925 : bot;
918 926
919 av_extend (av, top - bot); 927 av_extend (av, top - bot);
920 while (bot < top) 928 while (bot < top)
921 av_push (av, SvREFCNT_inc (*bot++)); 929 av_push (av, SvREFCNT_inc_NN (*bot++));
922 930
923 PL_runops = RUNOPS_DEFAULT; 931 PL_runops = RUNOPS_DEFAULT;
924 ENTER; 932 ENTER;
925 SAVETMPS; 933 SAVETMPS;
926 EXTEND (SP, 3); 934 EXTEND (SP, 3);
1074 coro_cctx *cctx; 1082 coro_cctx *cctx;
1075 void *stack_start; 1083 void *stack_start;
1076 size_t stack_size; 1084 size_t stack_size;
1077 1085
1078 ++cctx_count; 1086 ++cctx_count;
1079
1080 Newz (0, cctx, 1, coro_cctx); 1087 Newz (0, cctx, 1, coro_cctx);
1081 1088
1089 cctx->gen = cctx_gen;
1090
1082#if HAVE_MMAP 1091#if HAVE_MMAP
1083 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1092 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1084 /* mmap supposedly does allocate-on-write for us */ 1093 /* mmap supposedly does allocate-on-write for us */
1085 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1094 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1086 1095
1087 if (cctx->sptr != (void *)-1) 1096 if (cctx->sptr != (void *)-1)
1088 { 1097 {
1094 cctx->flags |= CC_MAPPED; 1103 cctx->flags |= CC_MAPPED;
1095 } 1104 }
1096 else 1105 else
1097#endif 1106#endif
1098 { 1107 {
1099 cctx->ssize = coro_stacksize * (long)sizeof (long); 1108 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1100 New (0, cctx->sptr, coro_stacksize, long); 1109 New (0, cctx->sptr, cctx_stacksize, long);
1101 1110
1102 if (!cctx->sptr) 1111 if (!cctx->sptr)
1103 { 1112 {
1104 perror ("FATAL: unable to allocate stack for coroutine"); 1113 perror ("FATAL: unable to allocate stack for coroutine");
1105 _exit (EXIT_FAILURE); 1114 _exit (EXIT_FAILURE);
1120{ 1129{
1121 if (!cctx) 1130 if (!cctx)
1122 return; 1131 return;
1123 1132
1124 --cctx_count; 1133 --cctx_count;
1134 coro_destroy (&cctx->cctx);
1125 1135
1136 /* coro_transfer creates new, empty cctx's */
1137 if (cctx->sptr)
1138 {
1126#if CORO_USE_VALGRIND 1139#if CORO_USE_VALGRIND
1127 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1140 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1128#endif 1141#endif
1129 1142
1130#if HAVE_MMAP 1143#if HAVE_MMAP
1131 if (cctx->flags & CC_MAPPED) 1144 if (cctx->flags & CC_MAPPED)
1132 munmap (cctx->sptr, cctx->ssize); 1145 munmap (cctx->sptr, cctx->ssize);
1133 else 1146 else
1134#endif 1147#endif
1135 Safefree (cctx->sptr); 1148 Safefree (cctx->sptr);
1149 }
1136 1150
1137 Safefree (cctx); 1151 Safefree (cctx);
1138} 1152}
1139 1153
1140/* wether this cctx should be destructed */ 1154/* wether this cctx should be destructed */
1141#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1155#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1142 1156
1143static coro_cctx * 1157static coro_cctx *
1144cctx_get (pTHX) 1158cctx_get (pTHX)
1145{ 1159{
1146 while (expect_true (cctx_first)) 1160 while (expect_true (cctx_first))
1159} 1173}
1160 1174
1161static void 1175static void
1162cctx_put (coro_cctx *cctx) 1176cctx_put (coro_cctx *cctx)
1163{ 1177{
1178 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1179
1164 /* free another cctx if overlimit */ 1180 /* free another cctx if overlimit */
1165 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1181 if (expect_false (cctx_idle >= cctx_max_idle))
1166 { 1182 {
1167 coro_cctx *first = cctx_first; 1183 coro_cctx *first = cctx_first;
1168 cctx_first = first->next; 1184 cctx_first = first->next;
1169 --cctx_idle; 1185 --cctx_idle;
1170 1186
1202/* always use the TRANSFER macro */ 1218/* always use the TRANSFER macro */
1203static void NOINLINE 1219static void NOINLINE
1204transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1220transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1205{ 1221{
1206 dSTACKLEVEL; 1222 dSTACKLEVEL;
1207 static volatile int has_throw;
1208 1223
1209 /* sometimes transfer is only called to set idle_sp */ 1224 /* sometimes transfer is only called to set idle_sp */
1210 if (expect_false (!next)) 1225 if (expect_false (!next))
1211 { 1226 {
1212 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1227 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1213 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1228 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1214 } 1229 }
1215 else if (expect_true (prev != next)) 1230 else if (expect_true (prev != next))
1216 { 1231 {
1232 static volatile int has_throw;
1217 coro_cctx *prev__cctx; 1233 coro_cctx *prev__cctx;
1218 1234
1219 if (expect_false (prev->flags & CF_NEW)) 1235 if (expect_false (prev->flags & CF_NEW))
1220 { 1236 {
1221 /* create a new empty context */ 1237 /* create a new empty/source context */
1238 ++cctx_count;
1222 Newz (0, prev->cctx, 1, coro_cctx); 1239 New (0, prev->cctx, 1, coro_cctx);
1240 prev->cctx->sptr = 0;
1241 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1242
1223 prev->flags &= ~CF_NEW; 1243 prev->flags &= ~CF_NEW;
1224 prev->flags |= CF_RUNNING; 1244 prev->flags |= CF_RUNNING;
1225 } 1245 }
1226 1246
1227 prev->flags &= ~CF_RUNNING; 1247 prev->flags &= ~CF_RUNNING;
1334 croak ("FATAL: tried to destroy currently running coroutine"); 1354 croak ("FATAL: tried to destroy currently running coroutine");
1335 1355
1336 save_perl (aTHX_ &temp); 1356 save_perl (aTHX_ &temp);
1337 load_perl (aTHX_ coro); 1357 load_perl (aTHX_ coro);
1338 1358
1339 coro_destroy (aTHX_ coro); 1359 coro_destruct (aTHX_ coro);
1340 1360
1341 load_perl (aTHX_ &temp); 1361 load_perl (aTHX_ &temp);
1342 1362
1343 coro->slot = 0; 1363 coro->slot = 0;
1344 } 1364 }
1450 LOCK; 1470 LOCK;
1451 1471
1452 sv_hook = coro_nready ? 0 : coro_readyhook; 1472 sv_hook = coro_nready ? 0 : coro_readyhook;
1453 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1473 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1454 1474
1455 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1475 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1456 ++coro_nready; 1476 ++coro_nready;
1457 1477
1458 UNLOCK; 1478 UNLOCK;
1459 1479
1460 if (sv_hook) 1480 if (sv_hook)
1632 else 1652 else
1633 coro->slot->runops = RUNOPS_DEFAULT; 1653 coro->slot->runops = RUNOPS_DEFAULT;
1634 } 1654 }
1635} 1655}
1636 1656
1657#if 0
1637static int 1658static int
1638coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1659coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1639{ 1660{
1640 AV *padlist; 1661 AV *padlist;
1641 AV *av = (AV *)mg->mg_obj; 1662 AV *av = (AV *)mg->mg_obj;
1647 1668
1648static MGVTBL coro_gensub_vtbl = { 1669static MGVTBL coro_gensub_vtbl = {
1649 0, 0, 0, 0, 1670 0, 0, 0, 0,
1650 coro_gensub_free 1671 coro_gensub_free
1651}; 1672};
1673#endif
1652 1674
1653/*****************************************************************************/ 1675/*****************************************************************************/
1654/* PerlIO::cede */ 1676/* PerlIO::cede */
1655 1677
1656typedef struct 1678typedef struct
1662static IV 1684static IV
1663PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 1685PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1664{ 1686{
1665 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1687 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1666 1688
1667 self->every = SvNV (arg); 1689 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1668 self->next = nvtime () + self->every; 1690 self->next = nvtime () + self->every;
1669 1691
1670 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 1692 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1671} 1693}
1672 1694
1688 { 1710 {
1689 api_cede (); 1711 api_cede ();
1690 self->next = now + self->every; 1712 self->next = now + self->every;
1691 } 1713 }
1692 1714
1693 return PerlIOBuf_flush (f); 1715 return PerlIOBuf_flush (aTHX_ f);
1694} 1716}
1695 1717
1696static PerlIO_funcs PerlIO_cede = 1718static PerlIO_funcs PerlIO_cede =
1697{ 1719{
1698 sizeof(PerlIO_funcs), 1720 sizeof(PerlIO_funcs),
1731PROTOTYPES: DISABLE 1753PROTOTYPES: DISABLE
1732 1754
1733BOOT: 1755BOOT:
1734{ 1756{
1735#ifdef USE_ITHREADS 1757#ifdef USE_ITHREADS
1736 MUTEX_INIT (&coro_mutex); 1758 MUTEX_INIT (&coro_lock);
1737#endif 1759#endif
1738 BOOT_PAGESIZE; 1760 BOOT_PAGESIZE;
1739 1761
1740 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1762 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1741 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1763 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1828 ta.next = 0; 1850 ta.next = 0;
1829 break; 1851 break;
1830 1852
1831 case 1: 1853 case 1:
1832 if (items != 2) 1854 if (items != 2)
1833 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1855 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1834 1856
1835 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1857 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1836 break; 1858 break;
1837 1859
1838 case 2: 1860 case 2:
1872 _exit (code); 1894 _exit (code);
1873 1895
1874int 1896int
1875cctx_stacksize (int new_stacksize = 0) 1897cctx_stacksize (int new_stacksize = 0)
1876 CODE: 1898 CODE:
1877 RETVAL = coro_stacksize; 1899 RETVAL = cctx_stacksize;
1878 if (new_stacksize) 1900 if (new_stacksize)
1901 {
1879 coro_stacksize = new_stacksize; 1902 cctx_stacksize = new_stacksize;
1903 ++cctx_gen;
1904 }
1905 OUTPUT:
1906 RETVAL
1907
1908int
1909cctx_max_idle (int max_idle = 0)
1910 CODE:
1911 RETVAL = cctx_max_idle;
1912 if (max_idle > 1)
1913 cctx_max_idle = max_idle;
1880 OUTPUT: 1914 OUTPUT:
1881 RETVAL 1915 RETVAL
1882 1916
1883int 1917int
1884cctx_count () 1918cctx_count ()
1980 CODE: 2014 CODE:
1981 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2015 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1982 OUTPUT: 2016 OUTPUT:
1983 RETVAL 2017 RETVAL
1984 2018
1985IV 2019UV
1986rss (Coro::State coro) 2020rss (Coro::State coro)
1987 PROTOTYPE: $ 2021 PROTOTYPE: $
1988 ALIAS: 2022 ALIAS:
1989 usecount = 1 2023 usecount = 1
1990 CODE: 2024 CODE:
2001 CODE: 2035 CODE:
2002 struct coro *coro = SvSTATE (coro_current); 2036 struct coro *coro = SvSTATE (coro_current);
2003 coro->cctx->idle_sp = 0; 2037 coro->cctx->idle_sp = 0;
2004 2038
2005void 2039void
2006throw (Coro::State self, SV *throw = &PL_sv_undef)
2007 PROTOTYPE: $;$
2008 CODE:
2009 SvREFCNT_dec (self->throw);
2010 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2011
2012void
2013swap_defsv (Coro::State self) 2040swap_defsv (Coro::State self)
2014 PROTOTYPE: $ 2041 PROTOTYPE: $
2015 ALIAS: 2042 ALIAS:
2016 swap_defav = 1 2043 swap_defav = 1
2017 CODE: 2044 CODE:
2071void 2098void
2072_set_current (SV *current) 2099_set_current (SV *current)
2073 PROTOTYPE: $ 2100 PROTOTYPE: $
2074 CODE: 2101 CODE:
2075 SvREFCNT_dec (SvRV (coro_current)); 2102 SvREFCNT_dec (SvRV (coro_current));
2076 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2103 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2077 2104
2078void 2105void
2079_set_readyhook (SV *hook) 2106_set_readyhook (SV *hook)
2080 PROTOTYPE: $ 2107 PROTOTYPE: $
2081 CODE: 2108 CODE:
2120 CODE: 2147 CODE:
2121 RETVAL = coro_nready; 2148 RETVAL = coro_nready;
2122 OUTPUT: 2149 OUTPUT:
2123 RETVAL 2150 RETVAL
2124 2151
2152void
2153throw (Coro::State self, SV *throw = &PL_sv_undef)
2154 PROTOTYPE: $;$
2155 CODE:
2156 SvREFCNT_dec (self->throw);
2157 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2158
2125# for async_pool speedup 2159# for async_pool speedup
2126void 2160void
2127_pool_1 (SV *cb) 2161_pool_1 (SV *cb)
2128 CODE: 2162 CODE:
2129{ 2163{
2141 SvREFCNT_dec (old); 2175 SvREFCNT_dec (old);
2142 croak ("\3async_pool terminate\2\n"); 2176 croak ("\3async_pool terminate\2\n");
2143 } 2177 }
2144 2178
2145 SvREFCNT_dec (coro->saved_deffh); 2179 SvREFCNT_dec (coro->saved_deffh);
2146 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2180 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2147 2181
2148 hv_store (hv, "desc", sizeof ("desc") - 1, 2182 hv_store (hv, "desc", sizeof ("desc") - 1,
2149 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2183 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2150 2184
2151 invoke_av = (AV *)SvRV (invoke); 2185 invoke_av = (AV *)SvRV (invoke);
2155 2189
2156 if (len > 0) 2190 if (len > 0)
2157 { 2191 {
2158 av_fill (defav, len - 1); 2192 av_fill (defav, len - 1);
2159 for (i = 0; i < len; ++i) 2193 for (i = 0; i < len; ++i)
2160 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2194 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2161 } 2195 }
2162 2196
2163 SvREFCNT_dec (invoke); 2197 SvREFCNT_dec (invoke);
2164} 2198}
2165 2199
2172 sv_setsv (cb, &PL_sv_undef); 2206 sv_setsv (cb, &PL_sv_undef);
2173 2207
2174 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2208 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2175 coro->saved_deffh = 0; 2209 coro->saved_deffh = 0;
2176 2210
2177 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2211 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2178 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2212 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2179 { 2213 {
2180 SV *old = PL_diehook; 2214 SV *old = PL_diehook;
2181 PL_diehook = 0; 2215 PL_diehook = 0;
2182 SvREFCNT_dec (old); 2216 SvREFCNT_dec (old);
2244#endif 2278#endif
2245 2279
2246 2280
2247MODULE = Coro::State PACKAGE = Coro::AIO 2281MODULE = Coro::State PACKAGE = Coro::AIO
2248 2282
2249SV * 2283void
2250_get_state () 2284_get_state (SV *self)
2251 CODE: 2285 PPCODE:
2252{ 2286{
2253 struct io_state *data; 2287 AV *defav = GvAV (PL_defgv);
2254 2288 AV *av = newAV ();
2289 int i;
2255 RETVAL = newSV (sizeof (struct io_state)); 2290 SV *data_sv = newSV (sizeof (struct io_state));
2256 data = (struct io_state *)SvPVX (RETVAL); 2291 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2257 SvCUR_set (RETVAL, sizeof (struct io_state)); 2292 SvCUR_set (data_sv, sizeof (struct io_state));
2258 SvPOK_only (RETVAL); 2293 SvPOK_only (data_sv);
2259 2294
2260 data->errorno = errno; 2295 data->errorno = errno;
2261 data->laststype = PL_laststype; 2296 data->laststype = PL_laststype;
2262 data->laststatval = PL_laststatval; 2297 data->laststatval = PL_laststatval;
2263 data->statcache = PL_statcache; 2298 data->statcache = PL_statcache;
2299
2300 av_extend (av, AvFILLp (defav) + 1 + 1);
2301
2302 for (i = 0; i <= AvFILLp (defav); ++i)
2303 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2304
2305 av_push (av, data_sv);
2306
2307 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2308
2309 api_ready (self);
2264} 2310}
2265 OUTPUT:
2266 RETVAL
2267 2311
2268void 2312void
2269_set_state (char *data_) 2313_set_state (SV *state)
2270 PROTOTYPE: $ 2314 PROTOTYPE: $
2271 CODE: 2315 PPCODE:
2272{ 2316{
2273 struct io_state *data = (void *)data_; 2317 AV *av = (AV *)SvRV (state);
2318 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2319 int i;
2274 2320
2275 errno = data->errorno; 2321 errno = data->errorno;
2276 PL_laststype = data->laststype; 2322 PL_laststype = data->laststype;
2277 PL_laststatval = data->laststatval; 2323 PL_laststatval = data->laststatval;
2278 PL_statcache = data->statcache; 2324 PL_statcache = data->statcache;
2325
2326 EXTEND (SP, AvFILLp (av));
2327 for (i = 0; i < AvFILLp (av); ++i)
2328 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2279} 2329}
2280 2330
2281 2331
2282MODULE = Coro::State PACKAGE = Coro::AnyEvent 2332MODULE = Coro::State PACKAGE = Coro::AnyEvent
2283 2333

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines