… | |
… | |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
103 | #ifndef CvISXSUB |
103 | #ifndef CvISXSUB |
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
105 | #endif |
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
106 | |
109 | |
107 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
108 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
110 | #endif |
113 | #endif |
… | |
… | |
130 | #else |
133 | #else |
131 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
132 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
133 | #endif |
136 | #endif |
134 | |
137 | |
135 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
136 | |
139 | |
137 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
138 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
139 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
140 | # define INLINE static inline |
143 | # define INLINE static inline |
… | |
… | |
172 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
174 | |
177 | |
175 | static AV *av_destroy; /* destruction queue */ |
178 | static AV *av_destroy; /* destruction queue */ |
176 | static SV *sv_manager; /* the manager coro */ |
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
177 | |
181 | |
178 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
179 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
180 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
181 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
… | |
… | |
225 | int valgrind_id; |
229 | int valgrind_id; |
226 | #endif |
230 | #endif |
227 | unsigned char flags; |
231 | unsigned char flags; |
228 | } coro_cctx; |
232 | } coro_cctx; |
229 | |
233 | |
|
|
234 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
235 | |
|
|
236 | /*****************************************************************************/ |
|
|
237 | |
230 | enum { |
238 | enum { |
231 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
232 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
233 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
234 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
239 | { |
247 | { |
240 | SV *defsv; |
248 | SV *defsv; |
241 | AV *defav; |
249 | AV *defav; |
242 | SV *errsv; |
250 | SV *errsv; |
243 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
244 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
245 | # include "state.h" |
254 | # include "state.h" |
246 | #undef VAR |
255 | #undef VAR |
247 | } perl_slots; |
256 | } perl_slots; |
248 | |
257 | |
… | |
… | |
275 | |
284 | |
276 | /* async_pool */ |
285 | /* async_pool */ |
277 | SV *saved_deffh; |
286 | SV *saved_deffh; |
278 | SV *invoke_cb; |
287 | SV *invoke_cb; |
279 | AV *invoke_av; |
288 | AV *invoke_av; |
|
|
289 | |
|
|
290 | /* on_enter/on_leave */ |
|
|
291 | AV *on_enter; |
|
|
292 | AV *on_leave; |
280 | |
293 | |
281 | /* linked list */ |
294 | /* linked list */ |
282 | struct coro *next, *prev; |
295 | struct coro *next, *prev; |
283 | }; |
296 | }; |
284 | |
297 | |
… | |
… | |
343 | INLINE CV * |
356 | INLINE CV * |
344 | coro_sv_2cv (pTHX_ SV *sv) |
357 | coro_sv_2cv (pTHX_ SV *sv) |
345 | { |
358 | { |
346 | HV *st; |
359 | HV *st; |
347 | GV *gvp; |
360 | GV *gvp; |
348 | return sv_2cv (sv, &st, &gvp, 0); |
361 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
362 | |
|
|
363 | if (!cv) |
|
|
364 | croak ("code reference expected"); |
|
|
365 | |
|
|
366 | return cv; |
349 | } |
367 | } |
|
|
368 | |
|
|
369 | /*****************************************************************************/ |
|
|
370 | /* magic glue */ |
|
|
371 | |
|
|
372 | #define CORO_MAGIC_type_cv 26 |
|
|
373 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
374 | |
|
|
375 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
376 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
377 | ? SvMAGIC (sv) \ |
|
|
378 | : mg_find (sv, type)) |
|
|
379 | |
|
|
380 | #define CORO_MAGIC(sv, type) \ |
|
|
381 | (expect_true (SvMAGIC (sv)) \ |
|
|
382 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
383 | : 0) |
|
|
384 | |
|
|
385 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
386 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
387 | |
|
|
388 | INLINE struct coro * |
|
|
389 | SvSTATE_ (pTHX_ SV *coro) |
|
|
390 | { |
|
|
391 | HV *stash; |
|
|
392 | MAGIC *mg; |
|
|
393 | |
|
|
394 | if (SvROK (coro)) |
|
|
395 | coro = SvRV (coro); |
|
|
396 | |
|
|
397 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
398 | croak ("Coro::State object required"); |
|
|
399 | |
|
|
400 | stash = SvSTASH (coro); |
|
|
401 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
402 | { |
|
|
403 | /* very slow, but rare, check */ |
|
|
404 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
405 | croak ("Coro::State object required"); |
|
|
406 | } |
|
|
407 | |
|
|
408 | mg = CORO_MAGIC_state (coro); |
|
|
409 | return (struct coro *)mg->mg_ptr; |
|
|
410 | } |
|
|
411 | |
|
|
412 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
413 | |
|
|
414 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
415 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
416 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
417 | |
|
|
418 | /*****************************************************************************/ |
|
|
419 | /* padlist management and caching */ |
350 | |
420 | |
351 | static AV * |
421 | static AV * |
352 | coro_clone_padlist (pTHX_ CV *cv) |
422 | coro_derive_padlist (pTHX_ CV *cv) |
353 | { |
423 | { |
354 | AV *padlist = CvPADLIST (cv); |
424 | AV *padlist = CvPADLIST (cv); |
355 | AV *newpadlist, *newpad; |
425 | AV *newpadlist, *newpad; |
356 | |
426 | |
357 | newpadlist = newAV (); |
427 | newpadlist = newAV (); |
… | |
… | |
362 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
432 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
363 | #endif |
433 | #endif |
364 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
434 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
365 | --AvFILLp (padlist); |
435 | --AvFILLp (padlist); |
366 | |
436 | |
367 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
437 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
368 | av_store (newpadlist, 1, (SV *)newpad); |
438 | av_store (newpadlist, 1, (SV *)newpad); |
369 | |
439 | |
370 | return newpadlist; |
440 | return newpadlist; |
371 | } |
441 | } |
372 | |
442 | |
373 | static void |
443 | static void |
374 | free_padlist (pTHX_ AV *padlist) |
444 | free_padlist (pTHX_ AV *padlist) |
375 | { |
445 | { |
376 | /* may be during global destruction */ |
446 | /* may be during global destruction */ |
377 | if (SvREFCNT (padlist)) |
447 | if (!IN_DESTRUCT) |
378 | { |
448 | { |
379 | I32 i = AvFILLp (padlist); |
449 | I32 i = AvFILLp (padlist); |
380 | while (i >= 0) |
450 | |
|
|
451 | while (i > 0) /* special-case index 0 */ |
381 | { |
452 | { |
382 | SV **svp = av_fetch (padlist, i--, FALSE); |
453 | /* we try to be extra-careful here */ |
383 | if (svp) |
454 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
384 | { |
455 | I32 j = AvFILLp (av); |
385 | SV *sv; |
456 | |
386 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
457 | while (j >= 0) |
|
|
458 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
459 | |
|
|
460 | AvFILLp (av) = -1; |
387 | SvREFCNT_dec (sv); |
461 | SvREFCNT_dec (av); |
388 | |
|
|
389 | SvREFCNT_dec (*svp); |
|
|
390 | } |
|
|
391 | } |
462 | } |
392 | |
463 | |
|
|
464 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
465 | |
|
|
466 | AvFILLp (padlist) = -1; |
393 | SvREFCNT_dec ((SV*)padlist); |
467 | SvREFCNT_dec ((SV*)padlist); |
394 | } |
468 | } |
395 | } |
469 | } |
396 | |
470 | |
397 | static int |
471 | static int |
… | |
… | |
406 | |
480 | |
407 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
481 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
408 | |
482 | |
409 | return 0; |
483 | return 0; |
410 | } |
484 | } |
411 | |
|
|
412 | #define CORO_MAGIC_type_cv 26 |
|
|
413 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
414 | |
485 | |
415 | static MGVTBL coro_cv_vtbl = { |
486 | static MGVTBL coro_cv_vtbl = { |
416 | 0, 0, 0, 0, |
487 | 0, 0, 0, 0, |
417 | coro_cv_free |
488 | coro_cv_free |
418 | }; |
489 | }; |
419 | |
|
|
420 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
421 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
422 | ? SvMAGIC (sv) \ |
|
|
423 | : mg_find (sv, type)) |
|
|
424 | |
|
|
425 | #define CORO_MAGIC(sv, type) \ |
|
|
426 | (expect_true (SvMAGIC (sv)) \ |
|
|
427 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
428 | : 0) |
|
|
429 | |
|
|
430 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
431 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
432 | |
|
|
433 | INLINE struct coro * |
|
|
434 | SvSTATE_ (pTHX_ SV *coro) |
|
|
435 | { |
|
|
436 | HV *stash; |
|
|
437 | MAGIC *mg; |
|
|
438 | |
|
|
439 | if (SvROK (coro)) |
|
|
440 | coro = SvRV (coro); |
|
|
441 | |
|
|
442 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
443 | croak ("Coro::State object required"); |
|
|
444 | |
|
|
445 | stash = SvSTASH (coro); |
|
|
446 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
447 | { |
|
|
448 | /* very slow, but rare, check */ |
|
|
449 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
450 | croak ("Coro::State object required"); |
|
|
451 | } |
|
|
452 | |
|
|
453 | mg = CORO_MAGIC_state (coro); |
|
|
454 | return (struct coro *)mg->mg_ptr; |
|
|
455 | } |
|
|
456 | |
|
|
457 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
458 | |
|
|
459 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
460 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
461 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
462 | |
490 | |
463 | /* the next two functions merely cache the padlists */ |
491 | /* the next two functions merely cache the padlists */ |
464 | static void |
492 | static void |
465 | get_padlist (pTHX_ CV *cv) |
493 | get_padlist (pTHX_ CV *cv) |
466 | { |
494 | { |
… | |
… | |
477 | CV *cp = Perl_cv_clone (aTHX_ cv); |
505 | CV *cp = Perl_cv_clone (aTHX_ cv); |
478 | CvPADLIST (cv) = CvPADLIST (cp); |
506 | CvPADLIST (cv) = CvPADLIST (cp); |
479 | CvPADLIST (cp) = 0; |
507 | CvPADLIST (cp) = 0; |
480 | SvREFCNT_dec (cp); |
508 | SvREFCNT_dec (cp); |
481 | #else |
509 | #else |
482 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
510 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
483 | #endif |
511 | #endif |
484 | } |
512 | } |
485 | } |
513 | } |
486 | |
514 | |
487 | static void |
515 | static void |
… | |
… | |
494 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
522 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
495 | |
523 | |
496 | av = (AV *)mg->mg_obj; |
524 | av = (AV *)mg->mg_obj; |
497 | |
525 | |
498 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
526 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
499 | av_extend (av, AvMAX (av) + 1); |
527 | av_extend (av, AvFILLp (av) + 1); |
500 | |
528 | |
501 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
529 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
502 | } |
530 | } |
503 | |
531 | |
504 | /** load & save, init *******************************************************/ |
532 | /** load & save, init *******************************************************/ |
|
|
533 | |
|
|
534 | static void |
|
|
535 | on_enterleave_call (pTHX_ SV *cb); |
505 | |
536 | |
506 | static void |
537 | static void |
507 | load_perl (pTHX_ Coro__State c) |
538 | load_perl (pTHX_ Coro__State c) |
508 | { |
539 | { |
509 | perl_slots *slot = c->slot; |
540 | perl_slots *slot = c->slot; |
510 | c->slot = 0; |
541 | c->slot = 0; |
511 | |
542 | |
512 | PL_mainstack = c->mainstack; |
543 | PL_mainstack = c->mainstack; |
513 | |
544 | |
514 | GvSV (PL_defgv) = slot->defsv; |
545 | GvSV (PL_defgv) = slot->defsv; |
515 | GvAV (PL_defgv) = slot->defav; |
546 | GvAV (PL_defgv) = slot->defav; |
516 | GvSV (PL_errgv) = slot->errsv; |
547 | GvSV (PL_errgv) = slot->errsv; |
517 | GvSV (irsgv) = slot->irsgv; |
548 | GvSV (irsgv) = slot->irsgv; |
|
|
549 | GvHV (PL_hintgv) = slot->hinthv; |
518 | |
550 | |
519 | #define VAR(name,type) PL_ ## name = slot->name; |
551 | #define VAR(name,type) PL_ ## name = slot->name; |
520 | # include "state.h" |
552 | # include "state.h" |
521 | #undef VAR |
553 | #undef VAR |
522 | |
554 | |
… | |
… | |
536 | PUTBACK; |
568 | PUTBACK; |
537 | } |
569 | } |
538 | |
570 | |
539 | slf_frame = c->slf_frame; |
571 | slf_frame = c->slf_frame; |
540 | CORO_THROW = c->except; |
572 | CORO_THROW = c->except; |
|
|
573 | |
|
|
574 | if (expect_false (c->on_enter)) |
|
|
575 | { |
|
|
576 | int i; |
|
|
577 | |
|
|
578 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
579 | on_enterleave_call (AvARRAY (c->on_enter)[i]); |
|
|
580 | } |
541 | } |
581 | } |
542 | |
582 | |
543 | static void |
583 | static void |
544 | save_perl (pTHX_ Coro__State c) |
584 | save_perl (pTHX_ Coro__State c) |
545 | { |
585 | { |
|
|
586 | if (expect_false (c->on_leave)) |
|
|
587 | { |
|
|
588 | int i; |
|
|
589 | |
|
|
590 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
591 | on_enterleave_call (AvARRAY (c->on_leave)[i]); |
|
|
592 | } |
|
|
593 | |
546 | c->except = CORO_THROW; |
594 | c->except = CORO_THROW; |
547 | c->slf_frame = slf_frame; |
595 | c->slf_frame = slf_frame; |
548 | |
596 | |
549 | { |
597 | { |
550 | dSP; |
598 | dSP; |
… | |
… | |
608 | c->mainstack = PL_mainstack; |
656 | c->mainstack = PL_mainstack; |
609 | |
657 | |
610 | { |
658 | { |
611 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
659 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
612 | |
660 | |
613 | slot->defav = GvAV (PL_defgv); |
661 | slot->defav = GvAV (PL_defgv); |
614 | slot->defsv = DEFSV; |
662 | slot->defsv = DEFSV; |
615 | slot->errsv = ERRSV; |
663 | slot->errsv = ERRSV; |
616 | slot->irsgv = GvSV (irsgv); |
664 | slot->irsgv = GvSV (irsgv); |
|
|
665 | slot->hinthv = GvHV (PL_hintgv); |
617 | |
666 | |
618 | #define VAR(name,type) slot->name = PL_ ## name; |
667 | #define VAR(name,type) slot->name = PL_ ## name; |
619 | # include "state.h" |
668 | # include "state.h" |
620 | #undef VAR |
669 | #undef VAR |
621 | } |
670 | } |
… | |
… | |
748 | #endif |
797 | #endif |
749 | |
798 | |
750 | /* |
799 | /* |
751 | * This overrides the default magic get method of %SIG elements. |
800 | * This overrides the default magic get method of %SIG elements. |
752 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
801 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
753 | * and instead of tryign to save and restore the hash elements, we just provide |
802 | * and instead of trying to save and restore the hash elements, we just provide |
754 | * readback here. |
803 | * readback here. |
755 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
756 | * not expecting this to actually change the hook. This is a potential problem |
|
|
757 | * when a schedule happens then, but we ignore this. |
|
|
758 | */ |
804 | */ |
759 | static int |
805 | static int |
760 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
806 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
761 | { |
807 | { |
762 | const char *s = MgPV_nolen_const (mg); |
808 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
815 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
861 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
816 | |
862 | |
817 | if (svp) |
863 | if (svp) |
818 | { |
864 | { |
819 | SV *old = *svp; |
865 | SV *old = *svp; |
820 | *svp = newSVsv (sv); |
866 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
821 | SvREFCNT_dec (old); |
867 | SvREFCNT_dec (old); |
822 | return 0; |
868 | return 0; |
823 | } |
869 | } |
824 | } |
870 | } |
825 | |
871 | |
… | |
… | |
857 | |
903 | |
858 | PL_runops = RUNOPS_DEFAULT; |
904 | PL_runops = RUNOPS_DEFAULT; |
859 | PL_curcop = &PL_compiling; |
905 | PL_curcop = &PL_compiling; |
860 | PL_in_eval = EVAL_NULL; |
906 | PL_in_eval = EVAL_NULL; |
861 | PL_comppad = 0; |
907 | PL_comppad = 0; |
|
|
908 | PL_comppad_name = 0; |
|
|
909 | PL_comppad_name_fill = 0; |
|
|
910 | PL_comppad_name_floor = 0; |
862 | PL_curpm = 0; |
911 | PL_curpm = 0; |
863 | PL_curpad = 0; |
912 | PL_curpad = 0; |
864 | PL_localizing = 0; |
913 | PL_localizing = 0; |
865 | PL_dirty = 0; |
914 | PL_dirty = 0; |
866 | PL_restartop = 0; |
915 | PL_restartop = 0; |
867 | #if PERL_VERSION_ATLEAST (5,10,0) |
916 | #if PERL_VERSION_ATLEAST (5,10,0) |
868 | PL_parser = 0; |
917 | PL_parser = 0; |
869 | #endif |
918 | #endif |
|
|
919 | PL_hints = 0; |
870 | |
920 | |
871 | /* recreate the die/warn hooks */ |
921 | /* recreate the die/warn hooks */ |
872 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
922 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
873 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
923 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
874 | |
924 | |
875 | GvSV (PL_defgv) = newSV (0); |
925 | GvSV (PL_defgv) = newSV (0); |
876 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
926 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
877 | GvSV (PL_errgv) = newSV (0); |
927 | GvSV (PL_errgv) = newSV (0); |
878 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
928 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
929 | GvHV (PL_hintgv) = 0; |
879 | PL_rs = newSVsv (GvSV (irsgv)); |
930 | PL_rs = newSVsv (GvSV (irsgv)); |
880 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
931 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
881 | |
932 | |
882 | { |
933 | { |
883 | dSP; |
934 | dSP; |
… | |
… | |
912 | /* copy throw, in case it was set before coro_setup */ |
963 | /* copy throw, in case it was set before coro_setup */ |
913 | CORO_THROW = coro->except; |
964 | CORO_THROW = coro->except; |
914 | } |
965 | } |
915 | |
966 | |
916 | static void |
967 | static void |
917 | coro_destruct (pTHX_ struct coro *coro) |
968 | coro_unwind_stacks (pTHX) |
918 | { |
969 | { |
919 | if (!IN_DESTRUCT) |
970 | if (!IN_DESTRUCT) |
920 | { |
971 | { |
921 | /* restore all saved variables and stuff */ |
972 | /* restore all saved variables and stuff */ |
922 | LEAVE_SCOPE (0); |
973 | LEAVE_SCOPE (0); |
… | |
… | |
930 | POPSTACK_TO (PL_mainstack); |
981 | POPSTACK_TO (PL_mainstack); |
931 | |
982 | |
932 | /* unwind main stack */ |
983 | /* unwind main stack */ |
933 | dounwind (-1); |
984 | dounwind (-1); |
934 | } |
985 | } |
|
|
986 | } |
|
|
987 | |
|
|
988 | static void |
|
|
989 | coro_destruct_perl (pTHX_ struct coro *coro) |
|
|
990 | { |
|
|
991 | coro_unwind_stacks (aTHX); |
935 | |
992 | |
936 | SvREFCNT_dec (GvSV (PL_defgv)); |
993 | SvREFCNT_dec (GvSV (PL_defgv)); |
937 | SvREFCNT_dec (GvAV (PL_defgv)); |
994 | SvREFCNT_dec (GvAV (PL_defgv)); |
938 | SvREFCNT_dec (GvSV (PL_errgv)); |
995 | SvREFCNT_dec (GvSV (PL_errgv)); |
939 | SvREFCNT_dec (PL_defoutgv); |
996 | SvREFCNT_dec (PL_defoutgv); |
940 | SvREFCNT_dec (PL_rs); |
997 | SvREFCNT_dec (PL_rs); |
941 | SvREFCNT_dec (GvSV (irsgv)); |
998 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
999 | SvREFCNT_dec (GvHV (PL_hintgv)); |
942 | |
1000 | |
943 | SvREFCNT_dec (PL_diehook); |
1001 | SvREFCNT_dec (PL_diehook); |
944 | SvREFCNT_dec (PL_warnhook); |
1002 | SvREFCNT_dec (PL_warnhook); |
945 | |
1003 | |
946 | SvREFCNT_dec (coro->saved_deffh); |
1004 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
964 | static int |
1022 | static int |
965 | runops_trace (pTHX) |
1023 | runops_trace (pTHX) |
966 | { |
1024 | { |
967 | COP *oldcop = 0; |
1025 | COP *oldcop = 0; |
968 | int oldcxix = -2; |
1026 | int oldcxix = -2; |
969 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
970 | coro_cctx *cctx = coro->cctx; |
|
|
971 | |
1027 | |
972 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
1028 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
973 | { |
1029 | { |
974 | PERL_ASYNC_CHECK (); |
1030 | PERL_ASYNC_CHECK (); |
975 | |
1031 | |
976 | if (cctx->flags & CC_TRACE_ALL) |
1032 | if (cctx_current->flags & CC_TRACE_ALL) |
977 | { |
1033 | { |
978 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
1034 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
979 | { |
1035 | { |
980 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1036 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
981 | SV **bot, **top; |
1037 | SV **bot, **top; |
982 | AV *av = newAV (); /* return values */ |
1038 | AV *av = newAV (); /* return values */ |
983 | SV **cb; |
1039 | SV **cb; |
… | |
… | |
1020 | |
1076 | |
1021 | if (PL_curcop != &PL_compiling) |
1077 | if (PL_curcop != &PL_compiling) |
1022 | { |
1078 | { |
1023 | SV **cb; |
1079 | SV **cb; |
1024 | |
1080 | |
1025 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1081 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1026 | { |
1082 | { |
1027 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1083 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1028 | |
1084 | |
1029 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1085 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1030 | { |
1086 | { |
… | |
… | |
1053 | } |
1109 | } |
1054 | |
1110 | |
1055 | oldcxix = cxstack_ix; |
1111 | oldcxix = cxstack_ix; |
1056 | } |
1112 | } |
1057 | |
1113 | |
1058 | if (cctx->flags & CC_TRACE_LINE) |
1114 | if (cctx_current->flags & CC_TRACE_LINE) |
1059 | { |
1115 | { |
1060 | dSP; |
1116 | dSP; |
1061 | |
1117 | |
1062 | PL_runops = RUNOPS_DEFAULT; |
1118 | PL_runops = RUNOPS_DEFAULT; |
1063 | ENTER; |
1119 | ENTER; |
… | |
… | |
1082 | |
1138 | |
1083 | TAINT_NOT; |
1139 | TAINT_NOT; |
1084 | return 0; |
1140 | return 0; |
1085 | } |
1141 | } |
1086 | |
1142 | |
1087 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1088 | static struct CoroSLF cctx_ssl_frame; |
1143 | static struct CoroSLF cctx_ssl_frame; |
1089 | |
1144 | |
1090 | static void |
1145 | static void |
1091 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1146 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1092 | { |
1147 | { |
1093 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1094 | ta->next = 0; |
1148 | ta->prev = 0; |
1095 | } |
1149 | } |
1096 | |
1150 | |
1097 | static int |
1151 | static int |
1098 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1152 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1099 | { |
1153 | { |
… | |
… | |
1102 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1156 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1103 | } |
1157 | } |
1104 | |
1158 | |
1105 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1159 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1106 | static void NOINLINE |
1160 | static void NOINLINE |
1107 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1161 | cctx_prepare (pTHX) |
1108 | { |
1162 | { |
1109 | PL_top_env = &PL_start_env; |
1163 | PL_top_env = &PL_start_env; |
1110 | |
1164 | |
1111 | if (cctx->flags & CC_TRACE) |
1165 | if (cctx_current->flags & CC_TRACE) |
1112 | PL_runops = runops_trace; |
1166 | PL_runops = runops_trace; |
1113 | |
1167 | |
1114 | /* we already must be executing an SLF op, there is no other valid way |
1168 | /* we already must be executing an SLF op, there is no other valid way |
1115 | * that can lead to creation of a new cctx */ |
1169 | * that can lead to creation of a new cctx */ |
1116 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1170 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1117 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1171 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1118 | |
1172 | |
1119 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1173 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1120 | cctx_ssl_cctx = cctx; |
|
|
1121 | cctx_ssl_frame = slf_frame; |
1174 | cctx_ssl_frame = slf_frame; |
1122 | |
1175 | |
1123 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1176 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1124 | slf_frame.check = slf_check_set_stacklevel; |
1177 | slf_frame.check = slf_check_set_stacklevel; |
1125 | } |
1178 | } |
… | |
… | |
1148 | /* normally we would need to skip the entersub here */ |
1201 | /* normally we would need to skip the entersub here */ |
1149 | /* not doing so will re-execute it, which is exactly what we want */ |
1202 | /* not doing so will re-execute it, which is exactly what we want */ |
1150 | /* PL_nop = PL_nop->op_next */ |
1203 | /* PL_nop = PL_nop->op_next */ |
1151 | |
1204 | |
1152 | /* inject a fake subroutine call to cctx_init */ |
1205 | /* inject a fake subroutine call to cctx_init */ |
1153 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1206 | cctx_prepare (aTHX); |
1154 | |
1207 | |
1155 | /* cctx_run is the alternative tail of transfer() */ |
1208 | /* cctx_run is the alternative tail of transfer() */ |
1156 | transfer_tail (aTHX); |
1209 | transfer_tail (aTHX); |
1157 | |
1210 | |
1158 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1211 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
1166 | /* |
1219 | /* |
1167 | * If perl-run returns we assume exit() was being called or the coro |
1220 | * If perl-run returns we assume exit() was being called or the coro |
1168 | * fell off the end, which seems to be the only valid (non-bug) |
1221 | * fell off the end, which seems to be the only valid (non-bug) |
1169 | * reason for perl_run to return. We try to exit by jumping to the |
1222 | * reason for perl_run to return. We try to exit by jumping to the |
1170 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1223 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1171 | * coroutine as Coro has no such concept |
1224 | * coroutine as Coro has no such concept. |
|
|
1225 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1226 | * that backend are too broken to do it in a standards-compliant way. |
1172 | */ |
1227 | */ |
1173 | PL_top_env = main_top_env; |
1228 | PL_top_env = main_top_env; |
1174 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1229 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1175 | } |
1230 | } |
1176 | } |
1231 | } |
… | |
… | |
1253 | cctx_destroy (coro_cctx *cctx) |
1308 | cctx_destroy (coro_cctx *cctx) |
1254 | { |
1309 | { |
1255 | if (!cctx) |
1310 | if (!cctx) |
1256 | return; |
1311 | return; |
1257 | |
1312 | |
|
|
1313 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
|
|
1314 | |
1258 | --cctx_count; |
1315 | --cctx_count; |
1259 | coro_destroy (&cctx->cctx); |
1316 | coro_destroy (&cctx->cctx); |
1260 | |
1317 | |
1261 | /* coro_transfer creates new, empty cctx's */ |
1318 | /* coro_transfer creates new, empty cctx's */ |
1262 | if (cctx->sptr) |
1319 | if (cctx->sptr) |
… | |
… | |
1325 | /* TODO: throwing up here is considered harmful */ |
1382 | /* TODO: throwing up here is considered harmful */ |
1326 | |
1383 | |
1327 | if (expect_true (prev != next)) |
1384 | if (expect_true (prev != next)) |
1328 | { |
1385 | { |
1329 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1386 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1330 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1387 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1331 | |
1388 | |
1332 | if (expect_false (next->flags & CF_RUNNING)) |
1389 | if (expect_false (next->flags & CF_RUNNING)) |
1333 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1390 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1334 | |
1391 | |
1335 | if (expect_false (next->flags & CF_DESTROYED)) |
1392 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1347 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1404 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1348 | { |
1405 | { |
1349 | dSTACKLEVEL; |
1406 | dSTACKLEVEL; |
1350 | |
1407 | |
1351 | /* sometimes transfer is only called to set idle_sp */ |
1408 | /* sometimes transfer is only called to set idle_sp */ |
1352 | if (expect_false (!next)) |
1409 | if (expect_false (!prev)) |
1353 | { |
1410 | { |
1354 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1411 | cctx_current->idle_sp = STACKLEVEL; |
1355 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1412 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1356 | } |
1413 | } |
1357 | else if (expect_true (prev != next)) |
1414 | else if (expect_true (prev != next)) |
1358 | { |
1415 | { |
1359 | coro_cctx *prev__cctx; |
1416 | coro_cctx *cctx_prev; |
1360 | |
1417 | |
1361 | if (expect_false (prev->flags & CF_NEW)) |
1418 | if (expect_false (prev->flags & CF_NEW)) |
1362 | { |
1419 | { |
1363 | /* create a new empty/source context */ |
1420 | /* create a new empty/source context */ |
1364 | prev->cctx = cctx_new_empty (); |
|
|
1365 | prev->flags &= ~CF_NEW; |
1421 | prev->flags &= ~CF_NEW; |
1366 | prev->flags |= CF_RUNNING; |
1422 | prev->flags |= CF_RUNNING; |
1367 | } |
1423 | } |
1368 | |
1424 | |
1369 | prev->flags &= ~CF_RUNNING; |
1425 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1380 | coro_setup (aTHX_ next); |
1436 | coro_setup (aTHX_ next); |
1381 | } |
1437 | } |
1382 | else |
1438 | else |
1383 | load_perl (aTHX_ next); |
1439 | load_perl (aTHX_ next); |
1384 | |
1440 | |
1385 | prev__cctx = prev->cctx; |
|
|
1386 | |
|
|
1387 | /* possibly untie and reuse the cctx */ |
1441 | /* possibly untie and reuse the cctx */ |
1388 | if (expect_true ( |
1442 | if (expect_true ( |
1389 | prev__cctx->idle_sp == STACKLEVEL |
1443 | cctx_current->idle_sp == STACKLEVEL |
1390 | && !(prev__cctx->flags & CC_TRACE) |
1444 | && !(cctx_current->flags & CC_TRACE) |
1391 | && !force_cctx |
1445 | && !force_cctx |
1392 | )) |
1446 | )) |
1393 | { |
1447 | { |
1394 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1448 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1395 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1449 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1396 | |
1450 | |
1397 | prev->cctx = 0; |
|
|
1398 | |
|
|
1399 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1451 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1400 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1452 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1401 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1453 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1402 | if (!next->cctx) |
1454 | if (expect_true (!next->cctx)) |
1403 | next->cctx = cctx_get (aTHX); |
1455 | next->cctx = cctx_get (aTHX); |
1404 | |
1456 | |
1405 | cctx_put (prev__cctx); |
1457 | cctx_put (cctx_current); |
1406 | } |
1458 | } |
|
|
1459 | else |
|
|
1460 | prev->cctx = cctx_current; |
1407 | |
1461 | |
1408 | ++next->usecount; |
1462 | ++next->usecount; |
1409 | |
1463 | |
1410 | if (expect_true (!next->cctx)) |
1464 | cctx_prev = cctx_current; |
1411 | next->cctx = cctx_get (aTHX); |
1465 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1412 | |
1466 | |
1413 | if (expect_false (prev__cctx != next->cctx)) |
1467 | next->cctx = 0; |
|
|
1468 | |
|
|
1469 | if (expect_false (cctx_prev != cctx_current)) |
1414 | { |
1470 | { |
1415 | prev__cctx->top_env = PL_top_env; |
1471 | cctx_prev->top_env = PL_top_env; |
1416 | PL_top_env = next->cctx->top_env; |
1472 | PL_top_env = cctx_current->top_env; |
1417 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1473 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1418 | } |
1474 | } |
1419 | |
1475 | |
1420 | transfer_tail (aTHX); |
1476 | transfer_tail (aTHX); |
1421 | } |
1477 | } |
1422 | } |
1478 | } |
… | |
… | |
1444 | --coro_nready; |
1500 | --coro_nready; |
1445 | } |
1501 | } |
1446 | else |
1502 | else |
1447 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1503 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1448 | |
1504 | |
1449 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1505 | if (coro->mainstack |
|
|
1506 | && coro->mainstack != main_mainstack |
|
|
1507 | && coro->slot |
|
|
1508 | && !PL_dirty) |
1450 | { |
1509 | { |
1451 | struct coro temp; |
1510 | struct coro *current = SvSTATE_current; |
1452 | |
1511 | |
1453 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1512 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1454 | |
1513 | |
1455 | save_perl (aTHX_ &temp); |
1514 | save_perl (aTHX_ current); |
1456 | load_perl (aTHX_ coro); |
1515 | load_perl (aTHX_ coro); |
1457 | |
1516 | |
1458 | coro_destruct (aTHX_ coro); |
1517 | coro_destruct_perl (aTHX_ coro); |
1459 | |
1518 | |
1460 | load_perl (aTHX_ &temp); |
1519 | load_perl (aTHX_ current); |
1461 | |
1520 | |
1462 | coro->slot = 0; |
1521 | coro->slot = 0; |
1463 | } |
1522 | } |
1464 | |
1523 | |
1465 | cctx_destroy (coro->cctx); |
1524 | cctx_destroy (coro->cctx); |
… | |
… | |
1577 | { |
1636 | { |
1578 | struct coro *coro; |
1637 | struct coro *coro; |
1579 | SV *sv_hook; |
1638 | SV *sv_hook; |
1580 | void (*xs_hook)(void); |
1639 | void (*xs_hook)(void); |
1581 | |
1640 | |
1582 | if (SvROK (coro_sv)) |
|
|
1583 | coro_sv = SvRV (coro_sv); |
|
|
1584 | |
|
|
1585 | coro = SvSTATE (coro_sv); |
1641 | coro = SvSTATE (coro_sv); |
1586 | |
1642 | |
1587 | if (coro->flags & CF_READY) |
1643 | if (coro->flags & CF_READY) |
1588 | return 0; |
1644 | return 0; |
1589 | |
1645 | |
… | |
… | |
1663 | } |
1719 | } |
1664 | } |
1720 | } |
1665 | else |
1721 | else |
1666 | { |
1722 | { |
1667 | /* nothing to schedule: call the idle handler */ |
1723 | /* nothing to schedule: call the idle handler */ |
|
|
1724 | if (SvROK (sv_idle) |
|
|
1725 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1726 | { |
|
|
1727 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1728 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1729 | --coro_nready; |
|
|
1730 | } |
|
|
1731 | else |
|
|
1732 | { |
1668 | dSP; |
1733 | dSP; |
1669 | |
1734 | |
1670 | ENTER; |
1735 | ENTER; |
1671 | SAVETMPS; |
1736 | SAVETMPS; |
1672 | |
1737 | |
1673 | PUSHMARK (SP); |
1738 | PUSHMARK (SP); |
1674 | PUTBACK; |
1739 | PUTBACK; |
1675 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1740 | call_sv (sv_idle, G_VOID | G_DISCARD); |
1676 | |
1741 | |
1677 | FREETMPS; |
1742 | FREETMPS; |
1678 | LEAVE; |
1743 | LEAVE; |
|
|
1744 | } |
1679 | } |
1745 | } |
1680 | } |
1746 | } |
1681 | } |
1747 | } |
1682 | |
1748 | |
1683 | INLINE void |
1749 | INLINE void |
… | |
… | |
1754 | static void |
1820 | static void |
1755 | api_trace (pTHX_ SV *coro_sv, int flags) |
1821 | api_trace (pTHX_ SV *coro_sv, int flags) |
1756 | { |
1822 | { |
1757 | struct coro *coro = SvSTATE (coro_sv); |
1823 | struct coro *coro = SvSTATE (coro_sv); |
1758 | |
1824 | |
|
|
1825 | if (coro->flags & CF_RUNNING) |
|
|
1826 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1827 | |
1759 | if (flags & CC_TRACE) |
1828 | if (flags & CC_TRACE) |
1760 | { |
1829 | { |
1761 | if (!coro->cctx) |
1830 | if (!coro->cctx) |
1762 | coro->cctx = cctx_new_run (); |
1831 | coro->cctx = cctx_new_run (); |
1763 | else if (!(coro->cctx->flags & CC_TRACE)) |
1832 | else if (!(coro->cctx->flags & CC_TRACE)) |
1764 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1833 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1765 | |
1834 | |
1766 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1835 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1767 | } |
1836 | } |
1768 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1837 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1769 | { |
1838 | { |
… | |
… | |
1825 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1894 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1826 | api_ready (aTHX_ sv_manager); |
1895 | api_ready (aTHX_ sv_manager); |
1827 | |
1896 | |
1828 | frame->prepare = prepare_schedule; |
1897 | frame->prepare = prepare_schedule; |
1829 | frame->check = slf_check_repeat; |
1898 | frame->check = slf_check_repeat; |
|
|
1899 | |
|
|
1900 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1901 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1902 | /*coro_unwind_stacks (aTHX);*/ |
1830 | } |
1903 | } |
1831 | |
1904 | |
1832 | /*****************************************************************************/ |
1905 | /*****************************************************************************/ |
1833 | /* async pool handler */ |
1906 | /* async pool handler */ |
1834 | |
1907 | |
… | |
… | |
2253 | |
2326 | |
2254 | PL_op->op_ppaddr = pp_slf; |
2327 | PL_op->op_ppaddr = pp_slf; |
2255 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2328 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2256 | |
2329 | |
2257 | PL_op = (OP *)&slf_restore; |
2330 | PL_op = (OP *)&slf_restore; |
|
|
2331 | } |
|
|
2332 | |
|
|
2333 | /*****************************************************************************/ |
|
|
2334 | /* dynamic wind */ |
|
|
2335 | |
|
|
2336 | static void |
|
|
2337 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2338 | { |
|
|
2339 | dSP; |
|
|
2340 | |
|
|
2341 | PUSHSTACK; |
|
|
2342 | |
|
|
2343 | PUSHMARK (SP); |
|
|
2344 | PUTBACK; |
|
|
2345 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2346 | SPAGAIN; |
|
|
2347 | |
|
|
2348 | POPSTACK; |
|
|
2349 | } |
|
|
2350 | |
|
|
2351 | static SV * |
|
|
2352 | coro_avp_pop_and_free (AV **avp) |
|
|
2353 | { |
|
|
2354 | AV *av = *avp; |
|
|
2355 | SV *res = av_pop (av); |
|
|
2356 | |
|
|
2357 | if (AvFILLp (av) < 0) |
|
|
2358 | { |
|
|
2359 | *avp = 0; |
|
|
2360 | SvREFCNT_dec (av); |
|
|
2361 | } |
|
|
2362 | |
|
|
2363 | return res; |
|
|
2364 | } |
|
|
2365 | |
|
|
2366 | static void |
|
|
2367 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2368 | { |
|
|
2369 | SV *cb = coro_avp_pop_and_free (&((struct coro *)coro)->on_enter); |
|
|
2370 | SvREFCNT_dec (cb); |
|
|
2371 | } |
|
|
2372 | |
|
|
2373 | static void |
|
|
2374 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2375 | { |
|
|
2376 | SV *cb = coro_avp_pop_and_free (&((struct coro *)coro)->on_leave); |
|
|
2377 | on_enterleave_call (sv_2mortal (cb)); |
2258 | } |
2378 | } |
2259 | |
2379 | |
2260 | /*****************************************************************************/ |
2380 | /*****************************************************************************/ |
2261 | /* PerlIO::cede */ |
2381 | /* PerlIO::cede */ |
2262 | |
2382 | |
… | |
… | |
2343 | SV **ary; |
2463 | SV **ary; |
2344 | |
2464 | |
2345 | /* unfortunately, building manually saves memory */ |
2465 | /* unfortunately, building manually saves memory */ |
2346 | Newx (ary, 2, SV *); |
2466 | Newx (ary, 2, SV *); |
2347 | AvALLOC (av) = ary; |
2467 | AvALLOC (av) = ary; |
|
|
2468 | #if PERL_VERSION_ATLEAST (5,10,0) |
2348 | /*AvARRAY (av) = ary;*/ |
2469 | AvARRAY (av) = ary; |
|
|
2470 | #else |
2349 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2471 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2472 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2473 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2474 | #endif |
2350 | AvMAX (av) = 1; |
2475 | AvMAX (av) = 1; |
2351 | AvFILLp (av) = 0; |
2476 | AvFILLp (av) = 0; |
2352 | ary [0] = newSViv (count); |
2477 | ary [0] = newSViv (count); |
2353 | |
2478 | |
2354 | return newRV_noinc ((SV *)av); |
2479 | return newRV_noinc ((SV *)av); |
… | |
… | |
2724 | XSRETURN_EMPTY; |
2849 | XSRETURN_EMPTY; |
2725 | } |
2850 | } |
2726 | |
2851 | |
2727 | /*****************************************************************************/ |
2852 | /*****************************************************************************/ |
2728 | |
2853 | |
|
|
2854 | #if CORO_CLONE |
|
|
2855 | # include "clone.c" |
|
|
2856 | #endif |
|
|
2857 | |
2729 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2858 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2730 | |
2859 | |
2731 | PROTOTYPES: DISABLE |
2860 | PROTOTYPES: DISABLE |
2732 | |
2861 | |
2733 | BOOT: |
2862 | BOOT: |
… | |
… | |
2736 | # if CORO_PTHREAD |
2865 | # if CORO_PTHREAD |
2737 | coro_thx = PERL_GET_CONTEXT; |
2866 | coro_thx = PERL_GET_CONTEXT; |
2738 | # endif |
2867 | # endif |
2739 | #endif |
2868 | #endif |
2740 | BOOT_PAGESIZE; |
2869 | BOOT_PAGESIZE; |
|
|
2870 | |
|
|
2871 | cctx_current = cctx_new_empty (); |
2741 | |
2872 | |
2742 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2873 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2743 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2874 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2744 | |
2875 | |
2745 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2876 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
… | |
… | |
2871 | void |
3002 | void |
2872 | _exit (int code) |
3003 | _exit (int code) |
2873 | PROTOTYPE: $ |
3004 | PROTOTYPE: $ |
2874 | CODE: |
3005 | CODE: |
2875 | _exit (code); |
3006 | _exit (code); |
|
|
3007 | |
|
|
3008 | SV * |
|
|
3009 | clone (Coro::State coro) |
|
|
3010 | CODE: |
|
|
3011 | { |
|
|
3012 | #if CORO_CLONE |
|
|
3013 | struct coro *ncoro = coro_clone (aTHX_ coro); |
|
|
3014 | MAGIC *mg; |
|
|
3015 | /* TODO: too much duplication */ |
|
|
3016 | ncoro->hv = newHV (); |
|
|
3017 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
3018 | mg->mg_flags |= MGf_DUP; |
|
|
3019 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
3020 | #else |
|
|
3021 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
3022 | #endif |
|
|
3023 | } |
|
|
3024 | OUTPUT: |
|
|
3025 | RETVAL |
2876 | |
3026 | |
2877 | int |
3027 | int |
2878 | cctx_stacksize (int new_stacksize = 0) |
3028 | cctx_stacksize (int new_stacksize = 0) |
2879 | PROTOTYPE: ;$ |
3029 | PROTOTYPE: ;$ |
2880 | CODE: |
3030 | CODE: |
… | |
… | |
2930 | eval = 1 |
3080 | eval = 1 |
2931 | CODE: |
3081 | CODE: |
2932 | { |
3082 | { |
2933 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3083 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2934 | { |
3084 | { |
2935 | struct coro temp; |
3085 | struct coro *current = SvSTATE_current; |
2936 | |
3086 | |
2937 | if (!(coro->flags & CF_RUNNING)) |
3087 | if (current != coro) |
2938 | { |
3088 | { |
2939 | PUTBACK; |
3089 | PUTBACK; |
2940 | save_perl (aTHX_ &temp); |
3090 | save_perl (aTHX_ current); |
2941 | load_perl (aTHX_ coro); |
3091 | load_perl (aTHX_ coro); |
|
|
3092 | SPAGAIN; |
2942 | } |
3093 | } |
2943 | |
3094 | |
2944 | { |
|
|
2945 | dSP; |
|
|
2946 | ENTER; |
|
|
2947 | SAVETMPS; |
|
|
2948 | PUTBACK; |
|
|
2949 | PUSHSTACK; |
3095 | PUSHSTACK; |
|
|
3096 | |
2950 | PUSHMARK (SP); |
3097 | PUSHMARK (SP); |
|
|
3098 | PUTBACK; |
2951 | |
3099 | |
2952 | if (ix) |
3100 | if (ix) |
2953 | eval_sv (coderef, 0); |
3101 | eval_sv (coderef, 0); |
2954 | else |
3102 | else |
2955 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3103 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
2956 | |
3104 | |
|
|
3105 | SPAGAIN; |
2957 | POPSTACK; |
3106 | POPSTACK; |
2958 | SPAGAIN; |
|
|
2959 | FREETMPS; |
|
|
2960 | LEAVE; |
|
|
2961 | PUTBACK; |
|
|
2962 | } |
|
|
2963 | |
3107 | |
2964 | if (!(coro->flags & CF_RUNNING)) |
3108 | if (current != coro) |
2965 | { |
3109 | { |
|
|
3110 | PUTBACK; |
2966 | save_perl (aTHX_ coro); |
3111 | save_perl (aTHX_ coro); |
2967 | load_perl (aTHX_ &temp); |
3112 | load_perl (aTHX_ current); |
2968 | SPAGAIN; |
3113 | SPAGAIN; |
2969 | } |
3114 | } |
2970 | } |
3115 | } |
2971 | } |
3116 | } |
2972 | |
3117 | |
… | |
… | |
3001 | |
3146 | |
3002 | SV * |
3147 | SV * |
3003 | has_cctx (Coro::State coro) |
3148 | has_cctx (Coro::State coro) |
3004 | PROTOTYPE: $ |
3149 | PROTOTYPE: $ |
3005 | CODE: |
3150 | CODE: |
3006 | RETVAL = boolSV (!!coro->cctx); |
3151 | /* maybe manage the running flag differently */ |
|
|
3152 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
3007 | OUTPUT: |
3153 | OUTPUT: |
3008 | RETVAL |
3154 | RETVAL |
3009 | |
3155 | |
3010 | int |
3156 | int |
3011 | is_traced (Coro::State coro) |
3157 | is_traced (Coro::State coro) |
… | |
… | |
3031 | |
3177 | |
3032 | void |
3178 | void |
3033 | force_cctx () |
3179 | force_cctx () |
3034 | PROTOTYPE: |
3180 | PROTOTYPE: |
3035 | CODE: |
3181 | CODE: |
3036 | SvSTATE_current->cctx->idle_sp = 0; |
3182 | cctx_current->idle_sp = 0; |
3037 | |
3183 | |
3038 | void |
3184 | void |
3039 | swap_defsv (Coro::State self) |
3185 | swap_defsv (Coro::State self) |
3040 | PROTOTYPE: $ |
3186 | PROTOTYPE: $ |
3041 | ALIAS: |
3187 | ALIAS: |
… | |
… | |
3058 | { |
3204 | { |
3059 | int i; |
3205 | int i; |
3060 | |
3206 | |
3061 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3207 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3062 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3208 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3063 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3209 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3064 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3210 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3065 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3211 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3066 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3212 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3067 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3213 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3068 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3214 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3215 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3069 | |
3216 | |
3070 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3217 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3071 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3218 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3072 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3219 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3073 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3220 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
… | |
… | |
3252 | rouse_wait (...) |
3399 | rouse_wait (...) |
3253 | PROTOTYPE: ;$ |
3400 | PROTOTYPE: ;$ |
3254 | PPCODE: |
3401 | PPCODE: |
3255 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3402 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3256 | |
3403 | |
|
|
3404 | void |
|
|
3405 | on_enter (SV *block) |
|
|
3406 | ALIAS: |
|
|
3407 | on_leave = 1 |
|
|
3408 | PROTOTYPE: & |
|
|
3409 | CODE: |
|
|
3410 | { |
|
|
3411 | struct coro *coro = SvSTATE_current; |
|
|
3412 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3413 | |
|
|
3414 | block = (SV *)coro_sv_2cv (block); |
|
|
3415 | |
|
|
3416 | if (!*avp) |
|
|
3417 | *avp = newAV (); |
|
|
3418 | |
|
|
3419 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3420 | |
|
|
3421 | if (!ix) |
|
|
3422 | on_enterleave_call (aTHX_ block); |
|
|
3423 | |
|
|
3424 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ |
|
|
3425 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3426 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ |
|
|
3427 | } |
|
|
3428 | |
3257 | |
3429 | |
3258 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3430 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3259 | |
3431 | |
3260 | BOOT: |
3432 | BOOT: |
3261 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3433 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |