… | |
… | |
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 | |
22 | |
14 | #define SUB_INIT "Coro::State::initialize" |
23 | /* perl-related */ |
15 | |
|
|
16 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
24 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
17 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
25 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
18 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
26 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
|
|
27 | /* c-related */ |
19 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
28 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
|
|
29 | #ifdef CORO_LAZY_STACK |
|
|
30 | # define TRANSFER_LAZY_STACK 0x00000010 |
|
|
31 | #else |
|
|
32 | # define TRANSFER_LAZY_STACK 0x00000000 |
|
|
33 | #endif |
20 | |
34 | |
21 | #define TRANSFER_SAVE_ALL -1 |
35 | #define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \ |
|
|
36 | |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT) |
|
|
37 | |
|
|
38 | #define SUB_INIT "Coro::State::initialize" |
|
|
39 | #define UCORO_STATE "_coro_state" |
|
|
40 | |
|
|
41 | /* The next macro should delcare a variable stacklevel that contains and approximation |
|
|
42 | * to the current C stack pointer. It's property is that it changes with each call |
|
|
43 | * and should be unique. */ |
|
|
44 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
|
|
45 | |
|
|
46 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
47 | |
|
|
48 | /* this is actually not only the c stack but also c registers etc... */ |
|
|
49 | typedef struct { |
|
|
50 | int refcnt; /* pointer reference counter */ |
|
|
51 | int usecnt; /* shared by how many coroutines */ |
|
|
52 | int gencnt; /* generation counter */ |
|
|
53 | |
|
|
54 | coro_context cctx; |
|
|
55 | |
|
|
56 | void *sptr; |
|
|
57 | long ssize; /* positive == mmap, otherwise malloc */ |
|
|
58 | } coro_stack; |
22 | |
59 | |
23 | struct coro { |
60 | struct coro { |
24 | /* the optional C context */ |
61 | /* the optional C context */ |
25 | coro_context cctx; |
62 | coro_stack *stack; |
26 | void *sptr; |
63 | void *cursp; |
27 | long ssize; |
64 | int gencnt; |
28 | |
65 | |
29 | /* optionally saved, might be zero */ |
66 | /* optionally saved, might be zero */ |
30 | AV *defav; |
67 | AV *defav; |
31 | SV *defsv; |
68 | SV *defsv; |
32 | SV *errsv; |
69 | SV *errsv; |
33 | |
70 | |
34 | /* saved global state not related to stacks */ |
71 | /* saved global state not related to stacks */ |
35 | U8 dowarn; |
72 | U8 dowarn; |
|
|
73 | I32 in_eval; |
36 | |
74 | |
37 | /* the stacks and related info (callchain etc..) */ |
75 | /* the stacks and related info (callchain etc..) */ |
38 | PERL_SI *curstackinfo; |
76 | PERL_SI *curstackinfo; |
39 | AV *curstack; |
77 | AV *curstack; |
40 | AV *mainstack; |
78 | AV *mainstack; |
… | |
… | |
58 | I32 savestack_max; |
96 | I32 savestack_max; |
59 | OP **retstack; |
97 | OP **retstack; |
60 | I32 retstack_ix; |
98 | I32 retstack_ix; |
61 | I32 retstack_max; |
99 | I32 retstack_max; |
62 | COP *curcop; |
100 | COP *curcop; |
63 | JMPENV start_env; |
|
|
64 | JMPENV *top_env; |
101 | JMPENV *top_env; |
65 | |
102 | |
66 | /* data associated with this coroutine (initial args) */ |
103 | /* data associated with this coroutine (initial args) */ |
67 | AV *args; |
104 | AV *args; |
68 | }; |
105 | }; |
… | |
… | |
70 | typedef struct coro *Coro__State; |
107 | typedef struct coro *Coro__State; |
71 | typedef struct coro *Coro__State_or_hashref; |
108 | typedef struct coro *Coro__State_or_hashref; |
72 | |
109 | |
73 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
110 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
74 | static HV *coro_state_stash; |
111 | static HV *coro_state_stash; |
|
|
112 | static SV *ucoro_state_sv; |
|
|
113 | static U32 ucoro_state_hash; |
75 | static HV *padlist_cache; |
114 | static HV *padlist_cache; |
|
|
115 | |
|
|
116 | /* for Coro.pm */ |
|
|
117 | static GV *coro_current, *coro_idle; |
|
|
118 | static AV *coro_ready; |
76 | |
119 | |
77 | /* mostly copied from op.c:cv_clone2 */ |
120 | /* mostly copied from op.c:cv_clone2 */ |
78 | STATIC AV * |
121 | STATIC AV * |
79 | clone_padlist (AV *protopadlist) |
122 | clone_padlist (AV *protopadlist) |
80 | { |
123 | { |
… | |
… | |
251 | |
294 | |
252 | static void |
295 | static void |
253 | load_state(pTHX_ Coro__State c) |
296 | load_state(pTHX_ Coro__State c) |
254 | { |
297 | { |
255 | PL_dowarn = c->dowarn; |
298 | PL_dowarn = c->dowarn; |
|
|
299 | PL_in_eval = c->in_eval; |
256 | |
300 | |
257 | PL_curstackinfo = c->curstackinfo; |
301 | PL_curstackinfo = c->curstackinfo; |
258 | PL_curstack = c->curstack; |
302 | PL_curstack = c->curstack; |
259 | PL_mainstack = c->mainstack; |
303 | PL_mainstack = c->mainstack; |
260 | PL_stack_sp = c->stack_sp; |
304 | PL_stack_sp = c->stack_sp; |
… | |
… | |
277 | PL_savestack_max = c->savestack_max; |
321 | PL_savestack_max = c->savestack_max; |
278 | PL_retstack = c->retstack; |
322 | PL_retstack = c->retstack; |
279 | PL_retstack_ix = c->retstack_ix; |
323 | PL_retstack_ix = c->retstack_ix; |
280 | PL_retstack_max = c->retstack_max; |
324 | PL_retstack_max = c->retstack_max; |
281 | PL_curcop = c->curcop; |
325 | PL_curcop = c->curcop; |
282 | PL_start_env = c->start_env; |
|
|
283 | PL_top_env = c->top_env; |
326 | PL_top_env = c->top_env; |
284 | |
327 | |
285 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
328 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
286 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
329 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
287 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
330 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
386 | /* but if it's missing the defav contents magically get replaced sometimes */ |
429 | /* but if it's missing the defav contents magically get replaced sometimes */ |
387 | if (c->defav) |
430 | if (c->defav) |
388 | av_reify (c->defav); |
431 | av_reify (c->defav); |
389 | |
432 | |
390 | c->dowarn = PL_dowarn; |
433 | c->dowarn = PL_dowarn; |
|
|
434 | c->in_eval = PL_in_eval; |
391 | |
435 | |
392 | c->curstackinfo = PL_curstackinfo; |
436 | c->curstackinfo = PL_curstackinfo; |
393 | c->curstack = PL_curstack; |
437 | c->curstack = PL_curstack; |
394 | c->mainstack = PL_mainstack; |
438 | c->mainstack = PL_mainstack; |
395 | c->stack_sp = PL_stack_sp; |
439 | c->stack_sp = PL_stack_sp; |
… | |
… | |
412 | c->savestack_max = PL_savestack_max; |
456 | c->savestack_max = PL_savestack_max; |
413 | c->retstack = PL_retstack; |
457 | c->retstack = PL_retstack; |
414 | c->retstack_ix = PL_retstack_ix; |
458 | c->retstack_ix = PL_retstack_ix; |
415 | c->retstack_max = PL_retstack_max; |
459 | c->retstack_max = PL_retstack_max; |
416 | c->curcop = PL_curcop; |
460 | c->curcop = PL_curcop; |
417 | c->start_env = PL_start_env; |
|
|
418 | c->top_env = PL_top_env; |
461 | c->top_env = PL_top_env; |
419 | } |
462 | } |
420 | |
463 | |
421 | /* |
464 | /* |
422 | * allocate various perl stacks. This is an exact copy |
465 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
434 | |
477 | |
435 | PL_stack_base = AvARRAY(PL_curstack); |
478 | PL_stack_base = AvARRAY(PL_curstack); |
436 | PL_stack_sp = PL_stack_base; |
479 | PL_stack_sp = PL_stack_base; |
437 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
480 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
438 | |
481 | |
439 | New(50,PL_tmps_stack,64,SV*); |
482 | New(50,PL_tmps_stack,96,SV*); |
440 | PL_tmps_floor = -1; |
483 | PL_tmps_floor = -1; |
441 | PL_tmps_ix = -1; |
484 | PL_tmps_ix = -1; |
442 | PL_tmps_max = 64; |
485 | PL_tmps_max = 96; |
443 | |
486 | |
444 | New(54,PL_markstack,12,I32); |
487 | New(54,PL_markstack,16,I32); |
445 | PL_markstack_ptr = PL_markstack; |
488 | PL_markstack_ptr = PL_markstack; |
446 | PL_markstack_max = PL_markstack + 12; |
489 | PL_markstack_max = PL_markstack + 16; |
447 | |
490 | |
448 | SET_MARK_OFFSET; |
491 | SET_MARK_OFFSET; |
449 | |
492 | |
450 | New(54,PL_scopestack,12,I32); |
493 | New(54,PL_scopestack,16,I32); |
451 | PL_scopestack_ix = 0; |
494 | PL_scopestack_ix = 0; |
452 | PL_scopestack_max = 12; |
495 | PL_scopestack_max = 16; |
453 | |
496 | |
454 | New(54,PL_savestack,64,ANY); |
497 | New(54,PL_savestack,96,ANY); |
455 | PL_savestack_ix = 0; |
498 | PL_savestack_ix = 0; |
456 | PL_savestack_max = 64; |
499 | PL_savestack_max = 96; |
457 | |
500 | |
458 | New(54,PL_retstack,8,OP*); |
501 | New(54,PL_retstack,8,OP*); |
459 | PL_retstack_ix = 0; |
502 | PL_retstack_ix = 0; |
460 | PL_retstack_max = 8; |
503 | PL_retstack_max = 8; |
461 | } |
504 | } |
… | |
… | |
502 | Safefree(PL_savestack); |
545 | Safefree(PL_savestack); |
503 | Safefree(PL_retstack); |
546 | Safefree(PL_retstack); |
504 | } |
547 | } |
505 | |
548 | |
506 | static void |
549 | static void |
507 | allocate_stack (Coro__State ctx) |
550 | allocate_stack (Coro__State ctx, int alloc) |
508 | { |
551 | { |
509 | #ifdef HAVE_MMAP |
552 | coro_stack *stack; |
510 | ctx->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ |
553 | |
511 | ctx->sptr = mmap (0, ctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
554 | New (0, stack, 1, coro_stack); |
512 | if (ctx->sptr == (void *)-1) |
555 | |
513 | #endif |
556 | stack->refcnt = 1; |
|
|
557 | stack->usecnt = 1; |
|
|
558 | stack->gencnt = ctx->gencnt = 0; |
|
|
559 | if (alloc) |
514 | { |
560 | { |
|
|
561 | #ifdef HAVE_MMAP |
|
|
562 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
|
|
563 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
|
|
564 | if (stack->sptr == (void *)-1) |
|
|
565 | #endif |
|
|
566 | { |
515 | /*FIXME*//*D*//* reasonable stack size! */ |
567 | /*FIXME*//*D*//* reasonable stack size! */ |
516 | ctx->ssize = 4096 * sizeof (long); |
568 | stack->ssize = -4096 * sizeof (long); |
517 | New (0, ctx->sptr, 4096, long); |
569 | New (0, stack->sptr, 4096, long); |
|
|
570 | } |
518 | } |
571 | } |
|
|
572 | else |
|
|
573 | stack->sptr = 0; |
|
|
574 | |
|
|
575 | ctx->stack = stack; |
519 | } |
576 | } |
520 | |
577 | |
521 | static void |
578 | static void |
522 | deallocate_stack (Coro__State ctx) |
579 | deallocate_stack (Coro__State ctx) |
523 | { |
580 | { |
|
|
581 | coro_stack *stack = ctx->stack; |
|
|
582 | |
|
|
583 | ctx->stack = 0; |
|
|
584 | |
|
|
585 | if (stack) |
|
|
586 | { |
|
|
587 | if (!--stack->refcnt) |
|
|
588 | { |
524 | #ifdef HAVE_MMAP |
589 | #ifdef HAVE_MMAP |
525 | munmap (ctx->sptr, ctx->ssize); |
590 | if (stack->ssize > 0 && stack->sptr) |
|
|
591 | munmap (stack->sptr, stack->ssize); |
|
|
592 | else |
526 | #else |
593 | #else |
527 | Safefree (ctx->sptr); |
594 | Safefree (stack->sptr); |
528 | #endif |
595 | #endif |
|
|
596 | Safefree (stack); |
|
|
597 | } |
|
|
598 | else if (ctx->gencnt == stack->gencnt) |
|
|
599 | --stack->usecnt; |
|
|
600 | } |
529 | } |
601 | } |
530 | |
602 | |
531 | /* might go away together with optional SAVE_CCTXT */ |
|
|
532 | static void |
603 | static void |
533 | setup_coro (void *arg) |
604 | setup_coro (void *arg) |
534 | { |
605 | { |
535 | /* |
606 | /* |
536 | * emulate part of the perl startup here. |
607 | * emulate part of the perl startup here. |
… | |
… | |
538 | dSP; |
609 | dSP; |
539 | Coro__State ctx = (Coro__State)arg; |
610 | Coro__State ctx = (Coro__State)arg; |
540 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
611 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
541 | |
612 | |
542 | coro_init_stacks (aTHX); |
613 | coro_init_stacks (aTHX); |
543 | JMPENV_BOOTSTRAP; |
|
|
544 | SPAGAIN; |
|
|
545 | |
|
|
546 | /*PL_curcop = 0;*/ |
614 | /*PL_curcop = 0;*/ |
|
|
615 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
547 | SvREFCNT_dec (GvAV (PL_defgv)); |
616 | SvREFCNT_dec (GvAV (PL_defgv)); |
548 | GvAV (PL_defgv) = ctx->args; |
617 | GvAV (PL_defgv) = ctx->args; |
549 | |
618 | |
|
|
619 | SPAGAIN; |
|
|
620 | |
550 | if (ctx->sptr) |
621 | if (ctx->stack) |
551 | { |
622 | { |
|
|
623 | ctx->cursp = 0; |
|
|
624 | |
552 | PUSHMARK(SP); |
625 | PUSHMARK(SP); |
553 | PUTBACK; |
626 | PUTBACK; |
554 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
627 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
628 | |
|
|
629 | if (SvTRUE (ERRSV)) |
|
|
630 | croak (NULL); |
|
|
631 | else |
555 | croak ("FATAL: CCTXT coroutine returned!"); |
632 | croak ("FATAL: CCTXT coroutine returned!"); |
556 | } |
633 | } |
557 | else |
634 | else |
558 | { |
635 | { |
559 | UNOP myop; |
636 | UNOP myop; |
560 | |
637 | |
… | |
… | |
579 | |
656 | |
580 | ENTER; /* necessary e.g. for dounwind */ |
657 | ENTER; /* necessary e.g. for dounwind */ |
581 | } |
658 | } |
582 | } |
659 | } |
583 | |
660 | |
|
|
661 | static void |
|
|
662 | continue_coro (void *arg) |
|
|
663 | { |
|
|
664 | /* |
|
|
665 | * this is a _very_ stripped down perl interpreter ;) |
|
|
666 | */ |
|
|
667 | Coro__State ctx = (Coro__State)arg; |
|
|
668 | |
|
|
669 | /*FIXME*//* must set up top_env here */ |
|
|
670 | ctx->cursp = 0; |
|
|
671 | PL_op = PL_op->op_next; |
|
|
672 | CALLRUNOPS(aTHX); |
|
|
673 | |
|
|
674 | abort (); |
|
|
675 | } |
|
|
676 | |
584 | STATIC void |
677 | STATIC void |
585 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
678 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
586 | { |
679 | { |
587 | dSP; |
680 | dSP; |
|
|
681 | dSTACKLEVEL; |
588 | |
682 | |
589 | if (prev != next) |
683 | if (prev != next) |
590 | { |
684 | { |
591 | /* |
|
|
592 | * this could be done in newprocess which would lead to |
|
|
593 | * extremely elegant and fast (basically just SAVE/LOAD) |
|
|
594 | * code here, but lazy allocation of stacks has also |
|
|
595 | * some virtues and the overhead of the if() is nil. |
|
|
596 | */ |
|
|
597 | if (next->mainstack) |
685 | if (next->mainstack) |
598 | { |
686 | { |
599 | SAVE (prev, flags); |
687 | SAVE (prev, flags); |
600 | LOAD (next); |
688 | LOAD (next); |
601 | |
689 | |
602 | /* mark this state as in-use */ |
690 | /* mark this state as in-use */ |
603 | next->mainstack = 0; |
691 | next->mainstack = 0; |
604 | next->tmps_ix = -2; |
692 | next->tmps_ix = -2; |
605 | |
693 | |
|
|
694 | /* stacklevel changed? if yes, grab the stack for us! */ |
606 | if (flags & TRANSFER_SAVE_CCTXT) |
695 | if (flags & TRANSFER_SAVE_CCTXT) |
607 | { |
696 | { |
608 | if (!next->ssize) |
|
|
609 | croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize); |
|
|
610 | |
|
|
611 | if (!prev->ssize) |
697 | if (!prev->stack) |
612 | prev->ssize = 1; /* mark cctx as valid ;) */ |
698 | allocate_stack (prev, 0); |
|
|
699 | else if (prev->cursp != stacklevel |
|
|
700 | && prev->stack->usecnt > 1) |
|
|
701 | { |
|
|
702 | prev->gencnt = ++prev->stack->gencnt; |
|
|
703 | prev->stack->usecnt = 1; |
|
|
704 | } |
613 | |
705 | |
|
|
706 | /* has our stack been invalidated? */ |
|
|
707 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
708 | { |
|
|
709 | deallocate_stack (next); |
|
|
710 | allocate_stack (next, 1); |
|
|
711 | coro_create (&(next->stack->cctx), |
|
|
712 | continue_coro, (void *)next, |
|
|
713 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
714 | } |
|
|
715 | |
614 | coro_transfer (&(prev->cctx), &(next->cctx)); |
716 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
615 | } |
717 | } |
616 | |
718 | |
617 | } |
719 | } |
618 | else if (next->tmps_ix == -2) |
720 | else if (next->tmps_ix == -2) |
619 | croak ("tried to transfer to running coroutine"); |
721 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
621 | { |
723 | { |
622 | SAVE (prev, -1); /* first get rid of the old state */ |
724 | SAVE (prev, -1); /* first get rid of the old state */ |
623 | |
725 | |
624 | if (flags & TRANSFER_SAVE_CCTXT) |
726 | if (flags & TRANSFER_SAVE_CCTXT) |
625 | { |
727 | { |
626 | if (!next->ssize) |
728 | if (!prev->stack) |
|
|
729 | allocate_stack (prev, 0); |
|
|
730 | |
|
|
731 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
627 | { |
732 | { |
|
|
733 | setup_coro (next); |
|
|
734 | |
|
|
735 | prev->stack->refcnt++; |
|
|
736 | prev->stack->usecnt++; |
|
|
737 | next->stack = prev->stack; |
|
|
738 | next->gencnt = prev->gencnt; |
|
|
739 | } |
|
|
740 | else |
|
|
741 | { |
628 | allocate_stack (next); |
742 | allocate_stack (next, 1); |
629 | coro_create (&(next->cctx), |
743 | coro_create (&(next->stack->cctx), |
630 | setup_coro, (void *)next, |
744 | setup_coro, (void *)next, |
631 | next->sptr, next->ssize); |
745 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
746 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
632 | } |
747 | } |
633 | |
|
|
634 | if (!prev->ssize) |
|
|
635 | prev->ssize = 1; /* mark cctx as valid ;) */ |
|
|
636 | |
|
|
637 | coro_transfer (&(prev->cctx), &(next->cctx)); |
|
|
638 | } |
748 | } |
639 | else |
749 | else |
640 | setup_coro (next); |
750 | setup_coro (next); |
641 | } |
751 | } |
642 | } |
752 | } |
|
|
753 | |
|
|
754 | next->cursp = stacklevel; |
|
|
755 | } |
|
|
756 | |
|
|
757 | static struct coro * |
|
|
758 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
759 | { |
|
|
760 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
761 | { |
|
|
762 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
|
|
763 | |
|
|
764 | if (!he) |
|
|
765 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
|
|
766 | |
|
|
767 | arg = HeVAL(he); |
|
|
768 | } |
|
|
769 | |
|
|
770 | /* must also be changed inside Coro::Cont::yield */ |
|
|
771 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
|
|
772 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
773 | else |
|
|
774 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
643 | } |
775 | } |
644 | |
776 | |
645 | MODULE = Coro::State PACKAGE = Coro::State |
777 | MODULE = Coro::State PACKAGE = Coro::State |
646 | |
778 | |
647 | PROTOTYPES: ENABLE |
779 | PROTOTYPES: ENABLE |
648 | |
780 | |
649 | BOOT: |
781 | BOOT: |
650 | { /* {} necessary for stoopid perl-5.6.x */ |
782 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
783 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
|
|
784 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
651 | coro_state_stash = gv_stashpvn ("Coro::State", 10, TRUE); |
785 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
652 | |
786 | |
653 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
787 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
654 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
788 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
655 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
789 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
656 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
790 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
… | |
… | |
673 | |
807 | |
674 | New (0, coro, 1, struct coro); |
808 | New (0, coro, 1, struct coro); |
675 | |
809 | |
676 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
810 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
677 | coro->mainstack = 0; /* actual work is done inside transfer */ |
811 | coro->mainstack = 0; /* actual work is done inside transfer */ |
678 | coro->sptr = 0; |
812 | coro->stack = 0; |
679 | coro->ssize = 0; |
|
|
680 | |
813 | |
681 | RETVAL = coro; |
814 | RETVAL = coro; |
682 | OUTPUT: |
815 | OUTPUT: |
683 | RETVAL |
816 | RETVAL |
684 | |
817 | |
685 | void |
818 | void |
686 | transfer(prev, next, flags = TRANSFER_SAVE_ALL) |
819 | transfer(prev, next, flags) |
687 | Coro::State_or_hashref prev |
820 | Coro::State_or_hashref prev |
688 | Coro::State_or_hashref next |
821 | Coro::State_or_hashref next |
689 | int flags |
822 | int flags |
690 | PROTOTYPE: @ |
823 | PROTOTYPE: @ |
691 | CODE: |
824 | CODE: |
|
|
825 | PUTBACK; |
692 | transfer (aTHX_ prev, next, flags); |
826 | transfer (aTHX_ prev, next, flags); |
|
|
827 | SPAGAIN; |
693 | |
828 | |
694 | void |
829 | void |
695 | DESTROY(coro) |
830 | DESTROY(coro) |
696 | Coro::State coro |
831 | Coro::State coro |
697 | CODE: |
832 | CODE: |
… | |
… | |
708 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
843 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
709 | |
844 | |
710 | coro->mainstack = 0; |
845 | coro->mainstack = 0; |
711 | } |
846 | } |
712 | |
847 | |
713 | if (coro->sptr) |
|
|
714 | { |
|
|
715 | deallocate_stack (coro); |
848 | deallocate_stack (coro); |
716 | coro->sptr = 0; |
|
|
717 | } |
|
|
718 | |
849 | |
719 | Safefree (coro); |
850 | Safefree (coro); |
720 | |
851 | |
721 | void |
852 | void |
722 | flush() |
853 | flush() |
723 | CODE: |
854 | CODE: |
724 | #ifdef MAY_FLUSH |
855 | #ifdef MAY_FLUSH |
725 | flush_padlist_cache (); |
856 | flush_padlist_cache (); |
726 | #endif |
857 | #endif |
727 | |
858 | |
|
|
859 | void |
|
|
860 | _exit(code) |
|
|
861 | int code |
|
|
862 | PROTOTYPE: $ |
|
|
863 | CODE: |
|
|
864 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
865 | _exit (code); |
|
|
866 | #else |
|
|
867 | signal (SIGTERM, SIG_DFL); |
|
|
868 | raise (SIGTERM); |
|
|
869 | exit (code); |
|
|
870 | #endif |
|
|
871 | |
728 | MODULE = Coro::State PACKAGE = Coro::Cont |
872 | MODULE = Coro::State PACKAGE = Coro::Cont |
729 | |
873 | |
730 | # this is slightly dirty |
874 | # this is slightly dirty (should expose a c-level api) |
731 | |
875 | |
732 | void |
876 | void |
733 | yield(...) |
877 | yield(...) |
734 | PROTOTYPE: @ |
878 | PROTOTYPE: @ |
735 | CODE: |
879 | CODE: |
… | |
… | |
753 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
897 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
754 | SvREFCNT_dec (sv); |
898 | SvREFCNT_dec (sv); |
755 | |
899 | |
756 | transfer(aTHX_ prev, next, 0); |
900 | transfer(aTHX_ prev, next, 0); |
757 | |
901 | |
|
|
902 | MODULE = Coro::State PACKAGE = Coro |
|
|
903 | |
|
|
904 | # this is slightly dirty (should expose a c-level api) |
|
|
905 | |
|
|
906 | BOOT: |
|
|
907 | { |
|
|
908 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
|
|
909 | coro_ready = newAV (); |
|
|
910 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
|
|
911 | } |
|
|
912 | |
|
|
913 | void |
|
|
914 | ready(self) |
|
|
915 | SV * self |
|
|
916 | CODE: |
|
|
917 | av_push (coro_ready, SvREFCNT_inc (self)); |
|
|
918 | |
|
|
919 | void |
|
|
920 | schedule(...) |
|
|
921 | ALIAS: |
|
|
922 | cede = 1 |
|
|
923 | CODE: |
|
|
924 | SV *prev = GvSV (coro_current); |
|
|
925 | SV *next = av_shift (coro_ready); |
|
|
926 | |
|
|
927 | if (next == &PL_sv_undef) |
|
|
928 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
929 | |
|
|
930 | if (ix) |
|
|
931 | av_push (coro_ready, SvREFCNT_inc (prev)); |
|
|
932 | |
|
|
933 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
934 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
935 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
936 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
937 | SvREFCNT_dec (next); |
|
|
938 | SvREFCNT_dec (prev); |
|
|
939 | |