… | |
… | |
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 |
… | |
… | |
285 | |
289 | |
286 | /* for Coro.pm */ |
290 | /* for Coro.pm */ |
287 | static SV *coro_current; |
291 | static SV *coro_current; |
288 | static SV *coro_readyhook; |
292 | static SV *coro_readyhook; |
289 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
293 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
294 | static CV *cv_coro_run; |
290 | static struct coro *coro_first; |
295 | static struct coro *coro_first; |
291 | #define coro_nready coroapi.nready |
296 | #define coro_nready coroapi.nready |
292 | |
297 | |
293 | /** lowlevel stuff **********************************************************/ |
298 | /** lowlevel stuff **********************************************************/ |
294 | |
299 | |
… | |
… | |
318 | #if PERL_VERSION_ATLEAST (5,10,0) |
323 | #if PERL_VERSION_ATLEAST (5,10,0) |
319 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
324 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
320 | get_hv (name, create); |
325 | get_hv (name, create); |
321 | #endif |
326 | #endif |
322 | return get_hv (name, create); |
327 | return get_hv (name, create); |
|
|
328 | } |
|
|
329 | |
|
|
330 | /* may croak */ |
|
|
331 | INLINE CV * |
|
|
332 | coro_sv_2cv (SV *sv) |
|
|
333 | { |
|
|
334 | HV *st; |
|
|
335 | GV *gvp; |
|
|
336 | return sv_2cv (sv, &st, &gvp, 0); |
323 | } |
337 | } |
324 | |
338 | |
325 | static AV * |
339 | static AV * |
326 | coro_clone_padlist (pTHX_ CV *cv) |
340 | coro_clone_padlist (pTHX_ CV *cv) |
327 | { |
341 | { |
… | |
… | |
446 | else |
460 | else |
447 | { |
461 | { |
448 | #if CORO_PREFER_PERL_FUNCTIONS |
462 | #if CORO_PREFER_PERL_FUNCTIONS |
449 | /* this is probably cleaner? but also slower! */ |
463 | /* this is probably cleaner? but also slower! */ |
450 | /* in practise, it seems to be less stable */ |
464 | /* in practise, it seems to be less stable */ |
451 | CV *cp = Perl_cv_clone (cv); |
465 | CV *cp = Perl_cv_clone (aTHX_ cv); |
452 | CvPADLIST (cv) = CvPADLIST (cp); |
466 | CvPADLIST (cv) = CvPADLIST (cp); |
453 | CvPADLIST (cp) = 0; |
467 | CvPADLIST (cp) = 0; |
454 | SvREFCNT_dec (cp); |
468 | SvREFCNT_dec (cp); |
455 | #else |
469 | #else |
456 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
470 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
509 | |
523 | |
510 | PUTBACK; |
524 | PUTBACK; |
511 | } |
525 | } |
512 | |
526 | |
513 | slf_frame = c->slf_frame; |
527 | slf_frame = c->slf_frame; |
514 | coro_throw = c->throw; |
528 | CORO_THROW = c->except; |
515 | } |
529 | } |
516 | |
530 | |
517 | static void |
531 | static void |
518 | save_perl (pTHX_ Coro__State c) |
532 | save_perl (pTHX_ Coro__State c) |
519 | { |
533 | { |
520 | c->throw = coro_throw; |
534 | c->except = CORO_THROW; |
521 | c->slf_frame = slf_frame; |
535 | c->slf_frame = slf_frame; |
522 | |
536 | |
523 | { |
537 | { |
524 | dSP; |
538 | dSP; |
525 | I32 cxix = cxstack_ix; |
539 | I32 cxix = cxstack_ix; |
… | |
… | |
600 | * of perl.c:init_stacks, except that it uses less memory |
614 | * of perl.c:init_stacks, except that it uses less memory |
601 | * on the (sometimes correct) assumption that coroutines do |
615 | * on the (sometimes correct) assumption that coroutines do |
602 | * not usually need a lot of stackspace. |
616 | * not usually need a lot of stackspace. |
603 | */ |
617 | */ |
604 | #if CORO_PREFER_PERL_FUNCTIONS |
618 | #if CORO_PREFER_PERL_FUNCTIONS |
605 | # define coro_init_stacks init_stacks |
619 | # define coro_init_stacks(thx) init_stacks () |
606 | #else |
620 | #else |
607 | static void |
621 | static void |
608 | coro_init_stacks (pTHX) |
622 | coro_init_stacks (pTHX) |
609 | { |
623 | { |
610 | PL_curstackinfo = new_stackinfo(32, 8); |
624 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
857 | { |
871 | { |
858 | dSP; |
872 | dSP; |
859 | UNOP myop; |
873 | UNOP myop; |
860 | |
874 | |
861 | Zero (&myop, 1, UNOP); |
875 | Zero (&myop, 1, UNOP); |
862 | myop.op_next = Nullop; |
876 | myop.op_next = Nullop; |
|
|
877 | myop.op_type = OP_ENTERSUB; |
863 | myop.op_flags = OPf_WANT_VOID; |
878 | myop.op_flags = OPf_WANT_VOID; |
864 | |
879 | |
865 | PUSHMARK (SP); |
880 | PUSHMARK (SP); |
866 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
881 | PUSHs ((SV *)coro->startcv); |
867 | PUTBACK; |
882 | PUTBACK; |
868 | PL_op = (OP *)&myop; |
883 | PL_op = (OP *)&myop; |
869 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
884 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
870 | SPAGAIN; |
|
|
871 | } |
885 | } |
872 | |
886 | |
873 | /* this newly created coroutine might be run on an existing cctx which most |
887 | /* this newly created coroutine might be run on an existing cctx which most |
874 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
888 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
875 | */ |
889 | */ |
876 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
890 | 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 */ |
891 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
878 | |
892 | |
879 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
893 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
|
|
894 | coro_setup_op.op_next = PL_op; |
880 | coro_setup_op.op_type = OP_CUSTOM; |
895 | coro_setup_op.op_type = OP_CUSTOM; |
881 | coro_setup_op.op_ppaddr = pp_slf; |
896 | coro_setup_op.op_ppaddr = pp_slf; |
882 | coro_setup_op.op_next = PL_op; |
897 | /* no flags etc. required, as an init function won't be called */ |
883 | |
898 | |
884 | PL_op = (OP *)&coro_setup_op; |
899 | PL_op = (OP *)&coro_setup_op; |
885 | |
900 | |
886 | /* copy throw, in case it was set before coro_setup */ |
901 | /* copy throw, in case it was set before coro_setup */ |
887 | coro_throw = coro->throw; |
902 | CORO_THROW = coro->except; |
888 | } |
903 | } |
889 | |
904 | |
890 | static void |
905 | static void |
891 | coro_destruct (pTHX_ struct coro *coro) |
906 | coro_destruct (pTHX_ struct coro *coro) |
892 | { |
907 | { |
… | |
… | |
916 | |
931 | |
917 | SvREFCNT_dec (PL_diehook); |
932 | SvREFCNT_dec (PL_diehook); |
918 | SvREFCNT_dec (PL_warnhook); |
933 | SvREFCNT_dec (PL_warnhook); |
919 | |
934 | |
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 0; |
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 | |
1084 | if (cctx->flags & CC_TRACE) |
1099 | if (cctx->flags & CC_TRACE) |
1085 | PL_runops = runops_trace; |
1100 | PL_runops = runops_trace; |
1086 | |
1101 | |
1087 | /* we already must be in an SLF call, 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; |
… | |
… | |
1128 | transfer_tail (aTHX); |
1144 | transfer_tail (aTHX); |
1129 | |
1145 | |
1130 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1146 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1131 | PL_restartop = PL_op; |
1147 | PL_restartop = PL_op; |
1132 | perl_run (PL_curinterp); |
1148 | perl_run (PL_curinterp); |
|
|
1149 | /* |
|
|
1150 | * Unfortunately, there is no way to get at the return values of the |
|
|
1151 | * coro body here, as perl_run destroys these |
|
|
1152 | */ |
1133 | |
1153 | |
1134 | /* |
1154 | /* |
1135 | * If perl-run returns we assume exit() was being called or the coro |
1155 | * If perl-run returns we assume exit() was being called or the coro |
1136 | * fell off the end, which seems to be the only valid (non-bug) |
1156 | * fell off the end, which seems to be the only valid (non-bug) |
1137 | * reason for perl_run to return. We try to exit by jumping to the |
1157 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1317 | dSTACKLEVEL; |
1337 | dSTACKLEVEL; |
1318 | |
1338 | |
1319 | /* sometimes transfer is only called to set idle_sp */ |
1339 | /* sometimes transfer is only called to set idle_sp */ |
1320 | if (expect_false (!next)) |
1340 | if (expect_false (!next)) |
1321 | { |
1341 | { |
1322 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1342 | ((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 */ |
1343 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1324 | } |
1344 | } |
1325 | else if (expect_true (prev != next)) |
1345 | else if (expect_true (prev != next)) |
1326 | { |
1346 | { |
1327 | coro_cctx *prev__cctx; |
1347 | coro_cctx *prev__cctx; |
… | |
… | |
1352 | |
1372 | |
1353 | prev__cctx = prev->cctx; |
1373 | prev__cctx = prev->cctx; |
1354 | |
1374 | |
1355 | /* possibly untie and reuse the cctx */ |
1375 | /* possibly untie and reuse the cctx */ |
1356 | if (expect_true ( |
1376 | if (expect_true ( |
1357 | prev__cctx->idle_sp == (void *)stacklevel |
1377 | prev__cctx->idle_sp == STACKLEVEL |
1358 | && !(prev__cctx->flags & CC_TRACE) |
1378 | && !(prev__cctx->flags & CC_TRACE) |
1359 | && !force_cctx |
1379 | && !force_cctx |
1360 | )) |
1380 | )) |
1361 | { |
1381 | { |
1362 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1382 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1429 | |
1449 | |
1430 | coro->slot = 0; |
1450 | coro->slot = 0; |
1431 | } |
1451 | } |
1432 | |
1452 | |
1433 | cctx_destroy (coro->cctx); |
1453 | cctx_destroy (coro->cctx); |
|
|
1454 | SvREFCNT_dec (coro->startcv); |
1434 | SvREFCNT_dec (coro->args); |
1455 | SvREFCNT_dec (coro->args); |
|
|
1456 | SvREFCNT_dec (CORO_THROW); |
1435 | |
1457 | |
1436 | if (coro->next) coro->next->prev = coro->prev; |
1458 | if (coro->next) coro->next->prev = coro->prev; |
1437 | if (coro->prev) coro->prev->next = coro->next; |
1459 | if (coro->prev) coro->prev->next = coro->next; |
1438 | if (coro == coro_first) coro_first = coro->next; |
1460 | if (coro == coro_first) coro_first = coro->next; |
1439 | |
1461 | |
… | |
… | |
1493 | |
1515 | |
1494 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1516 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1495 | TRANSFER (ta, 1); |
1517 | TRANSFER (ta, 1); |
1496 | } |
1518 | } |
1497 | |
1519 | |
|
|
1520 | /*****************************************************************************/ |
|
|
1521 | /* gensub: simple closure generation utility */ |
|
|
1522 | |
|
|
1523 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1524 | |
|
|
1525 | /* create a closure from XS, returns a code reference */ |
|
|
1526 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1527 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1528 | static SV * |
|
|
1529 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1530 | { |
|
|
1531 | CV *cv = (CV *)newSV (0); |
|
|
1532 | |
|
|
1533 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1534 | |
|
|
1535 | CvANON_on (cv); |
|
|
1536 | CvISXSUB_on (cv); |
|
|
1537 | CvXSUB (cv) = xsub; |
|
|
1538 | GENSUB_ARG = arg; |
|
|
1539 | |
|
|
1540 | return newRV_noinc ((SV *)cv); |
|
|
1541 | } |
|
|
1542 | |
1498 | /** Coro ********************************************************************/ |
1543 | /** Coro ********************************************************************/ |
1499 | |
1544 | |
1500 | INLINE void |
1545 | INLINE void |
1501 | coro_enq (pTHX_ struct coro *coro) |
1546 | coro_enq (pTHX_ struct coro *coro) |
1502 | { |
1547 | { |
… | |
… | |
1699 | if (coro->flags & CF_RUNNING) |
1744 | if (coro->flags & CF_RUNNING) |
1700 | PL_runops = RUNOPS_DEFAULT; |
1745 | PL_runops = RUNOPS_DEFAULT; |
1701 | else |
1746 | else |
1702 | coro->slot->runops = RUNOPS_DEFAULT; |
1747 | coro->slot->runops = RUNOPS_DEFAULT; |
1703 | } |
1748 | } |
|
|
1749 | } |
|
|
1750 | |
|
|
1751 | /*****************************************************************************/ |
|
|
1752 | /* rouse callback */ |
|
|
1753 | |
|
|
1754 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1755 | |
|
|
1756 | static void |
|
|
1757 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1758 | { |
|
|
1759 | dXSARGS; |
|
|
1760 | SV *data = (SV *)GENSUB_ARG; |
|
|
1761 | |
|
|
1762 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1763 | { |
|
|
1764 | /* first call, set args */ |
|
|
1765 | int i; |
|
|
1766 | AV *av = newAV (); |
|
|
1767 | SV *coro = SvRV (data); |
|
|
1768 | |
|
|
1769 | SvRV_set (data, (SV *)av); |
|
|
1770 | api_ready (aTHX_ coro); |
|
|
1771 | SvREFCNT_dec (coro); |
|
|
1772 | |
|
|
1773 | /* better take a full copy of the arguments */ |
|
|
1774 | while (items--) |
|
|
1775 | av_store (av, items, newSVsv (ST (items))); |
|
|
1776 | } |
|
|
1777 | |
|
|
1778 | XSRETURN_EMPTY; |
|
|
1779 | } |
|
|
1780 | |
|
|
1781 | static int |
|
|
1782 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1783 | { |
|
|
1784 | SV *data = (SV *)frame->data; |
|
|
1785 | |
|
|
1786 | if (CORO_THROW) |
|
|
1787 | return 0; |
|
|
1788 | |
|
|
1789 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1790 | return 1; |
|
|
1791 | |
|
|
1792 | /* now push all results on the stack */ |
|
|
1793 | { |
|
|
1794 | dSP; |
|
|
1795 | AV *av = (AV *)SvRV (data); |
|
|
1796 | int i; |
|
|
1797 | |
|
|
1798 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1799 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1800 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1801 | |
|
|
1802 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1803 | AvFILLp (av) = -1; |
|
|
1804 | av_undef (av); |
|
|
1805 | |
|
|
1806 | PUTBACK; |
|
|
1807 | } |
|
|
1808 | |
|
|
1809 | return 0; |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | static void |
|
|
1813 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1814 | { |
|
|
1815 | SV *cb; |
|
|
1816 | |
|
|
1817 | if (items) |
|
|
1818 | cb = arg [0]; |
|
|
1819 | else |
|
|
1820 | { |
|
|
1821 | struct coro *coro = SvSTATE_current; |
|
|
1822 | |
|
|
1823 | if (!coro->rouse_cb) |
|
|
1824 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1825 | |
|
|
1826 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1827 | coro->rouse_cb = 0; |
|
|
1828 | } |
|
|
1829 | |
|
|
1830 | if (!SvROK (cb) |
|
|
1831 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1832 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1833 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1834 | |
|
|
1835 | { |
|
|
1836 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1837 | SV *data = (SV *)GENSUB_ARG; |
|
|
1838 | |
|
|
1839 | frame->data = (void *)data; |
|
|
1840 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1841 | frame->check = slf_check_rouse_wait; |
|
|
1842 | } |
|
|
1843 | } |
|
|
1844 | |
|
|
1845 | static SV * |
|
|
1846 | coro_new_rouse_cb (pTHX) |
|
|
1847 | { |
|
|
1848 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1849 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1850 | SV *data = newRV_inc ((SV *)hv); |
|
|
1851 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1852 | |
|
|
1853 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1854 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1855 | |
|
|
1856 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1857 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1858 | |
|
|
1859 | return cb; |
1704 | } |
1860 | } |
1705 | |
1861 | |
1706 | /*****************************************************************************/ |
1862 | /*****************************************************************************/ |
1707 | /* schedule-like-function opcode (SLF) */ |
1863 | /* schedule-like-function opcode (SLF) */ |
1708 | |
1864 | |
… | |
… | |
1835 | } |
1991 | } |
1836 | while (slf_frame.check (aTHX_ &slf_frame)); |
1992 | while (slf_frame.check (aTHX_ &slf_frame)); |
1837 | |
1993 | |
1838 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1994 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1839 | |
1995 | |
|
|
1996 | /* exception handling */ |
|
|
1997 | if (expect_false (CORO_THROW)) |
|
|
1998 | { |
|
|
1999 | SV *exception = sv_2mortal (CORO_THROW); |
|
|
2000 | |
|
|
2001 | CORO_THROW = 0; |
|
|
2002 | sv_setsv (ERRSV, exception); |
|
|
2003 | croak (0); |
|
|
2004 | } |
|
|
2005 | |
1840 | /* return value handling - mostly like entersub */ |
2006 | /* return value handling - mostly like entersub */ |
1841 | /* make sure we put something on the stack in scalar context */ |
2007 | /* make sure we put something on the stack in scalar context */ |
1842 | if (GIMME_V == G_SCALAR) |
2008 | if (GIMME_V == G_SCALAR) |
1843 | { |
2009 | { |
1844 | dSP; |
2010 | dSP; |
… | |
… | |
1850 | bot [1] = *sp; |
2016 | bot [1] = *sp; |
1851 | |
2017 | |
1852 | SP = bot + 1; |
2018 | SP = bot + 1; |
1853 | |
2019 | |
1854 | PUTBACK; |
2020 | 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 | } |
2021 | } |
1866 | |
2022 | |
1867 | return NORMAL; |
2023 | return NORMAL; |
1868 | } |
2024 | } |
1869 | |
2025 | |
… | |
… | |
1991 | PerlIOBuf_get_cnt, |
2147 | PerlIOBuf_get_cnt, |
1992 | PerlIOBuf_set_ptrcnt, |
2148 | PerlIOBuf_set_ptrcnt, |
1993 | }; |
2149 | }; |
1994 | |
2150 | |
1995 | /*****************************************************************************/ |
2151 | /*****************************************************************************/ |
|
|
2152 | /* Coro::Semaphore & Coro::Signal */ |
|
|
2153 | |
|
|
2154 | static SV * |
|
|
2155 | coro_waitarray_new (pTHX_ int count) |
|
|
2156 | { |
|
|
2157 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2158 | AV *av = newAV (); |
|
|
2159 | SV **ary; |
|
|
2160 | |
|
|
2161 | /* unfortunately, building manually saves memory */ |
|
|
2162 | Newx (ary, 2, SV *); |
|
|
2163 | AvALLOC (av) = ary; |
|
|
2164 | /*AvARRAY (av) = ary;*/ |
|
|
2165 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
|
|
2166 | AvMAX (av) = 1; |
|
|
2167 | AvFILLp (av) = 0; |
|
|
2168 | ary [0] = newSViv (count); |
|
|
2169 | |
|
|
2170 | return newRV_noinc ((SV *)av); |
|
|
2171 | } |
|
|
2172 | |
1996 | /* Coro::Semaphore */ |
2173 | /* semaphore */ |
1997 | |
2174 | |
1998 | static void |
2175 | static void |
1999 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2176 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2000 | { |
2177 | { |
2001 | SV *count_sv = AvARRAY (av)[0]; |
2178 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2013 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2190 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2014 | AvARRAY (av)[1] = count_sv; |
2191 | AvARRAY (av)[1] = count_sv; |
2015 | cb = av_shift (av); |
2192 | cb = av_shift (av); |
2016 | |
2193 | |
2017 | if (SvOBJECT (cb)) |
2194 | if (SvOBJECT (cb)) |
|
|
2195 | { |
2018 | api_ready (aTHX_ cb); |
2196 | api_ready (aTHX_ cb); |
2019 | else |
2197 | --count; |
2020 | croak ("callbacks not yet supported"); |
2198 | } |
|
|
2199 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2200 | { |
|
|
2201 | dSP; |
|
|
2202 | PUSHMARK (SP); |
|
|
2203 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2204 | PUTBACK; |
|
|
2205 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2206 | } |
2021 | |
2207 | |
2022 | SvREFCNT_dec (cb); |
2208 | SvREFCNT_dec (cb); |
2023 | |
|
|
2024 | --count; |
|
|
2025 | } |
2209 | } |
2026 | } |
2210 | } |
2027 | |
2211 | |
2028 | static void |
2212 | static void |
2029 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2213 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2031 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2215 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2032 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2216 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2033 | } |
2217 | } |
2034 | |
2218 | |
2035 | static int |
2219 | static int |
2036 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2220 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2037 | { |
2221 | { |
2038 | AV *av = (AV *)frame->data; |
2222 | AV *av = (AV *)frame->data; |
2039 | SV *count_sv = AvARRAY (av)[0]; |
2223 | SV *count_sv = AvARRAY (av)[0]; |
2040 | |
2224 | |
|
|
2225 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2226 | if (CORO_THROW) |
|
|
2227 | return 0; |
2041 | if (SvIVX (count_sv) > 0) |
2228 | else if (SvIVX (count_sv) > 0) |
2042 | { |
2229 | { |
2043 | SvSTATE_current->on_destroy = 0; |
2230 | SvSTATE_current->on_destroy = 0; |
|
|
2231 | |
|
|
2232 | if (acquire) |
2044 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2233 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2234 | else |
|
|
2235 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2236 | |
2045 | return 0; |
2237 | return 0; |
2046 | } |
2238 | } |
2047 | else |
2239 | else |
2048 | { |
2240 | { |
2049 | int i; |
2241 | int i; |
… | |
… | |
2058 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2250 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2059 | return 1; |
2251 | return 1; |
2060 | } |
2252 | } |
2061 | } |
2253 | } |
2062 | |
2254 | |
2063 | static void |
2255 | static int |
|
|
2256 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2257 | { |
|
|
2258 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 1); |
|
|
2259 | } |
|
|
2260 | |
|
|
2261 | static int |
|
|
2262 | slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame) |
|
|
2263 | { |
|
|
2264 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 0); |
|
|
2265 | } |
|
|
2266 | |
|
|
2267 | static void |
2064 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2268 | slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2065 | { |
2269 | { |
2066 | AV *av = (AV *)SvRV (arg [0]); |
2270 | AV *av = (AV *)SvRV (arg [0]); |
2067 | |
2271 | |
2068 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2272 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2069 | { |
2273 | { |
2070 | frame->data = (void *)av; |
2274 | frame->data = (void *)av; |
2071 | frame->prepare = prepare_nop; |
2275 | frame->prepare = prepare_nop; |
2072 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2073 | } |
2276 | } |
2074 | else |
2277 | else |
2075 | { |
2278 | { |
2076 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2279 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2077 | |
2280 | |
2078 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2281 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2079 | frame->prepare = prepare_schedule; |
2282 | frame->prepare = prepare_schedule; |
2080 | |
2283 | |
2081 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2284 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2082 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2285 | /* 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; |
2286 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2085 | } |
2287 | } |
|
|
2288 | } |
2086 | |
2289 | |
|
|
2290 | static void |
|
|
2291 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2292 | { |
|
|
2293 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2087 | frame->check = slf_check_semaphore_down; |
2294 | frame->check = slf_check_semaphore_down; |
2088 | |
|
|
2089 | } |
2295 | } |
2090 | |
2296 | |
2091 | /*****************************************************************************/ |
2297 | static void |
2092 | /* gensub: simple closure generation utility */ |
2298 | 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 | { |
2299 | { |
2102 | CV *cv = (CV *)newSV (0); |
2300 | if (items >= 2) |
|
|
2301 | { |
|
|
2302 | /* callback form */ |
|
|
2303 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2304 | CV *cb_cv = coro_sv_2cv (arg [1]); |
2103 | |
2305 | |
2104 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2306 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2105 | |
2307 | |
2106 | CvANON_on (cv); |
2308 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2107 | CvISXSUB_on (cv); |
2309 | coro_semaphore_adjust (aTHX_ av, 0); |
2108 | CvXSUB (cv) = xsub; |
|
|
2109 | GENSUB_ARG = arg; |
|
|
2110 | |
2310 | |
2111 | return newRV_noinc ((SV *)cv); |
2311 | frame->prepare = prepare_nop; |
|
|
2312 | frame->check = slf_check_nop; |
|
|
2313 | } |
|
|
2314 | else |
|
|
2315 | { |
|
|
2316 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
|
|
2317 | frame->check = slf_check_semaphore_wait; |
|
|
2318 | } |
|
|
2319 | } |
|
|
2320 | |
|
|
2321 | /* signal */ |
|
|
2322 | |
|
|
2323 | static void |
|
|
2324 | coro_signal_wake (pTHX_ AV *av, int count) |
|
|
2325 | { |
|
|
2326 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2327 | |
|
|
2328 | /* now signal count waiters */ |
|
|
2329 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2330 | { |
|
|
2331 | SV *cb; |
|
|
2332 | |
|
|
2333 | /* swap first two elements so we can shift a waiter */ |
|
|
2334 | cb = AvARRAY (av)[0]; |
|
|
2335 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2336 | AvARRAY (av)[1] = cb; |
|
|
2337 | |
|
|
2338 | cb = av_shift (av); |
|
|
2339 | |
|
|
2340 | api_ready (aTHX_ cb); |
|
|
2341 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2342 | SvREFCNT_dec (cb); |
|
|
2343 | |
|
|
2344 | --count; |
|
|
2345 | } |
|
|
2346 | } |
|
|
2347 | |
|
|
2348 | static int |
|
|
2349 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2350 | { |
|
|
2351 | /* if we are about to throw, also stop waiting */ |
|
|
2352 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2353 | } |
|
|
2354 | |
|
|
2355 | static void |
|
|
2356 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2357 | { |
|
|
2358 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2359 | |
|
|
2360 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2361 | { |
|
|
2362 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2363 | frame->prepare = prepare_nop; |
|
|
2364 | frame->check = slf_check_nop; |
|
|
2365 | } |
|
|
2366 | else |
|
|
2367 | { |
|
|
2368 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2369 | |
|
|
2370 | av_push (av, waiter); |
|
|
2371 | |
|
|
2372 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2373 | frame->prepare = prepare_schedule; |
|
|
2374 | frame->check = slf_check_signal_wait; |
|
|
2375 | } |
2112 | } |
2376 | } |
2113 | |
2377 | |
2114 | /*****************************************************************************/ |
2378 | /*****************************************************************************/ |
2115 | /* Coro::AIO */ |
2379 | /* Coro::AIO */ |
2116 | |
2380 | |
… | |
… | |
2165 | |
2429 | |
2166 | static int |
2430 | static int |
2167 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2431 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2168 | { |
2432 | { |
2169 | AV *state = (AV *)frame->data; |
2433 | AV *state = (AV *)frame->data; |
|
|
2434 | |
|
|
2435 | /* if we are about to throw, return early */ |
|
|
2436 | /* this does not cancel the aio request, but at least */ |
|
|
2437 | /* it quickly returns */ |
|
|
2438 | if (CORO_THROW) |
|
|
2439 | return 0; |
2170 | |
2440 | |
2171 | /* one element that is an RV? repeat! */ |
2441 | /* one element that is an RV? repeat! */ |
2172 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2442 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2173 | return 1; |
2443 | return 1; |
2174 | |
2444 | |
… | |
… | |
2345 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2615 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2346 | } |
2616 | } |
2347 | |
2617 | |
2348 | SV * |
2618 | SV * |
2349 | new (char *klass, ...) |
2619 | new (char *klass, ...) |
|
|
2620 | ALIAS: |
|
|
2621 | Coro::new = 1 |
2350 | CODE: |
2622 | CODE: |
2351 | { |
2623 | { |
2352 | struct coro *coro; |
2624 | struct coro *coro; |
2353 | MAGIC *mg; |
2625 | MAGIC *mg; |
2354 | HV *hv; |
2626 | HV *hv; |
|
|
2627 | CV *cb; |
2355 | int i; |
2628 | int i; |
|
|
2629 | |
|
|
2630 | if (items > 1) |
|
|
2631 | { |
|
|
2632 | cb = coro_sv_2cv (ST (1)); |
|
|
2633 | |
|
|
2634 | if (!ix) |
|
|
2635 | { |
|
|
2636 | if (CvISXSUB (cb)) |
|
|
2637 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2638 | |
|
|
2639 | if (!CvROOT (cb)) |
|
|
2640 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2641 | } |
|
|
2642 | } |
2356 | |
2643 | |
2357 | Newz (0, coro, 1, struct coro); |
2644 | Newz (0, coro, 1, struct coro); |
2358 | coro->args = newAV (); |
2645 | coro->args = newAV (); |
2359 | coro->flags = CF_NEW; |
2646 | coro->flags = CF_NEW; |
2360 | |
2647 | |
… | |
… | |
2365 | coro->hv = hv = newHV (); |
2652 | coro->hv = hv = newHV (); |
2366 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2653 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2367 | mg->mg_flags |= MGf_DUP; |
2654 | mg->mg_flags |= MGf_DUP; |
2368 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2655 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2369 | |
2656 | |
|
|
2657 | if (items > 1) |
|
|
2658 | { |
2370 | av_extend (coro->args, items - 1); |
2659 | av_extend (coro->args, items - 1 + ix); |
|
|
2660 | |
|
|
2661 | if (ix) |
|
|
2662 | { |
|
|
2663 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2664 | cb = cv_coro_run; |
|
|
2665 | } |
|
|
2666 | |
|
|
2667 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2668 | |
2371 | for (i = 1; i < items; i++) |
2669 | for (i = 2; i < items; i++) |
2372 | av_push (coro->args, newSVsv (ST (i))); |
2670 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2671 | } |
2373 | } |
2672 | } |
2374 | OUTPUT: |
2673 | OUTPUT: |
2375 | RETVAL |
2674 | RETVAL |
2376 | |
2675 | |
2377 | void |
2676 | void |
… | |
… | |
2506 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2805 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2507 | PROTOTYPE: $;$ |
2806 | PROTOTYPE: $;$ |
2508 | CODE: |
2807 | CODE: |
2509 | { |
2808 | { |
2510 | struct coro *current = SvSTATE_current; |
2809 | struct coro *current = SvSTATE_current; |
2511 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2810 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2512 | SvREFCNT_dec (*throwp); |
2811 | SvREFCNT_dec (*throwp); |
2513 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2812 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2514 | } |
2813 | } |
2515 | |
2814 | |
2516 | void |
2815 | void |
… | |
… | |
2578 | int i; |
2877 | int i; |
2579 | |
2878 | |
2580 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2879 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2581 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2880 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2582 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2881 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2583 | |
2882 | cv_coro_run = get_cv (aTHX_ "Coro::_terminate", GV_ADD); |
2584 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2883 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
2585 | SvREADONLY_on (coro_current); |
2884 | SvREADONLY_on (coro_current); |
2586 | |
2885 | |
2587 | coro_stash = gv_stashpv ("Coro", TRUE); |
2886 | coro_stash = gv_stashpv ("Coro", TRUE); |
2588 | |
2887 | |
2589 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2888 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
2605 | coroapi.ready = api_ready; |
2904 | coroapi.ready = api_ready; |
2606 | coroapi.is_ready = api_is_ready; |
2905 | coroapi.is_ready = api_is_ready; |
2607 | coroapi.nready = coro_nready; |
2906 | coroapi.nready = coro_nready; |
2608 | coroapi.current = coro_current; |
2907 | coroapi.current = coro_current; |
2609 | |
2908 | |
2610 | GCoroAPI = &coroapi; |
2909 | /*GCoroAPI = &coroapi;*/ |
2611 | sv_setiv (sv, (IV)&coroapi); |
2910 | sv_setiv (sv, (IV)&coroapi); |
2612 | SvREADONLY_on (sv); |
2911 | SvREADONLY_on (sv); |
2613 | } |
2912 | } |
2614 | } |
2913 | } |
2615 | |
2914 | |
… | |
… | |
2751 | api_trace (aTHX_ coro_current, 0); |
3050 | api_trace (aTHX_ coro_current, 0); |
2752 | |
3051 | |
2753 | av_push (av_async_pool, newSVsv (coro_current)); |
3052 | av_push (av_async_pool, newSVsv (coro_current)); |
2754 | } |
3053 | } |
2755 | |
3054 | |
|
|
3055 | SV * |
|
|
3056 | rouse_cb () |
|
|
3057 | PROTOTYPE: |
|
|
3058 | CODE: |
|
|
3059 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3060 | OUTPUT: |
|
|
3061 | RETVAL |
|
|
3062 | |
|
|
3063 | void |
|
|
3064 | rouse_wait (...) |
|
|
3065 | PROTOTYPE: ;$ |
|
|
3066 | PPCODE: |
|
|
3067 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
|
|
3068 | |
2756 | |
3069 | |
2757 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3070 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2758 | |
3071 | |
2759 | BOOT: |
3072 | BOOT: |
2760 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3073 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2761 | |
3074 | |
2762 | |
3075 | |
2763 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3076 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2764 | |
3077 | |
2765 | SV * |
3078 | SV * |
2766 | new (SV *klass, SV *count_ = 0) |
3079 | new (SV *klass, SV *count = 0) |
2767 | CODE: |
3080 | CODE: |
2768 | { |
3081 | RETVAL = sv_bless ( |
2769 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
3082 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2770 | AV *av = newAV (); |
3083 | GvSTASH (CvGV (cv)) |
2771 | SV **ary; |
3084 | ); |
|
|
3085 | OUTPUT: |
|
|
3086 | RETVAL |
2772 | |
3087 | |
2773 | /* unfortunately, building manually saves memory */ |
3088 | # helper for Coro::Channel |
2774 | Newx (ary, 2, SV *); |
3089 | SV * |
2775 | AvALLOC (av) = ary; |
3090 | _alloc (int count) |
2776 | AvARRAY (av) = ary; |
3091 | CODE: |
2777 | AvMAX (av) = 1; |
3092 | 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: |
3093 | OUTPUT: |
2784 | RETVAL |
3094 | RETVAL |
2785 | |
3095 | |
2786 | SV * |
3096 | SV * |
2787 | count (SV *self) |
3097 | count (SV *self) |
… | |
… | |
2796 | adjust = 1 |
3106 | adjust = 1 |
2797 | CODE: |
3107 | CODE: |
2798 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3108 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2799 | |
3109 | |
2800 | void |
3110 | void |
2801 | down (SV *self) |
3111 | down (...) |
2802 | CODE: |
3112 | CODE: |
2803 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3113 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
3114 | |
|
|
3115 | void |
|
|
3116 | wait (...) |
|
|
3117 | CODE: |
|
|
3118 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2804 | |
3119 | |
2805 | void |
3120 | void |
2806 | try (SV *self) |
3121 | try (SV *self) |
2807 | PPCODE: |
3122 | PPCODE: |
2808 | { |
3123 | { |
… | |
… | |
2820 | XSRETURN_NO; |
3135 | XSRETURN_NO; |
2821 | } |
3136 | } |
2822 | |
3137 | |
2823 | void |
3138 | void |
2824 | waiters (SV *self) |
3139 | waiters (SV *self) |
2825 | CODE: |
3140 | PPCODE: |
2826 | { |
3141 | { |
2827 | AV *av = (AV *)SvRV (self); |
3142 | AV *av = (AV *)SvRV (self); |
|
|
3143 | int wcount = AvFILLp (av) + 1 - 1; |
2828 | |
3144 | |
2829 | if (GIMME_V == G_SCALAR) |
3145 | if (GIMME_V == G_SCALAR) |
2830 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
3146 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2831 | else |
3147 | else |
2832 | { |
3148 | { |
2833 | int i; |
3149 | int i; |
2834 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
3150 | EXTEND (SP, wcount); |
2835 | for (i = 1; i <= AvFILLp (av); ++i) |
3151 | for (i = 1; i <= wcount; ++i) |
2836 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3152 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2837 | } |
3153 | } |
2838 | } |
3154 | } |
|
|
3155 | |
|
|
3156 | MODULE = Coro::State PACKAGE = Coro::Signal |
|
|
3157 | |
|
|
3158 | SV * |
|
|
3159 | new (SV *klass) |
|
|
3160 | CODE: |
|
|
3161 | RETVAL = sv_bless ( |
|
|
3162 | coro_waitarray_new (aTHX_ 0), |
|
|
3163 | GvSTASH (CvGV (cv)) |
|
|
3164 | ); |
|
|
3165 | OUTPUT: |
|
|
3166 | RETVAL |
|
|
3167 | |
|
|
3168 | void |
|
|
3169 | wait (...) |
|
|
3170 | CODE: |
|
|
3171 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3172 | |
|
|
3173 | void |
|
|
3174 | broadcast (SV *self) |
|
|
3175 | CODE: |
|
|
3176 | { |
|
|
3177 | AV *av = (AV *)SvRV (self); |
|
|
3178 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3179 | } |
|
|
3180 | |
|
|
3181 | void |
|
|
3182 | send (SV *self) |
|
|
3183 | CODE: |
|
|
3184 | { |
|
|
3185 | AV *av = (AV *)SvRV (self); |
|
|
3186 | |
|
|
3187 | if (AvFILLp (av)) |
|
|
3188 | coro_signal_wake (aTHX_ av, 1); |
|
|
3189 | else |
|
|
3190 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
|
|
3191 | } |
|
|
3192 | |
|
|
3193 | IV |
|
|
3194 | awaited (SV *self) |
|
|
3195 | CODE: |
|
|
3196 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3197 | OUTPUT: |
|
|
3198 | RETVAL |
2839 | |
3199 | |
2840 | |
3200 | |
2841 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3201 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2842 | |
3202 | |
2843 | BOOT: |
3203 | BOOT: |
… | |
… | |
2871 | |
3231 | |
2872 | void |
3232 | void |
2873 | _register (char *target, char *proto, SV *req) |
3233 | _register (char *target, char *proto, SV *req) |
2874 | CODE: |
3234 | CODE: |
2875 | { |
3235 | { |
2876 | HV *st; |
|
|
2877 | GV *gvp; |
|
|
2878 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
3236 | CV *req_cv = coro_sv_2cv (req); |
2879 | /* newXSproto doesn't return the CV on 5.8 */ |
3237 | /* newXSproto doesn't return the CV on 5.8 */ |
2880 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3238 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
2881 | sv_setpv ((SV *)slf_cv, proto); |
3239 | sv_setpv ((SV *)slf_cv, proto); |
2882 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3240 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
2883 | } |
3241 | } |