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.262 by root, Mon Nov 10 20:38:19 2008 UTC vs.
Revision 1.263 by root, Wed Nov 12 04:49:06 2008 UTC

46# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
47#endif 47#endif
48 48
49#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
50# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
51# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
52#else
53# define REGISTER_STACK(cctx,start,end)
54#endif 51#endif
55 52
56/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
57static int cctx_max_idle = 4; 54static int cctx_max_idle = 4;
58 55
128 125
129#if __GNUC__ >= 3 126#if __GNUC__ >= 3
130# define attribute(x) __attribute__(x) 127# define attribute(x) __attribute__(x)
131# define BARRIER __asm__ __volatile__ ("" : : : "memory") 128# define BARRIER __asm__ __volatile__ ("" : : : "memory")
132# define expect(expr,value) __builtin_expect ((expr),(value)) 129# define expect(expr,value) __builtin_expect ((expr),(value))
130# define INLINE static inline
133#else 131#else
134# define attribute(x) 132# define attribute(x)
135# define BARRIER 133# define BARRIER
136# define expect(expr,value) (expr) 134# define expect(expr,value) (expr)
135# define INLINE static
137#endif 136#endif
138 137
139#define expect_false(expr) expect ((expr) != 0, 0) 138#define expect_false(expr) expect ((expr) != 0, 0)
140#define expect_true(expr) expect ((expr) != 0, 1) 139#define expect_true(expr) expect ((expr) != 0, 1)
141 140
413 : 0 412 : 0
414 413
415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 414#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 415#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
417 416
418static struct coro * 417INLINE struct coro *
419SvSTATE_ (pTHX_ SV *coro) 418SvSTATE_ (pTHX_ SV *coro)
420{ 419{
421 HV *stash; 420 HV *stash;
422 MAGIC *mg; 421 MAGIC *mg;
423 422
727#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 726#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
728 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 727 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
729 (const char*)(mg)->mg_ptr) 728 (const char*)(mg)->mg_ptr)
730#endif 729#endif
731 730
731/* we sometimes need to create the effect of entersub calling us */
732#define ENTERSUB_HEAD ENTER; SAVETMPS
733/* we somtimes need to create the effect of leaving via entersub */
734#define ENTERSUB_TAIL LEAVE
735
732/* 736/*
733 * This overrides the default magic get method of %SIG elements. 737 * This overrides the default magic get method of %SIG elements.
734 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 738 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
735 * and instead of tryign to save and restore the hash elements, we just provide 739 * and instead of tryign to save and restore the hash elements, we just provide
736 * readback here. 740 * readback here.
859 /* this newly created coroutine might be run on an existing cctx which most 863 /* this newly created coroutine might be run on an existing cctx which most
860 * likely was suspended in set_stacklevel, called from entersub. 864 * likely was suspended in set_stacklevel, called from entersub.
861 * set_stacklevel doesn't do anything on return, but entersub does LEAVE, 865 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
862 * so we ENTER here for symmetry. 866 * so we ENTER here for symmetry.
863 */ 867 */
864 ENTER; 868 ENTERSUB_HEAD;
865} 869}
866 870
867static void 871static void
868coro_destruct (pTHX_ struct coro *coro) 872coro_destruct (pTHX_ struct coro *coro)
869{ 873{
898 SvREFCNT_dec (coro->throw); 902 SvREFCNT_dec (coro->throw);
899 903
900 coro_destruct_stacks (aTHX); 904 coro_destruct_stacks (aTHX);
901} 905}
902 906
903static void 907INLINE void
904free_coro_mortal (pTHX) 908free_coro_mortal (pTHX)
905{ 909{
906 if (expect_true (coro_mortal)) 910 if (expect_true (coro_mortal))
907 { 911 {
908 SvREFCNT_dec (coro_mortal); 912 SvREFCNT_dec (coro_mortal);
1069 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1073 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1070 SPAGAIN; 1074 SPAGAIN;
1071} 1075}
1072 1076
1073/* the tail of transfer: execute stuff we can only do after a transfer */ 1077/* the tail of transfer: execute stuff we can only do after a transfer */
1074static void 1078INLINE void
1075transfer_tail (pTHX) 1079transfer_tail (pTHX)
1076{ 1080{
1077 struct coro *next = (struct coro *)transfer_next; 1081 struct coro *next = (struct coro *)transfer_next;
1078 transfer_next = 0; //D for temporary assertion in transfer 1082 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1079 assert (("FATAL ERROR: internal error 1067 in Coro module, please report", next));//D 1083 assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next));
1080 1084
1081 free_coro_mortal (aTHX); 1085 free_coro_mortal (aTHX);
1082 UNLOCK; 1086 UNLOCK;
1083 1087
1084 if (expect_false (next->throw)) 1088 if (expect_false (next->throw))
1104#endif 1108#endif
1105 { 1109 {
1106 dTHX; 1110 dTHX;
1107 1111
1108 /* entersub called ENTER, but we never 'returned', undo that here */ 1112 /* entersub called ENTER, but we never 'returned', undo that here */
1109 LEAVE; 1113 ENTERSUB_TAIL;
1110 1114
1111 /* we now skip the entersub that did lead to transfer() */ 1115 /* we now skip the entersub that did lead to transfer() */
1112 PL_op = PL_op->op_next; 1116 PL_op = PL_op->op_next;
1113 1117
1114 /* inject a fake subroutine call to cctx_init */ 1118 /* inject a fake subroutine call to cctx_init */
1173 /* mmap supposedly does allocate-on-write for us */ 1177 /* mmap supposedly does allocate-on-write for us */
1174 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1178 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1175 1179
1176 if (cctx->sptr != (void *)-1) 1180 if (cctx->sptr != (void *)-1)
1177 { 1181 {
1178# if CORO_STACKGUARD 1182 #if CORO_STACKGUARD
1179 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1183 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1180# endif 1184 #endif
1181 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1185 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1182 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1186 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1183 cctx->flags |= CC_MAPPED; 1187 cctx->flags |= CC_MAPPED;
1184 } 1188 }
1185 else 1189 else
1186#endif 1190#endif
1187 { 1191 {
1196 1200
1197 stack_start = cctx->sptr; 1201 stack_start = cctx->sptr;
1198 stack_size = cctx->ssize; 1202 stack_size = cctx->ssize;
1199 } 1203 }
1200 1204
1201 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1205 #if CORO_USE_VALGRIND
1206 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1207 #endif
1208
1202 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1209 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1203 1210
1204 return cctx; 1211 return cctx;
1205} 1212}
1206 1213
1214 coro_destroy (&cctx->cctx); 1221 coro_destroy (&cctx->cctx);
1215 1222
1216 /* coro_transfer creates new, empty cctx's */ 1223 /* coro_transfer creates new, empty cctx's */
1217 if (cctx->sptr) 1224 if (cctx->sptr)
1218 { 1225 {
1219#if CORO_USE_VALGRIND 1226 #if CORO_USE_VALGRIND
1220 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1227 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1221#endif 1228 #endif
1222 1229
1223#if HAVE_MMAP 1230#if HAVE_MMAP
1224 if (cctx->flags & CC_MAPPED) 1231 if (cctx->flags & CC_MAPPED)
1225 munmap (cctx->sptr, cctx->ssize); 1232 munmap (cctx->sptr, cctx->ssize);
1226 else 1233 else
1253} 1260}
1254 1261
1255static void 1262static void
1256cctx_put (coro_cctx *cctx) 1263cctx_put (coro_cctx *cctx)
1257{ 1264{
1258 assert (("cctx_put called on non-initialised cctx", cctx->sptr)); 1265 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1259 1266
1260 /* free another cctx if overlimit */ 1267 /* free another cctx if overlimit */
1261 if (expect_false (cctx_idle >= cctx_max_idle)) 1268 if (expect_false (cctx_idle >= cctx_max_idle))
1262 { 1269 {
1263 coro_cctx *first = cctx_first; 1270 coro_cctx *first = cctx_first;
1345 && !(prev__cctx->flags & CC_TRACE) 1352 && !(prev__cctx->flags & CC_TRACE)
1346 && !force_cctx 1353 && !force_cctx
1347 )) 1354 ))
1348 { 1355 {
1349 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1356 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1350 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1357 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1351 1358
1352 prev->cctx = 0; 1359 prev->cctx = 0;
1353 1360
1354 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1361 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1355 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1362 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1363 ++next->usecount; 1370 ++next->usecount;
1364 1371
1365 if (expect_true (!next->cctx)) 1372 if (expect_true (!next->cctx))
1366 next->cctx = cctx_get (aTHX); 1373 next->cctx = cctx_get (aTHX);
1367 1374
1368 assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D 1375 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1369 transfer_next = next; 1376 transfer_next = next;
1370 1377
1371 if (expect_false (prev__cctx != next->cctx)) 1378 if (expect_false (prev__cctx != next->cctx))
1372 { 1379 {
1373 prev__cctx->top_env = PL_top_env; 1380 prev__cctx->top_env = PL_top_env;
1558 1565
1559static int 1566static int
1560api_is_ready (SV *coro_sv) 1567api_is_ready (SV *coro_sv)
1561{ 1568{
1562 dTHX; 1569 dTHX;
1570
1563 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1571 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1564} 1572}
1565 1573
1566static void 1574INLINE void
1567prepare_schedule (pTHX_ struct transfer_args *ta) 1575prepare_schedule (pTHX_ struct transfer_args *ta)
1568{ 1576{
1569 SV *prev_sv, *next_sv; 1577 SV *prev_sv, *next_sv;
1570 1578
1571 for (;;) 1579 for (;;)
1610 1618
1611 /* free this only after the transfer */ 1619 /* free this only after the transfer */
1612 prev_sv = SvRV (coro_current); 1620 prev_sv = SvRV (coro_current);
1613 ta->prev = SvSTATE (prev_sv); 1621 ta->prev = SvSTATE (prev_sv);
1614 TRANSFER_CHECK (*ta); 1622 TRANSFER_CHECK (*ta);
1615 assert (ta->next->flags & CF_READY); 1623 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1616 ta->next->flags &= ~CF_READY; 1624 ta->next->flags &= ~CF_READY;
1617 SvRV_set (coro_current, next_sv); 1625 SvRV_set (coro_current, next_sv);
1618 1626
1619 LOCK; 1627 LOCK;
1620 free_coro_mortal (aTHX); 1628 free_coro_mortal (aTHX);
1621 coro_mortal = prev_sv; 1629 coro_mortal = prev_sv;
1622 UNLOCK; 1630 UNLOCK;
1623} 1631}
1624 1632
1625static void 1633INLINE void
1626prepare_cede (pTHX_ struct transfer_args *ta) 1634prepare_cede (pTHX_ struct transfer_args *ta)
1627{ 1635{
1628 api_ready (coro_current); 1636 api_ready (coro_current);
1629 prepare_schedule (aTHX_ ta); 1637 prepare_schedule (aTHX_ ta);
1630} 1638}
2142 2150
2143 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2151 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2144 coro_ready[i] = newAV (); 2152 coro_ready[i] = newAV ();
2145 2153
2146 { 2154 {
2147 SV *sv = perl_get_sv ("Coro::API", TRUE); 2155 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2148 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2149 2156
2150 coroapi.schedule = api_schedule; 2157 coroapi.schedule = api_schedule;
2151 coroapi.cede = api_cede; 2158 coroapi.cede = api_cede;
2152 coroapi.cede_notself = api_cede_notself; 2159 coroapi.cede_notself = api_cede_notself;
2153 coroapi.ready = api_ready; 2160 coroapi.ready = api_ready;
2421 2428
2422MODULE = Coro::State PACKAGE = PerlIO::cede 2429MODULE = Coro::State PACKAGE = PerlIO::cede
2423 2430
2424BOOT: 2431BOOT:
2425 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2433

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines