… | |
… | |
88 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
91 | #endif |
91 | #endif |
92 | |
92 | |
93 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macros try to return the current stack pointer, in an as |
94 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * portable way as possible. */ |
95 | * and should be unique. */ |
|
|
96 | #define dSTACKLEVEL int stacklevel |
95 | #define dSTACKLEVEL volatile char stacklevel |
97 | #define STACKLEVEL ((void *)&stacklevel) |
96 | #define STACKLEVEL ((void *)&stacklevel) |
98 | |
97 | |
99 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
100 | |
99 | |
101 | #if __GNUC__ >= 3 |
100 | #if __GNUC__ >= 3 |
… | |
… | |
210 | |
209 | |
211 | /* for Coro.pm */ |
210 | /* for Coro.pm */ |
212 | static SV *coro_current; |
211 | static SV *coro_current; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
212 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
214 | static int coro_nready; |
213 | static int coro_nready; |
215 | static struct coro *first; |
214 | static struct coro *coro_first; |
216 | |
215 | |
217 | /** lowlevel stuff **********************************************************/ |
216 | /** lowlevel stuff **********************************************************/ |
218 | |
217 | |
219 | static AV * |
218 | static AV * |
220 | coro_clone_padlist (pTHX_ CV *cv) |
219 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
395 | /* |
394 | /* |
396 | * the worst thing you can imagine happens first - we have to save |
395 | * the worst thing you can imagine happens first - we have to save |
397 | * (and reinitialize) all cv's in the whole callchain :( |
396 | * (and reinitialize) all cv's in the whole callchain :( |
398 | */ |
397 | */ |
399 | |
398 | |
400 | EXTEND (SP, 3 + 1); |
|
|
401 | PUSHs (Nullsv); |
399 | XPUSHs (Nullsv); |
402 | /* this loop was inspired by pp_caller */ |
400 | /* this loop was inspired by pp_caller */ |
403 | for (;;) |
401 | for (;;) |
404 | { |
402 | { |
405 | while (cxix >= 0) |
403 | while (cxix >= 0) |
406 | { |
404 | { |
… | |
… | |
425 | |
423 | |
426 | if (top_si->si_type == PERLSI_MAIN) |
424 | if (top_si->si_type == PERLSI_MAIN) |
427 | break; |
425 | break; |
428 | |
426 | |
429 | top_si = top_si->si_prev; |
427 | top_si = top_si->si_prev; |
430 | ccstk = top_si->si_cxstack; |
428 | ccstk = top_si->si_cxstack; |
431 | cxix = top_si->si_cxix; |
429 | cxix = top_si->si_cxix; |
432 | } |
430 | } |
433 | |
431 | |
434 | PUTBACK; |
432 | PUTBACK; |
435 | } |
433 | } |
436 | |
434 | |
… | |
… | |
455 | # define coro_init_stacks init_stacks |
453 | # define coro_init_stacks init_stacks |
456 | #else |
454 | #else |
457 | static void |
455 | static void |
458 | coro_init_stacks (pTHX) |
456 | coro_init_stacks (pTHX) |
459 | { |
457 | { |
460 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
458 | PL_curstackinfo = new_stackinfo(64, 6); |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
459 | PL_curstackinfo->si_type = PERLSI_MAIN; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
460 | PL_curstack = PL_curstackinfo->si_stack; |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
461 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
464 | |
462 | |
465 | PL_stack_base = AvARRAY(PL_curstack); |
463 | PL_stack_base = AvARRAY(PL_curstack); |
466 | PL_stack_sp = PL_stack_base; |
464 | PL_stack_sp = PL_stack_base; |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
465 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
468 | |
466 | |
469 | New(50,PL_tmps_stack,128,SV*); |
467 | New(50,PL_tmps_stack,64,SV*); |
470 | PL_tmps_floor = -1; |
468 | PL_tmps_floor = -1; |
471 | PL_tmps_ix = -1; |
469 | PL_tmps_ix = -1; |
472 | PL_tmps_max = 128; |
470 | PL_tmps_max = 64; |
473 | |
471 | |
474 | New(54,PL_markstack,32,I32); |
472 | New(54,PL_markstack,16,I32); |
475 | PL_markstack_ptr = PL_markstack; |
473 | PL_markstack_ptr = PL_markstack; |
476 | PL_markstack_max = PL_markstack + 32; |
474 | PL_markstack_max = PL_markstack + 16; |
477 | |
475 | |
478 | #ifdef SET_MARK_OFFSET |
476 | #ifdef SET_MARK_OFFSET |
479 | SET_MARK_OFFSET; |
477 | SET_MARK_OFFSET; |
480 | #endif |
478 | #endif |
481 | |
479 | |
482 | New(54,PL_scopestack,32,I32); |
480 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
481 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 32; |
482 | PL_scopestack_max = 16; |
485 | |
483 | |
486 | New(54,PL_savestack,64,ANY); |
484 | New(54,PL_savestack,64,ANY); |
487 | PL_savestack_ix = 0; |
485 | PL_savestack_ix = 0; |
488 | PL_savestack_max = 64; |
486 | PL_savestack_max = 64; |
489 | |
487 | |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
488 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | New(54,PL_retstack,16,OP*); |
489 | New(54,PL_retstack,4,OP*); |
492 | PL_retstack_ix = 0; |
490 | PL_retstack_ix = 0; |
493 | PL_retstack_max = 16; |
491 | PL_retstack_max = 4; |
494 | #endif |
492 | #endif |
495 | } |
493 | } |
496 | #endif |
494 | #endif |
497 | |
495 | |
498 | /* |
496 | /* |
… | |
… | |
547 | { |
545 | { |
548 | size_t rss = sizeof (coro); |
546 | size_t rss = sizeof (coro); |
549 | |
547 | |
550 | if (coro->mainstack) |
548 | if (coro->mainstack) |
551 | { |
549 | { |
|
|
550 | if (coro->flags & CF_RUNNING) |
|
|
551 | { |
|
|
552 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
553 | # include "state.h" |
|
|
554 | #undef VAR |
|
|
555 | } |
|
|
556 | |
552 | rss += sizeof (coro->curstackinfo); |
557 | rss += sizeof (coro->curstackinfo); |
553 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
558 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
554 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
559 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
555 | |
|
|
556 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
560 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
557 | |
|
|
558 | rss += coro->tmps_max * sizeof (SV *); |
561 | rss += coro->tmps_max * sizeof (SV *); |
559 | |
|
|
560 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
562 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
561 | |
|
|
562 | rss += coro->scopestack_max * sizeof (I32); |
563 | rss += coro->scopestack_max * sizeof (I32); |
563 | |
|
|
564 | rss += coro->savestack_max * sizeof (ANY); |
564 | rss += coro->savestack_max * sizeof (ANY); |
565 | |
565 | |
566 | #if !PERL_VERSION_ATLEAST (5,9,0) |
566 | #if !PERL_VERSION_ATLEAST (5,9,0) |
567 | rss += coro->retstack_max * sizeof (OP *); |
567 | rss += coro->retstack_max * sizeof (OP *); |
568 | #endif |
568 | #endif |
… | |
… | |
929 | cctx_destroy (coro->cctx); |
929 | cctx_destroy (coro->cctx); |
930 | SvREFCNT_dec (coro->args); |
930 | SvREFCNT_dec (coro->args); |
931 | |
931 | |
932 | if (coro->next) coro->next->prev = coro->prev; |
932 | if (coro->next) coro->next->prev = coro->prev; |
933 | if (coro->prev) coro->prev->next = coro->next; |
933 | if (coro->prev) coro->prev->next = coro->next; |
934 | if (coro == first) first = coro->next; |
934 | if (coro == coro_first) coro_first = coro->next; |
935 | |
935 | |
936 | return 1; |
936 | return 1; |
937 | } |
937 | } |
938 | |
938 | |
939 | static int |
939 | static int |
… | |
… | |
1247 | Newz (0, coro, 1, struct coro); |
1247 | Newz (0, coro, 1, struct coro); |
1248 | coro->args = newAV (); |
1248 | coro->args = newAV (); |
1249 | coro->save = CORO_SAVE_DEF; |
1249 | coro->save = CORO_SAVE_DEF; |
1250 | coro->flags = CF_NEW; |
1250 | coro->flags = CF_NEW; |
1251 | |
1251 | |
1252 | if (first) first->prev = coro; |
1252 | if (coro_first) coro_first->prev = coro; |
1253 | coro->next = first; |
1253 | coro->next = coro_first; |
1254 | first = coro; |
1254 | coro_first = coro; |
1255 | |
1255 | |
1256 | coro->hv = hv = newHV (); |
1256 | coro->hv = hv = newHV (); |
1257 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1257 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1258 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1258 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1259 | |
1259 | |
… | |
… | |
1368 | void |
1368 | void |
1369 | list () |
1369 | list () |
1370 | PPCODE: |
1370 | PPCODE: |
1371 | { |
1371 | { |
1372 | struct coro *coro; |
1372 | struct coro *coro; |
1373 | for (coro = first; coro; coro = coro->next) |
1373 | for (coro = coro_first; coro; coro = coro->next) |
1374 | if (coro->hv) |
1374 | if (coro->hv) |
1375 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1375 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1376 | } |
1376 | } |
1377 | |
1377 | |
1378 | void |
1378 | void |