|
|
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 | |
|
|
199 | #ifdef SvPADBUSY |
189 | if (!SvPADBUSY (sv)) |
200 | if (!SvPADBUSY (sv)) |
|
|
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 | { |
… | |
… | |
224 | #endif |
237 | #endif |
225 | |
238 | |
226 | return newpadlist; |
239 | return newpadlist; |
227 | } |
240 | } |
228 | |
241 | |
229 | #ifdef MAY_FLUSH |
|
|
230 | STATIC void |
242 | STATIC void |
231 | free_padlist (AV *padlist) |
243 | free_padlist (pTHX_ AV *padlist) |
232 | { |
244 | { |
233 | /* may be during global destruction */ |
245 | /* may be during global destruction */ |
234 | if (SvREFCNT(padlist)) |
246 | if (SvREFCNT (padlist)) |
235 | { |
247 | { |
236 | I32 i = AvFILLp(padlist); |
248 | I32 i = AvFILLp (padlist); |
237 | while (i >= 0) |
249 | while (i >= 0) |
238 | { |
250 | { |
239 | SV **svp = av_fetch(padlist, i--, FALSE); |
251 | SV **svp = av_fetch (padlist, i--, FALSE); |
240 | SV *sv = svp ? *svp : Nullsv; |
|
|
241 | if (sv) |
252 | if (svp) |
|
|
253 | { |
|
|
254 | SV *sv; |
|
|
255 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
242 | SvREFCNT_dec(sv); |
256 | SvREFCNT_dec (sv); |
|
|
257 | |
|
|
258 | SvREFCNT_dec (*svp); |
|
|
259 | } |
243 | } |
260 | } |
244 | |
261 | |
245 | SvREFCNT_dec((SV*)padlist); |
262 | SvREFCNT_dec ((SV*)padlist); |
246 | } |
263 | } |
247 | } |
264 | } |
248 | #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 | |
|
|
277 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
|
|
278 | |
|
|
279 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
249 | |
280 | |
250 | /* the next two functions merely cache the padlists */ |
281 | /* the next two functions merely cache the padlists */ |
251 | STATIC void |
282 | STATIC void |
252 | get_padlist (CV *cv) |
283 | get_padlist (pTHX_ CV *cv) |
253 | { |
284 | { |
254 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
285 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
255 | |
286 | |
256 | if (he && AvFILLp ((AV *)*he) >= 0) |
287 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
257 | CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
288 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
258 | else |
289 | else |
259 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
290 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
260 | } |
291 | } |
261 | |
292 | |
262 | STATIC void |
293 | STATIC void |
263 | put_padlist (CV *cv) |
294 | put_padlist (pTHX_ CV *cv) |
264 | { |
295 | { |
265 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); |
296 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
266 | |
297 | |
267 | if (SvTYPE (*he) != SVt_PVAV) |
298 | if (!mg) |
268 | { |
299 | { |
269 | SvREFCNT_dec (*he); |
300 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
301 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
302 | mg->mg_virtual = &vtbl_coro; |
270 | *he = (SV *)newAV (); |
303 | mg->mg_obj = (SV *)newAV (); |
271 | } |
304 | } |
272 | |
305 | |
273 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
306 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
274 | } |
307 | } |
275 | |
|
|
276 | #ifdef MAY_FLUSH |
|
|
277 | STATIC void |
|
|
278 | flush_padlist_cache () |
|
|
279 | { |
|
|
280 | HV *hv = padlist_cache; |
|
|
281 | padlist_cache = newHV (); |
|
|
282 | |
|
|
283 | if (hv_iterinit (hv)) |
|
|
284 | { |
|
|
285 | HE *he; |
|
|
286 | AV *padlist; |
|
|
287 | |
|
|
288 | while (!!(he = hv_iternext (hv))) |
|
|
289 | { |
|
|
290 | AV *av = (AV *)HeVAL(he); |
|
|
291 | |
|
|
292 | /* casting is fun. */ |
|
|
293 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
294 | free_padlist (padlist); |
|
|
295 | } |
|
|
296 | } |
|
|
297 | |
|
|
298 | SvREFCNT_dec (hv); |
|
|
299 | } |
|
|
300 | #endif |
|
|
301 | |
308 | |
302 | #define SB do { |
309 | #define SB do { |
303 | #define SE } while (0) |
310 | #define SE } while (0) |
304 | |
311 | |
305 | #define LOAD(state) load_state(aTHX_ (state)); |
312 | #define LOAD(state) load_state(aTHX_ (state)); |
… | |
… | |
354 | { |
361 | { |
355 | AV *padlist = (AV *)POPs; |
362 | AV *padlist = (AV *)POPs; |
356 | |
363 | |
357 | if (padlist) |
364 | if (padlist) |
358 | { |
365 | { |
359 | put_padlist (cv); /* mark this padlist as available */ |
366 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
360 | CvPADLIST(cv) = padlist; |
367 | CvPADLIST(cv) = padlist; |
361 | #ifdef USE_THREADS |
|
|
362 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
363 | #endif |
|
|
364 | } |
368 | } |
365 | |
369 | |
366 | ++CvDEPTH(cv); |
370 | ++CvDEPTH(cv); |
367 | } |
371 | } |
368 | |
372 | |
… | |
… | |
395 | if (CxTYPE(cx) == CXt_SUB) |
399 | if (CxTYPE(cx) == CXt_SUB) |
396 | { |
400 | { |
397 | CV *cv = cx->blk_sub.cv; |
401 | CV *cv = cx->blk_sub.cv; |
398 | if (CvDEPTH(cv)) |
402 | if (CvDEPTH(cv)) |
399 | { |
403 | { |
400 | #ifdef USE_THREADS |
|
|
401 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
402 | /*CvOWNER(cv) = 0;*/ |
|
|
403 | /*error must unlock this cv etc.. etc...*/ |
|
|
404 | #endif |
|
|
405 | EXTEND (SP, CvDEPTH(cv)*2); |
404 | EXTEND (SP, CvDEPTH(cv)*2); |
406 | |
405 | |
407 | while (--CvDEPTH(cv)) |
406 | while (--CvDEPTH(cv)) |
408 | { |
407 | { |
409 | /* this tells the restore code to increment CvDEPTH */ |
408 | /* this tells the restore code to increment CvDEPTH */ |
… | |
… | |
412 | } |
411 | } |
413 | |
412 | |
414 | PUSHs ((SV *)CvPADLIST(cv)); |
413 | PUSHs ((SV *)CvPADLIST(cv)); |
415 | PUSHs ((SV *)cv); |
414 | PUSHs ((SV *)cv); |
416 | |
415 | |
417 | get_padlist (cv); /* this is a monster */ |
416 | get_padlist (aTHX_ cv); /* this is a monster */ |
418 | } |
417 | } |
419 | } |
418 | } |
420 | #ifdef CXt_FORMAT |
419 | #ifdef CXt_FORMAT |
421 | else if (CxTYPE(cx) == CXt_FORMAT) |
420 | else if (CxTYPE(cx) == CXt_FORMAT) |
422 | { |
421 | { |
… | |
… | |
475 | } |
474 | } |
476 | |
475 | |
477 | /* |
476 | /* |
478 | * allocate various perl stacks. This is an exact copy |
477 | * allocate various perl stacks. This is an exact copy |
479 | * of perl.c:init_stacks, except that it uses less memory |
478 | * of perl.c:init_stacks, except that it uses less memory |
480 | * on the assumption that coroutines do not usually need |
479 | * on the (sometimes correct) assumption that coroutines do |
481 | * a lot of stackspace. |
480 | * not usually need a lot of stackspace. |
482 | */ |
481 | */ |
483 | STATIC void |
482 | STATIC void |
484 | coro_init_stacks (pTHX) |
483 | coro_init_stacks (pTHX) |
485 | { |
484 | { |
|
|
485 | LOCK; |
|
|
486 | |
486 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
487 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
487 | PL_curstackinfo->si_type = PERLSI_MAIN; |
488 | PL_curstackinfo->si_type = PERLSI_MAIN; |
488 | PL_curstack = PL_curstackinfo->si_stack; |
489 | PL_curstack = PL_curstackinfo->si_stack; |
489 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
490 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
490 | |
491 | |
… | |
… | |
514 | PL_savestack_max = 96; |
515 | PL_savestack_max = 96; |
515 | |
516 | |
516 | New(54,PL_retstack,8,OP*); |
517 | New(54,PL_retstack,8,OP*); |
517 | PL_retstack_ix = 0; |
518 | PL_retstack_ix = 0; |
518 | PL_retstack_max = 8; |
519 | PL_retstack_max = 8; |
|
|
520 | |
|
|
521 | UNLOCK; |
519 | } |
522 | } |
520 | |
523 | |
521 | /* |
524 | /* |
522 | * destroy the stacks, the callchain etc... |
525 | * destroy the stacks, the callchain etc... |
523 | */ |
526 | */ |
… | |
… | |
525 | destroy_stacks(pTHX) |
528 | destroy_stacks(pTHX) |
526 | { |
529 | { |
527 | if (!IN_DESTRUCT) |
530 | if (!IN_DESTRUCT) |
528 | { |
531 | { |
529 | /* is this ugly, I ask? */ |
532 | /* is this ugly, I ask? */ |
530 | while (PL_scopestack_ix) |
533 | LEAVE_SCOPE (0); |
531 | LEAVE; |
|
|
532 | |
534 | |
533 | /* sure it is, but more important: is it correct?? :/ */ |
535 | /* sure it is, but more important: is it correct?? :/ */ |
534 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
|
|
535 | FREETMPS; |
536 | FREETMPS; |
|
|
537 | |
|
|
538 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
536 | } |
539 | } |
537 | |
540 | |
538 | while (PL_curstackinfo->si_next) |
541 | while (PL_curstackinfo->si_next) |
539 | PL_curstackinfo = PL_curstackinfo->si_next; |
542 | PL_curstackinfo = PL_curstackinfo->si_next; |
540 | |
543 | |
541 | while (PL_curstackinfo) |
544 | while (PL_curstackinfo) |
542 | { |
545 | { |
543 | PERL_SI *p = PL_curstackinfo->si_prev; |
546 | PERL_SI *p = PL_curstackinfo->si_prev; |
544 | |
547 | |
545 | { |
548 | { /*D*//*remove*/ |
546 | dSP; |
549 | dSP; |
547 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
550 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
548 | PUTBACK; /* possibly superfluous */ |
551 | PUTBACK; /* possibly superfluous */ |
549 | } |
552 | } |
550 | |
553 | |
551 | if (!IN_DESTRUCT) |
554 | if (!IN_DESTRUCT) |
552 | { |
555 | { |
553 | dounwind(-1); |
556 | dounwind (-1);/*D*//*remove*/ |
554 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
557 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
555 | } |
558 | } |
556 | |
559 | |
557 | Safefree(PL_curstackinfo->si_cxstack); |
560 | Safefree (PL_curstackinfo->si_cxstack); |
558 | Safefree(PL_curstackinfo); |
561 | Safefree (PL_curstackinfo); |
559 | PL_curstackinfo = p; |
562 | PL_curstackinfo = p; |
560 | } |
563 | } |
561 | |
564 | |
562 | Safefree(PL_tmps_stack); |
565 | Safefree (PL_tmps_stack); |
563 | Safefree(PL_markstack); |
566 | Safefree (PL_markstack); |
564 | Safefree(PL_scopestack); |
567 | Safefree (PL_scopestack); |
565 | Safefree(PL_savestack); |
568 | Safefree (PL_savestack); |
566 | Safefree(PL_retstack); |
569 | Safefree (PL_retstack); |
567 | } |
570 | } |
568 | |
571 | |
569 | static void |
572 | static void |
570 | allocate_stack (Coro__State ctx, int alloc) |
573 | allocate_stack (Coro__State ctx, int alloc) |
571 | { |
574 | { |
… | |
… | |
574 | New (0, stack, 1, coro_stack); |
577 | New (0, stack, 1, coro_stack); |
575 | |
578 | |
576 | stack->refcnt = 1; |
579 | stack->refcnt = 1; |
577 | stack->usecnt = 1; |
580 | stack->usecnt = 1; |
578 | stack->gencnt = ctx->gencnt = 0; |
581 | stack->gencnt = ctx->gencnt = 0; |
|
|
582 | |
579 | if (alloc) |
583 | if (alloc) |
580 | { |
584 | { |
581 | #if HAVE_MMAP |
585 | #if HAVE_MMAP |
582 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
586 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
583 | 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); |
584 | if (stack->sptr == (void *)-1) |
588 | if (stack->sptr == (void *)-1) |
585 | #endif |
589 | #endif |
586 | { |
590 | { |
587 | /*FIXME*//*D*//* reasonable stack size! */ |
591 | /*FIXME*//*D*//* reasonable stack size! */ |
588 | stack->ssize = -4096 * sizeof (long); |
592 | stack->ssize = - (8192 * sizeof (long)); |
589 | New (0, stack->sptr, 4096, long); |
593 | New (0, stack->sptr, 8192, long); |
590 | } |
594 | } |
591 | } |
595 | } |
592 | else |
596 | else |
593 | stack->sptr = 0; |
597 | stack->sptr = 0; |
594 | |
598 | |
… | |
… | |
624 | setup_coro (void *arg) |
628 | setup_coro (void *arg) |
625 | { |
629 | { |
626 | /* |
630 | /* |
627 | * emulate part of the perl startup here. |
631 | * emulate part of the perl startup here. |
628 | */ |
632 | */ |
|
|
633 | dTHX; |
629 | dSP; |
634 | dSP; |
630 | Coro__State ctx = (Coro__State)arg; |
635 | Coro__State ctx = (Coro__State)arg; |
631 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
636 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
632 | |
637 | |
633 | coro_init_stacks (aTHX); |
638 | coro_init_stacks (aTHX); |
634 | /*PL_curcop = 0;*/ |
639 | /*PL_curcop = 0;*/ |
635 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
640 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
636 | SvREFCNT_dec (GvAV (PL_defgv)); |
641 | SvREFCNT_dec (GvAV (PL_defgv)); |
… | |
… | |
682 | continue_coro (void *arg) |
687 | continue_coro (void *arg) |
683 | { |
688 | { |
684 | /* |
689 | /* |
685 | * this is a _very_ stripped down perl interpreter ;) |
690 | * this is a _very_ stripped down perl interpreter ;) |
686 | */ |
691 | */ |
|
|
692 | dTHX; |
687 | Coro__State ctx = (Coro__State)arg; |
693 | Coro__State ctx = (Coro__State)arg; |
688 | JMPENV coro_start_env; |
694 | JMPENV coro_start_env; |
689 | |
695 | |
690 | PL_top_env = &ctx->start_env; |
696 | PL_top_env = &ctx->start_env; |
691 | |
697 | |
… | |
… | |
695 | |
701 | |
696 | abort (); |
702 | abort (); |
697 | } |
703 | } |
698 | |
704 | |
699 | STATIC void |
705 | STATIC void |
700 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
706 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
701 | { |
707 | { |
702 | dSTACKLEVEL; |
708 | dSTACKLEVEL; |
703 | static struct coro *xnext; |
|
|
704 | |
709 | |
705 | if (prev != next) |
710 | if (prev != next) |
706 | { |
711 | { |
707 | xnext = next; |
|
|
708 | |
|
|
709 | if (next->mainstack) |
712 | if (next->mainstack) |
710 | { |
713 | { |
|
|
714 | LOCK; |
711 | SAVE (prev, flags); |
715 | SAVE (prev, flags); |
712 | LOAD (next); |
716 | LOAD (next); |
|
|
717 | UNLOCK; |
713 | |
718 | |
714 | /* mark this state as in-use */ |
719 | /* mark this state as in-use */ |
715 | next->mainstack = 0; |
720 | next->mainstack = 0; |
716 | next->tmps_ix = -2; |
721 | next->tmps_ix = -2; |
717 | |
722 | |
… | |
… | |
736 | continue_coro, (void *)next, |
741 | continue_coro, (void *)next, |
737 | next->stack->sptr, labs (next->stack->ssize)); |
742 | next->stack->sptr, labs (next->stack->ssize)); |
738 | } |
743 | } |
739 | |
744 | |
740 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
745 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
746 | prev->cursp = stacklevel; |
741 | /* don't add any code here */ |
747 | /* don't add any code here */ |
742 | } |
748 | } |
743 | |
749 | else |
|
|
750 | next->cursp = stacklevel; |
744 | } |
751 | } |
745 | else if (next->tmps_ix == -2) |
752 | else if (next->tmps_ix == -2) |
746 | croak ("tried to transfer to running coroutine"); |
753 | croak ("tried to transfer to running coroutine"); |
747 | else |
754 | else |
748 | { |
755 | { |
|
|
756 | LOCK; |
749 | SAVE (prev, -1); /* first get rid of the old state */ |
757 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
758 | UNLOCK; |
750 | |
759 | |
751 | if (flags & TRANSFER_SAVE_CCTXT) |
760 | if (flags & TRANSFER_SAVE_CCTXT) |
752 | { |
761 | { |
753 | if (!prev->stack) |
762 | if (!prev->stack) |
754 | allocate_stack (prev, 0); |
763 | allocate_stack (prev, 0); |
… | |
… | |
756 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
765 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
757 | { |
766 | { |
758 | PL_top_env = &next->start_env; |
767 | PL_top_env = &next->start_env; |
759 | |
768 | |
760 | setup_coro (next); |
769 | setup_coro (next); |
|
|
770 | next->cursp = stacklevel; |
761 | |
771 | |
762 | prev->stack->refcnt++; |
772 | prev->stack->refcnt++; |
763 | prev->stack->usecnt++; |
773 | prev->stack->usecnt++; |
764 | next->stack = prev->stack; |
774 | next->stack = prev->stack; |
765 | next->gencnt = prev->gencnt; |
775 | next->gencnt = prev->gencnt; |
… | |
… | |
770 | allocate_stack (next, 1); |
780 | allocate_stack (next, 1); |
771 | coro_create (&(next->stack->cctx), |
781 | coro_create (&(next->stack->cctx), |
772 | setup_coro, (void *)next, |
782 | setup_coro, (void *)next, |
773 | next->stack->sptr, labs (next->stack->ssize)); |
783 | next->stack->sptr, labs (next->stack->ssize)); |
774 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
784 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
785 | prev->cursp = stacklevel; |
775 | /* don't add any code here */ |
786 | /* don't add any code here */ |
776 | } |
787 | } |
777 | } |
788 | } |
778 | else |
789 | else |
|
|
790 | { |
779 | setup_coro (next); |
791 | setup_coro (next); |
|
|
792 | next->cursp = stacklevel; |
|
|
793 | } |
780 | } |
794 | } |
781 | |
|
|
782 | /* |
|
|
783 | * xnext is now either prev or next, depending on wether |
|
|
784 | * we switched the c stack or not. that's why I use a global |
|
|
785 | * variable, that should become thread-specific at one point. |
|
|
786 | */ |
|
|
787 | xnext->cursp = stacklevel; |
|
|
788 | } |
795 | } |
789 | |
796 | |
|
|
797 | LOCK; |
790 | if (coro_mortal) |
798 | if (coro_mortal) |
791 | { |
799 | { |
792 | SvREFCNT_dec (coro_mortal); |
800 | SvREFCNT_dec (coro_mortal); |
793 | coro_mortal = 0; |
801 | coro_mortal = 0; |
794 | } |
802 | } |
|
|
803 | UNLOCK; |
795 | } |
804 | } |
796 | |
805 | |
797 | #define SV_CORO(sv,func) \ |
806 | #define SV_CORO(sv,func) \ |
798 | do { \ |
807 | do { \ |
799 | if (SvROK (sv)) \ |
808 | if (SvROK (sv)) \ |
800 | sv = SvRV (sv); \ |
809 | sv = SvRV (sv); \ |
801 | \ |
810 | \ |
802 | if (SvTYPE(sv) == SVt_PVHV) \ |
811 | if (SvTYPE (sv) == SVt_PVHV) \ |
803 | { \ |
812 | { \ |
804 | 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); \ |
805 | \ |
814 | \ |
806 | if (!he) \ |
815 | if (!he) \ |
807 | 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); \ |
808 | \ |
817 | \ |
809 | (sv) = SvRV (HeVAL(he)); \ |
818 | (sv) = SvRV (HeVAL(he)); \ |
810 | } \ |
819 | } \ |
811 | \ |
820 | \ |
812 | /* must also be changed inside Coro::Cont::yield */ \ |
821 | /* must also be changed inside Coro::Cont::yield */ \ |
813 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
822 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
814 | 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); \ |
815 | \ |
824 | \ |
816 | } while(0) |
825 | } while(0) |
817 | |
826 | |
818 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
827 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
… | |
… | |
821 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
830 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
822 | { |
831 | { |
823 | SV_CORO (prev, "Coro::transfer"); |
832 | SV_CORO (prev, "Coro::transfer"); |
824 | SV_CORO (next, "Coro::transfer"); |
833 | SV_CORO (next, "Coro::transfer"); |
825 | |
834 | |
826 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
835 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
827 | } |
836 | } |
828 | |
837 | |
829 | /** Coro ********************************************************************/ |
838 | /** Coro ********************************************************************/ |
830 | |
839 | |
831 | #define PRIO_MAX 3 |
840 | #define PRIO_MAX 3 |
… | |
… | |
839 | static GV *coro_current, *coro_idle; |
848 | static GV *coro_current, *coro_idle; |
840 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
849 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
841 | static int coro_nready; |
850 | static int coro_nready; |
842 | |
851 | |
843 | static void |
852 | static void |
844 | coro_enq (SV *sv) |
853 | coro_enq (pTHX_ SV *sv) |
845 | { |
854 | { |
846 | if (SvTYPE (sv) == SVt_PVHV) |
855 | if (SvTYPE (sv) == SVt_PVHV) |
847 | { |
856 | { |
848 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
857 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
849 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
858 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
… | |
… | |
860 | |
869 | |
861 | 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"); |
862 | } |
871 | } |
863 | |
872 | |
864 | static SV * |
873 | static SV * |
865 | coro_deq (int min_prio) |
874 | coro_deq (pTHX_ int min_prio) |
866 | { |
875 | { |
867 | int prio = PRIO_MAX - PRIO_MIN; |
876 | int prio = PRIO_MAX - PRIO_MIN; |
868 | |
877 | |
869 | min_prio -= PRIO_MIN; |
878 | min_prio -= PRIO_MIN; |
870 | if (min_prio < 0) |
879 | if (min_prio < 0) |
… | |
… | |
881 | } |
890 | } |
882 | |
891 | |
883 | static void |
892 | static void |
884 | api_ready (SV *coro) |
893 | api_ready (SV *coro) |
885 | { |
894 | { |
|
|
895 | dTHX; |
|
|
896 | |
886 | if (SvROK (coro)) |
897 | if (SvROK (coro)) |
887 | coro = SvRV (coro); |
898 | coro = SvRV (coro); |
888 | |
899 | |
|
|
900 | LOCK; |
889 | coro_enq (SvREFCNT_inc (coro)); |
901 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
|
|
902 | UNLOCK; |
890 | } |
903 | } |
891 | |
904 | |
892 | static void |
905 | static void |
893 | api_schedule (void) |
906 | api_schedule (void) |
894 | { |
907 | { |
|
|
908 | dTHX; |
|
|
909 | |
895 | SV *prev, *next; |
910 | SV *prev, *next; |
896 | |
911 | |
|
|
912 | LOCK; |
|
|
913 | |
897 | prev = SvRV (GvSV (coro_current)); |
914 | prev = SvRV (GvSV (coro_current)); |
898 | next = coro_deq (PRIO_MIN); |
915 | next = coro_deq (aTHX_ PRIO_MIN); |
899 | |
916 | |
900 | if (!next) |
917 | if (!next) |
901 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
918 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
902 | |
919 | |
903 | /* free this only after the transfer */ |
920 | /* free this only after the transfer */ |
… | |
… | |
906 | |
923 | |
907 | SvRV (GvSV (coro_current)) = next; |
924 | SvRV (GvSV (coro_current)) = next; |
908 | |
925 | |
909 | SV_CORO (next, "Coro::schedule"); |
926 | SV_CORO (next, "Coro::schedule"); |
910 | |
927 | |
|
|
928 | UNLOCK; |
|
|
929 | |
911 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
930 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
912 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
931 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
913 | } |
932 | } |
914 | |
933 | |
915 | static void |
934 | static void |
916 | api_cede (void) |
935 | api_cede (void) |
917 | { |
936 | { |
|
|
937 | dTHX; |
|
|
938 | |
|
|
939 | LOCK; |
918 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
940 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
941 | UNLOCK; |
919 | |
942 | |
920 | api_schedule (); |
943 | api_schedule (); |
921 | } |
944 | } |
922 | |
945 | |
923 | MODULE = Coro::State PACKAGE = Coro::State |
946 | MODULE = Coro::State PACKAGE = Coro::State |
924 | |
947 | |
925 | PROTOTYPES: ENABLE |
948 | PROTOTYPES: ENABLE |
926 | |
949 | |
927 | BOOT: |
950 | BOOT: |
928 | { /* {} 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 | |
929 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
956 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
930 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
957 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
931 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
958 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
932 | |
959 | |
933 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
960 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
934 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
961 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
935 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
962 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
936 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
963 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
937 | |
|
|
938 | if (!padlist_cache) |
|
|
939 | padlist_cache = newHV (); |
|
|
940 | |
964 | |
941 | main_mainstack = PL_mainstack; |
965 | main_mainstack = PL_mainstack; |
942 | |
966 | |
943 | coroapi.ver = CORO_API_VERSION; |
967 | coroapi.ver = CORO_API_VERSION; |
944 | coroapi.transfer = api_transfer; |
968 | coroapi.transfer = api_transfer; |
… | |
… | |
955 | croak ("Coro::State::_newprocess expects an arrayref"); |
979 | croak ("Coro::State::_newprocess expects an arrayref"); |
956 | |
980 | |
957 | Newz (0, coro, 1, struct coro); |
981 | Newz (0, coro, 1, struct coro); |
958 | |
982 | |
959 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
983 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
960 | coro->mainstack = 0; /* actual work is done inside transfer */ |
984 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
961 | coro->stack = 0; |
985 | /*coro->stack = 0;*/ |
962 | |
986 | |
963 | /* same as JMPENV_BOOTSTRAP */ |
987 | /* same as JMPENV_BOOTSTRAP */ |
964 | /* 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 */ |
965 | //Zero(&coro->start_env, 1, JMPENV); |
989 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
966 | coro->start_env.je_ret = -1; |
990 | coro->start_env.je_ret = -1; |
967 | coro->start_env.je_mustcatch = TRUE; |
991 | coro->start_env.je_mustcatch = TRUE; |
968 | |
992 | |
969 | RETVAL = coro; |
993 | RETVAL = coro; |
970 | OUTPUT: |
994 | OUTPUT: |
… | |
… | |
1008 | deallocate_stack (coro); |
1032 | deallocate_stack (coro); |
1009 | SvREFCNT_dec (coro->args); |
1033 | SvREFCNT_dec (coro->args); |
1010 | Safefree (coro); |
1034 | Safefree (coro); |
1011 | |
1035 | |
1012 | void |
1036 | void |
1013 | flush() |
|
|
1014 | CODE: |
|
|
1015 | #ifdef MAY_FLUSH |
|
|
1016 | flush_padlist_cache (); |
|
|
1017 | #endif |
|
|
1018 | |
|
|
1019 | void |
|
|
1020 | _exit(code) |
1037 | _exit(code) |
1021 | int code |
1038 | int code |
1022 | PROTOTYPE: $ |
1039 | PROTOTYPE: $ |
1023 | CODE: |
1040 | CODE: |
1024 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
1025 | _exit (code); |
1041 | _exit (code); |
1026 | #else |
|
|
1027 | signal (SIGTERM, SIG_DFL); |
|
|
1028 | raise (SIGTERM); |
|
|
1029 | exit (code); |
|
|
1030 | #endif |
|
|
1031 | |
1042 | |
1032 | MODULE = Coro::State PACKAGE = Coro::Cont |
1043 | MODULE = Coro::State PACKAGE = Coro::Cont |
1033 | |
1044 | |
1034 | # this is slightly dirty (should expose a c-level api) |
1045 | # this is slightly dirty (should expose a c-level api) |
1035 | |
1046 | |
… | |
… | |
1094 | sv_setiv(sv, (IV)&coroapi); |
1105 | sv_setiv(sv, (IV)&coroapi); |
1095 | SvREADONLY_on(sv); |
1106 | SvREADONLY_on(sv); |
1096 | } |
1107 | } |
1097 | } |
1108 | } |
1098 | |
1109 | |
|
|
1110 | #if !PERL_MICRO |
|
|
1111 | |
1099 | void |
1112 | void |
1100 | ready(self) |
1113 | ready(self) |
1101 | SV * self |
1114 | SV * self |
1102 | PROTOTYPE: $ |
1115 | PROTOTYPE: $ |
1103 | CODE: |
1116 | CODE: |
1104 | api_ready (self); |
1117 | api_ready (self); |
|
|
1118 | |
|
|
1119 | #endif |
1105 | |
1120 | |
1106 | int |
1121 | int |
1107 | nready(...) |
1122 | nready(...) |
1108 | PROTOTYPE: |
1123 | PROTOTYPE: |
1109 | CODE: |
1124 | CODE: |