… | |
… | |
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; |
… | |
… | |
181 | static struct CoroAPI coroapi; |
166 | static struct CoroAPI coroapi; |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
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; |
|
|
187 | |
171 | |
188 | static GV *irsgv; /* $/ */ |
172 | static GV *irsgv; /* $/ */ |
189 | static GV *stdoutgv; /* *STDOUT */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
190 | static SV *rv_diehook; |
174 | static SV *rv_diehook; |
191 | static SV *rv_warnhook; |
175 | static SV *rv_warnhook; |
… | |
… | |
257 | /* this is a structure representing a perl-level coroutine */ |
241 | /* this is a structure representing a perl-level coroutine */ |
258 | struct coro { |
242 | struct coro { |
259 | /* the C coroutine allocated to this perl coroutine, if any */ |
243 | /* the C coroutine allocated to this perl coroutine, if any */ |
260 | coro_cctx *cctx; |
244 | coro_cctx *cctx; |
261 | |
245 | |
262 | /* process data */ |
246 | /* state data */ |
263 | struct CoroSLF slf_frame; /* saved slf frame */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
264 | AV *mainstack; |
248 | AV *mainstack; |
265 | perl_slots *slot; /* basically the saved sp */ |
249 | perl_slots *slot; /* basically the saved sp */ |
266 | |
250 | |
267 | AV *args; /* data associated with this coroutine (initial args) */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
268 | int refcnt; /* coroutines are refcounted, yes */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
269 | int flags; /* CF_ flags */ |
253 | int flags; /* CF_ flags */ |
270 | HV *hv; /* the perl hash associated with this coro, if any */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
271 | |
256 | |
272 | /* statistics */ |
257 | /* statistics */ |
273 | int usecount; /* number of transfers to this coro */ |
258 | int usecount; /* number of transfers to this coro */ |
274 | |
259 | |
275 | /* coro process data */ |
260 | /* coro process data */ |
… | |
… | |
284 | }; |
269 | }; |
285 | |
270 | |
286 | typedef struct coro *Coro__State; |
271 | typedef struct coro *Coro__State; |
287 | typedef struct coro *Coro__State_or_hashref; |
272 | typedef struct coro *Coro__State_or_hashref; |
288 | |
273 | |
|
|
274 | /* the following variables are effectively part of the perl context */ |
|
|
275 | /* and get copied between struct coro and these variables */ |
|
|
276 | /* the mainr easonw e don't support windows process emulation */ |
289 | static struct CoroSLF slf_frame; /* the current slf frame */ |
277 | static struct CoroSLF slf_frame; /* the current slf frame */ |
|
|
278 | static SV *coro_throw; |
290 | |
279 | |
291 | /** Coro ********************************************************************/ |
280 | /** Coro ********************************************************************/ |
292 | |
281 | |
293 | #define PRIO_MAX 3 |
282 | #define PRIO_MAX 3 |
294 | #define PRIO_HIGH 1 |
283 | #define PRIO_HIGH 1 |
… | |
… | |
403 | static MGVTBL coro_cv_vtbl = { |
392 | static MGVTBL coro_cv_vtbl = { |
404 | 0, 0, 0, 0, |
393 | 0, 0, 0, 0, |
405 | coro_cv_free |
394 | coro_cv_free |
406 | }; |
395 | }; |
407 | |
396 | |
408 | #define CORO_MAGIC(sv, type) \ |
397 | #define CORO_MAGIC(sv, type) \ |
409 | SvMAGIC (sv) \ |
398 | expect_true (SvMAGIC (sv)) \ |
410 | ? SvMAGIC (sv)->mg_type == type \ |
399 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
411 | ? SvMAGIC (sv) \ |
400 | ? SvMAGIC (sv) \ |
412 | : mg_find (sv, type) \ |
401 | : mg_find (sv, type) \ |
413 | : 0 |
402 | : 0 |
414 | |
403 | |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
404 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
416 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
405 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
417 | |
406 | |
… | |
… | |
438 | mg = CORO_MAGIC_state (coro); |
427 | mg = CORO_MAGIC_state (coro); |
439 | return (struct coro *)mg->mg_ptr; |
428 | return (struct coro *)mg->mg_ptr; |
440 | } |
429 | } |
441 | |
430 | |
442 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
431 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
432 | |
|
|
433 | /* fastert than SvSTATE, but expects a coroutine hv */ |
|
|
434 | INLINE struct coro * |
|
|
435 | SvSTATE_hv (SV *sv) |
|
|
436 | { |
|
|
437 | MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state) |
|
|
438 | ? SvMAGIC (sv) |
|
|
439 | : mg_find (sv, CORO_MAGIC_type_state); |
|
|
440 | |
|
|
441 | return (struct coro *)mg->mg_ptr; |
|
|
442 | } |
|
|
443 | |
|
|
444 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
443 | |
445 | |
444 | /* the next two functions merely cache the padlists */ |
446 | /* the next two functions merely cache the padlists */ |
445 | static void |
447 | static void |
446 | get_padlist (pTHX_ CV *cv) |
448 | get_padlist (pTHX_ CV *cv) |
447 | { |
449 | { |
… | |
… | |
515 | } |
517 | } |
516 | |
518 | |
517 | PUTBACK; |
519 | PUTBACK; |
518 | } |
520 | } |
519 | |
521 | |
520 | slf_frame = c->slf_frame; |
522 | slf_frame = c->slf_frame; |
|
|
523 | coro_throw = c->throw; |
521 | } |
524 | } |
522 | |
525 | |
523 | static void |
526 | static void |
524 | save_perl (pTHX_ Coro__State c) |
527 | save_perl (pTHX_ Coro__State c) |
525 | { |
528 | { |
|
|
529 | c->throw = coro_throw; |
526 | c->slf_frame = slf_frame; |
530 | c->slf_frame = slf_frame; |
527 | |
531 | |
528 | { |
532 | { |
529 | dSP; |
533 | dSP; |
530 | I32 cxix = cxstack_ix; |
534 | I32 cxix = cxstack_ix; |
… | |
… | |
811 | |
815 | |
812 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
816 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
813 | } |
817 | } |
814 | |
818 | |
815 | static void |
819 | static void |
816 | prepare_nop (aTHX_ struct coro_transfer_args *ta) |
820 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
817 | { |
821 | { |
818 | /* kind of mega-hacky, but works */ |
822 | /* kind of mega-hacky, but works */ |
819 | ta->next = ta->prev = (struct coro *)ta; |
823 | ta->next = ta->prev = (struct coro *)ta; |
820 | } |
824 | } |
821 | |
825 | |
822 | static int |
826 | static int |
823 | slf_check_nop (aTHX) |
827 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
824 | { |
828 | { |
825 | return 0; |
829 | return 0; |
826 | } |
830 | } |
827 | |
831 | |
828 | static void |
832 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
829 | coro_setup (pTHX_ struct coro *coro) |
833 | coro_setup (pTHX_ struct coro *coro) |
830 | { |
834 | { |
831 | /* |
835 | /* |
832 | * emulate part of the perl startup here. |
836 | * emulate part of the perl startup here. |
833 | */ |
837 | */ |
… | |
… | |
876 | /* this newly created coroutine might be run on an existing cctx which most |
880 | /* this newly created coroutine might be run on an existing cctx which most |
877 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
881 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
878 | */ |
882 | */ |
879 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
883 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
880 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
884 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
|
|
885 | |
|
|
886 | coro_throw = coro->throw; |
881 | } |
887 | } |
882 | |
888 | |
883 | static void |
889 | static void |
884 | coro_destruct (pTHX_ struct coro *coro) |
890 | coro_destruct (pTHX_ struct coro *coro) |
885 | { |
891 | { |
… | |
… | |
909 | |
915 | |
910 | SvREFCNT_dec (PL_diehook); |
916 | SvREFCNT_dec (PL_diehook); |
911 | SvREFCNT_dec (PL_warnhook); |
917 | SvREFCNT_dec (PL_warnhook); |
912 | |
918 | |
913 | SvREFCNT_dec (coro->saved_deffh); |
919 | SvREFCNT_dec (coro->saved_deffh); |
914 | SvREFCNT_dec (coro->throw); |
920 | SvREFCNT_dec (coro_throw); |
915 | |
921 | |
916 | coro_destruct_stacks (aTHX); |
922 | coro_destruct_stacks (aTHX); |
917 | } |
923 | } |
918 | |
924 | |
919 | INLINE void |
925 | INLINE void |
… | |
… | |
929 | static int |
935 | static int |
930 | runops_trace (pTHX) |
936 | runops_trace (pTHX) |
931 | { |
937 | { |
932 | COP *oldcop = 0; |
938 | COP *oldcop = 0; |
933 | int oldcxix = -2; |
939 | int oldcxix = -2; |
934 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
940 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
935 | coro_cctx *cctx = coro->cctx; |
941 | coro_cctx *cctx = coro->cctx; |
936 | |
942 | |
937 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
943 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
938 | { |
944 | { |
939 | PERL_ASYNC_CHECK (); |
945 | PERL_ASYNC_CHECK (); |
… | |
… | |
1088 | |
1094 | |
1089 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1095 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1090 | INLINE void |
1096 | INLINE void |
1091 | transfer_tail (pTHX) |
1097 | transfer_tail (pTHX) |
1092 | { |
1098 | { |
1093 | struct coro *next = (struct coro *)transfer_next; |
|
|
1094 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
|
|
1095 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
|
|
1096 | |
|
|
1097 | free_coro_mortal (aTHX); |
1099 | free_coro_mortal (aTHX); |
1098 | UNLOCK; |
|
|
1099 | |
|
|
1100 | if (expect_false (next->throw)) |
|
|
1101 | { |
|
|
1102 | SV *exception = sv_2mortal (next->throw); |
|
|
1103 | |
|
|
1104 | next->throw = 0; |
|
|
1105 | sv_setsv (ERRSV, exception); |
|
|
1106 | croak (0); |
|
|
1107 | } |
|
|
1108 | } |
1100 | } |
1109 | |
1101 | |
1110 | /* |
1102 | /* |
1111 | * this is a _very_ stripped down perl interpreter ;) |
1103 | * this is a _very_ stripped down perl interpreter ;) |
1112 | */ |
1104 | */ |
… | |
… | |
1127 | |
1119 | |
1128 | /* inject a fake subroutine call to cctx_init */ |
1120 | /* inject a fake subroutine call to cctx_init */ |
1129 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1121 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1130 | |
1122 | |
1131 | /* cctx_run is the alternative tail of transfer() */ |
1123 | /* cctx_run is the alternative tail of transfer() */ |
1132 | /* TODO: throwing an exception here might be deadly, VERIFY */ |
|
|
1133 | transfer_tail (aTHX); |
1124 | transfer_tail (aTHX); |
1134 | |
1125 | |
1135 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1126 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1136 | PL_restartop = PL_op; |
1127 | PL_restartop = PL_op; |
1137 | perl_run (PL_curinterp); |
1128 | perl_run (PL_curinterp); |
… | |
… | |
1293 | /** coroutine switching *****************************************************/ |
1284 | /** coroutine switching *****************************************************/ |
1294 | |
1285 | |
1295 | static void |
1286 | static void |
1296 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1287 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1297 | { |
1288 | { |
|
|
1289 | /* TODO: throwing up here is considered harmful */ |
|
|
1290 | |
1298 | if (expect_true (prev != next)) |
1291 | if (expect_true (prev != next)) |
1299 | { |
1292 | { |
1300 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1293 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1301 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1294 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1302 | |
1295 | |
… | |
… | |
1312 | #endif |
1305 | #endif |
1313 | } |
1306 | } |
1314 | } |
1307 | } |
1315 | |
1308 | |
1316 | /* always use the TRANSFER macro */ |
1309 | /* always use the TRANSFER macro */ |
1317 | static void NOINLINE |
1310 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1318 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1311 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1319 | { |
1312 | { |
1320 | dSTACKLEVEL; |
1313 | dSTACKLEVEL; |
1321 | |
1314 | |
1322 | /* sometimes transfer is only called to set idle_sp */ |
1315 | /* sometimes transfer is only called to set idle_sp */ |
1323 | if (expect_false (!next)) |
1316 | if (expect_false (!next)) |
1324 | { |
1317 | { |
1325 | ((coro_cctx *)prev)->idle_sp = stacklevel; |
1318 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1326 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1319 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1327 | } |
1320 | } |
1328 | else if (expect_true (prev != next)) |
1321 | else if (expect_true (prev != next)) |
1329 | { |
1322 | { |
1330 | coro_cctx *prev__cctx; |
1323 | coro_cctx *prev__cctx; |
… | |
… | |
1337 | prev->flags |= CF_RUNNING; |
1330 | prev->flags |= CF_RUNNING; |
1338 | } |
1331 | } |
1339 | |
1332 | |
1340 | prev->flags &= ~CF_RUNNING; |
1333 | prev->flags &= ~CF_RUNNING; |
1341 | next->flags |= CF_RUNNING; |
1334 | next->flags |= CF_RUNNING; |
1342 | |
|
|
1343 | LOCK; |
|
|
1344 | |
1335 | |
1345 | /* first get rid of the old state */ |
1336 | /* first get rid of the old state */ |
1346 | save_perl (aTHX_ prev); |
1337 | save_perl (aTHX_ prev); |
1347 | |
1338 | |
1348 | if (expect_false (next->flags & CF_NEW)) |
1339 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1357 | |
1348 | |
1358 | prev__cctx = prev->cctx; |
1349 | prev__cctx = prev->cctx; |
1359 | |
1350 | |
1360 | /* possibly untie and reuse the cctx */ |
1351 | /* possibly untie and reuse the cctx */ |
1361 | if (expect_true ( |
1352 | if (expect_true ( |
1362 | prev__cctx->idle_sp == stacklevel |
1353 | prev__cctx->idle_sp == (void *)stacklevel |
1363 | && !(prev__cctx->flags & CC_TRACE) |
1354 | && !(prev__cctx->flags & CC_TRACE) |
1364 | && !force_cctx |
1355 | && !force_cctx |
1365 | )) |
1356 | )) |
1366 | { |
1357 | { |
1367 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1358 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1381 | ++next->usecount; |
1372 | ++next->usecount; |
1382 | |
1373 | |
1383 | if (expect_true (!next->cctx)) |
1374 | if (expect_true (!next->cctx)) |
1384 | next->cctx = cctx_get (aTHX); |
1375 | next->cctx = cctx_get (aTHX); |
1385 | |
1376 | |
1386 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1387 | transfer_next = next; |
|
|
1388 | |
|
|
1389 | if (expect_false (prev__cctx != next->cctx)) |
1377 | if (expect_false (prev__cctx != next->cctx)) |
1390 | { |
1378 | { |
1391 | prev__cctx->top_env = PL_top_env; |
1379 | prev__cctx->top_env = PL_top_env; |
1392 | PL_top_env = next->cctx->top_env; |
1380 | PL_top_env = next->cctx->top_env; |
1393 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1381 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
… | |
… | |
1406 | coro_state_destroy (pTHX_ struct coro *coro) |
1394 | coro_state_destroy (pTHX_ struct coro *coro) |
1407 | { |
1395 | { |
1408 | if (coro->flags & CF_DESTROYED) |
1396 | if (coro->flags & CF_DESTROYED) |
1409 | return 0; |
1397 | return 0; |
1410 | |
1398 | |
|
|
1399 | if (coro->on_destroy) |
|
|
1400 | coro->on_destroy (aTHX_ coro); |
|
|
1401 | |
1411 | coro->flags |= CF_DESTROYED; |
1402 | coro->flags |= CF_DESTROYED; |
1412 | |
1403 | |
1413 | if (coro->flags & CF_READY) |
1404 | if (coro->flags & CF_READY) |
1414 | { |
1405 | { |
1415 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1406 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1416 | /* alternative: look through all ready queues and remove the coro */ |
1407 | /* alternative: look through all ready queues and remove the coro */ |
1417 | LOCK; |
|
|
1418 | --coro_nready; |
1408 | --coro_nready; |
1419 | UNLOCK; |
|
|
1420 | } |
1409 | } |
1421 | else |
1410 | else |
1422 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1411 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1423 | |
1412 | |
1424 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1413 | if (coro->mainstack && coro->mainstack != main_mainstack) |
… | |
… | |
1502 | TRANSFER (ta, 1); |
1491 | TRANSFER (ta, 1); |
1503 | } |
1492 | } |
1504 | |
1493 | |
1505 | /** Coro ********************************************************************/ |
1494 | /** Coro ********************************************************************/ |
1506 | |
1495 | |
1507 | static void |
1496 | INLINE void |
1508 | coro_enq (pTHX_ SV *coro_sv) |
1497 | coro_enq (pTHX_ struct coro *coro) |
1509 | { |
1498 | { |
1510 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1499 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1511 | } |
1500 | } |
1512 | |
1501 | |
1513 | static SV * |
1502 | INLINE SV * |
1514 | coro_deq (pTHX) |
1503 | coro_deq (pTHX) |
1515 | { |
1504 | { |
1516 | int prio; |
1505 | int prio; |
1517 | |
1506 | |
1518 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1507 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1537 | if (coro->flags & CF_READY) |
1526 | if (coro->flags & CF_READY) |
1538 | return 0; |
1527 | return 0; |
1539 | |
1528 | |
1540 | coro->flags |= CF_READY; |
1529 | coro->flags |= CF_READY; |
1541 | |
1530 | |
1542 | LOCK; |
|
|
1543 | |
|
|
1544 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1531 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1545 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1532 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1546 | |
1533 | |
1547 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1534 | coro_enq (aTHX_ coro); |
1548 | ++coro_nready; |
1535 | ++coro_nready; |
1549 | |
1536 | |
1550 | UNLOCK; |
|
|
1551 | |
|
|
1552 | if (sv_hook) |
1537 | if (sv_hook) |
1553 | { |
1538 | { |
1554 | dSP; |
1539 | dSP; |
1555 | |
1540 | |
1556 | ENTER; |
1541 | ENTER; |
… | |
… | |
1582 | { |
1567 | { |
1583 | SV *prev_sv, *next_sv; |
1568 | SV *prev_sv, *next_sv; |
1584 | |
1569 | |
1585 | for (;;) |
1570 | for (;;) |
1586 | { |
1571 | { |
1587 | LOCK; |
|
|
1588 | next_sv = coro_deq (aTHX); |
1572 | next_sv = coro_deq (aTHX); |
1589 | |
1573 | |
1590 | /* nothing to schedule: call the idle handler */ |
1574 | /* nothing to schedule: call the idle handler */ |
1591 | if (expect_false (!next_sv)) |
1575 | if (expect_false (!next_sv)) |
1592 | { |
1576 | { |
1593 | dSP; |
1577 | dSP; |
1594 | UNLOCK; |
|
|
1595 | |
1578 | |
1596 | ENTER; |
1579 | ENTER; |
1597 | SAVETMPS; |
1580 | SAVETMPS; |
1598 | |
1581 | |
1599 | PUSHMARK (SP); |
1582 | PUSHMARK (SP); |
… | |
… | |
1604 | FREETMPS; |
1587 | FREETMPS; |
1605 | LEAVE; |
1588 | LEAVE; |
1606 | continue; |
1589 | continue; |
1607 | } |
1590 | } |
1608 | |
1591 | |
1609 | ta->next = SvSTATE (next_sv); |
1592 | ta->next = SvSTATE_hv (next_sv); |
1610 | |
1593 | |
1611 | /* cannot transfer to destroyed coros, skip and look for next */ |
1594 | /* cannot transfer to destroyed coros, skip and look for next */ |
1612 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1595 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1613 | { |
1596 | { |
1614 | UNLOCK; |
|
|
1615 | SvREFCNT_dec (next_sv); |
1597 | SvREFCNT_dec (next_sv); |
1616 | /* coro_nready has already been taken care of by destroy */ |
1598 | /* coro_nready has already been taken care of by destroy */ |
1617 | continue; |
1599 | continue; |
1618 | } |
1600 | } |
1619 | |
1601 | |
1620 | --coro_nready; |
1602 | --coro_nready; |
1621 | UNLOCK; |
|
|
1622 | break; |
1603 | break; |
1623 | } |
1604 | } |
1624 | |
1605 | |
1625 | /* free this only after the transfer */ |
1606 | /* free this only after the transfer */ |
1626 | prev_sv = SvRV (coro_current); |
1607 | prev_sv = SvRV (coro_current); |
1627 | ta->prev = SvSTATE (prev_sv); |
1608 | ta->prev = SvSTATE_hv (prev_sv); |
1628 | TRANSFER_CHECK (*ta); |
1609 | TRANSFER_CHECK (*ta); |
1629 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1610 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1630 | ta->next->flags &= ~CF_READY; |
1611 | ta->next->flags &= ~CF_READY; |
1631 | SvRV_set (coro_current, next_sv); |
1612 | SvRV_set (coro_current, next_sv); |
1632 | |
1613 | |
1633 | LOCK; |
|
|
1634 | free_coro_mortal (aTHX); |
1614 | free_coro_mortal (aTHX); |
1635 | coro_mortal = prev_sv; |
1615 | coro_mortal = prev_sv; |
1636 | UNLOCK; |
|
|
1637 | } |
1616 | } |
1638 | |
1617 | |
1639 | INLINE void |
1618 | INLINE void |
1640 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1619 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1641 | { |
1620 | { |
… | |
… | |
1719 | PL_runops = RUNOPS_DEFAULT; |
1698 | PL_runops = RUNOPS_DEFAULT; |
1720 | else |
1699 | else |
1721 | coro->slot->runops = RUNOPS_DEFAULT; |
1700 | coro->slot->runops = RUNOPS_DEFAULT; |
1722 | } |
1701 | } |
1723 | } |
1702 | } |
1724 | |
|
|
1725 | #if 0 |
|
|
1726 | static int |
|
|
1727 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1728 | { |
|
|
1729 | AV *padlist; |
|
|
1730 | AV *av = (AV *)mg->mg_obj; |
|
|
1731 | |
|
|
1732 | abort (); |
|
|
1733 | |
|
|
1734 | return 0; |
|
|
1735 | } |
|
|
1736 | |
|
|
1737 | static MGVTBL coro_gensub_vtbl = { |
|
|
1738 | 0, 0, 0, 0, |
|
|
1739 | coro_gensub_free |
|
|
1740 | }; |
|
|
1741 | #endif |
|
|
1742 | |
1703 | |
1743 | /*****************************************************************************/ |
1704 | /*****************************************************************************/ |
1744 | /* PerlIO::cede */ |
1705 | /* PerlIO::cede */ |
1745 | |
1706 | |
1746 | typedef struct |
1707 | typedef struct |
… | |
… | |
1815 | PerlIOBuf_set_ptrcnt, |
1776 | PerlIOBuf_set_ptrcnt, |
1816 | }; |
1777 | }; |
1817 | |
1778 | |
1818 | /*****************************************************************************/ |
1779 | /*****************************************************************************/ |
1819 | |
1780 | |
1820 | static const CV *slf_cv; /* for quick consistency check */ |
|
|
1821 | |
|
|
1822 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1781 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1782 | static const CV *slf_cv; |
1823 | static SV *slf_arg0; |
1783 | static SV **slf_argv; |
1824 | static SV *slf_arg1; |
1784 | static int slf_argc, slf_arga; /* count, allocated */ |
1825 | static SV *slf_arg2; |
1785 | static I32 slf_ax; /* top of stack, for restore */ |
1826 | |
1786 | |
1827 | /* this restores the stack in the case we patched the entersub, to */ |
1787 | /* this restores the stack in the case we patched the entersub, to */ |
1828 | /* recreate the stack frame as perl will on following calls */ |
1788 | /* recreate the stack frame as perl will on following calls */ |
1829 | /* since entersub cleared the stack */ |
1789 | /* since entersub cleared the stack */ |
1830 | static OP * |
1790 | static OP * |
1831 | pp_restore (pTHX) |
1791 | pp_restore (pTHX) |
1832 | { |
1792 | { |
1833 | dSP; |
1793 | int i; |
|
|
1794 | SV **SP = PL_stack_base + slf_ax; |
1834 | |
1795 | |
1835 | PUSHMARK (SP); |
1796 | PUSHMARK (SP); |
1836 | |
1797 | |
1837 | EXTEND (SP, 3); |
1798 | EXTEND (SP, slf_argc + 1); |
|
|
1799 | |
|
|
1800 | for (i = 0; i < slf_argc; ++i) |
1838 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1801 | PUSHs (sv_2mortal (slf_argv [i])); |
1839 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
1802 | |
1840 | if (slf_arg2) PUSHs (sv_2mortal (slf_arg2)); |
|
|
1841 | PUSHs ((SV *)CvGV (slf_cv)); |
1803 | PUSHs ((SV *)CvGV (slf_cv)); |
1842 | |
1804 | |
1843 | RETURNOP (slf_restore.op_first); |
1805 | RETURNOP (slf_restore.op_first); |
1844 | } |
1806 | } |
1845 | |
1807 | |
… | |
… | |
1848 | { |
1810 | { |
1849 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1811 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1850 | } |
1812 | } |
1851 | |
1813 | |
1852 | static void |
1814 | static void |
1853 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1815 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1854 | { |
1816 | { |
1855 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1817 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1856 | |
1818 | |
1857 | frame->prepare = slf_prepare_set_stacklevel; |
1819 | frame->prepare = slf_prepare_set_stacklevel; |
1858 | frame->check = slf_check_nop; |
1820 | frame->check = slf_check_nop; |
… | |
… | |
1862 | static void |
1824 | static void |
1863 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1825 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1864 | { |
1826 | { |
1865 | SV **arg = (SV **)slf_frame.data; |
1827 | SV **arg = (SV **)slf_frame.data; |
1866 | |
1828 | |
1867 | prepare_transfer (ta, arg [0], arg [1]); |
1829 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1868 | } |
1830 | } |
1869 | |
1831 | |
1870 | static void |
1832 | static void |
1871 | slf_init_transfer (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1833 | slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1872 | { |
1834 | { |
1873 | if (items != 2) |
1835 | if (items != 2) |
1874 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
1836 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
1875 | |
1837 | |
1876 | frame->prepare = slf_prepare_transfer; |
1838 | frame->prepare = slf_prepare_transfer; |
1877 | frame->check = slf_check_nop; |
1839 | frame->check = slf_check_nop; |
1878 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1840 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1879 | } |
1841 | } |
1880 | |
1842 | |
1881 | static void |
1843 | static void |
1882 | slf_init_schedule (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1844 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1883 | { |
1845 | { |
1884 | frame->prepare = prepare_schedule; |
1846 | frame->prepare = prepare_schedule; |
1885 | frame->check = slf_check_nop; |
1847 | frame->check = slf_check_nop; |
1886 | } |
1848 | } |
1887 | |
1849 | |
1888 | static void |
1850 | static void |
1889 | slf_init_cede (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1851 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1890 | { |
1852 | { |
1891 | frame->prepare = prepare_cede; |
1853 | frame->prepare = prepare_cede; |
1892 | frame->check = slf_check_nop; |
1854 | frame->check = slf_check_nop; |
1893 | } |
1855 | } |
1894 | |
1856 | |
1895 | static void |
1857 | static void |
1896 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1858 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1897 | { |
1859 | { |
1898 | frame->prepare = prepare_cede_notself; |
1860 | frame->prepare = prepare_cede_notself; |
1899 | frame->check = slf_check_nop; |
1861 | frame->check = slf_check_nop; |
1900 | } |
1862 | } |
1901 | |
1863 | |
… | |
… | |
1927 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1889 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1928 | /* for us, divert to the real entersub */ |
1890 | /* for us, divert to the real entersub */ |
1929 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1891 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1930 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1892 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1931 | |
1893 | |
1932 | /* pop args */ |
|
|
1933 | SP = PL_stack_base + POPMARK; |
|
|
1934 | |
|
|
1935 | if (!(PL_op->op_flags & OPf_STACKED)) |
1894 | if (!(PL_op->op_flags & OPf_STACKED)) |
1936 | { |
1895 | { |
1937 | /* ampersand-form of call, use @_ instead of stack */ |
1896 | /* ampersand-form of call, use @_ instead of stack */ |
1938 | AV *av = GvAV (PL_defgv); |
1897 | AV *av = GvAV (PL_defgv); |
1939 | arg = AvARRAY (av); |
1898 | arg = AvARRAY (av); |
1940 | items = AvFILLp (av) + 1; |
1899 | items = AvFILLp (av) + 1; |
1941 | } |
1900 | } |
1942 | |
1901 | |
|
|
1902 | /* now call the init function, which needs to set up slf_frame */ |
|
|
1903 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) |
|
|
1904 | (aTHX_ &slf_frame, GvCV (gv), arg, items); |
|
|
1905 | |
|
|
1906 | /* pop args */ |
|
|
1907 | SP = PL_stack_base + POPMARK; |
|
|
1908 | |
1943 | PUTBACK; |
1909 | PUTBACK; |
1944 | |
|
|
1945 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items); |
|
|
1946 | } |
1910 | } |
1947 | |
1911 | |
1948 | /* now interpret the slf_frame */ |
1912 | /* now that we have a slf_frame, interpret it! */ |
1949 | /* we use a callback system not to make the code needlessly */ |
1913 | /* we use a callback system not to make the code needlessly */ |
1950 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1914 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1951 | |
1915 | |
1952 | do |
1916 | do |
1953 | { |
1917 | { |
… | |
… | |
1958 | |
1922 | |
1959 | checkmark = PL_stack_sp - PL_stack_base; |
1923 | checkmark = PL_stack_sp - PL_stack_base; |
1960 | } |
1924 | } |
1961 | while (slf_frame.check (aTHX_ &slf_frame)); |
1925 | while (slf_frame.check (aTHX_ &slf_frame)); |
1962 | |
1926 | |
|
|
1927 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
|
|
1928 | |
|
|
1929 | /* return value handling - mostly like entersub */ |
1963 | { |
1930 | { |
1964 | dSP; |
1931 | dSP; |
1965 | SV **bot = PL_stack_base + checkmark; |
1932 | SV **bot = PL_stack_base + checkmark; |
1966 | int gimme = GIMME_V; |
1933 | int gimme = GIMME_V; |
1967 | |
|
|
1968 | slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */ |
|
|
1969 | |
1934 | |
1970 | /* make sure we put something on the stack in scalar context */ |
1935 | /* make sure we put something on the stack in scalar context */ |
1971 | if (gimme == G_SCALAR) |
1936 | if (gimme == G_SCALAR) |
1972 | { |
1937 | { |
1973 | if (sp == bot) |
1938 | if (sp == bot) |
… | |
… | |
1977 | } |
1942 | } |
1978 | |
1943 | |
1979 | PUTBACK; |
1944 | PUTBACK; |
1980 | } |
1945 | } |
1981 | |
1946 | |
|
|
1947 | /* exception handling */ |
|
|
1948 | if (expect_false (coro_throw)) |
|
|
1949 | { |
|
|
1950 | SV *exception = sv_2mortal (coro_throw); |
|
|
1951 | |
|
|
1952 | coro_throw = 0; |
|
|
1953 | sv_setsv (ERRSV, exception); |
|
|
1954 | croak (0); |
|
|
1955 | } |
|
|
1956 | |
1982 | return NORMAL; |
1957 | return NORMAL; |
1983 | } |
1958 | } |
1984 | |
1959 | |
1985 | static void |
1960 | static void |
1986 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) |
1961 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax) |
1987 | { |
1962 | { |
|
|
1963 | int i; |
|
|
1964 | SV **arg = PL_stack_base + ax; |
|
|
1965 | int items = PL_stack_sp - arg + 1; |
|
|
1966 | |
1988 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1967 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1989 | |
1968 | |
1990 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1969 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1991 | && PL_op->op_ppaddr != pp_slf) |
1970 | && PL_op->op_ppaddr != pp_slf) |
1992 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1971 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1993 | |
1972 | |
1994 | if (items > 3) |
|
|
1995 | croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); |
|
|
1996 | |
|
|
1997 | CvFLAGS (cv) |= CVf_SLF; |
1973 | CvFLAGS (cv) |= CVf_SLF; |
1998 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1974 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1999 | slf_cv = cv; |
1975 | slf_cv = cv; |
2000 | |
1976 | |
2001 | /* we patch the op, and then re-run the whole call */ |
1977 | /* we patch the op, and then re-run the whole call */ |
2002 | /* we have to put the same argument on the stack for this to work */ |
1978 | /* we have to put the same argument on the stack for this to work */ |
2003 | /* and this will be done by pp_restore */ |
1979 | /* and this will be done by pp_restore */ |
2004 | slf_restore.op_next = (OP *)&slf_restore; |
1980 | slf_restore.op_next = (OP *)&slf_restore; |
2005 | slf_restore.op_type = OP_NULL; |
1981 | slf_restore.op_type = OP_CUSTOM; |
2006 | slf_restore.op_ppaddr = pp_restore; |
1982 | slf_restore.op_ppaddr = pp_restore; |
2007 | slf_restore.op_first = PL_op; |
1983 | slf_restore.op_first = PL_op; |
2008 | |
1984 | |
2009 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1985 | slf_ax = ax - 1; /* undo the ax++ inside dAXMARK */ |
2010 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
1986 | |
2011 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
1987 | if (items > slf_arga) |
|
|
1988 | { |
|
|
1989 | slf_arga = items; |
|
|
1990 | free (slf_argv); |
|
|
1991 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
|
|
1992 | } |
|
|
1993 | |
|
|
1994 | slf_argc = items; |
|
|
1995 | |
|
|
1996 | for (i = 0; i < items; ++i) |
|
|
1997 | slf_argv [i] = SvREFCNT_inc (arg [i]); |
2012 | |
1998 | |
2013 | PL_op->op_ppaddr = pp_slf; |
1999 | PL_op->op_ppaddr = pp_slf; |
|
|
2000 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2014 | |
2001 | |
2015 | PL_op = (OP *)&slf_restore; |
2002 | PL_op = (OP *)&slf_restore; |
2016 | } |
2003 | } |
2017 | |
2004 | |
2018 | /*****************************************************************************/ |
2005 | /*****************************************************************************/ |
|
|
2006 | |
|
|
2007 | static void |
|
|
2008 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
|
|
2009 | { |
|
|
2010 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2011 | IV count = SvIVX (count_sv); |
|
|
2012 | |
|
|
2013 | count += adjust; |
|
|
2014 | SvIVX (count_sv) = count; |
|
|
2015 | |
|
|
2016 | /* now wake up as many waiters as are expected to lock */ |
|
|
2017 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2018 | { |
|
|
2019 | SV *cb; |
|
|
2020 | |
|
|
2021 | /* swap first two elements so we can shift a waiter */ |
|
|
2022 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2023 | AvARRAY (av)[1] = count_sv; |
|
|
2024 | cb = av_shift (av); |
|
|
2025 | |
|
|
2026 | if (SvOBJECT (cb)) |
|
|
2027 | api_ready (aTHX_ cb); |
|
|
2028 | else |
|
|
2029 | croak ("callbacks not yet supported"); |
|
|
2030 | |
|
|
2031 | SvREFCNT_dec (cb); |
|
|
2032 | |
|
|
2033 | --count; |
|
|
2034 | } |
|
|
2035 | } |
|
|
2036 | |
|
|
2037 | static void |
|
|
2038 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2039 | { |
|
|
2040 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
|
|
2041 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
|
|
2042 | } |
2019 | |
2043 | |
2020 | static int |
2044 | static int |
2021 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2045 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2022 | { |
2046 | { |
2023 | AV *av = (AV *)frame->data; |
2047 | AV *av = (AV *)frame->data; |
2024 | SV *count_sv = AvARRAY (av)[0]; |
2048 | SV *count_sv = AvARRAY (av)[0]; |
2025 | |
2049 | |
2026 | if (SvIVX (count_sv) > 0) |
2050 | if (SvIVX (count_sv) > 0) |
2027 | { |
2051 | { |
|
|
2052 | SvSTATE_current->on_destroy = 0; |
2028 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2053 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2029 | return 0; |
2054 | return 0; |
2030 | } |
2055 | } |
2031 | else |
2056 | else |
2032 | { |
2057 | { |
… | |
… | |
2043 | return 1; |
2068 | return 1; |
2044 | } |
2069 | } |
2045 | } |
2070 | } |
2046 | |
2071 | |
2047 | static void |
2072 | static void |
2048 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
2073 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2049 | { |
2074 | { |
2050 | AV *av = (AV *)SvRV (arg [0]); |
2075 | AV *av = (AV *)SvRV (arg [0]); |
2051 | |
2076 | |
2052 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2077 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2053 | { |
2078 | { |
2054 | frame->data = (void *)av; |
2079 | frame->data = (void *)av; |
2055 | frame->prepare = prepare_nop; |
2080 | frame->prepare = prepare_nop; |
|
|
2081 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2056 | } |
2082 | } |
2057 | else |
2083 | else |
2058 | { |
2084 | { |
2059 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2085 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2060 | |
2086 | |
2061 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2087 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2062 | frame->prepare = prepare_schedule; |
2088 | frame->prepare = prepare_schedule; |
|
|
2089 | |
|
|
2090 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2091 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2092 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2093 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2063 | } |
2094 | } |
2064 | |
2095 | |
2065 | frame->check = slf_check_semaphore_down; |
2096 | frame->check = slf_check_semaphore_down; |
2066 | |
2097 | |
2067 | } |
2098 | } |
2068 | |
2099 | |
|
|
2100 | /*****************************************************************************/ |
|
|
2101 | |
|
|
2102 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2103 | |
|
|
2104 | /* create a closure from XS, returns a code reference */ |
|
|
2105 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2106 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2107 | static SV * |
|
|
2108 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2109 | { |
|
|
2110 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2111 | |
|
|
2112 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2113 | |
|
|
2114 | CvANON_on (cv); |
|
|
2115 | CvISXSUB_on (cv); |
|
|
2116 | CvXSUB (cv) = xsub; |
|
|
2117 | GENSUB_ARG = arg; |
|
|
2118 | |
|
|
2119 | return newRV_noinc ((SV *)cv); |
|
|
2120 | } |
|
|
2121 | |
|
|
2122 | /*****************************************************************************/ |
|
|
2123 | |
2069 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2124 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2070 | |
2125 | |
2071 | PROTOTYPES: DISABLE |
2126 | PROTOTYPES: DISABLE |
2072 | |
2127 | |
2073 | BOOT: |
2128 | BOOT: |
2074 | { |
2129 | { |
2075 | #ifdef USE_ITHREADS |
2130 | #ifdef USE_ITHREADS |
2076 | MUTEX_INIT (&coro_lock); |
|
|
2077 | # if CORO_PTHREAD |
2131 | # if CORO_PTHREAD |
2078 | coro_thx = PERL_GET_CONTEXT; |
2132 | coro_thx = PERL_GET_CONTEXT; |
2079 | # endif |
2133 | # endif |
2080 | #endif |
2134 | #endif |
2081 | BOOT_PAGESIZE; |
2135 | BOOT_PAGESIZE; |
… | |
… | |
2101 | main_mainstack = PL_mainstack; |
2155 | main_mainstack = PL_mainstack; |
2102 | main_top_env = PL_top_env; |
2156 | main_top_env = PL_top_env; |
2103 | |
2157 | |
2104 | while (main_top_env->je_prev) |
2158 | while (main_top_env->je_prev) |
2105 | main_top_env = main_top_env->je_prev; |
2159 | main_top_env = main_top_env->je_prev; |
|
|
2160 | |
|
|
2161 | { |
|
|
2162 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2163 | |
|
|
2164 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2165 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2166 | newSVpv ("coro_slf", 0), 0); |
|
|
2167 | |
|
|
2168 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2169 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2170 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2171 | } |
2106 | |
2172 | |
2107 | coroapi.ver = CORO_API_VERSION; |
2173 | coroapi.ver = CORO_API_VERSION; |
2108 | coroapi.rev = CORO_API_REVISION; |
2174 | coroapi.rev = CORO_API_REVISION; |
2109 | |
2175 | |
2110 | coroapi.transfer = api_transfer; |
2176 | coroapi.transfer = api_transfer; |
… | |
… | |
2158 | RETVAL |
2224 | RETVAL |
2159 | |
2225 | |
2160 | void |
2226 | void |
2161 | _set_stacklevel (...) |
2227 | _set_stacklevel (...) |
2162 | CODE: |
2228 | CODE: |
2163 | api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); |
2229 | CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); |
2164 | |
2230 | |
2165 | void |
2231 | void |
2166 | transfer (...) |
2232 | transfer (...) |
2167 | PROTOTYPE: $$ |
2233 | PROTOTYPE: $$ |
2168 | CODE: |
2234 | CODE: |
2169 | api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); |
2235 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
2170 | |
2236 | |
2171 | bool |
2237 | bool |
2172 | _destroy (SV *coro_sv) |
2238 | _destroy (SV *coro_sv) |
2173 | CODE: |
2239 | CODE: |
2174 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2240 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2292 | |
2358 | |
2293 | void |
2359 | void |
2294 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2360 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2295 | PROTOTYPE: $;$ |
2361 | PROTOTYPE: $;$ |
2296 | CODE: |
2362 | CODE: |
|
|
2363 | { |
|
|
2364 | struct coro *current = SvSTATE_current; |
|
|
2365 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2297 | SvREFCNT_dec (self->throw); |
2366 | SvREFCNT_dec (*throwp); |
2298 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2367 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2368 | } |
2299 | |
2369 | |
2300 | void |
2370 | void |
2301 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2371 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2302 | PROTOTYPE: $;$ |
2372 | PROTOTYPE: $;$ |
2303 | C_ARGS: aTHX_ coro, flags |
2373 | C_ARGS: aTHX_ coro, flags |
… | |
… | |
2334 | |
2404 | |
2335 | void |
2405 | void |
2336 | force_cctx () |
2406 | force_cctx () |
2337 | PROTOTYPE: |
2407 | PROTOTYPE: |
2338 | CODE: |
2408 | CODE: |
2339 | struct coro *coro = SvSTATE (coro_current); |
|
|
2340 | coro->cctx->idle_sp = 0; |
2409 | SvSTATE_current->cctx->idle_sp = 0; |
2341 | |
2410 | |
2342 | void |
2411 | void |
2343 | swap_defsv (Coro::State self) |
2412 | swap_defsv (Coro::State self) |
2344 | PROTOTYPE: $ |
2413 | PROTOTYPE: $ |
2345 | ALIAS: |
2414 | ALIAS: |
… | |
… | |
2398 | } |
2467 | } |
2399 | |
2468 | |
2400 | void |
2469 | void |
2401 | schedule (...) |
2470 | schedule (...) |
2402 | CODE: |
2471 | CODE: |
2403 | api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); |
2472 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
2404 | |
2473 | |
2405 | void |
2474 | void |
2406 | cede (...) |
2475 | cede (...) |
2407 | CODE: |
2476 | CODE: |
2408 | api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); |
2477 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2409 | |
2478 | |
2410 | void |
2479 | void |
2411 | cede_notself (...) |
2480 | cede_notself (...) |
2412 | CODE: |
2481 | CODE: |
2413 | api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); |
2482 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
2414 | |
2483 | |
2415 | void |
2484 | void |
2416 | _set_current (SV *current) |
2485 | _set_current (SV *current) |
2417 | PROTOTYPE: $ |
2486 | PROTOTYPE: $ |
2418 | CODE: |
2487 | CODE: |
… | |
… | |
2421 | |
2490 | |
2422 | void |
2491 | void |
2423 | _set_readyhook (SV *hook) |
2492 | _set_readyhook (SV *hook) |
2424 | PROTOTYPE: $ |
2493 | PROTOTYPE: $ |
2425 | CODE: |
2494 | CODE: |
2426 | LOCK; |
|
|
2427 | SvREFCNT_dec (coro_readyhook); |
2495 | SvREFCNT_dec (coro_readyhook); |
2428 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2496 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2429 | UNLOCK; |
|
|
2430 | |
2497 | |
2431 | int |
2498 | int |
2432 | prio (Coro::State coro, int newprio = 0) |
2499 | prio (Coro::State coro, int newprio = 0) |
2433 | PROTOTYPE: $;$ |
2500 | PROTOTYPE: $;$ |
2434 | ALIAS: |
2501 | ALIAS: |
… | |
… | |
2470 | # for async_pool speedup |
2537 | # for async_pool speedup |
2471 | void |
2538 | void |
2472 | _pool_1 (SV *cb) |
2539 | _pool_1 (SV *cb) |
2473 | CODE: |
2540 | CODE: |
2474 | { |
2541 | { |
2475 | struct coro *coro = SvSTATE (coro_current); |
|
|
2476 | HV *hv = (HV *)SvRV (coro_current); |
2542 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2543 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2477 | AV *defav = GvAV (PL_defgv); |
2544 | AV *defav = GvAV (PL_defgv); |
2478 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2545 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2479 | AV *invoke_av; |
2546 | AV *invoke_av; |
2480 | int i, len; |
2547 | int i, len; |
2481 | |
2548 | |
… | |
… | |
2508 | |
2575 | |
2509 | void |
2576 | void |
2510 | _pool_2 (SV *cb) |
2577 | _pool_2 (SV *cb) |
2511 | CODE: |
2578 | CODE: |
2512 | { |
2579 | { |
2513 | struct coro *coro = SvSTATE (coro_current); |
2580 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2581 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2514 | |
2582 | |
2515 | sv_setsv (cb, &PL_sv_undef); |
2583 | sv_setsv (cb, &PL_sv_undef); |
2516 | |
2584 | |
2517 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2585 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2518 | coro->saved_deffh = 0; |
2586 | coro->saved_deffh = 0; |
… | |
… | |
2525 | SvREFCNT_dec (old); |
2593 | SvREFCNT_dec (old); |
2526 | croak ("\3async_pool terminate\2\n"); |
2594 | croak ("\3async_pool terminate\2\n"); |
2527 | } |
2595 | } |
2528 | |
2596 | |
2529 | av_clear (GvAV (PL_defgv)); |
2597 | av_clear (GvAV (PL_defgv)); |
2530 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2598 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2531 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2599 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2532 | |
2600 | |
2533 | coro->prio = 0; |
2601 | coro->prio = 0; |
2534 | |
2602 | |
2535 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2603 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2536 | api_trace (aTHX_ coro_current, 0); |
2604 | api_trace (aTHX_ coro_current, 0); |
2537 | |
2605 | |
2538 | av_push (av_async_pool, newSVsv (coro_current)); |
2606 | av_push (av_async_pool, newSVsv (coro_current)); |
2539 | } |
2607 | } |
2540 | |
|
|
2541 | #if 0 |
|
|
2542 | |
|
|
2543 | void |
|
|
2544 | _generator_call (...) |
|
|
2545 | PROTOTYPE: @ |
|
|
2546 | PPCODE: |
|
|
2547 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2548 | xxxx |
|
|
2549 | abort (); |
|
|
2550 | |
|
|
2551 | SV * |
|
|
2552 | gensub (SV *sub, ...) |
|
|
2553 | PROTOTYPE: &;@ |
|
|
2554 | CODE: |
|
|
2555 | { |
|
|
2556 | struct coro *coro; |
|
|
2557 | MAGIC *mg; |
|
|
2558 | CV *xcv; |
|
|
2559 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2560 | int i; |
|
|
2561 | |
|
|
2562 | CvGV (ncv) = CvGV (cv); |
|
|
2563 | CvFILE (ncv) = CvFILE (cv); |
|
|
2564 | |
|
|
2565 | Newz (0, coro, 1, struct coro); |
|
|
2566 | coro->args = newAV (); |
|
|
2567 | coro->flags = CF_NEW; |
|
|
2568 | |
|
|
2569 | av_extend (coro->args, items - 1); |
|
|
2570 | for (i = 1; i < items; i++) |
|
|
2571 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2572 | |
|
|
2573 | CvISXSUB_on (ncv); |
|
|
2574 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2575 | |
|
|
2576 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2577 | |
|
|
2578 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2579 | CvANON_on (ncv); |
|
|
2580 | |
|
|
2581 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2582 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2583 | } |
|
|
2584 | OUTPUT: |
|
|
2585 | RETVAL |
|
|
2586 | |
|
|
2587 | #endif |
|
|
2588 | |
2608 | |
2589 | |
2609 | |
2590 | MODULE = Coro::State PACKAGE = Coro::AIO |
2610 | MODULE = Coro::State PACKAGE = Coro::AIO |
2591 | |
2611 | |
2592 | void |
2612 | void |
… | |
… | |
2642 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2662 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2643 | |
2663 | |
2644 | BOOT: |
2664 | BOOT: |
2645 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2665 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2646 | |
2666 | |
2647 | SV * |
2667 | void |
2648 | _schedule (...) |
2668 | _schedule (...) |
2649 | CODE: |
2669 | CODE: |
2650 | { |
2670 | { |
2651 | static int incede; |
2671 | static int incede; |
2652 | |
2672 | |
… | |
… | |
2698 | void |
2718 | void |
2699 | up (SV *self, int adjust = 1) |
2719 | up (SV *self, int adjust = 1) |
2700 | ALIAS: |
2720 | ALIAS: |
2701 | adjust = 1 |
2721 | adjust = 1 |
2702 | CODE: |
2722 | CODE: |
2703 | { |
2723 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2704 | AV *av = (AV *)SvRV (self); |
|
|
2705 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2706 | IV count = SvIVX (count_sv); |
|
|
2707 | |
|
|
2708 | count += ix ? adjust : 1; |
|
|
2709 | SvIVX (count_sv) = count; |
|
|
2710 | |
|
|
2711 | /* now wake up as many waiters as possible */ |
|
|
2712 | while (count > 0 && AvFILLp (av) >= count) |
|
|
2713 | { |
|
|
2714 | SV *cb; |
|
|
2715 | |
|
|
2716 | /* swap first two elements so we can shift a waiter */ |
|
|
2717 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2718 | AvARRAY (av)[1] = count_sv; |
|
|
2719 | cb = av_shift (av); |
|
|
2720 | |
|
|
2721 | if (SvOBJECT (cb)) |
|
|
2722 | api_ready (cb); |
|
|
2723 | else |
|
|
2724 | croak ("callbacks not yet supported"); |
|
|
2725 | |
|
|
2726 | SvREFCNT_dec (cb); |
|
|
2727 | } |
|
|
2728 | } |
|
|
2729 | |
2724 | |
2730 | void |
2725 | void |
2731 | down (SV *self) |
2726 | down (SV *self) |
2732 | CODE: |
2727 | CODE: |
2733 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
2728 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2734 | |
2729 | |
2735 | void |
2730 | void |
2736 | try (SV *self) |
2731 | try (SV *self) |
2737 | PPCODE: |
2732 | PPCODE: |
2738 | { |
2733 | { |