… | |
… | |
188 | # include "state.h" |
188 | # include "state.h" |
189 | #undef VAR |
189 | #undef VAR |
190 | |
190 | |
191 | /* coro process data */ |
191 | /* coro process data */ |
192 | int prio; |
192 | int prio; |
|
|
193 | |
|
|
194 | /* linked list */ |
|
|
195 | struct coro *next, *prev; |
|
|
196 | HV *hv; /* the perl hash associated with this coro, if any */ |
193 | }; |
197 | }; |
194 | |
198 | |
195 | typedef struct coro *Coro__State; |
199 | typedef struct coro *Coro__State; |
196 | typedef struct coro *Coro__State_or_hashref; |
200 | typedef struct coro *Coro__State_or_hashref; |
197 | |
201 | |
… | |
… | |
206 | |
210 | |
207 | /* for Coro.pm */ |
211 | /* for Coro.pm */ |
208 | static SV *coro_current; |
212 | static SV *coro_current; |
209 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
210 | static int coro_nready; |
214 | static int coro_nready; |
|
|
215 | static struct coro *coro_first; |
211 | |
216 | |
212 | /** lowlevel stuff **********************************************************/ |
217 | /** lowlevel stuff **********************************************************/ |
213 | |
218 | |
214 | static AV * |
219 | static AV * |
215 | coro_clone_padlist (pTHX_ CV *cv) |
220 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
374 | CvPADLIST (cv) = (AV *)POPs; |
379 | CvPADLIST (cv) = (AV *)POPs; |
375 | } |
380 | } |
376 | |
381 | |
377 | PUTBACK; |
382 | PUTBACK; |
378 | } |
383 | } |
379 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
380 | } |
384 | } |
381 | |
385 | |
382 | static void |
386 | static void |
383 | save_perl (pTHX_ Coro__State c) |
387 | save_perl (pTHX_ Coro__State c) |
384 | { |
388 | { |
385 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
386 | { |
389 | { |
387 | dSP; |
390 | dSP; |
388 | I32 cxix = cxstack_ix; |
391 | I32 cxix = cxstack_ix; |
389 | PERL_CONTEXT *ccstk = cxstack; |
392 | PERL_CONTEXT *ccstk = cxstack; |
390 | PERL_SI *top_si = PL_curstackinfo; |
393 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
401 | { |
404 | { |
402 | while (cxix >= 0) |
405 | while (cxix >= 0) |
403 | { |
406 | { |
404 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
407 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
405 | |
408 | |
406 | if (CxTYPE (cx) == CXt_SUB) |
409 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
407 | { |
410 | { |
408 | CV *cv = cx->blk_sub.cv; |
411 | CV *cv = cx->blk_sub.cv; |
409 | |
412 | |
410 | if (CvDEPTH (cv)) |
413 | if (CvDEPTH (cv)) |
411 | { |
414 | { |
… | |
… | |
452 | # define coro_init_stacks init_stacks |
455 | # define coro_init_stacks init_stacks |
453 | #else |
456 | #else |
454 | static void |
457 | static void |
455 | coro_init_stacks (pTHX) |
458 | coro_init_stacks (pTHX) |
456 | { |
459 | { |
457 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
460 | PL_curstackinfo = new_stackinfo(64, 6); |
458 | PL_curstackinfo->si_type = PERLSI_MAIN; |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
459 | PL_curstack = PL_curstackinfo->si_stack; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
460 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
461 | |
464 | |
462 | PL_stack_base = AvARRAY(PL_curstack); |
465 | PL_stack_base = AvARRAY(PL_curstack); |
463 | PL_stack_sp = PL_stack_base; |
466 | PL_stack_sp = PL_stack_base; |
464 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
465 | |
468 | |
466 | New(50,PL_tmps_stack,128,SV*); |
469 | New(50,PL_tmps_stack,64,SV*); |
467 | PL_tmps_floor = -1; |
470 | PL_tmps_floor = -1; |
468 | PL_tmps_ix = -1; |
471 | PL_tmps_ix = -1; |
469 | PL_tmps_max = 128; |
472 | PL_tmps_max = 64; |
470 | |
473 | |
471 | New(54,PL_markstack,32,I32); |
474 | New(54,PL_markstack,16,I32); |
472 | PL_markstack_ptr = PL_markstack; |
475 | PL_markstack_ptr = PL_markstack; |
473 | PL_markstack_max = PL_markstack + 32; |
476 | PL_markstack_max = PL_markstack + 16; |
474 | |
477 | |
475 | #ifdef SET_MARK_OFFSET |
478 | #ifdef SET_MARK_OFFSET |
476 | SET_MARK_OFFSET; |
479 | SET_MARK_OFFSET; |
477 | #endif |
480 | #endif |
478 | |
481 | |
479 | New(54,PL_scopestack,32,I32); |
482 | New(54,PL_scopestack,16,I32); |
480 | PL_scopestack_ix = 0; |
483 | PL_scopestack_ix = 0; |
481 | PL_scopestack_max = 32; |
484 | PL_scopestack_max = 16; |
482 | |
485 | |
483 | New(54,PL_savestack,64,ANY); |
486 | New(54,PL_savestack,64,ANY); |
484 | PL_savestack_ix = 0; |
487 | PL_savestack_ix = 0; |
485 | PL_savestack_max = 64; |
488 | PL_savestack_max = 64; |
486 | |
489 | |
487 | #if !PERL_VERSION_ATLEAST (5,9,0) |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
488 | New(54,PL_retstack,16,OP*); |
491 | New(54,PL_retstack,4,OP*); |
489 | PL_retstack_ix = 0; |
492 | PL_retstack_ix = 0; |
490 | PL_retstack_max = 16; |
493 | PL_retstack_max = 4; |
491 | #endif |
494 | #endif |
492 | } |
495 | } |
493 | #endif |
496 | #endif |
494 | |
497 | |
495 | /* |
498 | /* |
… | |
… | |
535 | Safefree (PL_scopestack); |
538 | Safefree (PL_scopestack); |
536 | Safefree (PL_savestack); |
539 | Safefree (PL_savestack); |
537 | #if !PERL_VERSION_ATLEAST (5,9,0) |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
538 | Safefree (PL_retstack); |
541 | Safefree (PL_retstack); |
539 | #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; |
540 | } |
574 | } |
541 | |
575 | |
542 | /** coroutine stack handling ************************************************/ |
576 | /** coroutine stack handling ************************************************/ |
543 | |
577 | |
544 | static void |
578 | static void |
… | |
… | |
588 | coro_mortal = 0; |
622 | coro_mortal = 0; |
589 | } |
623 | } |
590 | } |
624 | } |
591 | |
625 | |
592 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
626 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
593 | /* _cctx_init shoukld be careful, as it could be called at almost any time */ |
627 | /* _cctx_init should be careful, as it could be called at almost any time */ |
594 | /* during execution of a pelr program */ |
628 | /* during execution of a perl program */ |
595 | static void NOINLINE |
629 | static void NOINLINE |
596 | prepare_cctx (pTHX_ coro_cctx *cctx) |
630 | prepare_cctx (pTHX_ coro_cctx *cctx) |
597 | { |
631 | { |
598 | dSP; |
632 | dSP; |
599 | LOGOP myop; |
633 | LOGOP myop; |
… | |
… | |
877 | struct coro temp; |
911 | struct coro temp; |
878 | |
912 | |
879 | assert (!(coro->flags & CF_RUNNING)); |
913 | assert (!(coro->flags & CF_RUNNING)); |
880 | |
914 | |
881 | Zero (&temp, 1, struct coro); |
915 | Zero (&temp, 1, struct coro); |
882 | temp.save = CORO_SAVE_DEF; |
916 | temp.save = CORO_SAVE_ALL; |
883 | |
917 | |
884 | if (coro->flags & CF_RUNNING) |
918 | if (coro->flags & CF_RUNNING) |
885 | croak ("FATAL: tried to destroy currently running coroutine"); |
919 | croak ("FATAL: tried to destroy currently running coroutine"); |
886 | |
920 | |
887 | save_perl (aTHX_ &temp); |
921 | save_perl (aTHX_ &temp); |
… | |
… | |
895 | } |
929 | } |
896 | |
930 | |
897 | cctx_destroy (coro->cctx); |
931 | cctx_destroy (coro->cctx); |
898 | SvREFCNT_dec (coro->args); |
932 | SvREFCNT_dec (coro->args); |
899 | |
933 | |
|
|
934 | if (coro->next) coro->next->prev = coro->prev; |
|
|
935 | if (coro->prev) coro->prev->next = coro->next; |
|
|
936 | if (coro == coro_first) coro_first = coro->next; |
|
|
937 | |
900 | return 1; |
938 | return 1; |
901 | } |
939 | } |
902 | |
940 | |
903 | static int |
941 | static int |
904 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
942 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
905 | { |
943 | { |
906 | struct coro *coro = (struct coro *)mg->mg_ptr; |
944 | struct coro *coro = (struct coro *)mg->mg_ptr; |
907 | mg->mg_ptr = 0; |
945 | mg->mg_ptr = 0; |
|
|
946 | |
|
|
947 | coro->hv = 0; |
908 | |
948 | |
909 | if (--coro->refcnt < 0) |
949 | if (--coro->refcnt < 0) |
910 | { |
950 | { |
911 | coro_state_destroy (aTHX_ coro); |
951 | coro_state_destroy (aTHX_ coro); |
912 | Safefree (coro); |
952 | Safefree (coro); |
… | |
… | |
1209 | Newz (0, coro, 1, struct coro); |
1249 | Newz (0, coro, 1, struct coro); |
1210 | coro->args = newAV (); |
1250 | coro->args = newAV (); |
1211 | coro->save = CORO_SAVE_DEF; |
1251 | coro->save = CORO_SAVE_DEF; |
1212 | coro->flags = CF_NEW; |
1252 | coro->flags = CF_NEW; |
1213 | |
1253 | |
|
|
1254 | if (coro_first) coro_first->prev = coro; |
|
|
1255 | coro->next = coro_first; |
|
|
1256 | coro_first = coro; |
|
|
1257 | |
1214 | hv = newHV (); |
1258 | coro->hv = hv = newHV (); |
1215 | 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; |
1216 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1260 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1217 | |
1261 | |
1218 | for (i = 1; i < items; i++) |
1262 | for (i = 1; i < items; i++) |
1219 | av_push (coro->args, newSVsv (ST (i))); |
1263 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1321 | CODE: |
1365 | CODE: |
1322 | RETVAL = cctx_idle; |
1366 | RETVAL = cctx_idle; |
1323 | OUTPUT: |
1367 | OUTPUT: |
1324 | RETVAL |
1368 | RETVAL |
1325 | |
1369 | |
|
|
1370 | void |
|
|
1371 | list () |
|
|
1372 | PPCODE: |
|
|
1373 | { |
|
|
1374 | struct coro *coro; |
|
|
1375 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1376 | if (coro->hv) |
|
|
1377 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1378 | } |
|
|
1379 | |
|
|
1380 | void |
|
|
1381 | _eval (Coro::State coro, SV *coderef) |
|
|
1382 | CODE: |
|
|
1383 | { |
|
|
1384 | if (coro->mainstack) |
|
|
1385 | { |
|
|
1386 | struct coro temp; |
|
|
1387 | Zero (&temp, 1, struct coro); |
|
|
1388 | temp.save = CORO_SAVE_ALL; |
|
|
1389 | |
|
|
1390 | if (!(coro->flags & CF_RUNNING)) |
|
|
1391 | { |
|
|
1392 | save_perl (aTHX_ &temp); |
|
|
1393 | load_perl (aTHX_ coro); |
|
|
1394 | } |
|
|
1395 | |
|
|
1396 | { |
|
|
1397 | dSP; |
|
|
1398 | ENTER; |
|
|
1399 | SAVETMPS; |
|
|
1400 | PUSHMARK (SP); |
|
|
1401 | PUTBACK; |
|
|
1402 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1403 | SPAGAIN; |
|
|
1404 | FREETMPS; |
|
|
1405 | LEAVE; |
|
|
1406 | PUTBACK; |
|
|
1407 | } |
|
|
1408 | |
|
|
1409 | if (!(coro->flags & CF_RUNNING)) |
|
|
1410 | { |
|
|
1411 | save_perl (aTHX_ coro); |
|
|
1412 | load_perl (aTHX_ &temp); |
|
|
1413 | } |
|
|
1414 | } |
|
|
1415 | } |
|
|
1416 | |
|
|
1417 | SV * |
|
|
1418 | is_ready (Coro::State coro) |
|
|
1419 | PROTOTYPE: $ |
|
|
1420 | ALIAS: |
|
|
1421 | is_ready = CF_READY |
|
|
1422 | is_running = CF_RUNNING |
|
|
1423 | is_new = CF_NEW |
|
|
1424 | is_destroyed = CF_DESTROYED |
|
|
1425 | CODE: |
|
|
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); |
|
|
1435 | OUTPUT: |
|
|
1436 | RETVAL |
|
|
1437 | |
|
|
1438 | |
1326 | MODULE = Coro::State PACKAGE = Coro |
1439 | MODULE = Coro::State PACKAGE = Coro |
1327 | |
1440 | |
1328 | BOOT: |
1441 | BOOT: |
1329 | { |
1442 | { |
1330 | int i; |
1443 | int i; |
… | |
… | |
1397 | CODE: |
1510 | CODE: |
1398 | RETVAL = boolSV (api_ready (self)); |
1511 | RETVAL = boolSV (api_ready (self)); |
1399 | OUTPUT: |
1512 | OUTPUT: |
1400 | RETVAL |
1513 | RETVAL |
1401 | |
1514 | |
1402 | SV * |
|
|
1403 | is_ready (SV *self) |
|
|
1404 | PROTOTYPE: $ |
|
|
1405 | CODE: |
|
|
1406 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1407 | OUTPUT: |
|
|
1408 | RETVAL |
|
|
1409 | |
|
|
1410 | int |
1515 | int |
1411 | nready (...) |
1516 | nready (...) |
1412 | PROTOTYPE: |
1517 | PROTOTYPE: |
1413 | CODE: |
1518 | CODE: |
1414 | RETVAL = coro_nready; |
1519 | RETVAL = coro_nready; |