1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
|
|
4 | |
|
|
5 | #include "patchlevel.h" |
|
|
6 | |
|
|
7 | #if PATCHLEVEL < 6 |
|
|
8 | # ifndef PL_ppaddr |
|
|
9 | # define PL_ppaddr ppaddr |
|
|
10 | # endif |
|
|
11 | # ifndef call_sv |
|
|
12 | # define call_sv perl_call_sv |
|
|
13 | # endif |
|
|
14 | # ifndef get_sv |
|
|
15 | # define get_sv perl_get_sv |
|
|
16 | # endif |
|
|
17 | # ifndef get_cv |
|
|
18 | # define get_cv perl_get_cv |
|
|
19 | # endif |
|
|
20 | # ifndef IS_PADGV |
|
|
21 | # define IS_PADGV(v) 0 |
|
|
22 | # endif |
|
|
23 | # ifndef IS_PADCONST |
|
|
24 | # define IS_PADCONST(v) 0 |
|
|
25 | # endif |
|
|
26 | #endif |
4 | |
27 | |
5 | #include "libcoro/coro.c" |
28 | #include "libcoro/coro.c" |
6 | |
29 | |
7 | #include <signal.h> |
30 | #include <signal.h> |
8 | |
31 | |
… | |
… | |
15 | # else |
38 | # else |
16 | # undef HAVE_MMAP |
39 | # undef HAVE_MMAP |
17 | # endif |
40 | # endif |
18 | # endif |
41 | # endif |
19 | #endif |
42 | #endif |
20 | |
|
|
21 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
|
|
22 | |
43 | |
23 | #define SUB_INIT "Coro::State::initialize" |
44 | #define SUB_INIT "Coro::State::initialize" |
24 | #define UCORO_STATE "_coro_state" |
45 | #define UCORO_STATE "_coro_state" |
25 | |
46 | |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
47 | /* The next macro should declare a variable stacklevel that contains and approximation |
… | |
… | |
161 | sv = (SV *) newAV (); |
182 | sv = (SV *) newAV (); |
162 | else if (*name == '%') |
183 | else if (*name == '%') |
163 | sv = (SV *) newHV (); |
184 | sv = (SV *) newHV (); |
164 | else |
185 | else |
165 | sv = NEWSV (0, 0); |
186 | sv = NEWSV (0, 0); |
|
|
187 | #ifdef SvPADBUSY |
166 | if (!SvPADBUSY (sv)) |
188 | if (!SvPADBUSY (sv)) |
|
|
189 | #endif |
167 | SvPADMY_on (sv); |
190 | SvPADMY_on (sv); |
168 | npad[ix] = sv; |
191 | npad[ix] = sv; |
169 | } |
192 | } |
170 | } |
193 | } |
171 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
194 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
… | |
… | |
201 | #endif |
224 | #endif |
202 | |
225 | |
203 | return newpadlist; |
226 | return newpadlist; |
204 | } |
227 | } |
205 | |
228 | |
206 | #ifdef MAY_FLUSH |
|
|
207 | STATIC void |
229 | STATIC void |
208 | free_padlist (AV *padlist) |
230 | free_padlist (AV *padlist) |
209 | { |
231 | { |
210 | /* may be during global destruction */ |
232 | /* may be during global destruction */ |
211 | if (SvREFCNT(padlist)) |
233 | if (SvREFCNT (padlist)) |
212 | { |
234 | { |
213 | I32 i = AvFILLp(padlist); |
235 | I32 i = AvFILLp (padlist); |
214 | while (i >= 0) |
236 | while (i >= 0) |
215 | { |
237 | { |
216 | SV **svp = av_fetch(padlist, i--, FALSE); |
238 | SV **svp = av_fetch (padlist, i--, FALSE); |
217 | SV *sv = svp ? *svp : Nullsv; |
|
|
218 | if (sv) |
239 | if (svp) |
|
|
240 | { |
|
|
241 | SV *sv; |
|
|
242 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
219 | SvREFCNT_dec(sv); |
243 | SvREFCNT_dec (sv); |
|
|
244 | |
|
|
245 | SvREFCNT_dec (*svp); |
|
|
246 | } |
220 | } |
247 | } |
221 | |
248 | |
222 | SvREFCNT_dec((SV*)padlist); |
249 | SvREFCNT_dec ((SV*)padlist); |
223 | } |
250 | } |
224 | } |
251 | } |
225 | #endif |
252 | |
|
|
253 | STATIC int |
|
|
254 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
255 | { |
|
|
256 | AV *padlist; |
|
|
257 | AV *av = (AV *)mg->mg_obj; |
|
|
258 | |
|
|
259 | /* casting is fun. */ |
|
|
260 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
261 | free_padlist (padlist); |
|
|
262 | } |
|
|
263 | |
|
|
264 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
|
|
265 | |
|
|
266 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
226 | |
267 | |
227 | /* the next two functions merely cache the padlists */ |
268 | /* the next two functions merely cache the padlists */ |
228 | STATIC void |
269 | STATIC void |
229 | get_padlist (CV *cv) |
270 | get_padlist (CV *cv) |
230 | { |
271 | { |
231 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
272 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
232 | |
273 | |
233 | if (he && AvFILLp ((AV *)*he) >= 0) |
274 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
234 | CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
275 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
235 | else |
276 | else |
236 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
277 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
237 | } |
278 | } |
238 | |
279 | |
239 | STATIC void |
280 | STATIC void |
240 | put_padlist (CV *cv) |
281 | put_padlist (CV *cv) |
241 | { |
282 | { |
242 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); |
283 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
243 | |
284 | |
244 | if (SvTYPE (*he) != SVt_PVAV) |
285 | if (!mg) |
245 | { |
286 | { |
246 | SvREFCNT_dec (*he); |
287 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
288 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
289 | mg->mg_virtual = &vtbl_coro; |
247 | *he = (SV *)newAV (); |
290 | mg->mg_obj = (SV *)newAV (); |
248 | } |
291 | } |
249 | |
292 | |
250 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
293 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
251 | } |
294 | } |
252 | |
|
|
253 | #ifdef MAY_FLUSH |
|
|
254 | STATIC void |
|
|
255 | flush_padlist_cache () |
|
|
256 | { |
|
|
257 | HV *hv = padlist_cache; |
|
|
258 | padlist_cache = newHV (); |
|
|
259 | |
|
|
260 | if (hv_iterinit (hv)) |
|
|
261 | { |
|
|
262 | HE *he; |
|
|
263 | AV *padlist; |
|
|
264 | |
|
|
265 | while (!!(he = hv_iternext (hv))) |
|
|
266 | { |
|
|
267 | AV *av = (AV *)HeVAL(he); |
|
|
268 | |
|
|
269 | /* casting is fun. */ |
|
|
270 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
271 | free_padlist (padlist); |
|
|
272 | } |
|
|
273 | } |
|
|
274 | |
|
|
275 | SvREFCNT_dec (hv); |
|
|
276 | } |
|
|
277 | #endif |
|
|
278 | |
295 | |
279 | #define SB do { |
296 | #define SB do { |
280 | #define SE } while (0) |
297 | #define SE } while (0) |
281 | |
298 | |
282 | #define LOAD(state) load_state(aTHX_ (state)); |
299 | #define LOAD(state) load_state(aTHX_ (state)); |
283 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
300 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
284 | |
301 | |
285 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
302 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
286 | |
303 | |
287 | static void |
304 | static void |
288 | load_state(pTHX_ Coro__State c) |
305 | load_state(pTHX_ Coro__State c) |
289 | { |
306 | { |
290 | PL_dowarn = c->dowarn; |
307 | PL_dowarn = c->dowarn; |
… | |
… | |
392 | PUSHs ((SV *)cv); |
409 | PUSHs ((SV *)cv); |
393 | |
410 | |
394 | get_padlist (cv); /* this is a monster */ |
411 | get_padlist (cv); /* this is a monster */ |
395 | } |
412 | } |
396 | } |
413 | } |
|
|
414 | #ifdef CXt_FORMAT |
397 | else if (CxTYPE(cx) == CXt_FORMAT) |
415 | else if (CxTYPE(cx) == CXt_FORMAT) |
398 | { |
416 | { |
399 | /* I never used formats, so how should I know how these are implemented? */ |
417 | /* I never used formats, so how should I know how these are implemented? */ |
400 | /* my bold guess is as a simple, plain sub... */ |
418 | /* my bold guess is as a simple, plain sub... */ |
401 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
419 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
402 | } |
420 | } |
|
|
421 | #endif |
403 | } |
422 | } |
404 | |
423 | |
405 | if (top_si->si_type == PERLSI_MAIN) |
424 | if (top_si->si_type == PERLSI_MAIN) |
406 | break; |
425 | break; |
407 | |
426 | |
… | |
… | |
450 | } |
469 | } |
451 | |
470 | |
452 | /* |
471 | /* |
453 | * allocate various perl stacks. This is an exact copy |
472 | * allocate various perl stacks. This is an exact copy |
454 | * of perl.c:init_stacks, except that it uses less memory |
473 | * of perl.c:init_stacks, except that it uses less memory |
455 | * on the assumption that coroutines do not usually need |
474 | * on the (sometimes correct) assumption that coroutines do |
456 | * a lot of stackspace. |
475 | * not usually need a lot of stackspace. |
457 | */ |
476 | */ |
458 | STATIC void |
477 | STATIC void |
459 | coro_init_stacks (pTHX) |
478 | coro_init_stacks (pTHX) |
460 | { |
479 | { |
461 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
480 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
… | |
… | |
474 | |
493 | |
475 | New(54,PL_markstack,16,I32); |
494 | New(54,PL_markstack,16,I32); |
476 | PL_markstack_ptr = PL_markstack; |
495 | PL_markstack_ptr = PL_markstack; |
477 | PL_markstack_max = PL_markstack + 16; |
496 | PL_markstack_max = PL_markstack + 16; |
478 | |
497 | |
|
|
498 | #ifdef SET_MARK_OFFSET |
479 | SET_MARK_OFFSET; |
499 | SET_MARK_OFFSET; |
|
|
500 | #endif |
480 | |
501 | |
481 | New(54,PL_scopestack,16,I32); |
502 | New(54,PL_scopestack,16,I32); |
482 | PL_scopestack_ix = 0; |
503 | PL_scopestack_ix = 0; |
483 | PL_scopestack_max = 16; |
504 | PL_scopestack_max = 16; |
484 | |
505 | |
… | |
… | |
491 | PL_retstack_max = 8; |
512 | PL_retstack_max = 8; |
492 | } |
513 | } |
493 | |
514 | |
494 | /* |
515 | /* |
495 | * destroy the stacks, the callchain etc... |
516 | * destroy the stacks, the callchain etc... |
496 | * still there is a memleak of 128 bytes... |
|
|
497 | */ |
517 | */ |
498 | STATIC void |
518 | STATIC void |
499 | destroy_stacks(pTHX) |
519 | destroy_stacks(pTHX) |
500 | { |
520 | { |
501 | if (!IN_DESTRUCT) |
521 | if (!IN_DESTRUCT) |
502 | { |
522 | { |
503 | /* is this ugly, I ask? */ |
523 | /* is this ugly, I ask? */ |
504 | while (PL_scopestack_ix) |
524 | LEAVE_SCOPE (0); |
505 | LEAVE; |
|
|
506 | |
525 | |
507 | /* sure it is, but more important: is it correct?? :/ */ |
526 | /* sure it is, but more important: is it correct?? :/ */ |
508 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
|
|
509 | FREETMPS; |
527 | FREETMPS; |
510 | } |
528 | } |
511 | |
529 | |
512 | while (PL_curstackinfo->si_next) |
530 | while (PL_curstackinfo->si_next) |
513 | PL_curstackinfo = PL_curstackinfo->si_next; |
531 | PL_curstackinfo = PL_curstackinfo->si_next; |
514 | |
532 | |
… | |
… | |
551 | stack->usecnt = 1; |
569 | stack->usecnt = 1; |
552 | stack->gencnt = ctx->gencnt = 0; |
570 | stack->gencnt = ctx->gencnt = 0; |
553 | if (alloc) |
571 | if (alloc) |
554 | { |
572 | { |
555 | #if HAVE_MMAP |
573 | #if HAVE_MMAP |
556 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
574 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
557 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
575 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
558 | if (stack->sptr == (void *)-1) |
576 | if (stack->sptr == (void *)-1) |
559 | #endif |
577 | #endif |
560 | { |
578 | { |
561 | /*FIXME*//*D*//* reasonable stack size! */ |
579 | /*FIXME*//*D*//* reasonable stack size! */ |
562 | stack->ssize = -4096 * sizeof (long); |
580 | stack->ssize = - (8192 * sizeof (long)); |
563 | New (0, stack->sptr, 4096, long); |
581 | New (0, stack->sptr, 8192, long); |
564 | } |
582 | } |
565 | } |
583 | } |
566 | else |
584 | else |
567 | stack->sptr = 0; |
585 | stack->sptr = 0; |
568 | |
586 | |
… | |
… | |
582 | { |
600 | { |
583 | #ifdef HAVE_MMAP |
601 | #ifdef HAVE_MMAP |
584 | if (stack->ssize > 0 && stack->sptr) |
602 | if (stack->ssize > 0 && stack->sptr) |
585 | munmap (stack->sptr, stack->ssize); |
603 | munmap (stack->sptr, stack->ssize); |
586 | else |
604 | else |
587 | #else |
605 | #endif |
588 | Safefree (stack->sptr); |
606 | Safefree (stack->sptr); |
589 | #endif |
607 | |
590 | Safefree (stack); |
608 | Safefree (stack); |
591 | } |
609 | } |
592 | else if (ctx->gencnt == stack->gencnt) |
610 | else if (ctx->gencnt == stack->gencnt) |
593 | --stack->usecnt; |
611 | --stack->usecnt; |
594 | } |
612 | } |
… | |
… | |
600 | /* |
618 | /* |
601 | * emulate part of the perl startup here. |
619 | * emulate part of the perl startup here. |
602 | */ |
620 | */ |
603 | dSP; |
621 | dSP; |
604 | Coro__State ctx = (Coro__State)arg; |
622 | Coro__State ctx = (Coro__State)arg; |
605 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
623 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
606 | |
624 | |
607 | coro_init_stacks (aTHX); |
625 | coro_init_stacks (aTHX); |
608 | /*PL_curcop = 0;*/ |
626 | /*PL_curcop = 0;*/ |
609 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
627 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
610 | SvREFCNT_dec (GvAV (PL_defgv)); |
628 | SvREFCNT_dec (GvAV (PL_defgv)); |
611 | GvAV (PL_defgv) = ctx->args; |
629 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
612 | |
630 | |
613 | SPAGAIN; |
631 | SPAGAIN; |
614 | |
632 | |
615 | if (ctx->stack) |
633 | if (ctx->stack) |
616 | { |
634 | { |
… | |
… | |
738 | next->stack = prev->stack; |
756 | next->stack = prev->stack; |
739 | next->gencnt = prev->gencnt; |
757 | next->gencnt = prev->gencnt; |
740 | } |
758 | } |
741 | else |
759 | else |
742 | { |
760 | { |
|
|
761 | assert (!next->stack); |
743 | allocate_stack (next, 1); |
762 | allocate_stack (next, 1); |
744 | coro_create (&(next->stack->cctx), |
763 | coro_create (&(next->stack->cctx), |
745 | setup_coro, (void *)next, |
764 | setup_coro, (void *)next, |
746 | next->stack->sptr, labs (next->stack->ssize)); |
765 | next->stack->sptr, labs (next->stack->ssize)); |
747 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
766 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
… | |
… | |
925 | Coro__State coro; |
944 | Coro__State coro; |
926 | |
945 | |
927 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
946 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
928 | croak ("Coro::State::_newprocess expects an arrayref"); |
947 | croak ("Coro::State::_newprocess expects an arrayref"); |
929 | |
948 | |
930 | New (0, coro, 1, struct coro); |
949 | Newz (0, coro, 1, struct coro); |
931 | |
950 | |
932 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
951 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
933 | coro->mainstack = 0; /* actual work is done inside transfer */ |
952 | coro->mainstack = 0; /* actual work is done inside transfer */ |
934 | coro->stack = 0; |
953 | coro->stack = 0; |
935 | |
954 | |
936 | /* same as JMPENV_BOOTSTRAP */ |
955 | /* same as JMPENV_BOOTSTRAP */ |
937 | /* we might be able to recycle start_env, but safe is safe */ |
956 | /* we might be able to recycle start_env, but safe is safe */ |
938 | Zero(&coro->start_env, 1, JMPENV); |
957 | //Zero(&coro->start_env, 1, JMPENV); |
939 | coro->start_env.je_ret = -1; |
958 | coro->start_env.je_ret = -1; |
940 | coro->start_env.je_mustcatch = TRUE; |
959 | coro->start_env.je_mustcatch = TRUE; |
941 | |
960 | |
942 | RETVAL = coro; |
961 | RETVAL = coro; |
943 | OUTPUT: |
962 | OUTPUT: |
… | |
… | |
977 | |
996 | |
978 | coro->mainstack = 0; |
997 | coro->mainstack = 0; |
979 | } |
998 | } |
980 | |
999 | |
981 | deallocate_stack (coro); |
1000 | deallocate_stack (coro); |
982 | |
1001 | SvREFCNT_dec (coro->args); |
983 | Safefree (coro); |
1002 | Safefree (coro); |
984 | |
|
|
985 | void |
|
|
986 | flush() |
|
|
987 | CODE: |
|
|
988 | #ifdef MAY_FLUSH |
|
|
989 | flush_padlist_cache (); |
|
|
990 | #endif |
|
|
991 | |
1003 | |
992 | void |
1004 | void |
993 | _exit(code) |
1005 | _exit(code) |
994 | int code |
1006 | int code |
995 | PROTOTYPE: $ |
1007 | PROTOTYPE: $ |
996 | CODE: |
1008 | CODE: |
997 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
998 | _exit (code); |
1009 | _exit (code); |
999 | #else |
|
|
1000 | signal (SIGTERM, SIG_DFL); |
|
|
1001 | raise (SIGTERM); |
|
|
1002 | exit (code); |
|
|
1003 | #endif |
|
|
1004 | |
1010 | |
1005 | MODULE = Coro::State PACKAGE = Coro::Cont |
1011 | MODULE = Coro::State PACKAGE = Coro::Cont |
1006 | |
1012 | |
1007 | # this is slightly dirty (should expose a c-level api) |
1013 | # this is slightly dirty (should expose a c-level api) |
1008 | |
1014 | |
… | |
… | |
1014 | SV *sv; |
1020 | SV *sv; |
1015 | AV *defav = GvAV (PL_defgv); |
1021 | AV *defav = GvAV (PL_defgv); |
1016 | struct coro *prev, *next; |
1022 | struct coro *prev, *next; |
1017 | |
1023 | |
1018 | if (!returnstk) |
1024 | if (!returnstk) |
1019 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
1025 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1020 | |
1026 | |
1021 | /* set up @_ -- ugly */ |
1027 | /* set up @_ -- ugly */ |
1022 | av_clear (defav); |
1028 | av_clear (defav); |
1023 | av_fill (defav, items - 1); |
1029 | av_fill (defav, items - 1); |
1024 | while (items--) |
1030 | while (items--) |
… | |
… | |
1067 | sv_setiv(sv, (IV)&coroapi); |
1073 | sv_setiv(sv, (IV)&coroapi); |
1068 | SvREADONLY_on(sv); |
1074 | SvREADONLY_on(sv); |
1069 | } |
1075 | } |
1070 | } |
1076 | } |
1071 | |
1077 | |
|
|
1078 | #if !PERL_MICRO |
|
|
1079 | |
1072 | void |
1080 | void |
1073 | ready(self) |
1081 | ready(self) |
1074 | SV * self |
1082 | SV * self |
1075 | PROTOTYPE: $ |
1083 | PROTOTYPE: $ |
1076 | CODE: |
1084 | CODE: |
1077 | api_ready (self); |
1085 | api_ready (self); |
|
|
1086 | |
|
|
1087 | #endif |
1078 | |
1088 | |
1079 | int |
1089 | int |
1080 | nready(...) |
1090 | nready(...) |
1081 | PROTOTYPE: |
1091 | PROTOTYPE: |
1082 | CODE: |
1092 | CODE: |