… | |
… | |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "libcoro/coro.c" |
5 | #include "libcoro/coro.c" |
6 | |
6 | |
|
|
7 | #include <signal.h> |
|
|
8 | |
7 | #ifdef HAVE_MMAP |
9 | #ifdef HAVE_MMAP |
8 | # include <unistd.h> |
10 | # include <unistd.h> |
9 | # include <sys/mman.h> |
11 | # include <sys/mman.h> |
|
|
12 | # ifndef MAP_ANON |
|
|
13 | # ifdef MAP_ANONYMOUS |
|
|
14 | # define MAP_ANON MAP_ANONYMOUS |
|
|
15 | # else |
|
|
16 | # undef HAVE_MMAP |
|
|
17 | # endif |
|
|
18 | # endif |
10 | #endif |
19 | #endif |
11 | |
20 | |
12 | #define MAY_FLUSH /* increases codesize */ |
21 | #define MAY_FLUSH /* increases codesize */ |
13 | |
|
|
14 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
|
|
15 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
|
|
16 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
|
|
17 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
|
|
18 | |
|
|
19 | #define TRANSFER_SAVE_ALL -1 |
|
|
20 | |
22 | |
21 | #define SUB_INIT "Coro::State::initialize" |
23 | #define SUB_INIT "Coro::State::initialize" |
22 | #define UCORO_STATE "_coro_state" |
24 | #define UCORO_STATE "_coro_state" |
23 | |
25 | |
|
|
26 | /* The next macro should delcare a variable stacklevel that contains and approximation |
|
|
27 | * to the current C stack pointer. Its property is that it changes with each call |
|
|
28 | * and should be unique. */ |
|
|
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
|
|
30 | |
|
|
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
32 | |
|
|
33 | #include "CoroAPI.h" |
|
|
34 | |
|
|
35 | static struct CoroAPI coroapi; |
|
|
36 | |
|
|
37 | /* this is actually not only the c stack but also c registers etc... */ |
|
|
38 | typedef struct { |
|
|
39 | int refcnt; /* pointer reference counter */ |
|
|
40 | int usecnt; /* shared by how many coroutines */ |
|
|
41 | int gencnt; /* generation counter */ |
|
|
42 | |
|
|
43 | coro_context cctx; |
|
|
44 | |
|
|
45 | void *sptr; |
|
|
46 | long ssize; /* positive == mmap, otherwise malloc */ |
|
|
47 | } coro_stack; |
|
|
48 | |
24 | struct coro { |
49 | struct coro { |
25 | /* the optional C context */ |
50 | /* the optional C context */ |
26 | coro_context cctx; |
51 | coro_stack *stack; |
27 | void *sptr; |
52 | void *cursp; |
28 | long ssize; |
53 | int gencnt; |
29 | |
54 | |
30 | /* optionally saved, might be zero */ |
55 | /* optionally saved, might be zero */ |
31 | AV *defav; |
56 | AV *defav; |
32 | SV *defsv; |
57 | SV *defsv; |
33 | SV *errsv; |
58 | SV *errsv; |
34 | |
59 | |
35 | /* saved global state not related to stacks */ |
60 | /* saved global state not related to stacks */ |
36 | U8 dowarn; |
61 | U8 dowarn; |
|
|
62 | I32 in_eval; |
37 | |
63 | |
38 | /* the stacks and related info (callchain etc..) */ |
64 | /* the stacks and related info (callchain etc..) */ |
39 | PERL_SI *curstackinfo; |
65 | PERL_SI *curstackinfo; |
40 | AV *curstack; |
66 | AV *curstack; |
41 | AV *mainstack; |
67 | AV *mainstack; |
… | |
… | |
59 | I32 savestack_max; |
85 | I32 savestack_max; |
60 | OP **retstack; |
86 | OP **retstack; |
61 | I32 retstack_ix; |
87 | I32 retstack_ix; |
62 | I32 retstack_max; |
88 | I32 retstack_max; |
63 | COP *curcop; |
89 | COP *curcop; |
64 | JMPENV start_env; |
|
|
65 | JMPENV *top_env; |
90 | JMPENV *top_env; |
66 | |
91 | |
67 | /* data associated with this coroutine (initial args) */ |
92 | /* data associated with this coroutine (initial args) */ |
68 | AV *args; |
93 | AV *args; |
69 | }; |
94 | }; |
… | |
… | |
254 | |
279 | |
255 | static void |
280 | static void |
256 | load_state(pTHX_ Coro__State c) |
281 | load_state(pTHX_ Coro__State c) |
257 | { |
282 | { |
258 | PL_dowarn = c->dowarn; |
283 | PL_dowarn = c->dowarn; |
|
|
284 | PL_in_eval = c->in_eval; |
259 | |
285 | |
260 | PL_curstackinfo = c->curstackinfo; |
286 | PL_curstackinfo = c->curstackinfo; |
261 | PL_curstack = c->curstack; |
287 | PL_curstack = c->curstack; |
262 | PL_mainstack = c->mainstack; |
288 | PL_mainstack = c->mainstack; |
263 | PL_stack_sp = c->stack_sp; |
289 | PL_stack_sp = c->stack_sp; |
… | |
… | |
280 | PL_savestack_max = c->savestack_max; |
306 | PL_savestack_max = c->savestack_max; |
281 | PL_retstack = c->retstack; |
307 | PL_retstack = c->retstack; |
282 | PL_retstack_ix = c->retstack_ix; |
308 | PL_retstack_ix = c->retstack_ix; |
283 | PL_retstack_max = c->retstack_max; |
309 | PL_retstack_max = c->retstack_max; |
284 | PL_curcop = c->curcop; |
310 | PL_curcop = c->curcop; |
285 | PL_start_env = c->start_env; |
|
|
286 | PL_top_env = c->top_env; |
311 | PL_top_env = c->top_env; |
287 | |
312 | |
288 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
313 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
289 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
314 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
290 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
315 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
389 | /* but if it's missing the defav contents magically get replaced sometimes */ |
414 | /* but if it's missing the defav contents magically get replaced sometimes */ |
390 | if (c->defav) |
415 | if (c->defav) |
391 | av_reify (c->defav); |
416 | av_reify (c->defav); |
392 | |
417 | |
393 | c->dowarn = PL_dowarn; |
418 | c->dowarn = PL_dowarn; |
|
|
419 | c->in_eval = PL_in_eval; |
394 | |
420 | |
395 | c->curstackinfo = PL_curstackinfo; |
421 | c->curstackinfo = PL_curstackinfo; |
396 | c->curstack = PL_curstack; |
422 | c->curstack = PL_curstack; |
397 | c->mainstack = PL_mainstack; |
423 | c->mainstack = PL_mainstack; |
398 | c->stack_sp = PL_stack_sp; |
424 | c->stack_sp = PL_stack_sp; |
… | |
… | |
415 | c->savestack_max = PL_savestack_max; |
441 | c->savestack_max = PL_savestack_max; |
416 | c->retstack = PL_retstack; |
442 | c->retstack = PL_retstack; |
417 | c->retstack_ix = PL_retstack_ix; |
443 | c->retstack_ix = PL_retstack_ix; |
418 | c->retstack_max = PL_retstack_max; |
444 | c->retstack_max = PL_retstack_max; |
419 | c->curcop = PL_curcop; |
445 | c->curcop = PL_curcop; |
420 | c->start_env = PL_start_env; |
|
|
421 | c->top_env = PL_top_env; |
446 | c->top_env = PL_top_env; |
422 | } |
447 | } |
423 | |
448 | |
424 | /* |
449 | /* |
425 | * allocate various perl stacks. This is an exact copy |
450 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
437 | |
462 | |
438 | PL_stack_base = AvARRAY(PL_curstack); |
463 | PL_stack_base = AvARRAY(PL_curstack); |
439 | PL_stack_sp = PL_stack_base; |
464 | PL_stack_sp = PL_stack_base; |
440 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
465 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
441 | |
466 | |
442 | New(50,PL_tmps_stack,64,SV*); |
467 | New(50,PL_tmps_stack,96,SV*); |
443 | PL_tmps_floor = -1; |
468 | PL_tmps_floor = -1; |
444 | PL_tmps_ix = -1; |
469 | PL_tmps_ix = -1; |
445 | PL_tmps_max = 64; |
470 | PL_tmps_max = 96; |
446 | |
471 | |
447 | New(54,PL_markstack,12,I32); |
472 | New(54,PL_markstack,16,I32); |
448 | PL_markstack_ptr = PL_markstack; |
473 | PL_markstack_ptr = PL_markstack; |
449 | PL_markstack_max = PL_markstack + 12; |
474 | PL_markstack_max = PL_markstack + 16; |
450 | |
475 | |
451 | SET_MARK_OFFSET; |
476 | SET_MARK_OFFSET; |
452 | |
477 | |
453 | New(54,PL_scopestack,12,I32); |
478 | New(54,PL_scopestack,16,I32); |
454 | PL_scopestack_ix = 0; |
479 | PL_scopestack_ix = 0; |
455 | PL_scopestack_max = 12; |
480 | PL_scopestack_max = 16; |
456 | |
481 | |
457 | New(54,PL_savestack,64,ANY); |
482 | New(54,PL_savestack,96,ANY); |
458 | PL_savestack_ix = 0; |
483 | PL_savestack_ix = 0; |
459 | PL_savestack_max = 64; |
484 | PL_savestack_max = 96; |
460 | |
485 | |
461 | New(54,PL_retstack,8,OP*); |
486 | New(54,PL_retstack,8,OP*); |
462 | PL_retstack_ix = 0; |
487 | PL_retstack_ix = 0; |
463 | PL_retstack_max = 8; |
488 | PL_retstack_max = 8; |
464 | } |
489 | } |
… | |
… | |
505 | Safefree(PL_savestack); |
530 | Safefree(PL_savestack); |
506 | Safefree(PL_retstack); |
531 | Safefree(PL_retstack); |
507 | } |
532 | } |
508 | |
533 | |
509 | static void |
534 | static void |
510 | allocate_stack (Coro__State ctx) |
535 | allocate_stack (Coro__State ctx, int alloc) |
511 | { |
536 | { |
|
|
537 | coro_stack *stack; |
|
|
538 | |
|
|
539 | New (0, stack, 1, coro_stack); |
|
|
540 | |
|
|
541 | stack->refcnt = 1; |
|
|
542 | stack->usecnt = 1; |
|
|
543 | stack->gencnt = ctx->gencnt = 0; |
|
|
544 | if (alloc) |
|
|
545 | { |
512 | #ifdef HAVE_MMAP |
546 | #ifdef HAVE_MMAP |
513 | ctx->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ |
547 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
514 | ctx->sptr = mmap (0, ctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
548 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
515 | if (ctx->sptr == (void *)-1) |
549 | if (stack->sptr == (void *)-1) |
516 | #endif |
550 | #endif |
517 | { |
551 | { |
518 | /*FIXME*//*D*//* reasonable stack size! */ |
552 | /*FIXME*//*D*//* reasonable stack size! */ |
519 | ctx->ssize = 4096 * sizeof (long); |
553 | stack->ssize = -4096 * sizeof (long); |
520 | New (0, ctx->sptr, 4096, long); |
554 | New (0, stack->sptr, 4096, long); |
|
|
555 | } |
521 | } |
556 | } |
|
|
557 | else |
|
|
558 | stack->sptr = 0; |
|
|
559 | |
|
|
560 | ctx->stack = stack; |
522 | } |
561 | } |
523 | |
562 | |
524 | static void |
563 | static void |
525 | deallocate_stack (Coro__State ctx) |
564 | deallocate_stack (Coro__State ctx) |
526 | { |
565 | { |
|
|
566 | coro_stack *stack = ctx->stack; |
|
|
567 | |
|
|
568 | ctx->stack = 0; |
|
|
569 | |
|
|
570 | if (stack) |
|
|
571 | { |
|
|
572 | if (!--stack->refcnt) |
|
|
573 | { |
527 | #ifdef HAVE_MMAP |
574 | #ifdef HAVE_MMAP |
528 | munmap (ctx->sptr, ctx->ssize); |
575 | if (stack->ssize > 0 && stack->sptr) |
|
|
576 | munmap (stack->sptr, stack->ssize); |
|
|
577 | else |
529 | #else |
578 | #else |
530 | Safefree (ctx->sptr); |
579 | Safefree (stack->sptr); |
531 | #endif |
580 | #endif |
|
|
581 | Safefree (stack); |
|
|
582 | } |
|
|
583 | else if (ctx->gencnt == stack->gencnt) |
|
|
584 | --stack->usecnt; |
|
|
585 | } |
532 | } |
586 | } |
533 | |
587 | |
534 | /* might go away together with optional SAVE_CCTXT */ |
|
|
535 | static void |
588 | static void |
536 | setup_coro (void *arg) |
589 | setup_coro (void *arg) |
537 | { |
590 | { |
538 | /* |
591 | /* |
539 | * emulate part of the perl startup here. |
592 | * emulate part of the perl startup here. |
… | |
… | |
541 | dSP; |
594 | dSP; |
542 | Coro__State ctx = (Coro__State)arg; |
595 | Coro__State ctx = (Coro__State)arg; |
543 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
596 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
544 | |
597 | |
545 | coro_init_stacks (aTHX); |
598 | coro_init_stacks (aTHX); |
546 | JMPENV_BOOTSTRAP; |
|
|
547 | SPAGAIN; |
|
|
548 | |
|
|
549 | /*PL_curcop = 0;*/ |
599 | /*PL_curcop = 0;*/ |
|
|
600 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
601 | SvREFCNT_dec (GvAV (PL_defgv)); |
551 | GvAV (PL_defgv) = ctx->args; |
602 | GvAV (PL_defgv) = ctx->args; |
552 | |
603 | |
|
|
604 | SPAGAIN; |
|
|
605 | |
553 | if (ctx->sptr) |
606 | if (ctx->stack) |
554 | { |
607 | { |
|
|
608 | ctx->cursp = 0; |
|
|
609 | |
555 | PUSHMARK(SP); |
610 | PUSHMARK(SP); |
556 | PUTBACK; |
611 | PUTBACK; |
557 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
612 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
613 | |
|
|
614 | if (SvTRUE (ERRSV)) |
|
|
615 | croak (NULL); |
|
|
616 | else |
558 | croak ("FATAL: CCTXT coroutine returned!"); |
617 | croak ("FATAL: CCTXT coroutine returned!"); |
559 | } |
618 | } |
560 | else |
619 | else |
561 | { |
620 | { |
562 | UNOP myop; |
621 | UNOP myop; |
563 | |
622 | |
… | |
… | |
582 | |
641 | |
583 | ENTER; /* necessary e.g. for dounwind */ |
642 | ENTER; /* necessary e.g. for dounwind */ |
584 | } |
643 | } |
585 | } |
644 | } |
586 | |
645 | |
|
|
646 | static void |
|
|
647 | continue_coro (void *arg) |
|
|
648 | { |
|
|
649 | /* |
|
|
650 | * this is a _very_ stripped down perl interpreter ;) |
|
|
651 | */ |
|
|
652 | Coro__State ctx = (Coro__State)arg; |
|
|
653 | |
|
|
654 | /*FIXME*//* must set up top_env here */ |
|
|
655 | ctx->cursp = 0; |
|
|
656 | PL_op = PL_op->op_next; |
|
|
657 | CALLRUNOPS(aTHX); |
|
|
658 | |
|
|
659 | abort (); |
|
|
660 | } |
|
|
661 | |
587 | STATIC void |
662 | STATIC void |
588 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
663 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
589 | { |
664 | { |
590 | dSP; |
665 | dSP; |
|
|
666 | dSTACKLEVEL; |
591 | |
667 | |
592 | if (prev != next) |
668 | if (prev != next) |
593 | { |
669 | { |
594 | /* |
|
|
595 | * this could be done in newprocess which would lead to |
|
|
596 | * extremely elegant and fast (basically just SAVE/LOAD) |
|
|
597 | * code here, but lazy allocation of stacks has also |
|
|
598 | * some virtues and the overhead of the if() is nil. |
|
|
599 | */ |
|
|
600 | if (next->mainstack) |
670 | if (next->mainstack) |
601 | { |
671 | { |
602 | SAVE (prev, flags); |
672 | SAVE (prev, flags); |
603 | LOAD (next); |
673 | LOAD (next); |
604 | |
674 | |
605 | /* mark this state as in-use */ |
675 | /* mark this state as in-use */ |
606 | next->mainstack = 0; |
676 | next->mainstack = 0; |
607 | next->tmps_ix = -2; |
677 | next->tmps_ix = -2; |
608 | |
678 | |
|
|
679 | /* stacklevel changed? if yes, grab the stack for us! */ |
609 | if (flags & TRANSFER_SAVE_CCTXT) |
680 | if (flags & TRANSFER_SAVE_CCTXT) |
610 | { |
681 | { |
611 | if (!next->ssize) |
|
|
612 | croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize); |
|
|
613 | |
|
|
614 | if (!prev->ssize) |
682 | if (!prev->stack) |
615 | prev->ssize = 1; /* mark cctx as valid ;) */ |
683 | allocate_stack (prev, 0); |
|
|
684 | else if (prev->cursp != stacklevel |
|
|
685 | && prev->stack->usecnt > 1) |
|
|
686 | { |
|
|
687 | prev->gencnt = ++prev->stack->gencnt; |
|
|
688 | prev->stack->usecnt = 1; |
|
|
689 | } |
616 | |
690 | |
|
|
691 | /* has our stack been invalidated? */ |
|
|
692 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
693 | { |
|
|
694 | deallocate_stack (next); |
|
|
695 | allocate_stack (next, 1); |
|
|
696 | coro_create (&(next->stack->cctx), |
|
|
697 | continue_coro, (void *)next, |
|
|
698 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
699 | } |
|
|
700 | |
617 | coro_transfer (&(prev->cctx), &(next->cctx)); |
701 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
618 | } |
702 | } |
619 | |
703 | |
620 | } |
704 | } |
621 | else if (next->tmps_ix == -2) |
705 | else if (next->tmps_ix == -2) |
622 | croak ("tried to transfer to running coroutine"); |
706 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
624 | { |
708 | { |
625 | SAVE (prev, -1); /* first get rid of the old state */ |
709 | SAVE (prev, -1); /* first get rid of the old state */ |
626 | |
710 | |
627 | if (flags & TRANSFER_SAVE_CCTXT) |
711 | if (flags & TRANSFER_SAVE_CCTXT) |
628 | { |
712 | { |
629 | if (!next->ssize) |
713 | if (!prev->stack) |
|
|
714 | allocate_stack (prev, 0); |
|
|
715 | |
|
|
716 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
630 | { |
717 | { |
|
|
718 | setup_coro (next); |
|
|
719 | |
|
|
720 | prev->stack->refcnt++; |
|
|
721 | prev->stack->usecnt++; |
|
|
722 | next->stack = prev->stack; |
|
|
723 | next->gencnt = prev->gencnt; |
|
|
724 | } |
|
|
725 | else |
|
|
726 | { |
631 | allocate_stack (next); |
727 | allocate_stack (next, 1); |
632 | coro_create (&(next->cctx), |
728 | coro_create (&(next->stack->cctx), |
633 | setup_coro, (void *)next, |
729 | setup_coro, (void *)next, |
634 | next->sptr, next->ssize); |
730 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
731 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
635 | } |
732 | } |
636 | |
|
|
637 | if (!prev->ssize) |
|
|
638 | prev->ssize = 1; /* mark cctx as valid ;) */ |
|
|
639 | |
|
|
640 | coro_transfer (&(prev->cctx), &(next->cctx)); |
|
|
641 | } |
733 | } |
642 | else |
734 | else |
643 | setup_coro (next); |
735 | setup_coro (next); |
644 | } |
736 | } |
645 | } |
737 | } |
|
|
738 | |
|
|
739 | next->cursp = stacklevel; |
|
|
740 | } |
|
|
741 | |
|
|
742 | static struct coro * |
|
|
743 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
744 | { |
|
|
745 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
746 | { |
|
|
747 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
|
|
748 | |
|
|
749 | if (!he) |
|
|
750 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
|
|
751 | |
|
|
752 | arg = HeVAL(he); |
|
|
753 | } |
|
|
754 | |
|
|
755 | /* must also be changed inside Coro::Cont::yield */ |
|
|
756 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
|
|
757 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
758 | else |
|
|
759 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
|
|
760 | } |
|
|
761 | |
|
|
762 | static void |
|
|
763 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
|
|
764 | { |
|
|
765 | transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), |
|
|
766 | sv_to_coro (next, "Coro::transfer", "next"), |
|
|
767 | flags); |
|
|
768 | } |
|
|
769 | |
|
|
770 | /** Coro ********************************************************************/ |
|
|
771 | |
|
|
772 | #define PRIO_MAX 3 |
|
|
773 | #define PRIO_HIGH 1 |
|
|
774 | #define PRIO_NORMAL 0 |
|
|
775 | #define PRIO_LOW -1 |
|
|
776 | #define PRIO_IDLE -3 |
|
|
777 | #define PRIO_MIN -4 |
|
|
778 | |
|
|
779 | /* for Coro.pm */ |
|
|
780 | static GV *coro_current, *coro_idle; |
|
|
781 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
782 | |
|
|
783 | static void |
|
|
784 | coro_enq (SV *sv) |
|
|
785 | { |
|
|
786 | if (SvROK (sv)) |
|
|
787 | { |
|
|
788 | SV *hv = SvRV (sv); |
|
|
789 | if (SvTYPE (hv) == SVt_PVHV) |
|
|
790 | { |
|
|
791 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
|
|
792 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
793 | |
|
|
794 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
795 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
796 | : prio; |
|
|
797 | |
|
|
798 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
799 | |
|
|
800 | return; |
|
|
801 | } |
|
|
802 | } |
|
|
803 | |
|
|
804 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
805 | } |
|
|
806 | |
|
|
807 | static SV * |
|
|
808 | coro_deq (int min_prio) |
|
|
809 | { |
|
|
810 | int prio = PRIO_MAX - PRIO_MIN; |
|
|
811 | |
|
|
812 | min_prio -= PRIO_MIN; |
|
|
813 | if (min_prio < 0) |
|
|
814 | min_prio = 0; |
|
|
815 | |
|
|
816 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
|
|
817 | if (av_len (coro_ready[prio]) >= 0) |
|
|
818 | return av_shift (coro_ready[prio]); |
|
|
819 | |
|
|
820 | return 0; |
|
|
821 | } |
|
|
822 | |
|
|
823 | static void |
|
|
824 | api_ready (SV *coro) |
|
|
825 | { |
|
|
826 | coro_enq (SvREFCNT_inc (coro)); |
|
|
827 | } |
|
|
828 | |
|
|
829 | static void |
|
|
830 | api_schedule (int cede) |
|
|
831 | { |
|
|
832 | SV *prev, *next; |
|
|
833 | |
|
|
834 | prev = GvSV (coro_current); |
|
|
835 | |
|
|
836 | if (cede) |
|
|
837 | coro_enq (SvREFCNT_inc (prev)); |
|
|
838 | |
|
|
839 | next = coro_deq (PRIO_MIN); |
|
|
840 | |
|
|
841 | if (!next) |
|
|
842 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
843 | |
|
|
844 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
845 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
846 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
847 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
848 | SvREFCNT_dec (next); |
|
|
849 | SvREFCNT_dec (prev); |
646 | } |
850 | } |
647 | |
851 | |
648 | MODULE = Coro::State PACKAGE = Coro::State |
852 | MODULE = Coro::State PACKAGE = Coro::State |
649 | |
853 | |
650 | PROTOTYPES: ENABLE |
854 | PROTOTYPES: ENABLE |
… | |
… | |
662 | |
866 | |
663 | if (!padlist_cache) |
867 | if (!padlist_cache) |
664 | padlist_cache = newHV (); |
868 | padlist_cache = newHV (); |
665 | |
869 | |
666 | main_mainstack = PL_mainstack; |
870 | main_mainstack = PL_mainstack; |
|
|
871 | |
|
|
872 | { |
|
|
873 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
874 | |
|
|
875 | coroapi.ver = CORO_API_VERSION - 1; |
|
|
876 | coroapi.transfer = api_transfer; |
|
|
877 | coroapi.schedule = api_schedule; |
|
|
878 | coroapi.ready = api_ready; |
|
|
879 | |
|
|
880 | GCoroAPI = &coroapi; |
|
|
881 | sv_setiv(sv, (IV)&coroapi); |
|
|
882 | SvREADONLY_on(sv); |
|
|
883 | } |
667 | } |
884 | } |
668 | |
885 | |
669 | Coro::State |
886 | Coro::State |
670 | _newprocess(args) |
887 | _newprocess(args) |
671 | SV * args |
888 | SV * args |
… | |
… | |
678 | |
895 | |
679 | New (0, coro, 1, struct coro); |
896 | New (0, coro, 1, struct coro); |
680 | |
897 | |
681 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
898 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
682 | coro->mainstack = 0; /* actual work is done inside transfer */ |
899 | coro->mainstack = 0; /* actual work is done inside transfer */ |
683 | coro->sptr = 0; |
900 | coro->stack = 0; |
684 | coro->ssize = 0; |
|
|
685 | |
901 | |
686 | RETVAL = coro; |
902 | RETVAL = coro; |
687 | OUTPUT: |
903 | OUTPUT: |
688 | RETVAL |
904 | RETVAL |
689 | |
905 | |
690 | void |
906 | void |
691 | transfer(prev, next, flags = TRANSFER_SAVE_ALL) |
907 | transfer(prev, next, flags) |
692 | Coro::State_or_hashref prev |
908 | Coro::State_or_hashref prev |
693 | Coro::State_or_hashref next |
909 | Coro::State_or_hashref next |
694 | int flags |
910 | int flags |
695 | PROTOTYPE: @ |
911 | PROTOTYPE: @ |
696 | CODE: |
912 | CODE: |
|
|
913 | PUTBACK; |
697 | transfer (aTHX_ prev, next, flags); |
914 | transfer (aTHX_ prev, next, flags); |
|
|
915 | SPAGAIN; |
698 | |
916 | |
699 | void |
917 | void |
700 | DESTROY(coro) |
918 | DESTROY(coro) |
701 | Coro::State coro |
919 | Coro::State coro |
702 | CODE: |
920 | CODE: |
… | |
… | |
713 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
931 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
714 | |
932 | |
715 | coro->mainstack = 0; |
933 | coro->mainstack = 0; |
716 | } |
934 | } |
717 | |
935 | |
718 | if (coro->sptr) |
|
|
719 | { |
|
|
720 | deallocate_stack (coro); |
936 | deallocate_stack (coro); |
721 | coro->sptr = 0; |
|
|
722 | } |
|
|
723 | |
937 | |
724 | Safefree (coro); |
938 | Safefree (coro); |
725 | |
939 | |
726 | void |
940 | void |
727 | flush() |
941 | flush() |
728 | CODE: |
942 | CODE: |
729 | #ifdef MAY_FLUSH |
943 | #ifdef MAY_FLUSH |
730 | flush_padlist_cache (); |
944 | flush_padlist_cache (); |
731 | #endif |
945 | #endif |
732 | |
946 | |
|
|
947 | void |
|
|
948 | _exit(code) |
|
|
949 | int code |
|
|
950 | PROTOTYPE: $ |
|
|
951 | CODE: |
|
|
952 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
953 | _exit (code); |
|
|
954 | #else |
|
|
955 | signal (SIGTERM, SIG_DFL); |
|
|
956 | raise (SIGTERM); |
|
|
957 | exit (code); |
|
|
958 | #endif |
|
|
959 | |
733 | MODULE = Coro::State PACKAGE = Coro::Cont |
960 | MODULE = Coro::State PACKAGE = Coro::Cont |
734 | |
961 | |
735 | # this is slightly dirty |
962 | # this is slightly dirty (should expose a c-level api) |
736 | |
963 | |
737 | void |
964 | void |
738 | yield(...) |
965 | yield(...) |
739 | PROTOTYPE: @ |
966 | PROTOTYPE: @ |
740 | CODE: |
967 | CODE: |
… | |
… | |
758 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
985 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
759 | SvREFCNT_dec (sv); |
986 | SvREFCNT_dec (sv); |
760 | |
987 | |
761 | transfer(aTHX_ prev, next, 0); |
988 | transfer(aTHX_ prev, next, 0); |
762 | |
989 | |
|
|
990 | MODULE = Coro::State PACKAGE = Coro |
|
|
991 | |
|
|
992 | # this is slightly dirty (should expose a c-level api) |
|
|
993 | |
|
|
994 | BOOT: |
|
|
995 | { |
|
|
996 | int i; |
|
|
997 | HV *stash = gv_stashpv ("Coro", TRUE); |
|
|
998 | |
|
|
999 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
1000 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
1001 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
1002 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
1003 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
1004 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
1005 | |
|
|
1006 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
|
|
1007 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
|
|
1008 | |
|
|
1009 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
1010 | coro_ready[i] = newAV (); |
|
|
1011 | } |
|
|
1012 | |
|
|
1013 | void |
|
|
1014 | ready(self) |
|
|
1015 | SV * self |
|
|
1016 | CODE: |
|
|
1017 | api_ready (self); |
|
|
1018 | |
|
|
1019 | void |
|
|
1020 | schedule(...) |
|
|
1021 | ALIAS: |
|
|
1022 | cede = 1 |
|
|
1023 | CODE: |
|
|
1024 | api_schedule (ix); |
|
|
1025 | |