… | |
… | |
33 | # define setjmp _setjmp /* deep magic */ |
33 | # define setjmp _setjmp /* deep magic */ |
34 | #else |
34 | #else |
35 | # include <inttypes.h> /* most portable stdint.h */ |
35 | # include <inttypes.h> /* most portable stdint.h */ |
36 | #endif |
36 | #endif |
37 | |
37 | |
38 | #ifdef HAVE_MMAP |
38 | #if HAVE_MMAP |
39 | # include <unistd.h> |
39 | # include <unistd.h> |
40 | # include <sys/mman.h> |
40 | # include <sys/mman.h> |
41 | # ifndef MAP_ANONYMOUS |
41 | # ifndef MAP_ANONYMOUS |
42 | # ifdef MAP_ANON |
42 | # ifdef MAP_ANON |
43 | # define MAP_ANONYMOUS MAP_ANON |
43 | # define MAP_ANONYMOUS MAP_ANON |
… | |
… | |
63 | #endif |
63 | #endif |
64 | |
64 | |
65 | /* the maximum number of idle cctx that will be pooled */ |
65 | /* the maximum number of idle cctx that will be pooled */ |
66 | static int cctx_max_idle = 4; |
66 | static int cctx_max_idle = 4; |
67 | |
67 | |
|
|
68 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
|
|
69 | # define HAS_SCOPESTACK_NAME 1 |
|
|
70 | #endif |
|
|
71 | |
68 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
72 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
69 | # undef CORO_STACKGUARD |
73 | # undef CORO_STACKGUARD |
70 | #endif |
74 | #endif |
71 | |
75 | |
72 | #ifndef CORO_STACKGUARD |
76 | #ifndef CORO_STACKGUARD |
… | |
… | |
212 | #define VARx(name,expr,type) type name; |
216 | #define VARx(name,expr,type) type name; |
213 | # include "state.h" |
217 | # include "state.h" |
214 | #undef VARx |
218 | #undef VARx |
215 | } perl_slots; |
219 | } perl_slots; |
216 | |
220 | |
217 | // how many context stack entries do we need for perl_slots |
221 | /* how many context stack entries do we need for perl_slots */ |
218 | #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)) |
219 | |
223 | |
220 | /* this is a structure representing a perl-level coroutine */ |
224 | /* this is a structure representing a perl-level coroutine */ |
221 | struct coro |
225 | struct coro |
222 | { |
226 | { |
… | |
… | |
291 | #define coro_nready coroapi.nready |
295 | #define coro_nready coroapi.nready |
292 | |
296 | |
293 | /** JIT *********************************************************************/ |
297 | /** JIT *********************************************************************/ |
294 | |
298 | |
295 | #if CORO_JIT |
299 | #if CORO_JIT |
|
|
300 | /* APPLE doesn't have HAVE_MMAP though */ |
|
|
301 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
296 | #ifndef CORO_JIT_TYPE |
302 | #ifndef CORO_JIT_TYPE |
297 | #if __x86_64 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris) |
303 | #if __x86_64 && CORO_JIT_UNIXY |
298 | #define CORO_JIT_TYPE "amd64-unix" |
304 | #define CORO_JIT_TYPE "amd64-unix" |
299 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
305 | #elif __i386 && CORO_JIT_UNIXY |
300 | #elif __i386 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris) |
|
|
301 | #define CORO_JIT_TYPE "x86-unix" |
306 | #define CORO_JIT_TYPE "x86-unix" |
302 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
|
|
303 | #else |
|
|
304 | #undef CORO_JIT |
|
|
305 | #endif |
307 | #endif |
306 | #endif |
308 | #endif |
307 | #endif |
309 | #endif |
308 | |
310 | |
|
|
311 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
|
|
312 | #undef CORO_JIT |
|
|
313 | #endif |
|
|
314 | |
309 | #if CORO_JIT |
315 | #if CORO_JIT |
|
|
316 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
310 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
317 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
311 | #endif |
318 | #endif |
312 | |
319 | |
313 | /** Coro::Select ************************************************************/ |
320 | /** Coro::Select ************************************************************/ |
314 | |
321 | |
315 | static OP *(*coro_old_pp_sselect) (pTHX); |
322 | static OP *(*coro_old_pp_sselect) (pTHX); |
… | |
… | |
332 | |
339 | |
333 | /** time stuff **************************************************************/ |
340 | /** time stuff **************************************************************/ |
334 | |
341 | |
335 | #ifdef HAS_GETTIMEOFDAY |
342 | #ifdef HAS_GETTIMEOFDAY |
336 | |
343 | |
337 | ECB_INLINE void |
344 | ecb_inline void |
338 | coro_u2time (pTHX_ UV ret[2]) |
345 | coro_u2time (pTHX_ UV ret[2]) |
339 | { |
346 | { |
340 | struct timeval tv; |
347 | struct timeval tv; |
341 | gettimeofday (&tv, 0); |
348 | gettimeofday (&tv, 0); |
342 | |
349 | |
343 | ret [0] = tv.tv_sec; |
350 | ret [0] = tv.tv_sec; |
344 | ret [1] = tv.tv_usec; |
351 | ret [1] = tv.tv_usec; |
345 | } |
352 | } |
346 | |
353 | |
347 | ECB_INLINE double |
354 | ecb_inline double |
348 | coro_nvtime () |
355 | coro_nvtime (void) |
349 | { |
356 | { |
350 | struct timeval tv; |
357 | struct timeval tv; |
351 | gettimeofday (&tv, 0); |
358 | gettimeofday (&tv, 0); |
352 | |
359 | |
353 | return tv.tv_sec + tv.tv_usec * 1e-6; |
360 | return tv.tv_sec + tv.tv_usec * 1e-6; |
354 | } |
361 | } |
355 | |
362 | |
356 | ECB_INLINE void |
363 | ecb_inline void |
357 | time_init (pTHX) |
364 | time_init (pTHX) |
358 | { |
365 | { |
359 | nvtime = coro_nvtime; |
366 | nvtime = coro_nvtime; |
360 | u2time = coro_u2time; |
367 | u2time = coro_u2time; |
361 | } |
368 | } |
362 | |
369 | |
363 | #else |
370 | #else |
364 | |
371 | |
365 | ECB_INLINE void |
372 | ecb_inline void |
366 | time_init (pTHX) |
373 | time_init (pTHX) |
367 | { |
374 | { |
368 | SV **svp; |
375 | SV **svp; |
369 | |
376 | |
370 | require_pv ("Time/HiRes.pm"); |
377 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
412 | get_hv (name, create); |
419 | get_hv (name, create); |
413 | #endif |
420 | #endif |
414 | return get_hv (name, create); |
421 | return get_hv (name, create); |
415 | } |
422 | } |
416 | |
423 | |
417 | ECB_INLINE void |
424 | ecb_inline void |
418 | coro_times_update () |
425 | coro_times_update (void) |
419 | { |
426 | { |
420 | #ifdef coro_clock_gettime |
427 | #ifdef coro_clock_gettime |
421 | struct timespec ts; |
428 | struct timespec ts; |
422 | |
429 | |
423 | ts.tv_sec = ts.tv_nsec = 0; |
430 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
435 | time_real [0] = tv [0]; |
442 | time_real [0] = tv [0]; |
436 | time_real [1] = tv [1] * 1000; |
443 | time_real [1] = tv [1] * 1000; |
437 | #endif |
444 | #endif |
438 | } |
445 | } |
439 | |
446 | |
440 | ECB_INLINE void |
447 | ecb_inline void |
441 | coro_times_add (struct coro *c) |
448 | coro_times_add (struct coro *c) |
442 | { |
449 | { |
443 | c->t_real [1] += time_real [1]; |
450 | c->t_real [1] += time_real [1]; |
444 | 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]; } |
445 | c->t_real [0] += time_real [0]; |
452 | c->t_real [0] += time_real [0]; |
… | |
… | |
447 | c->t_cpu [1] += time_cpu [1]; |
454 | c->t_cpu [1] += time_cpu [1]; |
448 | 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]; } |
449 | c->t_cpu [0] += time_cpu [0]; |
456 | c->t_cpu [0] += time_cpu [0]; |
450 | } |
457 | } |
451 | |
458 | |
452 | ECB_INLINE void |
459 | ecb_inline void |
453 | coro_times_sub (struct coro *c) |
460 | coro_times_sub (struct coro *c) |
454 | { |
461 | { |
455 | 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]; } |
456 | c->t_real [1] -= time_real [1]; |
463 | c->t_real [1] -= time_real [1]; |
457 | c->t_real [0] -= time_real [0]; |
464 | c->t_real [0] -= time_real [0]; |
… | |
… | |
478 | : 0) |
485 | : 0) |
479 | |
486 | |
480 | #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) |
481 | #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) |
482 | |
489 | |
483 | ECB_INLINE MAGIC * |
490 | ecb_inline MAGIC * |
484 | SvSTATEhv_p (pTHX_ SV *coro) |
491 | SvSTATEhv_p (pTHX_ SV *coro) |
485 | { |
492 | { |
486 | MAGIC *mg; |
493 | MAGIC *mg; |
487 | |
494 | |
488 | if (ecb_expect_true ( |
495 | if (ecb_expect_true ( |
… | |
… | |
493 | return mg; |
500 | return mg; |
494 | |
501 | |
495 | return 0; |
502 | return 0; |
496 | } |
503 | } |
497 | |
504 | |
498 | ECB_INLINE struct coro * |
505 | ecb_inline struct coro * |
499 | SvSTATE_ (pTHX_ SV *coro) |
506 | SvSTATE_ (pTHX_ SV *coro) |
500 | { |
507 | { |
501 | MAGIC *mg; |
508 | MAGIC *mg; |
502 | |
509 | |
503 | if (SvROK (coro)) |
510 | if (SvROK (coro)) |
… | |
… | |
517 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
524 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
518 | |
525 | |
519 | /*****************************************************************************/ |
526 | /*****************************************************************************/ |
520 | /* padlist management and caching */ |
527 | /* padlist management and caching */ |
521 | |
528 | |
522 | ECB_INLINE AV * |
529 | ecb_inline AV * |
523 | coro_derive_padlist (pTHX_ CV *cv) |
530 | coro_derive_padlist (pTHX_ CV *cv) |
524 | { |
531 | { |
525 | AV *padlist = CvPADLIST (cv); |
532 | AV *padlist = CvPADLIST (cv); |
526 | AV *newpadlist, *newpad; |
533 | AV *newpadlist, *newpad; |
527 | |
534 | |
… | |
… | |
539 | av_store (newpadlist, 1, (SV *)newpad); |
546 | av_store (newpadlist, 1, (SV *)newpad); |
540 | |
547 | |
541 | return newpadlist; |
548 | return newpadlist; |
542 | } |
549 | } |
543 | |
550 | |
544 | ECB_INLINE void |
551 | ecb_inline void |
545 | free_padlist (pTHX_ AV *padlist) |
552 | free_padlist (pTHX_ AV *padlist) |
546 | { |
553 | { |
547 | /* may be during global destruction */ |
554 | /* may be during global destruction */ |
548 | if (!IN_DESTRUCT) |
555 | if (!IN_DESTRUCT) |
549 | { |
556 | { |
… | |
… | |
592 | 0, 0, 0, 0, |
599 | 0, 0, 0, 0, |
593 | coro_cv_free |
600 | coro_cv_free |
594 | }; |
601 | }; |
595 | |
602 | |
596 | /* the next two functions merely cache the padlists */ |
603 | /* the next two functions merely cache the padlists */ |
597 | ECB_INLINE void |
604 | ecb_inline void |
598 | get_padlist (pTHX_ CV *cv) |
605 | get_padlist (pTHX_ CV *cv) |
599 | { |
606 | { |
600 | MAGIC *mg = CORO_MAGIC_cv (cv); |
607 | MAGIC *mg = CORO_MAGIC_cv (cv); |
601 | AV *av; |
608 | AV *av; |
602 | |
609 | |
… | |
… | |
615 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
622 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
616 | #endif |
623 | #endif |
617 | } |
624 | } |
618 | } |
625 | } |
619 | |
626 | |
620 | ECB_INLINE void |
627 | ecb_inline void |
621 | put_padlist (pTHX_ CV *cv) |
628 | put_padlist (pTHX_ CV *cv) |
622 | { |
629 | { |
623 | MAGIC *mg = CORO_MAGIC_cv (cv); |
630 | MAGIC *mg = CORO_MAGIC_cv (cv); |
624 | AV *av; |
631 | AV *av; |
625 | |
632 | |
… | |
… | |
814 | |
821 | |
815 | PUTBACK; |
822 | PUTBACK; |
816 | } |
823 | } |
817 | |
824 | |
818 | /* allocate some space on the context stack for our purposes */ |
825 | /* allocate some space on the context stack for our purposes */ |
819 | if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
826 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
820 | { |
827 | { |
821 | unsigned int i; |
828 | unsigned int i; |
822 | |
829 | |
823 | for (i = 0; i < SLOT_COUNT; ++i) |
830 | for (i = 0; i < SLOT_COUNT; ++i) |
824 | CXINC; |
831 | CXINC; |
… | |
… | |
876 | #endif |
883 | #endif |
877 | |
884 | |
878 | New(54,PL_scopestack,8,I32); |
885 | New(54,PL_scopestack,8,I32); |
879 | PL_scopestack_ix = 0; |
886 | PL_scopestack_ix = 0; |
880 | PL_scopestack_max = 8; |
887 | PL_scopestack_max = 8; |
|
|
888 | #if HAS_SCOPESTACK_NAME |
|
|
889 | New(54,PL_scopestack_name,8,const char*); |
|
|
890 | #endif |
881 | |
891 | |
882 | New(54,PL_savestack,24,ANY); |
892 | New(54,PL_savestack,24,ANY); |
883 | PL_savestack_ix = 0; |
893 | PL_savestack_ix = 0; |
884 | PL_savestack_max = 24; |
894 | PL_savestack_max = 24; |
885 | |
895 | |
… | |
… | |
913 | } |
923 | } |
914 | |
924 | |
915 | Safefree (PL_tmps_stack); |
925 | Safefree (PL_tmps_stack); |
916 | Safefree (PL_markstack); |
926 | Safefree (PL_markstack); |
917 | Safefree (PL_scopestack); |
927 | Safefree (PL_scopestack); |
|
|
928 | #if HAS_SCOPESTACK_NAME |
|
|
929 | Safefree (PL_scopestack_name); |
|
|
930 | #endif |
918 | Safefree (PL_savestack); |
931 | Safefree (PL_savestack); |
919 | #if !PERL_VERSION_ATLEAST (5,10,0) |
932 | #if !PERL_VERSION_ATLEAST (5,10,0) |
920 | Safefree (PL_retstack); |
933 | Safefree (PL_retstack); |
921 | #endif |
934 | #endif |
922 | } |
935 | } |
… | |
… | |
1187 | /* restore swapped sv's */ |
1200 | /* restore swapped sv's */ |
1188 | SWAP_SVS (coro); |
1201 | SWAP_SVS (coro); |
1189 | |
1202 | |
1190 | coro_destruct_stacks (aTHX); |
1203 | coro_destruct_stacks (aTHX); |
1191 | |
1204 | |
1192 | // now save some sv's to be free'd later |
1205 | /* now save some sv's to be free'd later */ |
1193 | svf [0] = GvSV (PL_defgv); |
1206 | svf [0] = GvSV (PL_defgv); |
1194 | svf [1] = (SV *)GvAV (PL_defgv); |
1207 | svf [1] = (SV *)GvAV (PL_defgv); |
1195 | svf [2] = GvSV (PL_errgv); |
1208 | svf [2] = GvSV (PL_errgv); |
1196 | svf [3] = (SV *)PL_defoutgv; |
1209 | svf [3] = (SV *)PL_defoutgv; |
1197 | svf [4] = PL_rs; |
1210 | svf [4] = PL_rs; |
… | |
… | |
1217 | SvREFCNT_dec (coro->invoke_cb); |
1230 | SvREFCNT_dec (coro->invoke_cb); |
1218 | SvREFCNT_dec (coro->invoke_av); |
1231 | SvREFCNT_dec (coro->invoke_av); |
1219 | } |
1232 | } |
1220 | } |
1233 | } |
1221 | |
1234 | |
1222 | ECB_INLINE void |
1235 | ecb_inline void |
1223 | free_coro_mortal (pTHX) |
1236 | free_coro_mortal (pTHX) |
1224 | { |
1237 | { |
1225 | if (ecb_expect_true (coro_mortal)) |
1238 | if (ecb_expect_true (coro_mortal)) |
1226 | { |
1239 | { |
1227 | SvREFCNT_dec ((SV *)coro_mortal); |
1240 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1386 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1399 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1387 | slf_frame.check = slf_check_set_stacklevel; |
1400 | slf_frame.check = slf_check_set_stacklevel; |
1388 | } |
1401 | } |
1389 | |
1402 | |
1390 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1403 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1391 | ECB_INLINE void |
1404 | ecb_inline void |
1392 | transfer_tail (pTHX) |
1405 | transfer_tail (pTHX) |
1393 | { |
1406 | { |
1394 | free_coro_mortal (aTHX); |
1407 | free_coro_mortal (aTHX); |
1395 | } |
1408 | } |
1396 | |
1409 | |
… | |
… | |
1440 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1453 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1441 | } |
1454 | } |
1442 | } |
1455 | } |
1443 | |
1456 | |
1444 | static coro_cctx * |
1457 | static coro_cctx * |
1445 | cctx_new () |
1458 | cctx_new (void) |
1446 | { |
1459 | { |
1447 | coro_cctx *cctx; |
1460 | coro_cctx *cctx; |
1448 | |
1461 | |
1449 | ++cctx_count; |
1462 | ++cctx_count; |
1450 | New (0, cctx, 1, coro_cctx); |
1463 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1456 | return cctx; |
1469 | return cctx; |
1457 | } |
1470 | } |
1458 | |
1471 | |
1459 | /* create a new cctx only suitable as source */ |
1472 | /* create a new cctx only suitable as source */ |
1460 | static coro_cctx * |
1473 | static coro_cctx * |
1461 | cctx_new_empty () |
1474 | cctx_new_empty (void) |
1462 | { |
1475 | { |
1463 | coro_cctx *cctx = cctx_new (); |
1476 | coro_cctx *cctx = cctx_new (); |
1464 | |
1477 | |
1465 | cctx->sptr = 0; |
1478 | cctx->sptr = 0; |
1466 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1479 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
… | |
… | |
1468 | return cctx; |
1481 | return cctx; |
1469 | } |
1482 | } |
1470 | |
1483 | |
1471 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1484 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1472 | static coro_cctx * |
1485 | static coro_cctx * |
1473 | cctx_new_run () |
1486 | cctx_new_run (void) |
1474 | { |
1487 | { |
1475 | coro_cctx *cctx = cctx_new (); |
1488 | coro_cctx *cctx = cctx_new (); |
1476 | void *stack_start; |
1489 | void *stack_start; |
1477 | size_t stack_size; |
1490 | size_t stack_size; |
1478 | |
1491 | |
1479 | #if HAVE_MMAP |
1492 | #if HAVE_MMAP |
1480 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1493 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1481 | /* mmap supposedly does allocate-on-write for us */ |
1494 | /* 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); |
1495 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); |
1483 | |
1496 | |
1484 | if (cctx->sptr != (void *)-1) |
1497 | if (cctx->sptr != (void *)-1) |
1485 | { |
1498 | { |
1486 | #if CORO_STACKGUARD |
1499 | #if CORO_STACKGUARD |
1487 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1500 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1789 | TRANSFER (ta, 1); |
1802 | TRANSFER (ta, 1); |
1790 | } |
1803 | } |
1791 | |
1804 | |
1792 | /** Coro ********************************************************************/ |
1805 | /** Coro ********************************************************************/ |
1793 | |
1806 | |
1794 | ECB_INLINE void |
1807 | ecb_inline void |
1795 | coro_enq (pTHX_ struct coro *coro) |
1808 | coro_enq (pTHX_ struct coro *coro) |
1796 | { |
1809 | { |
1797 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1810 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1798 | |
1811 | |
1799 | SvREFCNT_inc_NN (coro->hv); |
1812 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1801 | coro->next_ready = 0; |
1814 | coro->next_ready = 0; |
1802 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1815 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1803 | ready [1] = coro; |
1816 | ready [1] = coro; |
1804 | } |
1817 | } |
1805 | |
1818 | |
1806 | ECB_INLINE struct coro * |
1819 | ecb_inline struct coro * |
1807 | coro_deq (pTHX) |
1820 | coro_deq (pTHX) |
1808 | { |
1821 | { |
1809 | int prio; |
1822 | int prio; |
1810 | |
1823 | |
1811 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1824 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1864 | { |
1877 | { |
1865 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1878 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1866 | } |
1879 | } |
1867 | |
1880 | |
1868 | /* expects to own a reference to next->hv */ |
1881 | /* expects to own a reference to next->hv */ |
1869 | ECB_INLINE void |
1882 | ecb_inline void |
1870 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1883 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1871 | { |
1884 | { |
1872 | SV *prev_sv = SvRV (coro_current); |
1885 | SV *prev_sv = SvRV (coro_current); |
1873 | |
1886 | |
1874 | ta->prev = SvSTATE_hv (prev_sv); |
1887 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1933 | } |
1946 | } |
1934 | } |
1947 | } |
1935 | } |
1948 | } |
1936 | } |
1949 | } |
1937 | |
1950 | |
1938 | ECB_INLINE void |
1951 | ecb_inline void |
1939 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1952 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1940 | { |
1953 | { |
1941 | api_ready (aTHX_ coro_current); |
1954 | api_ready (aTHX_ coro_current); |
1942 | prepare_schedule (aTHX_ ta); |
1955 | prepare_schedule (aTHX_ ta); |
1943 | } |
1956 | } |
1944 | |
1957 | |
1945 | ECB_INLINE void |
1958 | ecb_inline void |
1946 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1959 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1947 | { |
1960 | { |
1948 | SV *prev = SvRV (coro_current); |
1961 | SV *prev = SvRV (coro_current); |
1949 | |
1962 | |
1950 | if (coro_nready) |
1963 | if (coro_nready) |
… | |
… | |
2950 | { |
2963 | { |
2951 | int i; |
2964 | int i; |
2952 | /* if we were woken up but can't down, we look through the whole */ |
2965 | /* 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 */ |
2966 | /* waiters list and only add us if we aren't in there already */ |
2954 | /* this avoids some degenerate memory usage cases */ |
2967 | /* this avoids some degenerate memory usage cases */ |
2955 | for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug |
2968 | for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */ |
2956 | if (AvARRAY (av)[i] == coro_hv) |
2969 | if (AvARRAY (av)[i] == coro_hv) |
2957 | return 1; |
2970 | return 1; |
2958 | |
2971 | |
2959 | av_push (av, SvREFCNT_inc (coro_hv)); |
2972 | av_push (av, SvREFCNT_inc (coro_hv)); |
2960 | return 1; |
2973 | return 1; |
… | |
… | |
3340 | #endif |
3353 | #endif |
3341 | |
3354 | |
3342 | #if CORO_JIT |
3355 | #if CORO_JIT |
3343 | |
3356 | |
3344 | static void ecb_noinline ecb_cold |
3357 | static void ecb_noinline ecb_cold |
3345 | pushav_3uv (pTHX_ UV a, UV b, UV c) |
3358 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
3346 | { |
3359 | { |
3347 | dSP; |
3360 | dSP; |
3348 | AV *av = newAV (); |
3361 | AV *av = newAV (); |
3349 | |
3362 | |
|
|
3363 | av_store (av, 3, newSVuv (d)); |
3350 | av_store (av, 2, newSVuv (c)); |
3364 | av_store (av, 2, newSVuv (c)); |
3351 | av_store (av, 1, newSVuv (b)); |
3365 | av_store (av, 1, newSVuv (b)); |
3352 | av_store (av, 0, newSVuv (a)); |
3366 | av_store (av, 0, newSVuv (a)); |
3353 | |
3367 | |
3354 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
3368 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
… | |
… | |
3361 | { |
3375 | { |
3362 | dSP; |
3376 | dSP; |
3363 | SV *load, *save; |
3377 | SV *load, *save; |
3364 | char *map_base; |
3378 | char *map_base; |
3365 | char *load_ptr, *save_ptr; |
3379 | char *load_ptr, *save_ptr; |
3366 | STRLEN load_len, save_len; |
3380 | STRLEN load_len, save_len, map_len; |
3367 | int count; |
3381 | int count; |
3368 | |
3382 | |
|
|
3383 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3384 | |
3369 | PUSHMARK (SP); |
3385 | PUSHMARK (SP); |
3370 | PUTBACK; |
|
|
3371 | #define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type)); |
3386 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
3372 | # include "state.h" |
3387 | # include "state.h" |
3373 | #undef VARx |
3388 | #undef VARx |
3374 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3389 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3375 | SPAGAIN; |
3390 | SPAGAIN; |
3376 | |
3391 | |
3377 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3392 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3378 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
3393 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
3379 | |
3394 | |
|
|
3395 | map_len = load_len + save_len + 16; |
|
|
3396 | |
3380 | map_base = mmap (0, load_len + save_len + 16, PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3397 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3381 | |
3398 | |
3382 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3399 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3383 | |
3400 | |
3384 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3401 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3385 | memcpy (map_base, load_ptr, load_len); |
3402 | memcpy (map_base, load_ptr, load_len); |
3386 | |
3403 | |
3387 | map_base += (load_len + 15) & ~15; |
3404 | map_base += (load_len + 15) & ~15; |
3388 | |
3405 | |
3389 | save_perl_slots = (load_save_perl_slots_type)map_base; |
3406 | save_perl_slots = (load_save_perl_slots_type)map_base; |
3390 | memcpy (map_base, save_ptr, save_len); |
3407 | memcpy (map_base, save_ptr, save_len); |
|
|
3408 | |
|
|
3409 | /* we are good citizens and try to make the page read-only, so the evil evil */ |
|
|
3410 | /* hackers might have it a bit more difficult */ |
|
|
3411 | mprotect (map_base, map_len, PROT_READ | PROT_EXEC); |
|
|
3412 | |
|
|
3413 | PUTBACK; |
|
|
3414 | eval_pv ("undef &Coro::State::_jit", 1); |
3391 | } |
3415 | } |
3392 | |
3416 | |
3393 | #endif |
3417 | #endif |
3394 | |
3418 | |
3395 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3419 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
… | |
… | |
3463 | time_init (aTHX); |
3487 | time_init (aTHX); |
3464 | |
3488 | |
3465 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3489 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3466 | #if CORO_JIT |
3490 | #if CORO_JIT |
3467 | PUTBACK; |
3491 | PUTBACK; |
3468 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3469 | jit_init (aTHX); |
3492 | jit_init (aTHX); |
3470 | perl_eval_pv ("undef &Coro::State::_jit", 1); |
|
|
3471 | SPAGAIN; |
3493 | SPAGAIN; |
3472 | #endif |
3494 | #endif |
3473 | } |
3495 | } |
3474 | |
3496 | |
3475 | SV * |
3497 | SV * |
… | |
… | |
4127 | } |
4149 | } |
4128 | |
4150 | |
4129 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4151 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4130 | |
4152 | |
4131 | void |
4153 | void |
4132 | _may_delete (SV *sem, int count, int extra_refs) |
4154 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4133 | PPCODE: |
4155 | PPCODE: |
4134 | { |
4156 | { |
4135 | AV *av = (AV *)SvRV (sem); |
4157 | AV *av = (AV *)SvRV (sem); |
4136 | |
4158 | |
4137 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4159 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |