1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include "patchlevel.h" |
|
|
6 | |
|
|
7 | #if PATCHLEVEL < 6 |
|
|
8 | # ifndef PL_ppaddr |
|
|
9 | # define PL_ppaddr ppaddr |
|
|
10 | # endif |
|
|
11 | # ifndef call_sv |
|
|
12 | # define call_sv perl_call_sv |
|
|
13 | # endif |
|
|
14 | # ifndef get_sv |
|
|
15 | # define get_sv perl_get_sv |
|
|
16 | # endif |
|
|
17 | # ifndef get_cv |
|
|
18 | # define get_cv perl_get_cv |
|
|
19 | # endif |
|
|
20 | # ifndef IS_PADGV |
|
|
21 | # define IS_PADGV(v) 0 |
|
|
22 | # endif |
|
|
23 | # ifndef IS_PADCONST |
|
|
24 | # define IS_PADCONST(v) 0 |
|
|
25 | # endif |
|
|
26 | #endif |
|
|
27 | |
5 | #include "libcoro/coro.c" |
28 | #include "libcoro/coro.c" |
6 | |
29 | |
7 | #include <signal.h> |
30 | #include <signal.h> |
8 | |
31 | |
9 | #ifdef HAVE_MMAP |
32 | #ifdef HAVE_MMAP |
10 | # include <unistd.h> |
33 | # include <unistd.h> |
11 | # include <sys/mman.h> |
34 | # include <sys/mman.h> |
12 | # ifndef MAP_ANON |
|
|
13 | # ifdef MAP_ANONYMOUS |
35 | # ifndef MAP_ANONYMOUS |
14 | # define MAP_ANON MAP_ANONYMOUS |
36 | # ifdef MAP_ANON |
|
|
37 | # define MAP_ANONYMOUS MAP_ANON |
15 | # else |
38 | # else |
16 | # undef HAVE_MMAP |
39 | # undef HAVE_MMAP |
17 | # endif |
40 | # endif |
18 | # endif |
41 | # endif |
19 | #endif |
42 | #endif |
20 | |
43 | |
21 | #define MAY_FLUSH /* increases codesize */ |
44 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
22 | |
45 | |
23 | #define SUB_INIT "Coro::State::initialize" |
46 | #define SUB_INIT "Coro::State::initialize" |
24 | #define UCORO_STATE "_coro_state" |
47 | #define UCORO_STATE "_coro_state" |
25 | |
48 | |
26 | /* The next macro should delcare a variable stacklevel that contains and approximation |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
27 | * to the current C stack pointer. Its property is that it changes with each call |
50 | * to the current C stack pointer. Its property is that it changes with each call |
28 | * and should be unique. */ |
51 | * and should be unique. */ |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
52 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
|
|
53 | |
|
|
54 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
30 | |
55 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
32 | |
57 | |
33 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
34 | |
59 | |
… | |
… | |
45 | void *sptr; |
70 | void *sptr; |
46 | long ssize; /* positive == mmap, otherwise malloc */ |
71 | long ssize; /* positive == mmap, otherwise malloc */ |
47 | } coro_stack; |
72 | } coro_stack; |
48 | |
73 | |
49 | struct coro { |
74 | struct coro { |
|
|
75 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
76 | JMPENV start_env; |
|
|
77 | |
50 | /* the optional C context */ |
78 | /* the optional C context */ |
51 | coro_stack *stack; |
79 | coro_stack *stack; |
52 | void *cursp; |
80 | void *cursp; |
53 | int gencnt; |
81 | int gencnt; |
54 | |
82 | |
… | |
… | |
66 | AV *curstack; |
94 | AV *curstack; |
67 | AV *mainstack; |
95 | AV *mainstack; |
68 | SV **stack_sp; |
96 | SV **stack_sp; |
69 | OP *op; |
97 | OP *op; |
70 | SV **curpad; |
98 | SV **curpad; |
|
|
99 | AV *comppad; |
71 | SV **stack_base; |
100 | SV **stack_base; |
72 | SV **stack_max; |
101 | SV **stack_max; |
73 | SV **tmps_stack; |
102 | SV **tmps_stack; |
74 | I32 tmps_floor; |
103 | I32 tmps_floor; |
75 | I32 tmps_ix; |
104 | I32 tmps_ix; |
… | |
… | |
99 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
128 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
100 | static HV *coro_state_stash; |
129 | static HV *coro_state_stash; |
101 | static SV *ucoro_state_sv; |
130 | static SV *ucoro_state_sv; |
102 | static U32 ucoro_state_hash; |
131 | static U32 ucoro_state_hash; |
103 | static HV *padlist_cache; |
132 | static HV *padlist_cache; |
|
|
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
104 | |
134 | |
105 | /* mostly copied from op.c:cv_clone2 */ |
135 | /* mostly copied from op.c:cv_clone2 */ |
106 | STATIC AV * |
136 | STATIC AV * |
107 | clone_padlist (AV *protopadlist) |
137 | clone_padlist (AV *protopadlist) |
108 | { |
138 | { |
… | |
… | |
195 | |
225 | |
196 | return newpadlist; |
226 | return newpadlist; |
197 | } |
227 | } |
198 | |
228 | |
199 | #ifdef MAY_FLUSH |
229 | #ifdef MAY_FLUSH |
200 | STATIC AV * |
230 | STATIC void |
201 | free_padlist (AV *padlist) |
231 | free_padlist (AV *padlist) |
202 | { |
232 | { |
203 | /* may be during global destruction */ |
233 | /* may be during global destruction */ |
204 | if (SvREFCNT(padlist)) |
234 | if (SvREFCNT(padlist)) |
205 | { |
235 | { |
… | |
… | |
270 | #endif |
300 | #endif |
271 | |
301 | |
272 | #define SB do { |
302 | #define SB do { |
273 | #define SE } while (0) |
303 | #define SE } while (0) |
274 | |
304 | |
275 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
305 | #define LOAD(state) load_state(aTHX_ (state)); |
276 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
306 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
277 | |
307 | |
278 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
308 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
279 | |
309 | |
280 | static void |
310 | static void |
281 | load_state(pTHX_ Coro__State c) |
311 | load_state(pTHX_ Coro__State c) |
282 | { |
312 | { |
283 | PL_dowarn = c->dowarn; |
313 | PL_dowarn = c->dowarn; |
… | |
… | |
287 | PL_curstack = c->curstack; |
317 | PL_curstack = c->curstack; |
288 | PL_mainstack = c->mainstack; |
318 | PL_mainstack = c->mainstack; |
289 | PL_stack_sp = c->stack_sp; |
319 | PL_stack_sp = c->stack_sp; |
290 | PL_op = c->op; |
320 | PL_op = c->op; |
291 | PL_curpad = c->curpad; |
321 | PL_curpad = c->curpad; |
|
|
322 | PL_comppad = c->comppad; |
292 | PL_stack_base = c->stack_base; |
323 | PL_stack_base = c->stack_base; |
293 | PL_stack_max = c->stack_max; |
324 | PL_stack_max = c->stack_max; |
294 | PL_tmps_stack = c->tmps_stack; |
325 | PL_tmps_stack = c->tmps_stack; |
295 | PL_tmps_floor = c->tmps_floor; |
326 | PL_tmps_floor = c->tmps_floor; |
296 | PL_tmps_ix = c->tmps_ix; |
327 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
355 | |
386 | |
356 | PUSHs (Nullsv); |
387 | PUSHs (Nullsv); |
357 | /* this loop was inspired by pp_caller */ |
388 | /* this loop was inspired by pp_caller */ |
358 | for (;;) |
389 | for (;;) |
359 | { |
390 | { |
360 | do |
391 | while (cxix >= 0) |
361 | { |
392 | { |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
393 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
363 | |
394 | |
364 | if (CxTYPE(cx) == CXt_SUB) |
395 | if (CxTYPE(cx) == CXt_SUB) |
365 | { |
396 | { |
… | |
… | |
384 | PUSHs ((SV *)cv); |
415 | PUSHs ((SV *)cv); |
385 | |
416 | |
386 | get_padlist (cv); /* this is a monster */ |
417 | get_padlist (cv); /* this is a monster */ |
387 | } |
418 | } |
388 | } |
419 | } |
|
|
420 | #ifdef CXt_FORMAT |
389 | else if (CxTYPE(cx) == CXt_FORMAT) |
421 | else if (CxTYPE(cx) == CXt_FORMAT) |
390 | { |
422 | { |
391 | /* I never used formats, so how should I know how these are implemented? */ |
423 | /* I never used formats, so how should I know how these are implemented? */ |
392 | /* my bold guess is as a simple, plain sub... */ |
424 | /* my bold guess is as a simple, plain sub... */ |
393 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
425 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
394 | } |
426 | } |
|
|
427 | #endif |
395 | } |
428 | } |
396 | while (cxix >= 0); |
|
|
397 | |
429 | |
398 | if (top_si->si_type == PERLSI_MAIN) |
430 | if (top_si->si_type == PERLSI_MAIN) |
399 | break; |
431 | break; |
400 | |
432 | |
401 | top_si = top_si->si_prev; |
433 | top_si = top_si->si_prev; |
… | |
… | |
407 | } |
439 | } |
408 | |
440 | |
409 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
441 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
442 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
443 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
412 | |
|
|
413 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
414 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
415 | if (c->defav) |
|
|
416 | av_reify (c->defav); |
|
|
417 | |
444 | |
418 | c->dowarn = PL_dowarn; |
445 | c->dowarn = PL_dowarn; |
419 | c->in_eval = PL_in_eval; |
446 | c->in_eval = PL_in_eval; |
420 | |
447 | |
421 | c->curstackinfo = PL_curstackinfo; |
448 | c->curstackinfo = PL_curstackinfo; |
422 | c->curstack = PL_curstack; |
449 | c->curstack = PL_curstack; |
423 | c->mainstack = PL_mainstack; |
450 | c->mainstack = PL_mainstack; |
424 | c->stack_sp = PL_stack_sp; |
451 | c->stack_sp = PL_stack_sp; |
425 | c->op = PL_op; |
452 | c->op = PL_op; |
426 | c->curpad = PL_curpad; |
453 | c->curpad = PL_curpad; |
|
|
454 | c->comppad = PL_comppad; |
427 | c->stack_base = PL_stack_base; |
455 | c->stack_base = PL_stack_base; |
428 | c->stack_max = PL_stack_max; |
456 | c->stack_max = PL_stack_max; |
429 | c->tmps_stack = PL_tmps_stack; |
457 | c->tmps_stack = PL_tmps_stack; |
430 | c->tmps_floor = PL_tmps_floor; |
458 | c->tmps_floor = PL_tmps_floor; |
431 | c->tmps_ix = PL_tmps_ix; |
459 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
471 | |
499 | |
472 | New(54,PL_markstack,16,I32); |
500 | New(54,PL_markstack,16,I32); |
473 | PL_markstack_ptr = PL_markstack; |
501 | PL_markstack_ptr = PL_markstack; |
474 | PL_markstack_max = PL_markstack + 16; |
502 | PL_markstack_max = PL_markstack + 16; |
475 | |
503 | |
|
|
504 | #ifdef SET_MARK_OFFSET |
476 | SET_MARK_OFFSET; |
505 | SET_MARK_OFFSET; |
|
|
506 | #endif |
477 | |
507 | |
478 | New(54,PL_scopestack,16,I32); |
508 | New(54,PL_scopestack,16,I32); |
479 | PL_scopestack_ix = 0; |
509 | PL_scopestack_ix = 0; |
480 | PL_scopestack_max = 16; |
510 | PL_scopestack_max = 16; |
481 | |
511 | |
… | |
… | |
488 | PL_retstack_max = 8; |
518 | PL_retstack_max = 8; |
489 | } |
519 | } |
490 | |
520 | |
491 | /* |
521 | /* |
492 | * destroy the stacks, the callchain etc... |
522 | * destroy the stacks, the callchain etc... |
493 | * still there is a memleak of 128 bytes... |
|
|
494 | */ |
523 | */ |
495 | STATIC void |
524 | STATIC void |
496 | destroy_stacks(pTHX) |
525 | destroy_stacks(pTHX) |
497 | { |
526 | { |
|
|
527 | if (!IN_DESTRUCT) |
|
|
528 | { |
498 | /* is this ugly, I ask? */ |
529 | /* is this ugly, I ask? */ |
499 | while (PL_scopestack_ix) |
530 | while (PL_scopestack_ix) |
500 | LEAVE; |
531 | LEAVE; |
501 | |
532 | |
502 | /* sure it is, but more important: is it correct?? :/ */ |
533 | /* sure it is, but more important: is it correct?? :/ */ |
503 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
534 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
504 | FREETMPS; |
535 | FREETMPS; |
|
|
536 | } |
505 | |
537 | |
506 | while (PL_curstackinfo->si_next) |
538 | while (PL_curstackinfo->si_next) |
507 | PL_curstackinfo = PL_curstackinfo->si_next; |
539 | PL_curstackinfo = PL_curstackinfo->si_next; |
508 | |
540 | |
509 | while (PL_curstackinfo) |
541 | while (PL_curstackinfo) |
… | |
… | |
514 | dSP; |
546 | dSP; |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
547 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
516 | PUTBACK; /* possibly superfluous */ |
548 | PUTBACK; /* possibly superfluous */ |
517 | } |
549 | } |
518 | |
550 | |
|
|
551 | if (!IN_DESTRUCT) |
|
|
552 | { |
519 | dounwind(-1); |
553 | dounwind(-1); |
520 | |
|
|
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
554 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
555 | } |
|
|
556 | |
522 | Safefree(PL_curstackinfo->si_cxstack); |
557 | Safefree(PL_curstackinfo->si_cxstack); |
523 | Safefree(PL_curstackinfo); |
558 | Safefree(PL_curstackinfo); |
524 | PL_curstackinfo = p; |
559 | PL_curstackinfo = p; |
525 | } |
560 | } |
526 | |
561 | |
… | |
… | |
541 | stack->refcnt = 1; |
576 | stack->refcnt = 1; |
542 | stack->usecnt = 1; |
577 | stack->usecnt = 1; |
543 | stack->gencnt = ctx->gencnt = 0; |
578 | stack->gencnt = ctx->gencnt = 0; |
544 | if (alloc) |
579 | if (alloc) |
545 | { |
580 | { |
546 | #ifdef HAVE_MMAP |
581 | #if HAVE_MMAP |
547 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
582 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
548 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
583 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
549 | if (stack->sptr == (void *)-1) |
584 | if (stack->sptr == (void *)-1) |
550 | #endif |
585 | #endif |
551 | { |
586 | { |
552 | /*FIXME*//*D*//* reasonable stack size! */ |
587 | /*FIXME*//*D*//* reasonable stack size! */ |
553 | stack->ssize = -4096 * sizeof (long); |
588 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
573 | { |
608 | { |
574 | #ifdef HAVE_MMAP |
609 | #ifdef HAVE_MMAP |
575 | if (stack->ssize > 0 && stack->sptr) |
610 | if (stack->ssize > 0 && stack->sptr) |
576 | munmap (stack->sptr, stack->ssize); |
611 | munmap (stack->sptr, stack->ssize); |
577 | else |
612 | else |
578 | #else |
613 | #endif |
579 | Safefree (stack->sptr); |
614 | Safefree (stack->sptr); |
580 | #endif |
615 | |
581 | Safefree (stack); |
616 | Safefree (stack); |
582 | } |
617 | } |
583 | else if (ctx->gencnt == stack->gencnt) |
618 | else if (ctx->gencnt == stack->gencnt) |
584 | --stack->usecnt; |
619 | --stack->usecnt; |
585 | } |
620 | } |
… | |
… | |
597 | |
632 | |
598 | coro_init_stacks (aTHX); |
633 | coro_init_stacks (aTHX); |
599 | /*PL_curcop = 0;*/ |
634 | /*PL_curcop = 0;*/ |
600 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
635 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
601 | SvREFCNT_dec (GvAV (PL_defgv)); |
636 | SvREFCNT_dec (GvAV (PL_defgv)); |
602 | GvAV (PL_defgv) = ctx->args; |
637 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
603 | |
638 | |
604 | SPAGAIN; |
639 | SPAGAIN; |
605 | |
640 | |
606 | if (ctx->stack) |
641 | if (ctx->stack) |
607 | { |
642 | { |
… | |
… | |
634 | * that doesn't matter, though, since it is only |
669 | * that doesn't matter, though, since it is only |
635 | * pp_nextstate and we never return... |
670 | * pp_nextstate and we never return... |
636 | * ah yes, and I don't care anyways ;) |
671 | * ah yes, and I don't care anyways ;) |
637 | */ |
672 | */ |
638 | PUTBACK; |
673 | PUTBACK; |
639 | PL_op = pp_entersub(); |
674 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
640 | SPAGAIN; |
675 | SPAGAIN; |
641 | |
676 | |
642 | ENTER; /* necessary e.g. for dounwind */ |
677 | ENTER; /* necessary e.g. for dounwind */ |
643 | } |
678 | } |
644 | } |
679 | } |
… | |
… | |
648 | { |
683 | { |
649 | /* |
684 | /* |
650 | * this is a _very_ stripped down perl interpreter ;) |
685 | * this is a _very_ stripped down perl interpreter ;) |
651 | */ |
686 | */ |
652 | Coro__State ctx = (Coro__State)arg; |
687 | Coro__State ctx = (Coro__State)arg; |
|
|
688 | JMPENV coro_start_env; |
653 | |
689 | |
654 | /*FIXME*//* must set up top_env here */ |
690 | PL_top_env = &ctx->start_env; |
|
|
691 | |
655 | ctx->cursp = 0; |
692 | ctx->cursp = 0; |
656 | PL_op = PL_op->op_next; |
693 | PL_op = PL_op->op_next; |
657 | CALLRUNOPS(aTHX); |
694 | CALLRUNOPS(aTHX); |
658 | |
695 | |
659 | abort (); |
696 | abort (); |
660 | } |
697 | } |
661 | |
698 | |
662 | STATIC void |
699 | STATIC void |
663 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
700 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
664 | { |
701 | { |
665 | dSP; |
|
|
666 | dSTACKLEVEL; |
702 | dSTACKLEVEL; |
|
|
703 | static struct coro *xnext; |
667 | |
704 | |
668 | if (prev != next) |
705 | if (prev != next) |
669 | { |
706 | { |
|
|
707 | xnext = next; |
|
|
708 | |
670 | if (next->mainstack) |
709 | if (next->mainstack) |
671 | { |
710 | { |
672 | SAVE (prev, flags); |
711 | SAVE (prev, flags); |
673 | LOAD (next); |
712 | LOAD (next); |
674 | |
713 | |
… | |
… | |
697 | continue_coro, (void *)next, |
736 | continue_coro, (void *)next, |
698 | next->stack->sptr, labs (next->stack->ssize)); |
737 | next->stack->sptr, labs (next->stack->ssize)); |
699 | } |
738 | } |
700 | |
739 | |
701 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
740 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
741 | /* don't add any code here */ |
702 | } |
742 | } |
703 | |
743 | |
704 | } |
744 | } |
705 | else if (next->tmps_ix == -2) |
745 | else if (next->tmps_ix == -2) |
706 | croak ("tried to transfer to running coroutine"); |
746 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
713 | if (!prev->stack) |
753 | if (!prev->stack) |
714 | allocate_stack (prev, 0); |
754 | allocate_stack (prev, 0); |
715 | |
755 | |
716 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
756 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
717 | { |
757 | { |
|
|
758 | PL_top_env = &next->start_env; |
|
|
759 | |
718 | setup_coro (next); |
760 | setup_coro (next); |
719 | |
761 | |
720 | prev->stack->refcnt++; |
762 | prev->stack->refcnt++; |
721 | prev->stack->usecnt++; |
763 | prev->stack->usecnt++; |
722 | next->stack = prev->stack; |
764 | next->stack = prev->stack; |
723 | next->gencnt = prev->gencnt; |
765 | next->gencnt = prev->gencnt; |
724 | } |
766 | } |
725 | else |
767 | else |
726 | { |
768 | { |
|
|
769 | assert (!next->stack); |
727 | allocate_stack (next, 1); |
770 | allocate_stack (next, 1); |
728 | coro_create (&(next->stack->cctx), |
771 | coro_create (&(next->stack->cctx), |
729 | setup_coro, (void *)next, |
772 | setup_coro, (void *)next, |
730 | next->stack->sptr, labs (next->stack->ssize)); |
773 | next->stack->sptr, labs (next->stack->ssize)); |
731 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
774 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
775 | /* don't add any code here */ |
732 | } |
776 | } |
733 | } |
777 | } |
734 | else |
778 | else |
735 | setup_coro (next); |
779 | setup_coro (next); |
736 | } |
780 | } |
|
|
781 | |
|
|
782 | /* |
|
|
783 | * xnext is now either prev or next, depending on wether |
|
|
784 | * we switched the c stack or not. that's why I use a global |
|
|
785 | * variable, that should become thread-specific at one point. |
|
|
786 | */ |
|
|
787 | xnext->cursp = stacklevel; |
737 | } |
788 | } |
738 | |
789 | |
739 | next->cursp = stacklevel; |
790 | if (coro_mortal) |
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 | { |
791 | { |
747 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
792 | SvREFCNT_dec (coro_mortal); |
748 | |
793 | coro_mortal = 0; |
749 | if (!he) |
|
|
750 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
|
|
751 | |
|
|
752 | arg = HeVAL(he); |
|
|
753 | } |
794 | } |
754 | |
795 | } |
|
|
796 | |
|
|
797 | #define SV_CORO(sv,func) \ |
|
|
798 | do { \ |
|
|
799 | if (SvROK (sv)) \ |
|
|
800 | sv = SvRV (sv); \ |
|
|
801 | \ |
|
|
802 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
803 | { \ |
|
|
804 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
|
|
805 | \ |
|
|
806 | if (!he) \ |
|
|
807 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
|
|
808 | \ |
|
|
809 | (sv) = SvRV (HeVAL(he)); \ |
|
|
810 | } \ |
|
|
811 | \ |
755 | /* must also be changed inside Coro::Cont::yield */ |
812 | /* must also be changed inside Coro::Cont::yield */ \ |
756 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
813 | if (!SvOBJECT(sv) || SvSTASH(sv) != 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); |
814 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
760 | } |
815 | \ |
|
|
816 | } while(0) |
|
|
817 | |
|
|
818 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
761 | |
819 | |
762 | static void |
820 | static void |
763 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
821 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
764 | { |
822 | { |
765 | transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), |
823 | SV_CORO (prev, "Coro::transfer"); |
766 | sv_to_coro (next, "Coro::transfer", "next"), |
824 | SV_CORO (next, "Coro::transfer"); |
767 | flags); |
825 | |
|
|
826 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
768 | } |
827 | } |
769 | |
828 | |
770 | /** Coro ********************************************************************/ |
829 | /** Coro ********************************************************************/ |
771 | |
830 | |
772 | #define PRIO_MAX 3 |
831 | #define PRIO_MAX 3 |
… | |
… | |
777 | #define PRIO_MIN -4 |
836 | #define PRIO_MIN -4 |
778 | |
837 | |
779 | /* for Coro.pm */ |
838 | /* for Coro.pm */ |
780 | static GV *coro_current, *coro_idle; |
839 | static GV *coro_current, *coro_idle; |
781 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
840 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
841 | static int coro_nready; |
782 | |
842 | |
783 | static void |
843 | static void |
784 | coro_enq (SV *sv) |
844 | coro_enq (SV *sv) |
785 | { |
845 | { |
786 | if (SvROK (sv)) |
|
|
787 | { |
|
|
788 | SV *hv = SvRV (sv); |
|
|
789 | if (SvTYPE (hv) == SVt_PVHV) |
846 | if (SvTYPE (sv) == SVt_PVHV) |
790 | { |
847 | { |
791 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
848 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
792 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
849 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
793 | |
850 | |
794 | prio = prio > PRIO_MAX ? PRIO_MAX |
851 | prio = prio > PRIO_MAX ? PRIO_MAX |
795 | : prio < PRIO_MIN ? PRIO_MIN |
852 | : prio < PRIO_MIN ? PRIO_MIN |
796 | : prio; |
853 | : prio; |
797 | |
854 | |
798 | av_push (coro_ready [prio - PRIO_MIN], sv); |
855 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
856 | coro_nready++; |
799 | |
857 | |
800 | return; |
858 | return; |
801 | } |
|
|
802 | } |
859 | } |
803 | |
860 | |
804 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
861 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
805 | } |
862 | } |
806 | |
863 | |
… | |
… | |
813 | if (min_prio < 0) |
870 | if (min_prio < 0) |
814 | min_prio = 0; |
871 | min_prio = 0; |
815 | |
872 | |
816 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
873 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
817 | if (av_len (coro_ready[prio]) >= 0) |
874 | if (av_len (coro_ready[prio]) >= 0) |
|
|
875 | { |
|
|
876 | coro_nready--; |
818 | return av_shift (coro_ready[prio]); |
877 | return av_shift (coro_ready[prio]); |
|
|
878 | } |
819 | |
879 | |
820 | return 0; |
880 | return 0; |
821 | } |
881 | } |
822 | |
882 | |
823 | static void |
883 | static void |
824 | api_ready (SV *coro) |
884 | api_ready (SV *coro) |
825 | { |
885 | { |
|
|
886 | if (SvROK (coro)) |
|
|
887 | coro = SvRV (coro); |
|
|
888 | |
826 | coro_enq (SvREFCNT_inc (coro)); |
889 | coro_enq (SvREFCNT_inc (coro)); |
827 | } |
890 | } |
828 | |
891 | |
829 | static void |
892 | static void |
830 | api_schedule (int cede) |
893 | api_schedule (void) |
831 | { |
894 | { |
832 | SV *prev, *next; |
895 | SV *prev, *next; |
833 | |
896 | |
834 | prev = GvSV (coro_current); |
897 | prev = SvRV (GvSV (coro_current)); |
835 | |
|
|
836 | if (cede) |
|
|
837 | coro_enq (SvREFCNT_inc (prev)); |
|
|
838 | |
|
|
839 | next = coro_deq (PRIO_MIN); |
898 | next = coro_deq (PRIO_MIN); |
840 | |
899 | |
841 | if (!next) |
900 | if (!next) |
842 | next = SvREFCNT_inc (GvSV (coro_idle)); |
901 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
843 | |
902 | |
844 | GvSV (coro_current) = SvREFCNT_inc (next); |
903 | /* free this only after the transfer */ |
845 | transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
904 | coro_mortal = prev; |
846 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
905 | SV_CORO (prev, "Coro::schedule"); |
|
|
906 | |
|
|
907 | SvRV (GvSV (coro_current)) = next; |
|
|
908 | |
|
|
909 | SV_CORO (next, "Coro::schedule"); |
|
|
910 | |
|
|
911 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
847 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
912 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
848 | SvREFCNT_dec (next); |
913 | } |
849 | SvREFCNT_dec (prev); |
914 | |
|
|
915 | static void |
|
|
916 | api_cede (void) |
|
|
917 | { |
|
|
918 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
919 | |
|
|
920 | api_schedule (); |
850 | } |
921 | } |
851 | |
922 | |
852 | MODULE = Coro::State PACKAGE = Coro::State |
923 | MODULE = Coro::State PACKAGE = Coro::State |
853 | |
924 | |
854 | PROTOTYPES: ENABLE |
925 | PROTOTYPES: ENABLE |
… | |
… | |
867 | if (!padlist_cache) |
938 | if (!padlist_cache) |
868 | padlist_cache = newHV (); |
939 | padlist_cache = newHV (); |
869 | |
940 | |
870 | main_mainstack = PL_mainstack; |
941 | main_mainstack = PL_mainstack; |
871 | |
942 | |
872 | { |
|
|
873 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
874 | |
|
|
875 | coroapi.ver = CORO_API_VERSION - 1; |
943 | coroapi.ver = CORO_API_VERSION; |
876 | coroapi.transfer = api_transfer; |
944 | 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 | } |
|
|
884 | } |
945 | } |
885 | |
946 | |
886 | Coro::State |
947 | Coro::State |
887 | _newprocess(args) |
948 | _newprocess(args) |
888 | SV * args |
949 | SV * args |
… | |
… | |
891 | Coro__State coro; |
952 | Coro__State coro; |
892 | |
953 | |
893 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
954 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
894 | croak ("Coro::State::_newprocess expects an arrayref"); |
955 | croak ("Coro::State::_newprocess expects an arrayref"); |
895 | |
956 | |
896 | New (0, coro, 1, struct coro); |
957 | Newz (0, coro, 1, struct coro); |
897 | |
958 | |
898 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
959 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
899 | coro->mainstack = 0; /* actual work is done inside transfer */ |
960 | coro->mainstack = 0; /* actual work is done inside transfer */ |
900 | coro->stack = 0; |
961 | coro->stack = 0; |
901 | |
962 | |
|
|
963 | /* same as JMPENV_BOOTSTRAP */ |
|
|
964 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
965 | //Zero(&coro->start_env, 1, JMPENV); |
|
|
966 | coro->start_env.je_ret = -1; |
|
|
967 | coro->start_env.je_mustcatch = TRUE; |
|
|
968 | |
902 | RETVAL = coro; |
969 | RETVAL = coro; |
903 | OUTPUT: |
970 | OUTPUT: |
904 | RETVAL |
971 | RETVAL |
905 | |
972 | |
906 | void |
973 | void |
907 | transfer(prev, next, flags) |
974 | transfer(prev, next, flags) |
908 | Coro::State_or_hashref prev |
975 | SV *prev |
909 | Coro::State_or_hashref next |
976 | SV *next |
910 | int flags |
977 | int flags |
911 | PROTOTYPE: @ |
978 | PROTOTYPE: @ |
912 | CODE: |
979 | CODE: |
913 | PUTBACK; |
980 | PUTBACK; |
|
|
981 | SV_CORO (next, "Coro::transfer"); |
|
|
982 | SV_CORO (prev, "Coro::transfer"); |
914 | transfer (aTHX_ prev, next, flags); |
983 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
915 | SPAGAIN; |
984 | SPAGAIN; |
916 | |
985 | |
917 | void |
986 | void |
918 | DESTROY(coro) |
987 | DESTROY(coro) |
919 | Coro::State coro |
988 | Coro::State coro |
… | |
… | |
921 | |
990 | |
922 | if (coro->mainstack && coro->mainstack != main_mainstack) |
991 | if (coro->mainstack && coro->mainstack != main_mainstack) |
923 | { |
992 | { |
924 | struct coro temp; |
993 | struct coro temp; |
925 | |
994 | |
|
|
995 | PUTBACK; |
926 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
996 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
927 | LOAD(aTHX_ coro); |
997 | LOAD(aTHX_ coro); |
|
|
998 | SPAGAIN; |
928 | |
999 | |
929 | destroy_stacks (aTHX); |
1000 | destroy_stacks (aTHX); |
930 | |
1001 | |
931 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1002 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
1003 | SPAGAIN; |
932 | |
1004 | |
933 | coro->mainstack = 0; |
1005 | coro->mainstack = 0; |
934 | } |
1006 | } |
935 | |
1007 | |
936 | deallocate_stack (coro); |
1008 | deallocate_stack (coro); |
937 | |
1009 | SvREFCNT_dec (coro->args); |
938 | Safefree (coro); |
1010 | Safefree (coro); |
939 | |
1011 | |
940 | void |
1012 | void |
941 | flush() |
1013 | flush() |
942 | CODE: |
1014 | CODE: |
… | |
… | |
969 | SV *sv; |
1041 | SV *sv; |
970 | AV *defav = GvAV (PL_defgv); |
1042 | AV *defav = GvAV (PL_defgv); |
971 | struct coro *prev, *next; |
1043 | struct coro *prev, *next; |
972 | |
1044 | |
973 | if (!returnstk) |
1045 | if (!returnstk) |
974 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
975 | |
1047 | |
976 | /* set up @_ -- ugly */ |
1048 | /* set up @_ -- ugly */ |
977 | av_clear (defav); |
1049 | av_clear (defav); |
978 | av_fill (defav, items - 1); |
1050 | av_fill (defav, items - 1); |
979 | while (items--) |
1051 | while (items--) |
… | |
… | |
1006 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1078 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1007 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1079 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1008 | |
1080 | |
1009 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1081 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1010 | coro_ready[i] = newAV (); |
1082 | coro_ready[i] = newAV (); |
|
|
1083 | |
|
|
1084 | { |
|
|
1085 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
1086 | |
|
|
1087 | coroapi.schedule = api_schedule; |
|
|
1088 | coroapi.cede = api_cede; |
|
|
1089 | coroapi.ready = api_ready; |
|
|
1090 | coroapi.nready = &coro_nready; |
|
|
1091 | coroapi.current = coro_current; |
|
|
1092 | |
|
|
1093 | GCoroAPI = &coroapi; |
|
|
1094 | sv_setiv(sv, (IV)&coroapi); |
|
|
1095 | SvREADONLY_on(sv); |
|
|
1096 | } |
1011 | } |
1097 | } |
1012 | |
1098 | |
1013 | void |
1099 | void |
1014 | ready(self) |
1100 | ready(self) |
1015 | SV * self |
1101 | SV * self |
|
|
1102 | PROTOTYPE: $ |
1016 | CODE: |
1103 | CODE: |
1017 | api_ready (self); |
1104 | api_ready (self); |
1018 | |
1105 | |
|
|
1106 | int |
|
|
1107 | nready(...) |
|
|
1108 | PROTOTYPE: |
|
|
1109 | CODE: |
|
|
1110 | RETVAL = coro_nready; |
|
|
1111 | OUTPUT: |
|
|
1112 | RETVAL |
|
|
1113 | |
1019 | void |
1114 | void |
1020 | schedule(...) |
1115 | schedule(...) |
1021 | ALIAS: |
1116 | PROTOTYPE: |
1022 | cede = 1 |
|
|
1023 | CODE: |
1117 | CODE: |
1024 | api_schedule (ix); |
1118 | api_schedule (); |
1025 | |
1119 | |
|
|
1120 | void |
|
|
1121 | cede(...) |
|
|
1122 | PROTOTYPE: |
|
|
1123 | CODE: |
|
|
1124 | api_cede (); |
|
|
1125 | |