… | |
… | |
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 |
… | |
… | |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
171 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
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 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
|
|
181 | |
175 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
177 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
178 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
179 | static HV *hv_sig; /* %SIG */ |
186 | static HV *hv_sig; /* %SIG */ |
180 | |
187 | |
181 | /* async_pool helper stuff */ |
188 | /* async_pool helper stuff */ |
182 | static SV *sv_pool_rss; |
189 | static SV *sv_pool_rss; |
183 | static SV *sv_pool_size; |
190 | static SV *sv_pool_size; |
184 | static SV *sv_async_pool_idle; |
191 | static SV *sv_async_pool_idle; /* description string */ |
185 | static AV *av_async_pool; |
192 | static AV *av_async_pool; /* idle pool */ |
186 | static SV *sv_Coro; |
193 | static SV *sv_Coro; /* class string */ |
187 | static CV *cv_pool_handler; |
194 | static CV *cv_pool_handler; |
188 | static CV *cv_coro_state_new; |
195 | static CV *cv_coro_state_new; |
189 | |
196 | |
190 | /* Coro::AnyEvent */ |
197 | /* Coro::AnyEvent */ |
191 | static SV *sv_activity; |
198 | static SV *sv_activity; |
… | |
… | |
222 | int valgrind_id; |
229 | int valgrind_id; |
223 | #endif |
230 | #endif |
224 | unsigned char flags; |
231 | unsigned char flags; |
225 | } coro_cctx; |
232 | } coro_cctx; |
226 | |
233 | |
|
|
234 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
235 | |
|
|
236 | /*****************************************************************************/ |
|
|
237 | |
227 | enum { |
238 | enum { |
228 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
229 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
230 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
231 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
236 | { |
247 | { |
237 | SV *defsv; |
248 | SV *defsv; |
238 | AV *defav; |
249 | AV *defav; |
239 | SV *errsv; |
250 | SV *errsv; |
240 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
241 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
242 | # include "state.h" |
254 | # include "state.h" |
243 | #undef VAR |
255 | #undef VAR |
244 | } perl_slots; |
256 | } perl_slots; |
245 | |
257 | |
… | |
… | |
272 | |
284 | |
273 | /* async_pool */ |
285 | /* async_pool */ |
274 | SV *saved_deffh; |
286 | SV *saved_deffh; |
275 | SV *invoke_cb; |
287 | SV *invoke_cb; |
276 | AV *invoke_av; |
288 | AV *invoke_av; |
|
|
289 | |
|
|
290 | /* on_enter/on_leave */ |
|
|
291 | AV *on_enter; |
|
|
292 | AV *on_leave; |
277 | |
293 | |
278 | /* linked list */ |
294 | /* linked list */ |
279 | struct coro *next, *prev; |
295 | struct coro *next, *prev; |
280 | }; |
296 | }; |
281 | |
297 | |
… | |
… | |
340 | INLINE CV * |
356 | INLINE CV * |
341 | coro_sv_2cv (pTHX_ SV *sv) |
357 | coro_sv_2cv (pTHX_ SV *sv) |
342 | { |
358 | { |
343 | HV *st; |
359 | HV *st; |
344 | GV *gvp; |
360 | GV *gvp; |
345 | 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; |
346 | } |
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 */ |
347 | |
420 | |
348 | static AV * |
421 | static AV * |
349 | coro_clone_padlist (pTHX_ CV *cv) |
422 | coro_derive_padlist (pTHX_ CV *cv) |
350 | { |
423 | { |
351 | AV *padlist = CvPADLIST (cv); |
424 | AV *padlist = CvPADLIST (cv); |
352 | AV *newpadlist, *newpad; |
425 | AV *newpadlist, *newpad; |
353 | |
426 | |
354 | newpadlist = newAV (); |
427 | newpadlist = newAV (); |
… | |
… | |
359 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
432 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
360 | #endif |
433 | #endif |
361 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
434 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
362 | --AvFILLp (padlist); |
435 | --AvFILLp (padlist); |
363 | |
436 | |
364 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
437 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
365 | av_store (newpadlist, 1, (SV *)newpad); |
438 | av_store (newpadlist, 1, (SV *)newpad); |
366 | |
439 | |
367 | return newpadlist; |
440 | return newpadlist; |
368 | } |
441 | } |
369 | |
442 | |
370 | static void |
443 | static void |
371 | free_padlist (pTHX_ AV *padlist) |
444 | free_padlist (pTHX_ AV *padlist) |
372 | { |
445 | { |
373 | /* may be during global destruction */ |
446 | /* may be during global destruction */ |
374 | if (SvREFCNT (padlist)) |
447 | if (!IN_DESTRUCT) |
375 | { |
448 | { |
376 | I32 i = AvFILLp (padlist); |
449 | I32 i = AvFILLp (padlist); |
377 | while (i >= 0) |
450 | |
|
|
451 | while (i > 0) /* special-case index 0 */ |
378 | { |
452 | { |
379 | SV **svp = av_fetch (padlist, i--, FALSE); |
453 | /* we try to be extra-careful here */ |
380 | if (svp) |
454 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
381 | { |
455 | I32 j = AvFILLp (av); |
382 | SV *sv; |
456 | |
383 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
457 | while (j >= 0) |
|
|
458 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
459 | |
|
|
460 | AvFILLp (av) = -1; |
384 | SvREFCNT_dec (sv); |
461 | SvREFCNT_dec (av); |
385 | |
|
|
386 | SvREFCNT_dec (*svp); |
|
|
387 | } |
|
|
388 | } |
462 | } |
389 | |
463 | |
|
|
464 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
465 | |
|
|
466 | AvFILLp (padlist) = -1; |
390 | SvREFCNT_dec ((SV*)padlist); |
467 | SvREFCNT_dec ((SV*)padlist); |
391 | } |
468 | } |
392 | } |
469 | } |
393 | |
470 | |
394 | static int |
471 | static int |
… | |
… | |
403 | |
480 | |
404 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
481 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
405 | |
482 | |
406 | return 0; |
483 | return 0; |
407 | } |
484 | } |
408 | |
|
|
409 | #define CORO_MAGIC_type_cv 26 |
|
|
410 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
411 | |
485 | |
412 | static MGVTBL coro_cv_vtbl = { |
486 | static MGVTBL coro_cv_vtbl = { |
413 | 0, 0, 0, 0, |
487 | 0, 0, 0, 0, |
414 | coro_cv_free |
488 | coro_cv_free |
415 | }; |
489 | }; |
416 | |
|
|
417 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
418 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
419 | ? SvMAGIC (sv) \ |
|
|
420 | : mg_find (sv, type)) |
|
|
421 | |
|
|
422 | #define CORO_MAGIC(sv, type) \ |
|
|
423 | (expect_true (SvMAGIC (sv)) \ |
|
|
424 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
425 | : 0) |
|
|
426 | |
|
|
427 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
428 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
429 | |
|
|
430 | INLINE struct coro * |
|
|
431 | SvSTATE_ (pTHX_ SV *coro) |
|
|
432 | { |
|
|
433 | HV *stash; |
|
|
434 | MAGIC *mg; |
|
|
435 | |
|
|
436 | if (SvROK (coro)) |
|
|
437 | coro = SvRV (coro); |
|
|
438 | |
|
|
439 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
440 | croak ("Coro::State object required"); |
|
|
441 | |
|
|
442 | stash = SvSTASH (coro); |
|
|
443 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
444 | { |
|
|
445 | /* very slow, but rare, check */ |
|
|
446 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
447 | croak ("Coro::State object required"); |
|
|
448 | } |
|
|
449 | |
|
|
450 | mg = CORO_MAGIC_state (coro); |
|
|
451 | return (struct coro *)mg->mg_ptr; |
|
|
452 | } |
|
|
453 | |
|
|
454 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
455 | |
|
|
456 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
457 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
458 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
459 | |
490 | |
460 | /* the next two functions merely cache the padlists */ |
491 | /* the next two functions merely cache the padlists */ |
461 | static void |
492 | static void |
462 | get_padlist (pTHX_ CV *cv) |
493 | get_padlist (pTHX_ CV *cv) |
463 | { |
494 | { |
… | |
… | |
474 | CV *cp = Perl_cv_clone (aTHX_ cv); |
505 | CV *cp = Perl_cv_clone (aTHX_ cv); |
475 | CvPADLIST (cv) = CvPADLIST (cp); |
506 | CvPADLIST (cv) = CvPADLIST (cp); |
476 | CvPADLIST (cp) = 0; |
507 | CvPADLIST (cp) = 0; |
477 | SvREFCNT_dec (cp); |
508 | SvREFCNT_dec (cp); |
478 | #else |
509 | #else |
479 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
510 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
480 | #endif |
511 | #endif |
481 | } |
512 | } |
482 | } |
513 | } |
483 | |
514 | |
484 | static void |
515 | static void |
… | |
… | |
491 | 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); |
492 | |
523 | |
493 | av = (AV *)mg->mg_obj; |
524 | av = (AV *)mg->mg_obj; |
494 | |
525 | |
495 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
526 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
496 | av_extend (av, AvMAX (av) + 1); |
527 | av_extend (av, AvFILLp (av) + 1); |
497 | |
528 | |
498 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
529 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
499 | } |
530 | } |
500 | |
531 | |
501 | /** load & save, init *******************************************************/ |
532 | /** load & save, init *******************************************************/ |
|
|
533 | |
|
|
534 | static void |
|
|
535 | on_enterleave_call (pTHX_ SV *cb); |
502 | |
536 | |
503 | static void |
537 | static void |
504 | load_perl (pTHX_ Coro__State c) |
538 | load_perl (pTHX_ Coro__State c) |
505 | { |
539 | { |
506 | perl_slots *slot = c->slot; |
540 | perl_slots *slot = c->slot; |
507 | c->slot = 0; |
541 | c->slot = 0; |
508 | |
542 | |
509 | PL_mainstack = c->mainstack; |
543 | PL_mainstack = c->mainstack; |
510 | |
544 | |
511 | GvSV (PL_defgv) = slot->defsv; |
545 | GvSV (PL_defgv) = slot->defsv; |
512 | GvAV (PL_defgv) = slot->defav; |
546 | GvAV (PL_defgv) = slot->defav; |
513 | GvSV (PL_errgv) = slot->errsv; |
547 | GvSV (PL_errgv) = slot->errsv; |
514 | GvSV (irsgv) = slot->irsgv; |
548 | GvSV (irsgv) = slot->irsgv; |
|
|
549 | GvHV (PL_hintgv) = slot->hinthv; |
515 | |
550 | |
516 | #define VAR(name,type) PL_ ## name = slot->name; |
551 | #define VAR(name,type) PL_ ## name = slot->name; |
517 | # include "state.h" |
552 | # include "state.h" |
518 | #undef VAR |
553 | #undef VAR |
519 | |
554 | |
… | |
… | |
533 | PUTBACK; |
568 | PUTBACK; |
534 | } |
569 | } |
535 | |
570 | |
536 | slf_frame = c->slf_frame; |
571 | slf_frame = c->slf_frame; |
537 | 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 | } |
538 | } |
581 | } |
539 | |
582 | |
540 | static void |
583 | static void |
541 | save_perl (pTHX_ Coro__State c) |
584 | save_perl (pTHX_ Coro__State c) |
542 | { |
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 | |
543 | c->except = CORO_THROW; |
594 | c->except = CORO_THROW; |
544 | c->slf_frame = slf_frame; |
595 | c->slf_frame = slf_frame; |
545 | |
596 | |
546 | { |
597 | { |
547 | dSP; |
598 | dSP; |
… | |
… | |
605 | c->mainstack = PL_mainstack; |
656 | c->mainstack = PL_mainstack; |
606 | |
657 | |
607 | { |
658 | { |
608 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
659 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
609 | |
660 | |
610 | slot->defav = GvAV (PL_defgv); |
661 | slot->defav = GvAV (PL_defgv); |
611 | slot->defsv = DEFSV; |
662 | slot->defsv = DEFSV; |
612 | slot->errsv = ERRSV; |
663 | slot->errsv = ERRSV; |
613 | slot->irsgv = GvSV (irsgv); |
664 | slot->irsgv = GvSV (irsgv); |
|
|
665 | slot->hinthv = GvHV (PL_hintgv); |
614 | |
666 | |
615 | #define VAR(name,type) slot->name = PL_ ## name; |
667 | #define VAR(name,type) slot->name = PL_ ## name; |
616 | # include "state.h" |
668 | # include "state.h" |
617 | #undef VAR |
669 | #undef VAR |
618 | } |
670 | } |
… | |
… | |
745 | #endif |
797 | #endif |
746 | |
798 | |
747 | /* |
799 | /* |
748 | * This overrides the default magic get method of %SIG elements. |
800 | * This overrides the default magic get method of %SIG elements. |
749 | * 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 |
750 | * 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 |
751 | * readback here. |
803 | * readback here. |
752 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
753 | * not expecting this to actually change the hook. This is a potential problem |
|
|
754 | * when a schedule happens then, but we ignore this. |
|
|
755 | */ |
804 | */ |
756 | static int |
805 | static int |
757 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
806 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
758 | { |
807 | { |
759 | const char *s = MgPV_nolen_const (mg); |
808 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
812 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
861 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
813 | |
862 | |
814 | if (svp) |
863 | if (svp) |
815 | { |
864 | { |
816 | SV *old = *svp; |
865 | SV *old = *svp; |
817 | *svp = newSVsv (sv); |
866 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
818 | SvREFCNT_dec (old); |
867 | SvREFCNT_dec (old); |
819 | return 0; |
868 | return 0; |
820 | } |
869 | } |
821 | } |
870 | } |
822 | |
871 | |
… | |
… | |
832 | |
881 | |
833 | static int |
882 | static int |
834 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
883 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
835 | { |
884 | { |
836 | return 0; |
885 | return 0; |
|
|
886 | } |
|
|
887 | |
|
|
888 | static int |
|
|
889 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
890 | { |
|
|
891 | return 1; |
837 | } |
892 | } |
838 | |
893 | |
839 | static UNOP coro_setup_op; |
894 | static UNOP coro_setup_op; |
840 | |
895 | |
841 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
896 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
… | |
… | |
848 | |
903 | |
849 | PL_runops = RUNOPS_DEFAULT; |
904 | PL_runops = RUNOPS_DEFAULT; |
850 | PL_curcop = &PL_compiling; |
905 | PL_curcop = &PL_compiling; |
851 | PL_in_eval = EVAL_NULL; |
906 | PL_in_eval = EVAL_NULL; |
852 | PL_comppad = 0; |
907 | PL_comppad = 0; |
|
|
908 | PL_comppad_name = 0; |
|
|
909 | PL_comppad_name_fill = 0; |
|
|
910 | PL_comppad_name_floor = 0; |
853 | PL_curpm = 0; |
911 | PL_curpm = 0; |
854 | PL_curpad = 0; |
912 | PL_curpad = 0; |
855 | PL_localizing = 0; |
913 | PL_localizing = 0; |
856 | PL_dirty = 0; |
914 | PL_dirty = 0; |
857 | PL_restartop = 0; |
915 | PL_restartop = 0; |
858 | #if PERL_VERSION_ATLEAST (5,10,0) |
916 | #if PERL_VERSION_ATLEAST (5,10,0) |
859 | PL_parser = 0; |
917 | PL_parser = 0; |
860 | #endif |
918 | #endif |
|
|
919 | PL_hints = 0; |
861 | |
920 | |
862 | /* recreate the die/warn hooks */ |
921 | /* recreate the die/warn hooks */ |
863 | 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 ); |
864 | 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); |
865 | |
924 | |
866 | GvSV (PL_defgv) = newSV (0); |
925 | GvSV (PL_defgv) = newSV (0); |
867 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
926 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
868 | GvSV (PL_errgv) = newSV (0); |
927 | GvSV (PL_errgv) = newSV (0); |
869 | 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; |
870 | PL_rs = newSVsv (GvSV (irsgv)); |
930 | PL_rs = newSVsv (GvSV (irsgv)); |
871 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
931 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
872 | |
932 | |
873 | { |
933 | { |
874 | dSP; |
934 | dSP; |
… | |
… | |
892 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
952 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
893 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
953 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
894 | |
954 | |
895 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
955 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
896 | coro_setup_op.op_next = PL_op; |
956 | coro_setup_op.op_next = PL_op; |
897 | coro_setup_op.op_type = OP_CUSTOM; |
957 | coro_setup_op.op_type = OP_ENTERSUB; |
898 | coro_setup_op.op_ppaddr = pp_slf; |
958 | coro_setup_op.op_ppaddr = pp_slf; |
899 | /* no flags etc. required, as an init function won't be called */ |
959 | /* no flags etc. required, as an init function won't be called */ |
900 | |
960 | |
901 | PL_op = (OP *)&coro_setup_op; |
961 | PL_op = (OP *)&coro_setup_op; |
902 | |
962 | |
903 | /* copy throw, in case it was set before coro_setup */ |
963 | /* copy throw, in case it was set before coro_setup */ |
904 | CORO_THROW = coro->except; |
964 | CORO_THROW = coro->except; |
905 | } |
965 | } |
906 | |
966 | |
907 | static void |
967 | static void |
908 | coro_destruct (pTHX_ struct coro *coro) |
968 | coro_unwind_stacks (pTHX) |
909 | { |
969 | { |
910 | if (!IN_DESTRUCT) |
970 | if (!IN_DESTRUCT) |
911 | { |
971 | { |
912 | /* restore all saved variables and stuff */ |
972 | /* restore all saved variables and stuff */ |
913 | LEAVE_SCOPE (0); |
973 | LEAVE_SCOPE (0); |
… | |
… | |
921 | POPSTACK_TO (PL_mainstack); |
981 | POPSTACK_TO (PL_mainstack); |
922 | |
982 | |
923 | /* unwind main stack */ |
983 | /* unwind main stack */ |
924 | dounwind (-1); |
984 | dounwind (-1); |
925 | } |
985 | } |
|
|
986 | } |
|
|
987 | |
|
|
988 | static void |
|
|
989 | coro_destruct_perl (pTHX_ struct coro *coro) |
|
|
990 | { |
|
|
991 | coro_unwind_stacks (aTHX); |
926 | |
992 | |
927 | SvREFCNT_dec (GvSV (PL_defgv)); |
993 | SvREFCNT_dec (GvSV (PL_defgv)); |
928 | SvREFCNT_dec (GvAV (PL_defgv)); |
994 | SvREFCNT_dec (GvAV (PL_defgv)); |
929 | SvREFCNT_dec (GvSV (PL_errgv)); |
995 | SvREFCNT_dec (GvSV (PL_errgv)); |
930 | SvREFCNT_dec (PL_defoutgv); |
996 | SvREFCNT_dec (PL_defoutgv); |
931 | SvREFCNT_dec (PL_rs); |
997 | SvREFCNT_dec (PL_rs); |
932 | SvREFCNT_dec (GvSV (irsgv)); |
998 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
999 | SvREFCNT_dec (GvHV (PL_hintgv)); |
933 | |
1000 | |
934 | SvREFCNT_dec (PL_diehook); |
1001 | SvREFCNT_dec (PL_diehook); |
935 | SvREFCNT_dec (PL_warnhook); |
1002 | SvREFCNT_dec (PL_warnhook); |
936 | |
1003 | |
937 | SvREFCNT_dec (coro->saved_deffh); |
1004 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
955 | static int |
1022 | static int |
956 | runops_trace (pTHX) |
1023 | runops_trace (pTHX) |
957 | { |
1024 | { |
958 | COP *oldcop = 0; |
1025 | COP *oldcop = 0; |
959 | int oldcxix = -2; |
1026 | int oldcxix = -2; |
960 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
961 | coro_cctx *cctx = coro->cctx; |
|
|
962 | |
1027 | |
963 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
1028 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
964 | { |
1029 | { |
965 | PERL_ASYNC_CHECK (); |
1030 | PERL_ASYNC_CHECK (); |
966 | |
1031 | |
967 | if (cctx->flags & CC_TRACE_ALL) |
1032 | if (cctx_current->flags & CC_TRACE_ALL) |
968 | { |
1033 | { |
969 | 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) |
970 | { |
1035 | { |
971 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1036 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
972 | SV **bot, **top; |
1037 | SV **bot, **top; |
973 | AV *av = newAV (); /* return values */ |
1038 | AV *av = newAV (); /* return values */ |
974 | SV **cb; |
1039 | SV **cb; |
… | |
… | |
1011 | |
1076 | |
1012 | if (PL_curcop != &PL_compiling) |
1077 | if (PL_curcop != &PL_compiling) |
1013 | { |
1078 | { |
1014 | SV **cb; |
1079 | SV **cb; |
1015 | |
1080 | |
1016 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1081 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1017 | { |
1082 | { |
1018 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1083 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1019 | |
1084 | |
1020 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1085 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1021 | { |
1086 | { |
1022 | runops_proc_t old_runops = PL_runops; |
|
|
1023 | dSP; |
1087 | dSP; |
1024 | GV *gv = CvGV (cx->blk_sub.cv); |
1088 | GV *gv = CvGV (cx->blk_sub.cv); |
1025 | SV *fullname = sv_2mortal (newSV (0)); |
1089 | SV *fullname = sv_2mortal (newSV (0)); |
1026 | |
1090 | |
1027 | if (isGV (gv)) |
1091 | if (isGV (gv)) |
… | |
… | |
1045 | } |
1109 | } |
1046 | |
1110 | |
1047 | oldcxix = cxstack_ix; |
1111 | oldcxix = cxstack_ix; |
1048 | } |
1112 | } |
1049 | |
1113 | |
1050 | if (cctx->flags & CC_TRACE_LINE) |
1114 | if (cctx_current->flags & CC_TRACE_LINE) |
1051 | { |
1115 | { |
1052 | dSP; |
1116 | dSP; |
1053 | |
1117 | |
1054 | PL_runops = RUNOPS_DEFAULT; |
1118 | PL_runops = RUNOPS_DEFAULT; |
1055 | ENTER; |
1119 | ENTER; |
… | |
… | |
1074 | |
1138 | |
1075 | TAINT_NOT; |
1139 | TAINT_NOT; |
1076 | return 0; |
1140 | return 0; |
1077 | } |
1141 | } |
1078 | |
1142 | |
1079 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1080 | static struct CoroSLF cctx_ssl_frame; |
1143 | static struct CoroSLF cctx_ssl_frame; |
1081 | |
1144 | |
1082 | static void |
1145 | static void |
1083 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1146 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1084 | { |
1147 | { |
1085 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1086 | ta->next = 0; |
1148 | ta->prev = 0; |
1087 | } |
1149 | } |
1088 | |
1150 | |
1089 | static int |
1151 | static int |
1090 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1152 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1091 | { |
1153 | { |
… | |
… | |
1094 | 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 */ |
1095 | } |
1157 | } |
1096 | |
1158 | |
1097 | /* 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 */ |
1098 | static void NOINLINE |
1160 | static void NOINLINE |
1099 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1161 | cctx_prepare (pTHX) |
1100 | { |
1162 | { |
1101 | PL_top_env = &PL_start_env; |
1163 | PL_top_env = &PL_start_env; |
1102 | |
1164 | |
1103 | if (cctx->flags & CC_TRACE) |
1165 | if (cctx_current->flags & CC_TRACE) |
1104 | PL_runops = runops_trace; |
1166 | PL_runops = runops_trace; |
1105 | |
1167 | |
1106 | /* 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 |
1107 | * that can lead to creation of a new cctx */ |
1169 | * that can lead to creation of a new cctx */ |
1108 | 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)", |
1109 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1171 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1110 | |
1172 | |
1111 | /* 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 */ |
1112 | cctx_ssl_cctx = cctx; |
|
|
1113 | cctx_ssl_frame = slf_frame; |
1174 | cctx_ssl_frame = slf_frame; |
1114 | |
1175 | |
1115 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1176 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1116 | slf_frame.check = slf_check_set_stacklevel; |
1177 | slf_frame.check = slf_check_set_stacklevel; |
1117 | } |
1178 | } |
… | |
… | |
1140 | /* normally we would need to skip the entersub here */ |
1201 | /* normally we would need to skip the entersub here */ |
1141 | /* 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 */ |
1142 | /* PL_nop = PL_nop->op_next */ |
1203 | /* PL_nop = PL_nop->op_next */ |
1143 | |
1204 | |
1144 | /* inject a fake subroutine call to cctx_init */ |
1205 | /* inject a fake subroutine call to cctx_init */ |
1145 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1206 | cctx_prepare (aTHX); |
1146 | |
1207 | |
1147 | /* cctx_run is the alternative tail of transfer() */ |
1208 | /* cctx_run is the alternative tail of transfer() */ |
1148 | transfer_tail (aTHX); |
1209 | transfer_tail (aTHX); |
1149 | |
1210 | |
1150 | /* 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 */ |
… | |
… | |
1158 | /* |
1219 | /* |
1159 | * 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 |
1160 | * 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) |
1161 | * 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 |
1162 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1223 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1163 | * 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. |
1164 | */ |
1227 | */ |
1165 | PL_top_env = main_top_env; |
1228 | PL_top_env = main_top_env; |
1166 | 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 */ |
1167 | } |
1230 | } |
1168 | } |
1231 | } |
… | |
… | |
1245 | cctx_destroy (coro_cctx *cctx) |
1308 | cctx_destroy (coro_cctx *cctx) |
1246 | { |
1309 | { |
1247 | if (!cctx) |
1310 | if (!cctx) |
1248 | return; |
1311 | return; |
1249 | |
1312 | |
|
|
1313 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
|
|
1314 | |
1250 | --cctx_count; |
1315 | --cctx_count; |
1251 | coro_destroy (&cctx->cctx); |
1316 | coro_destroy (&cctx->cctx); |
1252 | |
1317 | |
1253 | /* coro_transfer creates new, empty cctx's */ |
1318 | /* coro_transfer creates new, empty cctx's */ |
1254 | if (cctx->sptr) |
1319 | if (cctx->sptr) |
… | |
… | |
1317 | /* TODO: throwing up here is considered harmful */ |
1382 | /* TODO: throwing up here is considered harmful */ |
1318 | |
1383 | |
1319 | if (expect_true (prev != next)) |
1384 | if (expect_true (prev != next)) |
1320 | { |
1385 | { |
1321 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1386 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1322 | 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,"); |
1323 | |
1388 | |
1324 | if (expect_false (next->flags & CF_RUNNING)) |
1389 | if (expect_false (next->flags & CF_RUNNING)) |
1325 | 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,"); |
1326 | |
1391 | |
1327 | if (expect_false (next->flags & CF_DESTROYED)) |
1392 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1339 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1404 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1340 | { |
1405 | { |
1341 | dSTACKLEVEL; |
1406 | dSTACKLEVEL; |
1342 | |
1407 | |
1343 | /* sometimes transfer is only called to set idle_sp */ |
1408 | /* sometimes transfer is only called to set idle_sp */ |
1344 | if (expect_false (!next)) |
1409 | if (expect_false (!prev)) |
1345 | { |
1410 | { |
1346 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1411 | cctx_current->idle_sp = STACKLEVEL; |
1347 | 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 */ |
1348 | } |
1413 | } |
1349 | else if (expect_true (prev != next)) |
1414 | else if (expect_true (prev != next)) |
1350 | { |
1415 | { |
1351 | coro_cctx *prev__cctx; |
1416 | coro_cctx *cctx_prev; |
1352 | |
1417 | |
1353 | if (expect_false (prev->flags & CF_NEW)) |
1418 | if (expect_false (prev->flags & CF_NEW)) |
1354 | { |
1419 | { |
1355 | /* create a new empty/source context */ |
1420 | /* create a new empty/source context */ |
1356 | prev->cctx = cctx_new_empty (); |
|
|
1357 | prev->flags &= ~CF_NEW; |
1421 | prev->flags &= ~CF_NEW; |
1358 | prev->flags |= CF_RUNNING; |
1422 | prev->flags |= CF_RUNNING; |
1359 | } |
1423 | } |
1360 | |
1424 | |
1361 | prev->flags &= ~CF_RUNNING; |
1425 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1372 | coro_setup (aTHX_ next); |
1436 | coro_setup (aTHX_ next); |
1373 | } |
1437 | } |
1374 | else |
1438 | else |
1375 | load_perl (aTHX_ next); |
1439 | load_perl (aTHX_ next); |
1376 | |
1440 | |
1377 | prev__cctx = prev->cctx; |
|
|
1378 | |
|
|
1379 | /* possibly untie and reuse the cctx */ |
1441 | /* possibly untie and reuse the cctx */ |
1380 | if (expect_true ( |
1442 | if (expect_true ( |
1381 | prev__cctx->idle_sp == STACKLEVEL |
1443 | cctx_current->idle_sp == STACKLEVEL |
1382 | && !(prev__cctx->flags & CC_TRACE) |
1444 | && !(cctx_current->flags & CC_TRACE) |
1383 | && !force_cctx |
1445 | && !force_cctx |
1384 | )) |
1446 | )) |
1385 | { |
1447 | { |
1386 | /* 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 */ |
1387 | 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)); |
1388 | |
1450 | |
1389 | prev->cctx = 0; |
|
|
1390 | |
|
|
1391 | /* 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. */ |
1392 | /* 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 */ |
1393 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1453 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1394 | if (!next->cctx) |
1454 | if (expect_true (!next->cctx)) |
1395 | next->cctx = cctx_get (aTHX); |
1455 | next->cctx = cctx_get (aTHX); |
1396 | |
1456 | |
1397 | cctx_put (prev__cctx); |
1457 | cctx_put (cctx_current); |
1398 | } |
1458 | } |
|
|
1459 | else |
|
|
1460 | prev->cctx = cctx_current; |
1399 | |
1461 | |
1400 | ++next->usecount; |
1462 | ++next->usecount; |
1401 | |
1463 | |
1402 | if (expect_true (!next->cctx)) |
1464 | cctx_prev = cctx_current; |
1403 | next->cctx = cctx_get (aTHX); |
1465 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1404 | |
1466 | |
1405 | if (expect_false (prev__cctx != next->cctx)) |
1467 | next->cctx = 0; |
|
|
1468 | |
|
|
1469 | if (expect_false (cctx_prev != cctx_current)) |
1406 | { |
1470 | { |
1407 | prev__cctx->top_env = PL_top_env; |
1471 | cctx_prev->top_env = PL_top_env; |
1408 | PL_top_env = next->cctx->top_env; |
1472 | PL_top_env = cctx_current->top_env; |
1409 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1473 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1410 | } |
1474 | } |
1411 | |
1475 | |
1412 | transfer_tail (aTHX); |
1476 | transfer_tail (aTHX); |
1413 | } |
1477 | } |
1414 | } |
1478 | } |
… | |
… | |
1436 | --coro_nready; |
1500 | --coro_nready; |
1437 | } |
1501 | } |
1438 | else |
1502 | else |
1439 | 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 */ |
1440 | |
1504 | |
1441 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1505 | if (coro->mainstack |
|
|
1506 | && coro->mainstack != main_mainstack |
|
|
1507 | && coro->slot |
|
|
1508 | && !PL_dirty) |
1442 | { |
1509 | { |
1443 | struct coro temp; |
1510 | struct coro *current = SvSTATE_current; |
1444 | |
1511 | |
1445 | 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)); |
1446 | |
1513 | |
1447 | save_perl (aTHX_ &temp); |
1514 | save_perl (aTHX_ current); |
1448 | load_perl (aTHX_ coro); |
1515 | load_perl (aTHX_ coro); |
1449 | |
1516 | |
1450 | coro_destruct (aTHX_ coro); |
1517 | coro_destruct_perl (aTHX_ coro); |
1451 | |
1518 | |
1452 | load_perl (aTHX_ &temp); |
1519 | load_perl (aTHX_ current); |
1453 | |
1520 | |
1454 | coro->slot = 0; |
1521 | coro->slot = 0; |
1455 | } |
1522 | } |
1456 | |
1523 | |
1457 | cctx_destroy (coro->cctx); |
1524 | cctx_destroy (coro->cctx); |
… | |
… | |
1569 | { |
1636 | { |
1570 | struct coro *coro; |
1637 | struct coro *coro; |
1571 | SV *sv_hook; |
1638 | SV *sv_hook; |
1572 | void (*xs_hook)(void); |
1639 | void (*xs_hook)(void); |
1573 | |
1640 | |
1574 | if (SvROK (coro_sv)) |
|
|
1575 | coro_sv = SvRV (coro_sv); |
|
|
1576 | |
|
|
1577 | coro = SvSTATE (coro_sv); |
1641 | coro = SvSTATE (coro_sv); |
1578 | |
1642 | |
1579 | if (coro->flags & CF_READY) |
1643 | if (coro->flags & CF_READY) |
1580 | return 0; |
1644 | return 0; |
1581 | |
1645 | |
… | |
… | |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1678 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1615 | } |
1679 | } |
1616 | |
1680 | |
1617 | /* expects to own a reference to next->hv */ |
1681 | /* expects to own a reference to next->hv */ |
1618 | INLINE void |
1682 | INLINE void |
1619 | prepare_cede_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1683 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1620 | { |
1684 | { |
1621 | SV *prev_sv = SvRV (coro_current); |
1685 | SV *prev_sv = SvRV (coro_current); |
1622 | |
1686 | |
1623 | ta->prev = SvSTATE_hv (prev_sv); |
1687 | ta->prev = SvSTATE_hv (prev_sv); |
1624 | ta->next = next; |
1688 | ta->next = next; |
1625 | |
1689 | |
1626 | TRANSFER_CHECK (*ta); |
1690 | TRANSFER_CHECK (*ta); |
1627 | |
1691 | |
1628 | SvRV_set (coro_current, next->hv); |
1692 | SvRV_set (coro_current, (SV *)next->hv); |
1629 | |
1693 | |
1630 | free_coro_mortal (aTHX); |
1694 | free_coro_mortal (aTHX); |
1631 | coro_mortal = prev_sv; |
1695 | coro_mortal = prev_sv; |
1632 | } |
1696 | } |
1633 | |
1697 | |
… | |
… | |
1636 | { |
1700 | { |
1637 | for (;;) |
1701 | for (;;) |
1638 | { |
1702 | { |
1639 | SV *next_sv = coro_deq (aTHX); |
1703 | SV *next_sv = coro_deq (aTHX); |
1640 | |
1704 | |
1641 | if (expect_false (!next_sv)) |
1705 | if (expect_true (next_sv)) |
1642 | { |
|
|
1643 | /* nothing to schedule: call the idle handler */ |
|
|
1644 | dSP; |
|
|
1645 | |
|
|
1646 | ENTER; |
|
|
1647 | SAVETMPS; |
|
|
1648 | |
|
|
1649 | PUSHMARK (SP); |
|
|
1650 | PUTBACK; |
|
|
1651 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1652 | |
|
|
1653 | FREETMPS; |
|
|
1654 | LEAVE; |
|
|
1655 | } |
|
|
1656 | else |
|
|
1657 | { |
1706 | { |
1658 | struct coro *next = SvSTATE_hv (next_sv); |
1707 | struct coro *next = SvSTATE_hv (next_sv); |
1659 | |
1708 | |
1660 | /* cannot transfer to destroyed coros, skip and look for next */ |
1709 | /* cannot transfer to destroyed coros, skip and look for next */ |
1661 | if (expect_false (next->flags & CF_DESTROYED)) |
1710 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1663 | else |
1712 | else |
1664 | { |
1713 | { |
1665 | next->flags &= ~CF_READY; |
1714 | next->flags &= ~CF_READY; |
1666 | --coro_nready; |
1715 | --coro_nready; |
1667 | |
1716 | |
1668 | return prepare_cede_to (aTHX_ ta, next); |
1717 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1718 | break; |
1669 | } |
1719 | } |
1670 | } |
1720 | } |
|
|
1721 | else |
|
|
1722 | { |
|
|
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 | { |
|
|
1733 | dSP; |
|
|
1734 | |
|
|
1735 | ENTER; |
|
|
1736 | SAVETMPS; |
|
|
1737 | |
|
|
1738 | PUSHMARK (SP); |
|
|
1739 | PUTBACK; |
|
|
1740 | call_sv (sv_idle, G_VOID | G_DISCARD); |
|
|
1741 | |
|
|
1742 | FREETMPS; |
|
|
1743 | LEAVE; |
|
|
1744 | } |
|
|
1745 | } |
1671 | } |
1746 | } |
1672 | } |
1747 | } |
1673 | |
1748 | |
1674 | INLINE void |
1749 | INLINE void |
1675 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1750 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
… | |
… | |
1697 | { |
1772 | { |
1698 | struct coro_transfer_args ta; |
1773 | struct coro_transfer_args ta; |
1699 | |
1774 | |
1700 | prepare_schedule (aTHX_ &ta); |
1775 | prepare_schedule (aTHX_ &ta); |
1701 | TRANSFER (ta, 1); |
1776 | TRANSFER (ta, 1); |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | static void |
|
|
1780 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1781 | { |
|
|
1782 | struct coro_transfer_args ta; |
|
|
1783 | struct coro *next = SvSTATE (coro_sv); |
|
|
1784 | |
|
|
1785 | SvREFCNT_inc_NN (coro_sv); |
|
|
1786 | prepare_schedule_to (aTHX_ &ta, next); |
1702 | } |
1787 | } |
1703 | |
1788 | |
1704 | static int |
1789 | static int |
1705 | api_cede (pTHX) |
1790 | api_cede (pTHX) |
1706 | { |
1791 | { |
… | |
… | |
1735 | static void |
1820 | static void |
1736 | api_trace (pTHX_ SV *coro_sv, int flags) |
1821 | api_trace (pTHX_ SV *coro_sv, int flags) |
1737 | { |
1822 | { |
1738 | struct coro *coro = SvSTATE (coro_sv); |
1823 | struct coro *coro = SvSTATE (coro_sv); |
1739 | |
1824 | |
|
|
1825 | if (coro->flags & CF_RUNNING) |
|
|
1826 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1827 | |
1740 | if (flags & CC_TRACE) |
1828 | if (flags & CC_TRACE) |
1741 | { |
1829 | { |
1742 | if (!coro->cctx) |
1830 | if (!coro->cctx) |
1743 | coro->cctx = cctx_new_run (); |
1831 | coro->cctx = cctx_new_run (); |
1744 | else if (!(coro->cctx->flags & CC_TRACE)) |
1832 | else if (!(coro->cctx->flags & CC_TRACE)) |
1745 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1833 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1746 | |
1834 | |
1747 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1835 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1748 | } |
1836 | } |
1749 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1837 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1750 | { |
1838 | { |
… | |
… | |
1753 | if (coro->flags & CF_RUNNING) |
1841 | if (coro->flags & CF_RUNNING) |
1754 | PL_runops = RUNOPS_DEFAULT; |
1842 | PL_runops = RUNOPS_DEFAULT; |
1755 | else |
1843 | else |
1756 | coro->slot->runops = RUNOPS_DEFAULT; |
1844 | coro->slot->runops = RUNOPS_DEFAULT; |
1757 | } |
1845 | } |
|
|
1846 | } |
|
|
1847 | |
|
|
1848 | static void |
|
|
1849 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1850 | { |
|
|
1851 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1852 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1853 | |
|
|
1854 | if (on_destroyp) |
|
|
1855 | { |
|
|
1856 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1857 | |
|
|
1858 | while (AvFILLp (on_destroy) >= 0) |
|
|
1859 | { |
|
|
1860 | dSP; /* don't disturb outer sp */ |
|
|
1861 | SV *cb = av_pop (on_destroy); |
|
|
1862 | |
|
|
1863 | PUSHMARK (SP); |
|
|
1864 | |
|
|
1865 | if (statusp) |
|
|
1866 | { |
|
|
1867 | int i; |
|
|
1868 | AV *status = (AV *)SvRV (*statusp); |
|
|
1869 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1870 | |
|
|
1871 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1872 | PUSHs (AvARRAY (status)[i]); |
|
|
1873 | } |
|
|
1874 | |
|
|
1875 | PUTBACK; |
|
|
1876 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1877 | } |
|
|
1878 | } |
|
|
1879 | } |
|
|
1880 | |
|
|
1881 | static void |
|
|
1882 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1883 | { |
|
|
1884 | int i; |
|
|
1885 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1886 | AV *av = newAV (); |
|
|
1887 | |
|
|
1888 | av_extend (av, items - 1); |
|
|
1889 | for (i = 0; i < items; ++i) |
|
|
1890 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1891 | |
|
|
1892 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1893 | |
|
|
1894 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1895 | api_ready (aTHX_ sv_manager); |
|
|
1896 | |
|
|
1897 | frame->prepare = prepare_schedule; |
|
|
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);*/ |
1758 | } |
1903 | } |
1759 | |
1904 | |
1760 | /*****************************************************************************/ |
1905 | /*****************************************************************************/ |
1761 | /* async pool handler */ |
1906 | /* async pool handler */ |
1762 | |
1907 | |
… | |
… | |
1995 | static void |
2140 | static void |
1996 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2141 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1997 | { |
2142 | { |
1998 | frame->prepare = prepare_schedule; |
2143 | frame->prepare = prepare_schedule; |
1999 | frame->check = slf_check_nop; |
2144 | frame->check = slf_check_nop; |
|
|
2145 | } |
|
|
2146 | |
|
|
2147 | static void |
|
|
2148 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2149 | { |
|
|
2150 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2151 | |
|
|
2152 | SvREFCNT_inc_NN (next->hv); |
|
|
2153 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2154 | } |
|
|
2155 | |
|
|
2156 | static void |
|
|
2157 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2158 | { |
|
|
2159 | if (!items) |
|
|
2160 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2161 | |
|
|
2162 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2163 | frame->prepare = slf_prepare_schedule_to; |
|
|
2164 | frame->check = slf_check_nop; |
|
|
2165 | } |
|
|
2166 | |
|
|
2167 | static void |
|
|
2168 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2169 | { |
|
|
2170 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2171 | |
|
|
2172 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
2000 | } |
2173 | } |
2001 | |
2174 | |
2002 | static void |
2175 | static void |
2003 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2176 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2004 | { |
2177 | { |
… | |
… | |
2150 | } |
2323 | } |
2151 | else |
2324 | else |
2152 | slf_argc = 0; |
2325 | slf_argc = 0; |
2153 | |
2326 | |
2154 | PL_op->op_ppaddr = pp_slf; |
2327 | PL_op->op_ppaddr = pp_slf; |
2155 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2328 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2156 | |
2329 | |
2157 | 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)); |
2158 | } |
2378 | } |
2159 | |
2379 | |
2160 | /*****************************************************************************/ |
2380 | /*****************************************************************************/ |
2161 | /* PerlIO::cede */ |
2381 | /* PerlIO::cede */ |
2162 | |
2382 | |
… | |
… | |
2243 | SV **ary; |
2463 | SV **ary; |
2244 | |
2464 | |
2245 | /* unfortunately, building manually saves memory */ |
2465 | /* unfortunately, building manually saves memory */ |
2246 | Newx (ary, 2, SV *); |
2466 | Newx (ary, 2, SV *); |
2247 | AvALLOC (av) = ary; |
2467 | AvALLOC (av) = ary; |
|
|
2468 | #if PERL_VERSION_ATLEAST (5,10,0) |
2248 | /*AvARRAY (av) = ary;*/ |
2469 | AvARRAY (av) = ary; |
|
|
2470 | #else |
2249 | 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 |
2250 | AvMAX (av) = 1; |
2475 | AvMAX (av) = 1; |
2251 | AvFILLp (av) = 0; |
2476 | AvFILLp (av) = 0; |
2252 | ary [0] = newSViv (count); |
2477 | ary [0] = newSViv (count); |
2253 | |
2478 | |
2254 | return newRV_noinc ((SV *)av); |
2479 | return newRV_noinc ((SV *)av); |
… | |
… | |
2624 | XSRETURN_EMPTY; |
2849 | XSRETURN_EMPTY; |
2625 | } |
2850 | } |
2626 | |
2851 | |
2627 | /*****************************************************************************/ |
2852 | /*****************************************************************************/ |
2628 | |
2853 | |
|
|
2854 | #if CORO_CLONE |
|
|
2855 | # include "clone.c" |
|
|
2856 | #endif |
|
|
2857 | |
2629 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2858 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2630 | |
2859 | |
2631 | PROTOTYPES: DISABLE |
2860 | PROTOTYPES: DISABLE |
2632 | |
2861 | |
2633 | BOOT: |
2862 | BOOT: |
… | |
… | |
2637 | coro_thx = PERL_GET_CONTEXT; |
2866 | coro_thx = PERL_GET_CONTEXT; |
2638 | # endif |
2867 | # endif |
2639 | #endif |
2868 | #endif |
2640 | BOOT_PAGESIZE; |
2869 | BOOT_PAGESIZE; |
2641 | |
2870 | |
|
|
2871 | cctx_current = cctx_new_empty (); |
|
|
2872 | |
2642 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2873 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2643 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2874 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2644 | |
2875 | |
2645 | 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; |
2646 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2877 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2665 | |
2896 | |
2666 | { |
2897 | { |
2667 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2898 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2668 | |
2899 | |
2669 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2900 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2670 | hv_store_ent (PL_custom_op_names, slf, |
2901 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2671 | newSVpv ("coro_slf", 0), 0); |
|
|
2672 | |
2902 | |
2673 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2903 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2674 | hv_store_ent (PL_custom_op_descs, slf, |
2904 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2675 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2676 | } |
2905 | } |
2677 | |
2906 | |
2678 | coroapi.ver = CORO_API_VERSION; |
2907 | coroapi.ver = CORO_API_VERSION; |
2679 | coroapi.rev = CORO_API_REVISION; |
2908 | coroapi.rev = CORO_API_REVISION; |
2680 | |
2909 | |
… | |
… | |
2773 | void |
3002 | void |
2774 | _exit (int code) |
3003 | _exit (int code) |
2775 | PROTOTYPE: $ |
3004 | PROTOTYPE: $ |
2776 | CODE: |
3005 | CODE: |
2777 | _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 |
2778 | |
3026 | |
2779 | int |
3027 | int |
2780 | cctx_stacksize (int new_stacksize = 0) |
3028 | cctx_stacksize (int new_stacksize = 0) |
2781 | PROTOTYPE: ;$ |
3029 | PROTOTYPE: ;$ |
2782 | CODE: |
3030 | CODE: |
… | |
… | |
2832 | eval = 1 |
3080 | eval = 1 |
2833 | CODE: |
3081 | CODE: |
2834 | { |
3082 | { |
2835 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3083 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2836 | { |
3084 | { |
2837 | struct coro temp; |
3085 | struct coro *current = SvSTATE_current; |
2838 | |
3086 | |
2839 | if (!(coro->flags & CF_RUNNING)) |
3087 | if (current != coro) |
2840 | { |
3088 | { |
2841 | PUTBACK; |
3089 | PUTBACK; |
2842 | save_perl (aTHX_ &temp); |
3090 | save_perl (aTHX_ current); |
2843 | load_perl (aTHX_ coro); |
3091 | load_perl (aTHX_ coro); |
|
|
3092 | SPAGAIN; |
2844 | } |
3093 | } |
2845 | |
3094 | |
2846 | { |
|
|
2847 | dSP; |
|
|
2848 | ENTER; |
|
|
2849 | SAVETMPS; |
|
|
2850 | PUTBACK; |
|
|
2851 | PUSHSTACK; |
3095 | PUSHSTACK; |
|
|
3096 | |
2852 | PUSHMARK (SP); |
3097 | PUSHMARK (SP); |
|
|
3098 | PUTBACK; |
2853 | |
3099 | |
2854 | if (ix) |
3100 | if (ix) |
2855 | eval_sv (coderef, 0); |
3101 | eval_sv (coderef, 0); |
2856 | else |
3102 | else |
2857 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3103 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
2858 | |
3104 | |
|
|
3105 | SPAGAIN; |
2859 | POPSTACK; |
3106 | POPSTACK; |
2860 | SPAGAIN; |
|
|
2861 | FREETMPS; |
|
|
2862 | LEAVE; |
|
|
2863 | PUTBACK; |
|
|
2864 | } |
|
|
2865 | |
3107 | |
2866 | if (!(coro->flags & CF_RUNNING)) |
3108 | if (current != coro) |
2867 | { |
3109 | { |
|
|
3110 | PUTBACK; |
2868 | save_perl (aTHX_ coro); |
3111 | save_perl (aTHX_ coro); |
2869 | load_perl (aTHX_ &temp); |
3112 | load_perl (aTHX_ current); |
2870 | SPAGAIN; |
3113 | SPAGAIN; |
2871 | } |
3114 | } |
2872 | } |
3115 | } |
2873 | } |
3116 | } |
2874 | |
3117 | |
… | |
… | |
2903 | |
3146 | |
2904 | SV * |
3147 | SV * |
2905 | has_cctx (Coro::State coro) |
3148 | has_cctx (Coro::State coro) |
2906 | PROTOTYPE: $ |
3149 | PROTOTYPE: $ |
2907 | CODE: |
3150 | CODE: |
2908 | RETVAL = boolSV (!!coro->cctx); |
3151 | /* maybe manage the running flag differently */ |
|
|
3152 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
2909 | OUTPUT: |
3153 | OUTPUT: |
2910 | RETVAL |
3154 | RETVAL |
2911 | |
3155 | |
2912 | int |
3156 | int |
2913 | is_traced (Coro::State coro) |
3157 | is_traced (Coro::State coro) |
… | |
… | |
2933 | |
3177 | |
2934 | void |
3178 | void |
2935 | force_cctx () |
3179 | force_cctx () |
2936 | PROTOTYPE: |
3180 | PROTOTYPE: |
2937 | CODE: |
3181 | CODE: |
2938 | SvSTATE_current->cctx->idle_sp = 0; |
3182 | cctx_current->idle_sp = 0; |
2939 | |
3183 | |
2940 | void |
3184 | void |
2941 | swap_defsv (Coro::State self) |
3185 | swap_defsv (Coro::State self) |
2942 | PROTOTYPE: $ |
3186 | PROTOTYPE: $ |
2943 | ALIAS: |
3187 | ALIAS: |
… | |
… | |
2958 | |
3202 | |
2959 | BOOT: |
3203 | BOOT: |
2960 | { |
3204 | { |
2961 | int i; |
3205 | int i; |
2962 | |
3206 | |
2963 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2964 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3207 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2965 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3208 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2966 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3209 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
2967 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3210 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2968 | 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); |
|
|
3212 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3213 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3214 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3215 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
2969 | |
3216 | |
2970 | 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); |
2971 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3218 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
2972 | 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); |
2973 | 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); |
… | |
… | |
2986 | |
3233 | |
2987 | { |
3234 | { |
2988 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3235 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2989 | |
3236 | |
2990 | coroapi.schedule = api_schedule; |
3237 | coroapi.schedule = api_schedule; |
|
|
3238 | coroapi.schedule_to = api_schedule_to; |
2991 | coroapi.cede = api_cede; |
3239 | coroapi.cede = api_cede; |
2992 | coroapi.cede_notself = api_cede_notself; |
3240 | coroapi.cede_notself = api_cede_notself; |
2993 | coroapi.ready = api_ready; |
3241 | coroapi.ready = api_ready; |
2994 | coroapi.is_ready = api_is_ready; |
3242 | coroapi.is_ready = api_is_ready; |
2995 | coroapi.nready = coro_nready; |
3243 | coroapi.nready = coro_nready; |
… | |
… | |
3000 | SvREADONLY_on (sv); |
3248 | SvREADONLY_on (sv); |
3001 | } |
3249 | } |
3002 | } |
3250 | } |
3003 | |
3251 | |
3004 | void |
3252 | void |
|
|
3253 | terminate (...) |
|
|
3254 | CODE: |
|
|
3255 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3256 | |
|
|
3257 | void |
3005 | schedule (...) |
3258 | schedule (...) |
3006 | CODE: |
3259 | CODE: |
3007 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3260 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3261 | |
|
|
3262 | void |
|
|
3263 | schedule_to (...) |
|
|
3264 | CODE: |
|
|
3265 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3266 | |
|
|
3267 | void |
|
|
3268 | cede_to (...) |
|
|
3269 | CODE: |
|
|
3270 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
3008 | |
3271 | |
3009 | void |
3272 | void |
3010 | cede (...) |
3273 | cede (...) |
3011 | CODE: |
3274 | CODE: |
3012 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3275 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3013 | |
3276 | |
3014 | void |
3277 | void |
3015 | cede_notself (...) |
3278 | cede_notself (...) |
3016 | CODE: |
3279 | CODE: |
3017 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3280 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3281 | |
|
|
3282 | void |
|
|
3283 | _cancel (Coro::State self) |
|
|
3284 | CODE: |
|
|
3285 | coro_state_destroy (aTHX_ self); |
|
|
3286 | coro_call_on_destroy (aTHX_ self); |
3018 | |
3287 | |
3019 | void |
3288 | void |
3020 | _set_current (SV *current) |
3289 | _set_current (SV *current) |
3021 | PROTOTYPE: $ |
3290 | PROTOTYPE: $ |
3022 | CODE: |
3291 | CODE: |
… | |
… | |
3130 | rouse_wait (...) |
3399 | rouse_wait (...) |
3131 | PROTOTYPE: ;$ |
3400 | PROTOTYPE: ;$ |
3132 | PPCODE: |
3401 | PPCODE: |
3133 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3402 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3134 | |
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 | |
3135 | |
3429 | |
3136 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3430 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3137 | |
3431 | |
3138 | BOOT: |
3432 | BOOT: |
3139 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3433 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |