… | |
… | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
9 | #if USE_VALGRIND |
10 | # include <valgrind/valgrind.h> |
10 | # include <valgrind/valgrind.h> |
11 | #endif |
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
12 | |
15 | |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
14 | (PERL_REVISION > (a) \ |
17 | (PERL_REVISION > (a) \ |
15 | || (PERL_REVISION == (a) \ |
18 | || (PERL_REVISION == (a) \ |
16 | && (PERL_VERSION > (b) \ |
19 | && (PERL_VERSION > (b) \ |
… | |
… | |
98 | #else |
101 | #else |
99 | # define LOCK (void)0 |
102 | # define LOCK (void)0 |
100 | # define UNLOCK (void)0 |
103 | # define UNLOCK (void)0 |
101 | #endif |
104 | #endif |
102 | |
105 | |
|
|
106 | struct io_state |
|
|
107 | { |
|
|
108 | int errorno; |
|
|
109 | I32 laststype; |
|
|
110 | int laststatval; |
|
|
111 | Stat_t statcache; |
|
|
112 | }; |
|
|
113 | |
103 | static struct CoroAPI coroapi; |
114 | static struct CoroAPI coroapi; |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
115 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
105 | static HV *coro_state_stash, *coro_stash; |
116 | static HV *coro_state_stash, *coro_stash; |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
117 | static SV *coro_mortal; /* will be freed after next transfer */ |
107 | |
118 | |
… | |
… | |
127 | int valgrind_id; |
138 | int valgrind_id; |
128 | #endif |
139 | #endif |
129 | } coro_cctx; |
140 | } coro_cctx; |
130 | |
141 | |
131 | enum { |
142 | enum { |
132 | CF_RUNNING, /* coroutine is running */ |
143 | CF_RUNNING = 0x0001, /* coroutine is running */ |
133 | CF_READY, /* coroutine is ready */ |
144 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
145 | CF_NEW = 0x0004, /* ahs never been switched to */ |
134 | }; |
146 | }; |
135 | |
147 | |
136 | /* this is a structure representing a perl-level coroutine */ |
148 | /* this is a structure representing a perl-level coroutine */ |
137 | struct coro { |
149 | struct coro { |
138 | /* the c coroutine allocated to this perl coroutine, if any */ |
150 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
277 | } |
289 | } |
278 | |
290 | |
279 | #define SB do { |
291 | #define SB do { |
280 | #define SE } while (0) |
292 | #define SE } while (0) |
281 | |
293 | |
282 | #define LOAD(state) load_state((state)); |
294 | #define LOAD(state) load_state ((state)) |
283 | #define SAVE(state,flags) save_state((state),(flags)); |
295 | #define SAVE(state,flags) save_state ((state),(flags)) |
284 | |
296 | |
285 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
297 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
286 | |
298 | |
287 | static void |
299 | static void |
288 | load_state(Coro__State c) |
300 | load_state (Coro__State c) |
289 | { |
301 | { |
290 | #define VAR(name,type) PL_ ## name = c->name; |
302 | #define VAR(name,type) PL_ ## name = c->name; |
291 | # include "state.h" |
303 | # include "state.h" |
292 | #undef VAR |
304 | #undef VAR |
293 | |
305 | |
… | |
… | |
310 | PUTBACK; |
322 | PUTBACK; |
311 | } |
323 | } |
312 | } |
324 | } |
313 | |
325 | |
314 | static void |
326 | static void |
315 | save_state(Coro__State c, int flags) |
327 | save_state (Coro__State c, int flags) |
316 | { |
328 | { |
317 | { |
329 | { |
318 | dSP; |
330 | dSP; |
319 | I32 cxix = cxstack_ix; |
331 | I32 cxix = cxstack_ix; |
320 | PERL_CONTEXT *ccstk = cxstack; |
332 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
331 | { |
343 | { |
332 | while (cxix >= 0) |
344 | while (cxix >= 0) |
333 | { |
345 | { |
334 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
346 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
335 | |
347 | |
336 | if (CxTYPE(cx) == CXt_SUB) |
348 | if (CxTYPE (cx) == CXt_SUB) |
337 | { |
349 | { |
338 | CV *cv = cx->blk_sub.cv; |
350 | CV *cv = cx->blk_sub.cv; |
339 | |
351 | |
340 | if (CvDEPTH (cv)) |
352 | if (CvDEPTH (cv)) |
341 | { |
353 | { |
342 | EXTEND (SP, 3); |
354 | EXTEND (SP, 3); |
343 | |
355 | |
344 | PUSHs ((SV *)CvPADLIST(cv)); |
356 | PUSHs ((SV *)CvPADLIST (cv)); |
345 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
357 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
346 | PUSHs ((SV *)cv); |
358 | PUSHs ((SV *)cv); |
347 | |
359 | |
348 | CvDEPTH (cv) = 0; |
360 | CvDEPTH (cv) = 0; |
349 | get_padlist (cv); |
361 | get_padlist (cv); |
350 | } |
362 | } |
351 | } |
363 | } |
352 | #ifdef CXt_FORMAT |
364 | #ifdef CXt_FORMAT |
353 | else if (CxTYPE(cx) == CXt_FORMAT) |
365 | else if (CxTYPE (cx) == CXt_FORMAT) |
354 | { |
366 | { |
355 | /* I never used formats, so how should I know how these are implemented? */ |
367 | /* I never used formats, so how should I know how these are implemented? */ |
356 | /* my bold guess is as a simple, plain sub... */ |
368 | /* my bold guess is as a simple, plain sub... */ |
357 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
369 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
358 | } |
370 | } |
… | |
… | |
386 | * not usually need a lot of stackspace. |
398 | * not usually need a lot of stackspace. |
387 | */ |
399 | */ |
388 | static void |
400 | static void |
389 | coro_init_stacks () |
401 | coro_init_stacks () |
390 | { |
402 | { |
391 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
403 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
392 | PL_curstackinfo->si_type = PERLSI_MAIN; |
404 | PL_curstackinfo->si_type = PERLSI_MAIN; |
393 | PL_curstack = PL_curstackinfo->si_stack; |
405 | PL_curstack = PL_curstackinfo->si_stack; |
394 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
406 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
395 | |
407 | |
396 | PL_stack_base = AvARRAY(PL_curstack); |
408 | PL_stack_base = AvARRAY(PL_curstack); |
397 | PL_stack_sp = PL_stack_base; |
409 | PL_stack_sp = PL_stack_base; |
398 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
410 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
399 | |
411 | |
400 | New(50,PL_tmps_stack,96,SV*); |
412 | New(50,PL_tmps_stack,128,SV*); |
401 | PL_tmps_floor = -1; |
413 | PL_tmps_floor = -1; |
402 | PL_tmps_ix = -1; |
414 | PL_tmps_ix = -1; |
403 | PL_tmps_max = 96; |
415 | PL_tmps_max = 128; |
404 | |
416 | |
405 | New(54,PL_markstack,16,I32); |
417 | New(54,PL_markstack,32,I32); |
406 | PL_markstack_ptr = PL_markstack; |
418 | PL_markstack_ptr = PL_markstack; |
407 | PL_markstack_max = PL_markstack + 16; |
419 | PL_markstack_max = PL_markstack + 32; |
408 | |
420 | |
409 | #ifdef SET_MARK_OFFSET |
421 | #ifdef SET_MARK_OFFSET |
410 | SET_MARK_OFFSET; |
422 | SET_MARK_OFFSET; |
411 | #endif |
423 | #endif |
412 | |
424 | |
413 | New(54,PL_scopestack,16,I32); |
425 | New(54,PL_scopestack,32,I32); |
414 | PL_scopestack_ix = 0; |
426 | PL_scopestack_ix = 0; |
415 | PL_scopestack_max = 16; |
427 | PL_scopestack_max = 32; |
416 | |
428 | |
417 | New(54,PL_savestack,96,ANY); |
429 | New(54,PL_savestack,64,ANY); |
418 | PL_savestack_ix = 0; |
430 | PL_savestack_ix = 0; |
419 | PL_savestack_max = 96; |
431 | PL_savestack_max = 64; |
420 | |
432 | |
421 | #if !PERL_VERSION_ATLEAST (5,9,0) |
433 | #if !PERL_VERSION_ATLEAST (5,9,0) |
422 | New(54,PL_retstack,8,OP*); |
434 | New(54,PL_retstack,16,OP*); |
423 | PL_retstack_ix = 0; |
435 | PL_retstack_ix = 0; |
424 | PL_retstack_max = 8; |
436 | PL_retstack_max = 16; |
425 | #endif |
437 | #endif |
426 | } |
438 | } |
427 | |
439 | |
428 | /* |
440 | /* |
429 | * destroy the stacks, the callchain etc... |
441 | * destroy the stacks, the callchain etc... |
… | |
… | |
482 | * emulate part of the perl startup here. |
494 | * emulate part of the perl startup here. |
483 | */ |
495 | */ |
484 | |
496 | |
485 | coro_init_stacks (); |
497 | coro_init_stacks (); |
486 | |
498 | |
487 | PL_curcop = 0; |
499 | PL_curcop = &PL_compiling; |
488 | PL_in_eval = 0; |
500 | PL_in_eval = EVAL_NULL; |
489 | PL_curpm = 0; |
501 | PL_curpm = 0; |
|
|
502 | PL_localizing = 0; |
|
|
503 | PL_dirty = 0; |
|
|
504 | PL_restartop = 0; |
490 | |
505 | |
491 | { |
506 | { |
492 | dSP; |
507 | dSP; |
493 | LOGOP myop; |
508 | LOGOP myop; |
494 | |
509 | |
495 | /* I have no idea why this is needed, but it is */ |
|
|
496 | PUSHMARK (SP); |
|
|
497 | |
|
|
498 | SvREFCNT_dec (GvAV (PL_defgv)); |
510 | SvREFCNT_dec (GvAV (PL_defgv)); |
499 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
511 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
500 | |
512 | |
501 | Zero (&myop, 1, LOGOP); |
513 | Zero (&myop, 1, LOGOP); |
502 | myop.op_next = Nullop; |
514 | myop.op_next = Nullop; |
503 | myop.op_flags = OPf_WANT_VOID; |
515 | myop.op_flags = OPf_WANT_VOID; |
504 | |
516 | |
505 | PL_op = (OP *)&myop; |
|
|
506 | |
|
|
507 | PUSHMARK (SP); |
517 | PUSHMARK (SP); |
508 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
518 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
509 | PUTBACK; |
519 | PUTBACK; |
|
|
520 | PL_op = (OP *)&myop; |
510 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
521 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
511 | SPAGAIN; |
522 | SPAGAIN; |
512 | |
|
|
513 | ENTER; /* necessary e.g. for dounwind */ |
|
|
514 | } |
523 | } |
|
|
524 | |
|
|
525 | ENTER; /* necessary e.g. for dounwind */ |
515 | } |
526 | } |
516 | |
527 | |
517 | static void |
528 | static void |
518 | free_coro_mortal () |
529 | free_coro_mortal () |
519 | { |
530 | { |
… | |
… | |
522 | SvREFCNT_dec (coro_mortal); |
533 | SvREFCNT_dec (coro_mortal); |
523 | coro_mortal = 0; |
534 | coro_mortal = 0; |
524 | } |
535 | } |
525 | } |
536 | } |
526 | |
537 | |
|
|
538 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
527 | static void NOINLINE |
539 | static void NOINLINE |
528 | prepare_cctx (coro_cctx *cctx) |
540 | prepare_cctx (coro_cctx *cctx) |
529 | { |
541 | { |
530 | dSP; |
542 | dSP; |
531 | LOGOP myop; |
543 | LOGOP myop; |
532 | |
544 | |
533 | Zero (&myop, 1, LOGOP); |
545 | Zero (&myop, 1, LOGOP); |
534 | myop.op_next = PL_op; |
546 | myop.op_next = PL_op; |
535 | myop.op_flags = OPf_WANT_VOID; |
547 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
536 | |
|
|
537 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
538 | |
548 | |
539 | PUSHMARK (SP); |
549 | PUSHMARK (SP); |
|
|
550 | EXTEND (SP, 2); |
|
|
551 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
540 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
552 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
541 | PUTBACK; |
553 | PUTBACK; |
|
|
554 | PL_op = (OP *)&myop; |
542 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
555 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
543 | SPAGAIN; |
556 | SPAGAIN; |
544 | } |
557 | } |
545 | |
558 | |
546 | static void |
559 | static void |
547 | coro_run (void *arg) |
560 | coro_run (void *arg) |
548 | { |
561 | { |
549 | /* coro_run is the alternative epilogue of transfer() */ |
562 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
550 | UNLOCK; |
563 | UNLOCK; |
551 | |
564 | |
552 | /* |
565 | /* |
553 | * this is a _very_ stripped down perl interpreter ;) |
566 | * this is a _very_ stripped down perl interpreter ;) |
554 | */ |
567 | */ |
… | |
… | |
556 | |
569 | |
557 | /* inject call to cctx_init */ |
570 | /* inject call to cctx_init */ |
558 | prepare_cctx ((coro_cctx *)arg); |
571 | prepare_cctx ((coro_cctx *)arg); |
559 | |
572 | |
560 | /* somebody will hit me for both perl_run and PL_restartop */ |
573 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
574 | PL_restartop = PL_op; |
561 | perl_run (PL_curinterp); |
575 | perl_run (PL_curinterp); |
562 | |
576 | |
563 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
577 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
564 | abort (); |
578 | abort (); |
565 | } |
579 | } |
… | |
… | |
574 | New (0, cctx, 1, coro_cctx); |
588 | New (0, cctx, 1, coro_cctx); |
575 | |
589 | |
576 | #if HAVE_MMAP |
590 | #if HAVE_MMAP |
577 | |
591 | |
578 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
592 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
579 | /* mmap suppsedly does allocate-on-write for us */ |
593 | /* mmap supposedly does allocate-on-write for us */ |
580 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
594 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
581 | |
595 | |
582 | if (cctx->sptr == (void *)-1) |
596 | if (cctx->sptr == (void *)-1) |
583 | { |
597 | { |
584 | perror ("FATAL: unable to mmap stack for coroutine"); |
598 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
613 | |
627 | |
614 | return cctx; |
628 | return cctx; |
615 | } |
629 | } |
616 | |
630 | |
617 | static void |
631 | static void |
618 | cctx_free (coro_cctx *cctx) |
632 | cctx_destroy (coro_cctx *cctx) |
619 | { |
633 | { |
620 | if (!cctx) |
634 | if (!cctx) |
621 | return; |
635 | return; |
622 | |
636 | |
623 | --cctx_count; |
637 | --cctx_count; |
… | |
… | |
640 | { |
654 | { |
641 | coro_cctx *cctx; |
655 | coro_cctx *cctx; |
642 | |
656 | |
643 | if (cctx_first) |
657 | if (cctx_first) |
644 | { |
658 | { |
645 | --cctx_idle; |
|
|
646 | cctx = cctx_first; |
659 | cctx = cctx_first; |
647 | cctx_first = cctx->next; |
660 | cctx_first = cctx->next; |
|
|
661 | --cctx_idle; |
648 | } |
662 | } |
649 | else |
663 | else |
650 | { |
664 | { |
651 | cctx = cctx_new (); |
665 | cctx = cctx_new (); |
652 | PL_op = PL_op->op_next; |
666 | PL_op = PL_op->op_next; |
… | |
… | |
656 | } |
670 | } |
657 | |
671 | |
658 | static void |
672 | static void |
659 | cctx_put (coro_cctx *cctx) |
673 | cctx_put (coro_cctx *cctx) |
660 | { |
674 | { |
|
|
675 | /* free another cctx if overlimit */ |
|
|
676 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
677 | { |
|
|
678 | coro_cctx *first = cctx_first; |
|
|
679 | cctx_first = first->next; |
|
|
680 | --cctx_idle; |
|
|
681 | |
|
|
682 | assert (!first->inuse); |
|
|
683 | cctx_destroy (first); |
|
|
684 | } |
|
|
685 | |
661 | ++cctx_idle; |
686 | ++cctx_idle; |
662 | cctx->next = cctx_first; |
687 | cctx->next = cctx_first; |
663 | cctx_first = cctx; |
688 | cctx_first = cctx; |
664 | } |
689 | } |
665 | |
690 | |
… | |
… | |
674 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
699 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
675 | else if (prev != next) |
700 | else if (prev != next) |
676 | { |
701 | { |
677 | coro_cctx *prev__cctx; |
702 | coro_cctx *prev__cctx; |
678 | |
703 | |
679 | if (!prev->cctx) |
704 | if (prev->flags & CF_NEW) |
680 | { |
705 | { |
681 | /* create a new empty context */ |
706 | /* create a new empty context */ |
682 | Newz (0, prev->cctx, 1, coro_cctx); |
707 | Newz (0, prev->cctx, 1, coro_cctx); |
683 | prev->cctx->inuse = 1; |
708 | prev->cctx->inuse = 1; |
|
|
709 | prev->flags &= ~CF_NEW; |
684 | prev->flags |= CF_RUNNING; |
710 | prev->flags |= CF_RUNNING; |
685 | } |
711 | } |
686 | |
712 | |
|
|
713 | /*TODO: must not croak here */ |
687 | if (!prev->flags & CF_RUNNING) |
714 | if (!prev->flags & CF_RUNNING) |
688 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
715 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
689 | |
716 | |
690 | if (next->flags & CF_RUNNING) |
717 | if (next->flags & CF_RUNNING) |
691 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
718 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
… | |
… | |
693 | prev->flags &= ~CF_RUNNING; |
720 | prev->flags &= ~CF_RUNNING; |
694 | next->flags |= CF_RUNNING; |
721 | next->flags |= CF_RUNNING; |
695 | |
722 | |
696 | LOCK; |
723 | LOCK; |
697 | |
724 | |
698 | if (next->mainstack) |
725 | if (next->flags & CF_NEW) |
699 | { |
|
|
700 | /* coroutine already started */ |
|
|
701 | SAVE (prev, flags); |
|
|
702 | LOAD (next); |
|
|
703 | } |
|
|
704 | else |
|
|
705 | { |
726 | { |
706 | /* need to start coroutine */ |
727 | /* need to start coroutine */ |
|
|
728 | next->flags &= ~CF_NEW; |
707 | /* first get rid of the old state */ |
729 | /* first get rid of the old state */ |
708 | SAVE (prev, -1); |
730 | SAVE (prev, -1); |
709 | /* setup coroutine call */ |
731 | /* setup coroutine call */ |
710 | setup_coro (next); |
732 | setup_coro (next); |
711 | /* need a new stack */ |
733 | /* need a new stack */ |
712 | assert (!next->stack); |
734 | assert (!next->stack); |
713 | } |
735 | } |
|
|
736 | else |
|
|
737 | { |
|
|
738 | /* coroutine already started */ |
|
|
739 | SAVE (prev, flags); |
|
|
740 | LOAD (next); |
|
|
741 | } |
714 | |
742 | |
715 | prev__cctx = prev->cctx; |
743 | prev__cctx = prev->cctx; |
716 | |
744 | |
717 | /* possibly "free" the cctx */ |
745 | /* possibly "free" the cctx */ |
718 | if (prev__cctx->idle_sp == STACKLEVEL) |
746 | if (prev__cctx->idle_sp == STACKLEVEL) |
719 | { |
747 | { |
720 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
748 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
721 | assert (PL_top_env == prev__cctx->top_env); |
749 | assert (PL_top_env == prev__cctx->top_env); |
722 | |
750 | |
|
|
751 | prev->cctx = 0; |
|
|
752 | |
723 | cctx_put (prev__cctx); |
753 | cctx_put (prev__cctx); |
724 | prev->cctx = 0; |
754 | prev__cctx->inuse = 0; |
725 | } |
755 | } |
726 | |
756 | |
727 | if (!next->cctx) |
757 | if (!next->cctx) |
|
|
758 | { |
728 | next->cctx = cctx_get (); |
759 | next->cctx = cctx_get (); |
|
|
760 | assert (!next->cctx->inuse); |
|
|
761 | next->cctx->inuse = 1; |
|
|
762 | } |
729 | |
763 | |
730 | if (prev__cctx != next->cctx) |
764 | if (prev__cctx != next->cctx) |
731 | { |
765 | { |
732 | assert ( prev__cctx->inuse); |
|
|
733 | assert (!next->cctx->inuse); |
|
|
734 | |
|
|
735 | prev__cctx->inuse = 0; |
|
|
736 | next->cctx->inuse = 1; |
|
|
737 | |
|
|
738 | prev__cctx->top_env = PL_top_env; |
766 | prev__cctx->top_env = PL_top_env; |
739 | PL_top_env = next->cctx->top_env; |
767 | PL_top_env = next->cctx->top_env; |
740 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
768 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
741 | } |
769 | } |
742 | |
770 | |
… | |
… | |
758 | coro_state_destroy (struct coro *coro) |
786 | coro_state_destroy (struct coro *coro) |
759 | { |
787 | { |
760 | if (coro->refcnt--) |
788 | if (coro->refcnt--) |
761 | return; |
789 | return; |
762 | |
790 | |
763 | if (coro->flags & CF_RUNNING) |
|
|
764 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
765 | |
|
|
766 | if (coro->mainstack && coro->mainstack != main_mainstack) |
791 | if (coro->mainstack && coro->mainstack != main_mainstack) |
767 | { |
792 | { |
768 | struct coro temp; |
793 | struct coro temp; |
769 | |
794 | |
|
|
795 | if (coro->flags & CF_RUNNING) |
|
|
796 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
797 | |
770 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
798 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
771 | LOAD (coro); |
799 | LOAD (coro); |
772 | |
800 | |
773 | coro_destroy_stacks (); |
801 | coro_destroy_stacks (); |
774 | |
802 | |
775 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
803 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
776 | |
804 | |
777 | coro->mainstack = 0; |
805 | coro->mainstack = 0; |
778 | } |
806 | } |
779 | |
807 | |
780 | cctx_free (coro->cctx); |
808 | cctx_destroy (coro->cctx); |
781 | SvREFCNT_dec (coro->args); |
809 | SvREFCNT_dec (coro->args); |
782 | Safefree (coro); |
810 | Safefree (coro); |
783 | } |
811 | } |
784 | |
812 | |
785 | static int |
813 | static int |
… | |
… | |
845 | } |
873 | } |
846 | |
874 | |
847 | static void |
875 | static void |
848 | api_transfer (SV *prev, SV *next, int flags) |
876 | api_transfer (SV *prev, SV *next, int flags) |
849 | { |
877 | { |
850 | dTHX; |
|
|
851 | struct transfer_args ta; |
878 | struct transfer_args ta; |
852 | |
879 | |
853 | prepare_transfer (&ta, prev, next, flags); |
880 | prepare_transfer (&ta, prev, next, flags); |
854 | TRANSFER (ta); |
881 | TRANSFER (ta); |
855 | } |
882 | } |
… | |
… | |
862 | #define PRIO_LOW -1 |
889 | #define PRIO_LOW -1 |
863 | #define PRIO_IDLE -3 |
890 | #define PRIO_IDLE -3 |
864 | #define PRIO_MIN -4 |
891 | #define PRIO_MIN -4 |
865 | |
892 | |
866 | /* for Coro.pm */ |
893 | /* for Coro.pm */ |
867 | static GV *coro_current, *coro_idle; |
894 | static SV *coro_current; |
868 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
895 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
869 | static int coro_nready; |
896 | static int coro_nready; |
870 | |
897 | |
871 | static void |
898 | static void |
872 | coro_enq (SV *coro_sv) |
899 | coro_enq (SV *coro_sv) |
… | |
… | |
905 | coro = SvSTATE (coro_sv); |
932 | coro = SvSTATE (coro_sv); |
906 | |
933 | |
907 | if (coro->flags & CF_READY) |
934 | if (coro->flags & CF_READY) |
908 | return 0; |
935 | return 0; |
909 | |
936 | |
|
|
937 | #if 0 /* this is actually harmless */ |
910 | if (coro->flags & CF_RUNNING) |
938 | if (coro->flags & CF_RUNNING) |
911 | croak ("Coro::ready called on currently running coroutine"); |
939 | croak ("Coro::ready called on currently running coroutine"); |
|
|
940 | #endif |
912 | |
941 | |
913 | coro->flags |= CF_READY; |
942 | coro->flags |= CF_READY; |
914 | |
943 | |
915 | LOCK; |
944 | LOCK; |
916 | coro_enq (SvREFCNT_inc (coro_sv)); |
945 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
926 | } |
955 | } |
927 | |
956 | |
928 | static void |
957 | static void |
929 | prepare_schedule (struct transfer_args *ta) |
958 | prepare_schedule (struct transfer_args *ta) |
930 | { |
959 | { |
931 | SV *current, *prev, *next; |
960 | SV *prev, *next; |
932 | |
|
|
933 | current = GvSV (coro_current); |
|
|
934 | |
961 | |
935 | for (;;) |
962 | for (;;) |
936 | { |
963 | { |
937 | LOCK; |
964 | LOCK; |
938 | next = coro_deq (PRIO_MIN); |
965 | next = coro_deq (PRIO_MIN); |
… | |
… | |
947 | ENTER; |
974 | ENTER; |
948 | SAVETMPS; |
975 | SAVETMPS; |
949 | |
976 | |
950 | PUSHMARK (SP); |
977 | PUSHMARK (SP); |
951 | PUTBACK; |
978 | PUTBACK; |
952 | call_sv (GvSV (coro_idle), G_DISCARD); |
979 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
953 | |
980 | |
954 | FREETMPS; |
981 | FREETMPS; |
955 | LEAVE; |
982 | LEAVE; |
956 | } |
983 | } |
957 | } |
984 | } |
958 | |
985 | |
959 | prev = SvRV (current); |
986 | prev = SvRV (coro_current); |
960 | SvRV (current) = next; |
987 | SvRV_set (coro_current, next); |
961 | |
988 | |
962 | /* free this only after the transfer */ |
989 | /* free this only after the transfer */ |
963 | LOCK; |
990 | LOCK; |
964 | free_coro_mortal (); |
991 | free_coro_mortal (); |
965 | UNLOCK; |
992 | UNLOCK; |
966 | coro_mortal = prev; |
993 | coro_mortal = prev; |
967 | |
994 | |
|
|
995 | assert (!SvROK(prev));//D |
|
|
996 | assert (!SvROK(next));//D |
|
|
997 | |
968 | ta->prev = SvSTATE (prev); |
998 | ta->prev = SvSTATE (prev); |
969 | ta->next = SvSTATE (next); |
999 | ta->next = SvSTATE (next); |
970 | ta->flags = TRANSFER_SAVE_ALL; |
1000 | ta->flags = TRANSFER_SAVE_ALL; |
971 | |
1001 | |
|
|
1002 | assert (ta->flags & CF_READY); |
972 | ta->next->flags &= ~CF_READY; |
1003 | ta->next->flags &= ~CF_READY; |
973 | } |
1004 | } |
974 | |
1005 | |
975 | static void |
1006 | static void |
976 | prepare_cede (struct transfer_args *ta) |
1007 | prepare_cede (struct transfer_args *ta) |
977 | { |
1008 | { |
978 | api_ready (GvSV (coro_current)); |
1009 | api_ready (coro_current); |
979 | |
1010 | |
980 | prepare_schedule (ta); |
1011 | prepare_schedule (ta); |
981 | } |
1012 | } |
982 | |
1013 | |
983 | static void |
1014 | static void |
984 | api_schedule (void) |
1015 | api_schedule (void) |
985 | { |
1016 | { |
986 | dTHX; |
|
|
987 | struct transfer_args ta; |
1017 | struct transfer_args ta; |
988 | |
1018 | |
989 | prepare_schedule (&ta); |
1019 | prepare_schedule (&ta); |
990 | TRANSFER (ta); |
1020 | TRANSFER (ta); |
991 | } |
1021 | } |
992 | |
1022 | |
993 | static void |
1023 | static void |
994 | api_cede (void) |
1024 | api_cede (void) |
995 | { |
1025 | { |
996 | dTHX; |
|
|
997 | struct transfer_args ta; |
1026 | struct transfer_args ta; |
998 | |
1027 | |
999 | prepare_cede (&ta); |
1028 | prepare_cede (&ta); |
1000 | TRANSFER (ta); |
1029 | TRANSFER (ta); |
1001 | } |
1030 | } |
… | |
… | |
1033 | HV *hv; |
1062 | HV *hv; |
1034 | int i; |
1063 | int i; |
1035 | |
1064 | |
1036 | Newz (0, coro, 1, struct coro); |
1065 | Newz (0, coro, 1, struct coro); |
1037 | coro->args = newAV (); |
1066 | coro->args = newAV (); |
|
|
1067 | coro->flags = CF_NEW; |
1038 | |
1068 | |
1039 | hv = newHV (); |
1069 | hv = newHV (); |
1040 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1070 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1041 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1071 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1042 | |
1072 | |
… | |
… | |
1050 | _set_stacklevel (...) |
1080 | _set_stacklevel (...) |
1051 | ALIAS: |
1081 | ALIAS: |
1052 | Coro::State::transfer = 1 |
1082 | Coro::State::transfer = 1 |
1053 | Coro::schedule = 2 |
1083 | Coro::schedule = 2 |
1054 | Coro::cede = 3 |
1084 | Coro::cede = 3 |
1055 | Coro::Cont::yield = 4 |
|
|
1056 | CODE: |
1085 | CODE: |
1057 | { |
1086 | { |
1058 | struct transfer_args ta; |
1087 | struct transfer_args ta; |
1059 | |
1088 | |
1060 | switch (ix) |
1089 | switch (ix) |
… | |
… | |
1065 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1094 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1066 | break; |
1095 | break; |
1067 | |
1096 | |
1068 | case 1: |
1097 | case 1: |
1069 | if (items != 3) |
1098 | if (items != 3) |
1070 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1099 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1071 | |
1100 | |
1072 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1101 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1073 | break; |
1102 | break; |
1074 | |
1103 | |
1075 | case 2: |
1104 | case 2: |
… | |
… | |
1077 | break; |
1106 | break; |
1078 | |
1107 | |
1079 | case 3: |
1108 | case 3: |
1080 | prepare_cede (&ta); |
1109 | prepare_cede (&ta); |
1081 | break; |
1110 | break; |
1082 | |
|
|
1083 | case 4: |
|
|
1084 | { |
|
|
1085 | SV *yieldstack; |
|
|
1086 | SV *sv; |
|
|
1087 | AV *defav = GvAV (PL_defgv); |
|
|
1088 | |
|
|
1089 | yieldstack = *hv_fetch ( |
|
|
1090 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1091 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1092 | 0 |
|
|
1093 | ); |
|
|
1094 | |
|
|
1095 | /* set up @_ -- ugly */ |
|
|
1096 | av_clear (defav); |
|
|
1097 | av_fill (defav, items - 1); |
|
|
1098 | while (items--) |
|
|
1099 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1100 | |
|
|
1101 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1102 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1103 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1104 | ta.flags = 0; |
|
|
1105 | SvREFCNT_dec (sv); |
|
|
1106 | } |
|
|
1107 | break; |
|
|
1108 | |
|
|
1109 | } |
1111 | } |
1110 | |
1112 | |
1111 | TRANSFER (ta); |
1113 | TRANSFER (ta); |
1112 | } |
1114 | } |
1113 | |
1115 | |
… | |
… | |
1157 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1159 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1158 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1160 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1159 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1161 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1160 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1162 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1161 | |
1163 | |
1162 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1164 | coro_current = get_sv ("Coro::current", FALSE); |
1163 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1165 | SvREADONLY_on (coro_current); |
1164 | |
1166 | |
1165 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1167 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1166 | coro_ready[i] = newAV (); |
1168 | coro_ready[i] = newAV (); |
1167 | |
1169 | |
1168 | { |
1170 | { |
… | |
… | |
1223 | CODE: |
1225 | CODE: |
1224 | RETVAL = coro_nready; |
1226 | RETVAL = coro_nready; |
1225 | OUTPUT: |
1227 | OUTPUT: |
1226 | RETVAL |
1228 | RETVAL |
1227 | |
1229 | |
|
|
1230 | void |
|
|
1231 | _set_current (SV *current) |
|
|
1232 | PROTOTYPE: $ |
|
|
1233 | CODE: |
|
|
1234 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1235 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1236 | |
1228 | MODULE = Coro::State PACKAGE = Coro::AIO |
1237 | MODULE = Coro::State PACKAGE = Coro::AIO |
1229 | |
1238 | |
1230 | SV * |
1239 | SV * |
1231 | _get_state () |
1240 | _get_state () |
1232 | CODE: |
1241 | CODE: |
1233 | { |
1242 | { |
1234 | struct { |
1243 | RETVAL = newSV (sizeof (struct io_state)); |
1235 | int errorno; |
1244 | struct io_state *data = (struct io_state *)SvPVX (RETVAL); |
1236 | int laststype; |
1245 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
1237 | int laststatval; |
1246 | SvPOK_only (RETVAL); |
1238 | Stat_t statcache; |
|
|
1239 | } data; |
|
|
1240 | |
1247 | |
1241 | data.errorno = errno; |
1248 | data->errorno = errno; |
1242 | data.laststype = PL_laststype; |
1249 | data->laststype = PL_laststype; |
1243 | data.laststatval = PL_laststatval; |
1250 | data->laststatval = PL_laststatval; |
1244 | data.statcache = PL_statcache; |
1251 | data->statcache = PL_statcache; |
1245 | |
|
|
1246 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1247 | } |
1252 | } |
1248 | OUTPUT: |
1253 | OUTPUT: |
1249 | RETVAL |
1254 | RETVAL |
1250 | |
1255 | |
1251 | void |
1256 | void |
1252 | _set_state (char *data_) |
1257 | _set_state (char *data_) |
1253 | PROTOTYPE: $ |
1258 | PROTOTYPE: $ |
1254 | CODE: |
1259 | CODE: |
1255 | { |
1260 | { |
1256 | struct { |
1261 | struct io_state *data = (void *)data_; |
1257 | int errorno; |
|
|
1258 | int laststype; |
|
|
1259 | int laststatval; |
|
|
1260 | Stat_t statcache; |
|
|
1261 | } *data = (void *)data_; |
|
|
1262 | |
1262 | |
1263 | errno = data->errorno; |
1263 | errno = data->errorno; |
1264 | PL_laststype = data->laststype; |
1264 | PL_laststype = data->laststype; |
1265 | PL_laststatval = data->laststatval; |
1265 | PL_laststatval = data->laststatval; |
1266 | PL_statcache = data->statcache; |
1266 | PL_statcache = data->statcache; |
1267 | } |
1267 | } |
|
|
1268 | |