… | |
… | |
210 | |
210 | |
211 | /* for Coro.pm */ |
211 | /* for Coro.pm */ |
212 | static SV *coro_current; |
212 | static SV *coro_current; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
214 | static int coro_nready; |
214 | static int coro_nready; |
215 | static struct coro *first; |
215 | static struct coro *coro_first; |
216 | |
216 | |
217 | /** lowlevel stuff **********************************************************/ |
217 | /** lowlevel stuff **********************************************************/ |
218 | |
218 | |
219 | static AV * |
219 | static AV * |
220 | coro_clone_padlist (pTHX_ CV *cv) |
220 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
455 | # define coro_init_stacks init_stacks |
455 | # define coro_init_stacks init_stacks |
456 | #else |
456 | #else |
457 | static void |
457 | static void |
458 | coro_init_stacks (pTHX) |
458 | coro_init_stacks (pTHX) |
459 | { |
459 | { |
460 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
460 | PL_curstackinfo = new_stackinfo(64, 4); |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
464 | |
464 | |
465 | PL_stack_base = AvARRAY(PL_curstack); |
465 | PL_stack_base = AvARRAY(PL_curstack); |
466 | PL_stack_sp = PL_stack_base; |
466 | PL_stack_sp = PL_stack_base; |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
468 | |
468 | |
469 | New(50,PL_tmps_stack,128,SV*); |
469 | New(50,PL_tmps_stack,64,SV*); |
470 | PL_tmps_floor = -1; |
470 | PL_tmps_floor = -1; |
471 | PL_tmps_ix = -1; |
471 | PL_tmps_ix = -1; |
472 | PL_tmps_max = 128; |
472 | PL_tmps_max = 64; |
473 | |
473 | |
474 | New(54,PL_markstack,32,I32); |
474 | New(54,PL_markstack,16,I32); |
475 | PL_markstack_ptr = PL_markstack; |
475 | PL_markstack_ptr = PL_markstack; |
476 | PL_markstack_max = PL_markstack + 32; |
476 | PL_markstack_max = PL_markstack + 16; |
477 | |
477 | |
478 | #ifdef SET_MARK_OFFSET |
478 | #ifdef SET_MARK_OFFSET |
479 | SET_MARK_OFFSET; |
479 | SET_MARK_OFFSET; |
480 | #endif |
480 | #endif |
481 | |
481 | |
482 | New(54,PL_scopestack,32,I32); |
482 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
483 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 32; |
484 | PL_scopestack_max = 16; |
485 | |
485 | |
486 | New(54,PL_savestack,64,ANY); |
486 | New(54,PL_savestack,64,ANY); |
487 | PL_savestack_ix = 0; |
487 | PL_savestack_ix = 0; |
488 | PL_savestack_max = 64; |
488 | PL_savestack_max = 64; |
489 | |
489 | |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | New(54,PL_retstack,16,OP*); |
491 | New(54,PL_retstack,8,OP*); |
492 | PL_retstack_ix = 0; |
492 | PL_retstack_ix = 0; |
493 | PL_retstack_max = 16; |
493 | PL_retstack_max = 8; |
494 | #endif |
494 | #endif |
495 | } |
495 | } |
496 | #endif |
496 | #endif |
497 | |
497 | |
498 | /* |
498 | /* |
… | |
… | |
538 | Safefree (PL_scopestack); |
538 | Safefree (PL_scopestack); |
539 | Safefree (PL_savestack); |
539 | Safefree (PL_savestack); |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
541 | Safefree (PL_retstack); |
541 | Safefree (PL_retstack); |
542 | #endif |
542 | #endif |
|
|
543 | } |
|
|
544 | |
|
|
545 | static size_t |
|
|
546 | coro_rss (struct coro *coro) |
|
|
547 | { |
|
|
548 | size_t rss = sizeof (coro); |
|
|
549 | |
|
|
550 | if (coro->mainstack) |
|
|
551 | { |
|
|
552 | if (coro->flags & CF_RUNNING) |
|
|
553 | { |
|
|
554 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
555 | # include "state.h" |
|
|
556 | #undef VAR |
|
|
557 | } |
|
|
558 | |
|
|
559 | rss += sizeof (coro->curstackinfo); |
|
|
560 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
561 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
562 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
563 | rss += coro->tmps_max * sizeof (SV *); |
|
|
564 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
565 | rss += coro->scopestack_max * sizeof (I32); |
|
|
566 | rss += coro->savestack_max * sizeof (ANY); |
|
|
567 | |
|
|
568 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
569 | rss += coro->retstack_max * sizeof (OP *); |
|
|
570 | #endif |
|
|
571 | } |
|
|
572 | |
|
|
573 | return rss; |
543 | } |
574 | } |
544 | |
575 | |
545 | /** coroutine stack handling ************************************************/ |
576 | /** coroutine stack handling ************************************************/ |
546 | |
577 | |
547 | static void |
578 | static void |
… | |
… | |
900 | cctx_destroy (coro->cctx); |
931 | cctx_destroy (coro->cctx); |
901 | SvREFCNT_dec (coro->args); |
932 | SvREFCNT_dec (coro->args); |
902 | |
933 | |
903 | if (coro->next) coro->next->prev = coro->prev; |
934 | if (coro->next) coro->next->prev = coro->prev; |
904 | if (coro->prev) coro->prev->next = coro->next; |
935 | if (coro->prev) coro->prev->next = coro->next; |
905 | if (coro == first) first = coro->next; |
936 | if (coro == coro_first) coro_first = coro->next; |
906 | |
937 | |
907 | return 1; |
938 | return 1; |
908 | } |
939 | } |
909 | |
940 | |
910 | static int |
941 | static int |
… | |
… | |
1218 | Newz (0, coro, 1, struct coro); |
1249 | Newz (0, coro, 1, struct coro); |
1219 | coro->args = newAV (); |
1250 | coro->args = newAV (); |
1220 | coro->save = CORO_SAVE_DEF; |
1251 | coro->save = CORO_SAVE_DEF; |
1221 | coro->flags = CF_NEW; |
1252 | coro->flags = CF_NEW; |
1222 | |
1253 | |
1223 | if (first) first->prev = coro; |
1254 | if (coro_first) coro_first->prev = coro; |
1224 | coro->next = first; |
1255 | coro->next = coro_first; |
1225 | first = coro; |
1256 | coro_first = coro; |
1226 | |
1257 | |
1227 | coro->hv = hv = newHV (); |
1258 | coro->hv = hv = newHV (); |
1228 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1259 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1229 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1260 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1230 | |
1261 | |
… | |
… | |
1339 | void |
1370 | void |
1340 | list () |
1371 | list () |
1341 | PPCODE: |
1372 | PPCODE: |
1342 | { |
1373 | { |
1343 | struct coro *coro; |
1374 | struct coro *coro; |
1344 | for (coro = first; coro; coro = coro->next) |
1375 | for (coro = coro_first; coro; coro = coro->next) |
1345 | if (coro->hv) |
1376 | if (coro->hv) |
1346 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1377 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1347 | } |
1378 | } |
1348 | |
1379 | |
1349 | void |
1380 | void |
1350 | _eval (SV *coro_sv, SV *coderef) |
1381 | _eval (Coro::State coro, SV *coderef) |
1351 | CODE: |
1382 | CODE: |
1352 | { |
1383 | { |
1353 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1354 | if (coro->mainstack) |
1384 | if (coro->mainstack) |
1355 | { |
1385 | { |
1356 | struct coro temp; |
1386 | struct coro temp; |
1357 | Zero (&temp, 1, struct coro); |
1387 | Zero (&temp, 1, struct coro); |
1358 | temp.save = CORO_SAVE_ALL; |
1388 | temp.save = CORO_SAVE_ALL; |
… | |
… | |
1383 | } |
1413 | } |
1384 | } |
1414 | } |
1385 | } |
1415 | } |
1386 | |
1416 | |
1387 | SV * |
1417 | SV * |
1388 | is_ready (SV *coro_sv) |
1418 | is_ready (Coro::State coro) |
1389 | PROTOTYPE: $ |
1419 | PROTOTYPE: $ |
1390 | ALIAS: |
1420 | ALIAS: |
1391 | is_ready = CF_READY |
1421 | is_ready = CF_READY |
1392 | is_running = CF_RUNNING |
1422 | is_running = CF_RUNNING |
1393 | is_new = CF_NEW |
1423 | is_new = CF_NEW |
1394 | is_destroyed = CF_DESTROYED |
1424 | is_destroyed = CF_DESTROYED |
1395 | CODE: |
1425 | CODE: |
1396 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1397 | RETVAL = boolSV (coro->flags & ix); |
1426 | RETVAL = boolSV (coro->flags & ix); |
|
|
1427 | OUTPUT: |
|
|
1428 | RETVAL |
|
|
1429 | |
|
|
1430 | IV |
|
|
1431 | rss (Coro::State coro) |
|
|
1432 | PROTOTYPE: $ |
|
|
1433 | CODE: |
|
|
1434 | RETVAL = coro_rss (coro); |
1398 | OUTPUT: |
1435 | OUTPUT: |
1399 | RETVAL |
1436 | RETVAL |
1400 | |
1437 | |
1401 | |
1438 | |
1402 | MODULE = Coro::State PACKAGE = Coro |
1439 | MODULE = Coro::State PACKAGE = Coro |