… | |
… | |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
32 | #include <errno.h> |
32 | #include <errno.h> |
33 | |
33 | |
|
|
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
35 | # undef STACKGUARD |
|
|
36 | #endif |
|
|
37 | |
|
|
38 | #ifndef STACKGUARD |
|
|
39 | # define STACKGUARD 0 |
|
|
40 | #endif |
|
|
41 | |
34 | #ifdef HAVE_MMAP |
42 | #ifdef HAVE_MMAP |
35 | # include <unistd.h> |
43 | # include <unistd.h> |
36 | # include <sys/mman.h> |
44 | # include <sys/mman.h> |
37 | # ifndef MAP_ANONYMOUS |
45 | # ifndef MAP_ANONYMOUS |
38 | # ifdef MAP_ANON |
46 | # ifdef MAP_ANON |
39 | # define MAP_ANONYMOUS MAP_ANON |
47 | # define MAP_ANONYMOUS MAP_ANON |
40 | # else |
48 | # else |
41 | # undef HAVE_MMAP |
49 | # undef HAVE_MMAP |
42 | # endif |
50 | # endif |
43 | # endif |
51 | # endif |
|
|
52 | # include <limits.h> |
|
|
53 | # ifndef PAGESIZE |
|
|
54 | # define PAGESIZE pagesize |
|
|
55 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
56 | static long pagesize; |
|
|
57 | # else |
|
|
58 | # define BOOT_PAGESIZE |
44 | #endif |
59 | # endif |
|
|
60 | #endif |
45 | |
61 | |
46 | #define SUB_INIT "Coro::State::initialize" |
62 | #define SUB_INIT "Coro::State::initialize" |
47 | #define UCORO_STATE "_coro_state" |
|
|
48 | |
63 | |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
64 | /* The next macro should declare a variable stacklevel that contains and approximation |
50 | * to the current C stack pointer. Its property is that it changes with each call |
65 | * to the current C stack pointer. Its property is that it changes with each call |
51 | * and should be unique. */ |
66 | * and should be unique. */ |
52 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
67 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
… | |
… | |
66 | # define UNLOCK (void)0 |
81 | # define UNLOCK (void)0 |
67 | #endif |
82 | #endif |
68 | |
83 | |
69 | static struct CoroAPI coroapi; |
84 | static struct CoroAPI coroapi; |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
71 | static HV *coro_state_stash; |
86 | static HV *coro_state_stash, *coro_stash; |
72 | static SV *ucoro_state_sv; |
|
|
73 | static U32 ucoro_state_hash; |
|
|
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
75 | |
88 | |
76 | /* this is actually not only the c stack but also c registers etc... */ |
89 | /* this is actually not just the c stack but also c registers etc... */ |
77 | typedef struct { |
90 | typedef struct coro_stack { |
78 | int refcnt; /* pointer reference counter */ |
91 | struct coro_stack *next; |
79 | int usecnt; /* shared by how many coroutines */ |
|
|
80 | int gencnt; /* generation counter */ |
|
|
81 | |
92 | |
82 | coro_context cctx; |
93 | void *idle_sp; |
83 | |
|
|
84 | void *sptr; |
94 | void *sptr; |
85 | long ssize; /* positive == mmap, otherwise malloc */ |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
|
|
96 | |
|
|
97 | /* cpu state */ |
|
|
98 | coro_context cctx; |
86 | } coro_stack; |
99 | } coro_stack; |
87 | |
100 | |
|
|
101 | static coro_stack *main_stack; |
|
|
102 | |
88 | struct coro { |
103 | struct coro { |
89 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
90 | JMPENV start_env; |
|
|
91 | |
|
|
92 | /* the optional C context */ |
|
|
93 | coro_stack *stack; |
|
|
94 | void *cursp; |
|
|
95 | int gencnt; |
|
|
96 | |
|
|
97 | /* optionally saved, might be zero */ |
104 | /* optionally saved, might be zero */ |
98 | AV *defav; |
105 | AV *defav; |
99 | SV *defsv; |
106 | SV *defsv; |
100 | SV *errsv; |
107 | SV *errsv; |
101 | |
108 | |
102 | /* saved global state not related to stacks */ |
109 | /* saved global state not related to stacks */ |
103 | U8 dowarn; |
110 | U8 dowarn; |
104 | I32 in_eval; |
111 | I32 in_eval; |
|
|
112 | |
|
|
113 | /* the c stack, if any */ |
|
|
114 | coro_stack *stack; |
105 | |
115 | |
106 | /* the stacks and related info (callchain etc..) */ |
116 | /* the stacks and related info (callchain etc..) */ |
107 | PERL_SI *curstackinfo; |
117 | PERL_SI *curstackinfo; |
108 | AV *curstack; |
118 | AV *curstack; |
109 | AV *mainstack; |
119 | AV *mainstack; |
… | |
… | |
132 | I32 retstack_max; |
142 | I32 retstack_max; |
133 | PMOP *curpm; |
143 | PMOP *curpm; |
134 | COP *curcop; |
144 | COP *curcop; |
135 | JMPENV *top_env; |
145 | JMPENV *top_env; |
136 | |
146 | |
|
|
147 | /* coro process data */ |
|
|
148 | int prio; |
|
|
149 | |
137 | /* data associated with this coroutine (initial args) */ |
150 | /* data associated with this coroutine (initial args) */ |
138 | AV *args; |
151 | AV *args; |
|
|
152 | int refcnt; |
139 | }; |
153 | }; |
140 | |
154 | |
141 | typedef struct coro *Coro__State; |
155 | typedef struct coro *Coro__State; |
142 | typedef struct coro *Coro__State_or_hashref; |
156 | typedef struct coro *Coro__State_or_hashref; |
143 | |
157 | |
144 | /* mostly copied from op.c:cv_clone2 */ |
158 | static AV * |
145 | STATIC AV * |
159 | coro_clone_padlist (pTHX_ CV *cv) |
146 | clone_padlist (pTHX_ AV *protopadlist) |
|
|
147 | { |
160 | { |
148 | AV *av; |
161 | AV *padlist = CvPADLIST (cv); |
149 | I32 ix; |
|
|
150 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
|
|
151 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
|
|
152 | SV **pname = AvARRAY (protopad_name); |
|
|
153 | SV **ppad = AvARRAY (protopad); |
|
|
154 | I32 fname = AvFILLp (protopad_name); |
|
|
155 | I32 fpad = AvFILLp (protopad); |
|
|
156 | AV *newpadlist, *newpad_name, *newpad; |
162 | AV *newpadlist, *newpad; |
157 | SV **npad; |
|
|
158 | |
|
|
159 | newpad_name = newAV (); |
|
|
160 | for (ix = fname; ix >= 0; ix--) |
|
|
161 | av_store (newpad_name, ix, SvREFCNT_inc (pname[ix])); |
|
|
162 | |
|
|
163 | newpad = newAV (); |
|
|
164 | av_fill (newpad, AvFILLp (protopad)); |
|
|
165 | npad = AvARRAY (newpad); |
|
|
166 | |
163 | |
167 | newpadlist = newAV (); |
164 | newpadlist = newAV (); |
168 | AvREAL_off (newpadlist); |
165 | AvREAL_off (newpadlist); |
169 | av_store (newpadlist, 0, (SV *) newpad_name); |
166 | #if PERL_VERSION < 9 |
|
|
167 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
168 | #else |
|
|
169 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
170 | #endif |
|
|
171 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
|
|
172 | --AvFILLp (padlist); |
|
|
173 | |
|
|
174 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
170 | av_store (newpadlist, 1, (SV *) newpad); |
175 | av_store (newpadlist, 1, (SV *)newpad); |
171 | |
|
|
172 | av = newAV (); /* will be @_ */ |
|
|
173 | av_extend (av, 0); |
|
|
174 | av_store (newpad, 0, (SV *) av); |
|
|
175 | AvREIFY_on (av); |
|
|
176 | |
|
|
177 | for (ix = fpad; ix > 0; ix--) |
|
|
178 | { |
|
|
179 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
180 | |
|
|
181 | if (namesv && namesv != &PL_sv_undef) |
|
|
182 | { |
|
|
183 | char *name = SvPVX (namesv); /* XXX */ |
|
|
184 | |
|
|
185 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
|
|
186 | { /* lexical from outside? */ |
|
|
187 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
188 | } |
|
|
189 | else |
|
|
190 | { /* our own lexical */ |
|
|
191 | SV *sv; |
|
|
192 | if (*name == '&') |
|
|
193 | sv = SvREFCNT_inc (ppad[ix]); |
|
|
194 | else if (*name == '@') |
|
|
195 | sv = (SV *) newAV (); |
|
|
196 | else if (*name == '%') |
|
|
197 | sv = (SV *) newHV (); |
|
|
198 | else |
|
|
199 | sv = NEWSV (0, 0); |
|
|
200 | |
|
|
201 | #ifdef SvPADBUSY |
|
|
202 | if (!SvPADBUSY (sv)) |
|
|
203 | #endif |
|
|
204 | SvPADMY_on (sv); |
|
|
205 | |
|
|
206 | npad[ix] = sv; |
|
|
207 | } |
|
|
208 | } |
|
|
209 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
|
|
210 | { |
|
|
211 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
212 | } |
|
|
213 | else |
|
|
214 | { |
|
|
215 | SV *sv = NEWSV (0, 0); |
|
|
216 | SvPADTMP_on (sv); |
|
|
217 | npad[ix] = sv; |
|
|
218 | } |
|
|
219 | } |
|
|
220 | |
|
|
221 | #if 0 /* return -ENOTUNDERSTOOD */ |
|
|
222 | /* Now that vars are all in place, clone nested closures. */ |
|
|
223 | |
|
|
224 | for (ix = fpad; ix > 0; ix--) { |
|
|
225 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
226 | if (namesv |
|
|
227 | && namesv != &PL_sv_undef |
|
|
228 | && !(SvFLAGS(namesv) & SVf_FAKE) |
|
|
229 | && *SvPVX(namesv) == '&' |
|
|
230 | && CvCLONE(ppad[ix])) |
|
|
231 | { |
|
|
232 | CV *kid = cv_clone((CV*)ppad[ix]); |
|
|
233 | SvREFCNT_dec(ppad[ix]); |
|
|
234 | CvCLONE_on(kid); |
|
|
235 | SvPADMY_on(kid); |
|
|
236 | npad[ix] = (SV*)kid; |
|
|
237 | } |
|
|
238 | } |
|
|
239 | #endif |
|
|
240 | |
176 | |
241 | return newpadlist; |
177 | return newpadlist; |
242 | } |
178 | } |
243 | |
179 | |
244 | STATIC void |
180 | static void |
245 | free_padlist (pTHX_ AV *padlist) |
181 | free_padlist (pTHX_ AV *padlist) |
246 | { |
182 | { |
247 | /* may be during global destruction */ |
183 | /* may be during global destruction */ |
248 | if (SvREFCNT (padlist)) |
184 | if (SvREFCNT (padlist)) |
249 | { |
185 | { |
… | |
… | |
263 | |
199 | |
264 | SvREFCNT_dec ((SV*)padlist); |
200 | SvREFCNT_dec ((SV*)padlist); |
265 | } |
201 | } |
266 | } |
202 | } |
267 | |
203 | |
268 | STATIC int |
204 | static int |
269 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
205 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
270 | { |
206 | { |
271 | AV *padlist; |
207 | AV *padlist; |
272 | AV *av = (AV *)mg->mg_obj; |
208 | AV *av = (AV *)mg->mg_obj; |
273 | |
209 | |
… | |
… | |
283 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
219 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
284 | |
220 | |
285 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
221 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
286 | |
222 | |
287 | /* the next two functions merely cache the padlists */ |
223 | /* the next two functions merely cache the padlists */ |
288 | STATIC void |
224 | static void |
289 | get_padlist (pTHX_ CV *cv) |
225 | get_padlist (pTHX_ CV *cv) |
290 | { |
226 | { |
291 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
227 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
292 | |
228 | |
293 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
294 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
295 | else |
231 | else |
|
|
232 | { |
|
|
233 | #if 0 |
|
|
234 | /* this should work - but it doesn't :( */ |
|
|
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
|
|
236 | CvPADLIST (cv) = CvPADLIST (cp); |
|
|
237 | CvPADLIST (cp) = 0; |
|
|
238 | SvREFCNT_dec (cp); |
|
|
239 | #else |
296 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
240 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
|
|
241 | #endif |
|
|
242 | } |
297 | } |
243 | } |
298 | |
244 | |
299 | STATIC void |
245 | static void |
300 | put_padlist (pTHX_ CV *cv) |
246 | put_padlist (pTHX_ CV *cv) |
301 | { |
247 | { |
302 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
248 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
303 | |
249 | |
304 | if (!mg) |
250 | if (!mg) |
… | |
… | |
421 | } |
367 | } |
422 | |
368 | |
423 | PUSHs ((SV *)CvPADLIST(cv)); |
369 | PUSHs ((SV *)CvPADLIST(cv)); |
424 | PUSHs ((SV *)cv); |
370 | PUSHs ((SV *)cv); |
425 | |
371 | |
426 | get_padlist (aTHX_ cv); /* this is a monster */ |
372 | get_padlist (aTHX_ cv); |
427 | } |
373 | } |
428 | } |
374 | } |
429 | #ifdef CXt_FORMAT |
375 | #ifdef CXt_FORMAT |
430 | else if (CxTYPE(cx) == CXt_FORMAT) |
376 | else if (CxTYPE(cx) == CXt_FORMAT) |
431 | { |
377 | { |
… | |
… | |
491 | * allocate various perl stacks. This is an exact copy |
437 | * allocate various perl stacks. This is an exact copy |
492 | * of perl.c:init_stacks, except that it uses less memory |
438 | * of perl.c:init_stacks, except that it uses less memory |
493 | * on the (sometimes correct) assumption that coroutines do |
439 | * on the (sometimes correct) assumption that coroutines do |
494 | * not usually need a lot of stackspace. |
440 | * not usually need a lot of stackspace. |
495 | */ |
441 | */ |
496 | STATIC void |
442 | static void |
497 | coro_init_stacks (pTHX) |
443 | coro_init_stacks (pTHX) |
498 | { |
444 | { |
499 | LOCK; |
445 | LOCK; |
500 | |
446 | |
501 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
447 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
… | |
… | |
538 | } |
484 | } |
539 | |
485 | |
540 | /* |
486 | /* |
541 | * destroy the stacks, the callchain etc... |
487 | * destroy the stacks, the callchain etc... |
542 | */ |
488 | */ |
543 | STATIC void |
489 | static void |
544 | destroy_stacks(pTHX) |
490 | destroy_stacks(pTHX) |
545 | { |
491 | { |
546 | if (!IN_DESTRUCT) |
492 | if (!IN_DESTRUCT) |
547 | { |
493 | { |
548 | /* is this ugly, I ask? */ |
494 | /* is this ugly, I ask? */ |
… | |
… | |
586 | Safefree (PL_retstack); |
532 | Safefree (PL_retstack); |
587 | #endif |
533 | #endif |
588 | } |
534 | } |
589 | |
535 | |
590 | static void |
536 | static void |
591 | allocate_stack (Coro__State ctx, int alloc) |
537 | setup_coro (struct coro *coro) |
592 | { |
|
|
593 | coro_stack *stack; |
|
|
594 | |
|
|
595 | New (0, stack, 1, coro_stack); |
|
|
596 | |
|
|
597 | stack->refcnt = 1; |
|
|
598 | stack->usecnt = 1; |
|
|
599 | stack->gencnt = ctx->gencnt = 0; |
|
|
600 | |
|
|
601 | if (alloc) |
|
|
602 | { |
|
|
603 | #if HAVE_MMAP |
|
|
604 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
|
|
605 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
|
|
606 | if (stack->sptr == (void *)-1) |
|
|
607 | #endif |
|
|
608 | { |
|
|
609 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
|
|
610 | New (0, stack->sptr, STACKSIZE, long); |
|
|
611 | } |
|
|
612 | } |
|
|
613 | else |
|
|
614 | stack->sptr = 0; |
|
|
615 | |
|
|
616 | ctx->stack = stack; |
|
|
617 | } |
|
|
618 | |
|
|
619 | static void |
|
|
620 | deallocate_stack (Coro__State ctx) |
|
|
621 | { |
|
|
622 | coro_stack *stack = ctx->stack; |
|
|
623 | |
|
|
624 | ctx->stack = 0; |
|
|
625 | |
|
|
626 | if (stack) |
|
|
627 | { |
|
|
628 | if (!--stack->refcnt) |
|
|
629 | { |
|
|
630 | #ifdef HAVE_MMAP |
|
|
631 | if (stack->ssize > 0 && stack->sptr) |
|
|
632 | munmap (stack->sptr, stack->ssize); |
|
|
633 | else |
|
|
634 | #endif |
|
|
635 | Safefree (stack->sptr); |
|
|
636 | |
|
|
637 | Safefree (stack); |
|
|
638 | } |
|
|
639 | else if (ctx->gencnt == stack->gencnt) |
|
|
640 | --stack->usecnt; |
|
|
641 | } |
|
|
642 | } |
|
|
643 | |
|
|
644 | static void |
|
|
645 | setup_coro (void *arg) |
|
|
646 | { |
538 | { |
647 | /* |
539 | /* |
648 | * emulate part of the perl startup here. |
540 | * emulate part of the perl startup here. |
649 | */ |
541 | */ |
650 | dTHX; |
542 | dTHX; |
651 | dSP; |
543 | dSP; |
652 | Coro__State ctx = (Coro__State)arg; |
544 | UNOP myop; |
653 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
545 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
654 | |
546 | |
655 | coro_init_stacks (aTHX); |
547 | coro_init_stacks (aTHX); |
656 | /*PL_curcop = 0;*/ |
548 | /*PL_curcop = 0;*/ |
657 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
549 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
658 | SvREFCNT_dec (GvAV (PL_defgv)); |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
659 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
551 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
660 | |
552 | |
661 | SPAGAIN; |
553 | SPAGAIN; |
662 | |
554 | |
663 | if (ctx->stack) |
555 | Zero (&myop, 1, UNOP); |
664 | { |
556 | myop.op_next = Nullop; |
665 | ctx->cursp = 0; |
557 | myop.op_flags = OPf_WANT_VOID; |
666 | |
558 | |
|
|
559 | PL_op = (OP *)&myop; |
|
|
560 | |
667 | PUSHMARK(SP); |
561 | PUSHMARK(SP); |
|
|
562 | XPUSHs (sub_init); |
668 | PUTBACK; |
563 | PUTBACK; |
669 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
564 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
565 | SPAGAIN; |
670 | |
566 | |
671 | if (SvTRUE (ERRSV)) |
567 | ENTER; /* necessary e.g. for dounwind */ |
672 | croak (NULL); |
568 | } |
673 | else |
569 | |
674 | croak ("FATAL: CCTXT coroutine returned!"); |
570 | static void |
|
|
571 | transfer_tail () |
|
|
572 | { |
|
|
573 | if (coro_mortal) |
|
|
574 | { |
|
|
575 | SvREFCNT_dec (coro_mortal); |
|
|
576 | coro_mortal = 0; |
675 | } |
577 | } |
676 | else |
|
|
677 | { |
|
|
678 | UNOP myop; |
|
|
679 | |
578 | |
680 | PL_op = (OP *)&myop; |
579 | UNLOCK; |
681 | |
|
|
682 | Zero(&myop, 1, UNOP); |
|
|
683 | myop.op_next = Nullop; |
|
|
684 | myop.op_flags = OPf_WANT_VOID; |
|
|
685 | |
|
|
686 | PUSHMARK(SP); |
|
|
687 | XPUSHs (sub_init); |
|
|
688 | /* |
|
|
689 | * the next line is slightly wrong, as PL_op->op_next |
|
|
690 | * is actually being executed so we skip the first op. |
|
|
691 | * that doesn't matter, though, since it is only |
|
|
692 | * pp_nextstate and we never return... |
|
|
693 | * ah yes, and I don't care anyways ;) |
|
|
694 | */ |
|
|
695 | PUTBACK; |
|
|
696 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
697 | SPAGAIN; |
|
|
698 | |
|
|
699 | ENTER; /* necessary e.g. for dounwind */ |
|
|
700 | } |
|
|
701 | } |
580 | } |
702 | |
581 | |
703 | static void |
582 | static void |
704 | continue_coro (void *arg) |
583 | coro_run (void *arg) |
705 | { |
584 | { |
706 | /* |
585 | /* |
707 | * this is a _very_ stripped down perl interpreter ;) |
586 | * this is a _very_ stripped down perl interpreter ;) |
708 | */ |
587 | */ |
709 | dTHX; |
588 | dTHX; |
710 | Coro__State ctx = (Coro__State)arg; |
|
|
711 | |
589 | |
|
|
590 | transfer_tail (); |
|
|
591 | |
712 | PL_top_env = &ctx->start_env; |
592 | PL_top_env = &PL_start_env; |
713 | |
|
|
714 | ctx->cursp = 0; |
|
|
715 | PL_op = PL_op->op_next; |
593 | PL_restartop = PL_op->op_next; |
716 | CALLRUNOPS(aTHX); |
594 | /* somebody will hit me for both perl_run and PL_restart_top */ |
|
|
595 | perl_run (aTHX_ PERL_GET_CONTEXT); |
717 | |
596 | |
718 | abort (); |
597 | abort (); |
719 | } |
598 | } |
720 | |
599 | |
721 | STATIC void |
600 | static coro_stack * |
|
|
601 | stack_new () |
|
|
602 | { |
|
|
603 | coro_stack *stack; |
|
|
604 | |
|
|
605 | New (0, stack, 1, coro_stack); |
|
|
606 | |
|
|
607 | #if HAVE_MMAP |
|
|
608 | |
|
|
609 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
|
|
610 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
|
|
611 | |
|
|
612 | if (stack->sptr == (void *)-1) |
|
|
613 | { |
|
|
614 | fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); |
|
|
615 | _exit (EXIT_FAILURE); |
|
|
616 | } |
|
|
617 | |
|
|
618 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
619 | |
|
|
620 | #else |
|
|
621 | |
|
|
622 | stack->ssize = STACKSIZE * (long)sizeof (long); |
|
|
623 | New (0, stack->sptr, STACKSIZE, long); |
|
|
624 | |
|
|
625 | if (!stack->sptr) |
|
|
626 | { |
|
|
627 | fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); |
|
|
628 | _exit (EXIT_FAILURE); |
|
|
629 | } |
|
|
630 | |
|
|
631 | #endif |
|
|
632 | |
|
|
633 | coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); |
|
|
634 | |
|
|
635 | return stack; |
|
|
636 | } |
|
|
637 | |
|
|
638 | static void |
|
|
639 | stack_free (coro_stack *stack) |
|
|
640 | { |
|
|
641 | if (!stack || stack == main_stack) |
|
|
642 | return; |
|
|
643 | |
|
|
644 | #if HAVE_MMAP |
|
|
645 | munmap (stack->sptr, stack->ssize); |
|
|
646 | #else |
|
|
647 | Safefree (stack->sptr); |
|
|
648 | #endif |
|
|
649 | |
|
|
650 | Safefree (stack); |
|
|
651 | } |
|
|
652 | |
|
|
653 | static coro_stack *stack_first; |
|
|
654 | |
|
|
655 | static void |
|
|
656 | stack_get (struct coro *coro) |
|
|
657 | { |
|
|
658 | if (stack_first) |
|
|
659 | { |
|
|
660 | coro->stack = stack_first; |
|
|
661 | stack_first = stack_first->next; |
|
|
662 | } |
|
|
663 | else |
|
|
664 | coro->stack = stack_new (); |
|
|
665 | } |
|
|
666 | |
|
|
667 | static void |
|
|
668 | stack_put (coro_stack *stack) |
|
|
669 | { |
|
|
670 | stack->next = stack_first; |
|
|
671 | stack_first = stack; |
|
|
672 | } |
|
|
673 | |
|
|
674 | /* never call directly, always through the coro_state_transfer global variable */ |
|
|
675 | static void |
722 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
676 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
723 | { |
677 | { |
724 | dSTACKLEVEL; |
678 | dSTACKLEVEL; |
725 | |
679 | |
|
|
680 | /* sometimes transfer is only called to set idle_sp */ |
|
|
681 | if (!prev->stack->idle_sp) |
|
|
682 | prev->stack->idle_sp = stacklevel; |
|
|
683 | |
|
|
684 | LOCK; |
|
|
685 | |
726 | if (prev != next) |
686 | if (prev != next) |
727 | { |
687 | { |
|
|
688 | coro_stack *prev_stack = prev->stack; |
|
|
689 | |
|
|
690 | /* possibly "free" the stack */ |
|
|
691 | if (prev_stack->idle_sp == stacklevel) |
|
|
692 | { |
|
|
693 | stack_put (prev_stack); |
|
|
694 | prev->stack = 0; |
|
|
695 | } |
|
|
696 | |
728 | if (next->mainstack) |
697 | if (next->mainstack) |
729 | { |
698 | { |
730 | LOCK; |
699 | /* coroutine already started */ |
731 | SAVE (prev, flags); |
700 | SAVE (prev, flags); |
732 | LOAD (next); |
701 | LOAD (next); |
733 | UNLOCK; |
|
|
734 | |
|
|
735 | /* mark this state as in-use */ |
|
|
736 | next->mainstack = 0; |
|
|
737 | next->tmps_ix = -2; |
|
|
738 | |
|
|
739 | /* stacklevel changed? if yes, grab the stack for us! */ |
|
|
740 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
741 | { |
|
|
742 | if (!prev->stack) |
|
|
743 | allocate_stack (prev, 0); |
|
|
744 | else if (prev->cursp != stacklevel |
|
|
745 | && prev->stack->usecnt > 1) |
|
|
746 | { |
|
|
747 | prev->gencnt = ++prev->stack->gencnt; |
|
|
748 | prev->stack->usecnt = 1; |
|
|
749 | } |
|
|
750 | |
|
|
751 | /* has our stack been invalidated? */ |
|
|
752 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
753 | { |
|
|
754 | deallocate_stack (next); |
|
|
755 | allocate_stack (next, 1); |
|
|
756 | coro_create (&(next->stack->cctx), |
|
|
757 | continue_coro, (void *)next, |
|
|
758 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
759 | } |
|
|
760 | |
|
|
761 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
762 | prev->cursp = stacklevel; |
|
|
763 | /* don't add any code here */ |
|
|
764 | } |
|
|
765 | else |
|
|
766 | next->cursp = stacklevel; |
|
|
767 | } |
702 | } |
768 | else if (next->tmps_ix == -2) |
|
|
769 | croak ("tried to transfer to running coroutine"); |
|
|
770 | else |
703 | else |
771 | { |
704 | { |
772 | LOCK; |
705 | /* need to start coroutine */ |
773 | SAVE (prev, -1); /* first get rid of the old state */ |
706 | /* first get rid of the old state */ |
774 | UNLOCK; |
707 | SAVE (prev, -1); |
775 | |
708 | /* setup coroutine call */ |
776 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
777 | { |
|
|
778 | if (!prev->stack) |
|
|
779 | allocate_stack (prev, 0); |
|
|
780 | |
|
|
781 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
|
|
782 | { |
|
|
783 | PL_top_env = &next->start_env; |
|
|
784 | |
|
|
785 | setup_coro (next); |
|
|
786 | next->cursp = stacklevel; |
|
|
787 | |
|
|
788 | prev->stack->refcnt++; |
|
|
789 | prev->stack->usecnt++; |
|
|
790 | next->stack = prev->stack; |
|
|
791 | next->gencnt = prev->gencnt; |
|
|
792 | } |
|
|
793 | else |
|
|
794 | { |
|
|
795 | assert (!next->stack); |
|
|
796 | allocate_stack (next, 1); |
|
|
797 | coro_create (&(next->stack->cctx), |
|
|
798 | setup_coro, (void *)next, |
|
|
799 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
800 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
801 | prev->cursp = stacklevel; |
|
|
802 | /* don't add any code here */ |
|
|
803 | } |
|
|
804 | } |
|
|
805 | else |
|
|
806 | { |
|
|
807 | setup_coro (next); |
709 | setup_coro (next); |
808 | next->cursp = stacklevel; |
710 | /* need to change stack from main_stack to real one */ |
809 | } |
711 | next->stack = 0; |
810 | } |
712 | } |
|
|
713 | |
|
|
714 | if (!next->stack) |
|
|
715 | stack_get (next); |
|
|
716 | |
|
|
717 | if (prev_stack != next->stack) |
|
|
718 | coro_transfer (&prev_stack->cctx, &next->stack->cctx); |
811 | } |
719 | } |
812 | |
720 | |
813 | LOCK; |
721 | transfer_tail (); |
814 | if (coro_mortal) |
722 | } |
|
|
723 | |
|
|
724 | /* use this function pointer to call the above function */ |
|
|
725 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
726 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
727 | |
|
|
728 | static void |
|
|
729 | coro_state_destroy (struct coro *coro) |
|
|
730 | { |
|
|
731 | if (coro->refcnt--) |
|
|
732 | return; |
|
|
733 | |
|
|
734 | if (coro->mainstack && coro->mainstack != main_mainstack) |
815 | { |
735 | { |
816 | SvREFCNT_dec (coro_mortal); |
736 | struct coro temp; |
|
|
737 | |
|
|
738 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
|
|
739 | LOAD (aTHX_ coro); |
|
|
740 | |
|
|
741 | destroy_stacks (aTHX); |
|
|
742 | |
|
|
743 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
|
|
744 | |
817 | coro_mortal = 0; |
745 | coro->mainstack = 0; |
818 | } |
746 | } |
819 | UNLOCK; |
|
|
820 | } |
|
|
821 | |
747 | |
822 | #define SV_CORO(sv,func) \ |
748 | stack_free (coro->stack); |
823 | do { \ |
749 | SvREFCNT_dec (coro->args); |
824 | if (SvROK (sv)) \ |
750 | Safefree (coro); |
825 | sv = SvRV (sv); \ |
751 | } |
826 | \ |
|
|
827 | if (SvTYPE (sv) == SVt_PVHV) \ |
|
|
828 | { \ |
|
|
829 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
|
|
830 | \ |
|
|
831 | if (!he) \ |
|
|
832 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
|
|
833 | \ |
|
|
834 | (sv) = SvRV (HeVAL(he)); \ |
|
|
835 | } \ |
|
|
836 | \ |
|
|
837 | /* must also be changed inside Coro::Cont::yield */ \ |
|
|
838 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
|
|
839 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
|
|
840 | \ |
|
|
841 | } while(0) |
|
|
842 | |
752 | |
843 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
|
|
844 | |
|
|
845 | static void |
753 | static int |
|
|
754 | coro_state_clear (SV *sv, MAGIC *mg) |
|
|
755 | { |
|
|
756 | struct coro *coro = (struct coro *)mg->mg_ptr; |
|
|
757 | mg->mg_ptr = 0; |
|
|
758 | |
|
|
759 | coro_state_destroy (coro); |
|
|
760 | |
|
|
761 | return 0; |
|
|
762 | } |
|
|
763 | |
|
|
764 | static int |
|
|
765 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
|
|
766 | { |
|
|
767 | struct coro *coro = (struct coro *)mg->mg_ptr; |
|
|
768 | |
|
|
769 | ++coro->refcnt; |
|
|
770 | |
|
|
771 | return 0; |
|
|
772 | } |
|
|
773 | |
|
|
774 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
|
|
775 | |
|
|
776 | static struct coro * |
|
|
777 | SvSTATE (SV *coro) |
|
|
778 | { |
|
|
779 | HV *stash; |
|
|
780 | MAGIC *mg; |
|
|
781 | |
|
|
782 | if (SvROK (coro)) |
|
|
783 | coro = SvRV (coro); |
|
|
784 | |
|
|
785 | stash = SvSTASH (coro); |
|
|
786 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
787 | { |
|
|
788 | /* very slow, but rare, check */ |
|
|
789 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
790 | croak ("Coro::State object required"); |
|
|
791 | } |
|
|
792 | |
|
|
793 | mg = SvMAGIC (coro); |
|
|
794 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
795 | return (struct coro *)mg->mg_ptr; |
|
|
796 | } |
|
|
797 | |
|
|
798 | static void |
846 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
799 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
847 | { |
800 | { |
848 | SV_CORO (prev, "Coro::transfer"); |
|
|
849 | SV_CORO (next, "Coro::transfer"); |
|
|
850 | |
|
|
851 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
801 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
852 | } |
802 | } |
853 | |
803 | |
854 | /** Coro ********************************************************************/ |
804 | /** Coro ********************************************************************/ |
855 | |
805 | |
856 | #define PRIO_MAX 3 |
806 | #define PRIO_MAX 3 |
… | |
… | |
860 | #define PRIO_IDLE -3 |
810 | #define PRIO_IDLE -3 |
861 | #define PRIO_MIN -4 |
811 | #define PRIO_MIN -4 |
862 | |
812 | |
863 | /* for Coro.pm */ |
813 | /* for Coro.pm */ |
864 | static GV *coro_current, *coro_idle; |
814 | static GV *coro_current, *coro_idle; |
865 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
815 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
866 | static int coro_nready; |
816 | static int coro_nready; |
867 | |
817 | |
868 | static void |
818 | static void |
869 | coro_enq (pTHX_ SV *sv) |
819 | coro_enq (pTHX_ SV *sv) |
870 | { |
820 | { |
871 | SV **xprio; |
|
|
872 | int prio; |
821 | int prio; |
873 | |
822 | |
874 | if (SvTYPE (sv) != SVt_PVHV) |
823 | if (SvTYPE (sv) != SVt_PVHV) |
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
824 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
876 | |
825 | |
877 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
826 | prio = SvSTATE (sv)->prio; |
878 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
879 | |
|
|
880 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
881 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
882 | : prio; |
|
|
883 | |
827 | |
884 | av_push (coro_ready [prio - PRIO_MIN], sv); |
828 | av_push (coro_ready [prio - PRIO_MIN], sv); |
885 | coro_nready++; |
829 | coro_nready++; |
886 | } |
830 | } |
887 | |
831 | |
… | |
… | |
893 | min_prio -= PRIO_MIN; |
837 | min_prio -= PRIO_MIN; |
894 | if (min_prio < 0) |
838 | if (min_prio < 0) |
895 | min_prio = 0; |
839 | min_prio = 0; |
896 | |
840 | |
897 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
841 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
898 | if (av_len (coro_ready[prio]) >= 0) |
842 | if (AvFILLp (coro_ready [prio]) >= 0) |
899 | { |
843 | { |
900 | coro_nready--; |
844 | coro_nready--; |
901 | return av_shift (coro_ready[prio]); |
845 | return av_shift (coro_ready [prio]); |
902 | } |
846 | } |
903 | |
847 | |
904 | return 0; |
848 | return 0; |
905 | } |
849 | } |
906 | |
850 | |
… | |
… | |
921 | api_schedule (void) |
865 | api_schedule (void) |
922 | { |
866 | { |
923 | dTHX; |
867 | dTHX; |
924 | |
868 | |
925 | SV *prev, *next; |
869 | SV *prev, *next; |
|
|
870 | SV *current = GvSV (coro_current); |
926 | |
871 | |
|
|
872 | for (;;) |
|
|
873 | { |
927 | LOCK; |
874 | LOCK; |
928 | |
875 | |
929 | prev = SvRV (GvSV (coro_current)); |
|
|
930 | next = coro_deq (aTHX_ PRIO_MIN); |
876 | next = coro_deq (aTHX_ PRIO_MIN); |
931 | |
877 | |
932 | if (!next) |
878 | if (next) |
933 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
879 | break; |
|
|
880 | |
|
|
881 | UNLOCK; |
|
|
882 | |
|
|
883 | { |
|
|
884 | dSP; |
|
|
885 | |
|
|
886 | ENTER; |
|
|
887 | SAVETMPS; |
|
|
888 | |
|
|
889 | PUSHMARK (SP); |
|
|
890 | PUTBACK; |
|
|
891 | call_sv (GvSV (coro_idle), G_DISCARD); |
|
|
892 | |
|
|
893 | FREETMPS; |
|
|
894 | LEAVE; |
|
|
895 | } |
|
|
896 | } |
|
|
897 | |
|
|
898 | prev = SvRV (current); |
|
|
899 | SvRV (current) = next; |
934 | |
900 | |
935 | /* free this only after the transfer */ |
901 | /* free this only after the transfer */ |
936 | coro_mortal = prev; |
902 | coro_mortal = prev; |
937 | SV_CORO (prev, "Coro::schedule"); |
|
|
938 | |
|
|
939 | SvRV (GvSV (coro_current)) = next; |
|
|
940 | |
|
|
941 | SV_CORO (next, "Coro::schedule"); |
|
|
942 | |
903 | |
943 | UNLOCK; |
904 | UNLOCK; |
944 | |
905 | |
945 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
906 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
946 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
947 | } |
907 | } |
|
|
908 | |
|
|
909 | static int coro_cede_self; |
948 | |
910 | |
949 | static void |
911 | static void |
950 | api_cede (void) |
912 | api_cede (void) |
951 | { |
913 | { |
952 | dTHX; |
914 | dTHX; |
|
|
915 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
953 | |
916 | |
954 | LOCK; |
917 | LOCK; |
955 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
918 | |
|
|
919 | if (coro_cede_self) |
|
|
920 | { |
|
|
921 | AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN]; |
|
|
922 | av_unshift (runqueue, 1); |
|
|
923 | av_store (runqueue, 0, current); |
|
|
924 | coro_nready++; |
|
|
925 | coro_cede_self = 0; |
|
|
926 | } |
|
|
927 | else |
|
|
928 | coro_enq (aTHX_ current); |
|
|
929 | |
956 | UNLOCK; |
930 | UNLOCK; |
957 | |
931 | |
958 | api_schedule (); |
932 | api_schedule (); |
959 | } |
933 | } |
960 | |
934 | |
961 | MODULE = Coro::State PACKAGE = Coro::State |
935 | MODULE = Coro::State PACKAGE = Coro::State |
962 | |
936 | |
963 | PROTOTYPES: ENABLE |
937 | PROTOTYPES: DISABLE |
964 | |
938 | |
965 | BOOT: |
939 | BOOT: |
966 | { /* {} necessary for stoopid perl-5.6.x */ |
940 | { |
967 | #ifdef USE_ITHREADS |
941 | #ifdef USE_ITHREADS |
968 | MUTEX_INIT (&coro_mutex); |
942 | MUTEX_INIT (&coro_mutex); |
969 | #endif |
943 | #endif |
|
|
944 | BOOT_PAGESIZE; |
970 | |
945 | |
971 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
|
|
972 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
|
|
973 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
946 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
974 | |
947 | |
975 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
948 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
976 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
949 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
977 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
950 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
978 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
|
|
979 | |
951 | |
980 | main_mainstack = PL_mainstack; |
952 | main_mainstack = PL_mainstack; |
981 | |
953 | |
982 | coroapi.ver = CORO_API_VERSION; |
954 | coroapi.ver = CORO_API_VERSION; |
983 | coroapi.transfer = api_transfer; |
955 | coroapi.transfer = api_transfer; |
984 | } |
|
|
985 | |
956 | |
986 | Coro::State |
957 | Newz (0, main_stack, 1, coro_stack); |
987 | _newprocess(args) |
958 | main_stack->idle_sp = (void *)-1; |
988 | SV * args |
959 | |
989 | PROTOTYPE: $ |
960 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
|
|
961 | } |
|
|
962 | |
|
|
963 | SV * |
|
|
964 | new (char *klass, ...) |
990 | CODE: |
965 | CODE: |
991 | Coro__State coro; |
966 | { |
|
|
967 | struct coro *coro; |
|
|
968 | HV *hv; |
|
|
969 | int i; |
992 | |
970 | |
993 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
|
|
994 | croak ("Coro::State::_newprocess expects an arrayref"); |
|
|
995 | |
|
|
996 | Newz (0, coro, 1, struct coro); |
971 | Newz (0, coro, 1, struct coro); |
|
|
972 | coro->args = newAV (); |
997 | |
973 | |
998 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
974 | hv = newHV (); |
|
|
975 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
|
|
976 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
977 | |
|
|
978 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
979 | for (i = 1; i < items; i++) |
|
|
980 | av_push (coro->args, newSVsv (ST (i))); |
|
|
981 | |
|
|
982 | coro->stack = main_stack; |
999 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
983 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1000 | /*coro->stack = 0;*/ |
984 | /*coro->stack = 0;*/ |
1001 | |
985 | } |
1002 | /* same as JMPENV_BOOTSTRAP */ |
|
|
1003 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
1004 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
|
|
1005 | coro->start_env.je_ret = -1; |
|
|
1006 | coro->start_env.je_mustcatch = TRUE; |
|
|
1007 | |
|
|
1008 | RETVAL = coro; |
|
|
1009 | OUTPUT: |
986 | OUTPUT: |
1010 | RETVAL |
987 | RETVAL |
1011 | |
988 | |
1012 | void |
989 | void |
1013 | transfer(prev, next, flags) |
990 | transfer (prev, next, flags) |
1014 | SV *prev |
991 | SV *prev |
1015 | SV *next |
992 | SV *next |
1016 | int flags |
993 | int flags |
1017 | PROTOTYPE: @ |
|
|
1018 | CODE: |
994 | CODE: |
1019 | PUTBACK; |
995 | PUTBACK; |
1020 | SV_CORO (next, "Coro::transfer"); |
|
|
1021 | SV_CORO (prev, "Coro::transfer"); |
|
|
1022 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
996 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
1023 | SPAGAIN; |
997 | SPAGAIN; |
1024 | |
998 | |
|
|
999 | int |
|
|
1000 | prio (Coro::State coro, int newprio = 0) |
|
|
1001 | ALIAS: |
|
|
1002 | nice = 1 |
|
|
1003 | CODE: |
|
|
1004 | { |
|
|
1005 | RETVAL = coro->prio; |
|
|
1006 | |
|
|
1007 | if (items > 1) |
|
|
1008 | { |
|
|
1009 | if (ix) |
|
|
1010 | newprio += coro->prio; |
|
|
1011 | |
|
|
1012 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1013 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1014 | |
|
|
1015 | coro->prio = newprio; |
|
|
1016 | } |
|
|
1017 | } |
|
|
1018 | |
1025 | void |
1019 | void |
1026 | DESTROY(coro) |
1020 | _clear_idle_sp (Coro::State self) |
1027 | Coro::State coro |
1021 | CODE: |
1028 | CODE: |
1022 | self->stack->idle_sp = 0; |
1029 | |
|
|
1030 | if (coro->mainstack && coro->mainstack != main_mainstack) |
|
|
1031 | { |
|
|
1032 | struct coro temp; |
|
|
1033 | |
|
|
1034 | PUTBACK; |
|
|
1035 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
|
|
1036 | LOAD (aTHX_ coro); |
|
|
1037 | SPAGAIN; |
|
|
1038 | |
|
|
1039 | destroy_stacks (aTHX); |
|
|
1040 | |
|
|
1041 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
|
|
1042 | SPAGAIN; |
|
|
1043 | |
|
|
1044 | coro->mainstack = 0; |
|
|
1045 | } |
|
|
1046 | |
|
|
1047 | deallocate_stack (coro); |
|
|
1048 | SvREFCNT_dec (coro->args); |
|
|
1049 | Safefree (coro); |
|
|
1050 | |
1023 | |
1051 | void |
1024 | void |
|
|
1025 | _clone_state_from (SV *dst, SV *src) |
|
|
1026 | CODE: |
|
|
1027 | { |
|
|
1028 | struct coro *coro_src = SvSTATE (src); |
|
|
1029 | |
|
|
1030 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1031 | |
|
|
1032 | ++coro_src->refcnt; |
|
|
1033 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | void |
1052 | _exit(code) |
1037 | _exit (code) |
1053 | int code |
1038 | int code |
1054 | PROTOTYPE: $ |
1039 | PROTOTYPE: $ |
1055 | CODE: |
1040 | CODE: |
1056 | _exit (code); |
1041 | _exit (code); |
1057 | |
1042 | |
1058 | MODULE = Coro::State PACKAGE = Coro::Cont |
1043 | MODULE = Coro::State PACKAGE = Coro::Cont |
1059 | |
1044 | |
1060 | # this is slightly dirty (should expose a c-level api) |
|
|
1061 | |
|
|
1062 | void |
1045 | void |
1063 | yield(...) |
1046 | yield (...) |
1064 | PROTOTYPE: @ |
1047 | PROTOTYPE: @ |
1065 | CODE: |
1048 | CODE: |
|
|
1049 | { |
1066 | SV *yieldstack; |
1050 | SV *yieldstack; |
1067 | SV *sv; |
1051 | SV *sv; |
1068 | AV *defav = GvAV (PL_defgv); |
1052 | AV *defav = GvAV (PL_defgv); |
1069 | struct coro *prev, *next; |
1053 | struct coro *prev, *next; |
1070 | |
1054 | |
… | |
… | |
1079 | av_fill (defav, items - 1); |
1063 | av_fill (defav, items - 1); |
1080 | while (items--) |
1064 | while (items--) |
1081 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1065 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1082 | |
1066 | |
1083 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1067 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1084 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1068 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
1085 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1069 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
1086 | SvREFCNT_dec (sv); |
1070 | SvREFCNT_dec (sv); |
1087 | |
1071 | |
1088 | transfer (aTHX_ prev, next, 0); |
1072 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1073 | } |
1089 | |
1074 | |
1090 | MODULE = Coro::State PACKAGE = Coro |
1075 | MODULE = Coro::State PACKAGE = Coro |
1091 | |
1076 | |
1092 | # this is slightly dirty (should expose a c-level api) |
|
|
1093 | |
|
|
1094 | BOOT: |
1077 | BOOT: |
1095 | { |
1078 | { |
1096 | int i; |
1079 | int i; |
|
|
1080 | |
1097 | HV *stash = gv_stashpv ("Coro", TRUE); |
1081 | coro_stash = gv_stashpv ("Coro", TRUE); |
1098 | |
1082 | |
1099 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1083 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1100 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1084 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1101 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1085 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1102 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1086 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1103 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1087 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1104 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1088 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1105 | |
1089 | |
1106 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1090 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1107 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1091 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1108 | |
1092 | |
1109 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1093 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
… | |
… | |
1117 | coroapi.ready = api_ready; |
1101 | coroapi.ready = api_ready; |
1118 | coroapi.nready = &coro_nready; |
1102 | coroapi.nready = &coro_nready; |
1119 | coroapi.current = coro_current; |
1103 | coroapi.current = coro_current; |
1120 | |
1104 | |
1121 | GCoroAPI = &coroapi; |
1105 | GCoroAPI = &coroapi; |
1122 | sv_setiv(sv, (IV)&coroapi); |
1106 | sv_setiv (sv, (IV)&coroapi); |
1123 | SvREADONLY_on(sv); |
1107 | SvREADONLY_on (sv); |
1124 | } |
1108 | } |
1125 | } |
1109 | } |
1126 | |
1110 | |
1127 | #if !PERL_MICRO |
|
|
1128 | |
|
|
1129 | void |
1111 | void |
1130 | ready(self) |
1112 | ready (SV *self) |
1131 | SV * self |
|
|
1132 | PROTOTYPE: $ |
1113 | PROTOTYPE: $ |
1133 | CODE: |
1114 | CODE: |
1134 | api_ready (self); |
1115 | api_ready (self); |
1135 | |
1116 | |
1136 | #endif |
|
|
1137 | |
|
|
1138 | int |
1117 | int |
1139 | nready(...) |
1118 | nready (...) |
1140 | PROTOTYPE: |
1119 | PROTOTYPE: |
1141 | CODE: |
1120 | CODE: |
1142 | RETVAL = coro_nready; |
1121 | RETVAL = coro_nready; |
1143 | OUTPUT: |
1122 | OUTPUT: |
1144 | RETVAL |
1123 | RETVAL |
1145 | |
1124 | |
1146 | void |
1125 | void |
1147 | schedule(...) |
1126 | schedule (...) |
1148 | PROTOTYPE: |
1127 | PROTOTYPE: |
1149 | CODE: |
1128 | CODE: |
1150 | api_schedule (); |
1129 | api_schedule (); |
1151 | |
1130 | |
1152 | void |
1131 | void |
|
|
1132 | _set_cede_self () |
|
|
1133 | CODE: |
|
|
1134 | coro_cede_self = 1; |
|
|
1135 | |
|
|
1136 | void |
1153 | cede(...) |
1137 | cede (...) |
1154 | PROTOTYPE: |
1138 | PROTOTYPE: |
1155 | CODE: |
1139 | CODE: |
1156 | api_cede (); |
1140 | api_cede (); |
1157 | |
1141 | |
1158 | # and these are hacks |
1142 | MODULE = Coro::State PACKAGE = Coro::AIO |
|
|
1143 | |
1159 | SV * |
1144 | SV * |
1160 | _aio_get_state () |
1145 | _get_state () |
1161 | CODE: |
1146 | CODE: |
1162 | { |
1147 | { |
1163 | struct { |
1148 | struct { |
1164 | int errorno; |
1149 | int errorno; |
1165 | int laststype; |
1150 | int laststype; |
… | |
… | |
1176 | } |
1161 | } |
1177 | OUTPUT: |
1162 | OUTPUT: |
1178 | RETVAL |
1163 | RETVAL |
1179 | |
1164 | |
1180 | void |
1165 | void |
1181 | _aio_set_state (char *data_) |
1166 | _set_state (char *data_) |
1182 | PROTOTYPE: $ |
1167 | PROTOTYPE: $ |
1183 | CODE: |
1168 | CODE: |
1184 | { |
1169 | { |
1185 | struct { |
1170 | struct { |
1186 | int errorno; |
1171 | int errorno; |