… | |
… | |
12 | #include "perl.h" |
12 | #include "perl.h" |
13 | #include "XSUB.h" |
13 | #include "XSUB.h" |
14 | #include "perliol.h" |
14 | #include "perliol.h" |
15 | |
15 | |
16 | #include "schmorp.h" |
16 | #include "schmorp.h" |
|
|
17 | |
|
|
18 | #define ECB_NO_THREADS 1 |
17 | #include "ecb.h" |
19 | #include "ecb.h" |
18 | |
20 | |
19 | #include <stddef.h> |
21 | #include <stddef.h> |
20 | #include <stdio.h> |
22 | #include <stdio.h> |
21 | #include <errno.h> |
23 | #include <errno.h> |
… | |
… | |
33 | # define setjmp _setjmp /* deep magic */ |
35 | # define setjmp _setjmp /* deep magic */ |
34 | #else |
36 | #else |
35 | # include <inttypes.h> /* most portable stdint.h */ |
37 | # include <inttypes.h> /* most portable stdint.h */ |
36 | #endif |
38 | #endif |
37 | |
39 | |
38 | #ifdef HAVE_MMAP |
40 | #if HAVE_MMAP |
39 | # include <unistd.h> |
41 | # include <unistd.h> |
40 | # include <sys/mman.h> |
42 | # include <sys/mman.h> |
41 | # ifndef MAP_ANONYMOUS |
43 | # ifndef MAP_ANONYMOUS |
42 | # ifdef MAP_ANON |
44 | # ifdef MAP_ANON |
43 | # define MAP_ANONYMOUS MAP_ANON |
45 | # define MAP_ANONYMOUS MAP_ANON |
… | |
… | |
63 | #endif |
65 | #endif |
64 | |
66 | |
65 | /* the maximum number of idle cctx that will be pooled */ |
67 | /* the maximum number of idle cctx that will be pooled */ |
66 | static int cctx_max_idle = 4; |
68 | static int cctx_max_idle = 4; |
67 | |
69 | |
|
|
70 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
|
|
71 | # define HAS_SCOPESTACK_NAME 1 |
|
|
72 | #endif |
|
|
73 | |
68 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
74 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
69 | # undef CORO_STACKGUARD |
75 | # undef CORO_STACKGUARD |
70 | #endif |
76 | #endif |
71 | |
77 | |
72 | #ifndef CORO_STACKGUARD |
78 | #ifndef CORO_STACKGUARD |
… | |
… | |
174 | void *sptr; |
180 | void *sptr; |
175 | size_t ssize; |
181 | size_t ssize; |
176 | |
182 | |
177 | /* cpu state */ |
183 | /* cpu state */ |
178 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
184 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
185 | #ifndef NDEBUG |
179 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
186 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
|
|
187 | #endif |
180 | JMPENV *top_env; |
188 | JMPENV *top_env; |
181 | coro_context cctx; |
189 | coro_context cctx; |
182 | |
190 | |
183 | U32 gen; |
191 | U32 gen; |
184 | #if CORO_USE_VALGRIND |
192 | #if CORO_USE_VALGRIND |
… | |
… | |
204 | }; |
212 | }; |
205 | |
213 | |
206 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
214 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
207 | typedef struct |
215 | typedef struct |
208 | { |
216 | { |
209 | SV *defsv; |
|
|
210 | AV *defav; |
|
|
211 | SV *errsv; |
|
|
212 | SV *irsgv; |
|
|
213 | HV *hinthv; |
|
|
214 | #define VAR(name,type) type name; |
217 | #define VARx(name,expr,type) type name; |
215 | # include "state.h" |
218 | #include "state.h" |
216 | #undef VAR |
|
|
217 | } perl_slots; |
219 | } perl_slots; |
218 | |
220 | |
219 | // how many context stack entries do we need for perl_slots |
221 | /* how many context stack entries do we need for perl_slots */ |
220 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
222 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
221 | |
223 | |
222 | /* this is a structure representing a perl-level coroutine */ |
224 | /* this is a structure representing a perl-level coroutine */ |
223 | struct coro |
225 | struct coro |
224 | { |
226 | { |
… | |
… | |
290 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
292 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
291 | static CV *cv_coro_run; |
293 | static CV *cv_coro_run; |
292 | static struct coro *coro_first; |
294 | static struct coro *coro_first; |
293 | #define coro_nready coroapi.nready |
295 | #define coro_nready coroapi.nready |
294 | |
296 | |
|
|
297 | /** JIT *********************************************************************/ |
|
|
298 | |
|
|
299 | #if CORO_JIT |
|
|
300 | /* APPLE doesn't have HAVE_MMAP though */ |
|
|
301 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
|
|
302 | #ifndef CORO_JIT_TYPE |
|
|
303 | #if __x86_64 && CORO_JIT_UNIXY |
|
|
304 | #define CORO_JIT_TYPE "amd64-unix" |
|
|
305 | #elif __i386 && CORO_JIT_UNIXY |
|
|
306 | #define CORO_JIT_TYPE "x86-unix" |
|
|
307 | #endif |
|
|
308 | #endif |
|
|
309 | #endif |
|
|
310 | |
|
|
311 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
|
|
312 | #undef CORO_JIT |
|
|
313 | #endif |
|
|
314 | |
|
|
315 | #if CORO_JIT |
|
|
316 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
|
|
317 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
|
|
318 | #endif |
|
|
319 | |
295 | /** Coro::Select ************************************************************/ |
320 | /** Coro::Select ************************************************************/ |
296 | |
321 | |
297 | static OP *(*coro_old_pp_sselect) (pTHX); |
322 | static OP *(*coro_old_pp_sselect) (pTHX); |
298 | static SV *coro_select_select; |
323 | static SV *coro_select_select; |
299 | |
324 | |
… | |
… | |
314 | |
339 | |
315 | /** time stuff **************************************************************/ |
340 | /** time stuff **************************************************************/ |
316 | |
341 | |
317 | #ifdef HAS_GETTIMEOFDAY |
342 | #ifdef HAS_GETTIMEOFDAY |
318 | |
343 | |
319 | ECB_INLINE void |
344 | ecb_inline void |
320 | coro_u2time (pTHX_ UV ret[2]) |
345 | coro_u2time (pTHX_ UV ret[2]) |
321 | { |
346 | { |
322 | struct timeval tv; |
347 | struct timeval tv; |
323 | gettimeofday (&tv, 0); |
348 | gettimeofday (&tv, 0); |
324 | |
349 | |
325 | ret [0] = tv.tv_sec; |
350 | ret [0] = tv.tv_sec; |
326 | ret [1] = tv.tv_usec; |
351 | ret [1] = tv.tv_usec; |
327 | } |
352 | } |
328 | |
353 | |
329 | ECB_INLINE double |
354 | ecb_inline double |
330 | coro_nvtime () |
355 | coro_nvtime (void) |
331 | { |
356 | { |
332 | struct timeval tv; |
357 | struct timeval tv; |
333 | gettimeofday (&tv, 0); |
358 | gettimeofday (&tv, 0); |
334 | |
359 | |
335 | return tv.tv_sec + tv.tv_usec * 1e-6; |
360 | return tv.tv_sec + tv.tv_usec * 1e-6; |
336 | } |
361 | } |
337 | |
362 | |
338 | ECB_INLINE void |
363 | ecb_inline void |
339 | time_init (pTHX) |
364 | time_init (pTHX) |
340 | { |
365 | { |
341 | nvtime = coro_nvtime; |
366 | nvtime = coro_nvtime; |
342 | u2time = coro_u2time; |
367 | u2time = coro_u2time; |
343 | } |
368 | } |
344 | |
369 | |
345 | #else |
370 | #else |
346 | |
371 | |
347 | ECB_INLINE void |
372 | ecb_inline void |
348 | time_init (pTHX) |
373 | time_init (pTHX) |
349 | { |
374 | { |
350 | SV **svp; |
375 | SV **svp; |
351 | |
376 | |
352 | require_pv ("Time/HiRes.pm"); |
377 | require_pv ("Time/HiRes.pm"); |
353 | |
378 | |
354 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
379 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
355 | |
380 | |
356 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
381 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
357 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
382 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
358 | |
383 | |
… | |
… | |
394 | get_hv (name, create); |
419 | get_hv (name, create); |
395 | #endif |
420 | #endif |
396 | return get_hv (name, create); |
421 | return get_hv (name, create); |
397 | } |
422 | } |
398 | |
423 | |
399 | ECB_INLINE void |
424 | ecb_inline void |
400 | coro_times_update () |
425 | coro_times_update (void) |
401 | { |
426 | { |
402 | #ifdef coro_clock_gettime |
427 | #ifdef coro_clock_gettime |
403 | struct timespec ts; |
428 | struct timespec ts; |
404 | |
429 | |
405 | ts.tv_sec = ts.tv_nsec = 0; |
430 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
417 | time_real [0] = tv [0]; |
442 | time_real [0] = tv [0]; |
418 | time_real [1] = tv [1] * 1000; |
443 | time_real [1] = tv [1] * 1000; |
419 | #endif |
444 | #endif |
420 | } |
445 | } |
421 | |
446 | |
422 | ECB_INLINE void |
447 | ecb_inline void |
423 | coro_times_add (struct coro *c) |
448 | coro_times_add (struct coro *c) |
424 | { |
449 | { |
425 | c->t_real [1] += time_real [1]; |
450 | c->t_real [1] += time_real [1]; |
426 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
451 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
427 | c->t_real [0] += time_real [0]; |
452 | c->t_real [0] += time_real [0]; |
… | |
… | |
429 | c->t_cpu [1] += time_cpu [1]; |
454 | c->t_cpu [1] += time_cpu [1]; |
430 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
455 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
431 | c->t_cpu [0] += time_cpu [0]; |
456 | c->t_cpu [0] += time_cpu [0]; |
432 | } |
457 | } |
433 | |
458 | |
434 | ECB_INLINE void |
459 | ecb_inline void |
435 | coro_times_sub (struct coro *c) |
460 | coro_times_sub (struct coro *c) |
436 | { |
461 | { |
437 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
462 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
438 | c->t_real [1] -= time_real [1]; |
463 | c->t_real [1] -= time_real [1]; |
439 | c->t_real [0] -= time_real [0]; |
464 | c->t_real [0] -= time_real [0]; |
… | |
… | |
460 | : 0) |
485 | : 0) |
461 | |
486 | |
462 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
487 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
463 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
488 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
464 | |
489 | |
465 | ECB_INLINE MAGIC * |
490 | ecb_inline MAGIC * |
466 | SvSTATEhv_p (pTHX_ SV *coro) |
491 | SvSTATEhv_p (pTHX_ SV *coro) |
467 | { |
492 | { |
468 | MAGIC *mg; |
493 | MAGIC *mg; |
469 | |
494 | |
470 | if (ecb_expect_true ( |
495 | if (ecb_expect_true ( |
… | |
… | |
475 | return mg; |
500 | return mg; |
476 | |
501 | |
477 | return 0; |
502 | return 0; |
478 | } |
503 | } |
479 | |
504 | |
480 | ECB_INLINE struct coro * |
505 | ecb_inline struct coro * |
481 | SvSTATE_ (pTHX_ SV *coro) |
506 | SvSTATE_ (pTHX_ SV *coro) |
482 | { |
507 | { |
483 | MAGIC *mg; |
508 | MAGIC *mg; |
484 | |
509 | |
485 | if (SvROK (coro)) |
510 | if (SvROK (coro)) |
… | |
… | |
499 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
524 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
500 | |
525 | |
501 | /*****************************************************************************/ |
526 | /*****************************************************************************/ |
502 | /* padlist management and caching */ |
527 | /* padlist management and caching */ |
503 | |
528 | |
504 | ECB_INLINE AV * |
529 | ecb_inline AV * |
505 | coro_derive_padlist (pTHX_ CV *cv) |
530 | coro_derive_padlist (pTHX_ CV *cv) |
506 | { |
531 | { |
507 | AV *padlist = CvPADLIST (cv); |
532 | AV *padlist = CvPADLIST (cv); |
508 | AV *newpadlist, *newpad; |
533 | AV *newpadlist, *newpad; |
509 | |
534 | |
… | |
… | |
521 | av_store (newpadlist, 1, (SV *)newpad); |
546 | av_store (newpadlist, 1, (SV *)newpad); |
522 | |
547 | |
523 | return newpadlist; |
548 | return newpadlist; |
524 | } |
549 | } |
525 | |
550 | |
526 | ECB_INLINE void |
551 | ecb_inline void |
527 | free_padlist (pTHX_ AV *padlist) |
552 | free_padlist (pTHX_ AV *padlist) |
528 | { |
553 | { |
529 | /* may be during global destruction */ |
554 | /* may be during global destruction */ |
530 | if (!IN_DESTRUCT) |
555 | if (!IN_DESTRUCT) |
531 | { |
556 | { |
… | |
… | |
574 | 0, 0, 0, 0, |
599 | 0, 0, 0, 0, |
575 | coro_cv_free |
600 | coro_cv_free |
576 | }; |
601 | }; |
577 | |
602 | |
578 | /* the next two functions merely cache the padlists */ |
603 | /* the next two functions merely cache the padlists */ |
579 | ECB_INLINE void |
604 | ecb_inline void |
580 | get_padlist (pTHX_ CV *cv) |
605 | get_padlist (pTHX_ CV *cv) |
581 | { |
606 | { |
582 | MAGIC *mg = CORO_MAGIC_cv (cv); |
607 | MAGIC *mg = CORO_MAGIC_cv (cv); |
583 | AV *av; |
608 | AV *av; |
584 | |
609 | |
… | |
… | |
597 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
622 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
598 | #endif |
623 | #endif |
599 | } |
624 | } |
600 | } |
625 | } |
601 | |
626 | |
602 | ECB_INLINE void |
627 | ecb_inline void |
603 | put_padlist (pTHX_ CV *cv) |
628 | put_padlist (pTHX_ CV *cv) |
604 | { |
629 | { |
605 | MAGIC *mg = CORO_MAGIC_cv (cv); |
630 | MAGIC *mg = CORO_MAGIC_cv (cv); |
606 | AV *av; |
631 | AV *av; |
607 | |
632 | |
… | |
… | |
616 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
641 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
617 | } |
642 | } |
618 | |
643 | |
619 | /** load & save, init *******************************************************/ |
644 | /** load & save, init *******************************************************/ |
620 | |
645 | |
|
|
646 | ecb_inline void |
|
|
647 | swap_sv (SV *a, SV *b) |
|
|
648 | { |
|
|
649 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
650 | SV tmp; |
|
|
651 | |
|
|
652 | /* swap sv_any */ |
|
|
653 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
654 | |
|
|
655 | /* swap sv_flags */ |
|
|
656 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
657 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
658 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
659 | |
|
|
660 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
661 | /* perl 5.10 and later complicates this _quite_ a bit, but it also |
|
|
662 | * is much faster, so no quarrels here. alternatively, we could |
|
|
663 | * sv_upgrade to avoid this. |
|
|
664 | */ |
|
|
665 | { |
|
|
666 | /* swap sv_u */ |
|
|
667 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
668 | |
|
|
669 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
670 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
671 | */ |
|
|
672 | #define svany_in_head(type) \ |
|
|
673 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
674 | |
|
|
675 | if (svany_in_head (SvTYPE (a))) |
|
|
676 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
677 | |
|
|
678 | if (svany_in_head (SvTYPE (b))) |
|
|
679 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
680 | } |
|
|
681 | #endif |
|
|
682 | } |
|
|
683 | |
621 | /* swap sv heads, at least logically */ |
684 | /* swap sv heads, at least logically */ |
622 | static void |
685 | static void |
623 | swap_svs (pTHX_ Coro__State c) |
686 | swap_svs (pTHX_ Coro__State c) |
624 | { |
687 | { |
625 | int i; |
688 | int i; |
626 | |
689 | |
627 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
690 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
628 | { |
691 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
629 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
630 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
631 | |
|
|
632 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
633 | SV tmp; |
|
|
634 | |
|
|
635 | /* swap sv_any */ |
|
|
636 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
637 | |
|
|
638 | /* swap sv_flags */ |
|
|
639 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
640 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
641 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
642 | |
|
|
643 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
644 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
645 | * much faster, so no quarrels here. alternatively, we could |
|
|
646 | * sv_upgrade to avoid this. |
|
|
647 | */ |
|
|
648 | { |
|
|
649 | /* swap sv_u */ |
|
|
650 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
651 | |
|
|
652 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
653 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
654 | */ |
|
|
655 | #define svany_in_head(type) \ |
|
|
656 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
657 | |
|
|
658 | if (svany_in_head (SvTYPE (a))) |
|
|
659 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
660 | |
|
|
661 | if (svany_in_head (SvTYPE (b))) |
|
|
662 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
663 | } |
|
|
664 | #endif |
|
|
665 | } |
|
|
666 | } |
692 | } |
667 | |
693 | |
668 | #define SWAP_SVS(coro) \ |
694 | #define SWAP_SVS(coro) \ |
669 | if (ecb_expect_false ((coro)->swap_sv)) \ |
695 | if (ecb_expect_false ((coro)->swap_sv)) \ |
670 | swap_svs (aTHX_ (coro)) |
696 | swap_svs (aTHX_ (coro)) |
… | |
… | |
678 | perl_slots *slot = c->slot; |
704 | perl_slots *slot = c->slot; |
679 | c->slot = 0; |
705 | c->slot = 0; |
680 | |
706 | |
681 | PL_mainstack = c->mainstack; |
707 | PL_mainstack = c->mainstack; |
682 | |
708 | |
683 | GvSV (PL_defgv) = slot->defsv; |
709 | #if CORO_JIT |
684 | GvAV (PL_defgv) = slot->defav; |
710 | load_perl_slots (slot); |
685 | GvSV (PL_errgv) = slot->errsv; |
711 | #else |
686 | GvSV (irsgv) = slot->irsgv; |
|
|
687 | GvHV (PL_hintgv) = slot->hinthv; |
|
|
688 | |
|
|
689 | #define VAR(name,type) PL_ ## name = slot->name; |
712 | #define VARx(name,expr,type) expr = slot->name; |
690 | # include "state.h" |
713 | #include "state.h" |
691 | #undef VAR |
714 | #endif |
692 | |
715 | |
693 | { |
716 | { |
694 | dSP; |
717 | dSP; |
695 | |
718 | |
696 | CV *cv; |
719 | CV *cv; |
… | |
… | |
798 | |
821 | |
799 | PUTBACK; |
822 | PUTBACK; |
800 | } |
823 | } |
801 | |
824 | |
802 | /* allocate some space on the context stack for our purposes */ |
825 | /* allocate some space on the context stack for our purposes */ |
803 | if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
826 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
804 | { |
827 | { |
805 | unsigned int i; |
828 | unsigned int i; |
806 | |
829 | |
807 | for (i = 0; i < SLOT_COUNT; ++i) |
830 | for (i = 0; i < SLOT_COUNT; ++i) |
808 | CXINC; |
831 | CXINC; |
… | |
… | |
813 | c->mainstack = PL_mainstack; |
836 | c->mainstack = PL_mainstack; |
814 | |
837 | |
815 | { |
838 | { |
816 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
839 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
817 | |
840 | |
818 | slot->defav = GvAV (PL_defgv); |
841 | #if CORO_JIT |
819 | slot->defsv = DEFSV; |
842 | save_perl_slots (slot); |
820 | slot->errsv = ERRSV; |
843 | #else |
821 | slot->irsgv = GvSV (irsgv); |
|
|
822 | slot->hinthv = GvHV (PL_hintgv); |
|
|
823 | |
|
|
824 | #define VAR(name,type) slot->name = PL_ ## name; |
844 | #define VARx(name,expr,type) slot->name = expr; |
825 | # include "state.h" |
845 | #include "state.h" |
826 | #undef VAR |
846 | #endif |
827 | } |
847 | } |
828 | } |
848 | } |
829 | |
849 | |
830 | /* |
850 | /* |
831 | * allocate various perl stacks. This is almost an exact copy |
851 | * allocate various perl stacks. This is almost an exact copy |
… | |
… | |
862 | #endif |
882 | #endif |
863 | |
883 | |
864 | New(54,PL_scopestack,8,I32); |
884 | New(54,PL_scopestack,8,I32); |
865 | PL_scopestack_ix = 0; |
885 | PL_scopestack_ix = 0; |
866 | PL_scopestack_max = 8; |
886 | PL_scopestack_max = 8; |
|
|
887 | #if HAS_SCOPESTACK_NAME |
|
|
888 | New(54,PL_scopestack_name,8,const char*); |
|
|
889 | #endif |
867 | |
890 | |
868 | New(54,PL_savestack,24,ANY); |
891 | New(54,PL_savestack,24,ANY); |
869 | PL_savestack_ix = 0; |
892 | PL_savestack_ix = 0; |
870 | PL_savestack_max = 24; |
893 | PL_savestack_max = 24; |
871 | |
894 | |
… | |
… | |
899 | } |
922 | } |
900 | |
923 | |
901 | Safefree (PL_tmps_stack); |
924 | Safefree (PL_tmps_stack); |
902 | Safefree (PL_markstack); |
925 | Safefree (PL_markstack); |
903 | Safefree (PL_scopestack); |
926 | Safefree (PL_scopestack); |
|
|
927 | #if HAS_SCOPESTACK_NAME |
|
|
928 | Safefree (PL_scopestack_name); |
|
|
929 | #endif |
904 | Safefree (PL_savestack); |
930 | Safefree (PL_savestack); |
905 | #if !PERL_VERSION_ATLEAST (5,10,0) |
931 | #if !PERL_VERSION_ATLEAST (5,10,0) |
906 | Safefree (PL_retstack); |
932 | Safefree (PL_retstack); |
907 | #endif |
933 | #endif |
908 | } |
934 | } |
… | |
… | |
968 | { |
994 | { |
969 | SV **svp = 0; |
995 | SV **svp = 0; |
970 | |
996 | |
971 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
997 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
972 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
998 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
973 | |
999 | |
974 | if (svp) |
1000 | if (svp) |
975 | { |
1001 | { |
976 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
1002 | SV *ssv; |
|
|
1003 | |
|
|
1004 | if (!*svp) |
|
|
1005 | ssv = &PL_sv_undef; |
|
|
1006 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
|
|
1007 | ssv = sv_2mortal (newRV_inc (*svp)); |
|
|
1008 | else |
|
|
1009 | ssv = *svp; |
|
|
1010 | |
|
|
1011 | sv_setsv (sv, ssv); |
977 | return 0; |
1012 | return 0; |
978 | } |
1013 | } |
979 | } |
1014 | } |
980 | |
1015 | |
981 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1016 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
… | |
… | |
1075 | PL_hints = 0; |
1110 | PL_hints = 0; |
1076 | |
1111 | |
1077 | /* recreate the die/warn hooks */ |
1112 | /* recreate the die/warn hooks */ |
1078 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1113 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1079 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1114 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1080 | |
1115 | |
1081 | GvSV (PL_defgv) = newSV (0); |
1116 | GvSV (PL_defgv) = newSV (0); |
1082 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1117 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1083 | GvSV (PL_errgv) = newSV (0); |
1118 | GvSV (PL_errgv) = newSV (0); |
1084 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1119 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1085 | GvHV (PL_hintgv) = 0; |
1120 | GvHV (PL_hintgv) = 0; |
… | |
… | |
1173 | /* restore swapped sv's */ |
1208 | /* restore swapped sv's */ |
1174 | SWAP_SVS (coro); |
1209 | SWAP_SVS (coro); |
1175 | |
1210 | |
1176 | coro_destruct_stacks (aTHX); |
1211 | coro_destruct_stacks (aTHX); |
1177 | |
1212 | |
1178 | // now save some sv's to be free'd later |
1213 | /* now save some sv's to be free'd later */ |
1179 | svf [0] = GvSV (PL_defgv); |
1214 | svf [0] = GvSV (PL_defgv); |
1180 | svf [1] = (SV *)GvAV (PL_defgv); |
1215 | svf [1] = (SV *)GvAV (PL_defgv); |
1181 | svf [2] = GvSV (PL_errgv); |
1216 | svf [2] = GvSV (PL_errgv); |
1182 | svf [3] = (SV *)PL_defoutgv; |
1217 | svf [3] = (SV *)PL_defoutgv; |
1183 | svf [4] = PL_rs; |
1218 | svf [4] = PL_rs; |
… | |
… | |
1203 | SvREFCNT_dec (coro->invoke_cb); |
1238 | SvREFCNT_dec (coro->invoke_cb); |
1204 | SvREFCNT_dec (coro->invoke_av); |
1239 | SvREFCNT_dec (coro->invoke_av); |
1205 | } |
1240 | } |
1206 | } |
1241 | } |
1207 | |
1242 | |
1208 | ECB_INLINE void |
1243 | ecb_inline void |
1209 | free_coro_mortal (pTHX) |
1244 | free_coro_mortal (pTHX) |
1210 | { |
1245 | { |
1211 | if (ecb_expect_true (coro_mortal)) |
1246 | if (ecb_expect_true (coro_mortal)) |
1212 | { |
1247 | { |
1213 | SvREFCNT_dec ((SV *)coro_mortal); |
1248 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1372 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1407 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1373 | slf_frame.check = slf_check_set_stacklevel; |
1408 | slf_frame.check = slf_check_set_stacklevel; |
1374 | } |
1409 | } |
1375 | |
1410 | |
1376 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1411 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1377 | ECB_INLINE void |
1412 | ecb_inline void |
1378 | transfer_tail (pTHX) |
1413 | transfer_tail (pTHX) |
1379 | { |
1414 | { |
1380 | free_coro_mortal (aTHX); |
1415 | free_coro_mortal (aTHX); |
|
|
1416 | } |
|
|
1417 | |
|
|
1418 | /* try to exit the same way perl's main function would do */ |
|
|
1419 | /* we do not bother resetting the environment or other things *7 |
|
|
1420 | /* that are not, uhm, essential */ |
|
|
1421 | /* this obviously also doesn't work when perl is embedded */ |
|
|
1422 | static void ecb_noinline ecb_cold |
|
|
1423 | perlish_exit (pTHX) |
|
|
1424 | { |
|
|
1425 | int exitstatus = perl_destruct (PL_curinterp); |
|
|
1426 | perl_free (PL_curinterp); |
|
|
1427 | exit (exitstatus); |
1381 | } |
1428 | } |
1382 | |
1429 | |
1383 | /* |
1430 | /* |
1384 | * this is a _very_ stripped down perl interpreter ;) |
1431 | * this is a _very_ stripped down perl interpreter ;) |
1385 | */ |
1432 | */ |
… | |
… | |
1414 | */ |
1461 | */ |
1415 | |
1462 | |
1416 | /* |
1463 | /* |
1417 | * If perl-run returns we assume exit() was being called or the coro |
1464 | * If perl-run returns we assume exit() was being called or the coro |
1418 | * fell off the end, which seems to be the only valid (non-bug) |
1465 | * fell off the end, which seems to be the only valid (non-bug) |
1419 | * reason for perl_run to return. We try to exit by jumping to the |
1466 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1420 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1467 | * doing in that case. YMMV. |
1421 | * coroutine as Coro has no such concept. |
|
|
1422 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1423 | * that backend are too broken to do it in a standards-compliant way. |
|
|
1424 | */ |
1468 | */ |
1425 | PL_top_env = main_top_env; |
1469 | perlish_exit (aTHX); |
1426 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1427 | } |
1470 | } |
1428 | } |
1471 | } |
1429 | |
1472 | |
1430 | static coro_cctx * |
1473 | static coro_cctx * |
1431 | cctx_new () |
1474 | cctx_new (void) |
1432 | { |
1475 | { |
1433 | coro_cctx *cctx; |
1476 | coro_cctx *cctx; |
1434 | |
1477 | |
1435 | ++cctx_count; |
1478 | ++cctx_count; |
1436 | New (0, cctx, 1, coro_cctx); |
1479 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1442 | return cctx; |
1485 | return cctx; |
1443 | } |
1486 | } |
1444 | |
1487 | |
1445 | /* create a new cctx only suitable as source */ |
1488 | /* create a new cctx only suitable as source */ |
1446 | static coro_cctx * |
1489 | static coro_cctx * |
1447 | cctx_new_empty () |
1490 | cctx_new_empty (void) |
1448 | { |
1491 | { |
1449 | coro_cctx *cctx = cctx_new (); |
1492 | coro_cctx *cctx = cctx_new (); |
1450 | |
1493 | |
1451 | cctx->sptr = 0; |
1494 | cctx->sptr = 0; |
1452 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1495 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
… | |
… | |
1454 | return cctx; |
1497 | return cctx; |
1455 | } |
1498 | } |
1456 | |
1499 | |
1457 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1500 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1458 | static coro_cctx * |
1501 | static coro_cctx * |
1459 | cctx_new_run () |
1502 | cctx_new_run (void) |
1460 | { |
1503 | { |
1461 | coro_cctx *cctx = cctx_new (); |
1504 | coro_cctx *cctx = cctx_new (); |
1462 | void *stack_start; |
1505 | void *stack_start; |
1463 | size_t stack_size; |
1506 | size_t stack_size; |
1464 | |
1507 | |
1465 | #if HAVE_MMAP |
1508 | #if HAVE_MMAP |
1466 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1509 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1467 | /* mmap supposedly does allocate-on-write for us */ |
1510 | /* mmap supposedly does allocate-on-write for us */ |
1468 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1511 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0); |
1469 | |
1512 | |
1470 | if (cctx->sptr != (void *)-1) |
1513 | if (cctx->sptr != (void *)-1) |
1471 | { |
1514 | { |
1472 | #if CORO_STACKGUARD |
1515 | #if CORO_STACKGUARD |
1473 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1516 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1688 | return; |
1731 | return; |
1689 | |
1732 | |
1690 | slf_destroy (aTHX_ coro); |
1733 | slf_destroy (aTHX_ coro); |
1691 | |
1734 | |
1692 | coro->flags |= CF_ZOMBIE; |
1735 | coro->flags |= CF_ZOMBIE; |
1693 | |
1736 | |
1694 | if (coro->flags & CF_READY) |
1737 | if (coro->flags & CF_READY) |
1695 | { |
1738 | { |
1696 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1739 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1697 | /* alternative: look through all ready queues and remove the coro */ |
1740 | /* alternative: look through all ready queues and remove the coro */ |
1698 | --coro_nready; |
1741 | --coro_nready; |
… | |
… | |
1775 | TRANSFER (ta, 1); |
1818 | TRANSFER (ta, 1); |
1776 | } |
1819 | } |
1777 | |
1820 | |
1778 | /** Coro ********************************************************************/ |
1821 | /** Coro ********************************************************************/ |
1779 | |
1822 | |
1780 | ECB_INLINE void |
1823 | ecb_inline void |
1781 | coro_enq (pTHX_ struct coro *coro) |
1824 | coro_enq (pTHX_ struct coro *coro) |
1782 | { |
1825 | { |
1783 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1826 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1784 | |
1827 | |
1785 | SvREFCNT_inc_NN (coro->hv); |
1828 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1787 | coro->next_ready = 0; |
1830 | coro->next_ready = 0; |
1788 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1831 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1789 | ready [1] = coro; |
1832 | ready [1] = coro; |
1790 | } |
1833 | } |
1791 | |
1834 | |
1792 | ECB_INLINE struct coro * |
1835 | ecb_inline struct coro * |
1793 | coro_deq (pTHX) |
1836 | coro_deq (pTHX) |
1794 | { |
1837 | { |
1795 | int prio; |
1838 | int prio; |
1796 | |
1839 | |
1797 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1840 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1850 | { |
1893 | { |
1851 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1894 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1852 | } |
1895 | } |
1853 | |
1896 | |
1854 | /* expects to own a reference to next->hv */ |
1897 | /* expects to own a reference to next->hv */ |
1855 | ECB_INLINE void |
1898 | ecb_inline void |
1856 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1899 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1857 | { |
1900 | { |
1858 | SV *prev_sv = SvRV (coro_current); |
1901 | SV *prev_sv = SvRV (coro_current); |
1859 | |
1902 | |
1860 | ta->prev = SvSTATE_hv (prev_sv); |
1903 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1919 | } |
1962 | } |
1920 | } |
1963 | } |
1921 | } |
1964 | } |
1922 | } |
1965 | } |
1923 | |
1966 | |
1924 | ECB_INLINE void |
1967 | ecb_inline void |
1925 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1968 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1926 | { |
1969 | { |
1927 | api_ready (aTHX_ coro_current); |
1970 | api_ready (aTHX_ coro_current); |
1928 | prepare_schedule (aTHX_ ta); |
1971 | prepare_schedule (aTHX_ ta); |
1929 | } |
1972 | } |
1930 | |
1973 | |
1931 | ECB_INLINE void |
1974 | ecb_inline void |
1932 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1975 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1933 | { |
1976 | { |
1934 | SV *prev = SvRV (coro_current); |
1977 | SV *prev = SvRV (coro_current); |
1935 | |
1978 | |
1936 | if (coro_nready) |
1979 | if (coro_nready) |
… | |
… | |
2131 | |
2174 | |
2132 | static void |
2175 | static void |
2133 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2176 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2134 | { |
2177 | { |
2135 | AV *av; |
2178 | AV *av; |
2136 | |
2179 | |
2137 | if (coro->status) |
2180 | if (coro->status) |
2138 | { |
2181 | { |
2139 | av = coro->status; |
2182 | av = coro->status; |
2140 | av_clear (av); |
2183 | av_clear (av); |
2141 | } |
2184 | } |
… | |
… | |
2188 | |
2231 | |
2189 | coro = SvSTATE (arg [0]); |
2232 | coro = SvSTATE (arg [0]); |
2190 | coro_hv = coro->hv; |
2233 | coro_hv = coro->hv; |
2191 | |
2234 | |
2192 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2235 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2193 | |
2236 | |
2194 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2237 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2195 | { |
2238 | { |
2196 | /* coro currently busy cancelling something, so just notify it */ |
2239 | /* coro currently busy cancelling something, so just notify it */ |
2197 | coro->slf_frame.data = (void *)coro; |
2240 | coro->slf_frame.data = (void *)coro; |
2198 | |
2241 | |
… | |
… | |
2377 | |
2420 | |
2378 | static int |
2421 | static int |
2379 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2422 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2380 | { |
2423 | { |
2381 | SV *data = (SV *)frame->data; |
2424 | SV *data = (SV *)frame->data; |
2382 | |
2425 | |
2383 | if (CORO_THROW) |
2426 | if (CORO_THROW) |
2384 | return 0; |
2427 | return 0; |
2385 | |
2428 | |
2386 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2429 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2387 | return 1; |
2430 | return 1; |
… | |
… | |
2423 | cb = sv_2mortal (coro->rouse_cb); |
2466 | cb = sv_2mortal (coro->rouse_cb); |
2424 | coro->rouse_cb = 0; |
2467 | coro->rouse_cb = 0; |
2425 | } |
2468 | } |
2426 | |
2469 | |
2427 | if (!SvROK (cb) |
2470 | if (!SvROK (cb) |
2428 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2471 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2429 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2472 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2430 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2473 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2431 | |
2474 | |
2432 | { |
2475 | { |
2433 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2476 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
… | |
… | |
2936 | { |
2979 | { |
2937 | int i; |
2980 | int i; |
2938 | /* if we were woken up but can't down, we look through the whole */ |
2981 | /* if we were woken up but can't down, we look through the whole */ |
2939 | /* waiters list and only add us if we aren't in there already */ |
2982 | /* waiters list and only add us if we aren't in there already */ |
2940 | /* this avoids some degenerate memory usage cases */ |
2983 | /* this avoids some degenerate memory usage cases */ |
2941 | for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug |
2984 | for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */ |
2942 | if (AvARRAY (av)[i] == coro_hv) |
2985 | if (AvARRAY (av)[i] == coro_hv) |
2943 | return 1; |
2986 | return 1; |
2944 | |
2987 | |
2945 | av_push (av, SvREFCNT_inc (coro_hv)); |
2988 | av_push (av, SvREFCNT_inc (coro_hv)); |
2946 | return 1; |
2989 | return 1; |
… | |
… | |
3323 | |
3366 | |
3324 | #ifndef __cplusplus |
3367 | #ifndef __cplusplus |
3325 | ecb_cold XS(boot_Coro__State); |
3368 | ecb_cold XS(boot_Coro__State); |
3326 | #endif |
3369 | #endif |
3327 | |
3370 | |
|
|
3371 | #if CORO_JIT |
|
|
3372 | |
|
|
3373 | static void ecb_noinline ecb_cold |
|
|
3374 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
|
|
3375 | { |
|
|
3376 | dSP; |
|
|
3377 | AV *av = newAV (); |
|
|
3378 | |
|
|
3379 | av_store (av, 3, newSVuv (d)); |
|
|
3380 | av_store (av, 2, newSVuv (c)); |
|
|
3381 | av_store (av, 1, newSVuv (b)); |
|
|
3382 | av_store (av, 0, newSVuv (a)); |
|
|
3383 | |
|
|
3384 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
3385 | |
|
|
3386 | PUTBACK; |
|
|
3387 | } |
|
|
3388 | |
|
|
3389 | static void ecb_noinline ecb_cold |
|
|
3390 | jit_init (pTHX) |
|
|
3391 | { |
|
|
3392 | dSP; |
|
|
3393 | SV *load, *save; |
|
|
3394 | char *map_base; |
|
|
3395 | char *load_ptr, *save_ptr; |
|
|
3396 | STRLEN load_len, save_len, map_len; |
|
|
3397 | int count; |
|
|
3398 | |
|
|
3399 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3400 | |
|
|
3401 | PUSHMARK (SP); |
|
|
3402 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
|
|
3403 | #include "state.h" |
|
|
3404 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
|
|
3405 | SPAGAIN; |
|
|
3406 | |
|
|
3407 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
|
|
3408 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
|
|
3409 | |
|
|
3410 | map_len = load_len + save_len + 16; |
|
|
3411 | |
|
|
3412 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
|
|
3413 | |
|
|
3414 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
|
|
3415 | |
|
|
3416 | load_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3417 | memcpy (map_base, load_ptr, load_len); |
|
|
3418 | |
|
|
3419 | map_base += (load_len + 15) & ~15; |
|
|
3420 | |
|
|
3421 | save_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3422 | memcpy (map_base, save_ptr, save_len); |
|
|
3423 | |
|
|
3424 | /* we are good citizens and try to make the page read-only, so the evil evil */ |
|
|
3425 | /* hackers might have it a bit more difficult */ |
|
|
3426 | mprotect (map_base, map_len, PROT_READ | PROT_EXEC); |
|
|
3427 | |
|
|
3428 | PUTBACK; |
|
|
3429 | eval_pv ("undef &Coro::State::_jit", 1); |
|
|
3430 | } |
|
|
3431 | |
|
|
3432 | #endif |
|
|
3433 | |
3328 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3434 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3329 | |
3435 | |
3330 | PROTOTYPES: DISABLE |
3436 | PROTOTYPES: DISABLE |
3331 | |
3437 | |
3332 | BOOT: |
3438 | BOOT: |
… | |
… | |
3335 | # if CORO_PTHREAD |
3441 | # if CORO_PTHREAD |
3336 | coro_thx = PERL_GET_CONTEXT; |
3442 | coro_thx = PERL_GET_CONTEXT; |
3337 | # endif |
3443 | # endif |
3338 | #endif |
3444 | #endif |
3339 | BOOT_PAGESIZE; |
3445 | BOOT_PAGESIZE; |
|
|
3446 | |
|
|
3447 | /* perl defines these to check for existance first, but why it doesn't */ |
|
|
3448 | /* just create them one at init time is not clear to me, except for */ |
|
|
3449 | /* programs trying to delete them, but... */ |
|
|
3450 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
|
|
3451 | DEFSV; |
|
|
3452 | ERRSV; |
3340 | |
3453 | |
3341 | cctx_current = cctx_new_empty (); |
3454 | cctx_current = cctx_new_empty (); |
3342 | |
3455 | |
3343 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3456 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3344 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3457 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
3387 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3500 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3388 | |
3501 | |
3389 | time_init (aTHX); |
3502 | time_init (aTHX); |
3390 | |
3503 | |
3391 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3504 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
|
|
3505 | #if CORO_JIT |
|
|
3506 | PUTBACK; |
|
|
3507 | jit_init (aTHX); |
|
|
3508 | SPAGAIN; |
|
|
3509 | #endif |
3392 | } |
3510 | } |
3393 | |
3511 | |
3394 | SV * |
3512 | SV * |
3395 | new (SV *klass, ...) |
3513 | new (SV *klass, ...) |
3396 | ALIAS: |
3514 | ALIAS: |
… | |
… | |
3547 | RETVAL = boolSV (coro->flags & ix); |
3665 | RETVAL = boolSV (coro->flags & ix); |
3548 | OUTPUT: |
3666 | OUTPUT: |
3549 | RETVAL |
3667 | RETVAL |
3550 | |
3668 | |
3551 | void |
3669 | void |
3552 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3670 | throw (SV *self, SV *exception = &PL_sv_undef) |
3553 | PROTOTYPE: $;$ |
3671 | PROTOTYPE: $;$ |
3554 | CODE: |
3672 | CODE: |
3555 | { |
3673 | { |
|
|
3674 | struct coro *coro = SvSTATE (self); |
3556 | struct coro *current = SvSTATE_current; |
3675 | struct coro *current = SvSTATE_current; |
3557 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3676 | SV **exceptionp = coro == current ? &CORO_THROW : &coro->except; |
3558 | SvREFCNT_dec (*exceptionp); |
3677 | SvREFCNT_dec (*exceptionp); |
3559 | SvGETMAGIC (exception); |
3678 | SvGETMAGIC (exception); |
3560 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3679 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
|
|
3680 | |
|
|
3681 | api_ready (aTHX_ self); |
3561 | } |
3682 | } |
3562 | |
3683 | |
3563 | void |
3684 | void |
3564 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3685 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3565 | PROTOTYPE: $;$ |
3686 | PROTOTYPE: $;$ |
… | |
… | |
3695 | |
3816 | |
3696 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3817 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3697 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3818 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3698 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3819 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3699 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3820 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3700 | |
3821 | |
3701 | coro_stash = gv_stashpv ("Coro", TRUE); |
3822 | coro_stash = gv_stashpv ("Coro", TRUE); |
3702 | |
3823 | |
3703 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3824 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3704 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3825 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3705 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3826 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
4046 | } |
4167 | } |
4047 | |
4168 | |
4048 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4169 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4049 | |
4170 | |
4050 | void |
4171 | void |
4051 | _may_delete (SV *sem, int count, int extra_refs) |
4172 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4052 | PPCODE: |
4173 | PPCODE: |
4053 | { |
4174 | { |
4054 | AV *av = (AV *)SvRV (sem); |
4175 | AV *av = (AV *)SvRV (sem); |
4055 | |
4176 | |
4056 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4177 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |