… | |
… | |
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 |
… | |
… | |
132 | static struct CoroAPI coroapi; |
131 | static struct CoroAPI coroapi; |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
134 | static JMPENV *main_top_env; |
133 | static JMPENV *main_top_env; |
135 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
136 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
136 | |
|
|
137 | /* async_pool helper stuff */ |
|
|
138 | static SV *sv_pool_rss; |
|
|
139 | static SV *sv_pool_size; |
|
|
140 | static AV *av_async_pool; |
137 | |
141 | |
138 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
139 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
140 | |
144 | |
141 | /* this is a structure representing a c-level coroutine */ |
145 | /* this is a structure representing a c-level coroutine */ |
… | |
… | |
188 | # include "state.h" |
192 | # include "state.h" |
189 | #undef VAR |
193 | #undef VAR |
190 | |
194 | |
191 | /* coro process data */ |
195 | /* coro process data */ |
192 | int prio; |
196 | int prio; |
|
|
197 | |
|
|
198 | /* linked list */ |
|
|
199 | struct coro *next, *prev; |
|
|
200 | HV *hv; /* the perl hash associated with this coro, if any */ |
193 | }; |
201 | }; |
194 | |
202 | |
195 | typedef struct coro *Coro__State; |
203 | typedef struct coro *Coro__State; |
196 | typedef struct coro *Coro__State_or_hashref; |
204 | typedef struct coro *Coro__State_or_hashref; |
197 | |
205 | |
… | |
… | |
206 | |
214 | |
207 | /* for Coro.pm */ |
215 | /* for Coro.pm */ |
208 | static SV *coro_current; |
216 | static SV *coro_current; |
209 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
217 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
210 | static int coro_nready; |
218 | static int coro_nready; |
|
|
219 | static struct coro *coro_first; |
211 | |
220 | |
212 | /** lowlevel stuff **********************************************************/ |
221 | /** lowlevel stuff **********************************************************/ |
213 | |
222 | |
214 | static AV * |
223 | static AV * |
215 | coro_clone_padlist (pTHX_ CV *cv) |
224 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
374 | CvPADLIST (cv) = (AV *)POPs; |
383 | CvPADLIST (cv) = (AV *)POPs; |
375 | } |
384 | } |
376 | |
385 | |
377 | PUTBACK; |
386 | PUTBACK; |
378 | } |
387 | } |
379 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
380 | } |
388 | } |
381 | |
389 | |
382 | static void |
390 | static void |
383 | save_perl (pTHX_ Coro__State c) |
391 | save_perl (pTHX_ Coro__State c) |
384 | { |
392 | { |
385 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
386 | { |
393 | { |
387 | dSP; |
394 | dSP; |
388 | I32 cxix = cxstack_ix; |
395 | I32 cxix = cxstack_ix; |
389 | PERL_CONTEXT *ccstk = cxstack; |
396 | PERL_CONTEXT *ccstk = cxstack; |
390 | PERL_SI *top_si = PL_curstackinfo; |
397 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
392 | /* |
399 | /* |
393 | * the worst thing you can imagine happens first - we have to save |
400 | * the worst thing you can imagine happens first - we have to save |
394 | * (and reinitialize) all cv's in the whole callchain :( |
401 | * (and reinitialize) all cv's in the whole callchain :( |
395 | */ |
402 | */ |
396 | |
403 | |
397 | EXTEND (SP, 3 + 1); |
|
|
398 | PUSHs (Nullsv); |
404 | XPUSHs (Nullsv); |
399 | /* this loop was inspired by pp_caller */ |
405 | /* this loop was inspired by pp_caller */ |
400 | for (;;) |
406 | for (;;) |
401 | { |
407 | { |
402 | while (cxix >= 0) |
408 | while (cxix >= 0) |
403 | { |
409 | { |
404 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
410 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
405 | |
411 | |
406 | if (CxTYPE (cx) == CXt_SUB) |
412 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
407 | { |
413 | { |
408 | CV *cv = cx->blk_sub.cv; |
414 | CV *cv = cx->blk_sub.cv; |
409 | |
415 | |
410 | if (CvDEPTH (cv)) |
416 | if (CvDEPTH (cv)) |
411 | { |
417 | { |
… | |
… | |
422 | |
428 | |
423 | if (top_si->si_type == PERLSI_MAIN) |
429 | if (top_si->si_type == PERLSI_MAIN) |
424 | break; |
430 | break; |
425 | |
431 | |
426 | top_si = top_si->si_prev; |
432 | top_si = top_si->si_prev; |
427 | ccstk = top_si->si_cxstack; |
433 | ccstk = top_si->si_cxstack; |
428 | cxix = top_si->si_cxix; |
434 | cxix = top_si->si_cxix; |
429 | } |
435 | } |
430 | |
436 | |
431 | PUTBACK; |
437 | PUTBACK; |
432 | } |
438 | } |
433 | |
439 | |
… | |
… | |
452 | # define coro_init_stacks init_stacks |
458 | # define coro_init_stacks init_stacks |
453 | #else |
459 | #else |
454 | static void |
460 | static void |
455 | coro_init_stacks (pTHX) |
461 | coro_init_stacks (pTHX) |
456 | { |
462 | { |
457 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
463 | PL_curstackinfo = new_stackinfo(64, 6); |
458 | PL_curstackinfo->si_type = PERLSI_MAIN; |
464 | PL_curstackinfo->si_type = PERLSI_MAIN; |
459 | PL_curstack = PL_curstackinfo->si_stack; |
465 | PL_curstack = PL_curstackinfo->si_stack; |
460 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
466 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
461 | |
467 | |
462 | PL_stack_base = AvARRAY(PL_curstack); |
468 | PL_stack_base = AvARRAY(PL_curstack); |
463 | PL_stack_sp = PL_stack_base; |
469 | PL_stack_sp = PL_stack_base; |
464 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
470 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
465 | |
471 | |
466 | New(50,PL_tmps_stack,128,SV*); |
472 | New(50,PL_tmps_stack,64,SV*); |
467 | PL_tmps_floor = -1; |
473 | PL_tmps_floor = -1; |
468 | PL_tmps_ix = -1; |
474 | PL_tmps_ix = -1; |
469 | PL_tmps_max = 128; |
475 | PL_tmps_max = 64; |
470 | |
476 | |
471 | New(54,PL_markstack,32,I32); |
477 | New(54,PL_markstack,16,I32); |
472 | PL_markstack_ptr = PL_markstack; |
478 | PL_markstack_ptr = PL_markstack; |
473 | PL_markstack_max = PL_markstack + 32; |
479 | PL_markstack_max = PL_markstack + 16; |
474 | |
480 | |
475 | #ifdef SET_MARK_OFFSET |
481 | #ifdef SET_MARK_OFFSET |
476 | SET_MARK_OFFSET; |
482 | SET_MARK_OFFSET; |
477 | #endif |
483 | #endif |
478 | |
484 | |
479 | New(54,PL_scopestack,32,I32); |
485 | New(54,PL_scopestack,16,I32); |
480 | PL_scopestack_ix = 0; |
486 | PL_scopestack_ix = 0; |
481 | PL_scopestack_max = 32; |
487 | PL_scopestack_max = 16; |
482 | |
488 | |
483 | New(54,PL_savestack,64,ANY); |
489 | New(54,PL_savestack,64,ANY); |
484 | PL_savestack_ix = 0; |
490 | PL_savestack_ix = 0; |
485 | PL_savestack_max = 64; |
491 | PL_savestack_max = 64; |
486 | |
492 | |
487 | #if !PERL_VERSION_ATLEAST (5,9,0) |
493 | #if !PERL_VERSION_ATLEAST (5,9,0) |
488 | New(54,PL_retstack,16,OP*); |
494 | New(54,PL_retstack,4,OP*); |
489 | PL_retstack_ix = 0; |
495 | PL_retstack_ix = 0; |
490 | PL_retstack_max = 16; |
496 | PL_retstack_max = 4; |
491 | #endif |
497 | #endif |
492 | } |
498 | } |
493 | #endif |
499 | #endif |
494 | |
500 | |
495 | /* |
501 | /* |
… | |
… | |
535 | Safefree (PL_scopestack); |
541 | Safefree (PL_scopestack); |
536 | Safefree (PL_savestack); |
542 | Safefree (PL_savestack); |
537 | #if !PERL_VERSION_ATLEAST (5,9,0) |
543 | #if !PERL_VERSION_ATLEAST (5,9,0) |
538 | Safefree (PL_retstack); |
544 | Safefree (PL_retstack); |
539 | #endif |
545 | #endif |
|
|
546 | } |
|
|
547 | |
|
|
548 | static size_t |
|
|
549 | coro_rss (struct coro *coro) |
|
|
550 | { |
|
|
551 | size_t rss = sizeof (coro); |
|
|
552 | |
|
|
553 | if (coro->mainstack) |
|
|
554 | { |
|
|
555 | if (coro->flags & CF_RUNNING) |
|
|
556 | { |
|
|
557 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
558 | # include "state.h" |
|
|
559 | #undef VAR |
|
|
560 | } |
|
|
561 | |
|
|
562 | rss += sizeof (coro->curstackinfo); |
|
|
563 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
564 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
565 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
566 | rss += coro->tmps_max * sizeof (SV *); |
|
|
567 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
568 | rss += coro->scopestack_max * sizeof (I32); |
|
|
569 | rss += coro->savestack_max * sizeof (ANY); |
|
|
570 | |
|
|
571 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
572 | rss += coro->retstack_max * sizeof (OP *); |
|
|
573 | #endif |
|
|
574 | } |
|
|
575 | |
|
|
576 | return rss; |
540 | } |
577 | } |
541 | |
578 | |
542 | /** coroutine stack handling ************************************************/ |
579 | /** coroutine stack handling ************************************************/ |
543 | |
580 | |
544 | static void |
581 | static void |
… | |
… | |
567 | Zero (&myop, 1, LOGOP); |
604 | Zero (&myop, 1, LOGOP); |
568 | myop.op_next = Nullop; |
605 | myop.op_next = Nullop; |
569 | myop.op_flags = OPf_WANT_VOID; |
606 | myop.op_flags = OPf_WANT_VOID; |
570 | |
607 | |
571 | PUSHMARK (SP); |
608 | PUSHMARK (SP); |
572 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
609 | XPUSHs (av_shift (GvAV (PL_defgv))); |
573 | PUTBACK; |
610 | PUTBACK; |
574 | PL_op = (OP *)&myop; |
611 | PL_op = (OP *)&myop; |
575 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
612 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
576 | SPAGAIN; |
613 | SPAGAIN; |
577 | } |
614 | } |
… | |
… | |
588 | coro_mortal = 0; |
625 | coro_mortal = 0; |
589 | } |
626 | } |
590 | } |
627 | } |
591 | |
628 | |
592 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
629 | /* 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 */ |
630 | /* _cctx_init should be careful, as it could be called at almost any time */ |
594 | /* during execution of a pelr program */ |
631 | /* during execution of a perl program */ |
595 | static void NOINLINE |
632 | static void NOINLINE |
596 | prepare_cctx (pTHX_ coro_cctx *cctx) |
633 | prepare_cctx (pTHX_ coro_cctx *cctx) |
597 | { |
634 | { |
598 | dSP; |
635 | dSP; |
599 | LOGOP myop; |
636 | LOGOP myop; |
… | |
… | |
630 | |
667 | |
631 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
668 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
632 | PL_restartop = PL_op; |
669 | PL_restartop = PL_op; |
633 | perl_run (PL_curinterp); |
670 | perl_run (PL_curinterp); |
634 | |
671 | |
|
|
672 | /* |
635 | /* If perl-run returns we assume exit() was being called, which */ |
673 | * If perl-run returns we assume exit() was being called or the coro |
636 | /* seems to be the only valid (non-bug) reason for perl_run to return. */ |
674 | * fell off the end, which seems to be the only valid (non-bug) |
637 | /* We try to exit by jumping to the bootstrap-time "top" top_env, as */ |
675 | * reason for perl_run to return. We try to exit by jumping to the |
638 | /* we cannot restore the "main" coroutine as Coro has no such concept */ |
676 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
677 | * coroutine as Coro has no such concept |
|
|
678 | */ |
639 | PL_top_env = main_top_env; |
679 | PL_top_env = main_top_env; |
640 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
680 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
641 | } |
681 | } |
642 | |
682 | |
643 | static coro_cctx * |
683 | static coro_cctx * |
… | |
… | |
874 | struct coro temp; |
914 | struct coro temp; |
875 | |
915 | |
876 | assert (!(coro->flags & CF_RUNNING)); |
916 | assert (!(coro->flags & CF_RUNNING)); |
877 | |
917 | |
878 | Zero (&temp, 1, struct coro); |
918 | Zero (&temp, 1, struct coro); |
879 | temp.save = CORO_SAVE_DEF; |
919 | temp.save = CORO_SAVE_ALL; |
880 | |
920 | |
881 | if (coro->flags & CF_RUNNING) |
921 | if (coro->flags & CF_RUNNING) |
882 | croak ("FATAL: tried to destroy currently running coroutine"); |
922 | croak ("FATAL: tried to destroy currently running coroutine"); |
883 | |
923 | |
884 | save_perl (aTHX_ &temp); |
924 | save_perl (aTHX_ &temp); |
… | |
… | |
892 | } |
932 | } |
893 | |
933 | |
894 | cctx_destroy (coro->cctx); |
934 | cctx_destroy (coro->cctx); |
895 | SvREFCNT_dec (coro->args); |
935 | SvREFCNT_dec (coro->args); |
896 | |
936 | |
|
|
937 | if (coro->next) coro->next->prev = coro->prev; |
|
|
938 | if (coro->prev) coro->prev->next = coro->next; |
|
|
939 | if (coro == coro_first) coro_first = coro->next; |
|
|
940 | |
897 | return 1; |
941 | return 1; |
898 | } |
942 | } |
899 | |
943 | |
900 | static int |
944 | static int |
901 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
945 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
902 | { |
946 | { |
903 | struct coro *coro = (struct coro *)mg->mg_ptr; |
947 | struct coro *coro = (struct coro *)mg->mg_ptr; |
904 | mg->mg_ptr = 0; |
948 | mg->mg_ptr = 0; |
|
|
949 | |
|
|
950 | coro->hv = 0; |
905 | |
951 | |
906 | if (--coro->refcnt < 0) |
952 | if (--coro->refcnt < 0) |
907 | { |
953 | { |
908 | coro_state_destroy (aTHX_ coro); |
954 | coro_state_destroy (aTHX_ coro); |
909 | Safefree (coro); |
955 | Safefree (coro); |
… | |
… | |
1206 | Newz (0, coro, 1, struct coro); |
1252 | Newz (0, coro, 1, struct coro); |
1207 | coro->args = newAV (); |
1253 | coro->args = newAV (); |
1208 | coro->save = CORO_SAVE_DEF; |
1254 | coro->save = CORO_SAVE_DEF; |
1209 | coro->flags = CF_NEW; |
1255 | coro->flags = CF_NEW; |
1210 | |
1256 | |
|
|
1257 | if (coro_first) coro_first->prev = coro; |
|
|
1258 | coro->next = coro_first; |
|
|
1259 | coro_first = coro; |
|
|
1260 | |
1211 | hv = newHV (); |
1261 | coro->hv = hv = newHV (); |
1212 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1262 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1213 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1263 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1214 | |
1264 | |
1215 | for (i = 1; i < items; i++) |
1265 | for (i = 1; i < items; i++) |
1216 | av_push (coro->args, newSVsv (ST (i))); |
1266 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1318 | CODE: |
1368 | CODE: |
1319 | RETVAL = cctx_idle; |
1369 | RETVAL = cctx_idle; |
1320 | OUTPUT: |
1370 | OUTPUT: |
1321 | RETVAL |
1371 | RETVAL |
1322 | |
1372 | |
|
|
1373 | void |
|
|
1374 | list () |
|
|
1375 | PPCODE: |
|
|
1376 | { |
|
|
1377 | struct coro *coro; |
|
|
1378 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1379 | if (coro->hv) |
|
|
1380 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1381 | } |
|
|
1382 | |
|
|
1383 | void |
|
|
1384 | call (Coro::State coro, SV *coderef) |
|
|
1385 | ALIAS: |
|
|
1386 | eval = 1 |
|
|
1387 | CODE: |
|
|
1388 | { |
|
|
1389 | if (coro->mainstack) |
|
|
1390 | { |
|
|
1391 | struct coro temp; |
|
|
1392 | Zero (&temp, 1, struct coro); |
|
|
1393 | temp.save = CORO_SAVE_ALL; |
|
|
1394 | |
|
|
1395 | if (!(coro->flags & CF_RUNNING)) |
|
|
1396 | { |
|
|
1397 | save_perl (aTHX_ &temp); |
|
|
1398 | load_perl (aTHX_ coro); |
|
|
1399 | } |
|
|
1400 | |
|
|
1401 | { |
|
|
1402 | dSP; |
|
|
1403 | ENTER; |
|
|
1404 | SAVETMPS; |
|
|
1405 | PUSHMARK (SP); |
|
|
1406 | PUTBACK; |
|
|
1407 | if (ix) |
|
|
1408 | eval_sv (coderef, 0); |
|
|
1409 | else |
|
|
1410 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1411 | SPAGAIN; |
|
|
1412 | FREETMPS; |
|
|
1413 | LEAVE; |
|
|
1414 | PUTBACK; |
|
|
1415 | } |
|
|
1416 | |
|
|
1417 | if (!(coro->flags & CF_RUNNING)) |
|
|
1418 | { |
|
|
1419 | save_perl (aTHX_ coro); |
|
|
1420 | load_perl (aTHX_ &temp); |
|
|
1421 | } |
|
|
1422 | } |
|
|
1423 | } |
|
|
1424 | |
|
|
1425 | SV * |
|
|
1426 | is_ready (Coro::State coro) |
|
|
1427 | PROTOTYPE: $ |
|
|
1428 | ALIAS: |
|
|
1429 | is_ready = CF_READY |
|
|
1430 | is_running = CF_RUNNING |
|
|
1431 | is_new = CF_NEW |
|
|
1432 | is_destroyed = CF_DESTROYED |
|
|
1433 | CODE: |
|
|
1434 | RETVAL = boolSV (coro->flags & ix); |
|
|
1435 | OUTPUT: |
|
|
1436 | RETVAL |
|
|
1437 | |
|
|
1438 | SV * |
|
|
1439 | has_stack (Coro::State coro) |
|
|
1440 | PROTOTYPE: $ |
|
|
1441 | CODE: |
|
|
1442 | RETVAL = boolSV (!!coro->cctx); |
|
|
1443 | OUTPUT: |
|
|
1444 | RETVAL |
|
|
1445 | |
|
|
1446 | IV |
|
|
1447 | rss (Coro::State coro) |
|
|
1448 | PROTOTYPE: $ |
|
|
1449 | CODE: |
|
|
1450 | RETVAL = coro_rss (coro); |
|
|
1451 | OUTPUT: |
|
|
1452 | RETVAL |
|
|
1453 | |
|
|
1454 | |
1323 | MODULE = Coro::State PACKAGE = Coro |
1455 | MODULE = Coro::State PACKAGE = Coro |
1324 | |
1456 | |
1325 | BOOT: |
1457 | BOOT: |
1326 | { |
1458 | { |
1327 | int i; |
1459 | int i; |
|
|
1460 | |
|
|
1461 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1462 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1463 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1464 | |
|
|
1465 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1466 | SvREADONLY_on (coro_current); |
1328 | |
1467 | |
1329 | coro_stash = gv_stashpv ("Coro", TRUE); |
1468 | coro_stash = gv_stashpv ("Coro", TRUE); |
1330 | |
1469 | |
1331 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1470 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1332 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1471 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1333 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1472 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1334 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1473 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1335 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1474 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1336 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1475 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1337 | |
|
|
1338 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1339 | SvREADONLY_on (coro_current); |
|
|
1340 | |
1476 | |
1341 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1477 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1342 | coro_ready[i] = newAV (); |
1478 | coro_ready[i] = newAV (); |
1343 | |
1479 | |
1344 | { |
1480 | { |
… | |
… | |
1394 | CODE: |
1530 | CODE: |
1395 | RETVAL = boolSV (api_ready (self)); |
1531 | RETVAL = boolSV (api_ready (self)); |
1396 | OUTPUT: |
1532 | OUTPUT: |
1397 | RETVAL |
1533 | RETVAL |
1398 | |
1534 | |
1399 | SV * |
|
|
1400 | is_ready (SV *self) |
|
|
1401 | PROTOTYPE: $ |
|
|
1402 | CODE: |
|
|
1403 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1404 | OUTPUT: |
|
|
1405 | RETVAL |
|
|
1406 | |
|
|
1407 | int |
1535 | int |
1408 | nready (...) |
1536 | nready (...) |
1409 | PROTOTYPE: |
1537 | PROTOTYPE: |
1410 | CODE: |
1538 | CODE: |
1411 | RETVAL = coro_nready; |
1539 | RETVAL = coro_nready; |
1412 | OUTPUT: |
1540 | OUTPUT: |
1413 | RETVAL |
1541 | RETVAL |
1414 | |
1542 | |
|
|
1543 | # for async_pool speedup |
|
|
1544 | void |
|
|
1545 | _pool_1 (SV *cb) |
|
|
1546 | CODE: |
|
|
1547 | { |
|
|
1548 | int i, len; |
|
|
1549 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1550 | AV *defav = GvAV (PL_defgv); |
|
|
1551 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1552 | AV *invoke_av; |
|
|
1553 | |
|
|
1554 | if (!invoke) |
|
|
1555 | croak ("\3terminate\2\n"); |
|
|
1556 | |
|
|
1557 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1558 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1559 | |
|
|
1560 | invoke_av = (AV *)SvRV (invoke); |
|
|
1561 | len = av_len (invoke_av); |
|
|
1562 | |
|
|
1563 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1564 | |
|
|
1565 | if (len > 0) |
|
|
1566 | { |
|
|
1567 | av_fill (defav, len - 1); |
|
|
1568 | for (i = 0; i < len; ++i) |
|
|
1569 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1570 | } |
|
|
1571 | |
|
|
1572 | SvREFCNT_dec (invoke); |
|
|
1573 | } |
|
|
1574 | |
|
|
1575 | void |
|
|
1576 | _pool_2 (SV *cb) |
|
|
1577 | CODE: |
|
|
1578 | { |
|
|
1579 | struct coro *coro = SvSTATE (coro_current); |
|
|
1580 | |
|
|
1581 | sv_setsv (cb, &PL_sv_undef); |
|
|
1582 | |
|
|
1583 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1584 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1585 | croak ("\3terminate\2\n"); |
|
|
1586 | |
|
|
1587 | av_clear (GvAV (PL_defgv)); |
|
|
1588 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1589 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1590 | coro->save = CORO_SAVE_DEF; |
|
|
1591 | coro->prio = 0; |
|
|
1592 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1593 | } |
|
|
1594 | |
|
|
1595 | |
1415 | MODULE = Coro::State PACKAGE = Coro::AIO |
1596 | MODULE = Coro::State PACKAGE = Coro::AIO |
1416 | |
1597 | |
1417 | SV * |
1598 | SV * |
1418 | _get_state () |
1599 | _get_state () |
1419 | CODE: |
1600 | CODE: |