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.321 by root, Sat Nov 22 02:09:54 2008 UTC vs.
Revision 1.322 by root, Sat Nov 22 05:13:34 2008 UTC

1#define CORO_CLONE 1 //D
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#define PERL_NO_GET_CONTEXT 3#define PERL_NO_GET_CONTEXT
6#define PERL_EXT 4#define PERL_EXT
7 5
230 int valgrind_id; 228 int valgrind_id;
231#endif 229#endif
232 unsigned char flags; 230 unsigned char flags;
233} coro_cctx; 231} coro_cctx;
234 232
233coro_cctx *cctx_current; /* the currently running cctx */
234
235/*****************************************************************************/
236
235enum { 237enum {
236 CF_RUNNING = 0x0001, /* coroutine is running */ 238 CF_RUNNING = 0x0001, /* coroutine is running */
237 CF_READY = 0x0002, /* coroutine is ready */ 239 CF_READY = 0x0002, /* coroutine is ready */
238 CF_NEW = 0x0004, /* has never been switched to */ 240 CF_NEW = 0x0004, /* has never been switched to */
239 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 241 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
1087 1089
1088 TAINT_NOT; 1090 TAINT_NOT;
1089 return 0; 1091 return 0;
1090} 1092}
1091 1093
1092static struct coro_cctx *cctx_ssl_cctx;
1093static struct CoroSLF cctx_ssl_frame; 1094static struct CoroSLF cctx_ssl_frame;
1094 1095
1095static void 1096static void
1096slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1097slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1097{ 1098{
1098 ta->prev = (struct coro *)cctx_ssl_cctx;
1099 ta->next = 0; 1099 ta->prev = 0;
1100} 1100}
1101 1101
1102static int 1102static int
1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1104{ 1104{
1107 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1107 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1108} 1108}
1109 1109
1110/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1110/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1111static void NOINLINE 1111static void NOINLINE
1112cctx_prepare (pTHX_ coro_cctx *cctx) 1112cctx_prepare (pTHX)
1113{ 1113{
1114 PL_top_env = &PL_start_env; 1114 PL_top_env = &PL_start_env;
1115 1115
1116 if (cctx->flags & CC_TRACE) 1116 if (cctx_current->flags & CC_TRACE)
1117 PL_runops = runops_trace; 1117 PL_runops = runops_trace;
1118 1118
1119 /* we already must be executing an SLF op, there is no other valid way 1119 /* we already must be executing an SLF op, there is no other valid way
1120 * that can lead to creation of a new cctx */ 1120 * that can lead to creation of a new cctx */
1121 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1121 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1123 1123
1124 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1124 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1125 cctx_ssl_cctx = cctx;
1126 cctx_ssl_frame = slf_frame; 1125 cctx_ssl_frame = slf_frame;
1127 1126
1128 slf_frame.prepare = slf_prepare_set_stacklevel; 1127 slf_frame.prepare = slf_prepare_set_stacklevel;
1129 slf_frame.check = slf_check_set_stacklevel; 1128 slf_frame.check = slf_check_set_stacklevel;
1130} 1129}
1153 /* normally we would need to skip the entersub here */ 1152 /* normally we would need to skip the entersub here */
1154 /* not doing so will re-execute it, which is exactly what we want */ 1153 /* not doing so will re-execute it, which is exactly what we want */
1155 /* PL_nop = PL_nop->op_next */ 1154 /* PL_nop = PL_nop->op_next */
1156 1155
1157 /* inject a fake subroutine call to cctx_init */ 1156 /* inject a fake subroutine call to cctx_init */
1158 cctx_prepare (aTHX_ (coro_cctx *)arg); 1157 cctx_prepare (aTHX);
1159 1158
1160 /* cctx_run is the alternative tail of transfer() */ 1159 /* cctx_run is the alternative tail of transfer() */
1161 transfer_tail (aTHX); 1160 transfer_tail (aTHX);
1162 1161
1163 /* somebody or something will hit me for both perl_run and PL_restartop */ 1162 /* somebody or something will hit me for both perl_run and PL_restartop */
1258cctx_destroy (coro_cctx *cctx) 1257cctx_destroy (coro_cctx *cctx)
1259{ 1258{
1260 if (!cctx) 1259 if (!cctx)
1261 return; 1260 return;
1262 1261
1262 assert (cctx != cctx_current);//D temporary
1263
1263 --cctx_count; 1264 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1265 coro_destroy (&cctx->cctx);
1265 1266
1266 /* coro_transfer creates new, empty cctx's */ 1267 /* coro_transfer creates new, empty cctx's */
1267 if (cctx->sptr) 1268 if (cctx->sptr)
1352transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1353transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1353{ 1354{
1354 dSTACKLEVEL; 1355 dSTACKLEVEL;
1355 1356
1356 /* sometimes transfer is only called to set idle_sp */ 1357 /* sometimes transfer is only called to set idle_sp */
1357 if (expect_false (!next)) 1358 if (expect_false (!prev))
1358 { 1359 {
1359 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1360 cctx_current->idle_sp = STACKLEVEL;
1360 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1361 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1361 } 1362 }
1362 else if (expect_true (prev != next)) 1363 else if (expect_true (prev != next))
1363 { 1364 {
1364 coro_cctx *prev__cctx; 1365 coro_cctx *cctx_prev;
1365 1366
1366 if (expect_false (prev->flags & CF_NEW)) 1367 if (expect_false (prev->flags & CF_NEW))
1367 { 1368 {
1368 /* create a new empty/source context */ 1369 /* create a new empty/source context */
1369 prev->cctx = cctx_new_empty ();
1370 prev->flags &= ~CF_NEW; 1370 prev->flags &= ~CF_NEW;
1371 prev->flags |= CF_RUNNING; 1371 prev->flags |= CF_RUNNING;
1372 } 1372 }
1373 1373
1374 prev->flags &= ~CF_RUNNING; 1374 prev->flags &= ~CF_RUNNING;
1385 coro_setup (aTHX_ next); 1385 coro_setup (aTHX_ next);
1386 } 1386 }
1387 else 1387 else
1388 load_perl (aTHX_ next); 1388 load_perl (aTHX_ next);
1389 1389
1390 prev__cctx = prev->cctx;
1391
1392 /* possibly untie and reuse the cctx */ 1390 /* possibly untie and reuse the cctx */
1393 if (expect_true ( 1391 if (expect_true (
1394 prev__cctx->idle_sp == STACKLEVEL 1392 cctx_current->idle_sp == STACKLEVEL
1395 && !(prev__cctx->flags & CC_TRACE) 1393 && !(cctx_current->flags & CC_TRACE)
1396 && !force_cctx 1394 && !force_cctx
1397 )) 1395 ))
1398 { 1396 {
1399 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1397 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1400 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1398 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1401
1402 prev->cctx = 0;
1403 1399
1404 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1400 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1405 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1401 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1406 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1402 if (expect_false (CCTX_EXPIRED (cctx_current)))
1407 if (!next->cctx) 1403 if (!next->cctx)
1408 next->cctx = cctx_get (aTHX); 1404 next->cctx = cctx_get (aTHX);
1409 1405
1410 cctx_put (prev__cctx); 1406 cctx_put (cctx_current);
1407 assert (!prev->cctx);//D temporary
1411 } 1408 }
1409 else
1410 prev->cctx = cctx_current;
1412 1411
1413 ++next->usecount; 1412 ++next->usecount;
1414 1413
1415 if (expect_true (!next->cctx)) 1414 cctx_prev = cctx_current;
1416 next->cctx = cctx_get (aTHX); 1415 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1417 1416
1418 if (expect_false (prev__cctx != next->cctx)) 1417 next->cctx = 0;
1418
1419 if (expect_false (cctx_prev != cctx_current))
1419 { 1420 {
1420 prev__cctx->top_env = PL_top_env; 1421 cctx_prev->top_env = PL_top_env;
1421 PL_top_env = next->cctx->top_env; 1422 PL_top_env = cctx_current->top_env;
1422 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1423 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1423 } 1424 }
1424 1425
1425 transfer_tail (aTHX); 1426 transfer_tail (aTHX);
1426 } 1427 }
1427} 1428}
2746 coro_thx = PERL_GET_CONTEXT; 2747 coro_thx = PERL_GET_CONTEXT;
2747# endif 2748# endif
2748#endif 2749#endif
2749 BOOT_PAGESIZE; 2750 BOOT_PAGESIZE;
2750 2751
2752 cctx_current = cctx_new_empty ();
2753
2751 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2754 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2752 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2755 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2753 2756
2754 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2757 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2755 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2758 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2881_exit (int code) 2884_exit (int code)
2882 PROTOTYPE: $ 2885 PROTOTYPE: $
2883 CODE: 2886 CODE:
2884 _exit (code); 2887 _exit (code);
2885 2888
2886#if CORO_CLONE
2887
2888SV * 2889SV *
2889clone (Coro::State coro) 2890clone (Coro::State coro)
2890 CODE: 2891 CODE:
2891{ 2892{
2893#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 2894 struct coro *ncoro = coro_clone (coro);
2893 MAGIC *mg; 2895 MAGIC *mg;
2894 /* TODO: too much duplication */ 2896 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 2897 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 2898 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 2899 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); 2900 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2901#else
2902 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2903#endif
2899} 2904}
2900 OUTPUT: 2905 OUTPUT:
2901 RETVAL 2906 RETVAL
2902
2903#endif
2904 2907
2905int 2908int
2906cctx_stacksize (int new_stacksize = 0) 2909cctx_stacksize (int new_stacksize = 0)
2907 PROTOTYPE: ;$ 2910 PROTOTYPE: ;$
2908 CODE: 2911 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines