… | |
… | |
16 | |
16 | |
17 | #ifdef WIN32 |
17 | #ifdef WIN32 |
18 | # undef setjmp |
18 | # undef setjmp |
19 | # undef longjmp |
19 | # undef longjmp |
20 | # undef _exit |
20 | # undef _exit |
21 | # define setjmp _setjmp // deep magic, don't ask |
21 | # define setjmp _setjmp /* deep magic */ |
22 | #else |
22 | #else |
23 | # include <inttypes.h> /* most portable stdint.h */ |
23 | # include <inttypes.h> /* most portable stdint.h */ |
24 | #endif |
24 | #endif |
25 | |
25 | |
26 | #ifdef HAVE_MMAP |
26 | #ifdef HAVE_MMAP |
… | |
… | |
120 | #endif |
120 | #endif |
121 | |
121 | |
122 | /* The next macros try to return the current stack pointer, in an as |
122 | /* The next macros try to return the current stack pointer, in an as |
123 | * portable way as possible. */ |
123 | * portable way as possible. */ |
124 | #if __GNUC__ >= 4 |
124 | #if __GNUC__ >= 4 |
|
|
125 | # define dSTACKLEVEL int stacklevel_dummy |
125 | # define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) |
126 | # define STACKLEVEL __builtin_frame_address (0) |
126 | #else |
127 | #else |
127 | # define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel |
128 | # define dSTACKLEVEL volatile void *stacklevel |
|
|
129 | # define STACKLEVEL ((void *)&stacklevel) |
128 | #endif |
130 | #endif |
129 | |
131 | |
130 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
132 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
131 | |
133 | |
132 | #if __GNUC__ >= 3 |
134 | #if __GNUC__ >= 3 |
… | |
… | |
143 | #define expect_true(expr) expect ((expr) != 0, 1) |
145 | #define expect_true(expr) expect ((expr) != 0, 1) |
144 | |
146 | |
145 | #define NOINLINE attribute ((noinline)) |
147 | #define NOINLINE attribute ((noinline)) |
146 | |
148 | |
147 | #include "CoroAPI.h" |
149 | #include "CoroAPI.h" |
|
|
150 | #define GCoroAPI (&coroapi) /* very sneaky */ |
148 | |
151 | |
149 | #ifdef USE_ITHREADS |
152 | #ifdef USE_ITHREADS |
150 | # if CORO_PTHREAD |
153 | # if CORO_PTHREAD |
151 | static void *coro_thx; |
154 | static void *coro_thx; |
152 | # endif |
155 | # endif |
… | |
… | |
243 | /* state data */ |
246 | /* state data */ |
244 | struct CoroSLF slf_frame; /* saved slf frame */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
245 | AV *mainstack; |
248 | AV *mainstack; |
246 | perl_slots *slot; /* basically the saved sp */ |
249 | perl_slots *slot; /* basically the saved sp */ |
247 | |
250 | |
|
|
251 | CV *startcv; /* the CV to execute */ |
248 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | AV *args; /* data associated with this coroutine (initial args) */ |
249 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int refcnt; /* coroutines are refcounted, yes */ |
250 | int flags; /* CF_ flags */ |
254 | int flags; /* CF_ flags */ |
251 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | HV *hv; /* the perl hash associated with this coro, if any */ |
252 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | void (*on_destroy)(pTHX_ struct coro *coro); |
253 | |
257 | |
254 | /* statistics */ |
258 | /* statistics */ |
255 | int usecount; /* number of transfers to this coro */ |
259 | int usecount; /* number of transfers to this coro */ |
256 | |
260 | |
257 | /* coro process data */ |
261 | /* coro process data */ |
258 | int prio; |
262 | int prio; |
259 | SV *throw; /* exception to be thrown */ |
263 | SV *except; /* exception to be thrown */ |
|
|
264 | SV *rouse_cb; |
260 | |
265 | |
261 | /* async_pool */ |
266 | /* async_pool */ |
262 | SV *saved_deffh; |
267 | SV *saved_deffh; |
263 | |
268 | |
264 | /* linked list */ |
269 | /* linked list */ |
… | |
… | |
270 | |
275 | |
271 | /* the following variables are effectively part of the perl context */ |
276 | /* the following variables are effectively part of the perl context */ |
272 | /* and get copied between struct coro and these variables */ |
277 | /* and get copied between struct coro and these variables */ |
273 | /* the mainr easonw e don't support windows process emulation */ |
278 | /* the mainr easonw e don't support windows process emulation */ |
274 | static struct CoroSLF slf_frame; /* the current slf frame */ |
279 | static struct CoroSLF slf_frame; /* the current slf frame */ |
275 | static SV *coro_throw; |
|
|
276 | |
280 | |
277 | /** Coro ********************************************************************/ |
281 | /** Coro ********************************************************************/ |
278 | |
282 | |
279 | #define PRIO_MAX 3 |
283 | #define PRIO_MAX 3 |
280 | #define PRIO_HIGH 1 |
284 | #define PRIO_HIGH 1 |
… | |
… | |
318 | #if PERL_VERSION_ATLEAST (5,10,0) |
322 | #if PERL_VERSION_ATLEAST (5,10,0) |
319 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
323 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
320 | get_hv (name, create); |
324 | get_hv (name, create); |
321 | #endif |
325 | #endif |
322 | return get_hv (name, create); |
326 | return get_hv (name, create); |
|
|
327 | } |
|
|
328 | |
|
|
329 | /* may croak */ |
|
|
330 | INLINE CV * |
|
|
331 | coro_sv_2cv (SV *sv) |
|
|
332 | { |
|
|
333 | HV *st; |
|
|
334 | GV *gvp; |
|
|
335 | return sv_2cv (sv, &st, &gvp, 0); |
323 | } |
336 | } |
324 | |
337 | |
325 | static AV * |
338 | static AV * |
326 | coro_clone_padlist (pTHX_ CV *cv) |
339 | coro_clone_padlist (pTHX_ CV *cv) |
327 | { |
340 | { |
… | |
… | |
446 | else |
459 | else |
447 | { |
460 | { |
448 | #if CORO_PREFER_PERL_FUNCTIONS |
461 | #if CORO_PREFER_PERL_FUNCTIONS |
449 | /* this is probably cleaner? but also slower! */ |
462 | /* this is probably cleaner? but also slower! */ |
450 | /* in practise, it seems to be less stable */ |
463 | /* in practise, it seems to be less stable */ |
451 | CV *cp = Perl_cv_clone (cv); |
464 | CV *cp = Perl_cv_clone (aTHX_ cv); |
452 | CvPADLIST (cv) = CvPADLIST (cp); |
465 | CvPADLIST (cv) = CvPADLIST (cp); |
453 | CvPADLIST (cp) = 0; |
466 | CvPADLIST (cp) = 0; |
454 | SvREFCNT_dec (cp); |
467 | SvREFCNT_dec (cp); |
455 | #else |
468 | #else |
456 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
469 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
509 | |
522 | |
510 | PUTBACK; |
523 | PUTBACK; |
511 | } |
524 | } |
512 | |
525 | |
513 | slf_frame = c->slf_frame; |
526 | slf_frame = c->slf_frame; |
514 | coro_throw = c->throw; |
527 | CORO_THROW = c->except; |
515 | } |
528 | } |
516 | |
529 | |
517 | static void |
530 | static void |
518 | save_perl (pTHX_ Coro__State c) |
531 | save_perl (pTHX_ Coro__State c) |
519 | { |
532 | { |
520 | c->throw = coro_throw; |
533 | c->except = CORO_THROW; |
521 | c->slf_frame = slf_frame; |
534 | c->slf_frame = slf_frame; |
522 | |
535 | |
523 | { |
536 | { |
524 | dSP; |
537 | dSP; |
525 | I32 cxix = cxstack_ix; |
538 | I32 cxix = cxstack_ix; |
… | |
… | |
600 | * of perl.c:init_stacks, except that it uses less memory |
613 | * of perl.c:init_stacks, except that it uses less memory |
601 | * on the (sometimes correct) assumption that coroutines do |
614 | * on the (sometimes correct) assumption that coroutines do |
602 | * not usually need a lot of stackspace. |
615 | * not usually need a lot of stackspace. |
603 | */ |
616 | */ |
604 | #if CORO_PREFER_PERL_FUNCTIONS |
617 | #if CORO_PREFER_PERL_FUNCTIONS |
605 | # define coro_init_stacks init_stacks |
618 | # define coro_init_stacks(thx) init_stacks () |
606 | #else |
619 | #else |
607 | static void |
620 | static void |
608 | coro_init_stacks (pTHX) |
621 | coro_init_stacks (pTHX) |
609 | { |
622 | { |
610 | PL_curstackinfo = new_stackinfo(32, 8); |
623 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
857 | { |
870 | { |
858 | dSP; |
871 | dSP; |
859 | UNOP myop; |
872 | UNOP myop; |
860 | |
873 | |
861 | Zero (&myop, 1, UNOP); |
874 | Zero (&myop, 1, UNOP); |
862 | myop.op_next = Nullop; |
875 | myop.op_next = Nullop; |
863 | myop.op_flags = OPf_WANT_VOID; |
876 | myop.op_flags = OPf_WANT_VOID; |
864 | |
877 | |
865 | PUSHMARK (SP); |
878 | PUSHMARK (SP); |
866 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
879 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
867 | PUTBACK; |
880 | PUTBACK; |
… | |
… | |
875 | */ |
888 | */ |
876 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
889 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
877 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
890 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
878 | |
891 | |
879 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
892 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
|
|
893 | coro_setup_op.op_next = PL_op; |
880 | coro_setup_op.op_type = OP_CUSTOM; |
894 | coro_setup_op.op_type = OP_CUSTOM; |
881 | coro_setup_op.op_ppaddr = pp_slf; |
895 | coro_setup_op.op_ppaddr = pp_slf; |
882 | coro_setup_op.op_next = PL_op; |
896 | /* no flags required, as an init function won't be called */ |
883 | |
897 | |
884 | PL_op = (OP *)&coro_setup_op; |
898 | PL_op = (OP *)&coro_setup_op; |
885 | |
899 | |
886 | /* copy throw, in case it was set before coro_setup */ |
900 | /* copy throw, in case it was set before coro_setup */ |
887 | coro_throw = coro->throw; |
901 | CORO_THROW = coro->except; |
888 | } |
902 | } |
889 | |
903 | |
890 | static void |
904 | static void |
891 | coro_destruct (pTHX_ struct coro *coro) |
905 | coro_destruct (pTHX_ struct coro *coro) |
892 | { |
906 | { |
… | |
… | |
915 | SvREFCNT_dec (GvSV (irsgv)); |
929 | SvREFCNT_dec (GvSV (irsgv)); |
916 | |
930 | |
917 | SvREFCNT_dec (PL_diehook); |
931 | SvREFCNT_dec (PL_diehook); |
918 | SvREFCNT_dec (PL_warnhook); |
932 | SvREFCNT_dec (PL_warnhook); |
919 | |
933 | |
|
|
934 | SvREFCNT_dec (CORO_THROW); |
920 | SvREFCNT_dec (coro->saved_deffh); |
935 | SvREFCNT_dec (coro->saved_deffh); |
921 | SvREFCNT_dec (coro_throw); |
936 | SvREFCNT_dec (coro->rouse_cb); |
922 | |
937 | |
923 | coro_destruct_stacks (aTHX); |
938 | coro_destruct_stacks (aTHX); |
924 | } |
939 | } |
925 | |
940 | |
926 | INLINE void |
941 | INLINE void |
… | |
… | |
1013 | SAVETMPS; |
1028 | SAVETMPS; |
1014 | EXTEND (SP, 3); |
1029 | EXTEND (SP, 3); |
1015 | PUSHMARK (SP); |
1030 | PUSHMARK (SP); |
1016 | PUSHs (&PL_sv_yes); |
1031 | PUSHs (&PL_sv_yes); |
1017 | PUSHs (fullname); |
1032 | PUSHs (fullname); |
1018 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1033 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1019 | PUTBACK; |
1034 | PUTBACK; |
1020 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1035 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1021 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1036 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1022 | SPAGAIN; |
1037 | SPAGAIN; |
1023 | FREETMPS; |
1038 | FREETMPS; |
… | |
… | |
1070 | static int |
1085 | static int |
1071 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1086 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1072 | { |
1087 | { |
1073 | *frame = cctx_ssl_frame; |
1088 | *frame = cctx_ssl_frame; |
1074 | |
1089 | |
1075 | return 1; /* execute the restored frame - there must be one */ |
1090 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1076 | } |
1091 | } |
1077 | |
1092 | |
1078 | /* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ |
1093 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1079 | static void NOINLINE |
1094 | static void NOINLINE |
1080 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1095 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1081 | { |
1096 | { |
1082 | PL_top_env = &PL_start_env; |
1097 | PL_top_env = &PL_start_env; |
1083 | |
1098 | |
… | |
… | |
1087 | /* we already must be executing an SLF op, there is no other valid way |
1102 | /* we already must be executing an SLF op, there is no other valid way |
1088 | * that can lead to creation of a new cctx */ |
1103 | * that can lead to creation of a new cctx */ |
1089 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1104 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1090 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1105 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1091 | |
1106 | |
|
|
1107 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1092 | cctx_ssl_cctx = cctx; |
1108 | cctx_ssl_cctx = cctx; |
1093 | cctx_ssl_frame = slf_frame; |
1109 | cctx_ssl_frame = slf_frame; |
1094 | |
1110 | |
1095 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1111 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1096 | slf_frame.check = slf_check_set_stacklevel; |
1112 | slf_frame.check = slf_check_set_stacklevel; |
… | |
… | |
1317 | dSTACKLEVEL; |
1333 | dSTACKLEVEL; |
1318 | |
1334 | |
1319 | /* sometimes transfer is only called to set idle_sp */ |
1335 | /* sometimes transfer is only called to set idle_sp */ |
1320 | if (expect_false (!next)) |
1336 | if (expect_false (!next)) |
1321 | { |
1337 | { |
1322 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1338 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1323 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1339 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1324 | } |
1340 | } |
1325 | else if (expect_true (prev != next)) |
1341 | else if (expect_true (prev != next)) |
1326 | { |
1342 | { |
1327 | coro_cctx *prev__cctx; |
1343 | coro_cctx *prev__cctx; |
… | |
… | |
1352 | |
1368 | |
1353 | prev__cctx = prev->cctx; |
1369 | prev__cctx = prev->cctx; |
1354 | |
1370 | |
1355 | /* possibly untie and reuse the cctx */ |
1371 | /* possibly untie and reuse the cctx */ |
1356 | if (expect_true ( |
1372 | if (expect_true ( |
1357 | prev__cctx->idle_sp == (void *)stacklevel |
1373 | prev__cctx->idle_sp == STACKLEVEL |
1358 | && !(prev__cctx->flags & CC_TRACE) |
1374 | && !(prev__cctx->flags & CC_TRACE) |
1359 | && !force_cctx |
1375 | && !force_cctx |
1360 | )) |
1376 | )) |
1361 | { |
1377 | { |
1362 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1378 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1429 | |
1445 | |
1430 | coro->slot = 0; |
1446 | coro->slot = 0; |
1431 | } |
1447 | } |
1432 | |
1448 | |
1433 | cctx_destroy (coro->cctx); |
1449 | cctx_destroy (coro->cctx); |
|
|
1450 | SvREFCNT_dec (coro->startcv); |
1434 | SvREFCNT_dec (coro->args); |
1451 | SvREFCNT_dec (coro->args); |
1435 | |
1452 | |
1436 | if (coro->next) coro->next->prev = coro->prev; |
1453 | if (coro->next) coro->next->prev = coro->prev; |
1437 | if (coro->prev) coro->prev->next = coro->next; |
1454 | if (coro->prev) coro->prev->next = coro->next; |
1438 | if (coro == coro_first) coro_first = coro->next; |
1455 | if (coro == coro_first) coro_first = coro->next; |
… | |
… | |
1493 | |
1510 | |
1494 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1511 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1495 | TRANSFER (ta, 1); |
1512 | TRANSFER (ta, 1); |
1496 | } |
1513 | } |
1497 | |
1514 | |
|
|
1515 | /*****************************************************************************/ |
|
|
1516 | /* gensub: simple closure generation utility */ |
|
|
1517 | |
|
|
1518 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1519 | |
|
|
1520 | /* create a closure from XS, returns a code reference */ |
|
|
1521 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1522 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1523 | static SV * |
|
|
1524 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1525 | { |
|
|
1526 | CV *cv = (CV *)newSV (0); |
|
|
1527 | |
|
|
1528 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1529 | |
|
|
1530 | CvANON_on (cv); |
|
|
1531 | CvISXSUB_on (cv); |
|
|
1532 | CvXSUB (cv) = xsub; |
|
|
1533 | GENSUB_ARG = arg; |
|
|
1534 | |
|
|
1535 | return newRV_noinc ((SV *)cv); |
|
|
1536 | } |
|
|
1537 | |
1498 | /** Coro ********************************************************************/ |
1538 | /** Coro ********************************************************************/ |
1499 | |
1539 | |
1500 | INLINE void |
1540 | INLINE void |
1501 | coro_enq (pTHX_ struct coro *coro) |
1541 | coro_enq (pTHX_ struct coro *coro) |
1502 | { |
1542 | { |
… | |
… | |
1699 | if (coro->flags & CF_RUNNING) |
1739 | if (coro->flags & CF_RUNNING) |
1700 | PL_runops = RUNOPS_DEFAULT; |
1740 | PL_runops = RUNOPS_DEFAULT; |
1701 | else |
1741 | else |
1702 | coro->slot->runops = RUNOPS_DEFAULT; |
1742 | coro->slot->runops = RUNOPS_DEFAULT; |
1703 | } |
1743 | } |
|
|
1744 | } |
|
|
1745 | |
|
|
1746 | /*****************************************************************************/ |
|
|
1747 | /* rouse callback */ |
|
|
1748 | |
|
|
1749 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1750 | |
|
|
1751 | static void |
|
|
1752 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1753 | { |
|
|
1754 | dXSARGS; |
|
|
1755 | SV *data = (SV *)GENSUB_ARG; |
|
|
1756 | |
|
|
1757 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1758 | { |
|
|
1759 | /* first call, set args */ |
|
|
1760 | int i; |
|
|
1761 | AV *av = newAV (); |
|
|
1762 | SV *coro = SvRV (data); |
|
|
1763 | |
|
|
1764 | SvRV_set (data, (SV *)av); |
|
|
1765 | api_ready (aTHX_ coro); |
|
|
1766 | SvREFCNT_dec (coro); |
|
|
1767 | |
|
|
1768 | /* better take a full copy of the arguments */ |
|
|
1769 | while (items--) |
|
|
1770 | av_store (av, items, newSVsv (ST (items))); |
|
|
1771 | } |
|
|
1772 | |
|
|
1773 | XSRETURN_EMPTY; |
|
|
1774 | } |
|
|
1775 | |
|
|
1776 | static int |
|
|
1777 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1778 | { |
|
|
1779 | SV *data = (SV *)frame->data; |
|
|
1780 | |
|
|
1781 | if (CORO_THROW) |
|
|
1782 | return 0; |
|
|
1783 | |
|
|
1784 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1785 | return 1; |
|
|
1786 | |
|
|
1787 | /* now push all results on the stack */ |
|
|
1788 | { |
|
|
1789 | dSP; |
|
|
1790 | AV *av = (AV *)SvRV (data); |
|
|
1791 | int i; |
|
|
1792 | |
|
|
1793 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1794 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1795 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1796 | |
|
|
1797 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1798 | AvFILLp (av) = -1; |
|
|
1799 | av_undef (av); |
|
|
1800 | |
|
|
1801 | PUTBACK; |
|
|
1802 | } |
|
|
1803 | |
|
|
1804 | return 0; |
|
|
1805 | } |
|
|
1806 | |
|
|
1807 | static void |
|
|
1808 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1809 | { |
|
|
1810 | SV *cb; |
|
|
1811 | |
|
|
1812 | if (items) |
|
|
1813 | cb = arg [0]; |
|
|
1814 | else |
|
|
1815 | { |
|
|
1816 | struct coro *coro = SvSTATE_current; |
|
|
1817 | |
|
|
1818 | if (!coro->rouse_cb) |
|
|
1819 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1820 | |
|
|
1821 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1822 | coro->rouse_cb = 0; |
|
|
1823 | } |
|
|
1824 | |
|
|
1825 | if (!SvROK (cb) |
|
|
1826 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1827 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1828 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1829 | |
|
|
1830 | { |
|
|
1831 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1832 | SV *data = (SV *)GENSUB_ARG; |
|
|
1833 | |
|
|
1834 | frame->data = (void *)data; |
|
|
1835 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1836 | frame->check = slf_check_rouse_wait; |
|
|
1837 | } |
|
|
1838 | } |
|
|
1839 | |
|
|
1840 | static SV * |
|
|
1841 | coro_new_rouse_cb (pTHX) |
|
|
1842 | { |
|
|
1843 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1844 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1845 | SV *data = newRV_inc ((SV *)hv); |
|
|
1846 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1847 | |
|
|
1848 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1849 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1850 | |
|
|
1851 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1852 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1853 | |
|
|
1854 | return cb; |
1704 | } |
1855 | } |
1705 | |
1856 | |
1706 | /*****************************************************************************/ |
1857 | /*****************************************************************************/ |
1707 | /* schedule-like-function opcode (SLF) */ |
1858 | /* schedule-like-function opcode (SLF) */ |
1708 | |
1859 | |
… | |
… | |
1835 | } |
1986 | } |
1836 | while (slf_frame.check (aTHX_ &slf_frame)); |
1987 | while (slf_frame.check (aTHX_ &slf_frame)); |
1837 | |
1988 | |
1838 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1989 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1839 | |
1990 | |
|
|
1991 | /* exception handling */ |
|
|
1992 | if (expect_false (CORO_THROW)) |
|
|
1993 | { |
|
|
1994 | SV *exception = sv_2mortal (CORO_THROW); |
|
|
1995 | |
|
|
1996 | CORO_THROW = 0; |
|
|
1997 | sv_setsv (ERRSV, exception); |
|
|
1998 | croak (0); |
|
|
1999 | } |
|
|
2000 | |
1840 | /* return value handling - mostly like entersub */ |
2001 | /* return value handling - mostly like entersub */ |
1841 | /* make sure we put something on the stack in scalar context */ |
2002 | /* make sure we put something on the stack in scalar context */ |
1842 | if (GIMME_V == G_SCALAR) |
2003 | if (GIMME_V == G_SCALAR) |
1843 | { |
2004 | { |
1844 | dSP; |
2005 | dSP; |
… | |
… | |
1850 | bot [1] = *sp; |
2011 | bot [1] = *sp; |
1851 | |
2012 | |
1852 | SP = bot + 1; |
2013 | SP = bot + 1; |
1853 | |
2014 | |
1854 | PUTBACK; |
2015 | PUTBACK; |
1855 | } |
|
|
1856 | |
|
|
1857 | /* exception handling */ |
|
|
1858 | if (expect_false (coro_throw)) |
|
|
1859 | { |
|
|
1860 | SV *exception = sv_2mortal (coro_throw); |
|
|
1861 | |
|
|
1862 | coro_throw = 0; |
|
|
1863 | sv_setsv (ERRSV, exception); |
|
|
1864 | croak (0); |
|
|
1865 | } |
2016 | } |
1866 | |
2017 | |
1867 | return NORMAL; |
2018 | return NORMAL; |
1868 | } |
2019 | } |
1869 | |
2020 | |
… | |
… | |
1991 | PerlIOBuf_get_cnt, |
2142 | PerlIOBuf_get_cnt, |
1992 | PerlIOBuf_set_ptrcnt, |
2143 | PerlIOBuf_set_ptrcnt, |
1993 | }; |
2144 | }; |
1994 | |
2145 | |
1995 | /*****************************************************************************/ |
2146 | /*****************************************************************************/ |
|
|
2147 | /* Coro::Semaphore & Coro::Signal */ |
|
|
2148 | |
|
|
2149 | static SV * |
|
|
2150 | coro_waitarray_new (pTHX_ int count) |
|
|
2151 | { |
|
|
2152 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2153 | AV *av = newAV (); |
|
|
2154 | SV **ary; |
|
|
2155 | |
|
|
2156 | /* unfortunately, building manually saves memory */ |
|
|
2157 | Newx (ary, 2, SV *); |
|
|
2158 | AvALLOC (av) = ary; |
|
|
2159 | /*AvARRAY (av) = ary;*/ |
|
|
2160 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
|
|
2161 | AvMAX (av) = 1; |
|
|
2162 | AvFILLp (av) = 0; |
|
|
2163 | ary [0] = newSViv (count); |
|
|
2164 | |
|
|
2165 | return newRV_noinc ((SV *)av); |
|
|
2166 | } |
|
|
2167 | |
1996 | /* Coro::Semaphore */ |
2168 | /* semaphore */ |
1997 | |
2169 | |
1998 | static void |
2170 | static void |
1999 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2171 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2000 | { |
2172 | { |
2001 | SV *count_sv = AvARRAY (av)[0]; |
2173 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2013 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2185 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2014 | AvARRAY (av)[1] = count_sv; |
2186 | AvARRAY (av)[1] = count_sv; |
2015 | cb = av_shift (av); |
2187 | cb = av_shift (av); |
2016 | |
2188 | |
2017 | if (SvOBJECT (cb)) |
2189 | if (SvOBJECT (cb)) |
|
|
2190 | { |
2018 | api_ready (aTHX_ cb); |
2191 | api_ready (aTHX_ cb); |
2019 | else |
2192 | --count; |
2020 | croak ("callbacks not yet supported"); |
2193 | } |
|
|
2194 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2195 | { |
|
|
2196 | dSP; |
|
|
2197 | PUSHMARK (SP); |
|
|
2198 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2199 | PUTBACK; |
|
|
2200 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2201 | } |
2021 | |
2202 | |
2022 | SvREFCNT_dec (cb); |
2203 | SvREFCNT_dec (cb); |
2023 | |
|
|
2024 | --count; |
|
|
2025 | } |
2204 | } |
2026 | } |
2205 | } |
2027 | |
2206 | |
2028 | static void |
2207 | static void |
2029 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2208 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2031 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2210 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2032 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2211 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2033 | } |
2212 | } |
2034 | |
2213 | |
2035 | static int |
2214 | static int |
2036 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2215 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2037 | { |
2216 | { |
2038 | AV *av = (AV *)frame->data; |
2217 | AV *av = (AV *)frame->data; |
2039 | SV *count_sv = AvARRAY (av)[0]; |
2218 | SV *count_sv = AvARRAY (av)[0]; |
2040 | |
2219 | |
|
|
2220 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2221 | if (CORO_THROW) |
|
|
2222 | return 0; |
2041 | if (SvIVX (count_sv) > 0) |
2223 | else if (SvIVX (count_sv) > 0) |
2042 | { |
2224 | { |
2043 | SvSTATE_current->on_destroy = 0; |
2225 | SvSTATE_current->on_destroy = 0; |
|
|
2226 | |
|
|
2227 | if (acquire) |
2044 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2228 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2229 | else |
|
|
2230 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2231 | |
2045 | return 0; |
2232 | return 0; |
2046 | } |
2233 | } |
2047 | else |
2234 | else |
2048 | { |
2235 | { |
2049 | int i; |
2236 | int i; |
… | |
… | |
2058 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2245 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2059 | return 1; |
2246 | return 1; |
2060 | } |
2247 | } |
2061 | } |
2248 | } |
2062 | |
2249 | |
2063 | static void |
2250 | static int |
|
|
2251 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2252 | { |
|
|
2253 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 1); |
|
|
2254 | } |
|
|
2255 | |
|
|
2256 | static int |
|
|
2257 | slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame) |
|
|
2258 | { |
|
|
2259 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 0); |
|
|
2260 | } |
|
|
2261 | |
|
|
2262 | static void |
2064 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2263 | slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2065 | { |
2264 | { |
2066 | AV *av = (AV *)SvRV (arg [0]); |
2265 | AV *av = (AV *)SvRV (arg [0]); |
2067 | |
2266 | |
2068 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2267 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2069 | { |
2268 | { |
2070 | frame->data = (void *)av; |
2269 | frame->data = (void *)av; |
2071 | frame->prepare = prepare_nop; |
2270 | frame->prepare = prepare_nop; |
2072 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2073 | } |
2271 | } |
2074 | else |
2272 | else |
2075 | { |
2273 | { |
2076 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2274 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2077 | |
2275 | |
2078 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2276 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2079 | frame->prepare = prepare_schedule; |
2277 | frame->prepare = prepare_schedule; |
2080 | |
2278 | |
2081 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2279 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2082 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2280 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2083 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2084 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2281 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2085 | } |
2282 | } |
|
|
2283 | } |
2086 | |
2284 | |
|
|
2285 | static void |
|
|
2286 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2287 | { |
|
|
2288 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2087 | frame->check = slf_check_semaphore_down; |
2289 | frame->check = slf_check_semaphore_down; |
2088 | |
|
|
2089 | } |
2290 | } |
2090 | |
2291 | |
2091 | /*****************************************************************************/ |
2292 | static void |
2092 | /* gensub: simple closure generation utility */ |
2293 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2093 | |
|
|
2094 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2095 | |
|
|
2096 | /* create a closure from XS, returns a code reference */ |
|
|
2097 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2098 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2099 | static SV * |
|
|
2100 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2101 | { |
2294 | { |
2102 | CV *cv = (CV *)newSV (0); |
2295 | if (items >= 2) |
|
|
2296 | { |
|
|
2297 | /* callback form */ |
|
|
2298 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2299 | CV *cb_cv = coro_sv_2cv (arg [1]); |
2103 | |
2300 | |
2104 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2301 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2105 | |
2302 | |
2106 | CvANON_on (cv); |
2303 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2107 | CvISXSUB_on (cv); |
2304 | coro_semaphore_adjust (aTHX_ av, 0); |
2108 | CvXSUB (cv) = xsub; |
|
|
2109 | GENSUB_ARG = arg; |
|
|
2110 | |
2305 | |
2111 | return newRV_noinc ((SV *)cv); |
2306 | frame->prepare = prepare_nop; |
|
|
2307 | frame->check = slf_check_nop; |
|
|
2308 | } |
|
|
2309 | else |
|
|
2310 | { |
|
|
2311 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
|
|
2312 | frame->check = slf_check_semaphore_wait; |
|
|
2313 | } |
|
|
2314 | } |
|
|
2315 | |
|
|
2316 | /* signal */ |
|
|
2317 | |
|
|
2318 | static void |
|
|
2319 | coro_signal_wake (pTHX_ AV *av, int count) |
|
|
2320 | { |
|
|
2321 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2322 | |
|
|
2323 | /* now signal count waiters */ |
|
|
2324 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2325 | { |
|
|
2326 | SV *cb; |
|
|
2327 | |
|
|
2328 | /* swap first two elements so we can shift a waiter */ |
|
|
2329 | cb = AvARRAY (av)[0]; |
|
|
2330 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2331 | AvARRAY (av)[1] = cb; |
|
|
2332 | |
|
|
2333 | cb = av_shift (av); |
|
|
2334 | |
|
|
2335 | api_ready (aTHX_ cb); |
|
|
2336 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2337 | SvREFCNT_dec (cb); |
|
|
2338 | |
|
|
2339 | --count; |
|
|
2340 | } |
|
|
2341 | } |
|
|
2342 | |
|
|
2343 | static int |
|
|
2344 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2345 | { |
|
|
2346 | /* if we are about to throw, also stop waiting */ |
|
|
2347 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2348 | } |
|
|
2349 | |
|
|
2350 | static void |
|
|
2351 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2352 | { |
|
|
2353 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2354 | |
|
|
2355 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2356 | { |
|
|
2357 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2358 | frame->prepare = prepare_nop; |
|
|
2359 | frame->check = slf_check_nop; |
|
|
2360 | } |
|
|
2361 | else |
|
|
2362 | { |
|
|
2363 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2364 | |
|
|
2365 | av_push (av, waiter); |
|
|
2366 | |
|
|
2367 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2368 | frame->prepare = prepare_schedule; |
|
|
2369 | frame->check = slf_check_signal_wait; |
|
|
2370 | } |
2112 | } |
2371 | } |
2113 | |
2372 | |
2114 | /*****************************************************************************/ |
2373 | /*****************************************************************************/ |
2115 | /* Coro::AIO */ |
2374 | /* Coro::AIO */ |
2116 | |
2375 | |
… | |
… | |
2165 | |
2424 | |
2166 | static int |
2425 | static int |
2167 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2426 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2168 | { |
2427 | { |
2169 | AV *state = (AV *)frame->data; |
2428 | AV *state = (AV *)frame->data; |
|
|
2429 | |
|
|
2430 | /* if we are about to throw, return early */ |
|
|
2431 | /* this does not cancel the aio request, but at least */ |
|
|
2432 | /* it quickly returns */ |
|
|
2433 | if (CORO_THROW) |
|
|
2434 | return 0; |
2170 | |
2435 | |
2171 | /* one element that is an RV? repeat! */ |
2436 | /* one element that is an RV? repeat! */ |
2172 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2437 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2173 | return 1; |
2438 | return 1; |
2174 | |
2439 | |
… | |
… | |
2350 | CODE: |
2615 | CODE: |
2351 | { |
2616 | { |
2352 | struct coro *coro; |
2617 | struct coro *coro; |
2353 | MAGIC *mg; |
2618 | MAGIC *mg; |
2354 | HV *hv; |
2619 | HV *hv; |
|
|
2620 | CV *cb; |
2355 | int i; |
2621 | int i; |
|
|
2622 | |
|
|
2623 | if (items > 1) |
|
|
2624 | { |
|
|
2625 | cb = coro_sv_2cv (ST (1)); |
|
|
2626 | |
|
|
2627 | if (CvISXSUB (cb)) |
|
|
2628 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2629 | |
|
|
2630 | if (!CvROOT (cb)) |
|
|
2631 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2632 | } |
2356 | |
2633 | |
2357 | Newz (0, coro, 1, struct coro); |
2634 | Newz (0, coro, 1, struct coro); |
2358 | coro->args = newAV (); |
2635 | coro->args = newAV (); |
2359 | coro->flags = CF_NEW; |
2636 | coro->flags = CF_NEW; |
2360 | |
2637 | |
… | |
… | |
2365 | coro->hv = hv = newHV (); |
2642 | coro->hv = hv = newHV (); |
2366 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2643 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2367 | mg->mg_flags |= MGf_DUP; |
2644 | mg->mg_flags |= MGf_DUP; |
2368 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2645 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2369 | |
2646 | |
|
|
2647 | if (items > 1) |
|
|
2648 | { |
|
|
2649 | coro->startcv = SvREFCNT_inc_NN (cb); |
|
|
2650 | |
2370 | av_extend (coro->args, items - 1); |
2651 | av_extend (coro->args, items - 1); |
|
|
2652 | av_push (coro->args, SvREFCNT_inc (cb)); |
2371 | for (i = 1; i < items; i++) |
2653 | for (i = 2; i < items; i++) |
2372 | av_push (coro->args, newSVsv (ST (i))); |
2654 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2655 | } |
2373 | } |
2656 | } |
2374 | OUTPUT: |
2657 | OUTPUT: |
2375 | RETVAL |
2658 | RETVAL |
2376 | |
2659 | |
2377 | void |
2660 | void |
… | |
… | |
2506 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2789 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2507 | PROTOTYPE: $;$ |
2790 | PROTOTYPE: $;$ |
2508 | CODE: |
2791 | CODE: |
2509 | { |
2792 | { |
2510 | struct coro *current = SvSTATE_current; |
2793 | struct coro *current = SvSTATE_current; |
2511 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2794 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2512 | SvREFCNT_dec (*throwp); |
2795 | SvREFCNT_dec (*throwp); |
2513 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2796 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2514 | } |
2797 | } |
2515 | |
2798 | |
2516 | void |
2799 | void |
… | |
… | |
2605 | coroapi.ready = api_ready; |
2888 | coroapi.ready = api_ready; |
2606 | coroapi.is_ready = api_is_ready; |
2889 | coroapi.is_ready = api_is_ready; |
2607 | coroapi.nready = coro_nready; |
2890 | coroapi.nready = coro_nready; |
2608 | coroapi.current = coro_current; |
2891 | coroapi.current = coro_current; |
2609 | |
2892 | |
2610 | GCoroAPI = &coroapi; |
2893 | /*GCoroAPI = &coroapi;*/ |
2611 | sv_setiv (sv, (IV)&coroapi); |
2894 | sv_setiv (sv, (IV)&coroapi); |
2612 | SvREADONLY_on (sv); |
2895 | SvREADONLY_on (sv); |
2613 | } |
2896 | } |
2614 | } |
2897 | } |
2615 | |
2898 | |
… | |
… | |
2751 | api_trace (aTHX_ coro_current, 0); |
3034 | api_trace (aTHX_ coro_current, 0); |
2752 | |
3035 | |
2753 | av_push (av_async_pool, newSVsv (coro_current)); |
3036 | av_push (av_async_pool, newSVsv (coro_current)); |
2754 | } |
3037 | } |
2755 | |
3038 | |
|
|
3039 | SV * |
|
|
3040 | rouse_cb () |
|
|
3041 | PROTOTYPE: |
|
|
3042 | CODE: |
|
|
3043 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3044 | OUTPUT: |
|
|
3045 | RETVAL |
|
|
3046 | |
|
|
3047 | void |
|
|
3048 | rouse_wait (...) |
|
|
3049 | PROTOTYPE: ;$ |
|
|
3050 | PPCODE: |
|
|
3051 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
|
|
3052 | |
2756 | |
3053 | |
2757 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3054 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2758 | |
3055 | |
2759 | BOOT: |
3056 | BOOT: |
2760 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3057 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2761 | |
3058 | |
2762 | |
3059 | |
2763 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3060 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2764 | |
3061 | |
2765 | SV * |
3062 | SV * |
2766 | new (SV *klass, SV *count_ = 0) |
3063 | new (SV *klass, SV *count = 0) |
2767 | CODE: |
3064 | CODE: |
2768 | { |
3065 | RETVAL = sv_bless ( |
2769 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
3066 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2770 | AV *av = newAV (); |
3067 | GvSTASH (CvGV (cv)) |
2771 | SV **ary; |
3068 | ); |
|
|
3069 | OUTPUT: |
|
|
3070 | RETVAL |
2772 | |
3071 | |
2773 | /* unfortunately, building manually saves memory */ |
3072 | # helper for Coro::Channel |
2774 | Newx (ary, 2, SV *); |
3073 | SV * |
2775 | AvALLOC (av) = ary; |
3074 | _alloc (int count) |
2776 | AvARRAY (av) = ary; |
3075 | CODE: |
2777 | AvMAX (av) = 1; |
3076 | RETVAL = coro_waitarray_new (aTHX_ count); |
2778 | AvFILLp (av) = 0; |
|
|
2779 | ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1); |
|
|
2780 | |
|
|
2781 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
|
|
2782 | } |
|
|
2783 | OUTPUT: |
3077 | OUTPUT: |
2784 | RETVAL |
3078 | RETVAL |
2785 | |
3079 | |
2786 | SV * |
3080 | SV * |
2787 | count (SV *self) |
3081 | count (SV *self) |
… | |
… | |
2796 | adjust = 1 |
3090 | adjust = 1 |
2797 | CODE: |
3091 | CODE: |
2798 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3092 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2799 | |
3093 | |
2800 | void |
3094 | void |
2801 | down (SV *self) |
3095 | down (...) |
2802 | CODE: |
3096 | CODE: |
2803 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3097 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
3098 | |
|
|
3099 | void |
|
|
3100 | wait (...) |
|
|
3101 | CODE: |
|
|
3102 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2804 | |
3103 | |
2805 | void |
3104 | void |
2806 | try (SV *self) |
3105 | try (SV *self) |
2807 | PPCODE: |
3106 | PPCODE: |
2808 | { |
3107 | { |
… | |
… | |
2820 | XSRETURN_NO; |
3119 | XSRETURN_NO; |
2821 | } |
3120 | } |
2822 | |
3121 | |
2823 | void |
3122 | void |
2824 | waiters (SV *self) |
3123 | waiters (SV *self) |
2825 | CODE: |
3124 | PPCODE: |
2826 | { |
3125 | { |
2827 | AV *av = (AV *)SvRV (self); |
3126 | AV *av = (AV *)SvRV (self); |
|
|
3127 | int wcount = AvFILLp (av) + 1 - 1; |
2828 | |
3128 | |
2829 | if (GIMME_V == G_SCALAR) |
3129 | if (GIMME_V == G_SCALAR) |
2830 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
3130 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2831 | else |
3131 | else |
2832 | { |
3132 | { |
2833 | int i; |
3133 | int i; |
2834 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
3134 | EXTEND (SP, wcount); |
2835 | for (i = 1; i <= AvFILLp (av); ++i) |
3135 | for (i = 1; i <= wcount; ++i) |
2836 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3136 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2837 | } |
3137 | } |
2838 | } |
3138 | } |
|
|
3139 | |
|
|
3140 | MODULE = Coro::State PACKAGE = Coro::Signal |
|
|
3141 | |
|
|
3142 | SV * |
|
|
3143 | new (SV *klass) |
|
|
3144 | CODE: |
|
|
3145 | RETVAL = sv_bless ( |
|
|
3146 | coro_waitarray_new (aTHX_ 0), |
|
|
3147 | GvSTASH (CvGV (cv)) |
|
|
3148 | ); |
|
|
3149 | OUTPUT: |
|
|
3150 | RETVAL |
|
|
3151 | |
|
|
3152 | void |
|
|
3153 | wait (...) |
|
|
3154 | CODE: |
|
|
3155 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3156 | |
|
|
3157 | void |
|
|
3158 | broadcast (SV *self) |
|
|
3159 | CODE: |
|
|
3160 | { |
|
|
3161 | AV *av = (AV *)SvRV (self); |
|
|
3162 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3163 | } |
|
|
3164 | |
|
|
3165 | void |
|
|
3166 | send (SV *self) |
|
|
3167 | CODE: |
|
|
3168 | { |
|
|
3169 | AV *av = (AV *)SvRV (self); |
|
|
3170 | |
|
|
3171 | if (AvFILLp (av)) |
|
|
3172 | coro_signal_wake (aTHX_ av, 1); |
|
|
3173 | else |
|
|
3174 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
|
|
3175 | } |
|
|
3176 | |
|
|
3177 | IV |
|
|
3178 | awaited (SV *self) |
|
|
3179 | CODE: |
|
|
3180 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3181 | OUTPUT: |
|
|
3182 | RETVAL |
2839 | |
3183 | |
2840 | |
3184 | |
2841 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3185 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2842 | |
3186 | |
2843 | BOOT: |
3187 | BOOT: |
… | |
… | |
2871 | |
3215 | |
2872 | void |
3216 | void |
2873 | _register (char *target, char *proto, SV *req) |
3217 | _register (char *target, char *proto, SV *req) |
2874 | CODE: |
3218 | CODE: |
2875 | { |
3219 | { |
2876 | HV *st; |
|
|
2877 | GV *gvp; |
|
|
2878 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
3220 | CV *req_cv = coro_sv_2cv (req); |
2879 | /* newXSproto doesn't return the CV on 5.8 */ |
3221 | /* newXSproto doesn't return the CV on 5.8 */ |
2880 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3222 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
2881 | sv_setpv ((SV *)slf_cv, proto); |
3223 | sv_setpv ((SV *)slf_cv, proto); |
2882 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3224 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
2883 | } |
3225 | } |