… | |
… | |
133 | #else |
133 | #else |
134 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
135 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
136 | #endif |
136 | #endif |
137 | |
137 | |
138 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
139 | |
139 | |
140 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
141 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define INLINE static inline |
143 | # define INLINE static inline |
… | |
… | |
175 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
176 | 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 */ |
177 | |
177 | |
178 | static AV *av_destroy; /* destruction queue */ |
178 | static AV *av_destroy; /* destruction queue */ |
179 | static SV *sv_manager; /* the manager coro */ |
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
180 | |
181 | |
181 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
183 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
184 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
… | |
… | |
237 | enum { |
238 | enum { |
238 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
243 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
242 | }; |
244 | }; |
243 | |
245 | |
244 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
246 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
245 | typedef struct |
247 | typedef struct |
246 | { |
248 | { |
247 | SV *defsv; |
249 | SV *defsv; |
248 | AV *defav; |
250 | AV *defav; |
249 | SV *errsv; |
251 | SV *errsv; |
250 | SV *irsgv; |
252 | SV *irsgv; |
|
|
253 | HV *hinthv; |
251 | #define VAR(name,type) type name; |
254 | #define VAR(name,type) type name; |
252 | # include "state.h" |
255 | # include "state.h" |
253 | #undef VAR |
256 | #undef VAR |
254 | } perl_slots; |
257 | } perl_slots; |
255 | |
258 | |
… | |
… | |
257 | |
260 | |
258 | /* this is a structure representing a perl-level coroutine */ |
261 | /* this is a structure representing a perl-level coroutine */ |
259 | struct coro { |
262 | struct coro { |
260 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | /* the C coroutine allocated to this perl coroutine, if any */ |
261 | coro_cctx *cctx; |
264 | coro_cctx *cctx; |
|
|
265 | |
|
|
266 | /* ready queue */ |
|
|
267 | struct coro *next_ready; |
262 | |
268 | |
263 | /* state data */ |
269 | /* state data */ |
264 | struct CoroSLF slf_frame; /* saved slf frame */ |
270 | struct CoroSLF slf_frame; /* saved slf frame */ |
265 | AV *mainstack; |
271 | AV *mainstack; |
266 | perl_slots *slot; /* basically the saved sp */ |
272 | perl_slots *slot; /* basically the saved sp */ |
… | |
… | |
283 | /* async_pool */ |
289 | /* async_pool */ |
284 | SV *saved_deffh; |
290 | SV *saved_deffh; |
285 | SV *invoke_cb; |
291 | SV *invoke_cb; |
286 | AV *invoke_av; |
292 | AV *invoke_av; |
287 | |
293 | |
|
|
294 | /* on_enter/on_leave */ |
|
|
295 | AV *on_enter; |
|
|
296 | AV *on_leave; |
|
|
297 | |
288 | /* linked list */ |
298 | /* linked list */ |
289 | struct coro *next, *prev; |
299 | struct coro *next, *prev; |
290 | }; |
300 | }; |
291 | |
301 | |
292 | typedef struct coro *Coro__State; |
302 | typedef struct coro *Coro__State; |
… | |
… | |
307 | #define PRIO_MIN -4 |
317 | #define PRIO_MIN -4 |
308 | |
318 | |
309 | /* for Coro.pm */ |
319 | /* for Coro.pm */ |
310 | static SV *coro_current; |
320 | static SV *coro_current; |
311 | static SV *coro_readyhook; |
321 | static SV *coro_readyhook; |
312 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
322 | static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */ |
313 | static CV *cv_coro_run, *cv_coro_terminate; |
323 | static CV *cv_coro_run, *cv_coro_terminate; |
314 | static struct coro *coro_first; |
324 | static struct coro *coro_first; |
315 | #define coro_nready coroapi.nready |
325 | #define coro_nready coroapi.nready |
316 | |
326 | |
317 | /** lowlevel stuff **********************************************************/ |
327 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
350 | INLINE CV * |
360 | INLINE CV * |
351 | coro_sv_2cv (pTHX_ SV *sv) |
361 | coro_sv_2cv (pTHX_ SV *sv) |
352 | { |
362 | { |
353 | HV *st; |
363 | HV *st; |
354 | GV *gvp; |
364 | GV *gvp; |
355 | return sv_2cv (sv, &st, &gvp, 0); |
365 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
366 | |
|
|
367 | if (!cv) |
|
|
368 | croak ("code reference expected"); |
|
|
369 | |
|
|
370 | return cv; |
356 | } |
371 | } |
|
|
372 | |
|
|
373 | /*****************************************************************************/ |
|
|
374 | /* magic glue */ |
|
|
375 | |
|
|
376 | #define CORO_MAGIC_type_cv 26 |
|
|
377 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
378 | |
|
|
379 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
380 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
381 | ? SvMAGIC (sv) \ |
|
|
382 | : mg_find (sv, type)) |
|
|
383 | |
|
|
384 | #define CORO_MAGIC(sv, type) \ |
|
|
385 | (expect_true (SvMAGIC (sv)) \ |
|
|
386 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
387 | : 0) |
|
|
388 | |
|
|
389 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
390 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
391 | |
|
|
392 | INLINE struct coro * |
|
|
393 | SvSTATE_ (pTHX_ SV *coro) |
|
|
394 | { |
|
|
395 | HV *stash; |
|
|
396 | MAGIC *mg; |
|
|
397 | |
|
|
398 | if (SvROK (coro)) |
|
|
399 | coro = SvRV (coro); |
|
|
400 | |
|
|
401 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
402 | croak ("Coro::State object required"); |
|
|
403 | |
|
|
404 | stash = SvSTASH (coro); |
|
|
405 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
406 | { |
|
|
407 | /* very slow, but rare, check */ |
|
|
408 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
409 | croak ("Coro::State object required"); |
|
|
410 | } |
|
|
411 | |
|
|
412 | mg = CORO_MAGIC_state (coro); |
|
|
413 | return (struct coro *)mg->mg_ptr; |
|
|
414 | } |
|
|
415 | |
|
|
416 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
417 | |
|
|
418 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
419 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
420 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
421 | |
|
|
422 | /*****************************************************************************/ |
|
|
423 | /* padlist management and caching */ |
357 | |
424 | |
358 | static AV * |
425 | static AV * |
359 | coro_derive_padlist (pTHX_ CV *cv) |
426 | coro_derive_padlist (pTHX_ CV *cv) |
360 | { |
427 | { |
361 | AV *padlist = CvPADLIST (cv); |
428 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
369 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
436 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
370 | #endif |
437 | #endif |
371 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
438 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
372 | --AvFILLp (padlist); |
439 | --AvFILLp (padlist); |
373 | |
440 | |
374 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
441 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
375 | av_store (newpadlist, 1, (SV *)newpad); |
442 | av_store (newpadlist, 1, (SV *)newpad); |
376 | |
443 | |
377 | return newpadlist; |
444 | return newpadlist; |
378 | } |
445 | } |
379 | |
446 | |
380 | static void |
447 | static void |
381 | free_padlist (pTHX_ AV *padlist) |
448 | free_padlist (pTHX_ AV *padlist) |
382 | { |
449 | { |
383 | /* may be during global destruction */ |
450 | /* may be during global destruction */ |
384 | if (SvREFCNT (padlist)) |
451 | if (!IN_DESTRUCT) |
385 | { |
452 | { |
386 | I32 i = AvFILLp (padlist); |
453 | I32 i = AvFILLp (padlist); |
387 | while (i >= 0) |
454 | |
|
|
455 | while (i > 0) /* special-case index 0 */ |
388 | { |
456 | { |
389 | SV **svp = av_fetch (padlist, i--, FALSE); |
457 | /* we try to be extra-careful here */ |
390 | if (svp) |
458 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
391 | { |
459 | I32 j = AvFILLp (av); |
392 | SV *sv; |
460 | |
393 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
461 | while (j >= 0) |
|
|
462 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
463 | |
|
|
464 | AvFILLp (av) = -1; |
394 | SvREFCNT_dec (sv); |
465 | SvREFCNT_dec (av); |
395 | |
|
|
396 | SvREFCNT_dec (*svp); |
|
|
397 | } |
|
|
398 | } |
466 | } |
399 | |
467 | |
|
|
468 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
469 | |
|
|
470 | AvFILLp (padlist) = -1; |
400 | SvREFCNT_dec ((SV*)padlist); |
471 | SvREFCNT_dec ((SV*)padlist); |
401 | } |
472 | } |
402 | } |
473 | } |
403 | |
474 | |
404 | static int |
475 | static int |
… | |
… | |
413 | |
484 | |
414 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
485 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
415 | |
486 | |
416 | return 0; |
487 | return 0; |
417 | } |
488 | } |
418 | |
|
|
419 | #define CORO_MAGIC_type_cv 26 |
|
|
420 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
421 | |
489 | |
422 | static MGVTBL coro_cv_vtbl = { |
490 | static MGVTBL coro_cv_vtbl = { |
423 | 0, 0, 0, 0, |
491 | 0, 0, 0, 0, |
424 | coro_cv_free |
492 | coro_cv_free |
425 | }; |
493 | }; |
426 | |
|
|
427 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
428 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
429 | ? SvMAGIC (sv) \ |
|
|
430 | : mg_find (sv, type)) |
|
|
431 | |
|
|
432 | #define CORO_MAGIC(sv, type) \ |
|
|
433 | (expect_true (SvMAGIC (sv)) \ |
|
|
434 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
435 | : 0) |
|
|
436 | |
|
|
437 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
438 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
439 | |
|
|
440 | INLINE struct coro * |
|
|
441 | SvSTATE_ (pTHX_ SV *coro) |
|
|
442 | { |
|
|
443 | HV *stash; |
|
|
444 | MAGIC *mg; |
|
|
445 | |
|
|
446 | if (SvROK (coro)) |
|
|
447 | coro = SvRV (coro); |
|
|
448 | |
|
|
449 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
450 | croak ("Coro::State object required"); |
|
|
451 | |
|
|
452 | stash = SvSTASH (coro); |
|
|
453 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
454 | { |
|
|
455 | /* very slow, but rare, check */ |
|
|
456 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
457 | croak ("Coro::State object required"); |
|
|
458 | } |
|
|
459 | |
|
|
460 | mg = CORO_MAGIC_state (coro); |
|
|
461 | return (struct coro *)mg->mg_ptr; |
|
|
462 | } |
|
|
463 | |
|
|
464 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
465 | |
|
|
466 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
467 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
468 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
469 | |
494 | |
470 | /* the next two functions merely cache the padlists */ |
495 | /* the next two functions merely cache the padlists */ |
471 | static void |
496 | static void |
472 | get_padlist (pTHX_ CV *cv) |
497 | get_padlist (pTHX_ CV *cv) |
473 | { |
498 | { |
… | |
… | |
501 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
526 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
502 | |
527 | |
503 | av = (AV *)mg->mg_obj; |
528 | av = (AV *)mg->mg_obj; |
504 | |
529 | |
505 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
530 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
506 | av_extend (av, AvMAX (av) + 1); |
531 | av_extend (av, AvFILLp (av) + 1); |
507 | |
532 | |
508 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
533 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
509 | } |
534 | } |
510 | |
535 | |
511 | /** load & save, init *******************************************************/ |
536 | /** load & save, init *******************************************************/ |
|
|
537 | |
|
|
538 | static void |
|
|
539 | on_enterleave_call (pTHX_ SV *cb); |
512 | |
540 | |
513 | static void |
541 | static void |
514 | load_perl (pTHX_ Coro__State c) |
542 | load_perl (pTHX_ Coro__State c) |
515 | { |
543 | { |
516 | perl_slots *slot = c->slot; |
544 | perl_slots *slot = c->slot; |
517 | c->slot = 0; |
545 | c->slot = 0; |
518 | |
546 | |
519 | PL_mainstack = c->mainstack; |
547 | PL_mainstack = c->mainstack; |
520 | |
548 | |
521 | GvSV (PL_defgv) = slot->defsv; |
549 | GvSV (PL_defgv) = slot->defsv; |
522 | GvAV (PL_defgv) = slot->defav; |
550 | GvAV (PL_defgv) = slot->defav; |
523 | GvSV (PL_errgv) = slot->errsv; |
551 | GvSV (PL_errgv) = slot->errsv; |
524 | GvSV (irsgv) = slot->irsgv; |
552 | GvSV (irsgv) = slot->irsgv; |
|
|
553 | GvHV (PL_hintgv) = slot->hinthv; |
525 | |
554 | |
526 | #define VAR(name,type) PL_ ## name = slot->name; |
555 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | # include "state.h" |
556 | # include "state.h" |
528 | #undef VAR |
557 | #undef VAR |
529 | |
558 | |
… | |
… | |
543 | PUTBACK; |
572 | PUTBACK; |
544 | } |
573 | } |
545 | |
574 | |
546 | slf_frame = c->slf_frame; |
575 | slf_frame = c->slf_frame; |
547 | CORO_THROW = c->except; |
576 | CORO_THROW = c->except; |
|
|
577 | |
|
|
578 | if (expect_false (c->on_enter)) |
|
|
579 | { |
|
|
580 | int i; |
|
|
581 | |
|
|
582 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
583 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
584 | } |
548 | } |
585 | } |
549 | |
586 | |
550 | static void |
587 | static void |
551 | save_perl (pTHX_ Coro__State c) |
588 | save_perl (pTHX_ Coro__State c) |
552 | { |
589 | { |
|
|
590 | if (expect_false (c->on_leave)) |
|
|
591 | { |
|
|
592 | int i; |
|
|
593 | |
|
|
594 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
595 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
596 | } |
|
|
597 | |
553 | c->except = CORO_THROW; |
598 | c->except = CORO_THROW; |
554 | c->slf_frame = slf_frame; |
599 | c->slf_frame = slf_frame; |
555 | |
600 | |
556 | { |
601 | { |
557 | dSP; |
602 | dSP; |
… | |
… | |
615 | c->mainstack = PL_mainstack; |
660 | c->mainstack = PL_mainstack; |
616 | |
661 | |
617 | { |
662 | { |
618 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
663 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | |
664 | |
620 | slot->defav = GvAV (PL_defgv); |
665 | slot->defav = GvAV (PL_defgv); |
621 | slot->defsv = DEFSV; |
666 | slot->defsv = DEFSV; |
622 | slot->errsv = ERRSV; |
667 | slot->errsv = ERRSV; |
623 | slot->irsgv = GvSV (irsgv); |
668 | slot->irsgv = GvSV (irsgv); |
|
|
669 | slot->hinthv = GvHV (PL_hintgv); |
624 | |
670 | |
625 | #define VAR(name,type) slot->name = PL_ ## name; |
671 | #define VAR(name,type) slot->name = PL_ ## name; |
626 | # include "state.h" |
672 | # include "state.h" |
627 | #undef VAR |
673 | #undef VAR |
628 | } |
674 | } |
… | |
… | |
755 | #endif |
801 | #endif |
756 | |
802 | |
757 | /* |
803 | /* |
758 | * This overrides the default magic get method of %SIG elements. |
804 | * This overrides the default magic get method of %SIG elements. |
759 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
805 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
760 | * and instead of tryign to save and restore the hash elements, we just provide |
806 | * and instead of trying to save and restore the hash elements, we just provide |
761 | * readback here. |
807 | * readback here. |
762 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
763 | * not expecting this to actually change the hook. This is a potential problem |
|
|
764 | * when a schedule happens then, but we ignore this. |
|
|
765 | */ |
808 | */ |
766 | static int |
809 | static int |
767 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
810 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
768 | { |
811 | { |
769 | const char *s = MgPV_nolen_const (mg); |
812 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
822 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
865 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
823 | |
866 | |
824 | if (svp) |
867 | if (svp) |
825 | { |
868 | { |
826 | SV *old = *svp; |
869 | SV *old = *svp; |
827 | *svp = newSVsv (sv); |
870 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
828 | SvREFCNT_dec (old); |
871 | SvREFCNT_dec (old); |
829 | return 0; |
872 | return 0; |
830 | } |
873 | } |
831 | } |
874 | } |
832 | |
875 | |
… | |
… | |
864 | |
907 | |
865 | PL_runops = RUNOPS_DEFAULT; |
908 | PL_runops = RUNOPS_DEFAULT; |
866 | PL_curcop = &PL_compiling; |
909 | PL_curcop = &PL_compiling; |
867 | PL_in_eval = EVAL_NULL; |
910 | PL_in_eval = EVAL_NULL; |
868 | PL_comppad = 0; |
911 | PL_comppad = 0; |
|
|
912 | PL_comppad_name = 0; |
|
|
913 | PL_comppad_name_fill = 0; |
|
|
914 | PL_comppad_name_floor = 0; |
869 | PL_curpm = 0; |
915 | PL_curpm = 0; |
870 | PL_curpad = 0; |
916 | PL_curpad = 0; |
871 | PL_localizing = 0; |
917 | PL_localizing = 0; |
872 | PL_dirty = 0; |
918 | PL_dirty = 0; |
873 | PL_restartop = 0; |
919 | PL_restartop = 0; |
874 | #if PERL_VERSION_ATLEAST (5,10,0) |
920 | #if PERL_VERSION_ATLEAST (5,10,0) |
875 | PL_parser = 0; |
921 | PL_parser = 0; |
876 | #endif |
922 | #endif |
|
|
923 | PL_hints = 0; |
877 | |
924 | |
878 | /* recreate the die/warn hooks */ |
925 | /* recreate the die/warn hooks */ |
879 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
926 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
880 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
927 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
881 | |
928 | |
882 | GvSV (PL_defgv) = newSV (0); |
929 | GvSV (PL_defgv) = newSV (0); |
883 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
930 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
884 | GvSV (PL_errgv) = newSV (0); |
931 | GvSV (PL_errgv) = newSV (0); |
885 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
932 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
933 | GvHV (PL_hintgv) = 0; |
886 | PL_rs = newSVsv (GvSV (irsgv)); |
934 | PL_rs = newSVsv (GvSV (irsgv)); |
887 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
935 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
888 | |
936 | |
889 | { |
937 | { |
890 | dSP; |
938 | dSP; |
… | |
… | |
919 | /* copy throw, in case it was set before coro_setup */ |
967 | /* copy throw, in case it was set before coro_setup */ |
920 | CORO_THROW = coro->except; |
968 | CORO_THROW = coro->except; |
921 | } |
969 | } |
922 | |
970 | |
923 | static void |
971 | static void |
924 | coro_destruct (pTHX_ struct coro *coro) |
972 | coro_unwind_stacks (pTHX) |
925 | { |
973 | { |
926 | if (!IN_DESTRUCT) |
974 | if (!IN_DESTRUCT) |
927 | { |
975 | { |
928 | /* restore all saved variables and stuff */ |
976 | /* restore all saved variables and stuff */ |
929 | LEAVE_SCOPE (0); |
977 | LEAVE_SCOPE (0); |
… | |
… | |
937 | POPSTACK_TO (PL_mainstack); |
985 | POPSTACK_TO (PL_mainstack); |
938 | |
986 | |
939 | /* unwind main stack */ |
987 | /* unwind main stack */ |
940 | dounwind (-1); |
988 | dounwind (-1); |
941 | } |
989 | } |
|
|
990 | } |
|
|
991 | |
|
|
992 | static void |
|
|
993 | coro_destruct_perl (pTHX_ struct coro *coro) |
|
|
994 | { |
|
|
995 | coro_unwind_stacks (aTHX); |
942 | |
996 | |
943 | SvREFCNT_dec (GvSV (PL_defgv)); |
997 | SvREFCNT_dec (GvSV (PL_defgv)); |
944 | SvREFCNT_dec (GvAV (PL_defgv)); |
998 | SvREFCNT_dec (GvAV (PL_defgv)); |
945 | SvREFCNT_dec (GvSV (PL_errgv)); |
999 | SvREFCNT_dec (GvSV (PL_errgv)); |
946 | SvREFCNT_dec (PL_defoutgv); |
1000 | SvREFCNT_dec (PL_defoutgv); |
947 | SvREFCNT_dec (PL_rs); |
1001 | SvREFCNT_dec (PL_rs); |
948 | SvREFCNT_dec (GvSV (irsgv)); |
1002 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
1003 | SvREFCNT_dec (GvHV (PL_hintgv)); |
949 | |
1004 | |
950 | SvREFCNT_dec (PL_diehook); |
1005 | SvREFCNT_dec (PL_diehook); |
951 | SvREFCNT_dec (PL_warnhook); |
1006 | SvREFCNT_dec (PL_warnhook); |
952 | |
1007 | |
953 | SvREFCNT_dec (coro->saved_deffh); |
1008 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
1168 | /* |
1223 | /* |
1169 | * If perl-run returns we assume exit() was being called or the coro |
1224 | * If perl-run returns we assume exit() was being called or the coro |
1170 | * fell off the end, which seems to be the only valid (non-bug) |
1225 | * fell off the end, which seems to be the only valid (non-bug) |
1171 | * reason for perl_run to return. We try to exit by jumping to the |
1226 | * reason for perl_run to return. We try to exit by jumping to the |
1172 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1227 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1173 | * coroutine as Coro has no such concept |
1228 | * coroutine as Coro has no such concept. |
|
|
1229 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1230 | * that backend are too broken to do it in a standards-compliant way. |
1174 | */ |
1231 | */ |
1175 | PL_top_env = main_top_env; |
1232 | PL_top_env = main_top_env; |
1176 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1233 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1177 | } |
1234 | } |
1178 | } |
1235 | } |
… | |
… | |
1255 | cctx_destroy (coro_cctx *cctx) |
1312 | cctx_destroy (coro_cctx *cctx) |
1256 | { |
1313 | { |
1257 | if (!cctx) |
1314 | if (!cctx) |
1258 | return; |
1315 | return; |
1259 | |
1316 | |
1260 | assert (cctx != cctx_current);//D temporary |
1317 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1261 | |
1318 | |
1262 | --cctx_count; |
1319 | --cctx_count; |
1263 | coro_destroy (&cctx->cctx); |
1320 | coro_destroy (&cctx->cctx); |
1264 | |
1321 | |
1265 | /* coro_transfer creates new, empty cctx's */ |
1322 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1329 | /* TODO: throwing up here is considered harmful */ |
1386 | /* TODO: throwing up here is considered harmful */ |
1330 | |
1387 | |
1331 | if (expect_true (prev != next)) |
1388 | if (expect_true (prev != next)) |
1332 | { |
1389 | { |
1333 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1390 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1334 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1391 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1335 | |
1392 | |
1336 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1337 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
|
|
1338 | |
|
|
1339 | if (expect_false (next->flags & CF_DESTROYED)) |
1393 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1340 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1394 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1341 | |
1395 | |
1342 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1396 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1343 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1397 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1344 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1398 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1345 | #endif |
1399 | #endif |
… | |
… | |
1383 | coro_setup (aTHX_ next); |
1437 | coro_setup (aTHX_ next); |
1384 | } |
1438 | } |
1385 | else |
1439 | else |
1386 | load_perl (aTHX_ next); |
1440 | load_perl (aTHX_ next); |
1387 | |
1441 | |
1388 | assert (!prev->cctx);//D temporary |
|
|
1389 | |
|
|
1390 | /* possibly untie and reuse the cctx */ |
1442 | /* possibly untie and reuse the cctx */ |
1391 | if (expect_true ( |
1443 | if (expect_true ( |
1392 | cctx_current->idle_sp == STACKLEVEL |
1444 | cctx_current->idle_sp == STACKLEVEL |
1393 | && !(cctx_current->flags & CC_TRACE) |
1445 | && !(cctx_current->flags & CC_TRACE) |
1394 | && !force_cctx |
1446 | && !force_cctx |
… | |
… | |
1435 | coro_state_destroy (pTHX_ struct coro *coro) |
1487 | coro_state_destroy (pTHX_ struct coro *coro) |
1436 | { |
1488 | { |
1437 | if (coro->flags & CF_DESTROYED) |
1489 | if (coro->flags & CF_DESTROYED) |
1438 | return 0; |
1490 | return 0; |
1439 | |
1491 | |
1440 | if (coro->on_destroy) |
1492 | if (coro->on_destroy && !PL_dirty) |
1441 | coro->on_destroy (aTHX_ coro); |
1493 | coro->on_destroy (aTHX_ coro); |
1442 | |
1494 | |
1443 | coro->flags |= CF_DESTROYED; |
1495 | coro->flags |= CF_DESTROYED; |
1444 | |
1496 | |
1445 | if (coro->flags & CF_READY) |
1497 | if (coro->flags & CF_READY) |
… | |
… | |
1449 | --coro_nready; |
1501 | --coro_nready; |
1450 | } |
1502 | } |
1451 | else |
1503 | else |
1452 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1504 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1453 | |
1505 | |
1454 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1506 | if (coro->mainstack |
|
|
1507 | && coro->mainstack != main_mainstack |
|
|
1508 | && coro->slot |
|
|
1509 | && !PL_dirty) |
1455 | { |
1510 | { |
1456 | struct coro temp; |
1511 | struct coro *current = SvSTATE_current; |
1457 | |
1512 | |
1458 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1513 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1459 | |
1514 | |
1460 | save_perl (aTHX_ &temp); |
1515 | save_perl (aTHX_ current); |
1461 | load_perl (aTHX_ coro); |
1516 | load_perl (aTHX_ coro); |
1462 | |
1517 | |
1463 | coro_destruct (aTHX_ coro); |
1518 | coro_destruct_perl (aTHX_ coro); |
1464 | |
1519 | |
1465 | load_perl (aTHX_ &temp); |
1520 | load_perl (aTHX_ current); |
1466 | |
1521 | |
1467 | coro->slot = 0; |
1522 | coro->slot = 0; |
1468 | } |
1523 | } |
1469 | |
1524 | |
1470 | cctx_destroy (coro->cctx); |
1525 | cctx_destroy (coro->cctx); |
… | |
… | |
1560 | /** Coro ********************************************************************/ |
1615 | /** Coro ********************************************************************/ |
1561 | |
1616 | |
1562 | INLINE void |
1617 | INLINE void |
1563 | coro_enq (pTHX_ struct coro *coro) |
1618 | coro_enq (pTHX_ struct coro *coro) |
1564 | { |
1619 | { |
1565 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1620 | struct coro **ready = coro_ready [coro->prio - PRIO_MIN]; |
1566 | } |
|
|
1567 | |
1621 | |
1568 | INLINE SV * |
1622 | SvREFCNT_inc_NN (coro->hv); |
|
|
1623 | |
|
|
1624 | coro->next_ready = 0; |
|
|
1625 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
|
|
1626 | ready [1] = coro; |
|
|
1627 | } |
|
|
1628 | |
|
|
1629 | INLINE struct coro * |
1569 | coro_deq (pTHX) |
1630 | coro_deq (pTHX) |
1570 | { |
1631 | { |
1571 | int prio; |
1632 | int prio; |
1572 | |
1633 | |
1573 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1634 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1574 | if (AvFILLp (coro_ready [prio]) >= 0) |
1635 | { |
1575 | return av_shift (coro_ready [prio]); |
1636 | struct coro **ready = coro_ready [prio]; |
|
|
1637 | |
|
|
1638 | if (ready [0]) |
|
|
1639 | { |
|
|
1640 | struct coro *coro = ready [0]; |
|
|
1641 | ready [0] = coro->next_ready; |
|
|
1642 | return coro; |
|
|
1643 | } |
|
|
1644 | } |
1576 | |
1645 | |
1577 | return 0; |
1646 | return 0; |
1578 | } |
1647 | } |
1579 | |
1648 | |
1580 | static int |
1649 | static int |
… | |
… | |
1582 | { |
1651 | { |
1583 | struct coro *coro; |
1652 | struct coro *coro; |
1584 | SV *sv_hook; |
1653 | SV *sv_hook; |
1585 | void (*xs_hook)(void); |
1654 | void (*xs_hook)(void); |
1586 | |
1655 | |
1587 | if (SvROK (coro_sv)) |
|
|
1588 | coro_sv = SvRV (coro_sv); |
|
|
1589 | |
|
|
1590 | coro = SvSTATE (coro_sv); |
1656 | coro = SvSTATE (coro_sv); |
1591 | |
1657 | |
1592 | if (coro->flags & CF_READY) |
1658 | if (coro->flags & CF_READY) |
1593 | return 0; |
1659 | return 0; |
1594 | |
1660 | |
… | |
… | |
1647 | static void |
1713 | static void |
1648 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1714 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1649 | { |
1715 | { |
1650 | for (;;) |
1716 | for (;;) |
1651 | { |
1717 | { |
1652 | SV *next_sv = coro_deq (aTHX); |
1718 | struct coro *next = coro_deq (aTHX); |
1653 | |
1719 | |
1654 | if (expect_true (next_sv)) |
1720 | if (expect_true (next)) |
1655 | { |
1721 | { |
1656 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1657 | |
|
|
1658 | /* cannot transfer to destroyed coros, skip and look for next */ |
1722 | /* cannot transfer to destroyed coros, skip and look for next */ |
1659 | if (expect_false (next->flags & CF_DESTROYED)) |
1723 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1660 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1724 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1661 | else |
1725 | else |
1662 | { |
1726 | { |
1663 | next->flags &= ~CF_READY; |
1727 | next->flags &= ~CF_READY; |
1664 | --coro_nready; |
1728 | --coro_nready; |
1665 | |
1729 | |
… | |
… | |
1668 | } |
1732 | } |
1669 | } |
1733 | } |
1670 | else |
1734 | else |
1671 | { |
1735 | { |
1672 | /* nothing to schedule: call the idle handler */ |
1736 | /* nothing to schedule: call the idle handler */ |
|
|
1737 | if (SvROK (sv_idle) |
|
|
1738 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1739 | { |
|
|
1740 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1741 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1742 | --coro_nready; |
|
|
1743 | } |
|
|
1744 | else |
|
|
1745 | { |
1673 | dSP; |
1746 | dSP; |
1674 | |
1747 | |
1675 | ENTER; |
1748 | ENTER; |
1676 | SAVETMPS; |
1749 | SAVETMPS; |
1677 | |
1750 | |
1678 | PUSHMARK (SP); |
1751 | PUSHMARK (SP); |
1679 | PUTBACK; |
1752 | PUTBACK; |
1680 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1753 | call_sv (sv_idle, G_VOID | G_DISCARD); |
1681 | |
1754 | |
1682 | FREETMPS; |
1755 | FREETMPS; |
1683 | LEAVE; |
1756 | LEAVE; |
|
|
1757 | } |
1684 | } |
1758 | } |
1685 | } |
1759 | } |
1686 | } |
1760 | } |
1687 | |
1761 | |
1688 | INLINE void |
1762 | INLINE void |
… | |
… | |
1833 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1907 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1834 | api_ready (aTHX_ sv_manager); |
1908 | api_ready (aTHX_ sv_manager); |
1835 | |
1909 | |
1836 | frame->prepare = prepare_schedule; |
1910 | frame->prepare = prepare_schedule; |
1837 | frame->check = slf_check_repeat; |
1911 | frame->check = slf_check_repeat; |
|
|
1912 | |
|
|
1913 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1914 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1915 | /*coro_unwind_stacks (aTHX);*/ |
1838 | } |
1916 | } |
1839 | |
1917 | |
1840 | /*****************************************************************************/ |
1918 | /*****************************************************************************/ |
1841 | /* async pool handler */ |
1919 | /* async pool handler */ |
1842 | |
1920 | |
… | |
… | |
1925 | SV *data = (SV *)GENSUB_ARG; |
2003 | SV *data = (SV *)GENSUB_ARG; |
1926 | |
2004 | |
1927 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2005 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1928 | { |
2006 | { |
1929 | /* first call, set args */ |
2007 | /* first call, set args */ |
|
|
2008 | SV *coro = SvRV (data); |
1930 | AV *av = newAV (); |
2009 | AV *av = newAV (); |
1931 | SV *coro = SvRV (data); |
|
|
1932 | |
2010 | |
1933 | SvRV_set (data, (SV *)av); |
2011 | SvRV_set (data, (SV *)av); |
1934 | api_ready (aTHX_ coro); |
|
|
1935 | SvREFCNT_dec (coro); |
|
|
1936 | |
2012 | |
1937 | /* better take a full copy of the arguments */ |
2013 | /* better take a full copy of the arguments */ |
1938 | while (items--) |
2014 | while (items--) |
1939 | av_store (av, items, newSVsv (ST (items))); |
2015 | av_store (av, items, newSVsv (ST (items))); |
|
|
2016 | |
|
|
2017 | api_ready (aTHX_ coro); |
|
|
2018 | SvREFCNT_dec (coro); |
1940 | } |
2019 | } |
1941 | |
2020 | |
1942 | XSRETURN_EMPTY; |
2021 | XSRETURN_EMPTY; |
1943 | } |
2022 | } |
1944 | |
2023 | |
… | |
… | |
1961 | |
2040 | |
1962 | EXTEND (SP, AvFILLp (av) + 1); |
2041 | EXTEND (SP, AvFILLp (av) + 1); |
1963 | for (i = 0; i <= AvFILLp (av); ++i) |
2042 | for (i = 0; i <= AvFILLp (av); ++i) |
1964 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2043 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
1965 | |
2044 | |
1966 | /* we have stolen the elements, so ste length to zero and free */ |
2045 | /* we have stolen the elements, so set length to zero and free */ |
1967 | AvFILLp (av) = -1; |
2046 | AvFILLp (av) = -1; |
1968 | av_undef (av); |
2047 | av_undef (av); |
1969 | |
2048 | |
1970 | PUTBACK; |
2049 | PUTBACK; |
1971 | } |
2050 | } |
… | |
… | |
2261 | |
2340 | |
2262 | PL_op->op_ppaddr = pp_slf; |
2341 | PL_op->op_ppaddr = pp_slf; |
2263 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2342 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2264 | |
2343 | |
2265 | PL_op = (OP *)&slf_restore; |
2344 | PL_op = (OP *)&slf_restore; |
|
|
2345 | } |
|
|
2346 | |
|
|
2347 | /*****************************************************************************/ |
|
|
2348 | /* dynamic wind */ |
|
|
2349 | |
|
|
2350 | static void |
|
|
2351 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2352 | { |
|
|
2353 | dSP; |
|
|
2354 | |
|
|
2355 | PUSHSTACK; |
|
|
2356 | |
|
|
2357 | PUSHMARK (SP); |
|
|
2358 | PUTBACK; |
|
|
2359 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2360 | SPAGAIN; |
|
|
2361 | |
|
|
2362 | POPSTACK; |
|
|
2363 | } |
|
|
2364 | |
|
|
2365 | static SV * |
|
|
2366 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2367 | { |
|
|
2368 | AV *av = *avp; |
|
|
2369 | SV *res = av_pop (av); |
|
|
2370 | |
|
|
2371 | if (AvFILLp (av) < 0) |
|
|
2372 | { |
|
|
2373 | *avp = 0; |
|
|
2374 | SvREFCNT_dec (av); |
|
|
2375 | } |
|
|
2376 | |
|
|
2377 | return res; |
|
|
2378 | } |
|
|
2379 | |
|
|
2380 | static void |
|
|
2381 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2382 | { |
|
|
2383 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2384 | SvREFCNT_dec (cb); |
|
|
2385 | } |
|
|
2386 | |
|
|
2387 | static void |
|
|
2388 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2389 | { |
|
|
2390 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2391 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2266 | } |
2392 | } |
2267 | |
2393 | |
2268 | /*****************************************************************************/ |
2394 | /*****************************************************************************/ |
2269 | /* PerlIO::cede */ |
2395 | /* PerlIO::cede */ |
2270 | |
2396 | |
… | |
… | |
2351 | SV **ary; |
2477 | SV **ary; |
2352 | |
2478 | |
2353 | /* unfortunately, building manually saves memory */ |
2479 | /* unfortunately, building manually saves memory */ |
2354 | Newx (ary, 2, SV *); |
2480 | Newx (ary, 2, SV *); |
2355 | AvALLOC (av) = ary; |
2481 | AvALLOC (av) = ary; |
|
|
2482 | #if PERL_VERSION_ATLEAST (5,10,0) |
2356 | /*AvARRAY (av) = ary;*/ |
2483 | AvARRAY (av) = ary; |
|
|
2484 | #else |
2357 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2485 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2486 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2487 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2488 | #endif |
2358 | AvMAX (av) = 1; |
2489 | AvMAX (av) = 1; |
2359 | AvFILLp (av) = 0; |
2490 | AvFILLp (av) = 0; |
2360 | ary [0] = newSViv (count); |
2491 | ary [0] = newSViv (count); |
2361 | |
2492 | |
2362 | return newRV_noinc ((SV *)av); |
2493 | return newRV_noinc ((SV *)av); |
… | |
… | |
2891 | SV * |
3022 | SV * |
2892 | clone (Coro::State coro) |
3023 | clone (Coro::State coro) |
2893 | CODE: |
3024 | CODE: |
2894 | { |
3025 | { |
2895 | #if CORO_CLONE |
3026 | #if CORO_CLONE |
2896 | struct coro *ncoro = coro_clone (coro); |
3027 | struct coro *ncoro = coro_clone (aTHX_ coro); |
2897 | MAGIC *mg; |
3028 | MAGIC *mg; |
2898 | /* TODO: too much duplication */ |
3029 | /* TODO: too much duplication */ |
2899 | ncoro->hv = newHV (); |
3030 | ncoro->hv = newHV (); |
2900 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
3031 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2901 | mg->mg_flags |= MGf_DUP; |
3032 | mg->mg_flags |= MGf_DUP; |
… | |
… | |
2963 | eval = 1 |
3094 | eval = 1 |
2964 | CODE: |
3095 | CODE: |
2965 | { |
3096 | { |
2966 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3097 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2967 | { |
3098 | { |
2968 | struct coro temp; |
3099 | struct coro *current = SvSTATE_current; |
2969 | |
3100 | |
2970 | if (!(coro->flags & CF_RUNNING)) |
3101 | if (current != coro) |
2971 | { |
3102 | { |
2972 | PUTBACK; |
3103 | PUTBACK; |
2973 | save_perl (aTHX_ &temp); |
3104 | save_perl (aTHX_ current); |
2974 | load_perl (aTHX_ coro); |
3105 | load_perl (aTHX_ coro); |
|
|
3106 | SPAGAIN; |
2975 | } |
3107 | } |
2976 | |
3108 | |
2977 | { |
|
|
2978 | dSP; |
|
|
2979 | ENTER; |
|
|
2980 | SAVETMPS; |
|
|
2981 | PUTBACK; |
|
|
2982 | PUSHSTACK; |
3109 | PUSHSTACK; |
|
|
3110 | |
2983 | PUSHMARK (SP); |
3111 | PUSHMARK (SP); |
|
|
3112 | PUTBACK; |
2984 | |
3113 | |
2985 | if (ix) |
3114 | if (ix) |
2986 | eval_sv (coderef, 0); |
3115 | eval_sv (coderef, 0); |
2987 | else |
3116 | else |
2988 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3117 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
2989 | |
3118 | |
2990 | POPSTACK; |
3119 | POPSTACK; |
2991 | SPAGAIN; |
3120 | SPAGAIN; |
2992 | FREETMPS; |
|
|
2993 | LEAVE; |
|
|
2994 | PUTBACK; |
|
|
2995 | } |
|
|
2996 | |
3121 | |
2997 | if (!(coro->flags & CF_RUNNING)) |
3122 | if (current != coro) |
2998 | { |
3123 | { |
|
|
3124 | PUTBACK; |
2999 | save_perl (aTHX_ coro); |
3125 | save_perl (aTHX_ coro); |
3000 | load_perl (aTHX_ &temp); |
3126 | load_perl (aTHX_ current); |
3001 | SPAGAIN; |
3127 | SPAGAIN; |
3002 | } |
3128 | } |
3003 | } |
3129 | } |
3004 | } |
3130 | } |
3005 | |
3131 | |
… | |
… | |
3009 | ALIAS: |
3135 | ALIAS: |
3010 | is_ready = CF_READY |
3136 | is_ready = CF_READY |
3011 | is_running = CF_RUNNING |
3137 | is_running = CF_RUNNING |
3012 | is_new = CF_NEW |
3138 | is_new = CF_NEW |
3013 | is_destroyed = CF_DESTROYED |
3139 | is_destroyed = CF_DESTROYED |
|
|
3140 | is_suspended = CF_SUSPENDED |
3014 | CODE: |
3141 | CODE: |
3015 | RETVAL = boolSV (coro->flags & ix); |
3142 | RETVAL = boolSV (coro->flags & ix); |
3016 | OUTPUT: |
3143 | OUTPUT: |
3017 | RETVAL |
3144 | RETVAL |
3018 | |
3145 | |
… | |
… | |
3083 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3210 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3084 | |
3211 | |
3085 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3212 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3086 | } |
3213 | } |
3087 | |
3214 | |
|
|
3215 | void |
|
|
3216 | cancel (Coro::State self) |
|
|
3217 | CODE: |
|
|
3218 | coro_state_destroy (aTHX_ self); |
|
|
3219 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3220 | |
3088 | |
3221 | |
3089 | MODULE = Coro::State PACKAGE = Coro |
3222 | MODULE = Coro::State PACKAGE = Coro |
3090 | |
3223 | |
3091 | BOOT: |
3224 | BOOT: |
3092 | { |
3225 | { |
3093 | int i; |
3226 | int i; |
3094 | |
3227 | |
3095 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3228 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3096 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3229 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3097 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3230 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3098 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3231 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3099 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3232 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3100 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3233 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3101 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3234 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3102 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3235 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3236 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3103 | |
3237 | |
3104 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3238 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3105 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3239 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3106 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3240 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3107 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3241 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
… | |
… | |
3112 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3246 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3113 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3247 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3114 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3248 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3115 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3249 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3116 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3250 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3117 | |
|
|
3118 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
3119 | coro_ready[i] = newAV (); |
|
|
3120 | |
3251 | |
3121 | { |
3252 | { |
3122 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3253 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3123 | |
3254 | |
3124 | coroapi.schedule = api_schedule; |
3255 | coroapi.schedule = api_schedule; |
… | |
… | |
3165 | cede_notself (...) |
3296 | cede_notself (...) |
3166 | CODE: |
3297 | CODE: |
3167 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3298 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3168 | |
3299 | |
3169 | void |
3300 | void |
3170 | _cancel (Coro::State self) |
|
|
3171 | CODE: |
|
|
3172 | coro_state_destroy (aTHX_ self); |
|
|
3173 | coro_call_on_destroy (aTHX_ self); |
|
|
3174 | |
|
|
3175 | void |
|
|
3176 | _set_current (SV *current) |
3301 | _set_current (SV *current) |
3177 | PROTOTYPE: $ |
3302 | PROTOTYPE: $ |
3178 | CODE: |
3303 | CODE: |
3179 | SvREFCNT_dec (SvRV (coro_current)); |
3304 | SvREFCNT_dec (SvRV (coro_current)); |
3180 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3305 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
… | |
… | |
3222 | PROTOTYPE: |
3347 | PROTOTYPE: |
3223 | CODE: |
3348 | CODE: |
3224 | RETVAL = coro_nready; |
3349 | RETVAL = coro_nready; |
3225 | OUTPUT: |
3350 | OUTPUT: |
3226 | RETVAL |
3351 | RETVAL |
|
|
3352 | |
|
|
3353 | void |
|
|
3354 | suspend (Coro::State self) |
|
|
3355 | PROTOTYPE: $ |
|
|
3356 | CODE: |
|
|
3357 | self->flags |= CF_SUSPENDED; |
|
|
3358 | |
|
|
3359 | void |
|
|
3360 | resume (Coro::State self) |
|
|
3361 | PROTOTYPE: $ |
|
|
3362 | CODE: |
|
|
3363 | self->flags &= ~CF_SUSPENDED; |
3227 | |
3364 | |
3228 | void |
3365 | void |
3229 | _pool_handler (...) |
3366 | _pool_handler (...) |
3230 | CODE: |
3367 | CODE: |
3231 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
3368 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
… | |
… | |
3286 | rouse_wait (...) |
3423 | rouse_wait (...) |
3287 | PROTOTYPE: ;$ |
3424 | PROTOTYPE: ;$ |
3288 | PPCODE: |
3425 | PPCODE: |
3289 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3426 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3290 | |
3427 | |
|
|
3428 | void |
|
|
3429 | on_enter (SV *block) |
|
|
3430 | ALIAS: |
|
|
3431 | on_leave = 1 |
|
|
3432 | PROTOTYPE: & |
|
|
3433 | CODE: |
|
|
3434 | { |
|
|
3435 | struct coro *coro = SvSTATE_current; |
|
|
3436 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3437 | |
|
|
3438 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3439 | |
|
|
3440 | if (!*avp) |
|
|
3441 | *avp = newAV (); |
|
|
3442 | |
|
|
3443 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3444 | |
|
|
3445 | if (!ix) |
|
|
3446 | on_enterleave_call (aTHX_ block); |
|
|
3447 | |
|
|
3448 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3449 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3450 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3451 | } |
|
|
3452 | |
3291 | |
3453 | |
3292 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3454 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3293 | |
3455 | |
3294 | BOOT: |
3456 | BOOT: |
3295 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3457 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
3305 | GvSTASH (CvGV (cv)) |
3467 | GvSTASH (CvGV (cv)) |
3306 | ); |
3468 | ); |
3307 | OUTPUT: |
3469 | OUTPUT: |
3308 | RETVAL |
3470 | RETVAL |
3309 | |
3471 | |
3310 | # helper for Coro::Channel |
3472 | # helper for Coro::Channel and others |
3311 | SV * |
3473 | SV * |
3312 | _alloc (int count) |
3474 | _alloc (int count) |
3313 | CODE: |
3475 | CODE: |
3314 | RETVAL = coro_waitarray_new (aTHX_ count); |
3476 | RETVAL = coro_waitarray_new (aTHX_ count); |
3315 | OUTPUT: |
3477 | OUTPUT: |
… | |
… | |
3373 | for (i = 1; i <= wcount; ++i) |
3535 | for (i = 1; i <= wcount; ++i) |
3374 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3536 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3375 | } |
3537 | } |
3376 | } |
3538 | } |
3377 | |
3539 | |
|
|
3540 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
|
|
3541 | |
|
|
3542 | void |
|
|
3543 | _may_delete (SV *sem, int count, int extra_refs) |
|
|
3544 | PPCODE: |
|
|
3545 | { |
|
|
3546 | AV *av = (AV *)SvRV (sem); |
|
|
3547 | |
|
|
3548 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
|
|
3549 | && AvFILLp (av) == 0 /* no waiters, just count */ |
|
|
3550 | && SvIV (AvARRAY (av)[0]) == count) |
|
|
3551 | XSRETURN_YES; |
|
|
3552 | |
|
|
3553 | XSRETURN_NO; |
|
|
3554 | } |
|
|
3555 | |
3378 | MODULE = Coro::State PACKAGE = Coro::Signal |
3556 | MODULE = Coro::State PACKAGE = Coro::Signal |
3379 | |
3557 | |
3380 | SV * |
3558 | SV * |
3381 | new (SV *klass) |
3559 | new (SV *klass) |
3382 | CODE: |
3560 | CODE: |