|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
1 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
2 | #include "perl.h" |
4 | #include "perl.h" |
3 | #include "XSUB.h" |
5 | #include "XSUB.h" |
4 | |
6 | |
5 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
… | |
… | |
53 | |
55 | |
54 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
55 | |
57 | |
56 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
57 | |
59 | |
|
|
60 | #ifdef USE_ITHREADS |
|
|
61 | static perl_mutex coro_mutex; |
|
|
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
|
|
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
|
|
64 | #else |
|
|
65 | # define LOCK 0 |
|
|
66 | # define UNLOCK 0 |
|
|
67 | #endif |
|
|
68 | |
58 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
|
|
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
71 | static HV *coro_state_stash; |
|
|
72 | static SV *ucoro_state_sv; |
|
|
73 | static U32 ucoro_state_hash; |
|
|
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
59 | |
75 | |
60 | /* this is actually not only the c stack but also c registers etc... */ |
76 | /* this is actually not only the c stack but also c registers etc... */ |
61 | typedef struct { |
77 | typedef struct { |
62 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
63 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
121 | }; |
137 | }; |
122 | |
138 | |
123 | typedef struct coro *Coro__State; |
139 | typedef struct coro *Coro__State; |
124 | typedef struct coro *Coro__State_or_hashref; |
140 | typedef struct coro *Coro__State_or_hashref; |
125 | |
141 | |
126 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
127 | static HV *coro_state_stash; |
|
|
128 | static SV *ucoro_state_sv; |
|
|
129 | static U32 ucoro_state_hash; |
|
|
130 | static HV *padlist_cache; |
|
|
131 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
132 | |
|
|
133 | /* mostly copied from op.c:cv_clone2 */ |
142 | /* mostly copied from op.c:cv_clone2 */ |
134 | STATIC AV * |
143 | STATIC AV * |
135 | clone_padlist (AV *protopadlist) |
144 | clone_padlist (pTHX_ AV *protopadlist) |
136 | { |
145 | { |
137 | AV *av; |
146 | AV *av; |
138 | I32 ix; |
147 | I32 ix; |
139 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
148 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
140 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
149 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
… | |
… | |
164 | AvFLAGS (av) = AVf_REIFY; |
173 | AvFLAGS (av) = AVf_REIFY; |
165 | |
174 | |
166 | for (ix = fpad; ix > 0; ix--) |
175 | for (ix = fpad; ix > 0; ix--) |
167 | { |
176 | { |
168 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
177 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
178 | |
169 | if (namesv && namesv != &PL_sv_undef) |
179 | if (namesv && namesv != &PL_sv_undef) |
170 | { |
180 | { |
171 | char *name = SvPVX (namesv); /* XXX */ |
181 | char *name = SvPVX (namesv); /* XXX */ |
|
|
182 | |
172 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
183 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
173 | { /* lexical from outside? */ |
184 | { /* lexical from outside? */ |
174 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
185 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
175 | } |
186 | } |
176 | else |
187 | else |
… | |
… | |
182 | sv = (SV *) newAV (); |
193 | sv = (SV *) newAV (); |
183 | else if (*name == '%') |
194 | else if (*name == '%') |
184 | sv = (SV *) newHV (); |
195 | sv = (SV *) newHV (); |
185 | else |
196 | else |
186 | sv = NEWSV (0, 0); |
197 | sv = NEWSV (0, 0); |
|
|
198 | |
187 | #ifdef SvPADBUSY |
199 | #ifdef SvPADBUSY |
188 | if (!SvPADBUSY (sv)) |
200 | if (!SvPADBUSY (sv)) |
189 | #endif |
201 | #endif |
190 | SvPADMY_on (sv); |
202 | SvPADMY_on (sv); |
|
|
203 | |
191 | npad[ix] = sv; |
204 | npad[ix] = sv; |
192 | } |
205 | } |
193 | } |
206 | } |
194 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
207 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
195 | { |
208 | { |
… | |
… | |
225 | |
238 | |
226 | return newpadlist; |
239 | return newpadlist; |
227 | } |
240 | } |
228 | |
241 | |
229 | STATIC void |
242 | STATIC void |
230 | free_padlist (AV *padlist) |
243 | free_padlist (pTHX_ AV *padlist) |
231 | { |
244 | { |
232 | /* may be during global destruction */ |
245 | /* may be during global destruction */ |
233 | if (SvREFCNT (padlist)) |
246 | if (SvREFCNT (padlist)) |
234 | { |
247 | { |
235 | I32 i = AvFILLp (padlist); |
248 | I32 i = AvFILLp (padlist); |
… | |
… | |
249 | SvREFCNT_dec ((SV*)padlist); |
262 | SvREFCNT_dec ((SV*)padlist); |
250 | } |
263 | } |
251 | } |
264 | } |
252 | |
265 | |
253 | STATIC int |
266 | STATIC int |
254 | coro_cv_free (SV *sv, MAGIC *mg) |
267 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
255 | { |
268 | { |
256 | AV *padlist; |
269 | AV *padlist; |
257 | AV *av = (AV *)mg->mg_obj; |
270 | AV *av = (AV *)mg->mg_obj; |
258 | |
271 | |
259 | /* casting is fun. */ |
272 | /* casting is fun. */ |
260 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
273 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
261 | free_padlist (padlist); |
274 | free_padlist (aTHX_ padlist); |
262 | } |
275 | } |
263 | |
276 | |
264 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
277 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
265 | |
278 | |
266 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
279 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
267 | |
280 | |
268 | /* the next two functions merely cache the padlists */ |
281 | /* the next two functions merely cache the padlists */ |
269 | STATIC void |
282 | STATIC void |
270 | get_padlist (CV *cv) |
283 | get_padlist (pTHX_ CV *cv) |
271 | { |
284 | { |
272 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
285 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
273 | |
286 | |
274 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
287 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
275 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
288 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
276 | else |
289 | else |
277 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
290 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
278 | } |
291 | } |
279 | |
292 | |
280 | STATIC void |
293 | STATIC void |
281 | put_padlist (CV *cv) |
294 | put_padlist (pTHX_ CV *cv) |
282 | { |
295 | { |
283 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
296 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
284 | |
297 | |
285 | if (!mg) |
298 | if (!mg) |
286 | { |
299 | { |
… | |
… | |
348 | { |
361 | { |
349 | AV *padlist = (AV *)POPs; |
362 | AV *padlist = (AV *)POPs; |
350 | |
363 | |
351 | if (padlist) |
364 | if (padlist) |
352 | { |
365 | { |
353 | put_padlist (cv); /* mark this padlist as available */ |
366 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
354 | CvPADLIST(cv) = padlist; |
367 | CvPADLIST(cv) = padlist; |
355 | #ifdef USE_THREADS |
|
|
356 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
357 | #endif |
|
|
358 | } |
368 | } |
359 | |
369 | |
360 | ++CvDEPTH(cv); |
370 | ++CvDEPTH(cv); |
361 | } |
371 | } |
362 | |
372 | |
… | |
… | |
389 | if (CxTYPE(cx) == CXt_SUB) |
399 | if (CxTYPE(cx) == CXt_SUB) |
390 | { |
400 | { |
391 | CV *cv = cx->blk_sub.cv; |
401 | CV *cv = cx->blk_sub.cv; |
392 | if (CvDEPTH(cv)) |
402 | if (CvDEPTH(cv)) |
393 | { |
403 | { |
394 | #ifdef USE_THREADS |
|
|
395 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
396 | /*CvOWNER(cv) = 0;*/ |
|
|
397 | /*error must unlock this cv etc.. etc...*/ |
|
|
398 | #endif |
|
|
399 | EXTEND (SP, CvDEPTH(cv)*2); |
404 | EXTEND (SP, CvDEPTH(cv)*2); |
400 | |
405 | |
401 | while (--CvDEPTH(cv)) |
406 | while (--CvDEPTH(cv)) |
402 | { |
407 | { |
403 | /* this tells the restore code to increment CvDEPTH */ |
408 | /* this tells the restore code to increment CvDEPTH */ |
… | |
… | |
406 | } |
411 | } |
407 | |
412 | |
408 | PUSHs ((SV *)CvPADLIST(cv)); |
413 | PUSHs ((SV *)CvPADLIST(cv)); |
409 | PUSHs ((SV *)cv); |
414 | PUSHs ((SV *)cv); |
410 | |
415 | |
411 | get_padlist (cv); /* this is a monster */ |
416 | get_padlist (aTHX_ cv); /* this is a monster */ |
412 | } |
417 | } |
413 | } |
418 | } |
414 | #ifdef CXt_FORMAT |
419 | #ifdef CXt_FORMAT |
415 | else if (CxTYPE(cx) == CXt_FORMAT) |
420 | else if (CxTYPE(cx) == CXt_FORMAT) |
416 | { |
421 | { |
… | |
… | |
475 | * not usually need a lot of stackspace. |
480 | * not usually need a lot of stackspace. |
476 | */ |
481 | */ |
477 | STATIC void |
482 | STATIC void |
478 | coro_init_stacks (pTHX) |
483 | coro_init_stacks (pTHX) |
479 | { |
484 | { |
|
|
485 | LOCK; |
|
|
486 | |
480 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
487 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
481 | PL_curstackinfo->si_type = PERLSI_MAIN; |
488 | PL_curstackinfo->si_type = PERLSI_MAIN; |
482 | PL_curstack = PL_curstackinfo->si_stack; |
489 | PL_curstack = PL_curstackinfo->si_stack; |
483 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
490 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
484 | |
491 | |
… | |
… | |
508 | PL_savestack_max = 96; |
515 | PL_savestack_max = 96; |
509 | |
516 | |
510 | New(54,PL_retstack,8,OP*); |
517 | New(54,PL_retstack,8,OP*); |
511 | PL_retstack_ix = 0; |
518 | PL_retstack_ix = 0; |
512 | PL_retstack_max = 8; |
519 | PL_retstack_max = 8; |
|
|
520 | |
|
|
521 | UNLOCK; |
513 | } |
522 | } |
514 | |
523 | |
515 | /* |
524 | /* |
516 | * destroy the stacks, the callchain etc... |
525 | * destroy the stacks, the callchain etc... |
517 | */ |
526 | */ |
… | |
… | |
523 | /* is this ugly, I ask? */ |
532 | /* is this ugly, I ask? */ |
524 | LEAVE_SCOPE (0); |
533 | LEAVE_SCOPE (0); |
525 | |
534 | |
526 | /* sure it is, but more important: is it correct?? :/ */ |
535 | /* sure it is, but more important: is it correct?? :/ */ |
527 | FREETMPS; |
536 | FREETMPS; |
|
|
537 | |
|
|
538 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
528 | } |
539 | } |
529 | |
540 | |
530 | while (PL_curstackinfo->si_next) |
541 | while (PL_curstackinfo->si_next) |
531 | PL_curstackinfo = PL_curstackinfo->si_next; |
542 | PL_curstackinfo = PL_curstackinfo->si_next; |
532 | |
543 | |
533 | while (PL_curstackinfo) |
544 | while (PL_curstackinfo) |
534 | { |
545 | { |
535 | PERL_SI *p = PL_curstackinfo->si_prev; |
546 | PERL_SI *p = PL_curstackinfo->si_prev; |
536 | |
547 | |
537 | { |
548 | { /*D*//*remove*/ |
538 | dSP; |
549 | dSP; |
539 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
550 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
540 | PUTBACK; /* possibly superfluous */ |
551 | PUTBACK; /* possibly superfluous */ |
541 | } |
552 | } |
542 | |
553 | |
543 | if (!IN_DESTRUCT) |
554 | if (!IN_DESTRUCT) |
544 | { |
555 | { |
545 | dounwind(-1); |
556 | dounwind (-1);/*D*//*remove*/ |
546 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
557 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
547 | } |
558 | } |
548 | |
559 | |
549 | Safefree(PL_curstackinfo->si_cxstack); |
560 | Safefree (PL_curstackinfo->si_cxstack); |
550 | Safefree(PL_curstackinfo); |
561 | Safefree (PL_curstackinfo); |
551 | PL_curstackinfo = p; |
562 | PL_curstackinfo = p; |
552 | } |
563 | } |
553 | |
564 | |
554 | Safefree(PL_tmps_stack); |
565 | Safefree (PL_tmps_stack); |
555 | Safefree(PL_markstack); |
566 | Safefree (PL_markstack); |
556 | Safefree(PL_scopestack); |
567 | Safefree (PL_scopestack); |
557 | Safefree(PL_savestack); |
568 | Safefree (PL_savestack); |
558 | Safefree(PL_retstack); |
569 | Safefree (PL_retstack); |
559 | } |
570 | } |
560 | |
571 | |
561 | static void |
572 | static void |
562 | allocate_stack (Coro__State ctx, int alloc) |
573 | allocate_stack (Coro__State ctx, int alloc) |
563 | { |
574 | { |
… | |
… | |
566 | New (0, stack, 1, coro_stack); |
577 | New (0, stack, 1, coro_stack); |
567 | |
578 | |
568 | stack->refcnt = 1; |
579 | stack->refcnt = 1; |
569 | stack->usecnt = 1; |
580 | stack->usecnt = 1; |
570 | stack->gencnt = ctx->gencnt = 0; |
581 | stack->gencnt = ctx->gencnt = 0; |
|
|
582 | |
571 | if (alloc) |
583 | if (alloc) |
572 | { |
584 | { |
573 | #if HAVE_MMAP |
585 | #if HAVE_MMAP |
574 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
586 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
575 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
587 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
… | |
… | |
616 | setup_coro (void *arg) |
628 | setup_coro (void *arg) |
617 | { |
629 | { |
618 | /* |
630 | /* |
619 | * emulate part of the perl startup here. |
631 | * emulate part of the perl startup here. |
620 | */ |
632 | */ |
|
|
633 | dTHX; |
621 | dSP; |
634 | dSP; |
622 | Coro__State ctx = (Coro__State)arg; |
635 | Coro__State ctx = (Coro__State)arg; |
623 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
636 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
624 | |
637 | |
625 | coro_init_stacks (aTHX); |
638 | coro_init_stacks (aTHX); |
… | |
… | |
674 | continue_coro (void *arg) |
687 | continue_coro (void *arg) |
675 | { |
688 | { |
676 | /* |
689 | /* |
677 | * this is a _very_ stripped down perl interpreter ;) |
690 | * this is a _very_ stripped down perl interpreter ;) |
678 | */ |
691 | */ |
|
|
692 | dTHX; |
679 | Coro__State ctx = (Coro__State)arg; |
693 | Coro__State ctx = (Coro__State)arg; |
680 | JMPENV coro_start_env; |
694 | JMPENV coro_start_env; |
681 | |
695 | |
682 | PL_top_env = &ctx->start_env; |
696 | PL_top_env = &ctx->start_env; |
683 | |
697 | |
… | |
… | |
687 | |
701 | |
688 | abort (); |
702 | abort (); |
689 | } |
703 | } |
690 | |
704 | |
691 | STATIC void |
705 | STATIC void |
692 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
706 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
693 | { |
707 | { |
694 | dSTACKLEVEL; |
708 | dSTACKLEVEL; |
695 | static struct coro *xnext; |
|
|
696 | |
709 | |
697 | if (prev != next) |
710 | if (prev != next) |
698 | { |
711 | { |
699 | xnext = next; |
|
|
700 | |
|
|
701 | if (next->mainstack) |
712 | if (next->mainstack) |
702 | { |
713 | { |
|
|
714 | LOCK; |
703 | SAVE (prev, flags); |
715 | SAVE (prev, flags); |
704 | LOAD (next); |
716 | LOAD (next); |
|
|
717 | UNLOCK; |
705 | |
718 | |
706 | /* mark this state as in-use */ |
719 | /* mark this state as in-use */ |
707 | next->mainstack = 0; |
720 | next->mainstack = 0; |
708 | next->tmps_ix = -2; |
721 | next->tmps_ix = -2; |
709 | |
722 | |
… | |
… | |
728 | continue_coro, (void *)next, |
741 | continue_coro, (void *)next, |
729 | next->stack->sptr, labs (next->stack->ssize)); |
742 | next->stack->sptr, labs (next->stack->ssize)); |
730 | } |
743 | } |
731 | |
744 | |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
745 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
746 | prev->cursp = stacklevel; |
733 | /* don't add any code here */ |
747 | /* don't add any code here */ |
734 | } |
748 | } |
735 | |
749 | else |
|
|
750 | next->cursp = stacklevel; |
736 | } |
751 | } |
737 | else if (next->tmps_ix == -2) |
752 | else if (next->tmps_ix == -2) |
738 | croak ("tried to transfer to running coroutine"); |
753 | croak ("tried to transfer to running coroutine"); |
739 | else |
754 | else |
740 | { |
755 | { |
|
|
756 | LOCK; |
741 | SAVE (prev, -1); /* first get rid of the old state */ |
757 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
758 | UNLOCK; |
742 | |
759 | |
743 | if (flags & TRANSFER_SAVE_CCTXT) |
760 | if (flags & TRANSFER_SAVE_CCTXT) |
744 | { |
761 | { |
745 | if (!prev->stack) |
762 | if (!prev->stack) |
746 | allocate_stack (prev, 0); |
763 | allocate_stack (prev, 0); |
… | |
… | |
748 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
765 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
749 | { |
766 | { |
750 | PL_top_env = &next->start_env; |
767 | PL_top_env = &next->start_env; |
751 | |
768 | |
752 | setup_coro (next); |
769 | setup_coro (next); |
|
|
770 | next->cursp = stacklevel; |
753 | |
771 | |
754 | prev->stack->refcnt++; |
772 | prev->stack->refcnt++; |
755 | prev->stack->usecnt++; |
773 | prev->stack->usecnt++; |
756 | next->stack = prev->stack; |
774 | next->stack = prev->stack; |
757 | next->gencnt = prev->gencnt; |
775 | next->gencnt = prev->gencnt; |
… | |
… | |
762 | allocate_stack (next, 1); |
780 | allocate_stack (next, 1); |
763 | coro_create (&(next->stack->cctx), |
781 | coro_create (&(next->stack->cctx), |
764 | setup_coro, (void *)next, |
782 | setup_coro, (void *)next, |
765 | next->stack->sptr, labs (next->stack->ssize)); |
783 | next->stack->sptr, labs (next->stack->ssize)); |
766 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
784 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
785 | prev->cursp = stacklevel; |
767 | /* don't add any code here */ |
786 | /* don't add any code here */ |
768 | } |
787 | } |
769 | } |
788 | } |
770 | else |
789 | else |
|
|
790 | { |
771 | setup_coro (next); |
791 | setup_coro (next); |
|
|
792 | next->cursp = stacklevel; |
|
|
793 | } |
772 | } |
794 | } |
773 | |
|
|
774 | /* |
|
|
775 | * xnext is now either prev or next, depending on wether |
|
|
776 | * we switched the c stack or not. that's why I use a global |
|
|
777 | * variable, that should become thread-specific at one point. |
|
|
778 | */ |
|
|
779 | xnext->cursp = stacklevel; |
|
|
780 | } |
795 | } |
781 | |
796 | |
|
|
797 | LOCK; |
782 | if (coro_mortal) |
798 | if (coro_mortal) |
783 | { |
799 | { |
784 | SvREFCNT_dec (coro_mortal); |
800 | SvREFCNT_dec (coro_mortal); |
785 | coro_mortal = 0; |
801 | coro_mortal = 0; |
786 | } |
802 | } |
|
|
803 | UNLOCK; |
787 | } |
804 | } |
788 | |
805 | |
789 | #define SV_CORO(sv,func) \ |
806 | #define SV_CORO(sv,func) \ |
790 | do { \ |
807 | do { \ |
791 | if (SvROK (sv)) \ |
808 | if (SvROK (sv)) \ |
792 | sv = SvRV (sv); \ |
809 | sv = SvRV (sv); \ |
793 | \ |
810 | \ |
794 | if (SvTYPE(sv) == SVt_PVHV) \ |
811 | if (SvTYPE (sv) == SVt_PVHV) \ |
795 | { \ |
812 | { \ |
796 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
813 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
797 | \ |
814 | \ |
798 | if (!he) \ |
815 | if (!he) \ |
799 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
816 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
800 | \ |
817 | \ |
801 | (sv) = SvRV (HeVAL(he)); \ |
818 | (sv) = SvRV (HeVAL(he)); \ |
802 | } \ |
819 | } \ |
803 | \ |
820 | \ |
804 | /* must also be changed inside Coro::Cont::yield */ \ |
821 | /* must also be changed inside Coro::Cont::yield */ \ |
805 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
822 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
806 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
823 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
807 | \ |
824 | \ |
808 | } while(0) |
825 | } while(0) |
809 | |
826 | |
810 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
827 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
… | |
… | |
813 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
830 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
814 | { |
831 | { |
815 | SV_CORO (prev, "Coro::transfer"); |
832 | SV_CORO (prev, "Coro::transfer"); |
816 | SV_CORO (next, "Coro::transfer"); |
833 | SV_CORO (next, "Coro::transfer"); |
817 | |
834 | |
818 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
835 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
819 | } |
836 | } |
820 | |
837 | |
821 | /** Coro ********************************************************************/ |
838 | /** Coro ********************************************************************/ |
822 | |
839 | |
823 | #define PRIO_MAX 3 |
840 | #define PRIO_MAX 3 |
… | |
… | |
831 | static GV *coro_current, *coro_idle; |
848 | static GV *coro_current, *coro_idle; |
832 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
849 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
833 | static int coro_nready; |
850 | static int coro_nready; |
834 | |
851 | |
835 | static void |
852 | static void |
836 | coro_enq (SV *sv) |
853 | coro_enq (pTHX_ SV *sv) |
837 | { |
854 | { |
838 | if (SvTYPE (sv) == SVt_PVHV) |
855 | if (SvTYPE (sv) == SVt_PVHV) |
839 | { |
856 | { |
840 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
857 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
841 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
858 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
… | |
… | |
852 | |
869 | |
853 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
870 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
854 | } |
871 | } |
855 | |
872 | |
856 | static SV * |
873 | static SV * |
857 | coro_deq (int min_prio) |
874 | coro_deq (pTHX_ int min_prio) |
858 | { |
875 | { |
859 | int prio = PRIO_MAX - PRIO_MIN; |
876 | int prio = PRIO_MAX - PRIO_MIN; |
860 | |
877 | |
861 | min_prio -= PRIO_MIN; |
878 | min_prio -= PRIO_MIN; |
862 | if (min_prio < 0) |
879 | if (min_prio < 0) |
… | |
… | |
873 | } |
890 | } |
874 | |
891 | |
875 | static void |
892 | static void |
876 | api_ready (SV *coro) |
893 | api_ready (SV *coro) |
877 | { |
894 | { |
|
|
895 | dTHX; |
|
|
896 | |
878 | if (SvROK (coro)) |
897 | if (SvROK (coro)) |
879 | coro = SvRV (coro); |
898 | coro = SvRV (coro); |
880 | |
899 | |
|
|
900 | LOCK; |
881 | coro_enq (SvREFCNT_inc (coro)); |
901 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
|
|
902 | UNLOCK; |
882 | } |
903 | } |
883 | |
904 | |
884 | static void |
905 | static void |
885 | api_schedule (void) |
906 | api_schedule (void) |
886 | { |
907 | { |
|
|
908 | dTHX; |
|
|
909 | |
887 | SV *prev, *next; |
910 | SV *prev, *next; |
888 | |
911 | |
|
|
912 | LOCK; |
|
|
913 | |
889 | prev = SvRV (GvSV (coro_current)); |
914 | prev = SvRV (GvSV (coro_current)); |
890 | next = coro_deq (PRIO_MIN); |
915 | next = coro_deq (aTHX_ PRIO_MIN); |
891 | |
916 | |
892 | if (!next) |
917 | if (!next) |
893 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
918 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
894 | |
919 | |
895 | /* free this only after the transfer */ |
920 | /* free this only after the transfer */ |
… | |
… | |
898 | |
923 | |
899 | SvRV (GvSV (coro_current)) = next; |
924 | SvRV (GvSV (coro_current)) = next; |
900 | |
925 | |
901 | SV_CORO (next, "Coro::schedule"); |
926 | SV_CORO (next, "Coro::schedule"); |
902 | |
927 | |
|
|
928 | UNLOCK; |
|
|
929 | |
903 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
930 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
904 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
931 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
905 | } |
932 | } |
906 | |
933 | |
907 | static void |
934 | static void |
908 | api_cede (void) |
935 | api_cede (void) |
909 | { |
936 | { |
|
|
937 | dTHX; |
|
|
938 | |
|
|
939 | LOCK; |
910 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
940 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
941 | UNLOCK; |
911 | |
942 | |
912 | api_schedule (); |
943 | api_schedule (); |
913 | } |
944 | } |
914 | |
945 | |
915 | MODULE = Coro::State PACKAGE = Coro::State |
946 | MODULE = Coro::State PACKAGE = Coro::State |
916 | |
947 | |
917 | PROTOTYPES: ENABLE |
948 | PROTOTYPES: ENABLE |
918 | |
949 | |
919 | BOOT: |
950 | BOOT: |
920 | { /* {} necessary for stoopid perl-5.6.x */ |
951 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
952 | #ifdef USE_ITHREADS |
|
|
953 | MUTEX_INIT (&coro_mutex); |
|
|
954 | #endif |
|
|
955 | |
921 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
956 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
922 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
957 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
923 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
958 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
924 | |
959 | |
925 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
960 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
926 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
961 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
927 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
962 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
928 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
963 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
929 | |
|
|
930 | if (!padlist_cache) |
|
|
931 | padlist_cache = newHV (); |
|
|
932 | |
964 | |
933 | main_mainstack = PL_mainstack; |
965 | main_mainstack = PL_mainstack; |
934 | |
966 | |
935 | coroapi.ver = CORO_API_VERSION; |
967 | coroapi.ver = CORO_API_VERSION; |
936 | coroapi.transfer = api_transfer; |
968 | coroapi.transfer = api_transfer; |
… | |
… | |
947 | croak ("Coro::State::_newprocess expects an arrayref"); |
979 | croak ("Coro::State::_newprocess expects an arrayref"); |
948 | |
980 | |
949 | Newz (0, coro, 1, struct coro); |
981 | Newz (0, coro, 1, struct coro); |
950 | |
982 | |
951 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
983 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
952 | coro->mainstack = 0; /* actual work is done inside transfer */ |
984 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
953 | coro->stack = 0; |
985 | /*coro->stack = 0;*/ |
954 | |
986 | |
955 | /* same as JMPENV_BOOTSTRAP */ |
987 | /* same as JMPENV_BOOTSTRAP */ |
956 | /* we might be able to recycle start_env, but safe is safe */ |
988 | /* we might be able to recycle start_env, but safe is safe */ |
957 | //Zero(&coro->start_env, 1, JMPENV); |
989 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
958 | coro->start_env.je_ret = -1; |
990 | coro->start_env.je_ret = -1; |
959 | coro->start_env.je_mustcatch = TRUE; |
991 | coro->start_env.je_mustcatch = TRUE; |
960 | |
992 | |
961 | RETVAL = coro; |
993 | RETVAL = coro; |
962 | OUTPUT: |
994 | OUTPUT: |