|
|
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" |
… | |
… | |
39 | # undef HAVE_MMAP |
41 | # undef HAVE_MMAP |
40 | # endif |
42 | # endif |
41 | # endif |
43 | # endif |
42 | #endif |
44 | #endif |
43 | |
45 | |
44 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
|
|
45 | |
|
|
46 | #define SUB_INIT "Coro::State::initialize" |
46 | #define SUB_INIT "Coro::State::initialize" |
47 | #define UCORO_STATE "_coro_state" |
47 | #define UCORO_STATE "_coro_state" |
48 | |
48 | |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
49 | /* 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 |
50 | * to the current C stack pointer. Its property is that it changes with each call |
… | |
… | |
55 | |
55 | |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
57 | |
57 | |
58 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
59 | |
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 | |
60 | 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 */ |
61 | |
75 | |
62 | /* 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... */ |
63 | typedef struct { |
77 | typedef struct { |
64 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
65 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
123 | }; |
137 | }; |
124 | |
138 | |
125 | typedef struct coro *Coro__State; |
139 | typedef struct coro *Coro__State; |
126 | typedef struct coro *Coro__State_or_hashref; |
140 | typedef struct coro *Coro__State_or_hashref; |
127 | |
141 | |
128 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
129 | static HV *coro_state_stash; |
|
|
130 | static SV *ucoro_state_sv; |
|
|
131 | static U32 ucoro_state_hash; |
|
|
132 | static HV *padlist_cache; |
|
|
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
134 | |
|
|
135 | /* mostly copied from op.c:cv_clone2 */ |
142 | /* mostly copied from op.c:cv_clone2 */ |
136 | STATIC AV * |
143 | STATIC AV * |
137 | clone_padlist (AV *protopadlist) |
144 | clone_padlist (pTHX_ AV *protopadlist) |
138 | { |
145 | { |
139 | AV *av; |
146 | AV *av; |
140 | I32 ix; |
147 | I32 ix; |
141 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
148 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
142 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
149 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
… | |
… | |
166 | AvFLAGS (av) = AVf_REIFY; |
173 | AvFLAGS (av) = AVf_REIFY; |
167 | |
174 | |
168 | for (ix = fpad; ix > 0; ix--) |
175 | for (ix = fpad; ix > 0; ix--) |
169 | { |
176 | { |
170 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
177 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
178 | |
171 | if (namesv && namesv != &PL_sv_undef) |
179 | if (namesv && namesv != &PL_sv_undef) |
172 | { |
180 | { |
173 | char *name = SvPVX (namesv); /* XXX */ |
181 | char *name = SvPVX (namesv); /* XXX */ |
|
|
182 | |
174 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
183 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
175 | { /* lexical from outside? */ |
184 | { /* lexical from outside? */ |
176 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
185 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
177 | } |
186 | } |
178 | else |
187 | else |
… | |
… | |
184 | sv = (SV *) newAV (); |
193 | sv = (SV *) newAV (); |
185 | else if (*name == '%') |
194 | else if (*name == '%') |
186 | sv = (SV *) newHV (); |
195 | sv = (SV *) newHV (); |
187 | else |
196 | else |
188 | sv = NEWSV (0, 0); |
197 | sv = NEWSV (0, 0); |
|
|
198 | |
189 | #ifdef SvPADBUSY |
199 | #ifdef SvPADBUSY |
190 | if (!SvPADBUSY (sv)) |
200 | if (!SvPADBUSY (sv)) |
191 | #endif |
201 | #endif |
192 | SvPADMY_on (sv); |
202 | SvPADMY_on (sv); |
|
|
203 | |
193 | npad[ix] = sv; |
204 | npad[ix] = sv; |
194 | } |
205 | } |
195 | } |
206 | } |
196 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
207 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
197 | { |
208 | { |
… | |
… | |
226 | #endif |
237 | #endif |
227 | |
238 | |
228 | return newpadlist; |
239 | return newpadlist; |
229 | } |
240 | } |
230 | |
241 | |
231 | #ifdef MAY_FLUSH |
|
|
232 | STATIC void |
242 | STATIC void |
233 | free_padlist (AV *padlist) |
243 | free_padlist (pTHX_ AV *padlist) |
234 | { |
244 | { |
235 | /* may be during global destruction */ |
245 | /* may be during global destruction */ |
236 | if (SvREFCNT(padlist)) |
246 | if (SvREFCNT (padlist)) |
237 | { |
247 | { |
238 | I32 i = AvFILLp(padlist); |
248 | I32 i = AvFILLp (padlist); |
239 | while (i >= 0) |
249 | while (i >= 0) |
240 | { |
250 | { |
241 | SV **svp = av_fetch(padlist, i--, FALSE); |
251 | SV **svp = av_fetch (padlist, i--, FALSE); |
242 | SV *sv = svp ? *svp : Nullsv; |
|
|
243 | if (sv) |
252 | if (svp) |
|
|
253 | { |
|
|
254 | SV *sv; |
|
|
255 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
244 | SvREFCNT_dec(sv); |
256 | SvREFCNT_dec (sv); |
|
|
257 | |
|
|
258 | SvREFCNT_dec (*svp); |
|
|
259 | } |
245 | } |
260 | } |
246 | |
261 | |
247 | SvREFCNT_dec((SV*)padlist); |
262 | SvREFCNT_dec ((SV*)padlist); |
248 | } |
263 | } |
249 | } |
264 | } |
250 | #endif |
265 | |
|
|
266 | STATIC int |
|
|
267 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
268 | { |
|
|
269 | AV *padlist; |
|
|
270 | AV *av = (AV *)mg->mg_obj; |
|
|
271 | |
|
|
272 | /* casting is fun. */ |
|
|
273 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
274 | free_padlist (aTHX_ padlist); |
|
|
275 | |
|
|
276 | SvREFCNT_dec (av); |
|
|
277 | } |
|
|
278 | |
|
|
279 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
|
|
280 | |
|
|
281 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
251 | |
282 | |
252 | /* the next two functions merely cache the padlists */ |
283 | /* the next two functions merely cache the padlists */ |
253 | STATIC void |
284 | STATIC void |
254 | get_padlist (CV *cv) |
285 | get_padlist (pTHX_ CV *cv) |
255 | { |
286 | { |
256 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
287 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
257 | |
288 | |
258 | if (he && AvFILLp ((AV *)*he) >= 0) |
289 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
259 | CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
290 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
260 | else |
291 | else |
261 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
292 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
262 | } |
293 | } |
263 | |
294 | |
264 | STATIC void |
295 | STATIC void |
265 | put_padlist (CV *cv) |
296 | put_padlist (pTHX_ CV *cv) |
266 | { |
297 | { |
267 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); |
298 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
268 | |
299 | |
269 | if (SvTYPE (*he) != SVt_PVAV) |
300 | if (!mg) |
270 | { |
301 | { |
271 | SvREFCNT_dec (*he); |
302 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
303 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
304 | mg->mg_virtual = &vtbl_coro; |
272 | *he = (SV *)newAV (); |
305 | mg->mg_obj = (SV *)newAV (); |
273 | } |
306 | } |
274 | |
307 | |
275 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
308 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
276 | } |
309 | } |
277 | |
|
|
278 | #ifdef MAY_FLUSH |
|
|
279 | STATIC void |
|
|
280 | flush_padlist_cache () |
|
|
281 | { |
|
|
282 | HV *hv = padlist_cache; |
|
|
283 | padlist_cache = newHV (); |
|
|
284 | |
|
|
285 | if (hv_iterinit (hv)) |
|
|
286 | { |
|
|
287 | HE *he; |
|
|
288 | AV *padlist; |
|
|
289 | |
|
|
290 | while (!!(he = hv_iternext (hv))) |
|
|
291 | { |
|
|
292 | AV *av = (AV *)HeVAL(he); |
|
|
293 | |
|
|
294 | /* casting is fun. */ |
|
|
295 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
296 | free_padlist (padlist); |
|
|
297 | } |
|
|
298 | } |
|
|
299 | |
|
|
300 | SvREFCNT_dec (hv); |
|
|
301 | } |
|
|
302 | #endif |
|
|
303 | |
310 | |
304 | #define SB do { |
311 | #define SB do { |
305 | #define SE } while (0) |
312 | #define SE } while (0) |
306 | |
313 | |
307 | #define LOAD(state) load_state(aTHX_ (state)); |
314 | #define LOAD(state) load_state(aTHX_ (state)); |
… | |
… | |
356 | { |
363 | { |
357 | AV *padlist = (AV *)POPs; |
364 | AV *padlist = (AV *)POPs; |
358 | |
365 | |
359 | if (padlist) |
366 | if (padlist) |
360 | { |
367 | { |
361 | put_padlist (cv); /* mark this padlist as available */ |
368 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
362 | CvPADLIST(cv) = padlist; |
369 | CvPADLIST(cv) = padlist; |
363 | #ifdef USE_THREADS |
|
|
364 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
365 | #endif |
|
|
366 | } |
370 | } |
367 | |
371 | |
368 | ++CvDEPTH(cv); |
372 | ++CvDEPTH(cv); |
369 | } |
373 | } |
370 | |
374 | |
… | |
… | |
397 | if (CxTYPE(cx) == CXt_SUB) |
401 | if (CxTYPE(cx) == CXt_SUB) |
398 | { |
402 | { |
399 | CV *cv = cx->blk_sub.cv; |
403 | CV *cv = cx->blk_sub.cv; |
400 | if (CvDEPTH(cv)) |
404 | if (CvDEPTH(cv)) |
401 | { |
405 | { |
402 | #ifdef USE_THREADS |
|
|
403 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
404 | /*CvOWNER(cv) = 0;*/ |
|
|
405 | /*error must unlock this cv etc.. etc...*/ |
|
|
406 | #endif |
|
|
407 | EXTEND (SP, CvDEPTH(cv)*2); |
406 | EXTEND (SP, CvDEPTH(cv)*2); |
408 | |
407 | |
409 | while (--CvDEPTH(cv)) |
408 | while (--CvDEPTH(cv)) |
410 | { |
409 | { |
411 | /* this tells the restore code to increment CvDEPTH */ |
410 | /* this tells the restore code to increment CvDEPTH */ |
… | |
… | |
414 | } |
413 | } |
415 | |
414 | |
416 | PUSHs ((SV *)CvPADLIST(cv)); |
415 | PUSHs ((SV *)CvPADLIST(cv)); |
417 | PUSHs ((SV *)cv); |
416 | PUSHs ((SV *)cv); |
418 | |
417 | |
419 | get_padlist (cv); /* this is a monster */ |
418 | get_padlist (aTHX_ cv); /* this is a monster */ |
420 | } |
419 | } |
421 | } |
420 | } |
422 | #ifdef CXt_FORMAT |
421 | #ifdef CXt_FORMAT |
423 | else if (CxTYPE(cx) == CXt_FORMAT) |
422 | else if (CxTYPE(cx) == CXt_FORMAT) |
424 | { |
423 | { |
… | |
… | |
477 | } |
476 | } |
478 | |
477 | |
479 | /* |
478 | /* |
480 | * allocate various perl stacks. This is an exact copy |
479 | * allocate various perl stacks. This is an exact copy |
481 | * of perl.c:init_stacks, except that it uses less memory |
480 | * of perl.c:init_stacks, except that it uses less memory |
482 | * on the assumption that coroutines do not usually need |
481 | * on the (sometimes correct) assumption that coroutines do |
483 | * a lot of stackspace. |
482 | * not usually need a lot of stackspace. |
484 | */ |
483 | */ |
485 | STATIC void |
484 | STATIC void |
486 | coro_init_stacks (pTHX) |
485 | coro_init_stacks (pTHX) |
487 | { |
486 | { |
|
|
487 | LOCK; |
|
|
488 | |
488 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
489 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
489 | PL_curstackinfo->si_type = PERLSI_MAIN; |
490 | PL_curstackinfo->si_type = PERLSI_MAIN; |
490 | PL_curstack = PL_curstackinfo->si_stack; |
491 | PL_curstack = PL_curstackinfo->si_stack; |
491 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
492 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
492 | |
493 | |
… | |
… | |
516 | PL_savestack_max = 96; |
517 | PL_savestack_max = 96; |
517 | |
518 | |
518 | New(54,PL_retstack,8,OP*); |
519 | New(54,PL_retstack,8,OP*); |
519 | PL_retstack_ix = 0; |
520 | PL_retstack_ix = 0; |
520 | PL_retstack_max = 8; |
521 | PL_retstack_max = 8; |
|
|
522 | |
|
|
523 | UNLOCK; |
521 | } |
524 | } |
522 | |
525 | |
523 | /* |
526 | /* |
524 | * destroy the stacks, the callchain etc... |
527 | * destroy the stacks, the callchain etc... |
525 | */ |
528 | */ |
… | |
… | |
527 | destroy_stacks(pTHX) |
530 | destroy_stacks(pTHX) |
528 | { |
531 | { |
529 | if (!IN_DESTRUCT) |
532 | if (!IN_DESTRUCT) |
530 | { |
533 | { |
531 | /* is this ugly, I ask? */ |
534 | /* is this ugly, I ask? */ |
532 | while (PL_scopestack_ix) |
535 | LEAVE_SCOPE (0); |
533 | LEAVE; |
|
|
534 | |
536 | |
535 | /* sure it is, but more important: is it correct?? :/ */ |
537 | /* sure it is, but more important: is it correct?? :/ */ |
536 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
|
|
537 | FREETMPS; |
538 | FREETMPS; |
|
|
539 | |
|
|
540 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
538 | } |
541 | } |
539 | |
542 | |
540 | while (PL_curstackinfo->si_next) |
543 | while (PL_curstackinfo->si_next) |
541 | PL_curstackinfo = PL_curstackinfo->si_next; |
544 | PL_curstackinfo = PL_curstackinfo->si_next; |
542 | |
545 | |
543 | while (PL_curstackinfo) |
546 | while (PL_curstackinfo) |
544 | { |
547 | { |
545 | PERL_SI *p = PL_curstackinfo->si_prev; |
548 | PERL_SI *p = PL_curstackinfo->si_prev; |
546 | |
549 | |
547 | { |
550 | { /*D*//*remove*/ |
548 | dSP; |
551 | dSP; |
549 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
552 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
550 | PUTBACK; /* possibly superfluous */ |
553 | PUTBACK; /* possibly superfluous */ |
551 | } |
554 | } |
552 | |
555 | |
553 | if (!IN_DESTRUCT) |
556 | if (!IN_DESTRUCT) |
554 | { |
557 | { |
555 | dounwind(-1); |
558 | dounwind (-1);/*D*//*remove*/ |
556 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
559 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
557 | } |
560 | } |
558 | |
561 | |
559 | Safefree(PL_curstackinfo->si_cxstack); |
562 | Safefree (PL_curstackinfo->si_cxstack); |
560 | Safefree(PL_curstackinfo); |
563 | Safefree (PL_curstackinfo); |
561 | PL_curstackinfo = p; |
564 | PL_curstackinfo = p; |
562 | } |
565 | } |
563 | |
566 | |
564 | Safefree(PL_tmps_stack); |
567 | Safefree (PL_tmps_stack); |
565 | Safefree(PL_markstack); |
568 | Safefree (PL_markstack); |
566 | Safefree(PL_scopestack); |
569 | Safefree (PL_scopestack); |
567 | Safefree(PL_savestack); |
570 | Safefree (PL_savestack); |
568 | Safefree(PL_retstack); |
571 | Safefree (PL_retstack); |
569 | } |
572 | } |
570 | |
573 | |
571 | static void |
574 | static void |
572 | allocate_stack (Coro__State ctx, int alloc) |
575 | allocate_stack (Coro__State ctx, int alloc) |
573 | { |
576 | { |
… | |
… | |
576 | New (0, stack, 1, coro_stack); |
579 | New (0, stack, 1, coro_stack); |
577 | |
580 | |
578 | stack->refcnt = 1; |
581 | stack->refcnt = 1; |
579 | stack->usecnt = 1; |
582 | stack->usecnt = 1; |
580 | stack->gencnt = ctx->gencnt = 0; |
583 | stack->gencnt = ctx->gencnt = 0; |
|
|
584 | |
581 | if (alloc) |
585 | if (alloc) |
582 | { |
586 | { |
583 | #if HAVE_MMAP |
587 | #if HAVE_MMAP |
584 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
588 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
585 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
589 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
586 | if (stack->sptr == (void *)-1) |
590 | if (stack->sptr == (void *)-1) |
587 | #endif |
591 | #endif |
588 | { |
592 | { |
589 | /*FIXME*//*D*//* reasonable stack size! */ |
593 | /*FIXME*//*D*//* reasonable stack size! */ |
590 | stack->ssize = -4096 * sizeof (long); |
594 | stack->ssize = - (8192 * sizeof (long)); |
591 | New (0, stack->sptr, 4096, long); |
595 | New (0, stack->sptr, 8192, long); |
592 | } |
596 | } |
593 | } |
597 | } |
594 | else |
598 | else |
595 | stack->sptr = 0; |
599 | stack->sptr = 0; |
596 | |
600 | |
… | |
… | |
626 | setup_coro (void *arg) |
630 | setup_coro (void *arg) |
627 | { |
631 | { |
628 | /* |
632 | /* |
629 | * emulate part of the perl startup here. |
633 | * emulate part of the perl startup here. |
630 | */ |
634 | */ |
|
|
635 | dTHX; |
631 | dSP; |
636 | dSP; |
632 | Coro__State ctx = (Coro__State)arg; |
637 | Coro__State ctx = (Coro__State)arg; |
633 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
638 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
634 | |
639 | |
635 | coro_init_stacks (aTHX); |
640 | coro_init_stacks (aTHX); |
636 | /*PL_curcop = 0;*/ |
641 | /*PL_curcop = 0;*/ |
637 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
642 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
638 | SvREFCNT_dec (GvAV (PL_defgv)); |
643 | SvREFCNT_dec (GvAV (PL_defgv)); |
… | |
… | |
684 | continue_coro (void *arg) |
689 | continue_coro (void *arg) |
685 | { |
690 | { |
686 | /* |
691 | /* |
687 | * this is a _very_ stripped down perl interpreter ;) |
692 | * this is a _very_ stripped down perl interpreter ;) |
688 | */ |
693 | */ |
|
|
694 | dTHX; |
689 | Coro__State ctx = (Coro__State)arg; |
695 | Coro__State ctx = (Coro__State)arg; |
690 | JMPENV coro_start_env; |
696 | JMPENV coro_start_env; |
691 | |
697 | |
692 | PL_top_env = &ctx->start_env; |
698 | PL_top_env = &ctx->start_env; |
693 | |
699 | |
… | |
… | |
697 | |
703 | |
698 | abort (); |
704 | abort (); |
699 | } |
705 | } |
700 | |
706 | |
701 | STATIC void |
707 | STATIC void |
702 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
708 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
703 | { |
709 | { |
704 | dSTACKLEVEL; |
710 | dSTACKLEVEL; |
705 | static struct coro *xnext; |
|
|
706 | |
711 | |
707 | if (prev != next) |
712 | if (prev != next) |
708 | { |
713 | { |
709 | xnext = next; |
|
|
710 | |
|
|
711 | if (next->mainstack) |
714 | if (next->mainstack) |
712 | { |
715 | { |
|
|
716 | LOCK; |
713 | SAVE (prev, flags); |
717 | SAVE (prev, flags); |
714 | LOAD (next); |
718 | LOAD (next); |
|
|
719 | UNLOCK; |
715 | |
720 | |
716 | /* mark this state as in-use */ |
721 | /* mark this state as in-use */ |
717 | next->mainstack = 0; |
722 | next->mainstack = 0; |
718 | next->tmps_ix = -2; |
723 | next->tmps_ix = -2; |
719 | |
724 | |
… | |
… | |
738 | continue_coro, (void *)next, |
743 | continue_coro, (void *)next, |
739 | next->stack->sptr, labs (next->stack->ssize)); |
744 | next->stack->sptr, labs (next->stack->ssize)); |
740 | } |
745 | } |
741 | |
746 | |
742 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
747 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
748 | prev->cursp = stacklevel; |
743 | /* don't add any code here */ |
749 | /* don't add any code here */ |
744 | } |
750 | } |
745 | |
751 | else |
|
|
752 | next->cursp = stacklevel; |
746 | } |
753 | } |
747 | else if (next->tmps_ix == -2) |
754 | else if (next->tmps_ix == -2) |
748 | croak ("tried to transfer to running coroutine"); |
755 | croak ("tried to transfer to running coroutine"); |
749 | else |
756 | else |
750 | { |
757 | { |
|
|
758 | LOCK; |
751 | SAVE (prev, -1); /* first get rid of the old state */ |
759 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
760 | UNLOCK; |
752 | |
761 | |
753 | if (flags & TRANSFER_SAVE_CCTXT) |
762 | if (flags & TRANSFER_SAVE_CCTXT) |
754 | { |
763 | { |
755 | if (!prev->stack) |
764 | if (!prev->stack) |
756 | allocate_stack (prev, 0); |
765 | allocate_stack (prev, 0); |
… | |
… | |
758 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
767 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
759 | { |
768 | { |
760 | PL_top_env = &next->start_env; |
769 | PL_top_env = &next->start_env; |
761 | |
770 | |
762 | setup_coro (next); |
771 | setup_coro (next); |
|
|
772 | next->cursp = stacklevel; |
763 | |
773 | |
764 | prev->stack->refcnt++; |
774 | prev->stack->refcnt++; |
765 | prev->stack->usecnt++; |
775 | prev->stack->usecnt++; |
766 | next->stack = prev->stack; |
776 | next->stack = prev->stack; |
767 | next->gencnt = prev->gencnt; |
777 | next->gencnt = prev->gencnt; |
… | |
… | |
772 | allocate_stack (next, 1); |
782 | allocate_stack (next, 1); |
773 | coro_create (&(next->stack->cctx), |
783 | coro_create (&(next->stack->cctx), |
774 | setup_coro, (void *)next, |
784 | setup_coro, (void *)next, |
775 | next->stack->sptr, labs (next->stack->ssize)); |
785 | next->stack->sptr, labs (next->stack->ssize)); |
776 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
786 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
787 | prev->cursp = stacklevel; |
777 | /* don't add any code here */ |
788 | /* don't add any code here */ |
778 | } |
789 | } |
779 | } |
790 | } |
780 | else |
791 | else |
|
|
792 | { |
781 | setup_coro (next); |
793 | setup_coro (next); |
|
|
794 | next->cursp = stacklevel; |
|
|
795 | } |
782 | } |
796 | } |
783 | |
|
|
784 | /* |
|
|
785 | * xnext is now either prev or next, depending on wether |
|
|
786 | * we switched the c stack or not. that's why I use a global |
|
|
787 | * variable, that should become thread-specific at one point. |
|
|
788 | */ |
|
|
789 | xnext->cursp = stacklevel; |
|
|
790 | } |
797 | } |
791 | |
798 | |
|
|
799 | LOCK; |
792 | if (coro_mortal) |
800 | if (coro_mortal) |
793 | { |
801 | { |
794 | SvREFCNT_dec (coro_mortal); |
802 | SvREFCNT_dec (coro_mortal); |
795 | coro_mortal = 0; |
803 | coro_mortal = 0; |
796 | } |
804 | } |
|
|
805 | UNLOCK; |
797 | } |
806 | } |
798 | |
807 | |
799 | #define SV_CORO(sv,func) \ |
808 | #define SV_CORO(sv,func) \ |
800 | do { \ |
809 | do { \ |
801 | if (SvROK (sv)) \ |
810 | if (SvROK (sv)) \ |
802 | sv = SvRV (sv); \ |
811 | sv = SvRV (sv); \ |
803 | \ |
812 | \ |
804 | if (SvTYPE(sv) == SVt_PVHV) \ |
813 | if (SvTYPE (sv) == SVt_PVHV) \ |
805 | { \ |
814 | { \ |
806 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
815 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
807 | \ |
816 | \ |
808 | if (!he) \ |
817 | if (!he) \ |
809 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
818 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
810 | \ |
819 | \ |
811 | (sv) = SvRV (HeVAL(he)); \ |
820 | (sv) = SvRV (HeVAL(he)); \ |
812 | } \ |
821 | } \ |
813 | \ |
822 | \ |
814 | /* must also be changed inside Coro::Cont::yield */ \ |
823 | /* must also be changed inside Coro::Cont::yield */ \ |
815 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
824 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
816 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
825 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
817 | \ |
826 | \ |
818 | } while(0) |
827 | } while(0) |
819 | |
828 | |
820 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
829 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
… | |
… | |
823 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
832 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
824 | { |
833 | { |
825 | SV_CORO (prev, "Coro::transfer"); |
834 | SV_CORO (prev, "Coro::transfer"); |
826 | SV_CORO (next, "Coro::transfer"); |
835 | SV_CORO (next, "Coro::transfer"); |
827 | |
836 | |
828 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
837 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
829 | } |
838 | } |
830 | |
839 | |
831 | /** Coro ********************************************************************/ |
840 | /** Coro ********************************************************************/ |
832 | |
841 | |
833 | #define PRIO_MAX 3 |
842 | #define PRIO_MAX 3 |
… | |
… | |
841 | static GV *coro_current, *coro_idle; |
850 | static GV *coro_current, *coro_idle; |
842 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
851 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
843 | static int coro_nready; |
852 | static int coro_nready; |
844 | |
853 | |
845 | static void |
854 | static void |
846 | coro_enq (SV *sv) |
855 | coro_enq (pTHX_ SV *sv) |
847 | { |
856 | { |
848 | if (SvTYPE (sv) == SVt_PVHV) |
857 | if (SvTYPE (sv) == SVt_PVHV) |
849 | { |
858 | { |
850 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
859 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
851 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
860 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
… | |
… | |
862 | |
871 | |
863 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
872 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
864 | } |
873 | } |
865 | |
874 | |
866 | static SV * |
875 | static SV * |
867 | coro_deq (int min_prio) |
876 | coro_deq (pTHX_ int min_prio) |
868 | { |
877 | { |
869 | int prio = PRIO_MAX - PRIO_MIN; |
878 | int prio = PRIO_MAX - PRIO_MIN; |
870 | |
879 | |
871 | min_prio -= PRIO_MIN; |
880 | min_prio -= PRIO_MIN; |
872 | if (min_prio < 0) |
881 | if (min_prio < 0) |
… | |
… | |
883 | } |
892 | } |
884 | |
893 | |
885 | static void |
894 | static void |
886 | api_ready (SV *coro) |
895 | api_ready (SV *coro) |
887 | { |
896 | { |
|
|
897 | dTHX; |
|
|
898 | |
888 | if (SvROK (coro)) |
899 | if (SvROK (coro)) |
889 | coro = SvRV (coro); |
900 | coro = SvRV (coro); |
890 | |
901 | |
|
|
902 | LOCK; |
891 | coro_enq (SvREFCNT_inc (coro)); |
903 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
|
|
904 | UNLOCK; |
892 | } |
905 | } |
893 | |
906 | |
894 | static void |
907 | static void |
895 | api_schedule (void) |
908 | api_schedule (void) |
896 | { |
909 | { |
|
|
910 | dTHX; |
|
|
911 | |
897 | SV *prev, *next; |
912 | SV *prev, *next; |
898 | |
913 | |
|
|
914 | LOCK; |
|
|
915 | |
899 | prev = SvRV (GvSV (coro_current)); |
916 | prev = SvRV (GvSV (coro_current)); |
900 | next = coro_deq (PRIO_MIN); |
917 | next = coro_deq (aTHX_ PRIO_MIN); |
901 | |
918 | |
902 | if (!next) |
919 | if (!next) |
903 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
920 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
904 | |
921 | |
905 | /* free this only after the transfer */ |
922 | /* free this only after the transfer */ |
… | |
… | |
908 | |
925 | |
909 | SvRV (GvSV (coro_current)) = next; |
926 | SvRV (GvSV (coro_current)) = next; |
910 | |
927 | |
911 | SV_CORO (next, "Coro::schedule"); |
928 | SV_CORO (next, "Coro::schedule"); |
912 | |
929 | |
|
|
930 | UNLOCK; |
|
|
931 | |
913 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
932 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
914 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
933 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
915 | } |
934 | } |
916 | |
935 | |
917 | static void |
936 | static void |
918 | api_cede (void) |
937 | api_cede (void) |
919 | { |
938 | { |
|
|
939 | dTHX; |
|
|
940 | |
|
|
941 | LOCK; |
920 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
942 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
943 | UNLOCK; |
921 | |
944 | |
922 | api_schedule (); |
945 | api_schedule (); |
923 | } |
946 | } |
924 | |
947 | |
925 | MODULE = Coro::State PACKAGE = Coro::State |
948 | MODULE = Coro::State PACKAGE = Coro::State |
926 | |
949 | |
927 | PROTOTYPES: ENABLE |
950 | PROTOTYPES: ENABLE |
928 | |
951 | |
929 | BOOT: |
952 | BOOT: |
930 | { /* {} necessary for stoopid perl-5.6.x */ |
953 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
954 | #ifdef USE_ITHREADS |
|
|
955 | MUTEX_INIT (&coro_mutex); |
|
|
956 | #endif |
|
|
957 | |
931 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
958 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
932 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
959 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
933 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
960 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
934 | |
961 | |
935 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
962 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
936 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
963 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
937 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
964 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
938 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
965 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
939 | |
|
|
940 | if (!padlist_cache) |
|
|
941 | padlist_cache = newHV (); |
|
|
942 | |
966 | |
943 | main_mainstack = PL_mainstack; |
967 | main_mainstack = PL_mainstack; |
944 | |
968 | |
945 | coroapi.ver = CORO_API_VERSION; |
969 | coroapi.ver = CORO_API_VERSION; |
946 | coroapi.transfer = api_transfer; |
970 | coroapi.transfer = api_transfer; |
… | |
… | |
957 | croak ("Coro::State::_newprocess expects an arrayref"); |
981 | croak ("Coro::State::_newprocess expects an arrayref"); |
958 | |
982 | |
959 | Newz (0, coro, 1, struct coro); |
983 | Newz (0, coro, 1, struct coro); |
960 | |
984 | |
961 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
985 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
962 | coro->mainstack = 0; /* actual work is done inside transfer */ |
986 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
963 | coro->stack = 0; |
987 | /*coro->stack = 0;*/ |
964 | |
988 | |
965 | /* same as JMPENV_BOOTSTRAP */ |
989 | /* same as JMPENV_BOOTSTRAP */ |
966 | /* we might be able to recycle start_env, but safe is safe */ |
990 | /* we might be able to recycle start_env, but safe is safe */ |
967 | //Zero(&coro->start_env, 1, JMPENV); |
991 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
968 | coro->start_env.je_ret = -1; |
992 | coro->start_env.je_ret = -1; |
969 | coro->start_env.je_mustcatch = TRUE; |
993 | coro->start_env.je_mustcatch = TRUE; |
970 | |
994 | |
971 | RETVAL = coro; |
995 | RETVAL = coro; |
972 | OUTPUT: |
996 | OUTPUT: |
… | |
… | |
993 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1017 | if (coro->mainstack && coro->mainstack != main_mainstack) |
994 | { |
1018 | { |
995 | struct coro temp; |
1019 | struct coro temp; |
996 | |
1020 | |
997 | PUTBACK; |
1021 | PUTBACK; |
998 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1022 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
999 | LOAD(aTHX_ coro); |
1023 | LOAD (aTHX_ coro); |
1000 | SPAGAIN; |
1024 | SPAGAIN; |
1001 | |
1025 | |
1002 | destroy_stacks (aTHX); |
1026 | destroy_stacks (aTHX); |
1003 | |
1027 | |
1004 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1028 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1005 | SPAGAIN; |
1029 | SPAGAIN; |
1006 | |
1030 | |
1007 | coro->mainstack = 0; |
1031 | coro->mainstack = 0; |
1008 | } |
1032 | } |
1009 | |
1033 | |
1010 | deallocate_stack (coro); |
1034 | deallocate_stack (coro); |
1011 | SvREFCNT_dec (coro->args); |
1035 | SvREFCNT_dec (coro->args); |
1012 | Safefree (coro); |
1036 | Safefree (coro); |
1013 | |
|
|
1014 | void |
|
|
1015 | flush() |
|
|
1016 | CODE: |
|
|
1017 | #ifdef MAY_FLUSH |
|
|
1018 | flush_padlist_cache (); |
|
|
1019 | #endif |
|
|
1020 | |
1037 | |
1021 | void |
1038 | void |
1022 | _exit(code) |
1039 | _exit(code) |
1023 | int code |
1040 | int code |
1024 | PROTOTYPE: $ |
1041 | PROTOTYPE: $ |
1025 | CODE: |
1042 | CODE: |
1026 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
1027 | _exit (code); |
1043 | _exit (code); |
1028 | #else |
|
|
1029 | signal (SIGTERM, SIG_DFL); |
|
|
1030 | raise (SIGTERM); |
|
|
1031 | exit (code); |
|
|
1032 | #endif |
|
|
1033 | |
1044 | |
1034 | MODULE = Coro::State PACKAGE = Coro::Cont |
1045 | MODULE = Coro::State PACKAGE = Coro::Cont |
1035 | |
1046 | |
1036 | # this is slightly dirty (should expose a c-level api) |
1047 | # this is slightly dirty (should expose a c-level api) |
1037 | |
1048 | |
… | |
… | |
1051 | av_clear (defav); |
1062 | av_clear (defav); |
1052 | av_fill (defav, items - 1); |
1063 | av_fill (defav, items - 1); |
1053 | while (items--) |
1064 | while (items--) |
1054 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1065 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1055 | |
1066 | |
1056 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
1067 | SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */ |
1057 | sv = av_pop ((AV *)SvRV (returnstk)); |
1068 | sv = av_pop ((AV *)SvRV (returnstk)); |
1058 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1069 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1059 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1070 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1060 | SvREFCNT_dec (sv); |
1071 | SvREFCNT_dec (sv); |
1061 | |
1072 | |
1062 | transfer(aTHX_ prev, next, 0); |
1073 | transfer (aTHX_ prev, next, 0); |
1063 | |
1074 | |
1064 | MODULE = Coro::State PACKAGE = Coro |
1075 | MODULE = Coro::State PACKAGE = Coro |
1065 | |
1076 | |
1066 | # this is slightly dirty (should expose a c-level api) |
1077 | # this is slightly dirty (should expose a c-level api) |
1067 | |
1078 | |
… | |
… | |
1096 | sv_setiv(sv, (IV)&coroapi); |
1107 | sv_setiv(sv, (IV)&coroapi); |
1097 | SvREADONLY_on(sv); |
1108 | SvREADONLY_on(sv); |
1098 | } |
1109 | } |
1099 | } |
1110 | } |
1100 | |
1111 | |
|
|
1112 | #if !PERL_MICRO |
|
|
1113 | |
1101 | void |
1114 | void |
1102 | ready(self) |
1115 | ready(self) |
1103 | SV * self |
1116 | SV * self |
1104 | PROTOTYPE: $ |
1117 | PROTOTYPE: $ |
1105 | CODE: |
1118 | CODE: |
1106 | api_ready (self); |
1119 | api_ready (self); |
|
|
1120 | |
|
|
1121 | #endif |
1107 | |
1122 | |
1108 | int |
1123 | int |
1109 | nready(...) |
1124 | nready(...) |
1110 | PROTOTYPE: |
1125 | PROTOTYPE: |
1111 | CODE: |
1126 | CODE: |