… | |
… | |
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 |
|
|
19 | #include "ecb.h" |
17 | |
20 | |
18 | #include <stddef.h> |
21 | #include <stddef.h> |
19 | #include <stdio.h> |
22 | #include <stdio.h> |
20 | #include <errno.h> |
23 | #include <errno.h> |
21 | #include <assert.h> |
24 | #include <assert.h> |
… | |
… | |
32 | # define setjmp _setjmp /* deep magic */ |
35 | # define setjmp _setjmp /* deep magic */ |
33 | #else |
36 | #else |
34 | # include <inttypes.h> /* most portable stdint.h */ |
37 | # include <inttypes.h> /* most portable stdint.h */ |
35 | #endif |
38 | #endif |
36 | |
39 | |
37 | #ifdef HAVE_MMAP |
40 | #if HAVE_MMAP |
38 | # include <unistd.h> |
41 | # include <unistd.h> |
39 | # include <sys/mman.h> |
42 | # include <sys/mman.h> |
40 | # ifndef MAP_ANONYMOUS |
43 | # ifndef MAP_ANONYMOUS |
41 | # ifdef MAP_ANON |
44 | # ifdef MAP_ANON |
42 | # define MAP_ANONYMOUS MAP_ANON |
45 | # define MAP_ANONYMOUS MAP_ANON |
… | |
… | |
62 | #endif |
65 | #endif |
63 | |
66 | |
64 | /* the maximum number of idle cctx that will be pooled */ |
67 | /* the maximum number of idle cctx that will be pooled */ |
65 | static int cctx_max_idle = 4; |
68 | static int cctx_max_idle = 4; |
66 | |
69 | |
|
|
70 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
|
|
71 | # define HAS_SCOPESTACK_NAME 1 |
|
|
72 | #endif |
|
|
73 | |
67 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
74 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
68 | # undef CORO_STACKGUARD |
75 | # undef CORO_STACKGUARD |
69 | #endif |
76 | #endif |
70 | |
77 | |
71 | #ifndef CORO_STACKGUARD |
78 | #ifndef CORO_STACKGUARD |
… | |
… | |
87 | # define STACKLEVEL ((void *)&stacklevel) |
94 | # define STACKLEVEL ((void *)&stacklevel) |
88 | #endif |
95 | #endif |
89 | |
96 | |
90 | #define IN_DESTRUCT PL_dirty |
97 | #define IN_DESTRUCT PL_dirty |
91 | |
98 | |
92 | #if __GNUC__ >= 3 |
|
|
93 | # define attribute(x) __attribute__(x) |
|
|
94 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
95 | # define INLINE static inline |
|
|
96 | #else |
|
|
97 | # define attribute(x) |
|
|
98 | # define expect(expr,value) (expr) |
|
|
99 | # define INLINE static |
|
|
100 | #endif |
|
|
101 | |
|
|
102 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
103 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
104 | |
|
|
105 | #define NOINLINE attribute ((noinline)) |
|
|
106 | |
|
|
107 | #include "CoroAPI.h" |
99 | #include "CoroAPI.h" |
108 | #define GCoroAPI (&coroapi) /* very sneaky */ |
100 | #define GCoroAPI (&coroapi) /* very sneaky */ |
109 | |
101 | |
110 | #ifdef USE_ITHREADS |
102 | #ifdef USE_ITHREADS |
111 | # if CORO_PTHREAD |
103 | # if CORO_PTHREAD |
… | |
… | |
188 | void *sptr; |
180 | void *sptr; |
189 | size_t ssize; |
181 | size_t ssize; |
190 | |
182 | |
191 | /* cpu state */ |
183 | /* cpu state */ |
192 | 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 |
193 | 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 |
194 | JMPENV *top_env; |
188 | JMPENV *top_env; |
195 | coro_context cctx; |
189 | coro_context cctx; |
196 | |
190 | |
197 | U32 gen; |
191 | U32 gen; |
198 | #if CORO_USE_VALGRIND |
192 | #if CORO_USE_VALGRIND |
… | |
… | |
218 | }; |
212 | }; |
219 | |
213 | |
220 | /* 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 */ |
221 | typedef struct |
215 | typedef struct |
222 | { |
216 | { |
223 | SV *defsv; |
|
|
224 | AV *defav; |
|
|
225 | SV *errsv; |
|
|
226 | SV *irsgv; |
|
|
227 | HV *hinthv; |
|
|
228 | #define VAR(name,type) type name; |
217 | #define VARx(name,expr,type) type name; |
229 | # include "state.h" |
218 | #include "state.h" |
230 | #undef VAR |
|
|
231 | } perl_slots; |
219 | } perl_slots; |
232 | |
220 | |
233 | // how many context stack entries do we need for perl_slots |
221 | /* how many context stack entries do we need for perl_slots */ |
234 | #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)) |
235 | |
223 | |
236 | /* this is a structure representing a perl-level coroutine */ |
224 | /* this is a structure representing a perl-level coroutine */ |
237 | struct coro |
225 | struct coro |
238 | { |
226 | { |
… | |
… | |
304 | 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 */ |
305 | static CV *cv_coro_run; |
293 | static CV *cv_coro_run; |
306 | static struct coro *coro_first; |
294 | static struct coro *coro_first; |
307 | #define coro_nready coroapi.nready |
295 | #define coro_nready coroapi.nready |
308 | |
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 | |
309 | /** Coro::Select ************************************************************/ |
320 | /** Coro::Select ************************************************************/ |
310 | |
321 | |
311 | static OP *(*coro_old_pp_sselect) (pTHX); |
322 | static OP *(*coro_old_pp_sselect) (pTHX); |
312 | static SV *coro_select_select; |
323 | static SV *coro_select_select; |
313 | |
324 | |
… | |
… | |
328 | |
339 | |
329 | /** time stuff **************************************************************/ |
340 | /** time stuff **************************************************************/ |
330 | |
341 | |
331 | #ifdef HAS_GETTIMEOFDAY |
342 | #ifdef HAS_GETTIMEOFDAY |
332 | |
343 | |
333 | static void |
344 | ecb_inline void |
334 | coro_u2time (pTHX_ UV ret[2]) |
345 | coro_u2time (pTHX_ UV ret[2]) |
335 | { |
346 | { |
336 | struct timeval tv; |
347 | struct timeval tv; |
337 | gettimeofday (&tv, 0); |
348 | gettimeofday (&tv, 0); |
338 | |
349 | |
339 | ret [0] = tv.tv_sec; |
350 | ret [0] = tv.tv_sec; |
340 | ret [1] = tv.tv_usec; |
351 | ret [1] = tv.tv_usec; |
341 | } |
352 | } |
342 | |
353 | |
343 | static double |
354 | ecb_inline double |
344 | coro_nvtime () |
355 | coro_nvtime (void) |
345 | { |
356 | { |
346 | struct timeval tv; |
357 | struct timeval tv; |
347 | gettimeofday (&tv, 0); |
358 | gettimeofday (&tv, 0); |
348 | |
359 | |
349 | return tv.tv_sec + tv.tv_usec * 1e-6; |
360 | return tv.tv_sec + tv.tv_usec * 1e-6; |
350 | } |
361 | } |
351 | |
362 | |
352 | static void |
363 | ecb_inline void |
353 | time_init (pTHX) |
364 | time_init (pTHX) |
354 | { |
365 | { |
355 | nvtime = coro_nvtime; |
366 | nvtime = coro_nvtime; |
356 | u2time = coro_u2time; |
367 | u2time = coro_u2time; |
357 | } |
368 | } |
358 | |
369 | |
359 | #else |
370 | #else |
360 | |
371 | |
361 | static void |
372 | ecb_inline void |
362 | time_init (pTHX) |
373 | time_init (pTHX) |
363 | { |
374 | { |
364 | SV **svp; |
375 | SV **svp; |
365 | |
376 | |
366 | require_pv ("Time/HiRes.pm"); |
377 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
378 | |
389 | |
379 | #endif |
390 | #endif |
380 | |
391 | |
381 | /** lowlevel stuff **********************************************************/ |
392 | /** lowlevel stuff **********************************************************/ |
382 | |
393 | |
383 | static SV * |
394 | static SV * ecb_noinline |
384 | coro_get_sv (pTHX_ const char *name, int create) |
395 | coro_get_sv (pTHX_ const char *name, int create) |
385 | { |
396 | { |
386 | #if PERL_VERSION_ATLEAST (5,10,0) |
397 | #if PERL_VERSION_ATLEAST (5,10,0) |
387 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
398 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
388 | get_sv (name, create); |
399 | get_sv (name, create); |
389 | #endif |
400 | #endif |
390 | return get_sv (name, create); |
401 | return get_sv (name, create); |
391 | } |
402 | } |
392 | |
403 | |
393 | static AV * |
404 | static AV * ecb_noinline |
394 | coro_get_av (pTHX_ const char *name, int create) |
405 | coro_get_av (pTHX_ const char *name, int create) |
395 | { |
406 | { |
396 | #if PERL_VERSION_ATLEAST (5,10,0) |
407 | #if PERL_VERSION_ATLEAST (5,10,0) |
397 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
408 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
398 | get_av (name, create); |
409 | get_av (name, create); |
399 | #endif |
410 | #endif |
400 | return get_av (name, create); |
411 | return get_av (name, create); |
401 | } |
412 | } |
402 | |
413 | |
403 | static HV * |
414 | static HV * ecb_noinline |
404 | coro_get_hv (pTHX_ const char *name, int create) |
415 | coro_get_hv (pTHX_ const char *name, int create) |
405 | { |
416 | { |
406 | #if PERL_VERSION_ATLEAST (5,10,0) |
417 | #if PERL_VERSION_ATLEAST (5,10,0) |
407 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
418 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
408 | get_hv (name, create); |
419 | get_hv (name, create); |
409 | #endif |
420 | #endif |
410 | return get_hv (name, create); |
421 | return get_hv (name, create); |
411 | } |
422 | } |
412 | |
423 | |
413 | INLINE void |
424 | ecb_inline void |
414 | coro_times_update () |
425 | coro_times_update (void) |
415 | { |
426 | { |
416 | #ifdef coro_clock_gettime |
427 | #ifdef coro_clock_gettime |
417 | struct timespec ts; |
428 | struct timespec ts; |
418 | |
429 | |
419 | ts.tv_sec = ts.tv_nsec = 0; |
430 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
431 | time_real [0] = tv [0]; |
442 | time_real [0] = tv [0]; |
432 | time_real [1] = tv [1] * 1000; |
443 | time_real [1] = tv [1] * 1000; |
433 | #endif |
444 | #endif |
434 | } |
445 | } |
435 | |
446 | |
436 | INLINE void |
447 | ecb_inline void |
437 | coro_times_add (struct coro *c) |
448 | coro_times_add (struct coro *c) |
438 | { |
449 | { |
439 | c->t_real [1] += time_real [1]; |
450 | c->t_real [1] += time_real [1]; |
440 | 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]; } |
441 | c->t_real [0] += time_real [0]; |
452 | c->t_real [0] += time_real [0]; |
… | |
… | |
443 | c->t_cpu [1] += time_cpu [1]; |
454 | c->t_cpu [1] += time_cpu [1]; |
444 | 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]; } |
445 | c->t_cpu [0] += time_cpu [0]; |
456 | c->t_cpu [0] += time_cpu [0]; |
446 | } |
457 | } |
447 | |
458 | |
448 | INLINE void |
459 | ecb_inline void |
449 | coro_times_sub (struct coro *c) |
460 | coro_times_sub (struct coro *c) |
450 | { |
461 | { |
451 | 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]; } |
452 | c->t_real [1] -= time_real [1]; |
463 | c->t_real [1] -= time_real [1]; |
453 | c->t_real [0] -= time_real [0]; |
464 | c->t_real [0] -= time_real [0]; |
… | |
… | |
461 | /* magic glue */ |
472 | /* magic glue */ |
462 | |
473 | |
463 | #define CORO_MAGIC_type_cv 26 |
474 | #define CORO_MAGIC_type_cv 26 |
464 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
475 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
465 | |
476 | |
466 | #define CORO_MAGIC_NN(sv, type) \ |
477 | #define CORO_MAGIC_NN(sv, type) \ |
467 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
478 | (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \ |
468 | ? SvMAGIC (sv) \ |
479 | ? SvMAGIC (sv) \ |
469 | : mg_find (sv, type)) |
480 | : mg_find (sv, type)) |
470 | |
481 | |
471 | #define CORO_MAGIC(sv, type) \ |
482 | #define CORO_MAGIC(sv, type) \ |
472 | (expect_true (SvMAGIC (sv)) \ |
483 | (ecb_expect_true (SvMAGIC (sv)) \ |
473 | ? CORO_MAGIC_NN (sv, type) \ |
484 | ? CORO_MAGIC_NN (sv, type) \ |
474 | : 0) |
485 | : 0) |
475 | |
486 | |
476 | #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) |
477 | #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) |
478 | |
489 | |
479 | INLINE MAGIC * |
490 | ecb_inline MAGIC * |
480 | SvSTATEhv_p (pTHX_ SV *coro) |
491 | SvSTATEhv_p (pTHX_ SV *coro) |
481 | { |
492 | { |
482 | MAGIC *mg; |
493 | MAGIC *mg; |
483 | |
494 | |
484 | if (expect_true ( |
495 | if (ecb_expect_true ( |
485 | SvTYPE (coro) == SVt_PVHV |
496 | SvTYPE (coro) == SVt_PVHV |
486 | && (mg = CORO_MAGIC_state (coro)) |
497 | && (mg = CORO_MAGIC_state (coro)) |
487 | && mg->mg_virtual == &coro_state_vtbl |
498 | && mg->mg_virtual == &coro_state_vtbl |
488 | )) |
499 | )) |
489 | return mg; |
500 | return mg; |
490 | |
501 | |
491 | return 0; |
502 | return 0; |
492 | } |
503 | } |
493 | |
504 | |
494 | INLINE struct coro * |
505 | ecb_inline struct coro * |
495 | SvSTATE_ (pTHX_ SV *coro) |
506 | SvSTATE_ (pTHX_ SV *coro) |
496 | { |
507 | { |
497 | MAGIC *mg; |
508 | MAGIC *mg; |
498 | |
509 | |
499 | if (SvROK (coro)) |
510 | if (SvROK (coro)) |
… | |
… | |
513 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
524 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
514 | |
525 | |
515 | /*****************************************************************************/ |
526 | /*****************************************************************************/ |
516 | /* padlist management and caching */ |
527 | /* padlist management and caching */ |
517 | |
528 | |
518 | static AV * |
529 | ecb_inline AV * |
519 | coro_derive_padlist (pTHX_ CV *cv) |
530 | coro_derive_padlist (pTHX_ CV *cv) |
520 | { |
531 | { |
521 | AV *padlist = CvPADLIST (cv); |
532 | AV *padlist = CvPADLIST (cv); |
522 | AV *newpadlist, *newpad; |
533 | AV *newpadlist, *newpad; |
523 | |
534 | |
… | |
… | |
535 | av_store (newpadlist, 1, (SV *)newpad); |
546 | av_store (newpadlist, 1, (SV *)newpad); |
536 | |
547 | |
537 | return newpadlist; |
548 | return newpadlist; |
538 | } |
549 | } |
539 | |
550 | |
540 | static void |
551 | ecb_inline void |
541 | free_padlist (pTHX_ AV *padlist) |
552 | free_padlist (pTHX_ AV *padlist) |
542 | { |
553 | { |
543 | /* may be during global destruction */ |
554 | /* may be during global destruction */ |
544 | if (!IN_DESTRUCT) |
555 | if (!IN_DESTRUCT) |
545 | { |
556 | { |
… | |
… | |
588 | 0, 0, 0, 0, |
599 | 0, 0, 0, 0, |
589 | coro_cv_free |
600 | coro_cv_free |
590 | }; |
601 | }; |
591 | |
602 | |
592 | /* the next two functions merely cache the padlists */ |
603 | /* the next two functions merely cache the padlists */ |
593 | static void |
604 | ecb_inline void |
594 | get_padlist (pTHX_ CV *cv) |
605 | get_padlist (pTHX_ CV *cv) |
595 | { |
606 | { |
596 | MAGIC *mg = CORO_MAGIC_cv (cv); |
607 | MAGIC *mg = CORO_MAGIC_cv (cv); |
597 | AV *av; |
608 | AV *av; |
598 | |
609 | |
599 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
610 | if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
600 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
611 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
601 | else |
612 | else |
602 | { |
613 | { |
603 | #if CORO_PREFER_PERL_FUNCTIONS |
614 | #if CORO_PREFER_PERL_FUNCTIONS |
604 | /* this is probably cleaner? but also slower! */ |
615 | /* this is probably cleaner? but also slower! */ |
… | |
… | |
611 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
622 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
612 | #endif |
623 | #endif |
613 | } |
624 | } |
614 | } |
625 | } |
615 | |
626 | |
616 | static void |
627 | ecb_inline void |
617 | put_padlist (pTHX_ CV *cv) |
628 | put_padlist (pTHX_ CV *cv) |
618 | { |
629 | { |
619 | MAGIC *mg = CORO_MAGIC_cv (cv); |
630 | MAGIC *mg = CORO_MAGIC_cv (cv); |
620 | AV *av; |
631 | AV *av; |
621 | |
632 | |
622 | if (expect_false (!mg)) |
633 | if (ecb_expect_false (!mg)) |
623 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
634 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
624 | |
635 | |
625 | av = (AV *)mg->mg_obj; |
636 | av = (AV *)mg->mg_obj; |
626 | |
637 | |
627 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
638 | if (ecb_expect_false (AvFILLp (av) >= AvMAX (av))) |
628 | av_extend (av, AvFILLp (av) + 1); |
639 | av_extend (av, AvFILLp (av) + 1); |
629 | |
640 | |
630 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
641 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
631 | } |
642 | } |
632 | |
643 | |
633 | /** load & save, init *******************************************************/ |
644 | /** load & save, init *******************************************************/ |
634 | |
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 | |
635 | /* swap sv heads, at least logically */ |
684 | /* swap sv heads, at least logically */ |
636 | static void |
685 | static void |
637 | swap_svs (pTHX_ Coro__State c) |
686 | swap_svs (pTHX_ Coro__State c) |
638 | { |
687 | { |
639 | int i; |
688 | int i; |
640 | |
689 | |
641 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
690 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
642 | { |
691 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
643 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
644 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
645 | |
|
|
646 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
647 | SV tmp; |
|
|
648 | |
|
|
649 | /* swap sv_any */ |
|
|
650 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
651 | |
|
|
652 | /* swap sv_flags */ |
|
|
653 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
654 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
655 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
656 | |
|
|
657 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
658 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
659 | * much faster, so no quarrels here. alternatively, we could |
|
|
660 | * sv_upgrade to avoid this. |
|
|
661 | */ |
|
|
662 | { |
|
|
663 | /* swap sv_u */ |
|
|
664 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
665 | |
|
|
666 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
667 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
668 | */ |
|
|
669 | #define svany_in_head(type) \ |
|
|
670 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
671 | |
|
|
672 | if (svany_in_head (SvTYPE (a))) |
|
|
673 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
674 | |
|
|
675 | if (svany_in_head (SvTYPE (b))) |
|
|
676 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
677 | } |
|
|
678 | #endif |
|
|
679 | } |
|
|
680 | } |
692 | } |
681 | |
693 | |
682 | #define SWAP_SVS(coro) \ |
694 | #define SWAP_SVS(coro) \ |
683 | if (expect_false ((coro)->swap_sv)) \ |
695 | if (ecb_expect_false ((coro)->swap_sv)) \ |
684 | swap_svs (aTHX_ (coro)) |
696 | swap_svs (aTHX_ (coro)) |
685 | |
697 | |
686 | static void |
698 | static void |
687 | on_enterleave_call (pTHX_ SV *cb); |
699 | on_enterleave_call (pTHX_ SV *cb); |
688 | |
700 | |
… | |
… | |
692 | perl_slots *slot = c->slot; |
704 | perl_slots *slot = c->slot; |
693 | c->slot = 0; |
705 | c->slot = 0; |
694 | |
706 | |
695 | PL_mainstack = c->mainstack; |
707 | PL_mainstack = c->mainstack; |
696 | |
708 | |
697 | GvSV (PL_defgv) = slot->defsv; |
709 | #if CORO_JIT |
698 | GvAV (PL_defgv) = slot->defav; |
710 | load_perl_slots (slot); |
699 | GvSV (PL_errgv) = slot->errsv; |
711 | #else |
700 | GvSV (irsgv) = slot->irsgv; |
|
|
701 | GvHV (PL_hintgv) = slot->hinthv; |
|
|
702 | |
|
|
703 | #define VAR(name,type) PL_ ## name = slot->name; |
712 | #define VARx(name,expr,type) expr = slot->name; |
704 | # include "state.h" |
713 | #include "state.h" |
705 | #undef VAR |
714 | #endif |
706 | |
715 | |
707 | { |
716 | { |
708 | dSP; |
717 | dSP; |
709 | |
718 | |
710 | CV *cv; |
719 | CV *cv; |
711 | |
720 | |
712 | /* now do the ugly restore mess */ |
721 | /* now do the ugly restore mess */ |
713 | while (expect_true (cv = (CV *)POPs)) |
722 | while (ecb_expect_true (cv = (CV *)POPs)) |
714 | { |
723 | { |
715 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
724 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
716 | CvDEPTH (cv) = PTR2IV (POPs); |
725 | CvDEPTH (cv) = PTR2IV (POPs); |
717 | CvPADLIST (cv) = (AV *)POPs; |
726 | CvPADLIST (cv) = (AV *)POPs; |
718 | } |
727 | } |
… | |
… | |
721 | } |
730 | } |
722 | |
731 | |
723 | slf_frame = c->slf_frame; |
732 | slf_frame = c->slf_frame; |
724 | CORO_THROW = c->except; |
733 | CORO_THROW = c->except; |
725 | |
734 | |
726 | if (expect_false (enable_times)) |
735 | if (ecb_expect_false (enable_times)) |
727 | { |
736 | { |
728 | if (expect_false (!times_valid)) |
737 | if (ecb_expect_false (!times_valid)) |
729 | coro_times_update (); |
738 | coro_times_update (); |
730 | |
739 | |
731 | coro_times_sub (c); |
740 | coro_times_sub (c); |
732 | } |
741 | } |
733 | |
742 | |
734 | if (expect_false (c->on_enter)) |
743 | if (ecb_expect_false (c->on_enter)) |
735 | { |
744 | { |
736 | int i; |
745 | int i; |
737 | |
746 | |
738 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
747 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
739 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
748 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
… | |
… | |
745 | static void |
754 | static void |
746 | save_perl (pTHX_ Coro__State c) |
755 | save_perl (pTHX_ Coro__State c) |
747 | { |
756 | { |
748 | SWAP_SVS (c); |
757 | SWAP_SVS (c); |
749 | |
758 | |
750 | if (expect_false (c->on_leave)) |
759 | if (ecb_expect_false (c->on_leave)) |
751 | { |
760 | { |
752 | int i; |
761 | int i; |
753 | |
762 | |
754 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
763 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
755 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
764 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
756 | } |
765 | } |
757 | |
766 | |
758 | times_valid = 0; |
767 | times_valid = 0; |
759 | |
768 | |
760 | if (expect_false (enable_times)) |
769 | if (ecb_expect_false (enable_times)) |
761 | { |
770 | { |
762 | coro_times_update (); times_valid = 1; |
771 | coro_times_update (); times_valid = 1; |
763 | coro_times_add (c); |
772 | coro_times_add (c); |
764 | } |
773 | } |
765 | |
774 | |
… | |
… | |
779 | |
788 | |
780 | XPUSHs (Nullsv); |
789 | XPUSHs (Nullsv); |
781 | /* this loop was inspired by pp_caller */ |
790 | /* this loop was inspired by pp_caller */ |
782 | for (;;) |
791 | for (;;) |
783 | { |
792 | { |
784 | while (expect_true (cxix >= 0)) |
793 | while (ecb_expect_true (cxix >= 0)) |
785 | { |
794 | { |
786 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
795 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
787 | |
796 | |
788 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
797 | if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT)) |
789 | { |
798 | { |
790 | CV *cv = cx->blk_sub.cv; |
799 | CV *cv = cx->blk_sub.cv; |
791 | |
800 | |
792 | if (expect_true (CvDEPTH (cv))) |
801 | if (ecb_expect_true (CvDEPTH (cv))) |
793 | { |
802 | { |
794 | EXTEND (SP, 3); |
803 | EXTEND (SP, 3); |
795 | PUSHs ((SV *)CvPADLIST (cv)); |
804 | PUSHs ((SV *)CvPADLIST (cv)); |
796 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
805 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
797 | PUSHs ((SV *)cv); |
806 | PUSHs ((SV *)cv); |
… | |
… | |
800 | get_padlist (aTHX_ cv); |
809 | get_padlist (aTHX_ cv); |
801 | } |
810 | } |
802 | } |
811 | } |
803 | } |
812 | } |
804 | |
813 | |
805 | if (expect_true (top_si->si_type == PERLSI_MAIN)) |
814 | if (ecb_expect_true (top_si->si_type == PERLSI_MAIN)) |
806 | break; |
815 | break; |
807 | |
816 | |
808 | top_si = top_si->si_prev; |
817 | top_si = top_si->si_prev; |
809 | ccstk = top_si->si_cxstack; |
818 | ccstk = top_si->si_cxstack; |
810 | cxix = top_si->si_cxix; |
819 | cxix = top_si->si_cxix; |
… | |
… | |
812 | |
821 | |
813 | PUTBACK; |
822 | PUTBACK; |
814 | } |
823 | } |
815 | |
824 | |
816 | /* allocate some space on the context stack for our purposes */ |
825 | /* allocate some space on the context stack for our purposes */ |
817 | if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
826 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
818 | { |
827 | { |
819 | unsigned int i; |
828 | unsigned int i; |
820 | |
829 | |
821 | for (i = 0; i < SLOT_COUNT; ++i) |
830 | for (i = 0; i < SLOT_COUNT; ++i) |
822 | CXINC; |
831 | CXINC; |
… | |
… | |
827 | c->mainstack = PL_mainstack; |
836 | c->mainstack = PL_mainstack; |
828 | |
837 | |
829 | { |
838 | { |
830 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
839 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
831 | |
840 | |
832 | slot->defav = GvAV (PL_defgv); |
841 | #if CORO_JIT |
833 | slot->defsv = DEFSV; |
842 | save_perl_slots (slot); |
834 | slot->errsv = ERRSV; |
843 | #else |
835 | slot->irsgv = GvSV (irsgv); |
|
|
836 | slot->hinthv = GvHV (PL_hintgv); |
|
|
837 | |
|
|
838 | #define VAR(name,type) slot->name = PL_ ## name; |
844 | #define VARx(name,expr,type) slot->name = expr; |
839 | # include "state.h" |
845 | #include "state.h" |
840 | #undef VAR |
846 | #endif |
841 | } |
847 | } |
842 | } |
848 | } |
843 | |
849 | |
844 | /* |
850 | /* |
845 | * allocate various perl stacks. This is almost an exact copy |
851 | * allocate various perl stacks. This is almost an exact copy |
… | |
… | |
876 | #endif |
882 | #endif |
877 | |
883 | |
878 | New(54,PL_scopestack,8,I32); |
884 | New(54,PL_scopestack,8,I32); |
879 | PL_scopestack_ix = 0; |
885 | PL_scopestack_ix = 0; |
880 | 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 |
881 | |
890 | |
882 | New(54,PL_savestack,24,ANY); |
891 | New(54,PL_savestack,24,ANY); |
883 | PL_savestack_ix = 0; |
892 | PL_savestack_ix = 0; |
884 | PL_savestack_max = 24; |
893 | PL_savestack_max = 24; |
885 | |
894 | |
… | |
… | |
913 | } |
922 | } |
914 | |
923 | |
915 | Safefree (PL_tmps_stack); |
924 | Safefree (PL_tmps_stack); |
916 | Safefree (PL_markstack); |
925 | Safefree (PL_markstack); |
917 | Safefree (PL_scopestack); |
926 | Safefree (PL_scopestack); |
|
|
927 | #if HAS_SCOPESTACK_NAME |
|
|
928 | Safefree (PL_scopestack_name); |
|
|
929 | #endif |
918 | Safefree (PL_savestack); |
930 | Safefree (PL_savestack); |
919 | #if !PERL_VERSION_ATLEAST (5,10,0) |
931 | #if !PERL_VERSION_ATLEAST (5,10,0) |
920 | Safefree (PL_retstack); |
932 | Safefree (PL_retstack); |
921 | #endif |
933 | #endif |
922 | } |
934 | } |
… | |
… | |
971 | * This overrides the default magic get method of %SIG elements. |
983 | * This overrides the default magic get method of %SIG elements. |
972 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
984 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
973 | * and instead of trying to save and restore the hash elements (extremely slow), |
985 | * and instead of trying to save and restore the hash elements (extremely slow), |
974 | * we just provide our own readback here. |
986 | * we just provide our own readback here. |
975 | */ |
987 | */ |
976 | static int |
988 | static int ecb_cold |
977 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
989 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
978 | { |
990 | { |
979 | const char *s = MgPV_nolen_const (mg); |
991 | const char *s = MgPV_nolen_const (mg); |
980 | |
992 | |
981 | if (*s == '_') |
993 | if (*s == '_') |
… | |
… | |
985 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
997 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
986 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
998 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
987 | |
999 | |
988 | if (svp) |
1000 | if (svp) |
989 | { |
1001 | { |
990 | 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); |
991 | return 0; |
1012 | return 0; |
992 | } |
1013 | } |
993 | } |
1014 | } |
994 | |
1015 | |
995 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1016 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
996 | } |
1017 | } |
997 | |
1018 | |
998 | static int |
1019 | static int ecb_cold |
999 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1020 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1000 | { |
1021 | { |
1001 | const char *s = MgPV_nolen_const (mg); |
1022 | const char *s = MgPV_nolen_const (mg); |
1002 | |
1023 | |
1003 | if (*s == '_') |
1024 | if (*s == '_') |
… | |
… | |
1017 | } |
1038 | } |
1018 | |
1039 | |
1019 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1040 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1020 | } |
1041 | } |
1021 | |
1042 | |
1022 | static int |
1043 | static int ecb_cold |
1023 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1044 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1024 | { |
1045 | { |
1025 | const char *s = MgPV_nolen_const (mg); |
1046 | const char *s = MgPV_nolen_const (mg); |
1026 | |
1047 | |
1027 | if (*s == '_') |
1048 | if (*s == '_') |
… | |
… | |
1062 | return 1; |
1083 | return 1; |
1063 | } |
1084 | } |
1064 | |
1085 | |
1065 | static UNOP init_perl_op; |
1086 | static UNOP init_perl_op; |
1066 | |
1087 | |
1067 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1088 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1068 | init_perl (pTHX_ struct coro *coro) |
1089 | init_perl (pTHX_ struct coro *coro) |
1069 | { |
1090 | { |
1070 | /* |
1091 | /* |
1071 | * emulate part of the perl startup here. |
1092 | * emulate part of the perl startup here. |
1072 | */ |
1093 | */ |
… | |
… | |
1134 | /* copy throw, in case it was set before init_perl */ |
1155 | /* copy throw, in case it was set before init_perl */ |
1135 | CORO_THROW = coro->except; |
1156 | CORO_THROW = coro->except; |
1136 | |
1157 | |
1137 | SWAP_SVS (coro); |
1158 | SWAP_SVS (coro); |
1138 | |
1159 | |
1139 | if (expect_false (enable_times)) |
1160 | if (ecb_expect_false (enable_times)) |
1140 | { |
1161 | { |
1141 | coro_times_update (); |
1162 | coro_times_update (); |
1142 | coro_times_sub (coro); |
1163 | coro_times_sub (coro); |
1143 | } |
1164 | } |
1144 | } |
1165 | } |
… | |
… | |
1187 | /* restore swapped sv's */ |
1208 | /* restore swapped sv's */ |
1188 | SWAP_SVS (coro); |
1209 | SWAP_SVS (coro); |
1189 | |
1210 | |
1190 | coro_destruct_stacks (aTHX); |
1211 | coro_destruct_stacks (aTHX); |
1191 | |
1212 | |
1192 | // now save some sv's to be free'd later |
1213 | /* now save some sv's to be free'd later */ |
1193 | svf [0] = GvSV (PL_defgv); |
1214 | svf [0] = GvSV (PL_defgv); |
1194 | svf [1] = (SV *)GvAV (PL_defgv); |
1215 | svf [1] = (SV *)GvAV (PL_defgv); |
1195 | svf [2] = GvSV (PL_errgv); |
1216 | svf [2] = GvSV (PL_errgv); |
1196 | svf [3] = (SV *)PL_defoutgv; |
1217 | svf [3] = (SV *)PL_defoutgv; |
1197 | svf [4] = PL_rs; |
1218 | svf [4] = PL_rs; |
… | |
… | |
1217 | SvREFCNT_dec (coro->invoke_cb); |
1238 | SvREFCNT_dec (coro->invoke_cb); |
1218 | SvREFCNT_dec (coro->invoke_av); |
1239 | SvREFCNT_dec (coro->invoke_av); |
1219 | } |
1240 | } |
1220 | } |
1241 | } |
1221 | |
1242 | |
1222 | INLINE void |
1243 | ecb_inline void |
1223 | free_coro_mortal (pTHX) |
1244 | free_coro_mortal (pTHX) |
1224 | { |
1245 | { |
1225 | if (expect_true (coro_mortal)) |
1246 | if (ecb_expect_true (coro_mortal)) |
1226 | { |
1247 | { |
1227 | SvREFCNT_dec ((SV *)coro_mortal); |
1248 | SvREFCNT_dec ((SV *)coro_mortal); |
1228 | coro_mortal = 0; |
1249 | coro_mortal = 0; |
1229 | } |
1250 | } |
1230 | } |
1251 | } |
… | |
… | |
1365 | |
1386 | |
1366 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1387 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1367 | } |
1388 | } |
1368 | |
1389 | |
1369 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1390 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1370 | static void NOINLINE |
1391 | static void ecb_noinline |
1371 | cctx_prepare (pTHX) |
1392 | cctx_prepare (pTHX) |
1372 | { |
1393 | { |
1373 | PL_top_env = &PL_start_env; |
1394 | PL_top_env = &PL_start_env; |
1374 | |
1395 | |
1375 | if (cctx_current->flags & CC_TRACE) |
1396 | if (cctx_current->flags & CC_TRACE) |
… | |
… | |
1386 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1407 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1387 | slf_frame.check = slf_check_set_stacklevel; |
1408 | slf_frame.check = slf_check_set_stacklevel; |
1388 | } |
1409 | } |
1389 | |
1410 | |
1390 | /* 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 */ |
1391 | INLINE void |
1412 | ecb_inline void |
1392 | transfer_tail (pTHX) |
1413 | transfer_tail (pTHX) |
1393 | { |
1414 | { |
1394 | 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); |
1395 | } |
1428 | } |
1396 | |
1429 | |
1397 | /* |
1430 | /* |
1398 | * this is a _very_ stripped down perl interpreter ;) |
1431 | * this is a _very_ stripped down perl interpreter ;) |
1399 | */ |
1432 | */ |
… | |
… | |
1428 | */ |
1461 | */ |
1429 | |
1462 | |
1430 | /* |
1463 | /* |
1431 | * 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 |
1432 | * 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) |
1433 | * 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 |
1434 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1467 | * doing in that case. YMMV. |
1435 | * coroutine as Coro has no such concept. |
|
|
1436 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1437 | * that backend are too broken to do it in a standards-compliant way. |
|
|
1438 | */ |
1468 | */ |
1439 | PL_top_env = main_top_env; |
1469 | perlish_exit (aTHX); |
1440 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1441 | } |
1470 | } |
1442 | } |
1471 | } |
1443 | |
1472 | |
1444 | static coro_cctx * |
1473 | static coro_cctx * |
1445 | cctx_new () |
1474 | cctx_new (void) |
1446 | { |
1475 | { |
1447 | coro_cctx *cctx; |
1476 | coro_cctx *cctx; |
1448 | |
1477 | |
1449 | ++cctx_count; |
1478 | ++cctx_count; |
1450 | New (0, cctx, 1, coro_cctx); |
1479 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1456 | return cctx; |
1485 | return cctx; |
1457 | } |
1486 | } |
1458 | |
1487 | |
1459 | /* create a new cctx only suitable as source */ |
1488 | /* create a new cctx only suitable as source */ |
1460 | static coro_cctx * |
1489 | static coro_cctx * |
1461 | cctx_new_empty () |
1490 | cctx_new_empty (void) |
1462 | { |
1491 | { |
1463 | coro_cctx *cctx = cctx_new (); |
1492 | coro_cctx *cctx = cctx_new (); |
1464 | |
1493 | |
1465 | cctx->sptr = 0; |
1494 | cctx->sptr = 0; |
1466 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1495 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
… | |
… | |
1468 | return cctx; |
1497 | return cctx; |
1469 | } |
1498 | } |
1470 | |
1499 | |
1471 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1500 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1472 | static coro_cctx * |
1501 | static coro_cctx * |
1473 | cctx_new_run () |
1502 | cctx_new_run (void) |
1474 | { |
1503 | { |
1475 | coro_cctx *cctx = cctx_new (); |
1504 | coro_cctx *cctx = cctx_new (); |
1476 | void *stack_start; |
1505 | void *stack_start; |
1477 | size_t stack_size; |
1506 | size_t stack_size; |
1478 | |
1507 | |
1479 | #if HAVE_MMAP |
1508 | #if HAVE_MMAP |
1480 | 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; |
1481 | /* mmap supposedly does allocate-on-write for us */ |
1510 | /* mmap supposedly does allocate-on-write for us */ |
1482 | 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); |
1483 | |
1512 | |
1484 | if (cctx->sptr != (void *)-1) |
1513 | if (cctx->sptr != (void *)-1) |
1485 | { |
1514 | { |
1486 | #if CORO_STACKGUARD |
1515 | #if CORO_STACKGUARD |
1487 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1516 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1548 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1577 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1549 | |
1578 | |
1550 | static coro_cctx * |
1579 | static coro_cctx * |
1551 | cctx_get (pTHX) |
1580 | cctx_get (pTHX) |
1552 | { |
1581 | { |
1553 | while (expect_true (cctx_first)) |
1582 | while (ecb_expect_true (cctx_first)) |
1554 | { |
1583 | { |
1555 | coro_cctx *cctx = cctx_first; |
1584 | coro_cctx *cctx = cctx_first; |
1556 | cctx_first = cctx->next; |
1585 | cctx_first = cctx->next; |
1557 | --cctx_idle; |
1586 | --cctx_idle; |
1558 | |
1587 | |
1559 | if (expect_true (!CCTX_EXPIRED (cctx))) |
1588 | if (ecb_expect_true (!CCTX_EXPIRED (cctx))) |
1560 | return cctx; |
1589 | return cctx; |
1561 | |
1590 | |
1562 | cctx_destroy (cctx); |
1591 | cctx_destroy (cctx); |
1563 | } |
1592 | } |
1564 | |
1593 | |
… | |
… | |
1569 | cctx_put (coro_cctx *cctx) |
1598 | cctx_put (coro_cctx *cctx) |
1570 | { |
1599 | { |
1571 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1600 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1572 | |
1601 | |
1573 | /* free another cctx if overlimit */ |
1602 | /* free another cctx if overlimit */ |
1574 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1603 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1575 | { |
1604 | { |
1576 | coro_cctx *first = cctx_first; |
1605 | coro_cctx *first = cctx_first; |
1577 | cctx_first = first->next; |
1606 | cctx_first = first->next; |
1578 | --cctx_idle; |
1607 | --cctx_idle; |
1579 | |
1608 | |
… | |
… | |
1590 | static void |
1619 | static void |
1591 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1620 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1592 | { |
1621 | { |
1593 | /* TODO: throwing up here is considered harmful */ |
1622 | /* TODO: throwing up here is considered harmful */ |
1594 | |
1623 | |
1595 | if (expect_true (prev != next)) |
1624 | if (ecb_expect_true (prev != next)) |
1596 | { |
1625 | { |
1597 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1626 | if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1598 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1627 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1599 | |
1628 | |
1600 | if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1629 | if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1601 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1630 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1602 | |
1631 | |
1603 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1632 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1604 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1633 | if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING)) |
1605 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1634 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1606 | #endif |
1635 | #endif |
1607 | } |
1636 | } |
1608 | } |
1637 | } |
1609 | |
1638 | |
1610 | /* always use the TRANSFER macro */ |
1639 | /* always use the TRANSFER macro */ |
1611 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1640 | static void ecb_noinline /* noinline so we have a fixed stackframe */ |
1612 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1641 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1613 | { |
1642 | { |
1614 | dSTACKLEVEL; |
1643 | dSTACKLEVEL; |
1615 | |
1644 | |
1616 | /* sometimes transfer is only called to set idle_sp */ |
1645 | /* sometimes transfer is only called to set idle_sp */ |
1617 | if (expect_false (!prev)) |
1646 | if (ecb_expect_false (!prev)) |
1618 | { |
1647 | { |
1619 | cctx_current->idle_sp = STACKLEVEL; |
1648 | cctx_current->idle_sp = STACKLEVEL; |
1620 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1649 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1621 | } |
1650 | } |
1622 | else if (expect_true (prev != next)) |
1651 | else if (ecb_expect_true (prev != next)) |
1623 | { |
1652 | { |
1624 | coro_cctx *cctx_prev; |
1653 | coro_cctx *cctx_prev; |
1625 | |
1654 | |
1626 | if (expect_false (prev->flags & CF_NEW)) |
1655 | if (ecb_expect_false (prev->flags & CF_NEW)) |
1627 | { |
1656 | { |
1628 | /* create a new empty/source context */ |
1657 | /* create a new empty/source context */ |
1629 | prev->flags &= ~CF_NEW; |
1658 | prev->flags &= ~CF_NEW; |
1630 | prev->flags |= CF_RUNNING; |
1659 | prev->flags |= CF_RUNNING; |
1631 | } |
1660 | } |
… | |
… | |
1634 | next->flags |= CF_RUNNING; |
1663 | next->flags |= CF_RUNNING; |
1635 | |
1664 | |
1636 | /* first get rid of the old state */ |
1665 | /* first get rid of the old state */ |
1637 | save_perl (aTHX_ prev); |
1666 | save_perl (aTHX_ prev); |
1638 | |
1667 | |
1639 | if (expect_false (next->flags & CF_NEW)) |
1668 | if (ecb_expect_false (next->flags & CF_NEW)) |
1640 | { |
1669 | { |
1641 | /* need to start coroutine */ |
1670 | /* need to start coroutine */ |
1642 | next->flags &= ~CF_NEW; |
1671 | next->flags &= ~CF_NEW; |
1643 | /* setup coroutine call */ |
1672 | /* setup coroutine call */ |
1644 | init_perl (aTHX_ next); |
1673 | init_perl (aTHX_ next); |
1645 | } |
1674 | } |
1646 | else |
1675 | else |
1647 | load_perl (aTHX_ next); |
1676 | load_perl (aTHX_ next); |
1648 | |
1677 | |
1649 | /* possibly untie and reuse the cctx */ |
1678 | /* possibly untie and reuse the cctx */ |
1650 | if (expect_true ( |
1679 | if (ecb_expect_true ( |
1651 | cctx_current->idle_sp == STACKLEVEL |
1680 | cctx_current->idle_sp == STACKLEVEL |
1652 | && !(cctx_current->flags & CC_TRACE) |
1681 | && !(cctx_current->flags & CC_TRACE) |
1653 | && !force_cctx |
1682 | && !force_cctx |
1654 | )) |
1683 | )) |
1655 | { |
1684 | { |
1656 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1685 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1657 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1686 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1658 | |
1687 | |
1659 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1688 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1660 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1689 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1661 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1690 | if (ecb_expect_false (CCTX_EXPIRED (cctx_current))) |
1662 | if (expect_true (!next->cctx)) |
1691 | if (ecb_expect_true (!next->cctx)) |
1663 | next->cctx = cctx_get (aTHX); |
1692 | next->cctx = cctx_get (aTHX); |
1664 | |
1693 | |
1665 | cctx_put (cctx_current); |
1694 | cctx_put (cctx_current); |
1666 | } |
1695 | } |
1667 | else |
1696 | else |
1668 | prev->cctx = cctx_current; |
1697 | prev->cctx = cctx_current; |
1669 | |
1698 | |
1670 | ++next->usecount; |
1699 | ++next->usecount; |
1671 | |
1700 | |
1672 | cctx_prev = cctx_current; |
1701 | cctx_prev = cctx_current; |
1673 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1702 | cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1674 | |
1703 | |
1675 | next->cctx = 0; |
1704 | next->cctx = 0; |
1676 | |
1705 | |
1677 | if (expect_false (cctx_prev != cctx_current)) |
1706 | if (ecb_expect_false (cctx_prev != cctx_current)) |
1678 | { |
1707 | { |
1679 | cctx_prev->top_env = PL_top_env; |
1708 | cctx_prev->top_env = PL_top_env; |
1680 | PL_top_env = cctx_current->top_env; |
1709 | PL_top_env = cctx_current->top_env; |
1681 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1710 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1682 | } |
1711 | } |
… | |
… | |
1748 | Safefree (coro); |
1777 | Safefree (coro); |
1749 | |
1778 | |
1750 | return 0; |
1779 | return 0; |
1751 | } |
1780 | } |
1752 | |
1781 | |
1753 | static int |
1782 | static int ecb_cold |
1754 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1783 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1755 | { |
1784 | { |
1756 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1785 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1757 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1786 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1758 | mg->mg_ptr = 0; |
1787 | mg->mg_ptr = 0; |
… | |
… | |
1789 | TRANSFER (ta, 1); |
1818 | TRANSFER (ta, 1); |
1790 | } |
1819 | } |
1791 | |
1820 | |
1792 | /** Coro ********************************************************************/ |
1821 | /** Coro ********************************************************************/ |
1793 | |
1822 | |
1794 | INLINE void |
1823 | ecb_inline void |
1795 | coro_enq (pTHX_ struct coro *coro) |
1824 | coro_enq (pTHX_ struct coro *coro) |
1796 | { |
1825 | { |
1797 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1826 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1798 | |
1827 | |
1799 | SvREFCNT_inc_NN (coro->hv); |
1828 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1801 | coro->next_ready = 0; |
1830 | coro->next_ready = 0; |
1802 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1831 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1803 | ready [1] = coro; |
1832 | ready [1] = coro; |
1804 | } |
1833 | } |
1805 | |
1834 | |
1806 | INLINE struct coro * |
1835 | ecb_inline struct coro * |
1807 | coro_deq (pTHX) |
1836 | coro_deq (pTHX) |
1808 | { |
1837 | { |
1809 | int prio; |
1838 | int prio; |
1810 | |
1839 | |
1811 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1840 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1864 | { |
1893 | { |
1865 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1894 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1866 | } |
1895 | } |
1867 | |
1896 | |
1868 | /* expects to own a reference to next->hv */ |
1897 | /* expects to own a reference to next->hv */ |
1869 | INLINE void |
1898 | ecb_inline void |
1870 | 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) |
1871 | { |
1900 | { |
1872 | SV *prev_sv = SvRV (coro_current); |
1901 | SV *prev_sv = SvRV (coro_current); |
1873 | |
1902 | |
1874 | ta->prev = SvSTATE_hv (prev_sv); |
1903 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1887 | { |
1916 | { |
1888 | for (;;) |
1917 | for (;;) |
1889 | { |
1918 | { |
1890 | struct coro *next = coro_deq (aTHX); |
1919 | struct coro *next = coro_deq (aTHX); |
1891 | |
1920 | |
1892 | if (expect_true (next)) |
1921 | if (ecb_expect_true (next)) |
1893 | { |
1922 | { |
1894 | /* cannot transfer to destroyed coros, skip and look for next */ |
1923 | /* cannot transfer to destroyed coros, skip and look for next */ |
1895 | if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1924 | if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1896 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1925 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1897 | else |
1926 | else |
1898 | { |
1927 | { |
1899 | next->flags &= ~CF_READY; |
1928 | next->flags &= ~CF_READY; |
1900 | --coro_nready; |
1929 | --coro_nready; |
… | |
… | |
1933 | } |
1962 | } |
1934 | } |
1963 | } |
1935 | } |
1964 | } |
1936 | } |
1965 | } |
1937 | |
1966 | |
1938 | INLINE void |
1967 | ecb_inline void |
1939 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1968 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1940 | { |
1969 | { |
1941 | api_ready (aTHX_ coro_current); |
1970 | api_ready (aTHX_ coro_current); |
1942 | prepare_schedule (aTHX_ ta); |
1971 | prepare_schedule (aTHX_ ta); |
1943 | } |
1972 | } |
1944 | |
1973 | |
1945 | INLINE void |
1974 | ecb_inline void |
1946 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1975 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1947 | { |
1976 | { |
1948 | SV *prev = SvRV (coro_current); |
1977 | SV *prev = SvRV (coro_current); |
1949 | |
1978 | |
1950 | if (coro_nready) |
1979 | if (coro_nready) |
… | |
… | |
1980 | { |
2009 | { |
1981 | struct coro_transfer_args ta; |
2010 | struct coro_transfer_args ta; |
1982 | |
2011 | |
1983 | prepare_cede (aTHX_ &ta); |
2012 | prepare_cede (aTHX_ &ta); |
1984 | |
2013 | |
1985 | if (expect_true (ta.prev != ta.next)) |
2014 | if (ecb_expect_true (ta.prev != ta.next)) |
1986 | { |
2015 | { |
1987 | TRANSFER (ta, 1); |
2016 | TRANSFER (ta, 1); |
1988 | return 1; |
2017 | return 1; |
1989 | } |
2018 | } |
1990 | else |
2019 | else |
… | |
… | |
2203 | coro = SvSTATE (arg [0]); |
2232 | coro = SvSTATE (arg [0]); |
2204 | coro_hv = coro->hv; |
2233 | coro_hv = coro->hv; |
2205 | |
2234 | |
2206 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2235 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2207 | |
2236 | |
2208 | if (expect_false (coro->flags & CF_NOCANCEL)) |
2237 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2209 | { |
2238 | { |
2210 | /* coro currently busy cancelling something, so just notify it */ |
2239 | /* coro currently busy cancelling something, so just notify it */ |
2211 | coro->slf_frame.data = (void *)coro; |
2240 | coro->slf_frame.data = (void *)coro; |
2212 | |
2241 | |
2213 | frame->prepare = prepare_nop; |
2242 | frame->prepare = prepare_nop; |
… | |
… | |
2321 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2350 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2322 | { |
2351 | { |
2323 | HV *hv = (HV *)SvRV (coro_current); |
2352 | HV *hv = (HV *)SvRV (coro_current); |
2324 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2353 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2325 | |
2354 | |
2326 | if (expect_true (coro->saved_deffh)) |
2355 | if (ecb_expect_true (coro->saved_deffh)) |
2327 | { |
2356 | { |
2328 | /* subsequent iteration */ |
2357 | /* subsequent iteration */ |
2329 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2358 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2330 | coro->saved_deffh = 0; |
2359 | coro->saved_deffh = 0; |
2331 | |
2360 | |
… | |
… | |
2597 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2626 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2598 | |
2627 | |
2599 | /* set up the slf frame, unless it has already been set-up */ |
2628 | /* set up the slf frame, unless it has already been set-up */ |
2600 | /* the latter happens when a new coro has been started */ |
2629 | /* the latter happens when a new coro has been started */ |
2601 | /* or when a new cctx was attached to an existing coroutine */ |
2630 | /* or when a new cctx was attached to an existing coroutine */ |
2602 | if (expect_true (!slf_frame.prepare)) |
2631 | if (ecb_expect_true (!slf_frame.prepare)) |
2603 | { |
2632 | { |
2604 | /* first iteration */ |
2633 | /* first iteration */ |
2605 | dSP; |
2634 | dSP; |
2606 | SV **arg = PL_stack_base + TOPMARK + 1; |
2635 | SV **arg = PL_stack_base + TOPMARK + 1; |
2607 | int items = SP - arg; /* args without function object */ |
2636 | int items = SP - arg; /* args without function object */ |
… | |
… | |
2648 | while (slf_frame.check (aTHX_ &slf_frame)); |
2677 | while (slf_frame.check (aTHX_ &slf_frame)); |
2649 | |
2678 | |
2650 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2679 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2651 | |
2680 | |
2652 | /* exception handling */ |
2681 | /* exception handling */ |
2653 | if (expect_false (CORO_THROW)) |
2682 | if (ecb_expect_false (CORO_THROW)) |
2654 | { |
2683 | { |
2655 | SV *exception = sv_2mortal (CORO_THROW); |
2684 | SV *exception = sv_2mortal (CORO_THROW); |
2656 | |
2685 | |
2657 | CORO_THROW = 0; |
2686 | CORO_THROW = 0; |
2658 | sv_setsv (ERRSV, exception); |
2687 | sv_setsv (ERRSV, exception); |
… | |
… | |
2660 | } |
2689 | } |
2661 | |
2690 | |
2662 | /* return value handling - mostly like entersub */ |
2691 | /* return value handling - mostly like entersub */ |
2663 | /* make sure we put something on the stack in scalar context */ |
2692 | /* make sure we put something on the stack in scalar context */ |
2664 | if (GIMME_V == G_SCALAR |
2693 | if (GIMME_V == G_SCALAR |
2665 | && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2694 | && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2666 | { |
2695 | { |
2667 | dSP; |
2696 | dSP; |
2668 | SV **bot = PL_stack_base + checkmark; |
2697 | SV **bot = PL_stack_base + checkmark; |
2669 | |
2698 | |
2670 | if (sp == bot) /* too few, push undef */ |
2699 | if (sp == bot) /* too few, push undef */ |
… | |
… | |
2784 | { |
2813 | { |
2785 | PerlIOBuf base; |
2814 | PerlIOBuf base; |
2786 | NV next, every; |
2815 | NV next, every; |
2787 | } PerlIOCede; |
2816 | } PerlIOCede; |
2788 | |
2817 | |
2789 | static IV |
2818 | static IV ecb_cold |
2790 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2819 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2791 | { |
2820 | { |
2792 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2821 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2793 | |
2822 | |
2794 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2823 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2795 | self->next = nvtime () + self->every; |
2824 | self->next = nvtime () + self->every; |
2796 | |
2825 | |
2797 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2826 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2798 | } |
2827 | } |
2799 | |
2828 | |
2800 | static SV * |
2829 | static SV * ecb_cold |
2801 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2830 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2802 | { |
2831 | { |
2803 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2832 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2804 | |
2833 | |
2805 | return newSVnv (self->every); |
2834 | return newSVnv (self->every); |
… | |
… | |
2950 | { |
2979 | { |
2951 | int i; |
2980 | int i; |
2952 | /* 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 */ |
2953 | /* 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 */ |
2954 | /* this avoids some degenerate memory usage cases */ |
2983 | /* this avoids some degenerate memory usage cases */ |
2955 | 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 */ |
2956 | if (AvARRAY (av)[i] == coro_hv) |
2985 | if (AvARRAY (av)[i] == coro_hv) |
2957 | return 1; |
2986 | return 1; |
2958 | |
2987 | |
2959 | av_push (av, SvREFCNT_inc (coro_hv)); |
2988 | av_push (av, SvREFCNT_inc (coro_hv)); |
2960 | return 1; |
2989 | return 1; |
… | |
… | |
3217 | dSP; |
3246 | dSP; |
3218 | |
3247 | |
3219 | static SV *prio_cv; |
3248 | static SV *prio_cv; |
3220 | static SV *prio_sv; |
3249 | static SV *prio_sv; |
3221 | |
3250 | |
3222 | if (expect_false (!prio_cv)) |
3251 | if (ecb_expect_false (!prio_cv)) |
3223 | { |
3252 | { |
3224 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3253 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3225 | prio_sv = newSViv (0); |
3254 | prio_sv = newSViv (0); |
3226 | } |
3255 | } |
3227 | |
3256 | |
… | |
… | |
3333 | } |
3362 | } |
3334 | |
3363 | |
3335 | return coro_sv; |
3364 | return coro_sv; |
3336 | } |
3365 | } |
3337 | |
3366 | |
|
|
3367 | #ifndef __cplusplus |
|
|
3368 | ecb_cold XS(boot_Coro__State); |
|
|
3369 | #endif |
|
|
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 | |
3338 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3434 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3339 | |
3435 | |
3340 | PROTOTYPES: DISABLE |
3436 | PROTOTYPES: DISABLE |
3341 | |
3437 | |
3342 | BOOT: |
3438 | BOOT: |
… | |
… | |
3345 | # if CORO_PTHREAD |
3441 | # if CORO_PTHREAD |
3346 | coro_thx = PERL_GET_CONTEXT; |
3442 | coro_thx = PERL_GET_CONTEXT; |
3347 | # endif |
3443 | # endif |
3348 | #endif |
3444 | #endif |
3349 | 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; |
3350 | |
3453 | |
3351 | cctx_current = cctx_new_empty (); |
3454 | cctx_current = cctx_new_empty (); |
3352 | |
3455 | |
3353 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3456 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3354 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3457 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
3397 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3500 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3398 | |
3501 | |
3399 | time_init (aTHX); |
3502 | time_init (aTHX); |
3400 | |
3503 | |
3401 | 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 |
3402 | } |
3510 | } |
3403 | |
3511 | |
3404 | SV * |
3512 | SV * |
3405 | new (SV *klass, ...) |
3513 | new (SV *klass, ...) |
3406 | ALIAS: |
3514 | ALIAS: |
… | |
… | |
3557 | RETVAL = boolSV (coro->flags & ix); |
3665 | RETVAL = boolSV (coro->flags & ix); |
3558 | OUTPUT: |
3666 | OUTPUT: |
3559 | RETVAL |
3667 | RETVAL |
3560 | |
3668 | |
3561 | void |
3669 | void |
3562 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3670 | throw (SV *self, SV *exception = &PL_sv_undef) |
3563 | PROTOTYPE: $;$ |
3671 | PROTOTYPE: $;$ |
3564 | CODE: |
3672 | CODE: |
3565 | { |
3673 | { |
|
|
3674 | struct coro *coro = SvSTATE (self); |
3566 | struct coro *current = SvSTATE_current; |
3675 | struct coro *current = SvSTATE_current; |
3567 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3676 | SV **exceptionp = coro == current ? &CORO_THROW : &coro->except; |
3568 | SvREFCNT_dec (*exceptionp); |
3677 | SvREFCNT_dec (*exceptionp); |
3569 | SvGETMAGIC (exception); |
3678 | SvGETMAGIC (exception); |
3570 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3679 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
|
|
3680 | |
|
|
3681 | api_ready (aTHX_ self); |
3571 | } |
3682 | } |
3572 | |
3683 | |
3573 | void |
3684 | void |
3574 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3685 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3575 | PROTOTYPE: $;$ |
3686 | PROTOTYPE: $;$ |
… | |
… | |
3654 | times (Coro::State self) |
3765 | times (Coro::State self) |
3655 | PPCODE: |
3766 | PPCODE: |
3656 | { |
3767 | { |
3657 | struct coro *current = SvSTATE (coro_current); |
3768 | struct coro *current = SvSTATE (coro_current); |
3658 | |
3769 | |
3659 | if (expect_false (current == self)) |
3770 | if (ecb_expect_false (current == self)) |
3660 | { |
3771 | { |
3661 | coro_times_update (); |
3772 | coro_times_update (); |
3662 | coro_times_add (SvSTATE (coro_current)); |
3773 | coro_times_add (SvSTATE (coro_current)); |
3663 | } |
3774 | } |
3664 | |
3775 | |
3665 | EXTEND (SP, 2); |
3776 | EXTEND (SP, 2); |
3666 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3777 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3667 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3778 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3668 | |
3779 | |
3669 | if (expect_false (current == self)) |
3780 | if (ecb_expect_false (current == self)) |
3670 | coro_times_sub (SvSTATE (coro_current)); |
3781 | coro_times_sub (SvSTATE (coro_current)); |
3671 | } |
3782 | } |
3672 | |
3783 | |
3673 | void |
3784 | void |
3674 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3785 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
… | |
… | |
4056 | } |
4167 | } |
4057 | |
4168 | |
4058 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4169 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4059 | |
4170 | |
4060 | void |
4171 | void |
4061 | _may_delete (SV *sem, int count, int extra_refs) |
4172 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4062 | PPCODE: |
4173 | PPCODE: |
4063 | { |
4174 | { |
4064 | AV *av = (AV *)SvRV (sem); |
4175 | AV *av = (AV *)SvRV (sem); |
4065 | |
4176 | |
4066 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4177 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |