… | |
… | |
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; |
… | |
… | |
252 | #undef VAR |
236 | #undef VAR |
253 | } perl_slots; |
237 | } perl_slots; |
254 | |
238 | |
255 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
239 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
256 | |
240 | |
257 | /* this is the per-perl-coro slf frame info */ |
|
|
258 | /* it is treated like other "global" interpreter data */ |
|
|
259 | /* and unfortunately is copied around, so kepe it small */ |
|
|
260 | struct slf_frame |
|
|
261 | { |
|
|
262 | void (*prepare) (struct coro_transfer_args *ta); /* 0 means not yet initialised */ |
|
|
263 | int (*check) (pTHX); |
|
|
264 | }; |
|
|
265 | |
|
|
266 | /* this is a structure representing a perl-level coroutine */ |
241 | /* this is a structure representing a perl-level coroutine */ |
267 | struct coro { |
242 | struct coro { |
268 | /* the C coroutine allocated to this perl coroutine, if any */ |
243 | /* the C coroutine allocated to this perl coroutine, if any */ |
269 | coro_cctx *cctx; |
244 | coro_cctx *cctx; |
270 | |
245 | |
271 | /* process data */ |
246 | /* state data */ |
272 | struct slf_frame slf_frame; /* saved slf frame */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
273 | void *slf_data; |
|
|
274 | AV *mainstack; |
248 | AV *mainstack; |
275 | perl_slots *slot; /* basically the saved sp */ |
249 | perl_slots *slot; /* basically the saved sp */ |
276 | |
250 | |
277 | AV *args; /* data associated with this coroutine (initial args) */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
278 | int refcnt; /* coroutines are refcounted, yes */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
279 | int flags; /* CF_ flags */ |
253 | int flags; /* CF_ flags */ |
280 | 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); |
281 | |
256 | |
282 | /* statistics */ |
257 | /* statistics */ |
283 | int usecount; /* number of transfers to this coro */ |
258 | int usecount; /* number of transfers to this coro */ |
284 | |
259 | |
285 | /* coro process data */ |
260 | /* coro process data */ |
… | |
… | |
294 | }; |
269 | }; |
295 | |
270 | |
296 | typedef struct coro *Coro__State; |
271 | typedef struct coro *Coro__State; |
297 | typedef struct coro *Coro__State_or_hashref; |
272 | typedef struct coro *Coro__State_or_hashref; |
298 | |
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 */ |
299 | static struct slf_frame slf_frame; /* the current slf frame */ |
277 | static struct CoroSLF slf_frame; /* the current slf frame */ |
|
|
278 | static SV *coro_throw; |
300 | |
279 | |
301 | /** Coro ********************************************************************/ |
280 | /** Coro ********************************************************************/ |
302 | |
281 | |
303 | #define PRIO_MAX 3 |
282 | #define PRIO_MAX 3 |
304 | #define PRIO_HIGH 1 |
283 | #define PRIO_HIGH 1 |
… | |
… | |
413 | static MGVTBL coro_cv_vtbl = { |
392 | static MGVTBL coro_cv_vtbl = { |
414 | 0, 0, 0, 0, |
393 | 0, 0, 0, 0, |
415 | coro_cv_free |
394 | coro_cv_free |
416 | }; |
395 | }; |
417 | |
396 | |
418 | #define CORO_MAGIC(sv, type) \ |
397 | #define CORO_MAGIC(sv, type) \ |
419 | SvMAGIC (sv) \ |
398 | expect_true (SvMAGIC (sv)) \ |
420 | ? SvMAGIC (sv)->mg_type == type \ |
399 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
421 | ? SvMAGIC (sv) \ |
400 | ? SvMAGIC (sv) \ |
422 | : mg_find (sv, type) \ |
401 | : mg_find (sv, type) \ |
423 | : 0 |
402 | : 0 |
424 | |
403 | |
425 | #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) |
426 | #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) |
427 | |
406 | |
… | |
… | |
448 | mg = CORO_MAGIC_state (coro); |
427 | mg = CORO_MAGIC_state (coro); |
449 | return (struct coro *)mg->mg_ptr; |
428 | return (struct coro *)mg->mg_ptr; |
450 | } |
429 | } |
451 | |
430 | |
452 | #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)) |
453 | |
445 | |
454 | /* the next two functions merely cache the padlists */ |
446 | /* the next two functions merely cache the padlists */ |
455 | static void |
447 | static void |
456 | get_padlist (pTHX_ CV *cv) |
448 | get_padlist (pTHX_ CV *cv) |
457 | { |
449 | { |
… | |
… | |
525 | } |
517 | } |
526 | |
518 | |
527 | PUTBACK; |
519 | PUTBACK; |
528 | } |
520 | } |
529 | |
521 | |
530 | slf_frame = c->slf_frame; |
522 | slf_frame = c->slf_frame; |
531 | coroapi.slf_data = c->slf_data; |
523 | coro_throw = c->throw; |
532 | } |
524 | } |
533 | |
525 | |
534 | static void |
526 | static void |
535 | save_perl (pTHX_ Coro__State c) |
527 | save_perl (pTHX_ Coro__State c) |
536 | { |
528 | { |
537 | c->slf_data = coroapi.slf_data; |
529 | c->throw = coro_throw; |
538 | c->slf_frame = slf_frame; |
530 | c->slf_frame = slf_frame; |
539 | |
531 | |
540 | { |
532 | { |
541 | dSP; |
533 | dSP; |
542 | I32 cxix = cxstack_ix; |
534 | I32 cxix = cxstack_ix; |
… | |
… | |
823 | |
815 | |
824 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
816 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
825 | } |
817 | } |
826 | |
818 | |
827 | static void |
819 | static void |
828 | prepare_nop (aTHX_ struct coro_transfer_args *ta) |
820 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
829 | { |
821 | { |
830 | /* kind of mega-hacky, but works */ |
822 | /* kind of mega-hacky, but works */ |
831 | ta->next = ta->prev = (struct coro *)ta; |
823 | ta->next = ta->prev = (struct coro *)ta; |
832 | } |
824 | } |
833 | |
825 | |
834 | static int |
826 | static int |
835 | slf_check_nop (aTHX) |
827 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
836 | { |
828 | { |
837 | return 0; |
829 | return 0; |
838 | } |
830 | } |
839 | |
831 | |
840 | static void |
832 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
841 | coro_setup (pTHX_ struct coro *coro) |
833 | coro_setup (pTHX_ struct coro *coro) |
842 | { |
834 | { |
843 | /* |
835 | /* |
844 | * emulate part of the perl startup here. |
836 | * emulate part of the perl startup here. |
845 | */ |
837 | */ |
… | |
… | |
888 | /* 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 |
889 | * 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. |
890 | */ |
882 | */ |
891 | 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 */ |
892 | 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; |
893 | } |
887 | } |
894 | |
888 | |
895 | static void |
889 | static void |
896 | coro_destruct (pTHX_ struct coro *coro) |
890 | coro_destruct (pTHX_ struct coro *coro) |
897 | { |
891 | { |
… | |
… | |
921 | |
915 | |
922 | SvREFCNT_dec (PL_diehook); |
916 | SvREFCNT_dec (PL_diehook); |
923 | SvREFCNT_dec (PL_warnhook); |
917 | SvREFCNT_dec (PL_warnhook); |
924 | |
918 | |
925 | SvREFCNT_dec (coro->saved_deffh); |
919 | SvREFCNT_dec (coro->saved_deffh); |
926 | SvREFCNT_dec (coro->throw); |
920 | SvREFCNT_dec (coro_throw); |
927 | |
921 | |
928 | coro_destruct_stacks (aTHX); |
922 | coro_destruct_stacks (aTHX); |
929 | } |
923 | } |
930 | |
924 | |
931 | INLINE void |
925 | INLINE void |
… | |
… | |
941 | static int |
935 | static int |
942 | runops_trace (pTHX) |
936 | runops_trace (pTHX) |
943 | { |
937 | { |
944 | COP *oldcop = 0; |
938 | COP *oldcop = 0; |
945 | int oldcxix = -2; |
939 | int oldcxix = -2; |
946 | 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 */ |
947 | coro_cctx *cctx = coro->cctx; |
941 | coro_cctx *cctx = coro->cctx; |
948 | |
942 | |
949 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
943 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
950 | { |
944 | { |
951 | PERL_ASYNC_CHECK (); |
945 | PERL_ASYNC_CHECK (); |
… | |
… | |
1100 | |
1094 | |
1101 | /* 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 */ |
1102 | INLINE void |
1096 | INLINE void |
1103 | transfer_tail (pTHX) |
1097 | transfer_tail (pTHX) |
1104 | { |
1098 | { |
1105 | struct coro *next = (struct coro *)transfer_next; |
|
|
1106 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
|
|
1107 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
|
|
1108 | |
|
|
1109 | free_coro_mortal (aTHX); |
1099 | free_coro_mortal (aTHX); |
1110 | UNLOCK; |
|
|
1111 | |
|
|
1112 | if (expect_false (next->throw)) |
|
|
1113 | { |
|
|
1114 | SV *exception = sv_2mortal (next->throw); |
|
|
1115 | |
|
|
1116 | next->throw = 0; |
|
|
1117 | sv_setsv (ERRSV, exception); |
|
|
1118 | croak (0); |
|
|
1119 | } |
|
|
1120 | } |
1100 | } |
1121 | |
1101 | |
1122 | /* |
1102 | /* |
1123 | * this is a _very_ stripped down perl interpreter ;) |
1103 | * this is a _very_ stripped down perl interpreter ;) |
1124 | */ |
1104 | */ |
… | |
… | |
1139 | |
1119 | |
1140 | /* inject a fake subroutine call to cctx_init */ |
1120 | /* inject a fake subroutine call to cctx_init */ |
1141 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1121 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1142 | |
1122 | |
1143 | /* cctx_run is the alternative tail of transfer() */ |
1123 | /* cctx_run is the alternative tail of transfer() */ |
1144 | /* TODO: throwing an exception here might be deadly, VERIFY */ |
|
|
1145 | transfer_tail (aTHX); |
1124 | transfer_tail (aTHX); |
1146 | |
1125 | |
1147 | /* 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 */ |
1148 | PL_restartop = PL_op; |
1127 | PL_restartop = PL_op; |
1149 | perl_run (PL_curinterp); |
1128 | perl_run (PL_curinterp); |
… | |
… | |
1305 | /** coroutine switching *****************************************************/ |
1284 | /** coroutine switching *****************************************************/ |
1306 | |
1285 | |
1307 | static void |
1286 | static void |
1308 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1287 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1309 | { |
1288 | { |
|
|
1289 | /* TODO: throwing up here is considered harmful */ |
|
|
1290 | |
1310 | if (expect_true (prev != next)) |
1291 | if (expect_true (prev != next)) |
1311 | { |
1292 | { |
1312 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1293 | 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"); |
1294 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1314 | |
1295 | |
1315 | if (expect_false (next->flags & CF_RUNNING)) |
1296 | 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"); |
1297 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1317 | |
1298 | |
1318 | if (expect_false (next->flags & CF_DESTROYED)) |
1299 | 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"); |
1300 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1320 | |
1301 | |
1321 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1302 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1322 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1303 | 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"); |
1304 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1324 | #endif |
1305 | #endif |
1325 | } |
1306 | } |
1326 | } |
1307 | } |
1327 | |
1308 | |
1328 | /* always use the TRANSFER macro */ |
1309 | /* always use the TRANSFER macro */ |
1329 | static void NOINLINE |
1310 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1330 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1311 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1331 | { |
1312 | { |
1332 | dSTACKLEVEL; |
1313 | dSTACKLEVEL; |
1333 | |
1314 | |
1334 | /* sometimes transfer is only called to set idle_sp */ |
1315 | /* sometimes transfer is only called to set idle_sp */ |
1335 | if (expect_false (!next)) |
1316 | if (expect_false (!next)) |
1336 | { |
1317 | { |
1337 | ((coro_cctx *)prev)->idle_sp = stacklevel; |
1318 | ((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 */ |
1319 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1339 | } |
1320 | } |
1340 | else if (expect_true (prev != next)) |
1321 | else if (expect_true (prev != next)) |
1341 | { |
1322 | { |
1342 | coro_cctx *prev__cctx; |
1323 | coro_cctx *prev__cctx; |
… | |
… | |
1349 | prev->flags |= CF_RUNNING; |
1330 | prev->flags |= CF_RUNNING; |
1350 | } |
1331 | } |
1351 | |
1332 | |
1352 | prev->flags &= ~CF_RUNNING; |
1333 | prev->flags &= ~CF_RUNNING; |
1353 | next->flags |= CF_RUNNING; |
1334 | next->flags |= CF_RUNNING; |
1354 | |
|
|
1355 | LOCK; |
|
|
1356 | |
1335 | |
1357 | /* first get rid of the old state */ |
1336 | /* first get rid of the old state */ |
1358 | save_perl (aTHX_ prev); |
1337 | save_perl (aTHX_ prev); |
1359 | |
1338 | |
1360 | if (expect_false (next->flags & CF_NEW)) |
1339 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1369 | |
1348 | |
1370 | prev__cctx = prev->cctx; |
1349 | prev__cctx = prev->cctx; |
1371 | |
1350 | |
1372 | /* possibly untie and reuse the cctx */ |
1351 | /* possibly untie and reuse the cctx */ |
1373 | if (expect_true ( |
1352 | if (expect_true ( |
1374 | prev__cctx->idle_sp == stacklevel |
1353 | prev__cctx->idle_sp == (void *)stacklevel |
1375 | && !(prev__cctx->flags & CC_TRACE) |
1354 | && !(prev__cctx->flags & CC_TRACE) |
1376 | && !force_cctx |
1355 | && !force_cctx |
1377 | )) |
1356 | )) |
1378 | { |
1357 | { |
1379 | /* 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 */ |
… | |
… | |
1393 | ++next->usecount; |
1372 | ++next->usecount; |
1394 | |
1373 | |
1395 | if (expect_true (!next->cctx)) |
1374 | if (expect_true (!next->cctx)) |
1396 | next->cctx = cctx_get (aTHX); |
1375 | next->cctx = cctx_get (aTHX); |
1397 | |
1376 | |
1398 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1399 | transfer_next = next; |
|
|
1400 | |
|
|
1401 | if (expect_false (prev__cctx != next->cctx)) |
1377 | if (expect_false (prev__cctx != next->cctx)) |
1402 | { |
1378 | { |
1403 | prev__cctx->top_env = PL_top_env; |
1379 | prev__cctx->top_env = PL_top_env; |
1404 | PL_top_env = next->cctx->top_env; |
1380 | PL_top_env = next->cctx->top_env; |
1405 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1381 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
… | |
… | |
1418 | coro_state_destroy (pTHX_ struct coro *coro) |
1394 | coro_state_destroy (pTHX_ struct coro *coro) |
1419 | { |
1395 | { |
1420 | if (coro->flags & CF_DESTROYED) |
1396 | if (coro->flags & CF_DESTROYED) |
1421 | return 0; |
1397 | return 0; |
1422 | |
1398 | |
|
|
1399 | if (coro->on_destroy) |
|
|
1400 | coro->on_destroy (aTHX_ coro); |
|
|
1401 | |
1423 | coro->flags |= CF_DESTROYED; |
1402 | coro->flags |= CF_DESTROYED; |
1424 | |
1403 | |
1425 | if (coro->flags & CF_READY) |
1404 | if (coro->flags & CF_READY) |
1426 | { |
1405 | { |
1427 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1406 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1428 | /* alternative: look through all ready queues and remove the coro */ |
1407 | /* alternative: look through all ready queues and remove the coro */ |
1429 | LOCK; |
|
|
1430 | --coro_nready; |
1408 | --coro_nready; |
1431 | UNLOCK; |
|
|
1432 | } |
1409 | } |
1433 | else |
1410 | else |
1434 | 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 */ |
1435 | |
1412 | |
1436 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1413 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1437 | { |
1414 | { |
1438 | struct coro temp; |
1415 | struct coro temp; |
1439 | |
1416 | |
1440 | if (coro->flags & CF_RUNNING) |
1417 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1441 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
1442 | |
1418 | |
1443 | save_perl (aTHX_ &temp); |
1419 | save_perl (aTHX_ &temp); |
1444 | load_perl (aTHX_ coro); |
1420 | load_perl (aTHX_ coro); |
1445 | |
1421 | |
1446 | coro_destruct (aTHX_ coro); |
1422 | coro_destruct (aTHX_ coro); |
… | |
… | |
1515 | TRANSFER (ta, 1); |
1491 | TRANSFER (ta, 1); |
1516 | } |
1492 | } |
1517 | |
1493 | |
1518 | /** Coro ********************************************************************/ |
1494 | /** Coro ********************************************************************/ |
1519 | |
1495 | |
1520 | static void |
1496 | INLINE void |
1521 | coro_enq (pTHX_ SV *coro_sv) |
1497 | coro_enq (pTHX_ struct coro *coro) |
1522 | { |
1498 | { |
1523 | 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)); |
1524 | } |
1500 | } |
1525 | |
1501 | |
1526 | static SV * |
1502 | INLINE SV * |
1527 | coro_deq (pTHX) |
1503 | coro_deq (pTHX) |
1528 | { |
1504 | { |
1529 | int prio; |
1505 | int prio; |
1530 | |
1506 | |
1531 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1507 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1550 | if (coro->flags & CF_READY) |
1526 | if (coro->flags & CF_READY) |
1551 | return 0; |
1527 | return 0; |
1552 | |
1528 | |
1553 | coro->flags |= CF_READY; |
1529 | coro->flags |= CF_READY; |
1554 | |
1530 | |
1555 | LOCK; |
|
|
1556 | |
|
|
1557 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1531 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1558 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1532 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1559 | |
1533 | |
1560 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1534 | coro_enq (aTHX_ coro); |
1561 | ++coro_nready; |
1535 | ++coro_nready; |
1562 | |
1536 | |
1563 | UNLOCK; |
|
|
1564 | |
|
|
1565 | if (sv_hook) |
1537 | if (sv_hook) |
1566 | { |
1538 | { |
1567 | dSP; |
1539 | dSP; |
1568 | |
1540 | |
1569 | ENTER; |
1541 | ENTER; |
… | |
… | |
1595 | { |
1567 | { |
1596 | SV *prev_sv, *next_sv; |
1568 | SV *prev_sv, *next_sv; |
1597 | |
1569 | |
1598 | for (;;) |
1570 | for (;;) |
1599 | { |
1571 | { |
1600 | LOCK; |
|
|
1601 | next_sv = coro_deq (aTHX); |
1572 | next_sv = coro_deq (aTHX); |
1602 | |
1573 | |
1603 | /* nothing to schedule: call the idle handler */ |
1574 | /* nothing to schedule: call the idle handler */ |
1604 | if (expect_false (!next_sv)) |
1575 | if (expect_false (!next_sv)) |
1605 | { |
1576 | { |
1606 | dSP; |
1577 | dSP; |
1607 | UNLOCK; |
|
|
1608 | |
1578 | |
1609 | ENTER; |
1579 | ENTER; |
1610 | SAVETMPS; |
1580 | SAVETMPS; |
1611 | |
1581 | |
1612 | PUSHMARK (SP); |
1582 | PUSHMARK (SP); |
… | |
… | |
1617 | FREETMPS; |
1587 | FREETMPS; |
1618 | LEAVE; |
1588 | LEAVE; |
1619 | continue; |
1589 | continue; |
1620 | } |
1590 | } |
1621 | |
1591 | |
1622 | ta->next = SvSTATE (next_sv); |
1592 | ta->next = SvSTATE_hv (next_sv); |
1623 | |
1593 | |
1624 | /* cannot transfer to destroyed coros, skip and look for next */ |
1594 | /* cannot transfer to destroyed coros, skip and look for next */ |
1625 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1595 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1626 | { |
1596 | { |
1627 | UNLOCK; |
|
|
1628 | SvREFCNT_dec (next_sv); |
1597 | SvREFCNT_dec (next_sv); |
1629 | /* coro_nready has already been taken care of by destroy */ |
1598 | /* coro_nready has already been taken care of by destroy */ |
1630 | continue; |
1599 | continue; |
1631 | } |
1600 | } |
1632 | |
1601 | |
1633 | --coro_nready; |
1602 | --coro_nready; |
1634 | UNLOCK; |
|
|
1635 | break; |
1603 | break; |
1636 | } |
1604 | } |
1637 | |
1605 | |
1638 | /* free this only after the transfer */ |
1606 | /* free this only after the transfer */ |
1639 | prev_sv = SvRV (coro_current); |
1607 | prev_sv = SvRV (coro_current); |
1640 | ta->prev = SvSTATE (prev_sv); |
1608 | ta->prev = SvSTATE_hv (prev_sv); |
1641 | TRANSFER_CHECK (*ta); |
1609 | TRANSFER_CHECK (*ta); |
1642 | 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)); |
1643 | ta->next->flags &= ~CF_READY; |
1611 | ta->next->flags &= ~CF_READY; |
1644 | SvRV_set (coro_current, next_sv); |
1612 | SvRV_set (coro_current, next_sv); |
1645 | |
1613 | |
1646 | LOCK; |
|
|
1647 | free_coro_mortal (aTHX); |
1614 | free_coro_mortal (aTHX); |
1648 | coro_mortal = prev_sv; |
1615 | coro_mortal = prev_sv; |
1649 | UNLOCK; |
|
|
1650 | } |
1616 | } |
1651 | |
1617 | |
1652 | INLINE void |
1618 | INLINE void |
1653 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1619 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1654 | { |
1620 | { |
… | |
… | |
1718 | if (flags & CC_TRACE) |
1684 | if (flags & CC_TRACE) |
1719 | { |
1685 | { |
1720 | if (!coro->cctx) |
1686 | if (!coro->cctx) |
1721 | coro->cctx = cctx_new_run (); |
1687 | coro->cctx = cctx_new_run (); |
1722 | else if (!(coro->cctx->flags & CC_TRACE)) |
1688 | else if (!(coro->cctx->flags & CC_TRACE)) |
1723 | croak ("cannot enable tracing on coroutine with custom stack"); |
1689 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1724 | |
1690 | |
1725 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1691 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1726 | } |
1692 | } |
1727 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1693 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1728 | { |
1694 | { |
… | |
… | |
1732 | PL_runops = RUNOPS_DEFAULT; |
1698 | PL_runops = RUNOPS_DEFAULT; |
1733 | else |
1699 | else |
1734 | coro->slot->runops = RUNOPS_DEFAULT; |
1700 | coro->slot->runops = RUNOPS_DEFAULT; |
1735 | } |
1701 | } |
1736 | } |
1702 | } |
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 | |
1703 | |
1756 | /*****************************************************************************/ |
1704 | /*****************************************************************************/ |
1757 | /* PerlIO::cede */ |
1705 | /* PerlIO::cede */ |
1758 | |
1706 | |
1759 | typedef struct |
1707 | typedef struct |
… | |
… | |
1828 | PerlIOBuf_set_ptrcnt, |
1776 | PerlIOBuf_set_ptrcnt, |
1829 | }; |
1777 | }; |
1830 | |
1778 | |
1831 | /*****************************************************************************/ |
1779 | /*****************************************************************************/ |
1832 | |
1780 | |
1833 | static const CV *slf_cv; /* for quick consistency check */ |
|
|
1834 | |
|
|
1835 | 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; |
1836 | static SV *slf_arg0; |
1783 | static SV **slf_argv; |
1837 | static SV *slf_arg1; |
1784 | static int slf_argc, slf_arga; /* count, allocated */ |
|
|
1785 | static I32 slf_ax; /* top of stack, for restore */ |
1838 | |
1786 | |
1839 | /* 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 */ |
1840 | /* recreate the stack frame as perl will on following calls */ |
1788 | /* recreate the stack frame as perl will on following calls */ |
1841 | /* since entersub cleared the stack */ |
1789 | /* since entersub cleared the stack */ |
1842 | static OP * |
1790 | static OP * |
1843 | pp_restore (pTHX) |
1791 | pp_restore (pTHX) |
1844 | { |
1792 | { |
1845 | dSP; |
1793 | int i; |
|
|
1794 | SV **SP = PL_stack_base + slf_ax; |
1846 | |
1795 | |
1847 | PUSHMARK (SP); |
1796 | PUSHMARK (SP); |
1848 | |
1797 | |
1849 | EXTEND (SP, 3); |
1798 | EXTEND (SP, slf_argc + 1); |
|
|
1799 | |
|
|
1800 | for (i = 0; i < slf_argc; ++i) |
1850 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1801 | PUSHs (sv_2mortal (slf_argv [i])); |
1851 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
1802 | |
1852 | PUSHs ((SV *)CvGV (slf_cv)); |
1803 | PUSHs ((SV *)CvGV (slf_cv)); |
1853 | |
1804 | |
1854 | RETURNOP (slf_restore.op_first); |
1805 | RETURNOP (slf_restore.op_first); |
1855 | } |
1806 | } |
1856 | |
1807 | |
1857 | static void |
1808 | static void |
|
|
1809 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
|
|
1810 | { |
|
|
1811 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
|
|
1812 | } |
|
|
1813 | |
|
|
1814 | static void |
1858 | slf_init_set_stacklevel (pTHX_ SV **arg, int items) |
1815 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1859 | { |
1816 | { |
1860 | 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)); |
1861 | CORO_SLF_DATA = (void *)SvIV (arg [0]); |
|
|
1862 | } |
|
|
1863 | |
1818 | |
|
|
1819 | frame->prepare = slf_prepare_set_stacklevel; |
|
|
1820 | frame->check = slf_check_nop; |
|
|
1821 | frame->data = (void *)SvIV (arg [0]); |
|
|
1822 | } |
|
|
1823 | |
1864 | static void |
1824 | static void |
1865 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1825 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1866 | { |
1826 | { |
1867 | prepare_set_stacklevel (ta, (struct coro_cctx *)CORO_SLF_DATA); |
1827 | SV **arg = (SV **)slf_frame.data; |
1868 | } |
|
|
1869 | |
1828 | |
|
|
1829 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
|
|
1830 | } |
|
|
1831 | |
1870 | static void |
1832 | static void |
1871 | slf_init_transfer (pTHX_ 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 | |
|
|
1838 | frame->prepare = slf_prepare_transfer; |
|
|
1839 | frame->check = slf_check_nop; |
1876 | CORO_SLF_DATA = (void *)arg; /* let's hope it will stay valid */ |
1840 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1877 | } |
1841 | } |
1878 | |
1842 | |
1879 | static void |
1843 | static void |
1880 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1844 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1881 | { |
1845 | { |
1882 | SV **arg = (SV **)CORO_SLF_DATA; |
1846 | frame->prepare = prepare_schedule; |
1883 | |
1847 | frame->check = slf_check_nop; |
1884 | prepare_transfer (ta, arg [0], arg [1]); |
|
|
1885 | } |
1848 | } |
1886 | |
1849 | |
1887 | static void |
1850 | static void |
1888 | slf_init_nop (pTHX_ SV **arg, int items) |
1851 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1889 | { |
1852 | { |
|
|
1853 | frame->prepare = prepare_cede; |
|
|
1854 | frame->check = slf_check_nop; |
1890 | } |
1855 | } |
1891 | |
1856 | |
1892 | /* slf_prepare_schedule == prepare_schedule */ |
1857 | static void |
1893 | /* slf_prepare_cede == prepare_cede */ |
1858 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1894 | /* slf_prepare_notself == prepare_notself */ |
1859 | { |
|
|
1860 | frame->prepare = prepare_cede_notself; |
|
|
1861 | frame->check = slf_check_nop; |
|
|
1862 | } |
1895 | |
1863 | |
1896 | /* we hijack an hopefully unused CV flag for our purposes */ |
1864 | /* we hijack an hopefully unused CV flag for our purposes */ |
1897 | #define CVf_SLF 0x4000 |
1865 | #define CVf_SLF 0x4000 |
1898 | |
1866 | |
1899 | /* |
1867 | /* |
… | |
… | |
1905 | static OP * |
1873 | static OP * |
1906 | pp_slf (pTHX) |
1874 | pp_slf (pTHX) |
1907 | { |
1875 | { |
1908 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
1876 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
1909 | |
1877 | |
|
|
1878 | /* set up the slf frame, unless it has already been set-up */ |
|
|
1879 | /* the latter happens when a new coro has been started */ |
|
|
1880 | /* or when a new cctx was attached to an existing coroutine */ |
1910 | if (expect_true (!slf_frame.prepare)) |
1881 | if (expect_true (!slf_frame.prepare)) |
1911 | { |
1882 | { |
1912 | /* first iteration */ |
1883 | /* first iteration */ |
1913 | dSP; |
1884 | dSP; |
1914 | SV **arg = PL_stack_base + TOPMARK + 1; |
1885 | SV **arg = PL_stack_base + TOPMARK + 1; |
1915 | int items = SP - arg; /* args without function object */ |
1886 | int items = SP - arg; /* args without function object */ |
1916 | SV *gv = *sp; |
1887 | SV *gv = *sp; |
1917 | struct CoroSLF *slf; |
|
|
1918 | |
1888 | |
1919 | /* 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 */ |
1920 | /* for us, divert to the real entersub */ |
1890 | /* for us, divert to the real entersub */ |
1921 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1891 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1922 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1892 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1923 | |
|
|
1924 | /* pop args */ |
|
|
1925 | SP = PL_stack_base + POPMARK; |
|
|
1926 | |
1893 | |
1927 | if (!(PL_op->op_flags & OPf_STACKED)) |
1894 | if (!(PL_op->op_flags & OPf_STACKED)) |
1928 | { |
1895 | { |
1929 | /* ampersand-form of call, use @_ instead of stack */ |
1896 | /* ampersand-form of call, use @_ instead of stack */ |
1930 | AV *av = GvAV (PL_defgv); |
1897 | AV *av = GvAV (PL_defgv); |
1931 | arg = AvARRAY (av); |
1898 | arg = AvARRAY (av); |
1932 | items = AvFILLp (av) + 1; |
1899 | items = AvFILLp (av) + 1; |
1933 | } |
1900 | } |
1934 | |
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 | |
1935 | PUTBACK; |
1909 | PUTBACK; |
1936 | |
|
|
1937 | slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr; |
|
|
1938 | slf_frame.prepare = slf->prepare; |
|
|
1939 | slf_frame.check = slf->check; |
|
|
1940 | slf->init (aTHX_ arg, items); |
|
|
1941 | } |
1910 | } |
1942 | |
1911 | |
1943 | /* now interpret the slf_frame */ |
1912 | /* now that we have a slf_frame, interpret it! */ |
1944 | /* we use a callback system not to make the code needlessly */ |
1913 | /* we use a callback system not to make the code needlessly */ |
1945 | /* 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 */ |
1946 | |
1915 | |
1947 | do |
1916 | do |
1948 | { |
1917 | { |
… | |
… | |
1951 | slf_frame.prepare (aTHX_ &ta); |
1920 | slf_frame.prepare (aTHX_ &ta); |
1952 | TRANSFER (ta, 0); |
1921 | TRANSFER (ta, 0); |
1953 | |
1922 | |
1954 | checkmark = PL_stack_sp - PL_stack_base; |
1923 | checkmark = PL_stack_sp - PL_stack_base; |
1955 | } |
1924 | } |
1956 | while (slf_frame.check (aTHX)); |
1925 | while (slf_frame.check (aTHX_ &slf_frame)); |
1957 | |
1926 | |
|
|
1927 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
|
|
1928 | |
|
|
1929 | /* return value handling - mostly like entersub */ |
1958 | { |
1930 | { |
1959 | dSP; |
1931 | dSP; |
1960 | SV **bot = PL_stack_base + checkmark; |
1932 | SV **bot = PL_stack_base + checkmark; |
1961 | int gimme = GIMME_V; |
1933 | int gimme = GIMME_V; |
1962 | |
|
|
1963 | slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */ |
|
|
1964 | |
1934 | |
1965 | /* make sure we put something on the stack in scalar context */ |
1935 | /* make sure we put something on the stack in scalar context */ |
1966 | if (gimme == G_SCALAR) |
1936 | if (gimme == G_SCALAR) |
1967 | { |
1937 | { |
1968 | if (sp == bot) |
1938 | if (sp == bot) |
… | |
… | |
1972 | } |
1942 | } |
1973 | |
1943 | |
1974 | PUTBACK; |
1944 | PUTBACK; |
1975 | } |
1945 | } |
1976 | |
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 | |
1977 | return NORMAL; |
1957 | return NORMAL; |
1978 | } |
1958 | } |
1979 | |
1959 | |
1980 | static void |
1960 | static void |
1981 | api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items) |
1961 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax) |
1982 | { |
1962 | { |
1983 | assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf)); |
1963 | int i; |
|
|
1964 | SV **arg = PL_stack_base + ax; |
|
|
1965 | int items = PL_stack_sp - arg + 1; |
|
|
1966 | |
1984 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1967 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1985 | |
1968 | |
1986 | if (items > 2) |
1969 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1987 | croak ("Coro only supports a max of two arguments to SLF functions."); |
1970 | && PL_op->op_ppaddr != pp_slf) |
|
|
1971 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1988 | |
1972 | |
1989 | CvFLAGS (cv) |= CVf_SLF; |
1973 | CvFLAGS (cv) |= CVf_SLF; |
1990 | CvXSUBANY (cv).any_ptr = (void *)slf; |
1974 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1991 | slf_cv = cv; |
1975 | slf_cv = cv; |
1992 | |
1976 | |
1993 | /* we patch the op, and then re-run the whole call */ |
1977 | /* we patch the op, and then re-run the whole call */ |
1994 | /* 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 */ |
1995 | /* and this will be done by pp_restore */ |
1979 | /* and this will be done by pp_restore */ |
1996 | slf_restore.op_next = (OP *)&slf_restore; |
1980 | slf_restore.op_next = (OP *)&slf_restore; |
1997 | slf_restore.op_type = OP_NULL; |
1981 | slf_restore.op_type = OP_CUSTOM; |
1998 | slf_restore.op_ppaddr = pp_restore; |
1982 | slf_restore.op_ppaddr = pp_restore; |
1999 | slf_restore.op_first = PL_op; |
1983 | slf_restore.op_first = PL_op; |
2000 | |
1984 | |
2001 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1985 | slf_ax = ax - 1; /* undo the ax++ inside dAXMARK */ |
2002 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
1986 | |
|
|
1987 | if (PL_op->op_flags & OPf_STACKED) |
|
|
1988 | { |
|
|
1989 | if (items > slf_arga) |
|
|
1990 | { |
|
|
1991 | slf_arga = items; |
|
|
1992 | free (slf_argv); |
|
|
1993 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
|
|
1994 | } |
|
|
1995 | |
|
|
1996 | slf_argc = items; |
|
|
1997 | |
|
|
1998 | for (i = 0; i < items; ++i) |
|
|
1999 | slf_argv [i] = SvREFCNT_inc (arg [i]); |
|
|
2000 | } |
|
|
2001 | else |
|
|
2002 | slf_argc = 0; |
2003 | |
2003 | |
2004 | PL_op->op_ppaddr = pp_slf; |
2004 | PL_op->op_ppaddr = pp_slf; |
|
|
2005 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2005 | |
2006 | |
2006 | PL_op = (OP *)&slf_restore; |
2007 | PL_op = (OP *)&slf_restore; |
2007 | } |
2008 | } |
2008 | |
2009 | |
|
|
2010 | /*****************************************************************************/ |
|
|
2011 | |
|
|
2012 | static void |
|
|
2013 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
|
|
2014 | { |
|
|
2015 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2016 | IV count = SvIVX (count_sv); |
|
|
2017 | |
|
|
2018 | count += adjust; |
|
|
2019 | SvIVX (count_sv) = count; |
|
|
2020 | |
|
|
2021 | /* now wake up as many waiters as are expected to lock */ |
|
|
2022 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2023 | { |
|
|
2024 | SV *cb; |
|
|
2025 | |
|
|
2026 | /* swap first two elements so we can shift a waiter */ |
|
|
2027 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2028 | AvARRAY (av)[1] = count_sv; |
|
|
2029 | cb = av_shift (av); |
|
|
2030 | |
|
|
2031 | if (SvOBJECT (cb)) |
|
|
2032 | api_ready (aTHX_ cb); |
|
|
2033 | else |
|
|
2034 | croak ("callbacks not yet supported"); |
|
|
2035 | |
|
|
2036 | SvREFCNT_dec (cb); |
|
|
2037 | |
|
|
2038 | --count; |
|
|
2039 | } |
|
|
2040 | } |
|
|
2041 | |
|
|
2042 | static void |
|
|
2043 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2044 | { |
|
|
2045 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
|
|
2046 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
|
|
2047 | } |
|
|
2048 | |
|
|
2049 | static int |
|
|
2050 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2051 | { |
|
|
2052 | AV *av = (AV *)frame->data; |
|
|
2053 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2054 | |
|
|
2055 | if (SvIVX (count_sv) > 0) |
|
|
2056 | { |
|
|
2057 | SvSTATE_current->on_destroy = 0; |
|
|
2058 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2059 | return 0; |
|
|
2060 | } |
|
|
2061 | else |
|
|
2062 | { |
|
|
2063 | int i; |
|
|
2064 | /* if we were woken up but can't down, we look through the whole */ |
|
|
2065 | /* waiters list and only add us if we aren't in there already */ |
|
|
2066 | /* this avoids some degenerate memory usage cases */ |
|
|
2067 | |
|
|
2068 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2069 | if (AvARRAY (av)[i] == SvRV (coro_current)) |
|
|
2070 | return 1; |
|
|
2071 | |
|
|
2072 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2073 | return 1; |
|
|
2074 | } |
|
|
2075 | } |
|
|
2076 | |
|
|
2077 | static void |
|
|
2078 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2079 | { |
|
|
2080 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2081 | |
|
|
2082 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2083 | { |
|
|
2084 | frame->data = (void *)av; |
|
|
2085 | frame->prepare = prepare_nop; |
|
|
2086 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2087 | } |
|
|
2088 | else |
|
|
2089 | { |
|
|
2090 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2091 | |
|
|
2092 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
|
|
2093 | frame->prepare = prepare_schedule; |
|
|
2094 | |
|
|
2095 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2096 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2097 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2098 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2099 | } |
|
|
2100 | |
|
|
2101 | frame->check = slf_check_semaphore_down; |
|
|
2102 | |
|
|
2103 | } |
|
|
2104 | |
|
|
2105 | /*****************************************************************************/ |
|
|
2106 | |
|
|
2107 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2108 | |
|
|
2109 | /* create a closure from XS, returns a code reference */ |
|
|
2110 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2111 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2112 | static SV * |
|
|
2113 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2114 | { |
|
|
2115 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2116 | |
|
|
2117 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2118 | |
|
|
2119 | CvANON_on (cv); |
|
|
2120 | CvISXSUB_on (cv); |
|
|
2121 | CvXSUB (cv) = xsub; |
|
|
2122 | GENSUB_ARG = arg; |
|
|
2123 | |
|
|
2124 | return newRV_noinc ((SV *)cv); |
|
|
2125 | } |
|
|
2126 | |
|
|
2127 | /*****************************************************************************/ |
|
|
2128 | |
2009 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2129 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2010 | |
2130 | |
2011 | PROTOTYPES: DISABLE |
2131 | PROTOTYPES: DISABLE |
2012 | |
2132 | |
2013 | BOOT: |
2133 | BOOT: |
2014 | { |
2134 | { |
2015 | #ifdef USE_ITHREADS |
2135 | #ifdef USE_ITHREADS |
2016 | MUTEX_INIT (&coro_lock); |
|
|
2017 | # if CORO_PTHREAD |
2136 | # if CORO_PTHREAD |
2018 | coro_thx = PERL_GET_CONTEXT; |
2137 | coro_thx = PERL_GET_CONTEXT; |
2019 | # endif |
2138 | # endif |
2020 | #endif |
2139 | #endif |
2021 | BOOT_PAGESIZE; |
2140 | BOOT_PAGESIZE; |
… | |
… | |
2042 | main_top_env = PL_top_env; |
2161 | main_top_env = PL_top_env; |
2043 | |
2162 | |
2044 | while (main_top_env->je_prev) |
2163 | while (main_top_env->je_prev) |
2045 | main_top_env = main_top_env->je_prev; |
2164 | main_top_env = main_top_env->je_prev; |
2046 | |
2165 | |
|
|
2166 | { |
|
|
2167 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2168 | |
|
|
2169 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2170 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2171 | newSVpv ("coro_slf", 0), 0); |
|
|
2172 | |
|
|
2173 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2174 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2175 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2176 | } |
|
|
2177 | |
2047 | coroapi.ver = CORO_API_VERSION; |
2178 | coroapi.ver = CORO_API_VERSION; |
2048 | coroapi.rev = CORO_API_REVISION; |
2179 | coroapi.rev = CORO_API_REVISION; |
|
|
2180 | |
2049 | coroapi.transfer = api_transfer; |
2181 | coroapi.transfer = api_transfer; |
|
|
2182 | |
|
|
2183 | coroapi.sv_state = SvSTATE_; |
2050 | coroapi.execute_slf = api_execute_slf; |
2184 | coroapi.execute_slf = api_execute_slf; |
2051 | coroapi.sv_state = SvSTATE_; |
2185 | coroapi.prepare_nop = prepare_nop; |
|
|
2186 | coroapi.prepare_schedule = prepare_schedule; |
|
|
2187 | coroapi.prepare_cede = prepare_cede; |
|
|
2188 | coroapi.prepare_cede_notself = prepare_cede_notself; |
2052 | |
2189 | |
2053 | { |
2190 | { |
2054 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2191 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2055 | |
2192 | |
2056 | if (!svp) croak ("Time::HiRes is required"); |
2193 | if (!svp) croak ("Time::HiRes is required"); |
… | |
… | |
2092 | RETVAL |
2229 | RETVAL |
2093 | |
2230 | |
2094 | void |
2231 | void |
2095 | _set_stacklevel (...) |
2232 | _set_stacklevel (...) |
2096 | CODE: |
2233 | CODE: |
2097 | { |
2234 | CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); |
2098 | static struct CoroSLF slf = { slf_init_set_stacklevel, slf_prepare_set_stacklevel, slf_check_nop }; |
|
|
2099 | api_execute_slf (aTHX_ cv, &slf, &ST (0), items); |
|
|
2100 | } |
|
|
2101 | |
2235 | |
2102 | void |
2236 | void |
2103 | transfer (...) |
2237 | transfer (...) |
|
|
2238 | PROTOTYPE: $$ |
2104 | CODE: |
2239 | CODE: |
2105 | { |
2240 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
2106 | static struct CoroSLF slf = { slf_init_transfer, slf_prepare_transfer, slf_check_nop }; |
|
|
2107 | api_execute_slf (aTHX_ cv, &slf, &ST (0), items); |
|
|
2108 | } |
|
|
2109 | |
2241 | |
2110 | bool |
2242 | bool |
2111 | _destroy (SV *coro_sv) |
2243 | _destroy (SV *coro_sv) |
2112 | CODE: |
2244 | CODE: |
2113 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2245 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2120 | CODE: |
2252 | CODE: |
2121 | _exit (code); |
2253 | _exit (code); |
2122 | |
2254 | |
2123 | int |
2255 | int |
2124 | cctx_stacksize (int new_stacksize = 0) |
2256 | cctx_stacksize (int new_stacksize = 0) |
|
|
2257 | PROTOTYPE: ;$ |
2125 | CODE: |
2258 | CODE: |
2126 | RETVAL = cctx_stacksize; |
2259 | RETVAL = cctx_stacksize; |
2127 | if (new_stacksize) |
2260 | if (new_stacksize) |
2128 | { |
2261 | { |
2129 | cctx_stacksize = new_stacksize; |
2262 | cctx_stacksize = new_stacksize; |
… | |
… | |
2132 | OUTPUT: |
2265 | OUTPUT: |
2133 | RETVAL |
2266 | RETVAL |
2134 | |
2267 | |
2135 | int |
2268 | int |
2136 | cctx_max_idle (int max_idle = 0) |
2269 | cctx_max_idle (int max_idle = 0) |
|
|
2270 | PROTOTYPE: ;$ |
2137 | CODE: |
2271 | CODE: |
2138 | RETVAL = cctx_max_idle; |
2272 | RETVAL = cctx_max_idle; |
2139 | if (max_idle > 1) |
2273 | if (max_idle > 1) |
2140 | cctx_max_idle = max_idle; |
2274 | cctx_max_idle = max_idle; |
2141 | OUTPUT: |
2275 | OUTPUT: |
2142 | RETVAL |
2276 | RETVAL |
2143 | |
2277 | |
2144 | int |
2278 | int |
2145 | cctx_count () |
2279 | cctx_count () |
|
|
2280 | PROTOTYPE: |
2146 | CODE: |
2281 | CODE: |
2147 | RETVAL = cctx_count; |
2282 | RETVAL = cctx_count; |
2148 | OUTPUT: |
2283 | OUTPUT: |
2149 | RETVAL |
2284 | RETVAL |
2150 | |
2285 | |
2151 | int |
2286 | int |
2152 | cctx_idle () |
2287 | cctx_idle () |
|
|
2288 | PROTOTYPE: |
2153 | CODE: |
2289 | CODE: |
2154 | RETVAL = cctx_idle; |
2290 | RETVAL = cctx_idle; |
2155 | OUTPUT: |
2291 | OUTPUT: |
2156 | RETVAL |
2292 | RETVAL |
2157 | |
2293 | |
2158 | void |
2294 | void |
2159 | list () |
2295 | list () |
|
|
2296 | PROTOTYPE: |
2160 | PPCODE: |
2297 | PPCODE: |
2161 | { |
2298 | { |
2162 | struct coro *coro; |
2299 | struct coro *coro; |
2163 | for (coro = coro_first; coro; coro = coro->next) |
2300 | for (coro = coro_first; coro; coro = coro->next) |
2164 | if (coro->hv) |
2301 | if (coro->hv) |
… | |
… | |
2226 | |
2363 | |
2227 | void |
2364 | void |
2228 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2365 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2229 | PROTOTYPE: $;$ |
2366 | PROTOTYPE: $;$ |
2230 | CODE: |
2367 | CODE: |
|
|
2368 | { |
|
|
2369 | struct coro *current = SvSTATE_current; |
|
|
2370 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2231 | SvREFCNT_dec (self->throw); |
2371 | SvREFCNT_dec (*throwp); |
2232 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2372 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2373 | } |
2233 | |
2374 | |
2234 | void |
2375 | void |
2235 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2376 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
2377 | PROTOTYPE: $;$ |
2236 | C_ARGS: aTHX_ coro, flags |
2378 | C_ARGS: aTHX_ coro, flags |
2237 | |
2379 | |
2238 | SV * |
2380 | SV * |
2239 | has_cctx (Coro::State coro) |
2381 | has_cctx (Coro::State coro) |
2240 | PROTOTYPE: $ |
2382 | PROTOTYPE: $ |
… | |
… | |
2265 | OUTPUT: |
2407 | OUTPUT: |
2266 | RETVAL |
2408 | RETVAL |
2267 | |
2409 | |
2268 | void |
2410 | void |
2269 | force_cctx () |
2411 | force_cctx () |
|
|
2412 | PROTOTYPE: |
2270 | CODE: |
2413 | CODE: |
2271 | struct coro *coro = SvSTATE (coro_current); |
|
|
2272 | coro->cctx->idle_sp = 0; |
2414 | SvSTATE_current->cctx->idle_sp = 0; |
2273 | |
2415 | |
2274 | void |
2416 | void |
2275 | swap_defsv (Coro::State self) |
2417 | swap_defsv (Coro::State self) |
2276 | PROTOTYPE: $ |
2418 | PROTOTYPE: $ |
2277 | ALIAS: |
2419 | ALIAS: |
2278 | swap_defav = 1 |
2420 | swap_defav = 1 |
2279 | CODE: |
2421 | CODE: |
2280 | if (!self->slot) |
2422 | if (!self->slot) |
2281 | croak ("cannot swap state with coroutine that has no saved state"); |
2423 | croak ("cannot swap state with coroutine that has no saved state,"); |
2282 | else |
2424 | else |
2283 | { |
2425 | { |
2284 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2426 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2285 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2427 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2286 | |
2428 | |
… | |
… | |
2330 | } |
2472 | } |
2331 | |
2473 | |
2332 | void |
2474 | void |
2333 | schedule (...) |
2475 | schedule (...) |
2334 | CODE: |
2476 | CODE: |
2335 | { |
2477 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
2336 | static struct CoroSLF slf = { slf_init_nop, prepare_schedule, slf_check_nop }; |
|
|
2337 | api_execute_slf (aTHX_ cv, &slf, &ST (0), items); |
|
|
2338 | } |
|
|
2339 | |
2478 | |
2340 | void |
2479 | void |
2341 | cede (...) |
2480 | cede (...) |
2342 | CODE: |
2481 | CODE: |
2343 | { |
2482 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2344 | static struct CoroSLF slf = { slf_init_nop, prepare_cede, slf_check_nop }; |
|
|
2345 | api_execute_slf (aTHX_ cv, &slf, &ST (0), items); |
|
|
2346 | } |
|
|
2347 | |
2483 | |
2348 | void |
2484 | void |
2349 | cede_notself (...) |
2485 | cede_notself (...) |
2350 | CODE: |
2486 | CODE: |
2351 | { |
2487 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
2352 | static struct CoroSLF slf = { slf_init_nop, prepare_cede_notself, slf_check_nop }; |
|
|
2353 | api_execute_slf (aTHX_ cv, &slf, &ST (0), items); |
|
|
2354 | } |
|
|
2355 | |
2488 | |
2356 | void |
2489 | void |
2357 | _set_current (SV *current) |
2490 | _set_current (SV *current) |
2358 | PROTOTYPE: $ |
2491 | PROTOTYPE: $ |
2359 | CODE: |
2492 | CODE: |
… | |
… | |
2362 | |
2495 | |
2363 | void |
2496 | void |
2364 | _set_readyhook (SV *hook) |
2497 | _set_readyhook (SV *hook) |
2365 | PROTOTYPE: $ |
2498 | PROTOTYPE: $ |
2366 | CODE: |
2499 | CODE: |
2367 | LOCK; |
|
|
2368 | SvREFCNT_dec (coro_readyhook); |
2500 | SvREFCNT_dec (coro_readyhook); |
2369 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2501 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2370 | UNLOCK; |
|
|
2371 | |
2502 | |
2372 | int |
2503 | int |
2373 | prio (Coro::State coro, int newprio = 0) |
2504 | prio (Coro::State coro, int newprio = 0) |
|
|
2505 | PROTOTYPE: $;$ |
2374 | ALIAS: |
2506 | ALIAS: |
2375 | nice = 1 |
2507 | nice = 1 |
2376 | CODE: |
2508 | CODE: |
2377 | { |
2509 | { |
2378 | RETVAL = coro->prio; |
2510 | RETVAL = coro->prio; |
… | |
… | |
2410 | # for async_pool speedup |
2542 | # for async_pool speedup |
2411 | void |
2543 | void |
2412 | _pool_1 (SV *cb) |
2544 | _pool_1 (SV *cb) |
2413 | CODE: |
2545 | CODE: |
2414 | { |
2546 | { |
2415 | struct coro *coro = SvSTATE (coro_current); |
|
|
2416 | HV *hv = (HV *)SvRV (coro_current); |
2547 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2548 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2417 | AV *defav = GvAV (PL_defgv); |
2549 | AV *defav = GvAV (PL_defgv); |
2418 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2550 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2419 | AV *invoke_av; |
2551 | AV *invoke_av; |
2420 | int i, len; |
2552 | int i, len; |
2421 | |
2553 | |
… | |
… | |
2442 | { |
2574 | { |
2443 | av_fill (defav, len - 1); |
2575 | av_fill (defav, len - 1); |
2444 | for (i = 0; i < len; ++i) |
2576 | for (i = 0; i < len; ++i) |
2445 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2577 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2446 | } |
2578 | } |
2447 | |
|
|
2448 | SvREFCNT_dec (invoke); |
|
|
2449 | } |
2579 | } |
2450 | |
2580 | |
2451 | void |
2581 | void |
2452 | _pool_2 (SV *cb) |
2582 | _pool_2 (SV *cb) |
2453 | CODE: |
2583 | CODE: |
2454 | { |
2584 | { |
2455 | struct coro *coro = SvSTATE (coro_current); |
2585 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2586 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2456 | |
2587 | |
2457 | sv_setsv (cb, &PL_sv_undef); |
2588 | sv_setsv (cb, &PL_sv_undef); |
2458 | |
2589 | |
2459 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2590 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2460 | coro->saved_deffh = 0; |
2591 | coro->saved_deffh = 0; |
… | |
… | |
2467 | SvREFCNT_dec (old); |
2598 | SvREFCNT_dec (old); |
2468 | croak ("\3async_pool terminate\2\n"); |
2599 | croak ("\3async_pool terminate\2\n"); |
2469 | } |
2600 | } |
2470 | |
2601 | |
2471 | av_clear (GvAV (PL_defgv)); |
2602 | av_clear (GvAV (PL_defgv)); |
2472 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2603 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2473 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2604 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2474 | |
2605 | |
2475 | coro->prio = 0; |
2606 | coro->prio = 0; |
2476 | |
2607 | |
2477 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2608 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2478 | api_trace (aTHX_ coro_current, 0); |
2609 | api_trace (aTHX_ coro_current, 0); |
2479 | |
2610 | |
2480 | av_push (av_async_pool, newSVsv (coro_current)); |
2611 | av_push (av_async_pool, newSVsv (coro_current)); |
2481 | } |
2612 | } |
2482 | |
2613 | |
2483 | #if 0 |
|
|
2484 | |
|
|
2485 | void |
|
|
2486 | _generator_call (...) |
|
|
2487 | PROTOTYPE: @ |
|
|
2488 | PPCODE: |
|
|
2489 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2490 | xxxx |
|
|
2491 | abort (); |
|
|
2492 | |
|
|
2493 | SV * |
|
|
2494 | gensub (SV *sub, ...) |
|
|
2495 | PROTOTYPE: &;@ |
|
|
2496 | CODE: |
|
|
2497 | { |
|
|
2498 | struct coro *coro; |
|
|
2499 | MAGIC *mg; |
|
|
2500 | CV *xcv; |
|
|
2501 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2502 | int i; |
|
|
2503 | |
|
|
2504 | CvGV (ncv) = CvGV (cv); |
|
|
2505 | CvFILE (ncv) = CvFILE (cv); |
|
|
2506 | |
|
|
2507 | Newz (0, coro, 1, struct coro); |
|
|
2508 | coro->args = newAV (); |
|
|
2509 | coro->flags = CF_NEW; |
|
|
2510 | |
|
|
2511 | av_extend (coro->args, items - 1); |
|
|
2512 | for (i = 1; i < items; i++) |
|
|
2513 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2514 | |
|
|
2515 | CvISXSUB_on (ncv); |
|
|
2516 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2517 | |
|
|
2518 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2519 | |
|
|
2520 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2521 | CvANON_on (ncv); |
|
|
2522 | |
|
|
2523 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2524 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2525 | } |
|
|
2526 | OUTPUT: |
|
|
2527 | RETVAL |
|
|
2528 | |
|
|
2529 | #endif |
|
|
2530 | |
|
|
2531 | |
2614 | |
2532 | MODULE = Coro::State PACKAGE = Coro::AIO |
2615 | MODULE = Coro::State PACKAGE = Coro::AIO |
2533 | |
2616 | |
2534 | void |
2617 | void |
2535 | _get_state (SV *self) |
2618 | _get_state (SV *self) |
|
|
2619 | PROTOTYPE: $ |
2536 | PPCODE: |
2620 | PPCODE: |
2537 | { |
2621 | { |
2538 | AV *defav = GvAV (PL_defgv); |
2622 | AV *defav = GvAV (PL_defgv); |
2539 | AV *av = newAV (); |
2623 | AV *av = newAV (); |
2540 | int i; |
2624 | int i; |
… | |
… | |
2583 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2667 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2584 | |
2668 | |
2585 | BOOT: |
2669 | BOOT: |
2586 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2670 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2587 | |
2671 | |
2588 | SV * |
2672 | void |
2589 | _schedule (...) |
2673 | _schedule (...) |
2590 | PROTOTYPE: @ |
|
|
2591 | CODE: |
2674 | CODE: |
2592 | { |
2675 | { |
2593 | static int incede; |
2676 | static int incede; |
2594 | |
2677 | |
2595 | api_cede_notself (aTHX); |
2678 | api_cede_notself (aTHX); |
… | |
… | |
2614 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2697 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2615 | |
2698 | |
2616 | BOOT: |
2699 | BOOT: |
2617 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2700 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2618 | |
2701 | |
|
|
2702 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
|
|
2703 | |
|
|
2704 | SV * |
|
|
2705 | new (SV *klass, SV *count_ = 0) |
|
|
2706 | CODE: |
|
|
2707 | { |
|
|
2708 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2709 | AV *av = newAV (); |
|
|
2710 | av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); |
|
|
2711 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
|
|
2712 | } |
|
|
2713 | OUTPUT: |
|
|
2714 | RETVAL |
|
|
2715 | |
|
|
2716 | SV * |
|
|
2717 | count (SV *self) |
|
|
2718 | CODE: |
|
|
2719 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
|
|
2720 | OUTPUT: |
|
|
2721 | RETVAL |
|
|
2722 | |
|
|
2723 | void |
|
|
2724 | up (SV *self, int adjust = 1) |
|
|
2725 | ALIAS: |
|
|
2726 | adjust = 1 |
|
|
2727 | CODE: |
|
|
2728 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
|
|
2729 | |
|
|
2730 | void |
|
|
2731 | down (SV *self) |
|
|
2732 | CODE: |
|
|
2733 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
2734 | |
|
|
2735 | void |
|
|
2736 | try (SV *self) |
|
|
2737 | PPCODE: |
|
|
2738 | { |
|
|
2739 | AV *av = (AV *)SvRV (self); |
|
|
2740 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2741 | IV count = SvIVX (count_sv); |
|
|
2742 | |
|
|
2743 | if (count > 0) |
|
|
2744 | { |
|
|
2745 | --count; |
|
|
2746 | SvIVX (count_sv) = count; |
|
|
2747 | XSRETURN_YES; |
|
|
2748 | } |
|
|
2749 | else |
|
|
2750 | XSRETURN_NO; |
|
|
2751 | } |
|
|
2752 | |
|
|
2753 | void |
|
|
2754 | waiters (SV *self) |
|
|
2755 | CODE: |
|
|
2756 | { |
|
|
2757 | AV *av = (AV *)SvRV (self); |
|
|
2758 | |
|
|
2759 | if (GIMME_V == G_SCALAR) |
|
|
2760 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
|
|
2761 | else |
|
|
2762 | { |
|
|
2763 | int i; |
|
|
2764 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
|
|
2765 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2766 | PUSHs (newSVsv (AvARRAY (av)[i])); |
|
|
2767 | } |
|
|
2768 | } |
|
|
2769 | |