… | |
… | |
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 |
… | |
… | |
97 | # if CORO_PTHREAD |
97 | # if CORO_PTHREAD |
98 | static void *coro_thx; |
98 | static void *coro_thx; |
99 | # endif |
99 | # endif |
100 | #endif |
100 | #endif |
101 | |
101 | |
|
|
102 | /* used in state.h */ |
|
|
103 | #define VAR(name,type) VARx(name, PL_ ## name, type) |
|
|
104 | |
102 | #ifdef __linux |
105 | #ifdef __linux |
103 | # include <time.h> /* for timespec */ |
106 | # include <time.h> /* for timespec */ |
104 | # include <syscall.h> /* for SYS_* */ |
107 | # include <syscall.h> /* for SYS_* */ |
105 | # ifdef SYS_clock_gettime |
108 | # ifdef SYS_clock_gettime |
106 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
109 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
… | |
… | |
204 | }; |
207 | }; |
205 | |
208 | |
206 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
209 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
207 | typedef struct |
210 | typedef struct |
208 | { |
211 | { |
209 | SV *defsv; |
|
|
210 | AV *defav; |
|
|
211 | SV *errsv; |
|
|
212 | SV *irsgv; |
|
|
213 | HV *hinthv; |
|
|
214 | #define VAR(name,type) type name; |
212 | #define VARx(name,expr,type) type name; |
215 | # include "state.h" |
213 | # include "state.h" |
216 | #undef VAR |
214 | #undef VARx |
217 | } perl_slots; |
215 | } perl_slots; |
218 | |
216 | |
219 | // how many context stack entries do we need for perl_slots |
217 | /* how many context stack entries do we need for perl_slots */ |
220 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
218 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
221 | |
219 | |
222 | /* this is a structure representing a perl-level coroutine */ |
220 | /* this is a structure representing a perl-level coroutine */ |
223 | struct coro |
221 | struct coro |
224 | { |
222 | { |
… | |
… | |
290 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
288 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
291 | static CV *cv_coro_run; |
289 | static CV *cv_coro_run; |
292 | static struct coro *coro_first; |
290 | static struct coro *coro_first; |
293 | #define coro_nready coroapi.nready |
291 | #define coro_nready coroapi.nready |
294 | |
292 | |
|
|
293 | /** JIT *********************************************************************/ |
|
|
294 | |
|
|
295 | #if CORO_JIT |
|
|
296 | /* APPLE doesn't have HAVE_MMAP though */ |
|
|
297 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
|
|
298 | #ifndef CORO_JIT_TYPE |
|
|
299 | #if __x86_64 && CORO_JIT_UNIXY |
|
|
300 | #define CORO_JIT_TYPE "amd64-unix" |
|
|
301 | #elif __i386 && CORO_JIT_UNIXY |
|
|
302 | #define CORO_JIT_TYPE "x86-unix" |
|
|
303 | #endif |
|
|
304 | #endif |
|
|
305 | #endif |
|
|
306 | |
|
|
307 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
|
|
308 | #undef CORO_JIT |
|
|
309 | #endif |
|
|
310 | |
|
|
311 | #if CORO_JIT |
|
|
312 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
|
|
313 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
|
|
314 | #endif |
|
|
315 | |
295 | /** Coro::Select ************************************************************/ |
316 | /** Coro::Select ************************************************************/ |
296 | |
317 | |
297 | static OP *(*coro_old_pp_sselect) (pTHX); |
318 | static OP *(*coro_old_pp_sselect) (pTHX); |
298 | static SV *coro_select_select; |
319 | static SV *coro_select_select; |
299 | |
320 | |
… | |
… | |
314 | |
335 | |
315 | /** time stuff **************************************************************/ |
336 | /** time stuff **************************************************************/ |
316 | |
337 | |
317 | #ifdef HAS_GETTIMEOFDAY |
338 | #ifdef HAS_GETTIMEOFDAY |
318 | |
339 | |
319 | ECB_INLINE void |
340 | ecb_inline void |
320 | coro_u2time (pTHX_ UV ret[2]) |
341 | coro_u2time (pTHX_ UV ret[2]) |
321 | { |
342 | { |
322 | struct timeval tv; |
343 | struct timeval tv; |
323 | gettimeofday (&tv, 0); |
344 | gettimeofday (&tv, 0); |
324 | |
345 | |
325 | ret [0] = tv.tv_sec; |
346 | ret [0] = tv.tv_sec; |
326 | ret [1] = tv.tv_usec; |
347 | ret [1] = tv.tv_usec; |
327 | } |
348 | } |
328 | |
349 | |
329 | ECB_INLINE double |
350 | ecb_inline double |
330 | coro_nvtime () |
351 | coro_nvtime (void) |
331 | { |
352 | { |
332 | struct timeval tv; |
353 | struct timeval tv; |
333 | gettimeofday (&tv, 0); |
354 | gettimeofday (&tv, 0); |
334 | |
355 | |
335 | return tv.tv_sec + tv.tv_usec * 1e-6; |
356 | return tv.tv_sec + tv.tv_usec * 1e-6; |
336 | } |
357 | } |
337 | |
358 | |
338 | ECB_INLINE void |
359 | ecb_inline void |
339 | time_init (pTHX) |
360 | time_init (pTHX) |
340 | { |
361 | { |
341 | nvtime = coro_nvtime; |
362 | nvtime = coro_nvtime; |
342 | u2time = coro_u2time; |
363 | u2time = coro_u2time; |
343 | } |
364 | } |
344 | |
365 | |
345 | #else |
366 | #else |
346 | |
367 | |
347 | ECB_INLINE void |
368 | ecb_inline void |
348 | time_init (pTHX) |
369 | time_init (pTHX) |
349 | { |
370 | { |
350 | SV **svp; |
371 | SV **svp; |
351 | |
372 | |
352 | require_pv ("Time/HiRes.pm"); |
373 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
394 | get_hv (name, create); |
415 | get_hv (name, create); |
395 | #endif |
416 | #endif |
396 | return get_hv (name, create); |
417 | return get_hv (name, create); |
397 | } |
418 | } |
398 | |
419 | |
399 | ECB_INLINE void |
420 | ecb_inline void |
400 | coro_times_update () |
421 | coro_times_update (void) |
401 | { |
422 | { |
402 | #ifdef coro_clock_gettime |
423 | #ifdef coro_clock_gettime |
403 | struct timespec ts; |
424 | struct timespec ts; |
404 | |
425 | |
405 | ts.tv_sec = ts.tv_nsec = 0; |
426 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
417 | time_real [0] = tv [0]; |
438 | time_real [0] = tv [0]; |
418 | time_real [1] = tv [1] * 1000; |
439 | time_real [1] = tv [1] * 1000; |
419 | #endif |
440 | #endif |
420 | } |
441 | } |
421 | |
442 | |
422 | ECB_INLINE void |
443 | ecb_inline void |
423 | coro_times_add (struct coro *c) |
444 | coro_times_add (struct coro *c) |
424 | { |
445 | { |
425 | c->t_real [1] += time_real [1]; |
446 | c->t_real [1] += time_real [1]; |
426 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
447 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
427 | c->t_real [0] += time_real [0]; |
448 | c->t_real [0] += time_real [0]; |
… | |
… | |
429 | c->t_cpu [1] += time_cpu [1]; |
450 | c->t_cpu [1] += time_cpu [1]; |
430 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
451 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
431 | c->t_cpu [0] += time_cpu [0]; |
452 | c->t_cpu [0] += time_cpu [0]; |
432 | } |
453 | } |
433 | |
454 | |
434 | ECB_INLINE void |
455 | ecb_inline void |
435 | coro_times_sub (struct coro *c) |
456 | coro_times_sub (struct coro *c) |
436 | { |
457 | { |
437 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
458 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
438 | c->t_real [1] -= time_real [1]; |
459 | c->t_real [1] -= time_real [1]; |
439 | c->t_real [0] -= time_real [0]; |
460 | c->t_real [0] -= time_real [0]; |
… | |
… | |
460 | : 0) |
481 | : 0) |
461 | |
482 | |
462 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
483 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
463 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
484 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
464 | |
485 | |
465 | ECB_INLINE MAGIC * |
486 | ecb_inline MAGIC * |
466 | SvSTATEhv_p (pTHX_ SV *coro) |
487 | SvSTATEhv_p (pTHX_ SV *coro) |
467 | { |
488 | { |
468 | MAGIC *mg; |
489 | MAGIC *mg; |
469 | |
490 | |
470 | if (ecb_expect_true ( |
491 | if (ecb_expect_true ( |
… | |
… | |
475 | return mg; |
496 | return mg; |
476 | |
497 | |
477 | return 0; |
498 | return 0; |
478 | } |
499 | } |
479 | |
500 | |
480 | ECB_INLINE struct coro * |
501 | ecb_inline struct coro * |
481 | SvSTATE_ (pTHX_ SV *coro) |
502 | SvSTATE_ (pTHX_ SV *coro) |
482 | { |
503 | { |
483 | MAGIC *mg; |
504 | MAGIC *mg; |
484 | |
505 | |
485 | if (SvROK (coro)) |
506 | if (SvROK (coro)) |
… | |
… | |
499 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
520 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
500 | |
521 | |
501 | /*****************************************************************************/ |
522 | /*****************************************************************************/ |
502 | /* padlist management and caching */ |
523 | /* padlist management and caching */ |
503 | |
524 | |
504 | ECB_INLINE AV * |
525 | ecb_inline AV * |
505 | coro_derive_padlist (pTHX_ CV *cv) |
526 | coro_derive_padlist (pTHX_ CV *cv) |
506 | { |
527 | { |
507 | AV *padlist = CvPADLIST (cv); |
528 | AV *padlist = CvPADLIST (cv); |
508 | AV *newpadlist, *newpad; |
529 | AV *newpadlist, *newpad; |
509 | |
530 | |
… | |
… | |
521 | av_store (newpadlist, 1, (SV *)newpad); |
542 | av_store (newpadlist, 1, (SV *)newpad); |
522 | |
543 | |
523 | return newpadlist; |
544 | return newpadlist; |
524 | } |
545 | } |
525 | |
546 | |
526 | ECB_INLINE void |
547 | ecb_inline void |
527 | free_padlist (pTHX_ AV *padlist) |
548 | free_padlist (pTHX_ AV *padlist) |
528 | { |
549 | { |
529 | /* may be during global destruction */ |
550 | /* may be during global destruction */ |
530 | if (!IN_DESTRUCT) |
551 | if (!IN_DESTRUCT) |
531 | { |
552 | { |
… | |
… | |
574 | 0, 0, 0, 0, |
595 | 0, 0, 0, 0, |
575 | coro_cv_free |
596 | coro_cv_free |
576 | }; |
597 | }; |
577 | |
598 | |
578 | /* the next two functions merely cache the padlists */ |
599 | /* the next two functions merely cache the padlists */ |
579 | ECB_INLINE void |
600 | ecb_inline void |
580 | get_padlist (pTHX_ CV *cv) |
601 | get_padlist (pTHX_ CV *cv) |
581 | { |
602 | { |
582 | MAGIC *mg = CORO_MAGIC_cv (cv); |
603 | MAGIC *mg = CORO_MAGIC_cv (cv); |
583 | AV *av; |
604 | AV *av; |
584 | |
605 | |
… | |
… | |
597 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
618 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
598 | #endif |
619 | #endif |
599 | } |
620 | } |
600 | } |
621 | } |
601 | |
622 | |
602 | ECB_INLINE void |
623 | ecb_inline void |
603 | put_padlist (pTHX_ CV *cv) |
624 | put_padlist (pTHX_ CV *cv) |
604 | { |
625 | { |
605 | MAGIC *mg = CORO_MAGIC_cv (cv); |
626 | MAGIC *mg = CORO_MAGIC_cv (cv); |
606 | AV *av; |
627 | AV *av; |
607 | |
628 | |
… | |
… | |
678 | perl_slots *slot = c->slot; |
699 | perl_slots *slot = c->slot; |
679 | c->slot = 0; |
700 | c->slot = 0; |
680 | |
701 | |
681 | PL_mainstack = c->mainstack; |
702 | PL_mainstack = c->mainstack; |
682 | |
703 | |
683 | GvSV (PL_defgv) = slot->defsv; |
704 | #if CORO_JIT |
684 | GvAV (PL_defgv) = slot->defav; |
705 | load_perl_slots (slot); |
685 | GvSV (PL_errgv) = slot->errsv; |
706 | #else |
686 | GvSV (irsgv) = slot->irsgv; |
|
|
687 | GvHV (PL_hintgv) = slot->hinthv; |
|
|
688 | |
|
|
689 | #define VAR(name,type) PL_ ## name = slot->name; |
707 | #define VARx(name,expr,type) expr = slot->name; |
690 | # include "state.h" |
708 | # include "state.h" |
691 | #undef VAR |
709 | #undef VARx |
|
|
710 | #endif |
692 | |
711 | |
693 | { |
712 | { |
694 | dSP; |
713 | dSP; |
695 | |
714 | |
696 | CV *cv; |
715 | CV *cv; |
… | |
… | |
798 | |
817 | |
799 | PUTBACK; |
818 | PUTBACK; |
800 | } |
819 | } |
801 | |
820 | |
802 | /* allocate some space on the context stack for our purposes */ |
821 | /* allocate some space on the context stack for our purposes */ |
803 | if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
822 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
804 | { |
823 | { |
805 | unsigned int i; |
824 | unsigned int i; |
806 | |
825 | |
807 | for (i = 0; i < SLOT_COUNT; ++i) |
826 | for (i = 0; i < SLOT_COUNT; ++i) |
808 | CXINC; |
827 | CXINC; |
… | |
… | |
813 | c->mainstack = PL_mainstack; |
832 | c->mainstack = PL_mainstack; |
814 | |
833 | |
815 | { |
834 | { |
816 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
835 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
817 | |
836 | |
818 | slot->defav = GvAV (PL_defgv); |
837 | #if CORO_JIT |
819 | slot->defsv = DEFSV; |
838 | save_perl_slots (slot); |
820 | slot->errsv = ERRSV; |
839 | #else |
821 | slot->irsgv = GvSV (irsgv); |
|
|
822 | slot->hinthv = GvHV (PL_hintgv); |
|
|
823 | |
|
|
824 | #define VAR(name,type) slot->name = PL_ ## name; |
840 | #define VARx(name,expr,type) slot->name = expr; |
825 | # include "state.h" |
841 | # include "state.h" |
826 | #undef VAR |
842 | #undef VARx |
|
|
843 | #endif |
827 | } |
844 | } |
828 | } |
845 | } |
829 | |
846 | |
830 | /* |
847 | /* |
831 | * allocate various perl stacks. This is almost an exact copy |
848 | * allocate various perl stacks. This is almost an exact copy |
… | |
… | |
1173 | /* restore swapped sv's */ |
1190 | /* restore swapped sv's */ |
1174 | SWAP_SVS (coro); |
1191 | SWAP_SVS (coro); |
1175 | |
1192 | |
1176 | coro_destruct_stacks (aTHX); |
1193 | coro_destruct_stacks (aTHX); |
1177 | |
1194 | |
1178 | // now save some sv's to be free'd later |
1195 | /* now save some sv's to be free'd later */ |
1179 | svf [0] = GvSV (PL_defgv); |
1196 | svf [0] = GvSV (PL_defgv); |
1180 | svf [1] = (SV *)GvAV (PL_defgv); |
1197 | svf [1] = (SV *)GvAV (PL_defgv); |
1181 | svf [2] = GvSV (PL_errgv); |
1198 | svf [2] = GvSV (PL_errgv); |
1182 | svf [3] = (SV *)PL_defoutgv; |
1199 | svf [3] = (SV *)PL_defoutgv; |
1183 | svf [4] = PL_rs; |
1200 | svf [4] = PL_rs; |
… | |
… | |
1203 | SvREFCNT_dec (coro->invoke_cb); |
1220 | SvREFCNT_dec (coro->invoke_cb); |
1204 | SvREFCNT_dec (coro->invoke_av); |
1221 | SvREFCNT_dec (coro->invoke_av); |
1205 | } |
1222 | } |
1206 | } |
1223 | } |
1207 | |
1224 | |
1208 | ECB_INLINE void |
1225 | ecb_inline void |
1209 | free_coro_mortal (pTHX) |
1226 | free_coro_mortal (pTHX) |
1210 | { |
1227 | { |
1211 | if (ecb_expect_true (coro_mortal)) |
1228 | if (ecb_expect_true (coro_mortal)) |
1212 | { |
1229 | { |
1213 | SvREFCNT_dec ((SV *)coro_mortal); |
1230 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1372 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1389 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1373 | slf_frame.check = slf_check_set_stacklevel; |
1390 | slf_frame.check = slf_check_set_stacklevel; |
1374 | } |
1391 | } |
1375 | |
1392 | |
1376 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1393 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1377 | ECB_INLINE void |
1394 | ecb_inline void |
1378 | transfer_tail (pTHX) |
1395 | transfer_tail (pTHX) |
1379 | { |
1396 | { |
1380 | free_coro_mortal (aTHX); |
1397 | free_coro_mortal (aTHX); |
1381 | } |
1398 | } |
1382 | |
1399 | |
… | |
… | |
1426 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1443 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1427 | } |
1444 | } |
1428 | } |
1445 | } |
1429 | |
1446 | |
1430 | static coro_cctx * |
1447 | static coro_cctx * |
1431 | cctx_new () |
1448 | cctx_new (void) |
1432 | { |
1449 | { |
1433 | coro_cctx *cctx; |
1450 | coro_cctx *cctx; |
1434 | |
1451 | |
1435 | ++cctx_count; |
1452 | ++cctx_count; |
1436 | New (0, cctx, 1, coro_cctx); |
1453 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1442 | return cctx; |
1459 | return cctx; |
1443 | } |
1460 | } |
1444 | |
1461 | |
1445 | /* create a new cctx only suitable as source */ |
1462 | /* create a new cctx only suitable as source */ |
1446 | static coro_cctx * |
1463 | static coro_cctx * |
1447 | cctx_new_empty () |
1464 | cctx_new_empty (void) |
1448 | { |
1465 | { |
1449 | coro_cctx *cctx = cctx_new (); |
1466 | coro_cctx *cctx = cctx_new (); |
1450 | |
1467 | |
1451 | cctx->sptr = 0; |
1468 | cctx->sptr = 0; |
1452 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1469 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
… | |
… | |
1454 | return cctx; |
1471 | return cctx; |
1455 | } |
1472 | } |
1456 | |
1473 | |
1457 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1474 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1458 | static coro_cctx * |
1475 | static coro_cctx * |
1459 | cctx_new_run () |
1476 | cctx_new_run (void) |
1460 | { |
1477 | { |
1461 | coro_cctx *cctx = cctx_new (); |
1478 | coro_cctx *cctx = cctx_new (); |
1462 | void *stack_start; |
1479 | void *stack_start; |
1463 | size_t stack_size; |
1480 | size_t stack_size; |
1464 | |
1481 | |
1465 | #if HAVE_MMAP |
1482 | #if HAVE_MMAP |
1466 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1483 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1467 | /* mmap supposedly does allocate-on-write for us */ |
1484 | /* mmap supposedly does allocate-on-write for us */ |
1468 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1485 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); |
1469 | |
1486 | |
1470 | if (cctx->sptr != (void *)-1) |
1487 | if (cctx->sptr != (void *)-1) |
1471 | { |
1488 | { |
1472 | #if CORO_STACKGUARD |
1489 | #if CORO_STACKGUARD |
1473 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1490 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1775 | TRANSFER (ta, 1); |
1792 | TRANSFER (ta, 1); |
1776 | } |
1793 | } |
1777 | |
1794 | |
1778 | /** Coro ********************************************************************/ |
1795 | /** Coro ********************************************************************/ |
1779 | |
1796 | |
1780 | ECB_INLINE void |
1797 | ecb_inline void |
1781 | coro_enq (pTHX_ struct coro *coro) |
1798 | coro_enq (pTHX_ struct coro *coro) |
1782 | { |
1799 | { |
1783 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1800 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1784 | |
1801 | |
1785 | SvREFCNT_inc_NN (coro->hv); |
1802 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1787 | coro->next_ready = 0; |
1804 | coro->next_ready = 0; |
1788 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1805 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1789 | ready [1] = coro; |
1806 | ready [1] = coro; |
1790 | } |
1807 | } |
1791 | |
1808 | |
1792 | ECB_INLINE struct coro * |
1809 | ecb_inline struct coro * |
1793 | coro_deq (pTHX) |
1810 | coro_deq (pTHX) |
1794 | { |
1811 | { |
1795 | int prio; |
1812 | int prio; |
1796 | |
1813 | |
1797 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1814 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1850 | { |
1867 | { |
1851 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1868 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1852 | } |
1869 | } |
1853 | |
1870 | |
1854 | /* expects to own a reference to next->hv */ |
1871 | /* expects to own a reference to next->hv */ |
1855 | ECB_INLINE void |
1872 | ecb_inline void |
1856 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1873 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1857 | { |
1874 | { |
1858 | SV *prev_sv = SvRV (coro_current); |
1875 | SV *prev_sv = SvRV (coro_current); |
1859 | |
1876 | |
1860 | ta->prev = SvSTATE_hv (prev_sv); |
1877 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1919 | } |
1936 | } |
1920 | } |
1937 | } |
1921 | } |
1938 | } |
1922 | } |
1939 | } |
1923 | |
1940 | |
1924 | ECB_INLINE void |
1941 | ecb_inline void |
1925 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1942 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1926 | { |
1943 | { |
1927 | api_ready (aTHX_ coro_current); |
1944 | api_ready (aTHX_ coro_current); |
1928 | prepare_schedule (aTHX_ ta); |
1945 | prepare_schedule (aTHX_ ta); |
1929 | } |
1946 | } |
1930 | |
1947 | |
1931 | ECB_INLINE void |
1948 | ecb_inline void |
1932 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1949 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1933 | { |
1950 | { |
1934 | SV *prev = SvRV (coro_current); |
1951 | SV *prev = SvRV (coro_current); |
1935 | |
1952 | |
1936 | if (coro_nready) |
1953 | if (coro_nready) |
… | |
… | |
2936 | { |
2953 | { |
2937 | int i; |
2954 | int i; |
2938 | /* if we were woken up but can't down, we look through the whole */ |
2955 | /* if we were woken up but can't down, we look through the whole */ |
2939 | /* waiters list and only add us if we aren't in there already */ |
2956 | /* waiters list and only add us if we aren't in there already */ |
2940 | /* this avoids some degenerate memory usage cases */ |
2957 | /* this avoids some degenerate memory usage cases */ |
2941 | for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug |
2958 | for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */ |
2942 | if (AvARRAY (av)[i] == coro_hv) |
2959 | if (AvARRAY (av)[i] == coro_hv) |
2943 | return 1; |
2960 | return 1; |
2944 | |
2961 | |
2945 | av_push (av, SvREFCNT_inc (coro_hv)); |
2962 | av_push (av, SvREFCNT_inc (coro_hv)); |
2946 | return 1; |
2963 | return 1; |
… | |
… | |
3323 | |
3340 | |
3324 | #ifndef __cplusplus |
3341 | #ifndef __cplusplus |
3325 | ecb_cold XS(boot_Coro__State); |
3342 | ecb_cold XS(boot_Coro__State); |
3326 | #endif |
3343 | #endif |
3327 | |
3344 | |
|
|
3345 | #if CORO_JIT |
|
|
3346 | |
|
|
3347 | static void ecb_noinline ecb_cold |
|
|
3348 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
|
|
3349 | { |
|
|
3350 | dSP; |
|
|
3351 | AV *av = newAV (); |
|
|
3352 | |
|
|
3353 | av_store (av, 3, newSVuv (d)); |
|
|
3354 | av_store (av, 2, newSVuv (c)); |
|
|
3355 | av_store (av, 1, newSVuv (b)); |
|
|
3356 | av_store (av, 0, newSVuv (a)); |
|
|
3357 | |
|
|
3358 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
3359 | |
|
|
3360 | PUTBACK; |
|
|
3361 | } |
|
|
3362 | |
|
|
3363 | static void ecb_noinline ecb_cold |
|
|
3364 | jit_init (pTHX) |
|
|
3365 | { |
|
|
3366 | dSP; |
|
|
3367 | SV *load, *save; |
|
|
3368 | char *map_base; |
|
|
3369 | char *load_ptr, *save_ptr; |
|
|
3370 | STRLEN load_len, save_len, map_len; |
|
|
3371 | int count; |
|
|
3372 | |
|
|
3373 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3374 | |
|
|
3375 | PUSHMARK (SP); |
|
|
3376 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
|
|
3377 | # include "state.h" |
|
|
3378 | #undef VARx |
|
|
3379 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
|
|
3380 | SPAGAIN; |
|
|
3381 | |
|
|
3382 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
|
|
3383 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
|
|
3384 | |
|
|
3385 | map_len = load_len + save_len + 16; |
|
|
3386 | |
|
|
3387 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
|
|
3388 | |
|
|
3389 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
|
|
3390 | |
|
|
3391 | load_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3392 | memcpy (map_base, load_ptr, load_len); |
|
|
3393 | |
|
|
3394 | map_base += (load_len + 15) & ~15; |
|
|
3395 | |
|
|
3396 | save_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3397 | memcpy (map_base, save_ptr, save_len); |
|
|
3398 | |
|
|
3399 | /* we are good citizens and try to make the page read-only, so the evil evil */ |
|
|
3400 | /* hackers might have it a bit more difficult */ |
|
|
3401 | mprotect (map_base, map_len, PROT_READ | PROT_EXEC); |
|
|
3402 | |
|
|
3403 | PUTBACK; |
|
|
3404 | eval_pv ("undef &Coro::State::_jit", 1); |
|
|
3405 | } |
|
|
3406 | |
|
|
3407 | #endif |
|
|
3408 | |
3328 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3409 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3329 | |
3410 | |
3330 | PROTOTYPES: DISABLE |
3411 | PROTOTYPES: DISABLE |
3331 | |
3412 | |
3332 | BOOT: |
3413 | BOOT: |
… | |
… | |
3336 | coro_thx = PERL_GET_CONTEXT; |
3417 | coro_thx = PERL_GET_CONTEXT; |
3337 | # endif |
3418 | # endif |
3338 | #endif |
3419 | #endif |
3339 | BOOT_PAGESIZE; |
3420 | BOOT_PAGESIZE; |
3340 | |
3421 | |
|
|
3422 | /* perl defines these to check for existance first, but why it doesn't */ |
|
|
3423 | /* just create them one at init time is not clear to me, except for */ |
|
|
3424 | /* programs trying to delete them, but... */ |
|
|
3425 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
|
|
3426 | DEFSV; |
|
|
3427 | ERRSV; |
|
|
3428 | |
3341 | cctx_current = cctx_new_empty (); |
3429 | cctx_current = cctx_new_empty (); |
3342 | |
3430 | |
3343 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3431 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3344 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3432 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3345 | |
3433 | |
… | |
… | |
3387 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3475 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3388 | |
3476 | |
3389 | time_init (aTHX); |
3477 | time_init (aTHX); |
3390 | |
3478 | |
3391 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3479 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
|
|
3480 | #if CORO_JIT |
|
|
3481 | PUTBACK; |
|
|
3482 | jit_init (aTHX); |
|
|
3483 | SPAGAIN; |
|
|
3484 | #endif |
3392 | } |
3485 | } |
3393 | |
3486 | |
3394 | SV * |
3487 | SV * |
3395 | new (SV *klass, ...) |
3488 | new (SV *klass, ...) |
3396 | ALIAS: |
3489 | ALIAS: |
… | |
… | |
4046 | } |
4139 | } |
4047 | |
4140 | |
4048 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4141 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4049 | |
4142 | |
4050 | void |
4143 | void |
4051 | _may_delete (SV *sem, int count, int extra_refs) |
4144 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4052 | PPCODE: |
4145 | PPCODE: |
4053 | { |
4146 | { |
4054 | AV *av = (AV *)SvRV (sem); |
4147 | AV *av = (AV *)SvRV (sem); |
4055 | |
4148 | |
4056 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4149 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |