… | |
… | |
145 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static size_t coro_stacksize = CORO_STACKSIZE; |
146 | static struct CoroAPI coroapi; |
146 | static struct CoroAPI coroapi; |
147 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
148 | static JMPENV *main_top_env; |
148 | static JMPENV *main_top_env; |
149 | static HV *coro_state_stash, *coro_stash; |
149 | static HV *coro_state_stash, *coro_stash; |
150 | static SV *coro_mortal; /* will be freed after next transfer */ |
150 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
151 | |
151 | |
152 | static GV *irsgv; /* $/ */ |
152 | static GV *irsgv; /* $/ */ |
153 | static GV *stdoutgv; /* *STDOUT */ |
153 | static GV *stdoutgv; /* *STDOUT */ |
154 | |
154 | static SV *rv_diehook; |
|
|
155 | static SV *rv_warnhook; |
155 | static HV *hv_sig; /* %SIG */ |
156 | static HV *hv_sig; /* %SIG */ |
156 | static SV *sv_diehook; |
|
|
157 | static SV *sv_warnhook; |
|
|
158 | |
157 | |
159 | /* async_pool helper stuff */ |
158 | /* async_pool helper stuff */ |
160 | static SV *sv_pool_rss; |
159 | static SV *sv_pool_rss; |
161 | static SV *sv_pool_size; |
160 | static SV *sv_pool_size; |
162 | static AV *av_async_pool; |
161 | static AV *av_async_pool; |
… | |
… | |
260 | static struct coro *coro_first; |
259 | static struct coro *coro_first; |
261 | |
260 | |
262 | /** lowlevel stuff **********************************************************/ |
261 | /** lowlevel stuff **********************************************************/ |
263 | |
262 | |
264 | static SV * |
263 | static SV * |
265 | coro_get_sv (const char *name, int create) |
264 | coro_get_sv (pTHX_ const char *name, int create) |
266 | { |
265 | { |
267 | #if PERL_VERSION_ATLEAST (5,9,0) |
266 | #if PERL_VERSION_ATLEAST (5,9,0) |
268 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
267 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
269 | get_sv (name, create); |
268 | get_sv (name, create); |
270 | #endif |
269 | #endif |
271 | return get_sv (name, create); |
270 | return get_sv (name, create); |
272 | } |
271 | } |
273 | |
272 | |
274 | static AV * |
273 | static AV * |
275 | coro_get_av (const char *name, int create) |
274 | coro_get_av (pTHX_ const char *name, int create) |
276 | { |
275 | { |
277 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | #if PERL_VERSION_ATLEAST (5,9,0) |
278 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
277 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
279 | get_av (name, create); |
278 | get_av (name, create); |
280 | #endif |
279 | #endif |
281 | return get_av (name, create); |
280 | return get_av (name, create); |
282 | } |
281 | } |
283 | |
282 | |
284 | static HV * |
283 | static HV * |
285 | coro_get_hv (const char *name, int create) |
284 | coro_get_hv (pTHX_ const char *name, int create) |
286 | { |
285 | { |
287 | #if PERL_VERSION_ATLEAST (5,9,0) |
286 | #if PERL_VERSION_ATLEAST (5,9,0) |
288 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
287 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
289 | get_hv (name, create); |
288 | get_hv (name, create); |
290 | #endif |
289 | #endif |
… | |
… | |
345 | |
344 | |
346 | /* casting is fun. */ |
345 | /* casting is fun. */ |
347 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
346 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
348 | free_padlist (aTHX_ padlist); |
347 | free_padlist (aTHX_ padlist); |
349 | |
348 | |
350 | SvREFCNT_dec (av); |
|
|
351 | |
|
|
352 | return 0; |
349 | return 0; |
353 | } |
350 | } |
354 | |
351 | |
355 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
352 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
|
|
353 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
356 | |
354 | |
357 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
355 | static MGVTBL coro_cv_vtbl = { |
|
|
356 | 0, 0, 0, 0, |
|
|
357 | coro_cv_free |
|
|
358 | }; |
358 | |
359 | |
359 | #define CORO_MAGIC(cv) \ |
360 | #define CORO_MAGIC(sv,type) \ |
360 | SvMAGIC (cv) \ |
361 | SvMAGIC (sv) \ |
361 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
362 | ? SvMAGIC (sv)->mg_type == type \ |
362 | ? SvMAGIC (cv) \ |
363 | ? SvMAGIC (sv) \ |
363 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
364 | : mg_find (sv, type) \ |
364 | : 0 |
365 | : 0 |
|
|
366 | |
|
|
367 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
368 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
365 | |
369 | |
366 | static struct coro * |
370 | static struct coro * |
367 | SvSTATE_ (pTHX_ SV *coro) |
371 | SvSTATE_ (pTHX_ SV *coro) |
368 | { |
372 | { |
369 | HV *stash; |
373 | HV *stash; |
… | |
… | |
381 | /* very slow, but rare, check */ |
385 | /* very slow, but rare, check */ |
382 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
386 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
383 | croak ("Coro::State object required"); |
387 | croak ("Coro::State object required"); |
384 | } |
388 | } |
385 | |
389 | |
386 | mg = CORO_MAGIC (coro); |
390 | mg = CORO_MAGIC_state (coro); |
387 | return (struct coro *)mg->mg_ptr; |
391 | return (struct coro *)mg->mg_ptr; |
388 | } |
392 | } |
389 | |
393 | |
390 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
394 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
391 | |
395 | |
392 | /* the next two functions merely cache the padlists */ |
396 | /* the next two functions merely cache the padlists */ |
393 | static void |
397 | static void |
394 | get_padlist (pTHX_ CV *cv) |
398 | get_padlist (pTHX_ CV *cv) |
395 | { |
399 | { |
396 | MAGIC *mg = CORO_MAGIC (cv); |
400 | MAGIC *mg = CORO_MAGIC_cv (cv); |
397 | AV *av; |
401 | AV *av; |
398 | |
402 | |
399 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
403 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
400 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
404 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
401 | else |
405 | else |
… | |
… | |
413 | } |
417 | } |
414 | |
418 | |
415 | static void |
419 | static void |
416 | put_padlist (pTHX_ CV *cv) |
420 | put_padlist (pTHX_ CV *cv) |
417 | { |
421 | { |
418 | MAGIC *mg = CORO_MAGIC (cv); |
422 | MAGIC *mg = CORO_MAGIC_cv (cv); |
419 | AV *av; |
423 | AV *av; |
420 | |
424 | |
421 | if (expect_false (!mg)) |
425 | if (expect_false (!mg)) |
422 | { |
426 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
423 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
424 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
425 | mg->mg_virtual = &vtbl_coro; |
|
|
426 | mg->mg_obj = (SV *)newAV (); |
|
|
427 | } |
|
|
428 | |
427 | |
429 | av = (AV *)mg->mg_obj; |
428 | av = (AV *)mg->mg_obj; |
430 | |
429 | |
431 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
430 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
432 | av_extend (av, AvMAX (av) + 1); |
431 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
450 | GvSV (irsgv) = slot->irsgv; |
449 | GvSV (irsgv) = slot->irsgv; |
451 | |
450 | |
452 | #define VAR(name,type) PL_ ## name = slot->name; |
451 | #define VAR(name,type) PL_ ## name = slot->name; |
453 | # include "state.h" |
452 | # include "state.h" |
454 | #undef VAR |
453 | #undef VAR |
455 | |
|
|
456 | /*hv_store (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
457 | /*hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
458 | |
454 | |
459 | { |
455 | { |
460 | dSP; |
456 | dSP; |
461 | |
457 | |
462 | CV *cv; |
458 | CV *cv; |
… | |
… | |
668 | return rss; |
664 | return rss; |
669 | } |
665 | } |
670 | |
666 | |
671 | /** coroutine stack handling ************************************************/ |
667 | /** coroutine stack handling ************************************************/ |
672 | |
668 | |
|
|
669 | #if 0 |
|
|
670 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
671 | |
|
|
672 | /* |
|
|
673 | * This overrides the default magic get method of %SIG elements. |
|
|
674 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
|
|
675 | * and instead of tryign to save and restore the hash elements, we just provide |
|
|
676 | * readback here. |
|
|
677 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
678 | * not expecting this to actually change the hook. This is a potential problem |
|
|
679 | * when a schedule happens then, but we ignore this. |
|
|
680 | */ |
|
|
681 | static int |
|
|
682 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
683 | { |
|
|
684 | return orig_sigelem_get (aTHX_ sv, mg); |
|
|
685 | const char *s = MgPV_nolen_const (mg); |
|
|
686 | |
|
|
687 | if (*s == '_') |
|
|
688 | { |
|
|
689 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
|
|
690 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
|
|
691 | } |
|
|
692 | |
|
|
693 | return orig_sigelem_get (aTHX_ sv, mg); |
|
|
694 | } |
|
|
695 | #endif |
|
|
696 | |
673 | static void |
697 | static void |
674 | coro_setup (pTHX_ struct coro *coro) |
698 | coro_setup (pTHX_ struct coro *coro) |
675 | { |
699 | { |
676 | /* |
700 | /* |
677 | * emulate part of the perl startup here. |
701 | * emulate part of the perl startup here. |
… | |
… | |
685 | PL_curpm = 0; |
709 | PL_curpm = 0; |
686 | PL_curpad = 0; |
710 | PL_curpad = 0; |
687 | PL_localizing = 0; |
711 | PL_localizing = 0; |
688 | PL_dirty = 0; |
712 | PL_dirty = 0; |
689 | PL_restartop = 0; |
713 | PL_restartop = 0; |
690 | PL_diehook = 0; hv_store (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, SvREFCNT_inc (sv_diehook ), 0); |
714 | |
691 | PL_warnhook = 0; hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, SvREFCNT_inc (sv_warnhook), 0); |
715 | /* recreate the die/warn hooks */ |
|
|
716 | PL_diehook = 0; |
|
|
717 | hv_store (hv_sig, "__DIE__", sizeof ("__DIE__") - 1, newSV (0), 0); |
|
|
718 | PL_diehook = SvREFCNT_inc (rv_diehook); |
|
|
719 | |
|
|
720 | PL_warnhook = 0; |
|
|
721 | hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, newSV (0), 0); |
|
|
722 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
692 | |
723 | |
693 | GvSV (PL_defgv) = newSV (0); |
724 | GvSV (PL_defgv) = newSV (0); |
694 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
725 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
695 | GvSV (PL_errgv) = newSV (0); |
726 | GvSV (PL_errgv) = newSV (0); |
696 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
727 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
1089 | /* always use the TRANSFER macro */ |
1120 | /* always use the TRANSFER macro */ |
1090 | static void NOINLINE |
1121 | static void NOINLINE |
1091 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1122 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1092 | { |
1123 | { |
1093 | dSTACKLEVEL; |
1124 | dSTACKLEVEL; |
|
|
1125 | static volatile int has_throw; |
1094 | |
1126 | |
1095 | /* sometimes transfer is only called to set idle_sp */ |
1127 | /* sometimes transfer is only called to set idle_sp */ |
1096 | if (expect_false (!next)) |
1128 | if (expect_false (!next)) |
1097 | { |
1129 | { |
1098 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1130 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1113 | prev->flags &= ~CF_RUNNING; |
1145 | prev->flags &= ~CF_RUNNING; |
1114 | next->flags |= CF_RUNNING; |
1146 | next->flags |= CF_RUNNING; |
1115 | |
1147 | |
1116 | LOCK; |
1148 | LOCK; |
1117 | |
1149 | |
|
|
1150 | /* first get rid of the old state */ |
|
|
1151 | save_perl (aTHX_ prev); |
|
|
1152 | |
1118 | if (expect_false (next->flags & CF_NEW)) |
1153 | if (expect_false (next->flags & CF_NEW)) |
1119 | { |
1154 | { |
1120 | /* need to start coroutine */ |
1155 | /* need to start coroutine */ |
1121 | next->flags &= ~CF_NEW; |
1156 | next->flags &= ~CF_NEW; |
1122 | /* first get rid of the old state */ |
|
|
1123 | save_perl (aTHX_ prev); |
|
|
1124 | /* setup coroutine call */ |
1157 | /* setup coroutine call */ |
1125 | coro_setup (aTHX_ next); |
1158 | coro_setup (aTHX_ next); |
1126 | } |
1159 | } |
1127 | else |
1160 | else |
1128 | { |
|
|
1129 | /* coroutine already started */ |
|
|
1130 | save_perl (aTHX_ prev); |
|
|
1131 | load_perl (aTHX_ next); |
1161 | load_perl (aTHX_ next); |
1132 | } |
|
|
1133 | |
1162 | |
1134 | prev__cctx = prev->cctx; |
1163 | prev__cctx = prev->cctx; |
1135 | |
1164 | |
1136 | /* possibly "free" the cctx */ |
1165 | /* possibly "free" the cctx */ |
1137 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1166 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
… | |
… | |
1152 | |
1181 | |
1153 | ++next->usecount; |
1182 | ++next->usecount; |
1154 | |
1183 | |
1155 | if (expect_true (!next->cctx)) |
1184 | if (expect_true (!next->cctx)) |
1156 | next->cctx = cctx_get (aTHX); |
1185 | next->cctx = cctx_get (aTHX); |
|
|
1186 | |
|
|
1187 | has_throw = !!next->throw; |
1157 | |
1188 | |
1158 | if (expect_false (prev__cctx != next->cctx)) |
1189 | if (expect_false (prev__cctx != next->cctx)) |
1159 | { |
1190 | { |
1160 | prev__cctx->top_env = PL_top_env; |
1191 | prev__cctx->top_env = PL_top_env; |
1161 | PL_top_env = next->cctx->top_env; |
1192 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1163 | } |
1194 | } |
1164 | |
1195 | |
1165 | free_coro_mortal (aTHX); |
1196 | free_coro_mortal (aTHX); |
1166 | UNLOCK; |
1197 | UNLOCK; |
1167 | |
1198 | |
1168 | if (expect_false (prev->throw || next->throw)) |
1199 | if (expect_false (has_throw)) |
1169 | { |
1200 | { |
1170 | struct coro *coro = SvSTATE (coro_current); |
1201 | struct coro *coro = SvSTATE (coro_current); |
1171 | |
1202 | |
1172 | if (coro->throw) |
1203 | if (coro->throw) |
1173 | { |
1204 | { |
… | |
… | |
1299 | { |
1330 | { |
1300 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1331 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1301 | } |
1332 | } |
1302 | |
1333 | |
1303 | static SV * |
1334 | static SV * |
1304 | coro_deq (pTHX_ int min_prio) |
1335 | coro_deq (pTHX) |
1305 | { |
1336 | { |
1306 | int prio = PRIO_MAX - PRIO_MIN; |
1337 | int prio; |
1307 | |
1338 | |
1308 | min_prio -= PRIO_MIN; |
|
|
1309 | if (min_prio < 0) |
|
|
1310 | min_prio = 0; |
|
|
1311 | |
|
|
1312 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1339 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1313 | if (AvFILLp (coro_ready [prio]) >= 0) |
1340 | if (AvFILLp (coro_ready [prio]) >= 0) |
1314 | return av_shift (coro_ready [prio]); |
1341 | return av_shift (coro_ready [prio]); |
1315 | |
1342 | |
1316 | return 0; |
1343 | return 0; |
1317 | } |
1344 | } |
… | |
… | |
1353 | SV *prev_sv, *next_sv; |
1380 | SV *prev_sv, *next_sv; |
1354 | |
1381 | |
1355 | for (;;) |
1382 | for (;;) |
1356 | { |
1383 | { |
1357 | LOCK; |
1384 | LOCK; |
1358 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1385 | next_sv = coro_deq (aTHX); |
1359 | |
1386 | |
1360 | /* nothing to schedule: call the idle handler */ |
1387 | /* nothing to schedule: call the idle handler */ |
1361 | if (expect_false (!next_sv)) |
1388 | if (expect_false (!next_sv)) |
1362 | { |
1389 | { |
1363 | dSP; |
1390 | dSP; |
… | |
… | |
1367 | SAVETMPS; |
1394 | SAVETMPS; |
1368 | |
1395 | |
1369 | PUSHMARK (SP); |
1396 | PUSHMARK (SP); |
1370 | PUTBACK; |
1397 | PUTBACK; |
1371 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1398 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1399 | SPAGAIN; |
1372 | |
1400 | |
1373 | FREETMPS; |
1401 | FREETMPS; |
1374 | LEAVE; |
1402 | LEAVE; |
1375 | continue; |
1403 | continue; |
1376 | } |
1404 | } |
… | |
… | |
1505 | #endif |
1533 | #endif |
1506 | BOOT_PAGESIZE; |
1534 | BOOT_PAGESIZE; |
1507 | |
1535 | |
1508 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1536 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1509 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1537 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1510 | |
1538 | #if 0 |
|
|
1539 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
|
|
1540 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1541 | #endif |
1511 | hv_sig = coro_get_hv ("SIG", TRUE); |
1542 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1512 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1543 | rv_diehook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1513 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1544 | rv_warnhook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1514 | |
|
|
1515 | if (!PL_diehook ) hv_store (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, SvREFCNT_inc (sv_diehook ), 0); |
|
|
1516 | if (!PL_warnhook) hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, SvREFCNT_inc (sv_warnhook), 0); |
|
|
1517 | |
1545 | |
1518 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1546 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1519 | |
1547 | |
1520 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1548 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1521 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1549 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1538 | SV * |
1566 | SV * |
1539 | new (char *klass, ...) |
1567 | new (char *klass, ...) |
1540 | CODE: |
1568 | CODE: |
1541 | { |
1569 | { |
1542 | struct coro *coro; |
1570 | struct coro *coro; |
|
|
1571 | MAGIC *mg; |
1543 | HV *hv; |
1572 | HV *hv; |
1544 | int i; |
1573 | int i; |
1545 | |
1574 | |
1546 | Newz (0, coro, 1, struct coro); |
1575 | Newz (0, coro, 1, struct coro); |
1547 | coro->args = newAV (); |
1576 | coro->args = newAV (); |
… | |
… | |
1550 | if (coro_first) coro_first->prev = coro; |
1579 | if (coro_first) coro_first->prev = coro; |
1551 | coro->next = coro_first; |
1580 | coro->next = coro_first; |
1552 | coro_first = coro; |
1581 | coro_first = coro; |
1553 | |
1582 | |
1554 | coro->hv = hv = newHV (); |
1583 | coro->hv = hv = newHV (); |
1555 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1584 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1585 | mg->mg_flags |= MGf_DUP; |
1556 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1586 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1557 | |
1587 | |
1558 | av_extend (coro->args, items - 1); |
1588 | av_extend (coro->args, items - 1); |
1559 | for (i = 1; i < items; i++) |
1589 | for (i = 1; i < items; i++) |
1560 | av_push (coro->args, newSVsv (ST (i))); |
1590 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1574 | Coro::cede_notself = 4 |
1604 | Coro::cede_notself = 4 |
1575 | CODE: |
1605 | CODE: |
1576 | { |
1606 | { |
1577 | struct transfer_args ta; |
1607 | struct transfer_args ta; |
1578 | |
1608 | |
|
|
1609 | PUTBACK; |
1579 | switch (ix) |
1610 | switch (ix) |
1580 | { |
1611 | { |
1581 | case 0: |
1612 | case 0: |
1582 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1613 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1583 | ta.next = 0; |
1614 | ta.next = 0; |
… | |
… | |
1602 | if (!prepare_cede_notself (aTHX_ &ta)) |
1633 | if (!prepare_cede_notself (aTHX_ &ta)) |
1603 | XSRETURN_EMPTY; |
1634 | XSRETURN_EMPTY; |
1604 | |
1635 | |
1605 | break; |
1636 | break; |
1606 | } |
1637 | } |
|
|
1638 | SPAGAIN; |
1607 | |
1639 | |
1608 | BARRIER; |
1640 | BARRIER; |
1609 | PUTBACK; |
1641 | PUTBACK; |
1610 | TRANSFER (ta); |
1642 | TRANSFER (ta); |
1611 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1643 | SPAGAIN; /* might be the sp of a different coroutine now */ |
… | |
… | |
1669 | { |
1701 | { |
1670 | struct coro temp; |
1702 | struct coro temp; |
1671 | |
1703 | |
1672 | if (!(coro->flags & CF_RUNNING)) |
1704 | if (!(coro->flags & CF_RUNNING)) |
1673 | { |
1705 | { |
|
|
1706 | PUTBACK; |
1674 | save_perl (aTHX_ &temp); |
1707 | save_perl (aTHX_ &temp); |
1675 | load_perl (aTHX_ coro); |
1708 | load_perl (aTHX_ coro); |
1676 | } |
1709 | } |
1677 | |
1710 | |
1678 | { |
1711 | { |
… | |
… | |
1697 | |
1730 | |
1698 | if (!(coro->flags & CF_RUNNING)) |
1731 | if (!(coro->flags & CF_RUNNING)) |
1699 | { |
1732 | { |
1700 | save_perl (aTHX_ coro); |
1733 | save_perl (aTHX_ coro); |
1701 | load_perl (aTHX_ &temp); |
1734 | load_perl (aTHX_ &temp); |
|
|
1735 | SPAGAIN; |
1702 | } |
1736 | } |
1703 | } |
1737 | } |
1704 | } |
1738 | } |
1705 | |
1739 | |
1706 | SV * |
1740 | SV * |
… | |
… | |
1754 | |
1788 | |
1755 | BOOT: |
1789 | BOOT: |
1756 | { |
1790 | { |
1757 | int i; |
1791 | int i; |
1758 | |
1792 | |
1759 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1793 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1760 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1794 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1761 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1795 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1762 | |
1796 | |
1763 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1797 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1764 | SvREADONLY_on (coro_current); |
1798 | SvREADONLY_on (coro_current); |
1765 | |
1799 | |
1766 | coro_stash = gv_stashpv ("Coro", TRUE); |
1800 | coro_stash = gv_stashpv ("Coro", TRUE); |
1767 | |
1801 | |
1768 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1802 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |