|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
5 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
6 | |
10 | |
7 | #if PATCHLEVEL < 6 |
11 | // threaded perls might need much more, 65536 or more, |
|
|
12 | // which is 0.5 to 1MB |
|
|
13 | #define STACKSIZE 16384 |
|
|
14 | |
|
|
15 | #if PERL_VERSION < 6 |
8 | # ifndef PL_ppaddr |
16 | # ifndef PL_ppaddr |
9 | # define PL_ppaddr ppaddr |
17 | # define PL_ppaddr ppaddr |
10 | # endif |
18 | # endif |
11 | # ifndef call_sv |
19 | # ifndef call_sv |
12 | # define call_sv perl_call_sv |
20 | # define call_sv perl_call_sv |
… | |
… | |
23 | # ifndef IS_PADCONST |
31 | # ifndef IS_PADCONST |
24 | # define IS_PADCONST(v) 0 |
32 | # define IS_PADCONST(v) 0 |
25 | # endif |
33 | # endif |
26 | #endif |
34 | #endif |
27 | |
35 | |
28 | #include "libcoro/coro.c" |
|
|
29 | |
|
|
30 | #include <signal.h> |
36 | #include <signal.h> |
31 | |
37 | |
32 | #ifdef HAVE_MMAP |
38 | #ifdef HAVE_MMAP |
33 | # include <unistd.h> |
39 | # include <unistd.h> |
34 | # include <sys/mman.h> |
40 | # include <sys/mman.h> |
… | |
… | |
63 | # define LOCK 0 |
69 | # define LOCK 0 |
64 | # define UNLOCK 0 |
70 | # define UNLOCK 0 |
65 | #endif |
71 | #endif |
66 | |
72 | |
67 | static struct CoroAPI coroapi; |
73 | static struct CoroAPI coroapi; |
|
|
74 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
75 | static HV *coro_state_stash; |
|
|
76 | static SV *ucoro_state_sv; |
|
|
77 | static U32 ucoro_state_hash; |
|
|
78 | static SV *coro_mortal; /* will be freed after next transfer */ |
68 | |
79 | |
69 | /* this is actually not only the c stack but also c registers etc... */ |
80 | /* this is actually not only the c stack but also c registers etc... */ |
70 | typedef struct { |
81 | typedef struct { |
71 | int refcnt; /* pointer reference counter */ |
82 | int refcnt; /* pointer reference counter */ |
72 | int usecnt; /* shared by how many coroutines */ |
83 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
102 | AV *mainstack; |
113 | AV *mainstack; |
103 | SV **stack_sp; |
114 | SV **stack_sp; |
104 | OP *op; |
115 | OP *op; |
105 | SV **curpad; |
116 | SV **curpad; |
106 | AV *comppad; |
117 | AV *comppad; |
|
|
118 | CV *compcv; |
107 | SV **stack_base; |
119 | SV **stack_base; |
108 | SV **stack_max; |
120 | SV **stack_max; |
109 | SV **tmps_stack; |
121 | SV **tmps_stack; |
110 | I32 tmps_floor; |
122 | I32 tmps_floor; |
111 | I32 tmps_ix; |
123 | I32 tmps_ix; |
… | |
… | |
120 | I32 savestack_ix; |
132 | I32 savestack_ix; |
121 | I32 savestack_max; |
133 | I32 savestack_max; |
122 | OP **retstack; |
134 | OP **retstack; |
123 | I32 retstack_ix; |
135 | I32 retstack_ix; |
124 | I32 retstack_max; |
136 | I32 retstack_max; |
|
|
137 | PMOP *curpm; |
125 | COP *curcop; |
138 | COP *curcop; |
126 | JMPENV *top_env; |
139 | JMPENV *top_env; |
127 | |
140 | |
128 | /* data associated with this coroutine (initial args) */ |
141 | /* data associated with this coroutine (initial args) */ |
129 | AV *args; |
142 | AV *args; |
130 | }; |
143 | }; |
131 | |
144 | |
132 | typedef struct coro *Coro__State; |
145 | typedef struct coro *Coro__State; |
133 | typedef struct coro *Coro__State_or_hashref; |
146 | typedef struct coro *Coro__State_or_hashref; |
134 | |
147 | |
135 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
136 | static HV *coro_state_stash; |
|
|
137 | static SV *ucoro_state_sv; |
|
|
138 | static U32 ucoro_state_hash; |
|
|
139 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
140 | |
|
|
141 | /* mostly copied from op.c:cv_clone2 */ |
148 | /* mostly copied from op.c:cv_clone2 */ |
142 | STATIC AV * |
149 | STATIC AV * |
143 | clone_padlist (AV *protopadlist) |
150 | clone_padlist (pTHX_ AV *protopadlist) |
144 | { |
151 | { |
145 | AV *av; |
152 | AV *av; |
146 | I32 ix; |
153 | I32 ix; |
147 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
154 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
148 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
155 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
… | |
… | |
237 | |
244 | |
238 | return newpadlist; |
245 | return newpadlist; |
239 | } |
246 | } |
240 | |
247 | |
241 | STATIC void |
248 | STATIC void |
242 | free_padlist (AV *padlist) |
249 | free_padlist (pTHX_ AV *padlist) |
243 | { |
250 | { |
244 | /* may be during global destruction */ |
251 | /* may be during global destruction */ |
245 | if (SvREFCNT (padlist)) |
252 | if (SvREFCNT (padlist)) |
246 | { |
253 | { |
247 | I32 i = AvFILLp (padlist); |
254 | I32 i = AvFILLp (padlist); |
… | |
… | |
268 | AV *padlist; |
275 | AV *padlist; |
269 | AV *av = (AV *)mg->mg_obj; |
276 | AV *av = (AV *)mg->mg_obj; |
270 | |
277 | |
271 | /* casting is fun. */ |
278 | /* casting is fun. */ |
272 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
279 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
273 | free_padlist (padlist); |
280 | free_padlist (aTHX_ padlist); |
|
|
281 | |
|
|
282 | SvREFCNT_dec (av); |
274 | } |
283 | } |
275 | |
284 | |
276 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
285 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
277 | |
286 | |
278 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
287 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
279 | |
288 | |
280 | /* the next two functions merely cache the padlists */ |
289 | /* the next two functions merely cache the padlists */ |
281 | STATIC void |
290 | STATIC void |
282 | get_padlist (CV *cv) |
291 | get_padlist (pTHX_ CV *cv) |
283 | { |
292 | { |
284 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
293 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
285 | |
294 | |
286 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
295 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
287 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
296 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
288 | else |
297 | else |
289 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
298 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
290 | } |
299 | } |
291 | |
300 | |
292 | STATIC void |
301 | STATIC void |
293 | put_padlist (CV *cv) |
302 | put_padlist (pTHX_ CV *cv) |
294 | { |
303 | { |
295 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
304 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
296 | |
305 | |
297 | if (!mg) |
306 | if (!mg) |
298 | { |
307 | { |
… | |
… | |
324 | PL_mainstack = c->mainstack; |
333 | PL_mainstack = c->mainstack; |
325 | PL_stack_sp = c->stack_sp; |
334 | PL_stack_sp = c->stack_sp; |
326 | PL_op = c->op; |
335 | PL_op = c->op; |
327 | PL_curpad = c->curpad; |
336 | PL_curpad = c->curpad; |
328 | PL_comppad = c->comppad; |
337 | PL_comppad = c->comppad; |
|
|
338 | PL_compcv = c->compcv; |
329 | PL_stack_base = c->stack_base; |
339 | PL_stack_base = c->stack_base; |
330 | PL_stack_max = c->stack_max; |
340 | PL_stack_max = c->stack_max; |
331 | PL_tmps_stack = c->tmps_stack; |
341 | PL_tmps_stack = c->tmps_stack; |
332 | PL_tmps_floor = c->tmps_floor; |
342 | PL_tmps_floor = c->tmps_floor; |
333 | PL_tmps_ix = c->tmps_ix; |
343 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
342 | PL_savestack_ix = c->savestack_ix; |
352 | PL_savestack_ix = c->savestack_ix; |
343 | PL_savestack_max = c->savestack_max; |
353 | PL_savestack_max = c->savestack_max; |
344 | PL_retstack = c->retstack; |
354 | PL_retstack = c->retstack; |
345 | PL_retstack_ix = c->retstack_ix; |
355 | PL_retstack_ix = c->retstack_ix; |
346 | PL_retstack_max = c->retstack_max; |
356 | PL_retstack_max = c->retstack_max; |
|
|
357 | PL_curpm = c->curpm; |
347 | PL_curcop = c->curcop; |
358 | PL_curcop = c->curcop; |
348 | PL_top_env = c->top_env; |
359 | PL_top_env = c->top_env; |
349 | |
360 | |
350 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
361 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
351 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
362 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
360 | { |
371 | { |
361 | AV *padlist = (AV *)POPs; |
372 | AV *padlist = (AV *)POPs; |
362 | |
373 | |
363 | if (padlist) |
374 | if (padlist) |
364 | { |
375 | { |
365 | put_padlist (cv); /* mark this padlist as available */ |
376 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
366 | CvPADLIST(cv) = padlist; |
377 | CvPADLIST(cv) = padlist; |
367 | } |
378 | } |
368 | |
379 | |
369 | ++CvDEPTH(cv); |
380 | ++CvDEPTH(cv); |
370 | } |
381 | } |
… | |
… | |
410 | } |
421 | } |
411 | |
422 | |
412 | PUSHs ((SV *)CvPADLIST(cv)); |
423 | PUSHs ((SV *)CvPADLIST(cv)); |
413 | PUSHs ((SV *)cv); |
424 | PUSHs ((SV *)cv); |
414 | |
425 | |
415 | get_padlist (cv); /* this is a monster */ |
426 | get_padlist (aTHX_ cv); /* this is a monster */ |
416 | } |
427 | } |
417 | } |
428 | } |
418 | #ifdef CXt_FORMAT |
429 | #ifdef CXt_FORMAT |
419 | else if (CxTYPE(cx) == CXt_FORMAT) |
430 | else if (CxTYPE(cx) == CXt_FORMAT) |
420 | { |
431 | { |
… | |
… | |
448 | c->mainstack = PL_mainstack; |
459 | c->mainstack = PL_mainstack; |
449 | c->stack_sp = PL_stack_sp; |
460 | c->stack_sp = PL_stack_sp; |
450 | c->op = PL_op; |
461 | c->op = PL_op; |
451 | c->curpad = PL_curpad; |
462 | c->curpad = PL_curpad; |
452 | c->comppad = PL_comppad; |
463 | c->comppad = PL_comppad; |
|
|
464 | c->compcv = PL_compcv; |
453 | c->stack_base = PL_stack_base; |
465 | c->stack_base = PL_stack_base; |
454 | c->stack_max = PL_stack_max; |
466 | c->stack_max = PL_stack_max; |
455 | c->tmps_stack = PL_tmps_stack; |
467 | c->tmps_stack = PL_tmps_stack; |
456 | c->tmps_floor = PL_tmps_floor; |
468 | c->tmps_floor = PL_tmps_floor; |
457 | c->tmps_ix = PL_tmps_ix; |
469 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
466 | c->savestack_ix = PL_savestack_ix; |
478 | c->savestack_ix = PL_savestack_ix; |
467 | c->savestack_max = PL_savestack_max; |
479 | c->savestack_max = PL_savestack_max; |
468 | c->retstack = PL_retstack; |
480 | c->retstack = PL_retstack; |
469 | c->retstack_ix = PL_retstack_ix; |
481 | c->retstack_ix = PL_retstack_ix; |
470 | c->retstack_max = PL_retstack_max; |
482 | c->retstack_max = PL_retstack_max; |
|
|
483 | c->curpm = PL_curpm; |
471 | c->curcop = PL_curcop; |
484 | c->curcop = PL_curcop; |
472 | c->top_env = PL_top_env; |
485 | c->top_env = PL_top_env; |
473 | } |
486 | } |
474 | |
487 | |
475 | /* |
488 | /* |
… | |
… | |
531 | /* is this ugly, I ask? */ |
544 | /* is this ugly, I ask? */ |
532 | LEAVE_SCOPE (0); |
545 | LEAVE_SCOPE (0); |
533 | |
546 | |
534 | /* sure it is, but more important: is it correct?? :/ */ |
547 | /* sure it is, but more important: is it correct?? :/ */ |
535 | FREETMPS; |
548 | FREETMPS; |
|
|
549 | |
|
|
550 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
536 | } |
551 | } |
537 | |
552 | |
538 | while (PL_curstackinfo->si_next) |
553 | while (PL_curstackinfo->si_next) |
539 | PL_curstackinfo = PL_curstackinfo->si_next; |
554 | PL_curstackinfo = PL_curstackinfo->si_next; |
540 | |
555 | |
541 | while (PL_curstackinfo) |
556 | while (PL_curstackinfo) |
542 | { |
557 | { |
543 | PERL_SI *p = PL_curstackinfo->si_prev; |
558 | PERL_SI *p = PL_curstackinfo->si_prev; |
544 | |
559 | |
545 | { |
560 | { /*D*//*remove*/ |
546 | dSP; |
561 | dSP; |
547 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
562 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
548 | PUTBACK; /* possibly superfluous */ |
563 | PUTBACK; /* possibly superfluous */ |
549 | } |
564 | } |
550 | |
565 | |
551 | if (!IN_DESTRUCT) |
566 | if (!IN_DESTRUCT) |
552 | { |
567 | { |
553 | dounwind(-1); |
568 | dounwind (-1);/*D*//*remove*/ |
554 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
569 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
555 | } |
570 | } |
556 | |
571 | |
557 | Safefree(PL_curstackinfo->si_cxstack); |
572 | Safefree (PL_curstackinfo->si_cxstack); |
558 | Safefree(PL_curstackinfo); |
573 | Safefree (PL_curstackinfo); |
559 | PL_curstackinfo = p; |
574 | PL_curstackinfo = p; |
560 | } |
575 | } |
561 | |
576 | |
562 | Safefree(PL_tmps_stack); |
577 | Safefree (PL_tmps_stack); |
563 | Safefree(PL_markstack); |
578 | Safefree (PL_markstack); |
564 | Safefree(PL_scopestack); |
579 | Safefree (PL_scopestack); |
565 | Safefree(PL_savestack); |
580 | Safefree (PL_savestack); |
566 | Safefree(PL_retstack); |
581 | Safefree (PL_retstack); |
567 | } |
582 | } |
568 | |
583 | |
569 | static void |
584 | static void |
570 | allocate_stack (Coro__State ctx, int alloc) |
585 | allocate_stack (Coro__State ctx, int alloc) |
571 | { |
586 | { |
… | |
… | |
578 | stack->gencnt = ctx->gencnt = 0; |
593 | stack->gencnt = ctx->gencnt = 0; |
579 | |
594 | |
580 | if (alloc) |
595 | if (alloc) |
581 | { |
596 | { |
582 | #if HAVE_MMAP |
597 | #if HAVE_MMAP |
583 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
598 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
584 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
599 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
585 | if (stack->sptr == (void *)-1) |
600 | if (stack->sptr == (void *)-1) |
586 | #endif |
601 | #endif |
587 | { |
602 | { |
588 | /*FIXME*//*D*//* reasonable stack size! */ |
603 | /*FIXME*//*D*//* reasonable stack size! */ |
589 | stack->ssize = - (8192 * sizeof (long)); |
604 | stack->ssize = - (STACKSIZE * sizeof (long)); |
590 | New (0, stack->sptr, 8192, long); |
605 | New (0, stack->sptr, STACKSIZE, long); |
591 | } |
606 | } |
592 | } |
607 | } |
593 | else |
608 | else |
594 | stack->sptr = 0; |
609 | stack->sptr = 0; |
595 | |
610 | |
… | |
… | |
625 | setup_coro (void *arg) |
640 | setup_coro (void *arg) |
626 | { |
641 | { |
627 | /* |
642 | /* |
628 | * emulate part of the perl startup here. |
643 | * emulate part of the perl startup here. |
629 | */ |
644 | */ |
|
|
645 | dTHX; |
630 | dSP; |
646 | dSP; |
631 | Coro__State ctx = (Coro__State)arg; |
647 | Coro__State ctx = (Coro__State)arg; |
632 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
648 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
633 | |
649 | |
634 | coro_init_stacks (aTHX); |
650 | coro_init_stacks (aTHX); |
… | |
… | |
683 | continue_coro (void *arg) |
699 | continue_coro (void *arg) |
684 | { |
700 | { |
685 | /* |
701 | /* |
686 | * this is a _very_ stripped down perl interpreter ;) |
702 | * this is a _very_ stripped down perl interpreter ;) |
687 | */ |
703 | */ |
|
|
704 | dTHX; |
688 | Coro__State ctx = (Coro__State)arg; |
705 | Coro__State ctx = (Coro__State)arg; |
689 | JMPENV coro_start_env; |
706 | JMPENV coro_start_env; |
690 | |
707 | |
691 | PL_top_env = &ctx->start_env; |
708 | PL_top_env = &ctx->start_env; |
692 | |
709 | |
… | |
… | |
825 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
842 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
826 | { |
843 | { |
827 | SV_CORO (prev, "Coro::transfer"); |
844 | SV_CORO (prev, "Coro::transfer"); |
828 | SV_CORO (next, "Coro::transfer"); |
845 | SV_CORO (next, "Coro::transfer"); |
829 | |
846 | |
830 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
847 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
831 | } |
848 | } |
832 | |
849 | |
833 | /** Coro ********************************************************************/ |
850 | /** Coro ********************************************************************/ |
834 | |
851 | |
835 | #define PRIO_MAX 3 |
852 | #define PRIO_MAX 3 |
… | |
… | |
843 | static GV *coro_current, *coro_idle; |
860 | static GV *coro_current, *coro_idle; |
844 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
861 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
845 | static int coro_nready; |
862 | static int coro_nready; |
846 | |
863 | |
847 | static void |
864 | static void |
848 | coro_enq (SV *sv) |
865 | coro_enq (pTHX_ SV *sv) |
849 | { |
866 | { |
850 | if (SvTYPE (sv) == SVt_PVHV) |
867 | if (SvTYPE (sv) == SVt_PVHV) |
851 | { |
868 | { |
852 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
869 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
853 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
870 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
… | |
… | |
864 | |
881 | |
865 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
882 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
866 | } |
883 | } |
867 | |
884 | |
868 | static SV * |
885 | static SV * |
869 | coro_deq (int min_prio) |
886 | coro_deq (pTHX_ int min_prio) |
870 | { |
887 | { |
871 | int prio = PRIO_MAX - PRIO_MIN; |
888 | int prio = PRIO_MAX - PRIO_MIN; |
872 | |
889 | |
873 | min_prio -= PRIO_MIN; |
890 | min_prio -= PRIO_MIN; |
874 | if (min_prio < 0) |
891 | if (min_prio < 0) |
… | |
… | |
885 | } |
902 | } |
886 | |
903 | |
887 | static void |
904 | static void |
888 | api_ready (SV *coro) |
905 | api_ready (SV *coro) |
889 | { |
906 | { |
|
|
907 | dTHX; |
|
|
908 | |
890 | if (SvROK (coro)) |
909 | if (SvROK (coro)) |
891 | coro = SvRV (coro); |
910 | coro = SvRV (coro); |
892 | |
911 | |
893 | LOCK; |
912 | LOCK; |
894 | coro_enq (SvREFCNT_inc (coro)); |
913 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
895 | UNLOCK; |
914 | UNLOCK; |
896 | } |
915 | } |
897 | |
916 | |
898 | static void |
917 | static void |
899 | api_schedule (void) |
918 | api_schedule (void) |
900 | { |
919 | { |
|
|
920 | dTHX; |
|
|
921 | |
901 | SV *prev, *next; |
922 | SV *prev, *next; |
902 | |
923 | |
903 | LOCK; |
924 | LOCK; |
904 | |
925 | |
905 | prev = SvRV (GvSV (coro_current)); |
926 | prev = SvRV (GvSV (coro_current)); |
906 | next = coro_deq (PRIO_MIN); |
927 | next = coro_deq (aTHX_ PRIO_MIN); |
907 | |
928 | |
908 | if (!next) |
929 | if (!next) |
909 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
930 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
910 | |
931 | |
911 | /* free this only after the transfer */ |
932 | /* free this only after the transfer */ |
… | |
… | |
923 | } |
944 | } |
924 | |
945 | |
925 | static void |
946 | static void |
926 | api_cede (void) |
947 | api_cede (void) |
927 | { |
948 | { |
|
|
949 | dTHX; |
|
|
950 | |
928 | LOCK; |
951 | LOCK; |
929 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
952 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
930 | UNLOCK; |
953 | UNLOCK; |
931 | |
954 | |
932 | api_schedule (); |
955 | api_schedule (); |
933 | } |
956 | } |
934 | |
957 | |
… | |
… | |
968 | croak ("Coro::State::_newprocess expects an arrayref"); |
991 | croak ("Coro::State::_newprocess expects an arrayref"); |
969 | |
992 | |
970 | Newz (0, coro, 1, struct coro); |
993 | Newz (0, coro, 1, struct coro); |
971 | |
994 | |
972 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
995 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
973 | coro->mainstack = 0; /* actual work is done inside transfer */ |
996 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
974 | coro->stack = 0; |
997 | /*coro->stack = 0;*/ |
975 | |
998 | |
976 | /* same as JMPENV_BOOTSTRAP */ |
999 | /* same as JMPENV_BOOTSTRAP */ |
977 | /* we might be able to recycle start_env, but safe is safe */ |
1000 | /* we might be able to recycle start_env, but safe is safe */ |
978 | //Zero(&coro->start_env, 1, JMPENV); |
1001 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
979 | coro->start_env.je_ret = -1; |
1002 | coro->start_env.je_ret = -1; |
980 | coro->start_env.je_mustcatch = TRUE; |
1003 | coro->start_env.je_mustcatch = TRUE; |
981 | |
1004 | |
982 | RETVAL = coro; |
1005 | RETVAL = coro; |
983 | OUTPUT: |
1006 | OUTPUT: |
… | |
… | |
1004 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1027 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1005 | { |
1028 | { |
1006 | struct coro temp; |
1029 | struct coro temp; |
1007 | |
1030 | |
1008 | PUTBACK; |
1031 | PUTBACK; |
1009 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1032 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1010 | LOAD(aTHX_ coro); |
1033 | LOAD (aTHX_ coro); |
1011 | SPAGAIN; |
1034 | SPAGAIN; |
1012 | |
1035 | |
1013 | destroy_stacks (aTHX); |
1036 | destroy_stacks (aTHX); |
1014 | |
1037 | |
1015 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1038 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1016 | SPAGAIN; |
1039 | SPAGAIN; |
1017 | |
1040 | |
1018 | coro->mainstack = 0; |
1041 | coro->mainstack = 0; |
1019 | } |
1042 | } |
1020 | |
1043 | |
… | |
… | |
1035 | |
1058 | |
1036 | void |
1059 | void |
1037 | yield(...) |
1060 | yield(...) |
1038 | PROTOTYPE: @ |
1061 | PROTOTYPE: @ |
1039 | CODE: |
1062 | CODE: |
1040 | static SV *returnstk; |
1063 | SV *yieldstack; |
1041 | SV *sv; |
1064 | SV *sv; |
1042 | AV *defav = GvAV (PL_defgv); |
1065 | AV *defav = GvAV (PL_defgv); |
1043 | struct coro *prev, *next; |
1066 | struct coro *prev, *next; |
1044 | |
1067 | |
1045 | if (!returnstk) |
1068 | yieldstack = *hv_fetch ( |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1069 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1070 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1071 | 0 |
|
|
1072 | ); |
1047 | |
1073 | |
1048 | /* set up @_ -- ugly */ |
1074 | /* set up @_ -- ugly */ |
1049 | av_clear (defav); |
1075 | av_clear (defav); |
1050 | av_fill (defav, items - 1); |
1076 | av_fill (defav, items - 1); |
1051 | while (items--) |
1077 | while (items--) |
1052 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1078 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1053 | |
1079 | |
1054 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
1055 | sv = av_pop ((AV *)SvRV (returnstk)); |
1080 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1056 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1081 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1057 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1082 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1058 | SvREFCNT_dec (sv); |
1083 | SvREFCNT_dec (sv); |
1059 | |
1084 | |
1060 | transfer(aTHX_ prev, next, 0); |
1085 | transfer (aTHX_ prev, next, 0); |
1061 | |
1086 | |
1062 | MODULE = Coro::State PACKAGE = Coro |
1087 | MODULE = Coro::State PACKAGE = Coro |
1063 | |
1088 | |
1064 | # this is slightly dirty (should expose a c-level api) |
1089 | # this is slightly dirty (should expose a c-level api) |
1065 | |
1090 | |