… | |
… | |
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 | #include "ecb.h" |
17 | |
18 | |
|
|
19 | #include <stddef.h> |
18 | #include <stdio.h> |
20 | #include <stdio.h> |
19 | #include <errno.h> |
21 | #include <errno.h> |
20 | #include <assert.h> |
22 | #include <assert.h> |
21 | |
23 | |
22 | #ifndef SVs_PADSTALE |
24 | #ifndef SVs_PADSTALE |
… | |
… | |
31 | # define setjmp _setjmp /* deep magic */ |
33 | # define setjmp _setjmp /* deep magic */ |
32 | #else |
34 | #else |
33 | # include <inttypes.h> /* most portable stdint.h */ |
35 | # include <inttypes.h> /* most portable stdint.h */ |
34 | #endif |
36 | #endif |
35 | |
37 | |
36 | #ifdef HAVE_MMAP |
38 | #if HAVE_MMAP |
37 | # include <unistd.h> |
39 | # include <unistd.h> |
38 | # include <sys/mman.h> |
40 | # include <sys/mman.h> |
39 | # ifndef MAP_ANONYMOUS |
41 | # ifndef MAP_ANONYMOUS |
40 | # ifdef MAP_ANON |
42 | # ifdef MAP_ANON |
41 | # define MAP_ANONYMOUS MAP_ANON |
43 | # define MAP_ANONYMOUS MAP_ANON |
… | |
… | |
86 | # define STACKLEVEL ((void *)&stacklevel) |
88 | # define STACKLEVEL ((void *)&stacklevel) |
87 | #endif |
89 | #endif |
88 | |
90 | |
89 | #define IN_DESTRUCT PL_dirty |
91 | #define IN_DESTRUCT PL_dirty |
90 | |
92 | |
91 | #if __GNUC__ >= 3 |
|
|
92 | # define attribute(x) __attribute__(x) |
|
|
93 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
94 | # define INLINE static inline |
|
|
95 | #else |
|
|
96 | # define attribute(x) |
|
|
97 | # define expect(expr,value) (expr) |
|
|
98 | # define INLINE static |
|
|
99 | #endif |
|
|
100 | |
|
|
101 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
102 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
103 | |
|
|
104 | #define NOINLINE attribute ((noinline)) |
|
|
105 | |
|
|
106 | #include "CoroAPI.h" |
93 | #include "CoroAPI.h" |
107 | #define GCoroAPI (&coroapi) /* very sneaky */ |
94 | #define GCoroAPI (&coroapi) /* very sneaky */ |
108 | |
95 | |
109 | #ifdef USE_ITHREADS |
96 | #ifdef USE_ITHREADS |
110 | # if CORO_PTHREAD |
97 | # if CORO_PTHREAD |
111 | static void *coro_thx; |
98 | static void *coro_thx; |
112 | # endif |
99 | # endif |
113 | #endif |
100 | #endif |
|
|
101 | |
|
|
102 | /* used in state.h */ |
|
|
103 | #define VAR(name,type) VARx(name, PL_ ## name, type) |
114 | |
104 | |
115 | #ifdef __linux |
105 | #ifdef __linux |
116 | # include <time.h> /* for timespec */ |
106 | # include <time.h> /* for timespec */ |
117 | # include <syscall.h> /* for SYS_* */ |
107 | # include <syscall.h> /* for SYS_* */ |
118 | # ifdef SYS_clock_gettime |
108 | # ifdef SYS_clock_gettime |
… | |
… | |
217 | }; |
207 | }; |
218 | |
208 | |
219 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
209 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
220 | typedef struct |
210 | typedef struct |
221 | { |
211 | { |
222 | SV *defsv; |
|
|
223 | AV *defav; |
|
|
224 | SV *errsv; |
|
|
225 | SV *irsgv; |
|
|
226 | HV *hinthv; |
|
|
227 | #define VAR(name,type) type name; |
212 | #define VARx(name,expr,type) type name; |
228 | # include "state.h" |
213 | # include "state.h" |
229 | #undef VAR |
214 | #undef VARx |
230 | } perl_slots; |
215 | } perl_slots; |
231 | |
216 | |
232 | // how many context stack entries do we need for perl_slots |
217 | /* how many context stack entries do we need for perl_slots */ |
233 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
218 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
234 | |
219 | |
235 | /* this is a structure representing a perl-level coroutine */ |
220 | /* this is a structure representing a perl-level coroutine */ |
236 | struct coro |
221 | struct coro |
237 | { |
222 | { |
… | |
… | |
303 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
288 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
304 | static CV *cv_coro_run; |
289 | static CV *cv_coro_run; |
305 | static struct coro *coro_first; |
290 | static struct coro *coro_first; |
306 | #define coro_nready coroapi.nready |
291 | #define coro_nready coroapi.nready |
307 | |
292 | |
|
|
293 | /** JIT *********************************************************************/ |
|
|
294 | |
|
|
295 | #if CORO_JIT |
|
|
296 | /* APPLE doesn't have HAVE_MMAP though */ |
|
|
297 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
|
|
298 | #ifndef CORO_JIT_TYPE |
|
|
299 | #if __x86_64 && CORO_JIT_UNIXY |
|
|
300 | #define CORO_JIT_TYPE "amd64-unix" |
|
|
301 | #elif __i386 && CORO_JIT_UNIXY |
|
|
302 | #define CORO_JIT_TYPE "x86-unix" |
|
|
303 | #endif |
|
|
304 | #endif |
|
|
305 | #endif |
|
|
306 | |
|
|
307 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
|
|
308 | #undef CORO_JIT |
|
|
309 | #endif |
|
|
310 | |
|
|
311 | #if CORO_JIT |
|
|
312 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
|
|
313 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
|
|
314 | #endif |
|
|
315 | |
308 | /** Coro::Select ************************************************************/ |
316 | /** Coro::Select ************************************************************/ |
309 | |
317 | |
310 | static OP *(*coro_old_pp_sselect) (pTHX); |
318 | static OP *(*coro_old_pp_sselect) (pTHX); |
311 | static SV *coro_select_select; |
319 | static SV *coro_select_select; |
312 | |
320 | |
… | |
… | |
327 | |
335 | |
328 | /** time stuff **************************************************************/ |
336 | /** time stuff **************************************************************/ |
329 | |
337 | |
330 | #ifdef HAS_GETTIMEOFDAY |
338 | #ifdef HAS_GETTIMEOFDAY |
331 | |
339 | |
332 | static void |
340 | ECB_INLINE void |
333 | coro_u2time (pTHX_ UV ret[2]) |
341 | coro_u2time (pTHX_ UV ret[2]) |
334 | { |
342 | { |
335 | struct timeval tv; |
343 | struct timeval tv; |
336 | gettimeofday (&tv, 0); |
344 | gettimeofday (&tv, 0); |
337 | |
345 | |
338 | ret [0] = tv.tv_sec; |
346 | ret [0] = tv.tv_sec; |
339 | ret [1] = tv.tv_usec; |
347 | ret [1] = tv.tv_usec; |
340 | } |
348 | } |
341 | |
349 | |
342 | static double |
350 | ECB_INLINE double |
343 | coro_nvtime () |
351 | coro_nvtime (void) |
344 | { |
352 | { |
345 | struct timeval tv; |
353 | struct timeval tv; |
346 | gettimeofday (&tv, 0); |
354 | gettimeofday (&tv, 0); |
347 | |
355 | |
348 | return tv.tv_sec + tv.tv_usec * 1e-6; |
356 | return tv.tv_sec + tv.tv_usec * 1e-6; |
349 | } |
357 | } |
350 | |
358 | |
351 | static void |
359 | ECB_INLINE void |
352 | time_init (pTHX) |
360 | time_init (pTHX) |
353 | { |
361 | { |
354 | nvtime = coro_nvtime; |
362 | nvtime = coro_nvtime; |
355 | u2time = coro_u2time; |
363 | u2time = coro_u2time; |
356 | } |
364 | } |
357 | |
365 | |
358 | #else |
366 | #else |
359 | |
367 | |
360 | static void |
368 | ECB_INLINE void |
361 | time_init (pTHX) |
369 | time_init (pTHX) |
362 | { |
370 | { |
363 | SV **svp; |
371 | SV **svp; |
364 | |
372 | |
365 | require_pv ("Time/HiRes.pm"); |
373 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
377 | |
385 | |
378 | #endif |
386 | #endif |
379 | |
387 | |
380 | /** lowlevel stuff **********************************************************/ |
388 | /** lowlevel stuff **********************************************************/ |
381 | |
389 | |
382 | static SV * |
390 | static SV * ecb_noinline |
383 | coro_get_sv (pTHX_ const char *name, int create) |
391 | coro_get_sv (pTHX_ const char *name, int create) |
384 | { |
392 | { |
385 | #if PERL_VERSION_ATLEAST (5,10,0) |
393 | #if PERL_VERSION_ATLEAST (5,10,0) |
386 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
394 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
387 | get_sv (name, create); |
395 | get_sv (name, create); |
388 | #endif |
396 | #endif |
389 | return get_sv (name, create); |
397 | return get_sv (name, create); |
390 | } |
398 | } |
391 | |
399 | |
392 | static AV * |
400 | static AV * ecb_noinline |
393 | coro_get_av (pTHX_ const char *name, int create) |
401 | coro_get_av (pTHX_ const char *name, int create) |
394 | { |
402 | { |
395 | #if PERL_VERSION_ATLEAST (5,10,0) |
403 | #if PERL_VERSION_ATLEAST (5,10,0) |
396 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
404 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
397 | get_av (name, create); |
405 | get_av (name, create); |
398 | #endif |
406 | #endif |
399 | return get_av (name, create); |
407 | return get_av (name, create); |
400 | } |
408 | } |
401 | |
409 | |
402 | static HV * |
410 | static HV * ecb_noinline |
403 | coro_get_hv (pTHX_ const char *name, int create) |
411 | coro_get_hv (pTHX_ const char *name, int create) |
404 | { |
412 | { |
405 | #if PERL_VERSION_ATLEAST (5,10,0) |
413 | #if PERL_VERSION_ATLEAST (5,10,0) |
406 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
414 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
407 | get_hv (name, create); |
415 | get_hv (name, create); |
408 | #endif |
416 | #endif |
409 | return get_hv (name, create); |
417 | return get_hv (name, create); |
410 | } |
418 | } |
411 | |
419 | |
412 | INLINE void |
420 | ECB_INLINE void |
413 | coro_times_update () |
421 | coro_times_update (void) |
414 | { |
422 | { |
415 | #ifdef coro_clock_gettime |
423 | #ifdef coro_clock_gettime |
416 | struct timespec ts; |
424 | struct timespec ts; |
417 | |
425 | |
418 | ts.tv_sec = ts.tv_nsec = 0; |
426 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
430 | time_real [0] = tv [0]; |
438 | time_real [0] = tv [0]; |
431 | time_real [1] = tv [1] * 1000; |
439 | time_real [1] = tv [1] * 1000; |
432 | #endif |
440 | #endif |
433 | } |
441 | } |
434 | |
442 | |
435 | INLINE void |
443 | ECB_INLINE void |
436 | coro_times_add (struct coro *c) |
444 | coro_times_add (struct coro *c) |
437 | { |
445 | { |
438 | c->t_real [1] += time_real [1]; |
446 | c->t_real [1] += time_real [1]; |
439 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
447 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
440 | c->t_real [0] += time_real [0]; |
448 | c->t_real [0] += time_real [0]; |
… | |
… | |
442 | c->t_cpu [1] += time_cpu [1]; |
450 | c->t_cpu [1] += time_cpu [1]; |
443 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
451 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
444 | c->t_cpu [0] += time_cpu [0]; |
452 | c->t_cpu [0] += time_cpu [0]; |
445 | } |
453 | } |
446 | |
454 | |
447 | INLINE void |
455 | ECB_INLINE void |
448 | coro_times_sub (struct coro *c) |
456 | coro_times_sub (struct coro *c) |
449 | { |
457 | { |
450 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
458 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
451 | c->t_real [1] -= time_real [1]; |
459 | c->t_real [1] -= time_real [1]; |
452 | c->t_real [0] -= time_real [0]; |
460 | c->t_real [0] -= time_real [0]; |
… | |
… | |
460 | /* magic glue */ |
468 | /* magic glue */ |
461 | |
469 | |
462 | #define CORO_MAGIC_type_cv 26 |
470 | #define CORO_MAGIC_type_cv 26 |
463 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
471 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
464 | |
472 | |
465 | #define CORO_MAGIC_NN(sv, type) \ |
473 | #define CORO_MAGIC_NN(sv, type) \ |
466 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
474 | (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \ |
467 | ? SvMAGIC (sv) \ |
475 | ? SvMAGIC (sv) \ |
468 | : mg_find (sv, type)) |
476 | : mg_find (sv, type)) |
469 | |
477 | |
470 | #define CORO_MAGIC(sv, type) \ |
478 | #define CORO_MAGIC(sv, type) \ |
471 | (expect_true (SvMAGIC (sv)) \ |
479 | (ecb_expect_true (SvMAGIC (sv)) \ |
472 | ? CORO_MAGIC_NN (sv, type) \ |
480 | ? CORO_MAGIC_NN (sv, type) \ |
473 | : 0) |
481 | : 0) |
474 | |
482 | |
475 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
483 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
476 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
484 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
477 | |
485 | |
478 | INLINE MAGIC * |
486 | ECB_INLINE MAGIC * |
479 | SvSTATEhv_p (pTHX_ SV *coro) |
487 | SvSTATEhv_p (pTHX_ SV *coro) |
480 | { |
488 | { |
481 | MAGIC *mg; |
489 | MAGIC *mg; |
482 | |
490 | |
483 | if (expect_true ( |
491 | if (ecb_expect_true ( |
484 | SvTYPE (coro) == SVt_PVHV |
492 | SvTYPE (coro) == SVt_PVHV |
485 | && (mg = CORO_MAGIC_state (coro)) |
493 | && (mg = CORO_MAGIC_state (coro)) |
486 | && mg->mg_virtual == &coro_state_vtbl |
494 | && mg->mg_virtual == &coro_state_vtbl |
487 | )) |
495 | )) |
488 | return mg; |
496 | return mg; |
489 | |
497 | |
490 | return 0; |
498 | return 0; |
491 | } |
499 | } |
492 | |
500 | |
493 | INLINE struct coro * |
501 | ECB_INLINE struct coro * |
494 | SvSTATE_ (pTHX_ SV *coro) |
502 | SvSTATE_ (pTHX_ SV *coro) |
495 | { |
503 | { |
496 | MAGIC *mg; |
504 | MAGIC *mg; |
497 | |
505 | |
498 | if (SvROK (coro)) |
506 | if (SvROK (coro)) |
499 | coro = SvRV (coro); |
507 | coro = SvRV (coro); |
500 | |
508 | |
501 | mg = SvSTATEhv_p (coro); |
509 | mg = SvSTATEhv_p (aTHX_ coro); |
502 | if (!mg) |
510 | if (!mg) |
503 | croak ("Coro::State object required"); |
511 | croak ("Coro::State object required"); |
504 | |
512 | |
505 | return (struct coro *)mg->mg_ptr; |
513 | return (struct coro *)mg->mg_ptr; |
506 | } |
514 | } |
… | |
… | |
512 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
520 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
513 | |
521 | |
514 | /*****************************************************************************/ |
522 | /*****************************************************************************/ |
515 | /* padlist management and caching */ |
523 | /* padlist management and caching */ |
516 | |
524 | |
517 | static AV * |
525 | ECB_INLINE AV * |
518 | coro_derive_padlist (pTHX_ CV *cv) |
526 | coro_derive_padlist (pTHX_ CV *cv) |
519 | { |
527 | { |
520 | AV *padlist = CvPADLIST (cv); |
528 | AV *padlist = CvPADLIST (cv); |
521 | AV *newpadlist, *newpad; |
529 | AV *newpadlist, *newpad; |
522 | |
530 | |
… | |
… | |
534 | av_store (newpadlist, 1, (SV *)newpad); |
542 | av_store (newpadlist, 1, (SV *)newpad); |
535 | |
543 | |
536 | return newpadlist; |
544 | return newpadlist; |
537 | } |
545 | } |
538 | |
546 | |
539 | static void |
547 | ECB_INLINE void |
540 | free_padlist (pTHX_ AV *padlist) |
548 | free_padlist (pTHX_ AV *padlist) |
541 | { |
549 | { |
542 | /* may be during global destruction */ |
550 | /* may be during global destruction */ |
543 | if (!IN_DESTRUCT) |
551 | if (!IN_DESTRUCT) |
544 | { |
552 | { |
… | |
… | |
587 | 0, 0, 0, 0, |
595 | 0, 0, 0, 0, |
588 | coro_cv_free |
596 | coro_cv_free |
589 | }; |
597 | }; |
590 | |
598 | |
591 | /* the next two functions merely cache the padlists */ |
599 | /* the next two functions merely cache the padlists */ |
592 | static void |
600 | ECB_INLINE void |
593 | get_padlist (pTHX_ CV *cv) |
601 | get_padlist (pTHX_ CV *cv) |
594 | { |
602 | { |
595 | MAGIC *mg = CORO_MAGIC_cv (cv); |
603 | MAGIC *mg = CORO_MAGIC_cv (cv); |
596 | AV *av; |
604 | AV *av; |
597 | |
605 | |
598 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
606 | if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
599 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
607 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
600 | else |
608 | else |
601 | { |
609 | { |
602 | #if CORO_PREFER_PERL_FUNCTIONS |
610 | #if CORO_PREFER_PERL_FUNCTIONS |
603 | /* this is probably cleaner? but also slower! */ |
611 | /* this is probably cleaner? but also slower! */ |
… | |
… | |
610 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
618 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
611 | #endif |
619 | #endif |
612 | } |
620 | } |
613 | } |
621 | } |
614 | |
622 | |
615 | static void |
623 | ECB_INLINE void |
616 | put_padlist (pTHX_ CV *cv) |
624 | put_padlist (pTHX_ CV *cv) |
617 | { |
625 | { |
618 | MAGIC *mg = CORO_MAGIC_cv (cv); |
626 | MAGIC *mg = CORO_MAGIC_cv (cv); |
619 | AV *av; |
627 | AV *av; |
620 | |
628 | |
621 | if (expect_false (!mg)) |
629 | if (ecb_expect_false (!mg)) |
622 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
630 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
623 | |
631 | |
624 | av = (AV *)mg->mg_obj; |
632 | av = (AV *)mg->mg_obj; |
625 | |
633 | |
626 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
634 | if (ecb_expect_false (AvFILLp (av) >= AvMAX (av))) |
627 | av_extend (av, AvFILLp (av) + 1); |
635 | av_extend (av, AvFILLp (av) + 1); |
628 | |
636 | |
629 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
637 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
630 | } |
638 | } |
631 | |
639 | |
… | |
… | |
676 | } |
684 | } |
677 | #endif |
685 | #endif |
678 | } |
686 | } |
679 | } |
687 | } |
680 | |
688 | |
681 | #define SWAP_SVS(coro) \ |
689 | #define SWAP_SVS(coro) \ |
682 | if (expect_false ((coro)->swap_sv)) \ |
690 | if (ecb_expect_false ((coro)->swap_sv)) \ |
683 | swap_svs (aTHX_ (coro)) |
691 | swap_svs (aTHX_ (coro)) |
684 | |
692 | |
685 | static void |
693 | static void |
686 | on_enterleave_call (pTHX_ SV *cb); |
694 | on_enterleave_call (pTHX_ SV *cb); |
687 | |
695 | |
… | |
… | |
691 | perl_slots *slot = c->slot; |
699 | perl_slots *slot = c->slot; |
692 | c->slot = 0; |
700 | c->slot = 0; |
693 | |
701 | |
694 | PL_mainstack = c->mainstack; |
702 | PL_mainstack = c->mainstack; |
695 | |
703 | |
696 | GvSV (PL_defgv) = slot->defsv; |
704 | #if CORO_JIT |
697 | GvAV (PL_defgv) = slot->defav; |
705 | load_perl_slots (slot); |
698 | GvSV (PL_errgv) = slot->errsv; |
706 | #else |
699 | GvSV (irsgv) = slot->irsgv; |
|
|
700 | GvHV (PL_hintgv) = slot->hinthv; |
|
|
701 | |
|
|
702 | #define VAR(name,type) PL_ ## name = slot->name; |
707 | #define VARx(name,expr,type) expr = slot->name; |
703 | # include "state.h" |
708 | # include "state.h" |
704 | #undef VAR |
709 | #undef VARx |
|
|
710 | #endif |
705 | |
711 | |
706 | { |
712 | { |
707 | dSP; |
713 | dSP; |
708 | |
714 | |
709 | CV *cv; |
715 | CV *cv; |
710 | |
716 | |
711 | /* now do the ugly restore mess */ |
717 | /* now do the ugly restore mess */ |
712 | while (expect_true (cv = (CV *)POPs)) |
718 | while (ecb_expect_true (cv = (CV *)POPs)) |
713 | { |
719 | { |
714 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
720 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
715 | CvDEPTH (cv) = PTR2IV (POPs); |
721 | CvDEPTH (cv) = PTR2IV (POPs); |
716 | CvPADLIST (cv) = (AV *)POPs; |
722 | CvPADLIST (cv) = (AV *)POPs; |
717 | } |
723 | } |
… | |
… | |
720 | } |
726 | } |
721 | |
727 | |
722 | slf_frame = c->slf_frame; |
728 | slf_frame = c->slf_frame; |
723 | CORO_THROW = c->except; |
729 | CORO_THROW = c->except; |
724 | |
730 | |
725 | if (expect_false (enable_times)) |
731 | if (ecb_expect_false (enable_times)) |
726 | { |
732 | { |
727 | if (expect_false (!times_valid)) |
733 | if (ecb_expect_false (!times_valid)) |
728 | coro_times_update (); |
734 | coro_times_update (); |
729 | |
735 | |
730 | coro_times_sub (c); |
736 | coro_times_sub (c); |
731 | } |
737 | } |
732 | |
738 | |
733 | if (expect_false (c->on_enter)) |
739 | if (ecb_expect_false (c->on_enter)) |
734 | { |
740 | { |
735 | int i; |
741 | int i; |
736 | |
742 | |
737 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
743 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
738 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
744 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
… | |
… | |
744 | static void |
750 | static void |
745 | save_perl (pTHX_ Coro__State c) |
751 | save_perl (pTHX_ Coro__State c) |
746 | { |
752 | { |
747 | SWAP_SVS (c); |
753 | SWAP_SVS (c); |
748 | |
754 | |
749 | if (expect_false (c->on_leave)) |
755 | if (ecb_expect_false (c->on_leave)) |
750 | { |
756 | { |
751 | int i; |
757 | int i; |
752 | |
758 | |
753 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
759 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
754 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
760 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
755 | } |
761 | } |
756 | |
762 | |
757 | times_valid = 0; |
763 | times_valid = 0; |
758 | |
764 | |
759 | if (expect_false (enable_times)) |
765 | if (ecb_expect_false (enable_times)) |
760 | { |
766 | { |
761 | coro_times_update (); times_valid = 1; |
767 | coro_times_update (); times_valid = 1; |
762 | coro_times_add (c); |
768 | coro_times_add (c); |
763 | } |
769 | } |
764 | |
770 | |
… | |
… | |
778 | |
784 | |
779 | XPUSHs (Nullsv); |
785 | XPUSHs (Nullsv); |
780 | /* this loop was inspired by pp_caller */ |
786 | /* this loop was inspired by pp_caller */ |
781 | for (;;) |
787 | for (;;) |
782 | { |
788 | { |
783 | while (expect_true (cxix >= 0)) |
789 | while (ecb_expect_true (cxix >= 0)) |
784 | { |
790 | { |
785 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
791 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
786 | |
792 | |
787 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
793 | if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT)) |
788 | { |
794 | { |
789 | CV *cv = cx->blk_sub.cv; |
795 | CV *cv = cx->blk_sub.cv; |
790 | |
796 | |
791 | if (expect_true (CvDEPTH (cv))) |
797 | if (ecb_expect_true (CvDEPTH (cv))) |
792 | { |
798 | { |
793 | EXTEND (SP, 3); |
799 | EXTEND (SP, 3); |
794 | PUSHs ((SV *)CvPADLIST (cv)); |
800 | PUSHs ((SV *)CvPADLIST (cv)); |
795 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
801 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
796 | PUSHs ((SV *)cv); |
802 | PUSHs ((SV *)cv); |
… | |
… | |
799 | get_padlist (aTHX_ cv); |
805 | get_padlist (aTHX_ cv); |
800 | } |
806 | } |
801 | } |
807 | } |
802 | } |
808 | } |
803 | |
809 | |
804 | if (expect_true (top_si->si_type == PERLSI_MAIN)) |
810 | if (ecb_expect_true (top_si->si_type == PERLSI_MAIN)) |
805 | break; |
811 | break; |
806 | |
812 | |
807 | top_si = top_si->si_prev; |
813 | top_si = top_si->si_prev; |
808 | ccstk = top_si->si_cxstack; |
814 | ccstk = top_si->si_cxstack; |
809 | cxix = top_si->si_cxix; |
815 | cxix = top_si->si_cxix; |
… | |
… | |
811 | |
817 | |
812 | PUTBACK; |
818 | PUTBACK; |
813 | } |
819 | } |
814 | |
820 | |
815 | /* allocate some space on the context stack for our purposes */ |
821 | /* allocate some space on the context stack for our purposes */ |
816 | if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
822 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
817 | { |
823 | { |
818 | unsigned int i; |
824 | unsigned int i; |
819 | |
825 | |
820 | for (i = 0; i < SLOT_COUNT; ++i) |
826 | for (i = 0; i < SLOT_COUNT; ++i) |
821 | CXINC; |
827 | CXINC; |
… | |
… | |
826 | c->mainstack = PL_mainstack; |
832 | c->mainstack = PL_mainstack; |
827 | |
833 | |
828 | { |
834 | { |
829 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
835 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
830 | |
836 | |
831 | slot->defav = GvAV (PL_defgv); |
837 | #if CORO_JIT |
832 | slot->defsv = DEFSV; |
838 | save_perl_slots (slot); |
833 | slot->errsv = ERRSV; |
839 | #else |
834 | slot->irsgv = GvSV (irsgv); |
|
|
835 | slot->hinthv = GvHV (PL_hintgv); |
|
|
836 | |
|
|
837 | #define VAR(name,type) slot->name = PL_ ## name; |
840 | #define VARx(name,expr,type) slot->name = expr; |
838 | # include "state.h" |
841 | # include "state.h" |
839 | #undef VAR |
842 | #undef VARx |
|
|
843 | #endif |
840 | } |
844 | } |
841 | } |
845 | } |
842 | |
846 | |
843 | /* |
847 | /* |
844 | * allocate various perl stacks. This is almost an exact copy |
848 | * allocate various perl stacks. This is almost an exact copy |
… | |
… | |
967 | #endif |
971 | #endif |
968 | |
972 | |
969 | /* |
973 | /* |
970 | * This overrides the default magic get method of %SIG elements. |
974 | * This overrides the default magic get method of %SIG elements. |
971 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
975 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
972 | * and instead of trying to save and restore the hash elements, we just provide |
976 | * and instead of trying to save and restore the hash elements (extremely slow), |
973 | * readback here. |
977 | * we just provide our own readback here. |
974 | */ |
978 | */ |
975 | static int |
979 | static int ecb_cold |
976 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
980 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
977 | { |
981 | { |
978 | const char *s = MgPV_nolen_const (mg); |
982 | const char *s = MgPV_nolen_const (mg); |
979 | |
983 | |
980 | if (*s == '_') |
984 | if (*s == '_') |
… | |
… | |
992 | } |
996 | } |
993 | |
997 | |
994 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
998 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
995 | } |
999 | } |
996 | |
1000 | |
997 | static int |
1001 | static int ecb_cold |
998 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1002 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
999 | { |
1003 | { |
1000 | const char *s = MgPV_nolen_const (mg); |
1004 | const char *s = MgPV_nolen_const (mg); |
1001 | |
1005 | |
1002 | if (*s == '_') |
1006 | if (*s == '_') |
… | |
… | |
1016 | } |
1020 | } |
1017 | |
1021 | |
1018 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1022 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1019 | } |
1023 | } |
1020 | |
1024 | |
1021 | static int |
1025 | static int ecb_cold |
1022 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1026 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1023 | { |
1027 | { |
1024 | const char *s = MgPV_nolen_const (mg); |
1028 | const char *s = MgPV_nolen_const (mg); |
1025 | |
1029 | |
1026 | if (*s == '_') |
1030 | if (*s == '_') |
… | |
… | |
1061 | return 1; |
1065 | return 1; |
1062 | } |
1066 | } |
1063 | |
1067 | |
1064 | static UNOP init_perl_op; |
1068 | static UNOP init_perl_op; |
1065 | |
1069 | |
1066 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1070 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1067 | init_perl (pTHX_ struct coro *coro) |
1071 | init_perl (pTHX_ struct coro *coro) |
1068 | { |
1072 | { |
1069 | /* |
1073 | /* |
1070 | * emulate part of the perl startup here. |
1074 | * emulate part of the perl startup here. |
1071 | */ |
1075 | */ |
… | |
… | |
1086 | PL_parser = 0; |
1090 | PL_parser = 0; |
1087 | #endif |
1091 | #endif |
1088 | PL_hints = 0; |
1092 | PL_hints = 0; |
1089 | |
1093 | |
1090 | /* recreate the die/warn hooks */ |
1094 | /* recreate the die/warn hooks */ |
1091 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
1095 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1092 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
1096 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1093 | |
1097 | |
1094 | GvSV (PL_defgv) = newSV (0); |
1098 | GvSV (PL_defgv) = newSV (0); |
1095 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1099 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1096 | GvSV (PL_errgv) = newSV (0); |
1100 | GvSV (PL_errgv) = newSV (0); |
1097 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1101 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
1133 | /* copy throw, in case it was set before init_perl */ |
1137 | /* copy throw, in case it was set before init_perl */ |
1134 | CORO_THROW = coro->except; |
1138 | CORO_THROW = coro->except; |
1135 | |
1139 | |
1136 | SWAP_SVS (coro); |
1140 | SWAP_SVS (coro); |
1137 | |
1141 | |
1138 | if (expect_false (enable_times)) |
1142 | if (ecb_expect_false (enable_times)) |
1139 | { |
1143 | { |
1140 | coro_times_update (); |
1144 | coro_times_update (); |
1141 | coro_times_sub (coro); |
1145 | coro_times_sub (coro); |
1142 | } |
1146 | } |
1143 | } |
1147 | } |
… | |
… | |
1186 | /* restore swapped sv's */ |
1190 | /* restore swapped sv's */ |
1187 | SWAP_SVS (coro); |
1191 | SWAP_SVS (coro); |
1188 | |
1192 | |
1189 | coro_destruct_stacks (aTHX); |
1193 | coro_destruct_stacks (aTHX); |
1190 | |
1194 | |
1191 | // now save some sv's to be free'd later |
1195 | /* now save some sv's to be free'd later */ |
1192 | svf [0] = GvSV (PL_defgv); |
1196 | svf [0] = GvSV (PL_defgv); |
1193 | svf [1] = (SV *)GvAV (PL_defgv); |
1197 | svf [1] = (SV *)GvAV (PL_defgv); |
1194 | svf [2] = GvSV (PL_errgv); |
1198 | svf [2] = GvSV (PL_errgv); |
1195 | svf [3] = (SV *)PL_defoutgv; |
1199 | svf [3] = (SV *)PL_defoutgv; |
1196 | svf [4] = PL_rs; |
1200 | svf [4] = PL_rs; |
… | |
… | |
1216 | SvREFCNT_dec (coro->invoke_cb); |
1220 | SvREFCNT_dec (coro->invoke_cb); |
1217 | SvREFCNT_dec (coro->invoke_av); |
1221 | SvREFCNT_dec (coro->invoke_av); |
1218 | } |
1222 | } |
1219 | } |
1223 | } |
1220 | |
1224 | |
1221 | INLINE void |
1225 | ECB_INLINE void |
1222 | free_coro_mortal (pTHX) |
1226 | free_coro_mortal (pTHX) |
1223 | { |
1227 | { |
1224 | if (expect_true (coro_mortal)) |
1228 | if (ecb_expect_true (coro_mortal)) |
1225 | { |
1229 | { |
1226 | SvREFCNT_dec ((SV *)coro_mortal); |
1230 | SvREFCNT_dec ((SV *)coro_mortal); |
1227 | coro_mortal = 0; |
1231 | coro_mortal = 0; |
1228 | } |
1232 | } |
1229 | } |
1233 | } |
… | |
… | |
1364 | |
1368 | |
1365 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1369 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1366 | } |
1370 | } |
1367 | |
1371 | |
1368 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1372 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1369 | static void NOINLINE |
1373 | static void ecb_noinline |
1370 | cctx_prepare (pTHX) |
1374 | cctx_prepare (pTHX) |
1371 | { |
1375 | { |
1372 | PL_top_env = &PL_start_env; |
1376 | PL_top_env = &PL_start_env; |
1373 | |
1377 | |
1374 | if (cctx_current->flags & CC_TRACE) |
1378 | if (cctx_current->flags & CC_TRACE) |
… | |
… | |
1385 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1389 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1386 | slf_frame.check = slf_check_set_stacklevel; |
1390 | slf_frame.check = slf_check_set_stacklevel; |
1387 | } |
1391 | } |
1388 | |
1392 | |
1389 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1393 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1390 | INLINE void |
1394 | ECB_INLINE void |
1391 | transfer_tail (pTHX) |
1395 | transfer_tail (pTHX) |
1392 | { |
1396 | { |
1393 | free_coro_mortal (aTHX); |
1397 | free_coro_mortal (aTHX); |
1394 | } |
1398 | } |
1395 | |
1399 | |
… | |
… | |
1439 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1443 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1440 | } |
1444 | } |
1441 | } |
1445 | } |
1442 | |
1446 | |
1443 | static coro_cctx * |
1447 | static coro_cctx * |
1444 | cctx_new () |
1448 | cctx_new (void) |
1445 | { |
1449 | { |
1446 | coro_cctx *cctx; |
1450 | coro_cctx *cctx; |
1447 | |
1451 | |
1448 | ++cctx_count; |
1452 | ++cctx_count; |
1449 | New (0, cctx, 1, coro_cctx); |
1453 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1455 | return cctx; |
1459 | return cctx; |
1456 | } |
1460 | } |
1457 | |
1461 | |
1458 | /* create a new cctx only suitable as source */ |
1462 | /* create a new cctx only suitable as source */ |
1459 | static coro_cctx * |
1463 | static coro_cctx * |
1460 | cctx_new_empty () |
1464 | cctx_new_empty (void) |
1461 | { |
1465 | { |
1462 | coro_cctx *cctx = cctx_new (); |
1466 | coro_cctx *cctx = cctx_new (); |
1463 | |
1467 | |
1464 | cctx->sptr = 0; |
1468 | cctx->sptr = 0; |
1465 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1469 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
… | |
… | |
1467 | return cctx; |
1471 | return cctx; |
1468 | } |
1472 | } |
1469 | |
1473 | |
1470 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1474 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1471 | static coro_cctx * |
1475 | static coro_cctx * |
1472 | cctx_new_run () |
1476 | cctx_new_run (void) |
1473 | { |
1477 | { |
1474 | coro_cctx *cctx = cctx_new (); |
1478 | coro_cctx *cctx = cctx_new (); |
1475 | void *stack_start; |
1479 | void *stack_start; |
1476 | size_t stack_size; |
1480 | size_t stack_size; |
1477 | |
1481 | |
1478 | #if HAVE_MMAP |
1482 | #if HAVE_MMAP |
1479 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1483 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1480 | /* mmap supposedly does allocate-on-write for us */ |
1484 | /* mmap supposedly does allocate-on-write for us */ |
1481 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1485 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); |
1482 | |
1486 | |
1483 | if (cctx->sptr != (void *)-1) |
1487 | if (cctx->sptr != (void *)-1) |
1484 | { |
1488 | { |
1485 | #if CORO_STACKGUARD |
1489 | #if CORO_STACKGUARD |
1486 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1490 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1547 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1551 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1548 | |
1552 | |
1549 | static coro_cctx * |
1553 | static coro_cctx * |
1550 | cctx_get (pTHX) |
1554 | cctx_get (pTHX) |
1551 | { |
1555 | { |
1552 | while (expect_true (cctx_first)) |
1556 | while (ecb_expect_true (cctx_first)) |
1553 | { |
1557 | { |
1554 | coro_cctx *cctx = cctx_first; |
1558 | coro_cctx *cctx = cctx_first; |
1555 | cctx_first = cctx->next; |
1559 | cctx_first = cctx->next; |
1556 | --cctx_idle; |
1560 | --cctx_idle; |
1557 | |
1561 | |
1558 | if (expect_true (!CCTX_EXPIRED (cctx))) |
1562 | if (ecb_expect_true (!CCTX_EXPIRED (cctx))) |
1559 | return cctx; |
1563 | return cctx; |
1560 | |
1564 | |
1561 | cctx_destroy (cctx); |
1565 | cctx_destroy (cctx); |
1562 | } |
1566 | } |
1563 | |
1567 | |
… | |
… | |
1568 | cctx_put (coro_cctx *cctx) |
1572 | cctx_put (coro_cctx *cctx) |
1569 | { |
1573 | { |
1570 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1574 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1571 | |
1575 | |
1572 | /* free another cctx if overlimit */ |
1576 | /* free another cctx if overlimit */ |
1573 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1577 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1574 | { |
1578 | { |
1575 | coro_cctx *first = cctx_first; |
1579 | coro_cctx *first = cctx_first; |
1576 | cctx_first = first->next; |
1580 | cctx_first = first->next; |
1577 | --cctx_idle; |
1581 | --cctx_idle; |
1578 | |
1582 | |
… | |
… | |
1589 | static void |
1593 | static void |
1590 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1594 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1591 | { |
1595 | { |
1592 | /* TODO: throwing up here is considered harmful */ |
1596 | /* TODO: throwing up here is considered harmful */ |
1593 | |
1597 | |
1594 | if (expect_true (prev != next)) |
1598 | if (ecb_expect_true (prev != next)) |
1595 | { |
1599 | { |
1596 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1600 | if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1597 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1601 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1598 | |
1602 | |
1599 | if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1603 | if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1600 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1604 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1601 | |
1605 | |
1602 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1606 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1603 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1607 | if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING)) |
1604 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1608 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1605 | #endif |
1609 | #endif |
1606 | } |
1610 | } |
1607 | } |
1611 | } |
1608 | |
1612 | |
1609 | /* always use the TRANSFER macro */ |
1613 | /* always use the TRANSFER macro */ |
1610 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1614 | static void ecb_noinline /* noinline so we have a fixed stackframe */ |
1611 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1615 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1612 | { |
1616 | { |
1613 | dSTACKLEVEL; |
1617 | dSTACKLEVEL; |
1614 | |
1618 | |
1615 | /* sometimes transfer is only called to set idle_sp */ |
1619 | /* sometimes transfer is only called to set idle_sp */ |
1616 | if (expect_false (!prev)) |
1620 | if (ecb_expect_false (!prev)) |
1617 | { |
1621 | { |
1618 | cctx_current->idle_sp = STACKLEVEL; |
1622 | cctx_current->idle_sp = STACKLEVEL; |
1619 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1623 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1620 | } |
1624 | } |
1621 | else if (expect_true (prev != next)) |
1625 | else if (ecb_expect_true (prev != next)) |
1622 | { |
1626 | { |
1623 | coro_cctx *cctx_prev; |
1627 | coro_cctx *cctx_prev; |
1624 | |
1628 | |
1625 | if (expect_false (prev->flags & CF_NEW)) |
1629 | if (ecb_expect_false (prev->flags & CF_NEW)) |
1626 | { |
1630 | { |
1627 | /* create a new empty/source context */ |
1631 | /* create a new empty/source context */ |
1628 | prev->flags &= ~CF_NEW; |
1632 | prev->flags &= ~CF_NEW; |
1629 | prev->flags |= CF_RUNNING; |
1633 | prev->flags |= CF_RUNNING; |
1630 | } |
1634 | } |
… | |
… | |
1633 | next->flags |= CF_RUNNING; |
1637 | next->flags |= CF_RUNNING; |
1634 | |
1638 | |
1635 | /* first get rid of the old state */ |
1639 | /* first get rid of the old state */ |
1636 | save_perl (aTHX_ prev); |
1640 | save_perl (aTHX_ prev); |
1637 | |
1641 | |
1638 | if (expect_false (next->flags & CF_NEW)) |
1642 | if (ecb_expect_false (next->flags & CF_NEW)) |
1639 | { |
1643 | { |
1640 | /* need to start coroutine */ |
1644 | /* need to start coroutine */ |
1641 | next->flags &= ~CF_NEW; |
1645 | next->flags &= ~CF_NEW; |
1642 | /* setup coroutine call */ |
1646 | /* setup coroutine call */ |
1643 | init_perl (aTHX_ next); |
1647 | init_perl (aTHX_ next); |
1644 | } |
1648 | } |
1645 | else |
1649 | else |
1646 | load_perl (aTHX_ next); |
1650 | load_perl (aTHX_ next); |
1647 | |
1651 | |
1648 | /* possibly untie and reuse the cctx */ |
1652 | /* possibly untie and reuse the cctx */ |
1649 | if (expect_true ( |
1653 | if (ecb_expect_true ( |
1650 | cctx_current->idle_sp == STACKLEVEL |
1654 | cctx_current->idle_sp == STACKLEVEL |
1651 | && !(cctx_current->flags & CC_TRACE) |
1655 | && !(cctx_current->flags & CC_TRACE) |
1652 | && !force_cctx |
1656 | && !force_cctx |
1653 | )) |
1657 | )) |
1654 | { |
1658 | { |
1655 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1659 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1656 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1660 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1657 | |
1661 | |
1658 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1662 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1659 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1663 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1660 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1664 | if (ecb_expect_false (CCTX_EXPIRED (cctx_current))) |
1661 | if (expect_true (!next->cctx)) |
1665 | if (ecb_expect_true (!next->cctx)) |
1662 | next->cctx = cctx_get (aTHX); |
1666 | next->cctx = cctx_get (aTHX); |
1663 | |
1667 | |
1664 | cctx_put (cctx_current); |
1668 | cctx_put (cctx_current); |
1665 | } |
1669 | } |
1666 | else |
1670 | else |
1667 | prev->cctx = cctx_current; |
1671 | prev->cctx = cctx_current; |
1668 | |
1672 | |
1669 | ++next->usecount; |
1673 | ++next->usecount; |
1670 | |
1674 | |
1671 | cctx_prev = cctx_current; |
1675 | cctx_prev = cctx_current; |
1672 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1676 | cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1673 | |
1677 | |
1674 | next->cctx = 0; |
1678 | next->cctx = 0; |
1675 | |
1679 | |
1676 | if (expect_false (cctx_prev != cctx_current)) |
1680 | if (ecb_expect_false (cctx_prev != cctx_current)) |
1677 | { |
1681 | { |
1678 | cctx_prev->top_env = PL_top_env; |
1682 | cctx_prev->top_env = PL_top_env; |
1679 | PL_top_env = cctx_current->top_env; |
1683 | PL_top_env = cctx_current->top_env; |
1680 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1684 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1681 | } |
1685 | } |
… | |
… | |
1727 | SvREFCNT_dec (coro->startcv); |
1731 | SvREFCNT_dec (coro->startcv); |
1728 | SvREFCNT_dec (coro->args); |
1732 | SvREFCNT_dec (coro->args); |
1729 | SvREFCNT_dec (coro->swap_sv); |
1733 | SvREFCNT_dec (coro->swap_sv); |
1730 | SvREFCNT_dec (CORO_THROW); |
1734 | SvREFCNT_dec (CORO_THROW); |
1731 | |
1735 | |
1732 | coro_call_on_destroy (coro); |
1736 | coro_call_on_destroy (aTHX_ coro); |
1733 | |
1737 | |
1734 | /* more destruction mayhem in coro_state_free */ |
1738 | /* more destruction mayhem in coro_state_free */ |
1735 | } |
1739 | } |
1736 | |
1740 | |
1737 | static int |
1741 | static int |
1738 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1742 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1739 | { |
1743 | { |
1740 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1744 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1741 | mg->mg_ptr = 0; |
1745 | mg->mg_ptr = 0; |
1742 | |
1746 | |
1743 | coro_state_destroy (coro); |
1747 | coro_state_destroy (aTHX_ coro); |
1744 | SvREFCNT_dec (coro->on_destroy); |
1748 | SvREFCNT_dec (coro->on_destroy); |
1745 | SvREFCNT_dec (coro->status); |
1749 | SvREFCNT_dec (coro->status); |
1746 | |
1750 | |
1747 | Safefree (coro); |
1751 | Safefree (coro); |
1748 | |
1752 | |
1749 | return 0; |
1753 | return 0; |
1750 | } |
1754 | } |
1751 | |
1755 | |
1752 | static int |
1756 | static int ecb_cold |
1753 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1757 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1754 | { |
1758 | { |
1755 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1759 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1756 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1760 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1757 | mg->mg_ptr = 0; |
1761 | mg->mg_ptr = 0; |
… | |
… | |
1788 | TRANSFER (ta, 1); |
1792 | TRANSFER (ta, 1); |
1789 | } |
1793 | } |
1790 | |
1794 | |
1791 | /** Coro ********************************************************************/ |
1795 | /** Coro ********************************************************************/ |
1792 | |
1796 | |
1793 | INLINE void |
1797 | ECB_INLINE void |
1794 | coro_enq (pTHX_ struct coro *coro) |
1798 | coro_enq (pTHX_ struct coro *coro) |
1795 | { |
1799 | { |
1796 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1800 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1797 | |
1801 | |
1798 | SvREFCNT_inc_NN (coro->hv); |
1802 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1800 | coro->next_ready = 0; |
1804 | coro->next_ready = 0; |
1801 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1805 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1802 | ready [1] = coro; |
1806 | ready [1] = coro; |
1803 | } |
1807 | } |
1804 | |
1808 | |
1805 | INLINE struct coro * |
1809 | ECB_INLINE struct coro * |
1806 | coro_deq (pTHX) |
1810 | coro_deq (pTHX) |
1807 | { |
1811 | { |
1808 | int prio; |
1812 | int prio; |
1809 | |
1813 | |
1810 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1814 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1863 | { |
1867 | { |
1864 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1868 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1865 | } |
1869 | } |
1866 | |
1870 | |
1867 | /* expects to own a reference to next->hv */ |
1871 | /* expects to own a reference to next->hv */ |
1868 | INLINE void |
1872 | ECB_INLINE void |
1869 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1873 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1870 | { |
1874 | { |
1871 | SV *prev_sv = SvRV (coro_current); |
1875 | SV *prev_sv = SvRV (coro_current); |
1872 | |
1876 | |
1873 | ta->prev = SvSTATE_hv (prev_sv); |
1877 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1886 | { |
1890 | { |
1887 | for (;;) |
1891 | for (;;) |
1888 | { |
1892 | { |
1889 | struct coro *next = coro_deq (aTHX); |
1893 | struct coro *next = coro_deq (aTHX); |
1890 | |
1894 | |
1891 | if (expect_true (next)) |
1895 | if (ecb_expect_true (next)) |
1892 | { |
1896 | { |
1893 | /* cannot transfer to destroyed coros, skip and look for next */ |
1897 | /* cannot transfer to destroyed coros, skip and look for next */ |
1894 | if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1898 | if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1895 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1899 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1896 | else |
1900 | else |
1897 | { |
1901 | { |
1898 | next->flags &= ~CF_READY; |
1902 | next->flags &= ~CF_READY; |
1899 | --coro_nready; |
1903 | --coro_nready; |
… | |
… | |
1932 | } |
1936 | } |
1933 | } |
1937 | } |
1934 | } |
1938 | } |
1935 | } |
1939 | } |
1936 | |
1940 | |
1937 | INLINE void |
1941 | ECB_INLINE void |
1938 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1942 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1939 | { |
1943 | { |
1940 | api_ready (aTHX_ coro_current); |
1944 | api_ready (aTHX_ coro_current); |
1941 | prepare_schedule (aTHX_ ta); |
1945 | prepare_schedule (aTHX_ ta); |
1942 | } |
1946 | } |
1943 | |
1947 | |
1944 | INLINE void |
1948 | ECB_INLINE void |
1945 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1949 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1946 | { |
1950 | { |
1947 | SV *prev = SvRV (coro_current); |
1951 | SV *prev = SvRV (coro_current); |
1948 | |
1952 | |
1949 | if (coro_nready) |
1953 | if (coro_nready) |
… | |
… | |
1979 | { |
1983 | { |
1980 | struct coro_transfer_args ta; |
1984 | struct coro_transfer_args ta; |
1981 | |
1985 | |
1982 | prepare_cede (aTHX_ &ta); |
1986 | prepare_cede (aTHX_ &ta); |
1983 | |
1987 | |
1984 | if (expect_true (ta.prev != ta.next)) |
1988 | if (ecb_expect_true (ta.prev != ta.next)) |
1985 | { |
1989 | { |
1986 | TRANSFER (ta, 1); |
1990 | TRANSFER (ta, 1); |
1987 | return 1; |
1991 | return 1; |
1988 | } |
1992 | } |
1989 | else |
1993 | else |
… | |
… | |
2054 | PUTBACK; |
2058 | PUTBACK; |
2055 | } |
2059 | } |
2056 | } |
2060 | } |
2057 | |
2061 | |
2058 | static void |
2062 | static void |
2059 | coro_push_on_destroy (aTHX_ struct coro *coro, SV *cb) |
2063 | coro_push_on_destroy (pTHX_ struct coro *coro, SV *cb) |
2060 | { |
2064 | { |
2061 | if (!coro->on_destroy) |
2065 | if (!coro->on_destroy) |
2062 | coro->on_destroy = newAV (); |
2066 | coro->on_destroy = newAV (); |
2063 | |
2067 | |
2064 | av_push (coro->on_destroy, cb); |
2068 | av_push (coro->on_destroy, cb); |
… | |
… | |
2078 | if (!coro->status) |
2082 | if (!coro->status) |
2079 | return 1; |
2083 | return 1; |
2080 | |
2084 | |
2081 | frame->destroy = 0; |
2085 | frame->destroy = 0; |
2082 | |
2086 | |
2083 | coro_push_av (coro->status, GIMME_V); |
2087 | coro_push_av (aTHX_ coro->status, GIMME_V); |
2084 | |
2088 | |
2085 | SvREFCNT_dec ((SV *)coro->hv); |
2089 | SvREFCNT_dec ((SV *)coro->hv); |
2086 | |
2090 | |
2087 | return 0; |
2091 | return 0; |
2088 | } |
2092 | } |
… | |
… | |
2202 | coro = SvSTATE (arg [0]); |
2206 | coro = SvSTATE (arg [0]); |
2203 | coro_hv = coro->hv; |
2207 | coro_hv = coro->hv; |
2204 | |
2208 | |
2205 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2209 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2206 | |
2210 | |
2207 | if (expect_false (coro->flags & CF_NOCANCEL)) |
2211 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2208 | { |
2212 | { |
2209 | /* coro currently busy cancelling something, so just notify it */ |
2213 | /* coro currently busy cancelling something, so just notify it */ |
2210 | coro->slf_frame.data = (void *)coro; |
2214 | coro->slf_frame.data = (void *)coro; |
2211 | |
2215 | |
2212 | frame->prepare = prepare_nop; |
2216 | frame->prepare = prepare_nop; |
… | |
… | |
2320 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2324 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2321 | { |
2325 | { |
2322 | HV *hv = (HV *)SvRV (coro_current); |
2326 | HV *hv = (HV *)SvRV (coro_current); |
2323 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2327 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2324 | |
2328 | |
2325 | if (expect_true (coro->saved_deffh)) |
2329 | if (ecb_expect_true (coro->saved_deffh)) |
2326 | { |
2330 | { |
2327 | /* subsequent iteration */ |
2331 | /* subsequent iteration */ |
2328 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2332 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2329 | coro->saved_deffh = 0; |
2333 | coro->saved_deffh = 0; |
2330 | |
2334 | |
… | |
… | |
2596 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2600 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2597 | |
2601 | |
2598 | /* set up the slf frame, unless it has already been set-up */ |
2602 | /* set up the slf frame, unless it has already been set-up */ |
2599 | /* the latter happens when a new coro has been started */ |
2603 | /* the latter happens when a new coro has been started */ |
2600 | /* or when a new cctx was attached to an existing coroutine */ |
2604 | /* or when a new cctx was attached to an existing coroutine */ |
2601 | if (expect_true (!slf_frame.prepare)) |
2605 | if (ecb_expect_true (!slf_frame.prepare)) |
2602 | { |
2606 | { |
2603 | /* first iteration */ |
2607 | /* first iteration */ |
2604 | dSP; |
2608 | dSP; |
2605 | SV **arg = PL_stack_base + TOPMARK + 1; |
2609 | SV **arg = PL_stack_base + TOPMARK + 1; |
2606 | int items = SP - arg; /* args without function object */ |
2610 | int items = SP - arg; /* args without function object */ |
… | |
… | |
2647 | while (slf_frame.check (aTHX_ &slf_frame)); |
2651 | while (slf_frame.check (aTHX_ &slf_frame)); |
2648 | |
2652 | |
2649 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2653 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2650 | |
2654 | |
2651 | /* exception handling */ |
2655 | /* exception handling */ |
2652 | if (expect_false (CORO_THROW)) |
2656 | if (ecb_expect_false (CORO_THROW)) |
2653 | { |
2657 | { |
2654 | SV *exception = sv_2mortal (CORO_THROW); |
2658 | SV *exception = sv_2mortal (CORO_THROW); |
2655 | |
2659 | |
2656 | CORO_THROW = 0; |
2660 | CORO_THROW = 0; |
2657 | sv_setsv (ERRSV, exception); |
2661 | sv_setsv (ERRSV, exception); |
… | |
… | |
2659 | } |
2663 | } |
2660 | |
2664 | |
2661 | /* return value handling - mostly like entersub */ |
2665 | /* return value handling - mostly like entersub */ |
2662 | /* make sure we put something on the stack in scalar context */ |
2666 | /* make sure we put something on the stack in scalar context */ |
2663 | if (GIMME_V == G_SCALAR |
2667 | if (GIMME_V == G_SCALAR |
2664 | && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2668 | && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2665 | { |
2669 | { |
2666 | dSP; |
2670 | dSP; |
2667 | SV **bot = PL_stack_base + checkmark; |
2671 | SV **bot = PL_stack_base + checkmark; |
2668 | |
2672 | |
2669 | if (sp == bot) /* too few, push undef */ |
2673 | if (sp == bot) /* too few, push undef */ |
… | |
… | |
2783 | { |
2787 | { |
2784 | PerlIOBuf base; |
2788 | PerlIOBuf base; |
2785 | NV next, every; |
2789 | NV next, every; |
2786 | } PerlIOCede; |
2790 | } PerlIOCede; |
2787 | |
2791 | |
2788 | static IV |
2792 | static IV ecb_cold |
2789 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2793 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2790 | { |
2794 | { |
2791 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2795 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2792 | |
2796 | |
2793 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2797 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2794 | self->next = nvtime () + self->every; |
2798 | self->next = nvtime () + self->every; |
2795 | |
2799 | |
2796 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2800 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2797 | } |
2801 | } |
2798 | |
2802 | |
2799 | static SV * |
2803 | static SV * ecb_cold |
2800 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2804 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2801 | { |
2805 | { |
2802 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2806 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2803 | |
2807 | |
2804 | return newSVnv (self->every); |
2808 | return newSVnv (self->every); |
… | |
… | |
2949 | { |
2953 | { |
2950 | int i; |
2954 | int i; |
2951 | /* if we were woken up but can't down, we look through the whole */ |
2955 | /* if we were woken up but can't down, we look through the whole */ |
2952 | /* waiters list and only add us if we aren't in there already */ |
2956 | /* waiters list and only add us if we aren't in there already */ |
2953 | /* this avoids some degenerate memory usage cases */ |
2957 | /* this avoids some degenerate memory usage cases */ |
2954 | for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug |
2958 | for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */ |
2955 | if (AvARRAY (av)[i] == coro_hv) |
2959 | if (AvARRAY (av)[i] == coro_hv) |
2956 | return 1; |
2960 | return 1; |
2957 | |
2961 | |
2958 | av_push (av, SvREFCNT_inc (coro_hv)); |
2962 | av_push (av, SvREFCNT_inc (coro_hv)); |
2959 | return 1; |
2963 | return 1; |
… | |
… | |
3216 | dSP; |
3220 | dSP; |
3217 | |
3221 | |
3218 | static SV *prio_cv; |
3222 | static SV *prio_cv; |
3219 | static SV *prio_sv; |
3223 | static SV *prio_sv; |
3220 | |
3224 | |
3221 | if (expect_false (!prio_cv)) |
3225 | if (ecb_expect_false (!prio_cv)) |
3222 | { |
3226 | { |
3223 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3227 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3224 | prio_sv = newSViv (0); |
3228 | prio_sv = newSViv (0); |
3225 | } |
3229 | } |
3226 | |
3230 | |
… | |
… | |
3332 | } |
3336 | } |
3333 | |
3337 | |
3334 | return coro_sv; |
3338 | return coro_sv; |
3335 | } |
3339 | } |
3336 | |
3340 | |
|
|
3341 | #ifndef __cplusplus |
|
|
3342 | ecb_cold XS(boot_Coro__State); |
|
|
3343 | #endif |
|
|
3344 | |
|
|
3345 | #if CORO_JIT |
|
|
3346 | |
|
|
3347 | static void ecb_noinline ecb_cold |
|
|
3348 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
|
|
3349 | { |
|
|
3350 | dSP; |
|
|
3351 | AV *av = newAV (); |
|
|
3352 | |
|
|
3353 | av_store (av, 3, newSVuv (d)); |
|
|
3354 | av_store (av, 2, newSVuv (c)); |
|
|
3355 | av_store (av, 1, newSVuv (b)); |
|
|
3356 | av_store (av, 0, newSVuv (a)); |
|
|
3357 | |
|
|
3358 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
3359 | |
|
|
3360 | PUTBACK; |
|
|
3361 | } |
|
|
3362 | |
|
|
3363 | static void ecb_noinline ecb_cold |
|
|
3364 | jit_init (pTHX) |
|
|
3365 | { |
|
|
3366 | dSP; |
|
|
3367 | SV *load, *save; |
|
|
3368 | char *map_base; |
|
|
3369 | char *load_ptr, *save_ptr; |
|
|
3370 | STRLEN load_len, save_len, map_len; |
|
|
3371 | int count; |
|
|
3372 | |
|
|
3373 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3374 | |
|
|
3375 | PUSHMARK (SP); |
|
|
3376 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
|
|
3377 | # include "state.h" |
|
|
3378 | #undef VARx |
|
|
3379 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
|
|
3380 | SPAGAIN; |
|
|
3381 | |
|
|
3382 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
|
|
3383 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
|
|
3384 | |
|
|
3385 | map_len = load_len + save_len + 16; |
|
|
3386 | |
|
|
3387 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
|
|
3388 | |
|
|
3389 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
|
|
3390 | |
|
|
3391 | load_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3392 | memcpy (map_base, load_ptr, load_len); |
|
|
3393 | |
|
|
3394 | map_base += (load_len + 15) & ~15; |
|
|
3395 | |
|
|
3396 | save_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3397 | memcpy (map_base, save_ptr, save_len); |
|
|
3398 | |
|
|
3399 | /* we are good citizens and try to make the page read-only, so the evil evil */ |
|
|
3400 | /* hackers might have it a bit more difficult */ |
|
|
3401 | mprotect (map_base, map_len, PROT_READ | PROT_EXEC); |
|
|
3402 | |
|
|
3403 | PUTBACK; |
|
|
3404 | eval_pv ("undef &Coro::State::_jit", 1); |
|
|
3405 | } |
|
|
3406 | |
|
|
3407 | #endif |
|
|
3408 | |
3337 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3409 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3338 | |
3410 | |
3339 | PROTOTYPES: DISABLE |
3411 | PROTOTYPES: DISABLE |
3340 | |
3412 | |
3341 | BOOT: |
3413 | BOOT: |
… | |
… | |
3345 | coro_thx = PERL_GET_CONTEXT; |
3417 | coro_thx = PERL_GET_CONTEXT; |
3346 | # endif |
3418 | # endif |
3347 | #endif |
3419 | #endif |
3348 | BOOT_PAGESIZE; |
3420 | BOOT_PAGESIZE; |
3349 | |
3421 | |
|
|
3422 | /* perl defines these to check for existance first, but why it doesn't */ |
|
|
3423 | /* just create them one at init time is not clear to me, except for */ |
|
|
3424 | /* programs trying to delete them, but... */ |
|
|
3425 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
|
|
3426 | DEFSV; |
|
|
3427 | ERRSV; |
|
|
3428 | |
3350 | cctx_current = cctx_new_empty (); |
3429 | cctx_current = cctx_new_empty (); |
3351 | |
3430 | |
3352 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3431 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3353 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3432 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3354 | |
3433 | |
… | |
… | |
3396 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3475 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3397 | |
3476 | |
3398 | time_init (aTHX); |
3477 | time_init (aTHX); |
3399 | |
3478 | |
3400 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3479 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
|
|
3480 | #if CORO_JIT |
|
|
3481 | PUTBACK; |
|
|
3482 | jit_init (aTHX); |
|
|
3483 | SPAGAIN; |
|
|
3484 | #endif |
3401 | } |
3485 | } |
3402 | |
3486 | |
3403 | SV * |
3487 | SV * |
3404 | new (SV *klass, ...) |
3488 | new (SV *klass, ...) |
3405 | ALIAS: |
3489 | ALIAS: |
… | |
… | |
3653 | times (Coro::State self) |
3737 | times (Coro::State self) |
3654 | PPCODE: |
3738 | PPCODE: |
3655 | { |
3739 | { |
3656 | struct coro *current = SvSTATE (coro_current); |
3740 | struct coro *current = SvSTATE (coro_current); |
3657 | |
3741 | |
3658 | if (expect_false (current == self)) |
3742 | if (ecb_expect_false (current == self)) |
3659 | { |
3743 | { |
3660 | coro_times_update (); |
3744 | coro_times_update (); |
3661 | coro_times_add (SvSTATE (coro_current)); |
3745 | coro_times_add (SvSTATE (coro_current)); |
3662 | } |
3746 | } |
3663 | |
3747 | |
3664 | EXTEND (SP, 2); |
3748 | EXTEND (SP, 2); |
3665 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3749 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3666 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3750 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3667 | |
3751 | |
3668 | if (expect_false (current == self)) |
3752 | if (ecb_expect_false (current == self)) |
3669 | coro_times_sub (SvSTATE (coro_current)); |
3753 | coro_times_sub (SvSTATE (coro_current)); |
3670 | } |
3754 | } |
3671 | |
3755 | |
3672 | void |
3756 | void |
3673 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3757 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
… | |
… | |
4055 | } |
4139 | } |
4056 | |
4140 | |
4057 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4141 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4058 | |
4142 | |
4059 | void |
4143 | void |
4060 | _may_delete (SV *sem, int count, int extra_refs) |
4144 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4061 | PPCODE: |
4145 | PPCODE: |
4062 | { |
4146 | { |
4063 | AV *av = (AV *)SvRV (sem); |
4147 | AV *av = (AV *)SvRV (sem); |
4064 | |
4148 | |
4065 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4149 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |