… | |
… | |
142 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
143 | |
143 | |
144 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
145 | |
145 | |
146 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
147 | |
|
|
148 | static perl_mutex coro_lock; |
|
|
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
|
|
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
147 | # if CORO_PTHREAD |
152 | static void *coro_thx; |
148 | static void *coro_thx; |
153 | # endif |
149 | # endif |
154 | |
|
|
155 | #else |
|
|
156 | |
|
|
157 | # define LOCK (void)0 |
|
|
158 | # define UNLOCK (void)0 |
|
|
159 | |
|
|
160 | #endif |
150 | #endif |
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
|
|
166 | |
151 | |
167 | /* helper storage struct for Coro::AIO */ |
152 | /* helper storage struct for Coro::AIO */ |
168 | struct io_state |
153 | struct io_state |
169 | { |
154 | { |
170 | AV *res; |
155 | AV *res; |
… | |
… | |
183 | static JMPENV *main_top_env; |
168 | static JMPENV *main_top_env; |
184 | static HV *coro_state_stash, *coro_stash; |
169 | static HV *coro_state_stash, *coro_stash; |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
186 | static volatile struct coro *transfer_next; |
171 | static volatile struct coro *transfer_next; |
187 | |
172 | |
188 | struct transfer_args |
|
|
189 | { |
|
|
190 | struct coro *prev, *next; |
|
|
191 | }; |
|
|
192 | |
|
|
193 | static GV *irsgv; /* $/ */ |
173 | static GV *irsgv; /* $/ */ |
194 | static GV *stdoutgv; /* *STDOUT */ |
174 | static GV *stdoutgv; /* *STDOUT */ |
195 | static SV *rv_diehook; |
175 | static SV *rv_diehook; |
196 | static SV *rv_warnhook; |
176 | static SV *rv_warnhook; |
197 | static HV *hv_sig; /* %SIG */ |
177 | static HV *hv_sig; /* %SIG */ |
… | |
… | |
215 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
195 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
216 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
196 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
217 | }; |
197 | }; |
218 | |
198 | |
219 | /* this is a structure representing a c-level coroutine */ |
199 | /* this is a structure representing a c-level coroutine */ |
220 | typedef struct coro_cctx { |
200 | typedef struct coro_cctx |
|
|
201 | { |
221 | struct coro_cctx *next; |
202 | struct coro_cctx *next; |
222 | |
203 | |
223 | /* the stack */ |
204 | /* the stack */ |
224 | void *sptr; |
205 | void *sptr; |
225 | size_t ssize; |
206 | size_t ssize; |
… | |
… | |
243 | CF_NEW = 0x0004, /* has never been switched to */ |
224 | CF_NEW = 0x0004, /* has never been switched to */ |
244 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
225 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
245 | }; |
226 | }; |
246 | |
227 | |
247 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
228 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
248 | typedef struct { |
229 | typedef struct |
|
|
230 | { |
249 | SV *defsv; |
231 | SV *defsv; |
250 | AV *defav; |
232 | AV *defav; |
251 | SV *errsv; |
233 | SV *errsv; |
252 | SV *irsgv; |
234 | SV *irsgv; |
253 | #define VAR(name,type) type name; |
235 | #define VAR(name,type) type name; |
… | |
… | |
257 | |
239 | |
258 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
240 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
259 | |
241 | |
260 | /* this is a structure representing a perl-level coroutine */ |
242 | /* this is a structure representing a perl-level coroutine */ |
261 | struct coro { |
243 | struct coro { |
262 | /* the c coroutine allocated to this perl coroutine, if any */ |
244 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | coro_cctx *cctx; |
245 | coro_cctx *cctx; |
264 | |
246 | |
265 | /* process data */ |
247 | /* process data */ |
|
|
248 | struct CoroSLF slf_frame; /* saved slf frame */ |
266 | AV *mainstack; |
249 | AV *mainstack; |
267 | perl_slots *slot; /* basically the saved sp */ |
250 | perl_slots *slot; /* basically the saved sp */ |
268 | |
251 | |
269 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | AV *args; /* data associated with this coroutine (initial args) */ |
270 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int refcnt; /* coroutines are refcounted, yes */ |
271 | int flags; /* CF_ flags */ |
254 | int flags; /* CF_ flags */ |
272 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
256 | void (*on_destroy)(pTHX_ struct coro *coro); |
273 | |
257 | |
274 | /* statistics */ |
258 | /* statistics */ |
275 | int usecount; /* number of transfers to this coro */ |
259 | int usecount; /* number of transfers to this coro */ |
276 | |
260 | |
277 | /* coro process data */ |
261 | /* coro process data */ |
… | |
… | |
285 | struct coro *next, *prev; |
269 | struct coro *next, *prev; |
286 | }; |
270 | }; |
287 | |
271 | |
288 | typedef struct coro *Coro__State; |
272 | typedef struct coro *Coro__State; |
289 | typedef struct coro *Coro__State_or_hashref; |
273 | typedef struct coro *Coro__State_or_hashref; |
|
|
274 | |
|
|
275 | static struct CoroSLF slf_frame; /* the current slf frame */ |
290 | |
276 | |
291 | /** Coro ********************************************************************/ |
277 | /** Coro ********************************************************************/ |
292 | |
278 | |
293 | #define PRIO_MAX 3 |
279 | #define PRIO_MAX 3 |
294 | #define PRIO_HIGH 1 |
280 | #define PRIO_HIGH 1 |
… | |
… | |
514 | CvPADLIST (cv) = (AV *)POPs; |
500 | CvPADLIST (cv) = (AV *)POPs; |
515 | } |
501 | } |
516 | |
502 | |
517 | PUTBACK; |
503 | PUTBACK; |
518 | } |
504 | } |
|
|
505 | |
|
|
506 | slf_frame = c->slf_frame; |
519 | } |
507 | } |
520 | |
508 | |
521 | static void |
509 | static void |
522 | save_perl (pTHX_ Coro__State c) |
510 | save_perl (pTHX_ Coro__State c) |
523 | { |
511 | { |
|
|
512 | c->slf_frame = slf_frame; |
|
|
513 | |
524 | { |
514 | { |
525 | dSP; |
515 | dSP; |
526 | I32 cxix = cxstack_ix; |
516 | I32 cxix = cxstack_ix; |
527 | PERL_CONTEXT *ccstk = cxstack; |
517 | PERL_CONTEXT *ccstk = cxstack; |
528 | PERL_SI *top_si = PL_curstackinfo; |
518 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
595 | #undef VAR |
585 | #undef VAR |
596 | } |
586 | } |
597 | } |
587 | } |
598 | |
588 | |
599 | /* |
589 | /* |
600 | * allocate various perl stacks. This is an exact copy |
590 | * allocate various perl stacks. This is almost an exact copy |
601 | * of perl.c:init_stacks, except that it uses less memory |
591 | * of perl.c:init_stacks, except that it uses less memory |
602 | * on the (sometimes correct) assumption that coroutines do |
592 | * on the (sometimes correct) assumption that coroutines do |
603 | * not usually need a lot of stackspace. |
593 | * not usually need a lot of stackspace. |
604 | */ |
594 | */ |
605 | #if CORO_PREFER_PERL_FUNCTIONS |
595 | #if CORO_PREFER_PERL_FUNCTIONS |
… | |
… | |
712 | #endif |
702 | #endif |
713 | } |
703 | } |
714 | } |
704 | } |
715 | |
705 | |
716 | return rss; |
706 | return rss; |
717 | } |
|
|
718 | |
|
|
719 | /** set stacklevel support **************************************************/ |
|
|
720 | |
|
|
721 | /* we sometimes need to create the effect of pp_slf calling us */ |
|
|
722 | #define SLF_HEAD (void)0 |
|
|
723 | /* we sometimes need to create the effect of leaving via pp_slf */ |
|
|
724 | #define SLF_TAIL slf_tail (aTHX) |
|
|
725 | |
|
|
726 | INLINE void |
|
|
727 | slf_tail (pTHX) |
|
|
728 | { |
|
|
729 | dSP; |
|
|
730 | SV **bot = SP; |
|
|
731 | |
|
|
732 | int gimme = GIMME_V; |
|
|
733 | |
|
|
734 | /* make sure we put something on the stack in scalar context */ |
|
|
735 | if (gimme == G_SCALAR) |
|
|
736 | { |
|
|
737 | if (sp == bot) |
|
|
738 | XPUSHs (&PL_sv_undef); |
|
|
739 | |
|
|
740 | SP = bot + 1; |
|
|
741 | } |
|
|
742 | |
|
|
743 | PUTBACK; |
|
|
744 | } |
707 | } |
745 | |
708 | |
746 | /** coroutine stack handling ************************************************/ |
709 | /** coroutine stack handling ************************************************/ |
747 | |
710 | |
748 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
711 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
… | |
… | |
834 | |
797 | |
835 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
798 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
836 | } |
799 | } |
837 | |
800 | |
838 | static void |
801 | static void |
|
|
802 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
|
|
803 | { |
|
|
804 | /* kind of mega-hacky, but works */ |
|
|
805 | ta->next = ta->prev = (struct coro *)ta; |
|
|
806 | } |
|
|
807 | |
|
|
808 | static int |
|
|
809 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
|
|
810 | { |
|
|
811 | return 0; |
|
|
812 | } |
|
|
813 | |
|
|
814 | static void |
839 | coro_setup (pTHX_ struct coro *coro) |
815 | coro_setup (pTHX_ struct coro *coro) |
840 | { |
816 | { |
841 | /* |
817 | /* |
842 | * emulate part of the perl startup here. |
818 | * emulate part of the perl startup here. |
843 | */ |
819 | */ |
… | |
… | |
882 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
858 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
883 | SPAGAIN; |
859 | SPAGAIN; |
884 | } |
860 | } |
885 | |
861 | |
886 | /* this newly created coroutine might be run on an existing cctx which most |
862 | /* this newly created coroutine might be run on an existing cctx which most |
887 | * likely was suspended in set_stacklevel, called from pp_set_stacklevel, |
863 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
888 | * so we have to emulate entering pp_set_stacklevel here. |
|
|
889 | */ |
864 | */ |
890 | SLF_HEAD; |
865 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
|
|
866 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
891 | } |
867 | } |
892 | |
868 | |
893 | static void |
869 | static void |
894 | coro_destruct (pTHX_ struct coro *coro) |
870 | coro_destruct (pTHX_ struct coro *coro) |
895 | { |
871 | { |
… | |
… | |
1059 | TAINT_NOT; |
1035 | TAINT_NOT; |
1060 | return 0; |
1036 | return 0; |
1061 | } |
1037 | } |
1062 | |
1038 | |
1063 | static void |
1039 | static void |
1064 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
1040 | prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) |
1065 | { |
1041 | { |
1066 | ta->prev = (struct coro *)cctx; |
1042 | ta->prev = (struct coro *)cctx; |
1067 | ta->next = 0; |
1043 | ta->next = 0; |
1068 | } |
1044 | } |
1069 | |
1045 | |
… | |
… | |
1103 | struct coro *next = (struct coro *)transfer_next; |
1079 | struct coro *next = (struct coro *)transfer_next; |
1104 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
1080 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
1105 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1081 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1106 | |
1082 | |
1107 | free_coro_mortal (aTHX); |
1083 | free_coro_mortal (aTHX); |
1108 | UNLOCK; |
|
|
1109 | |
1084 | |
1110 | if (expect_false (next->throw)) |
1085 | if (expect_false (next->throw)) |
1111 | { |
1086 | { |
1112 | SV *exception = sv_2mortal (next->throw); |
1087 | SV *exception = sv_2mortal (next->throw); |
1113 | |
1088 | |
… | |
… | |
1129 | # endif |
1104 | # endif |
1130 | #endif |
1105 | #endif |
1131 | { |
1106 | { |
1132 | dTHX; |
1107 | dTHX; |
1133 | |
1108 | |
1134 | /* we are the alternative tail to pp_set_stacklevel */ |
1109 | /* normally we would need to skip the entersub here */ |
1135 | /* so do the same things here */ |
1110 | /* not doing so will re-execute it, which is exactly what we want */ |
1136 | SLF_TAIL; |
|
|
1137 | |
|
|
1138 | /* we now skip the op that did lead to transfer() */ |
|
|
1139 | PL_op = PL_op->op_next; |
1111 | /* PL_nop = PL_nop->op_next */ |
1140 | |
1112 | |
1141 | /* inject a fake subroutine call to cctx_init */ |
1113 | /* inject a fake subroutine call to cctx_init */ |
1142 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1114 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1143 | |
1115 | |
1144 | /* cctx_run is the alternative tail of transfer() */ |
1116 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1117 | /* TODO: throwing an exception here might be deadly, VERIFY */ |
1145 | transfer_tail (aTHX); |
1118 | transfer_tail (aTHX); |
1146 | |
1119 | |
1147 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1120 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1148 | PL_restartop = PL_op; |
1121 | PL_restartop = PL_op; |
1149 | perl_run (PL_curinterp); |
1122 | perl_run (PL_curinterp); |
… | |
… | |
1308 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1281 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1309 | { |
1282 | { |
1310 | if (expect_true (prev != next)) |
1283 | if (expect_true (prev != next)) |
1311 | { |
1284 | { |
1312 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1285 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1313 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
1286 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1314 | |
1287 | |
1315 | if (expect_false (next->flags & CF_RUNNING)) |
1288 | if (expect_false (next->flags & CF_RUNNING)) |
1316 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1289 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1317 | |
1290 | |
1318 | if (expect_false (next->flags & CF_DESTROYED)) |
1291 | if (expect_false (next->flags & CF_DESTROYED)) |
1319 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1292 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1320 | |
1293 | |
1321 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1294 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1322 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1295 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1323 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); |
1296 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1324 | #endif |
1297 | #endif |
1325 | } |
1298 | } |
1326 | } |
1299 | } |
1327 | |
1300 | |
1328 | /* always use the TRANSFER macro */ |
1301 | /* always use the TRANSFER macro */ |
… | |
… | |
1332 | dSTACKLEVEL; |
1305 | dSTACKLEVEL; |
1333 | |
1306 | |
1334 | /* sometimes transfer is only called to set idle_sp */ |
1307 | /* sometimes transfer is only called to set idle_sp */ |
1335 | if (expect_false (!next)) |
1308 | if (expect_false (!next)) |
1336 | { |
1309 | { |
1337 | ((coro_cctx *)prev)->idle_sp = stacklevel; |
1310 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1338 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1311 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1339 | } |
1312 | } |
1340 | else if (expect_true (prev != next)) |
1313 | else if (expect_true (prev != next)) |
1341 | { |
1314 | { |
1342 | coro_cctx *prev__cctx; |
1315 | coro_cctx *prev__cctx; |
… | |
… | |
1349 | prev->flags |= CF_RUNNING; |
1322 | prev->flags |= CF_RUNNING; |
1350 | } |
1323 | } |
1351 | |
1324 | |
1352 | prev->flags &= ~CF_RUNNING; |
1325 | prev->flags &= ~CF_RUNNING; |
1353 | next->flags |= CF_RUNNING; |
1326 | next->flags |= CF_RUNNING; |
1354 | |
|
|
1355 | LOCK; |
|
|
1356 | |
1327 | |
1357 | /* first get rid of the old state */ |
1328 | /* first get rid of the old state */ |
1358 | save_perl (aTHX_ prev); |
1329 | save_perl (aTHX_ prev); |
1359 | |
1330 | |
1360 | if (expect_false (next->flags & CF_NEW)) |
1331 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1369 | |
1340 | |
1370 | prev__cctx = prev->cctx; |
1341 | prev__cctx = prev->cctx; |
1371 | |
1342 | |
1372 | /* possibly untie and reuse the cctx */ |
1343 | /* possibly untie and reuse the cctx */ |
1373 | if (expect_true ( |
1344 | if (expect_true ( |
1374 | prev__cctx->idle_sp == stacklevel |
1345 | prev__cctx->idle_sp == (void *)stacklevel |
1375 | && !(prev__cctx->flags & CC_TRACE) |
1346 | && !(prev__cctx->flags & CC_TRACE) |
1376 | && !force_cctx |
1347 | && !force_cctx |
1377 | )) |
1348 | )) |
1378 | { |
1349 | { |
1379 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1350 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1418 | coro_state_destroy (pTHX_ struct coro *coro) |
1389 | coro_state_destroy (pTHX_ struct coro *coro) |
1419 | { |
1390 | { |
1420 | if (coro->flags & CF_DESTROYED) |
1391 | if (coro->flags & CF_DESTROYED) |
1421 | return 0; |
1392 | return 0; |
1422 | |
1393 | |
|
|
1394 | if (coro->on_destroy) |
|
|
1395 | coro->on_destroy (aTHX_ coro); |
|
|
1396 | |
1423 | coro->flags |= CF_DESTROYED; |
1397 | coro->flags |= CF_DESTROYED; |
1424 | |
1398 | |
1425 | if (coro->flags & CF_READY) |
1399 | if (coro->flags & CF_READY) |
1426 | { |
1400 | { |
1427 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1401 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1428 | /* alternative: look through all ready queues and remove the coro */ |
1402 | /* alternative: look through all ready queues and remove the coro */ |
1429 | LOCK; |
|
|
1430 | --coro_nready; |
1403 | --coro_nready; |
1431 | UNLOCK; |
|
|
1432 | } |
1404 | } |
1433 | else |
1405 | else |
1434 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1406 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1435 | |
1407 | |
1436 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1408 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1437 | { |
1409 | { |
1438 | struct coro temp; |
1410 | struct coro temp; |
1439 | |
1411 | |
1440 | if (coro->flags & CF_RUNNING) |
1412 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1441 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
1442 | |
1413 | |
1443 | save_perl (aTHX_ &temp); |
1414 | save_perl (aTHX_ &temp); |
1444 | load_perl (aTHX_ coro); |
1415 | load_perl (aTHX_ coro); |
1445 | |
1416 | |
1446 | coro_destruct (aTHX_ coro); |
1417 | coro_destruct (aTHX_ coro); |
… | |
… | |
1497 | # define MGf_DUP 0 |
1468 | # define MGf_DUP 0 |
1498 | #endif |
1469 | #endif |
1499 | }; |
1470 | }; |
1500 | |
1471 | |
1501 | static void |
1472 | static void |
1502 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1473 | prepare_transfer (pTHX_ struct coro_transfer_args *ta, SV *prev_sv, SV *next_sv) |
1503 | { |
1474 | { |
1504 | ta->prev = SvSTATE (prev_sv); |
1475 | ta->prev = SvSTATE (prev_sv); |
1505 | ta->next = SvSTATE (next_sv); |
1476 | ta->next = SvSTATE (next_sv); |
1506 | TRANSFER_CHECK (*ta); |
1477 | TRANSFER_CHECK (*ta); |
1507 | } |
1478 | } |
1508 | |
1479 | |
1509 | static void |
1480 | static void |
1510 | api_transfer (pTHX_ SV *prev_sv, SV *next_sv) |
1481 | api_transfer (pTHX_ SV *prev_sv, SV *next_sv) |
1511 | { |
1482 | { |
1512 | struct transfer_args ta; |
1483 | struct coro_transfer_args ta; |
1513 | |
1484 | |
1514 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1485 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1515 | TRANSFER (ta, 1); |
1486 | TRANSFER (ta, 1); |
1516 | } |
1487 | } |
1517 | |
1488 | |
… | |
… | |
1550 | if (coro->flags & CF_READY) |
1521 | if (coro->flags & CF_READY) |
1551 | return 0; |
1522 | return 0; |
1552 | |
1523 | |
1553 | coro->flags |= CF_READY; |
1524 | coro->flags |= CF_READY; |
1554 | |
1525 | |
1555 | LOCK; |
|
|
1556 | |
|
|
1557 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1526 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1558 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1527 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1559 | |
1528 | |
1560 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1529 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1561 | ++coro_nready; |
1530 | ++coro_nready; |
1562 | |
1531 | |
1563 | UNLOCK; |
|
|
1564 | |
|
|
1565 | if (sv_hook) |
1532 | if (sv_hook) |
1566 | { |
1533 | { |
1567 | dSP; |
1534 | dSP; |
1568 | |
1535 | |
1569 | ENTER; |
1536 | ENTER; |
… | |
… | |
1589 | { |
1556 | { |
1590 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1557 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1591 | } |
1558 | } |
1592 | |
1559 | |
1593 | INLINE void |
1560 | INLINE void |
1594 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1561 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1595 | { |
1562 | { |
1596 | SV *prev_sv, *next_sv; |
1563 | SV *prev_sv, *next_sv; |
1597 | |
1564 | |
1598 | for (;;) |
1565 | for (;;) |
1599 | { |
1566 | { |
1600 | LOCK; |
|
|
1601 | next_sv = coro_deq (aTHX); |
1567 | next_sv = coro_deq (aTHX); |
1602 | |
1568 | |
1603 | /* nothing to schedule: call the idle handler */ |
1569 | /* nothing to schedule: call the idle handler */ |
1604 | if (expect_false (!next_sv)) |
1570 | if (expect_false (!next_sv)) |
1605 | { |
1571 | { |
1606 | dSP; |
1572 | dSP; |
1607 | UNLOCK; |
|
|
1608 | |
1573 | |
1609 | ENTER; |
1574 | ENTER; |
1610 | SAVETMPS; |
1575 | SAVETMPS; |
1611 | |
1576 | |
1612 | PUSHMARK (SP); |
1577 | PUSHMARK (SP); |
… | |
… | |
1622 | ta->next = SvSTATE (next_sv); |
1587 | ta->next = SvSTATE (next_sv); |
1623 | |
1588 | |
1624 | /* cannot transfer to destroyed coros, skip and look for next */ |
1589 | /* cannot transfer to destroyed coros, skip and look for next */ |
1625 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1590 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1626 | { |
1591 | { |
1627 | UNLOCK; |
|
|
1628 | SvREFCNT_dec (next_sv); |
1592 | SvREFCNT_dec (next_sv); |
1629 | /* coro_nready has already been taken care of by destroy */ |
1593 | /* coro_nready has already been taken care of by destroy */ |
1630 | continue; |
1594 | continue; |
1631 | } |
1595 | } |
1632 | |
1596 | |
1633 | --coro_nready; |
1597 | --coro_nready; |
1634 | UNLOCK; |
|
|
1635 | break; |
1598 | break; |
1636 | } |
1599 | } |
1637 | |
1600 | |
1638 | /* free this only after the transfer */ |
1601 | /* free this only after the transfer */ |
1639 | prev_sv = SvRV (coro_current); |
1602 | prev_sv = SvRV (coro_current); |
… | |
… | |
1641 | TRANSFER_CHECK (*ta); |
1604 | TRANSFER_CHECK (*ta); |
1642 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1605 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1643 | ta->next->flags &= ~CF_READY; |
1606 | ta->next->flags &= ~CF_READY; |
1644 | SvRV_set (coro_current, next_sv); |
1607 | SvRV_set (coro_current, next_sv); |
1645 | |
1608 | |
1646 | LOCK; |
|
|
1647 | free_coro_mortal (aTHX); |
1609 | free_coro_mortal (aTHX); |
1648 | coro_mortal = prev_sv; |
1610 | coro_mortal = prev_sv; |
1649 | UNLOCK; |
|
|
1650 | } |
1611 | } |
1651 | |
1612 | |
1652 | INLINE void |
1613 | INLINE void |
1653 | prepare_cede (pTHX_ struct transfer_args *ta) |
1614 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1654 | { |
1615 | { |
1655 | api_ready (aTHX_ coro_current); |
1616 | api_ready (aTHX_ coro_current); |
1656 | prepare_schedule (aTHX_ ta); |
1617 | prepare_schedule (aTHX_ ta); |
1657 | } |
1618 | } |
1658 | |
1619 | |
1659 | static void |
1620 | INLINE void |
1660 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1621 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1661 | { |
1622 | { |
1662 | SV *prev = SvRV (coro_current); |
1623 | SV *prev = SvRV (coro_current); |
1663 | |
1624 | |
1664 | if (coro_nready) |
1625 | if (coro_nready) |
1665 | { |
1626 | { |
1666 | prepare_schedule (aTHX_ ta); |
1627 | prepare_schedule (aTHX_ ta); |
1667 | api_ready (aTHX_ prev); |
1628 | api_ready (aTHX_ prev); |
1668 | } |
1629 | } |
1669 | else |
1630 | else |
1670 | ta->prev = ta->next = SvSTATE (prev); |
1631 | prepare_nop (aTHX_ ta); |
1671 | } |
1632 | } |
1672 | |
1633 | |
1673 | static void |
1634 | static void |
1674 | api_schedule (pTHX) |
1635 | api_schedule (pTHX) |
1675 | { |
1636 | { |
1676 | struct transfer_args ta; |
1637 | struct coro_transfer_args ta; |
1677 | |
1638 | |
1678 | prepare_schedule (aTHX_ &ta); |
1639 | prepare_schedule (aTHX_ &ta); |
1679 | TRANSFER (ta, 1); |
1640 | TRANSFER (ta, 1); |
1680 | } |
1641 | } |
1681 | |
1642 | |
1682 | static int |
1643 | static int |
1683 | api_cede (pTHX) |
1644 | api_cede (pTHX) |
1684 | { |
1645 | { |
1685 | struct transfer_args ta; |
1646 | struct coro_transfer_args ta; |
1686 | |
1647 | |
1687 | prepare_cede (aTHX_ &ta); |
1648 | prepare_cede (aTHX_ &ta); |
1688 | |
1649 | |
1689 | if (expect_true (ta.prev != ta.next)) |
1650 | if (expect_true (ta.prev != ta.next)) |
1690 | { |
1651 | { |
… | |
… | |
1698 | static int |
1659 | static int |
1699 | api_cede_notself (pTHX) |
1660 | api_cede_notself (pTHX) |
1700 | { |
1661 | { |
1701 | if (coro_nready) |
1662 | if (coro_nready) |
1702 | { |
1663 | { |
1703 | struct transfer_args ta; |
1664 | struct coro_transfer_args ta; |
1704 | |
1665 | |
1705 | prepare_cede_notself (aTHX_ &ta); |
1666 | prepare_cede_notself (aTHX_ &ta); |
1706 | TRANSFER (ta, 1); |
1667 | TRANSFER (ta, 1); |
1707 | return 1; |
1668 | return 1; |
1708 | } |
1669 | } |
… | |
… | |
1718 | if (flags & CC_TRACE) |
1679 | if (flags & CC_TRACE) |
1719 | { |
1680 | { |
1720 | if (!coro->cctx) |
1681 | if (!coro->cctx) |
1721 | coro->cctx = cctx_new_run (); |
1682 | coro->cctx = cctx_new_run (); |
1722 | else if (!(coro->cctx->flags & CC_TRACE)) |
1683 | else if (!(coro->cctx->flags & CC_TRACE)) |
1723 | croak ("cannot enable tracing on coroutine with custom stack"); |
1684 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1724 | |
1685 | |
1725 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1686 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1726 | } |
1687 | } |
1727 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1688 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1728 | { |
1689 | { |
… | |
… | |
1732 | PL_runops = RUNOPS_DEFAULT; |
1693 | PL_runops = RUNOPS_DEFAULT; |
1733 | else |
1694 | else |
1734 | coro->slot->runops = RUNOPS_DEFAULT; |
1695 | coro->slot->runops = RUNOPS_DEFAULT; |
1735 | } |
1696 | } |
1736 | } |
1697 | } |
1737 | |
|
|
1738 | #if 0 |
|
|
1739 | static int |
|
|
1740 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1741 | { |
|
|
1742 | AV *padlist; |
|
|
1743 | AV *av = (AV *)mg->mg_obj; |
|
|
1744 | |
|
|
1745 | abort (); |
|
|
1746 | |
|
|
1747 | return 0; |
|
|
1748 | } |
|
|
1749 | |
|
|
1750 | static MGVTBL coro_gensub_vtbl = { |
|
|
1751 | 0, 0, 0, 0, |
|
|
1752 | coro_gensub_free |
|
|
1753 | }; |
|
|
1754 | #endif |
|
|
1755 | |
1698 | |
1756 | /*****************************************************************************/ |
1699 | /*****************************************************************************/ |
1757 | /* PerlIO::cede */ |
1700 | /* PerlIO::cede */ |
1758 | |
1701 | |
1759 | typedef struct |
1702 | typedef struct |
… | |
… | |
1833 | static const CV *slf_cv; /* for quick consistency check */ |
1776 | static const CV *slf_cv; /* for quick consistency check */ |
1834 | |
1777 | |
1835 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1778 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1836 | static SV *slf_arg0; |
1779 | static SV *slf_arg0; |
1837 | static SV *slf_arg1; |
1780 | static SV *slf_arg1; |
|
|
1781 | static SV *slf_arg2; |
1838 | |
1782 | |
1839 | /* this restores the stack in the case we patched the entersub, to */ |
1783 | /* this restores the stack in the case we patched the entersub, to */ |
1840 | /* recreate the stack frame as perl will on following calls */ |
1784 | /* recreate the stack frame as perl will on following calls */ |
1841 | /* since entersub cleared the stack */ |
1785 | /* since entersub cleared the stack */ |
1842 | static OP * |
1786 | static OP * |
… | |
… | |
1847 | PUSHMARK (SP); |
1791 | PUSHMARK (SP); |
1848 | |
1792 | |
1849 | EXTEND (SP, 3); |
1793 | EXTEND (SP, 3); |
1850 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1794 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1851 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
1795 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
|
|
1796 | if (slf_arg2) PUSHs (sv_2mortal (slf_arg2)); |
1852 | PUSHs ((SV *)CvGV (slf_cv)); |
1797 | PUSHs ((SV *)CvGV (slf_cv)); |
1853 | |
1798 | |
1854 | RETURNOP (slf_restore.op_first); |
1799 | RETURNOP (slf_restore.op_first); |
1855 | } |
1800 | } |
1856 | |
1801 | |
1857 | #define OPpENTERSUB_SLF 15 /* the part of op_private entersub hopefully doesn't use */ |
1802 | static void |
|
|
1803 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
|
|
1804 | { |
|
|
1805 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
|
|
1806 | } |
1858 | |
1807 | |
1859 | enum { |
1808 | static void |
1860 | CORO_SLF_CUSTOM = 0, |
1809 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1861 | CORO_SLF_SET_STACKLEVEL = 1, |
1810 | { |
1862 | CORO_SLF_TRANSFER = 2 |
1811 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1863 | }; |
|
|
1864 | |
1812 | |
1865 | /* declare prototype */ |
1813 | frame->prepare = slf_prepare_set_stacklevel; |
1866 | XS(XS_Coro__State__set_stacklevel); |
1814 | frame->check = slf_check_nop; |
|
|
1815 | frame->data = (void *)SvIV (arg [0]); |
|
|
1816 | } |
|
|
1817 | |
|
|
1818 | static void |
|
|
1819 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
|
|
1820 | { |
|
|
1821 | SV **arg = (SV **)slf_frame.data; |
|
|
1822 | |
|
|
1823 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
|
|
1824 | } |
|
|
1825 | |
|
|
1826 | static void |
|
|
1827 | slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1828 | { |
|
|
1829 | if (items != 2) |
|
|
1830 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
|
|
1831 | |
|
|
1832 | frame->prepare = slf_prepare_transfer; |
|
|
1833 | frame->check = slf_check_nop; |
|
|
1834 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
|
|
1835 | } |
|
|
1836 | |
|
|
1837 | static void |
|
|
1838 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1839 | { |
|
|
1840 | frame->prepare = prepare_schedule; |
|
|
1841 | frame->check = slf_check_nop; |
|
|
1842 | } |
|
|
1843 | |
|
|
1844 | static void |
|
|
1845 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1846 | { |
|
|
1847 | frame->prepare = prepare_cede; |
|
|
1848 | frame->check = slf_check_nop; |
|
|
1849 | } |
|
|
1850 | |
|
|
1851 | static void |
|
|
1852 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1853 | { |
|
|
1854 | frame->prepare = prepare_cede_notself; |
|
|
1855 | frame->check = slf_check_nop; |
|
|
1856 | } |
|
|
1857 | |
|
|
1858 | /* we hijack an hopefully unused CV flag for our purposes */ |
|
|
1859 | #define CVf_SLF 0x4000 |
1867 | |
1860 | |
1868 | /* |
1861 | /* |
1869 | * these not obviously related functions are all rolled into one |
1862 | * these not obviously related functions are all rolled into one |
1870 | * function to increase chances that they all will call transfer with the same |
1863 | * function to increase chances that they all will call transfer with the same |
1871 | * stack offset |
1864 | * stack offset |
1872 | * SLF stands for "schedule-like-function". |
1865 | * SLF stands for "schedule-like-function". |
1873 | */ |
1866 | */ |
1874 | static OP * |
1867 | static OP * |
1875 | pp_slf (pTHX) |
1868 | pp_slf (pTHX) |
1876 | { |
1869 | { |
|
|
1870 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
|
|
1871 | |
|
|
1872 | /* set up the slf frame, unless it has already been set-up */ |
|
|
1873 | /* the latter happens when a new coro has been started */ |
|
|
1874 | /* or when a new cctx was attached to an existing coroutine */ |
|
|
1875 | if (expect_true (!slf_frame.prepare)) |
|
|
1876 | { |
|
|
1877 | /* first iteration */ |
1877 | dSP; |
1878 | dSP; |
1878 | struct transfer_args ta; |
|
|
1879 | SV **arg = PL_stack_base + TOPMARK + 1; |
1879 | SV **arg = PL_stack_base + TOPMARK + 1; |
1880 | int items = SP - arg; /* args without function object */ |
1880 | int items = SP - arg; /* args without function object */ |
1881 | int ix = PL_op->op_private & OPpENTERSUB_SLF; |
|
|
1882 | struct CoroSLF *slf = 0; |
|
|
1883 | SV *gv = *sp; |
1881 | SV *gv = *sp; |
1884 | |
1882 | |
1885 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1883 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1886 | /* for us, divert to the real entersub */ |
1884 | /* for us, divert to the real entersub */ |
1887 | if (SvTYPE (gv) != SVt_PVGV || CvXSUB (GvCV (gv)) != XS_Coro__State__set_stacklevel) |
1885 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1888 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1886 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1889 | |
1887 | |
1890 | /* pop args */ |
1888 | /* pop args */ |
1891 | SP = PL_stack_base + POPMARK; |
1889 | SP = PL_stack_base + POPMARK; |
1892 | |
1890 | |
1893 | if (!(PL_op->op_flags & OPf_STACKED)) |
1891 | if (!(PL_op->op_flags & OPf_STACKED)) |
1894 | { |
1892 | { |
1895 | /* ampersand-form of call, use @_ instead of stack */ |
1893 | /* ampersand-form of call, use @_ instead of stack */ |
1896 | AV *av = GvAV (PL_defgv); |
1894 | AV *av = GvAV (PL_defgv); |
1897 | arg = AvARRAY (av); |
1895 | arg = AvARRAY (av); |
1898 | items = AvFILLp (av) + 1; |
1896 | items = AvFILLp (av) + 1; |
1899 | } |
1897 | } |
1900 | |
1898 | |
1901 | PUTBACK; |
1899 | PUTBACK; |
1902 | |
1900 | |
1903 | if (!ix) |
1901 | /* now call the init function, which needs to set up slf_frame */ |
|
|
1902 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) |
|
|
1903 | (aTHX_ &slf_frame, GvCV (gv), arg, items); |
1904 | { |
1904 | } |
1905 | slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr; |
|
|
1906 | ix = slf->prepare (aTHX_ arg, items); |
|
|
1907 | } |
|
|
1908 | |
1905 | |
1909 | switch (ix) |
1906 | /* now that we have a slf_frame, interpret it! */ |
1910 | { |
1907 | /* we use a callback system not to make the code needlessly */ |
1911 | case CORO_SLF_SET_STACKLEVEL: |
1908 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1912 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1913 | break; |
|
|
1914 | |
|
|
1915 | case CORO_SLF_TRANSFER: |
|
|
1916 | if (items != 2) |
|
|
1917 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1918 | |
|
|
1919 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1920 | break; |
|
|
1921 | |
|
|
1922 | case CORO_SLF_SCHEDULE: |
|
|
1923 | prepare_schedule (aTHX_ &ta); |
|
|
1924 | break; |
|
|
1925 | |
|
|
1926 | case CORO_SLF_CEDE: |
|
|
1927 | prepare_cede (aTHX_ &ta); |
|
|
1928 | break; |
|
|
1929 | |
|
|
1930 | case CORO_SLF_CEDE_NOTSELF: |
|
|
1931 | prepare_cede_notself (aTHX_ &ta); |
|
|
1932 | break; |
|
|
1933 | |
|
|
1934 | default: |
|
|
1935 | abort (); |
|
|
1936 | } |
|
|
1937 | |
1909 | |
1938 | do |
1910 | do |
|
|
1911 | { |
|
|
1912 | struct coro_transfer_args ta; |
|
|
1913 | |
|
|
1914 | slf_frame.prepare (aTHX_ &ta); |
1939 | TRANSFER (ta, 0); |
1915 | TRANSFER (ta, 0); |
1940 | while (slf && slf->check (aTHX)); |
|
|
1941 | |
1916 | |
1942 | SPAGAIN; |
1917 | checkmark = PL_stack_sp - PL_stack_base; |
|
|
1918 | } |
|
|
1919 | while (slf_frame.check (aTHX_ &slf_frame)); |
1943 | |
1920 | |
|
|
1921 | { |
|
|
1922 | dSP; |
|
|
1923 | SV **bot = PL_stack_base + checkmark; |
|
|
1924 | int gimme = GIMME_V; |
|
|
1925 | |
|
|
1926 | slf_frame.prepare = 0; /* invalidate the frame, so it gets initialised again next time */ |
|
|
1927 | |
|
|
1928 | /* make sure we put something on the stack in scalar context */ |
|
|
1929 | if (gimme == G_SCALAR) |
|
|
1930 | { |
|
|
1931 | if (sp == bot) |
|
|
1932 | XPUSHs (&PL_sv_undef); |
|
|
1933 | |
|
|
1934 | SP = bot + 1; |
|
|
1935 | } |
|
|
1936 | |
1944 | PUTBACK; |
1937 | PUTBACK; |
1945 | SLF_TAIL; |
1938 | } |
1946 | SPAGAIN; |
|
|
1947 | RETURN; |
|
|
1948 | } |
|
|
1949 | |
1939 | |
1950 | static void |
1940 | return NORMAL; |
1951 | coro_slf_patch (pTHX_ CV *cv, int ix, SV **args, int items) |
1941 | } |
1952 | { |
|
|
1953 | assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf)); |
|
|
1954 | |
1942 | |
|
|
1943 | static void |
|
|
1944 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) |
|
|
1945 | { |
1955 | assert (("FATAL: SLF call with illegal CV value", CvGV (cv))); |
1946 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
|
|
1947 | |
|
|
1948 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
|
|
1949 | && PL_op->op_ppaddr != pp_slf) |
|
|
1950 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
|
|
1951 | |
|
|
1952 | if (items > 3) |
|
|
1953 | croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); |
|
|
1954 | |
|
|
1955 | CvFLAGS (cv) |= CVf_SLF; |
|
|
1956 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1956 | slf_cv = cv; |
1957 | slf_cv = cv; |
1957 | |
1958 | |
1958 | /* we patch the op, and then re-run the whole call */ |
1959 | /* we patch the op, and then re-run the whole call */ |
1959 | /* we have to put the same argument on the stack for this to work */ |
1960 | /* we have to put the same argument on the stack for this to work */ |
1960 | /* and this will be done by pp_restore */ |
1961 | /* and this will be done by pp_restore */ |
1961 | slf_restore.op_next = (OP *)&slf_restore; |
1962 | slf_restore.op_next = (OP *)&slf_restore; |
1962 | slf_restore.op_type = OP_NULL; |
1963 | slf_restore.op_type = OP_CUSTOM; |
1963 | slf_restore.op_ppaddr = pp_restore; |
1964 | slf_restore.op_ppaddr = pp_restore; |
1964 | slf_restore.op_first = PL_op; |
1965 | slf_restore.op_first = PL_op; |
1965 | |
1966 | |
1966 | slf_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; |
1967 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1967 | slf_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; |
1968 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
|
|
1969 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
1968 | |
1970 | |
1969 | PL_op->op_ppaddr = pp_slf; |
1971 | PL_op->op_ppaddr = pp_slf; |
1970 | PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SLF | ix; /* we potentially share our private flags with entersub */ |
|
|
1971 | |
1972 | |
1972 | PL_op = (OP *)&slf_restore; |
1973 | PL_op = (OP *)&slf_restore; |
1973 | } |
1974 | } |
1974 | |
1975 | |
|
|
1976 | /*****************************************************************************/ |
|
|
1977 | |
|
|
1978 | static void |
|
|
1979 | coro_semaphore_adjust (AV *av, int adjust) |
|
|
1980 | { |
|
|
1981 | SV *count_sv = AvARRAY (av)[0]; |
|
|
1982 | IV count = SvIVX (count_sv); |
|
|
1983 | |
|
|
1984 | count += adjust; |
|
|
1985 | SvIVX (count_sv) = count; |
|
|
1986 | |
|
|
1987 | /* now wake up as many waiters as possible */ |
|
|
1988 | while (count > 0 && AvFILLp (av) >= count) |
|
|
1989 | { |
|
|
1990 | SV *cb; |
|
|
1991 | |
|
|
1992 | /* swap first two elements so we can shift a waiter */ |
|
|
1993 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
1994 | AvARRAY (av)[1] = count_sv; |
|
|
1995 | cb = av_shift (av); |
|
|
1996 | |
|
|
1997 | if (SvOBJECT (cb)) |
|
|
1998 | api_ready (aTHX_ cb); |
|
|
1999 | else |
|
|
2000 | croak ("callbacks not yet supported"); |
|
|
2001 | |
|
|
2002 | SvREFCNT_dec (cb); |
|
|
2003 | |
|
|
2004 | --count; |
|
|
2005 | } |
|
|
2006 | } |
|
|
2007 | |
|
|
2008 | static void |
|
|
2009 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2010 | { |
|
|
2011 | /* call $sem->adjust (0) to possibly wake up some waiters */ |
|
|
2012 | coro_semaphore_adjust ((AV *)coro->slf_frame.data, 0); |
|
|
2013 | } |
|
|
2014 | |
1975 | static void |
2015 | static int |
1976 | api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items) |
2016 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
1977 | { |
2017 | { |
1978 | CvXSUBANY (cv).any_ptr = (void *)slf; |
2018 | AV *av = (AV *)frame->data; |
1979 | coro_slf_patch (aTHX_ cv, CORO_SLF_CUSTOM, arg, items); |
2019 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2020 | |
|
|
2021 | if (SvIVX (count_sv) > 0) |
|
|
2022 | { |
|
|
2023 | SvSTATE (coro_current)->on_destroy = 0; |
|
|
2024 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2025 | return 0; |
|
|
2026 | } |
|
|
2027 | else |
|
|
2028 | { |
|
|
2029 | int i; |
|
|
2030 | /* if we were woken up but can't down, we look through the whole */ |
|
|
2031 | /* waiters list and only add us if we aren't in there already */ |
|
|
2032 | /* this avoids some degenerate memory usage cases */ |
|
|
2033 | |
|
|
2034 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2035 | if (AvARRAY (av)[i] == SvRV (coro_current)) |
|
|
2036 | return 1; |
|
|
2037 | |
|
|
2038 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2039 | return 1; |
|
|
2040 | } |
1980 | } |
2041 | } |
|
|
2042 | |
|
|
2043 | static void |
|
|
2044 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2045 | { |
|
|
2046 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2047 | |
|
|
2048 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2049 | { |
|
|
2050 | frame->data = (void *)av; |
|
|
2051 | frame->prepare = prepare_nop; |
|
|
2052 | } |
|
|
2053 | else |
|
|
2054 | { |
|
|
2055 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2056 | |
|
|
2057 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
|
|
2058 | frame->prepare = prepare_schedule; |
|
|
2059 | |
|
|
2060 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2061 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2062 | SvSTATE (coro_current)->on_destroy = coro_semaphore_on_destroy; |
|
|
2063 | } |
|
|
2064 | |
|
|
2065 | frame->check = slf_check_semaphore_down; |
|
|
2066 | |
|
|
2067 | } |
|
|
2068 | |
|
|
2069 | /*****************************************************************************/ |
|
|
2070 | |
|
|
2071 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2072 | |
|
|
2073 | /* create a closure from XS, returns a code reference */ |
|
|
2074 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2075 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2076 | static SV * |
|
|
2077 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2078 | { |
|
|
2079 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2080 | |
|
|
2081 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2082 | |
|
|
2083 | CvANON_on (cv); |
|
|
2084 | CvISXSUB_on (cv); |
|
|
2085 | CvXSUB (cv) = xsub; |
|
|
2086 | GENSUB_ARG = arg; |
|
|
2087 | |
|
|
2088 | return newRV_noinc ((SV *)cv); |
|
|
2089 | } |
|
|
2090 | |
|
|
2091 | /*****************************************************************************/ |
1981 | |
2092 | |
1982 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2093 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1983 | |
2094 | |
1984 | PROTOTYPES: DISABLE |
2095 | PROTOTYPES: DISABLE |
1985 | |
2096 | |
… | |
… | |
2015 | main_top_env = PL_top_env; |
2126 | main_top_env = PL_top_env; |
2016 | |
2127 | |
2017 | while (main_top_env->je_prev) |
2128 | while (main_top_env->je_prev) |
2018 | main_top_env = main_top_env->je_prev; |
2129 | main_top_env = main_top_env->je_prev; |
2019 | |
2130 | |
|
|
2131 | { |
|
|
2132 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2133 | |
|
|
2134 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2135 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2136 | newSVpv ("coro_slf", 0), 0); |
|
|
2137 | |
|
|
2138 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2139 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2140 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2141 | } |
|
|
2142 | |
2020 | coroapi.ver = CORO_API_VERSION; |
2143 | coroapi.ver = CORO_API_VERSION; |
2021 | coroapi.rev = CORO_API_REVISION; |
2144 | coroapi.rev = CORO_API_REVISION; |
|
|
2145 | |
2022 | coroapi.transfer = api_transfer; |
2146 | coroapi.transfer = api_transfer; |
|
|
2147 | |
|
|
2148 | coroapi.sv_state = SvSTATE_; |
2023 | coroapi.execute_slf = api_execute_slf; |
2149 | coroapi.execute_slf = api_execute_slf; |
|
|
2150 | coroapi.prepare_nop = prepare_nop; |
|
|
2151 | coroapi.prepare_schedule = prepare_schedule; |
|
|
2152 | coroapi.prepare_cede = prepare_cede; |
|
|
2153 | coroapi.prepare_cede_notself = prepare_cede_notself; |
2024 | |
2154 | |
2025 | { |
2155 | { |
2026 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2156 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2027 | |
2157 | |
2028 | if (!svp) croak ("Time::HiRes is required"); |
2158 | if (!svp) croak ("Time::HiRes is required"); |
… | |
… | |
2063 | OUTPUT: |
2193 | OUTPUT: |
2064 | RETVAL |
2194 | RETVAL |
2065 | |
2195 | |
2066 | void |
2196 | void |
2067 | _set_stacklevel (...) |
2197 | _set_stacklevel (...) |
2068 | ALIAS: |
2198 | CODE: |
2069 | _set_stacklevel = CORO_SLF_SET_STACKLEVEL |
2199 | api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); |
2070 | Coro::State::transfer = CORO_SLF_TRANSFER |
2200 | |
2071 | Coro::schedule = CORO_SLF_SCHEDULE |
2201 | void |
2072 | Coro::cede = CORO_SLF_CEDE |
2202 | transfer (...) |
2073 | Coro::cede_notself = CORO_SLF_CEDE_NOTSELF |
2203 | PROTOTYPE: $$ |
2074 | CODE: |
2204 | CODE: |
2075 | coro_slf_patch (aTHX_ cv, ix, &ST (0), items); |
2205 | api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); |
2076 | |
2206 | |
2077 | bool |
2207 | bool |
2078 | _destroy (SV *coro_sv) |
2208 | _destroy (SV *coro_sv) |
2079 | CODE: |
2209 | CODE: |
2080 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2210 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2087 | CODE: |
2217 | CODE: |
2088 | _exit (code); |
2218 | _exit (code); |
2089 | |
2219 | |
2090 | int |
2220 | int |
2091 | cctx_stacksize (int new_stacksize = 0) |
2221 | cctx_stacksize (int new_stacksize = 0) |
|
|
2222 | PROTOTYPE: ;$ |
2092 | CODE: |
2223 | CODE: |
2093 | RETVAL = cctx_stacksize; |
2224 | RETVAL = cctx_stacksize; |
2094 | if (new_stacksize) |
2225 | if (new_stacksize) |
2095 | { |
2226 | { |
2096 | cctx_stacksize = new_stacksize; |
2227 | cctx_stacksize = new_stacksize; |
… | |
… | |
2099 | OUTPUT: |
2230 | OUTPUT: |
2100 | RETVAL |
2231 | RETVAL |
2101 | |
2232 | |
2102 | int |
2233 | int |
2103 | cctx_max_idle (int max_idle = 0) |
2234 | cctx_max_idle (int max_idle = 0) |
|
|
2235 | PROTOTYPE: ;$ |
2104 | CODE: |
2236 | CODE: |
2105 | RETVAL = cctx_max_idle; |
2237 | RETVAL = cctx_max_idle; |
2106 | if (max_idle > 1) |
2238 | if (max_idle > 1) |
2107 | cctx_max_idle = max_idle; |
2239 | cctx_max_idle = max_idle; |
2108 | OUTPUT: |
2240 | OUTPUT: |
2109 | RETVAL |
2241 | RETVAL |
2110 | |
2242 | |
2111 | int |
2243 | int |
2112 | cctx_count () |
2244 | cctx_count () |
|
|
2245 | PROTOTYPE: |
2113 | CODE: |
2246 | CODE: |
2114 | RETVAL = cctx_count; |
2247 | RETVAL = cctx_count; |
2115 | OUTPUT: |
2248 | OUTPUT: |
2116 | RETVAL |
2249 | RETVAL |
2117 | |
2250 | |
2118 | int |
2251 | int |
2119 | cctx_idle () |
2252 | cctx_idle () |
|
|
2253 | PROTOTYPE: |
2120 | CODE: |
2254 | CODE: |
2121 | RETVAL = cctx_idle; |
2255 | RETVAL = cctx_idle; |
2122 | OUTPUT: |
2256 | OUTPUT: |
2123 | RETVAL |
2257 | RETVAL |
2124 | |
2258 | |
2125 | void |
2259 | void |
2126 | list () |
2260 | list () |
|
|
2261 | PROTOTYPE: |
2127 | PPCODE: |
2262 | PPCODE: |
2128 | { |
2263 | { |
2129 | struct coro *coro; |
2264 | struct coro *coro; |
2130 | for (coro = coro_first; coro; coro = coro->next) |
2265 | for (coro = coro_first; coro; coro = coro->next) |
2131 | if (coro->hv) |
2266 | if (coro->hv) |
… | |
… | |
2198 | SvREFCNT_dec (self->throw); |
2333 | SvREFCNT_dec (self->throw); |
2199 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2334 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2200 | |
2335 | |
2201 | void |
2336 | void |
2202 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2337 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
2338 | PROTOTYPE: $;$ |
2203 | C_ARGS: aTHX_ coro, flags |
2339 | C_ARGS: aTHX_ coro, flags |
2204 | |
2340 | |
2205 | SV * |
2341 | SV * |
2206 | has_cctx (Coro::State coro) |
2342 | has_cctx (Coro::State coro) |
2207 | PROTOTYPE: $ |
2343 | PROTOTYPE: $ |
… | |
… | |
2232 | OUTPUT: |
2368 | OUTPUT: |
2233 | RETVAL |
2369 | RETVAL |
2234 | |
2370 | |
2235 | void |
2371 | void |
2236 | force_cctx () |
2372 | force_cctx () |
|
|
2373 | PROTOTYPE: |
2237 | CODE: |
2374 | CODE: |
2238 | struct coro *coro = SvSTATE (coro_current); |
2375 | struct coro *coro = SvSTATE (coro_current); |
2239 | coro->cctx->idle_sp = 0; |
2376 | coro->cctx->idle_sp = 0; |
2240 | |
2377 | |
2241 | void |
2378 | void |
… | |
… | |
2243 | PROTOTYPE: $ |
2380 | PROTOTYPE: $ |
2244 | ALIAS: |
2381 | ALIAS: |
2245 | swap_defav = 1 |
2382 | swap_defav = 1 |
2246 | CODE: |
2383 | CODE: |
2247 | if (!self->slot) |
2384 | if (!self->slot) |
2248 | croak ("cannot swap state with coroutine that has no saved state"); |
2385 | croak ("cannot swap state with coroutine that has no saved state,"); |
2249 | else |
2386 | else |
2250 | { |
2387 | { |
2251 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2388 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2252 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2389 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2253 | |
2390 | |
… | |
… | |
2295 | SvREADONLY_on (sv); |
2432 | SvREADONLY_on (sv); |
2296 | } |
2433 | } |
2297 | } |
2434 | } |
2298 | |
2435 | |
2299 | void |
2436 | void |
|
|
2437 | schedule (...) |
|
|
2438 | CODE: |
|
|
2439 | api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); |
|
|
2440 | |
|
|
2441 | void |
|
|
2442 | cede (...) |
|
|
2443 | CODE: |
|
|
2444 | api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); |
|
|
2445 | |
|
|
2446 | void |
|
|
2447 | cede_notself (...) |
|
|
2448 | CODE: |
|
|
2449 | api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); |
|
|
2450 | |
|
|
2451 | void |
2300 | _set_current (SV *current) |
2452 | _set_current (SV *current) |
2301 | PROTOTYPE: $ |
2453 | PROTOTYPE: $ |
2302 | CODE: |
2454 | CODE: |
2303 | SvREFCNT_dec (SvRV (coro_current)); |
2455 | SvREFCNT_dec (SvRV (coro_current)); |
2304 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
2456 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
2305 | |
2457 | |
2306 | void |
2458 | void |
2307 | _set_readyhook (SV *hook) |
2459 | _set_readyhook (SV *hook) |
2308 | PROTOTYPE: $ |
2460 | PROTOTYPE: $ |
2309 | CODE: |
2461 | CODE: |
2310 | LOCK; |
|
|
2311 | SvREFCNT_dec (coro_readyhook); |
2462 | SvREFCNT_dec (coro_readyhook); |
2312 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2463 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2313 | UNLOCK; |
|
|
2314 | |
2464 | |
2315 | int |
2465 | int |
2316 | prio (Coro::State coro, int newprio = 0) |
2466 | prio (Coro::State coro, int newprio = 0) |
|
|
2467 | PROTOTYPE: $;$ |
2317 | ALIAS: |
2468 | ALIAS: |
2318 | nice = 1 |
2469 | nice = 1 |
2319 | CODE: |
2470 | CODE: |
2320 | { |
2471 | { |
2321 | RETVAL = coro->prio; |
2472 | RETVAL = coro->prio; |
… | |
… | |
2385 | { |
2536 | { |
2386 | av_fill (defav, len - 1); |
2537 | av_fill (defav, len - 1); |
2387 | for (i = 0; i < len; ++i) |
2538 | for (i = 0; i < len; ++i) |
2388 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2539 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2389 | } |
2540 | } |
2390 | |
|
|
2391 | SvREFCNT_dec (invoke); |
|
|
2392 | } |
2541 | } |
2393 | |
2542 | |
2394 | void |
2543 | void |
2395 | _pool_2 (SV *cb) |
2544 | _pool_2 (SV *cb) |
2396 | CODE: |
2545 | CODE: |
… | |
… | |
2421 | api_trace (aTHX_ coro_current, 0); |
2570 | api_trace (aTHX_ coro_current, 0); |
2422 | |
2571 | |
2423 | av_push (av_async_pool, newSVsv (coro_current)); |
2572 | av_push (av_async_pool, newSVsv (coro_current)); |
2424 | } |
2573 | } |
2425 | |
2574 | |
2426 | #if 0 |
|
|
2427 | |
|
|
2428 | void |
|
|
2429 | _generator_call (...) |
|
|
2430 | PROTOTYPE: @ |
|
|
2431 | PPCODE: |
|
|
2432 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2433 | xxxx |
|
|
2434 | abort (); |
|
|
2435 | |
|
|
2436 | SV * |
|
|
2437 | gensub (SV *sub, ...) |
|
|
2438 | PROTOTYPE: &;@ |
|
|
2439 | CODE: |
|
|
2440 | { |
|
|
2441 | struct coro *coro; |
|
|
2442 | MAGIC *mg; |
|
|
2443 | CV *xcv; |
|
|
2444 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2445 | int i; |
|
|
2446 | |
|
|
2447 | CvGV (ncv) = CvGV (cv); |
|
|
2448 | CvFILE (ncv) = CvFILE (cv); |
|
|
2449 | |
|
|
2450 | Newz (0, coro, 1, struct coro); |
|
|
2451 | coro->args = newAV (); |
|
|
2452 | coro->flags = CF_NEW; |
|
|
2453 | |
|
|
2454 | av_extend (coro->args, items - 1); |
|
|
2455 | for (i = 1; i < items; i++) |
|
|
2456 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2457 | |
|
|
2458 | CvISXSUB_on (ncv); |
|
|
2459 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2460 | |
|
|
2461 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2462 | |
|
|
2463 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2464 | CvANON_on (ncv); |
|
|
2465 | |
|
|
2466 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2467 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2468 | } |
|
|
2469 | OUTPUT: |
|
|
2470 | RETVAL |
|
|
2471 | |
|
|
2472 | #endif |
|
|
2473 | |
|
|
2474 | |
2575 | |
2475 | MODULE = Coro::State PACKAGE = Coro::AIO |
2576 | MODULE = Coro::State PACKAGE = Coro::AIO |
2476 | |
2577 | |
2477 | void |
2578 | void |
2478 | _get_state (SV *self) |
2579 | _get_state (SV *self) |
|
|
2580 | PROTOTYPE: $ |
2479 | PPCODE: |
2581 | PPCODE: |
2480 | { |
2582 | { |
2481 | AV *defav = GvAV (PL_defgv); |
2583 | AV *defav = GvAV (PL_defgv); |
2482 | AV *av = newAV (); |
2584 | AV *av = newAV (); |
2483 | int i; |
2585 | int i; |
… | |
… | |
2526 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2628 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2527 | |
2629 | |
2528 | BOOT: |
2630 | BOOT: |
2529 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2631 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2530 | |
2632 | |
2531 | SV * |
2633 | void |
2532 | _schedule (...) |
2634 | _schedule (...) |
2533 | PROTOTYPE: @ |
|
|
2534 | CODE: |
2635 | CODE: |
2535 | { |
2636 | { |
2536 | static int incede; |
2637 | static int incede; |
2537 | |
2638 | |
2538 | api_cede_notself (aTHX); |
2639 | api_cede_notself (aTHX); |
… | |
… | |
2557 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2658 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2558 | |
2659 | |
2559 | BOOT: |
2660 | BOOT: |
2560 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2661 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2561 | |
2662 | |
|
|
2663 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
|
|
2664 | |
|
|
2665 | SV * |
|
|
2666 | new (SV *klass, SV *count_ = 0) |
|
|
2667 | CODE: |
|
|
2668 | { |
|
|
2669 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2670 | AV *av = newAV (); |
|
|
2671 | av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); |
|
|
2672 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
|
|
2673 | } |
|
|
2674 | OUTPUT: |
|
|
2675 | RETVAL |
|
|
2676 | |
|
|
2677 | SV * |
|
|
2678 | count (SV *self) |
|
|
2679 | CODE: |
|
|
2680 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
|
|
2681 | OUTPUT: |
|
|
2682 | RETVAL |
|
|
2683 | |
|
|
2684 | void |
|
|
2685 | up (SV *self, int adjust = 1) |
|
|
2686 | ALIAS: |
|
|
2687 | adjust = 1 |
|
|
2688 | CODE: |
|
|
2689 | coro_semaphore_adjust ((AV *)SvRV (self), ix ? adjust : 1); |
|
|
2690 | |
|
|
2691 | void |
|
|
2692 | down (SV *self) |
|
|
2693 | CODE: |
|
|
2694 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
|
|
2695 | |
|
|
2696 | void |
|
|
2697 | try (SV *self) |
|
|
2698 | PPCODE: |
|
|
2699 | { |
|
|
2700 | AV *av = (AV *)SvRV (self); |
|
|
2701 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2702 | IV count = SvIVX (count_sv); |
|
|
2703 | |
|
|
2704 | if (count > 0) |
|
|
2705 | { |
|
|
2706 | --count; |
|
|
2707 | SvIVX (count_sv) = count; |
|
|
2708 | XSRETURN_YES; |
|
|
2709 | } |
|
|
2710 | else |
|
|
2711 | XSRETURN_NO; |
|
|
2712 | } |
|
|
2713 | |
|
|
2714 | void |
|
|
2715 | waiters (SV *self) |
|
|
2716 | CODE: |
|
|
2717 | { |
|
|
2718 | AV *av = (AV *)SvRV (self); |
|
|
2719 | |
|
|
2720 | if (GIMME_V == G_SCALAR) |
|
|
2721 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
|
|
2722 | else |
|
|
2723 | { |
|
|
2724 | int i; |
|
|
2725 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
|
|
2726 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2727 | PUSHs (newSVsv (AvARRAY (av)[i])); |
|
|
2728 | } |
|
|
2729 | } |
|
|
2730 | |