… | |
… | |
137 | |
137 | |
138 | #define IN_DESTRUCT PL_dirty |
138 | #define IN_DESTRUCT PL_dirty |
139 | |
139 | |
140 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
141 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
143 | # define INLINE static inline |
143 | # define INLINE static inline |
144 | #else |
144 | #else |
145 | # define attribute(x) |
145 | # define attribute(x) |
146 | # define expect(expr,value) (expr) |
146 | # define expect(expr,value) (expr) |
147 | # define INLINE static |
147 | # define INLINE static |
… | |
… | |
156 | #define GCoroAPI (&coroapi) /* very sneaky */ |
156 | #define GCoroAPI (&coroapi) /* very sneaky */ |
157 | |
157 | |
158 | #ifdef USE_ITHREADS |
158 | #ifdef USE_ITHREADS |
159 | # if CORO_PTHREAD |
159 | # if CORO_PTHREAD |
160 | static void *coro_thx; |
160 | static void *coro_thx; |
|
|
161 | # endif |
|
|
162 | #endif |
|
|
163 | |
|
|
164 | #ifdef __linux |
|
|
165 | # include <time.h> /* for timespec */ |
|
|
166 | # include <syscall.h> /* for SYS_* */ |
|
|
167 | # ifdef SYS_clock_gettime |
|
|
168 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
169 | # define CORO_CLOCK_MONOTONIC 1 |
|
|
170 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
161 | # endif |
171 | # endif |
162 | #endif |
172 | #endif |
163 | |
173 | |
164 | static double (*nvtime)(); /* so why doesn't it take void? */ |
174 | static double (*nvtime)(); /* so why doesn't it take void? */ |
165 | |
175 | |
… | |
… | |
195 | static CV *cv_coro_state_new; |
205 | static CV *cv_coro_state_new; |
196 | |
206 | |
197 | /* Coro::AnyEvent */ |
207 | /* Coro::AnyEvent */ |
198 | static SV *sv_activity; |
208 | static SV *sv_activity; |
199 | |
209 | |
|
|
210 | /* enable processtime/realtime profiling */ |
|
|
211 | static char profile_times; |
|
|
212 | typedef U32 coro_ts[2]; |
|
|
213 | |
200 | static struct coro_cctx *cctx_first; |
214 | static struct coro_cctx *cctx_first; |
201 | static int cctx_count, cctx_idle; |
215 | static int cctx_count, cctx_idle; |
202 | |
216 | |
203 | enum { |
217 | enum { |
204 | CC_MAPPED = 0x01, |
218 | CC_MAPPED = 0x01, |
… | |
… | |
238 | enum { |
252 | enum { |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
253 | CF_RUNNING = 0x0001, /* coroutine is running */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
254 | CF_READY = 0x0002, /* coroutine is ready */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
255 | CF_NEW = 0x0004, /* has never been switched to */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
256 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
257 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
243 | }; |
258 | }; |
244 | |
259 | |
245 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
260 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
246 | typedef struct |
261 | typedef struct |
247 | { |
262 | { |
… | |
… | |
260 | /* this is a structure representing a perl-level coroutine */ |
275 | /* this is a structure representing a perl-level coroutine */ |
261 | struct coro { |
276 | struct coro { |
262 | /* the C coroutine allocated to this perl coroutine, if any */ |
277 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | coro_cctx *cctx; |
278 | coro_cctx *cctx; |
264 | |
279 | |
|
|
280 | /* ready queue */ |
|
|
281 | struct coro *next_ready; |
|
|
282 | |
265 | /* state data */ |
283 | /* state data */ |
266 | struct CoroSLF slf_frame; /* saved slf frame */ |
284 | struct CoroSLF slf_frame; /* saved slf frame */ |
267 | AV *mainstack; |
285 | AV *mainstack; |
268 | perl_slots *slot; /* basically the saved sp */ |
286 | perl_slots *slot; /* basically the saved sp */ |
269 | |
287 | |
… | |
… | |
285 | /* async_pool */ |
303 | /* async_pool */ |
286 | SV *saved_deffh; |
304 | SV *saved_deffh; |
287 | SV *invoke_cb; |
305 | SV *invoke_cb; |
288 | AV *invoke_av; |
306 | AV *invoke_av; |
289 | |
307 | |
|
|
308 | /* on_enter/on_leave */ |
|
|
309 | AV *on_enter; |
|
|
310 | AV *on_leave; |
|
|
311 | |
290 | /* linked list */ |
312 | /* linked list */ |
291 | struct coro *next, *prev; |
313 | struct coro *next, *prev; |
292 | }; |
314 | }; |
293 | |
315 | |
294 | typedef struct coro *Coro__State; |
316 | typedef struct coro *Coro__State; |
… | |
… | |
299 | /* the mainr easonw e don't support windows process emulation */ |
321 | /* the mainr easonw e don't support windows process emulation */ |
300 | static struct CoroSLF slf_frame; /* the current slf frame */ |
322 | static struct CoroSLF slf_frame; /* the current slf frame */ |
301 | |
323 | |
302 | /** Coro ********************************************************************/ |
324 | /** Coro ********************************************************************/ |
303 | |
325 | |
304 | #define PRIO_MAX 3 |
326 | #define CORO_PRIO_MAX 3 |
305 | #define PRIO_HIGH 1 |
327 | #define CORO_PRIO_HIGH 1 |
306 | #define PRIO_NORMAL 0 |
328 | #define CORO_PRIO_NORMAL 0 |
307 | #define PRIO_LOW -1 |
329 | #define CORO_PRIO_LOW -1 |
308 | #define PRIO_IDLE -3 |
330 | #define CORO_PRIO_IDLE -3 |
309 | #define PRIO_MIN -4 |
331 | #define CORO_PRIO_MIN -4 |
310 | |
332 | |
311 | /* for Coro.pm */ |
333 | /* for Coro.pm */ |
312 | static SV *coro_current; |
334 | static SV *coro_current; |
313 | static SV *coro_readyhook; |
335 | static SV *coro_readyhook; |
314 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
336 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
315 | static CV *cv_coro_run, *cv_coro_terminate; |
337 | static CV *cv_coro_run, *cv_coro_terminate; |
316 | static struct coro *coro_first; |
338 | static struct coro *coro_first; |
317 | #define coro_nready coroapi.nready |
339 | #define coro_nready coroapi.nready |
318 | |
340 | |
319 | /** lowlevel stuff **********************************************************/ |
341 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
352 | INLINE CV * |
374 | INLINE CV * |
353 | coro_sv_2cv (pTHX_ SV *sv) |
375 | coro_sv_2cv (pTHX_ SV *sv) |
354 | { |
376 | { |
355 | HV *st; |
377 | HV *st; |
356 | GV *gvp; |
378 | GV *gvp; |
357 | return sv_2cv (sv, &st, &gvp, 0); |
379 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
380 | |
|
|
381 | if (!cv) |
|
|
382 | croak ("code reference expected"); |
|
|
383 | |
|
|
384 | return cv; |
358 | } |
385 | } |
359 | |
386 | |
360 | /*****************************************************************************/ |
387 | /*****************************************************************************/ |
361 | /* magic glue */ |
388 | /* magic glue */ |
362 | |
389 | |
… | |
… | |
462 | static int |
489 | static int |
463 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
490 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
464 | { |
491 | { |
465 | AV *padlist; |
492 | AV *padlist; |
466 | AV *av = (AV *)mg->mg_obj; |
493 | AV *av = (AV *)mg->mg_obj; |
|
|
494 | |
|
|
495 | /* perl manages to free our internal AV and _then_ call us */ |
|
|
496 | if (IN_DESTRUCT) |
|
|
497 | return; |
467 | |
498 | |
468 | /* casting is fun. */ |
499 | /* casting is fun. */ |
469 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
500 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
470 | free_padlist (aTHX_ padlist); |
501 | free_padlist (aTHX_ padlist); |
471 | |
502 | |
… | |
… | |
521 | } |
552 | } |
522 | |
553 | |
523 | /** load & save, init *******************************************************/ |
554 | /** load & save, init *******************************************************/ |
524 | |
555 | |
525 | static void |
556 | static void |
|
|
557 | on_enterleave_call (pTHX_ SV *cb); |
|
|
558 | |
|
|
559 | static void |
526 | load_perl (pTHX_ Coro__State c) |
560 | load_perl (pTHX_ Coro__State c) |
527 | { |
561 | { |
528 | perl_slots *slot = c->slot; |
562 | perl_slots *slot = c->slot; |
529 | c->slot = 0; |
563 | c->slot = 0; |
530 | |
564 | |
… | |
… | |
556 | PUTBACK; |
590 | PUTBACK; |
557 | } |
591 | } |
558 | |
592 | |
559 | slf_frame = c->slf_frame; |
593 | slf_frame = c->slf_frame; |
560 | CORO_THROW = c->except; |
594 | CORO_THROW = c->except; |
|
|
595 | |
|
|
596 | if (expect_false (c->on_enter)) |
|
|
597 | { |
|
|
598 | int i; |
|
|
599 | |
|
|
600 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
601 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
602 | } |
561 | } |
603 | } |
562 | |
604 | |
563 | static void |
605 | static void |
564 | save_perl (pTHX_ Coro__State c) |
606 | save_perl (pTHX_ Coro__State c) |
565 | { |
607 | { |
|
|
608 | if (expect_false (c->on_leave)) |
|
|
609 | { |
|
|
610 | int i; |
|
|
611 | |
|
|
612 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
613 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
614 | } |
|
|
615 | |
566 | c->except = CORO_THROW; |
616 | c->except = CORO_THROW; |
567 | c->slf_frame = slf_frame; |
617 | c->slf_frame = slf_frame; |
568 | |
618 | |
569 | { |
619 | { |
570 | dSP; |
620 | dSP; |
… | |
… | |
583 | { |
633 | { |
584 | while (expect_true (cxix >= 0)) |
634 | while (expect_true (cxix >= 0)) |
585 | { |
635 | { |
586 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
636 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
587 | |
637 | |
588 | if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) |
638 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
589 | { |
639 | { |
590 | CV *cv = cx->blk_sub.cv; |
640 | CV *cv = cx->blk_sub.cv; |
591 | |
641 | |
592 | if (expect_true (CvDEPTH (cv))) |
642 | if (expect_true (CvDEPTH (cv))) |
593 | { |
643 | { |
… | |
… | |
769 | #endif |
819 | #endif |
770 | |
820 | |
771 | /* |
821 | /* |
772 | * This overrides the default magic get method of %SIG elements. |
822 | * This overrides the default magic get method of %SIG elements. |
773 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
823 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
774 | * and instead of tryign to save and restore the hash elements, we just provide |
824 | * and instead of trying to save and restore the hash elements, we just provide |
775 | * readback here. |
825 | * readback here. |
776 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
777 | * not expecting this to actually change the hook. This is a potential problem |
|
|
778 | * when a schedule happens then, but we ignore this. |
|
|
779 | */ |
826 | */ |
780 | static int |
827 | static int |
781 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
828 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
782 | { |
829 | { |
783 | const char *s = MgPV_nolen_const (mg); |
830 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
836 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
883 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
837 | |
884 | |
838 | if (svp) |
885 | if (svp) |
839 | { |
886 | { |
840 | SV *old = *svp; |
887 | SV *old = *svp; |
841 | *svp = newSVsv (sv); |
888 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
842 | SvREFCNT_dec (old); |
889 | SvREFCNT_dec (old); |
843 | return 0; |
890 | return 0; |
844 | } |
891 | } |
845 | } |
892 | } |
846 | |
893 | |
… | |
… | |
938 | /* copy throw, in case it was set before coro_setup */ |
985 | /* copy throw, in case it was set before coro_setup */ |
939 | CORO_THROW = coro->except; |
986 | CORO_THROW = coro->except; |
940 | } |
987 | } |
941 | |
988 | |
942 | static void |
989 | static void |
943 | coro_destruct_perl (pTHX_ struct coro *coro) |
990 | coro_unwind_stacks (pTHX) |
944 | { |
991 | { |
945 | if (!IN_DESTRUCT) |
992 | if (!IN_DESTRUCT) |
946 | { |
993 | { |
947 | /* restore all saved variables and stuff */ |
994 | /* restore all saved variables and stuff */ |
948 | LEAVE_SCOPE (0); |
995 | LEAVE_SCOPE (0); |
… | |
… | |
956 | POPSTACK_TO (PL_mainstack); |
1003 | POPSTACK_TO (PL_mainstack); |
957 | |
1004 | |
958 | /* unwind main stack */ |
1005 | /* unwind main stack */ |
959 | dounwind (-1); |
1006 | dounwind (-1); |
960 | } |
1007 | } |
|
|
1008 | } |
961 | |
1009 | |
962 | SvREFCNT_dec (GvSV (PL_defgv)); |
1010 | static void |
963 | SvREFCNT_dec (GvAV (PL_defgv)); |
1011 | coro_destruct_perl (pTHX_ struct coro *coro) |
964 | SvREFCNT_dec (GvSV (PL_errgv)); |
1012 | { |
965 | SvREFCNT_dec (PL_defoutgv); |
1013 | SV *svf [9]; |
966 | SvREFCNT_dec (PL_rs); |
|
|
967 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
968 | SvREFCNT_dec (GvHV (PL_hintgv)); |
|
|
969 | |
1014 | |
970 | SvREFCNT_dec (PL_diehook); |
|
|
971 | SvREFCNT_dec (PL_warnhook); |
|
|
972 | |
1015 | { |
|
|
1016 | struct coro *current = SvSTATE_current; |
|
|
1017 | |
|
|
1018 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
|
|
1019 | |
|
|
1020 | save_perl (aTHX_ current); |
|
|
1021 | load_perl (aTHX_ coro); |
|
|
1022 | |
|
|
1023 | coro_unwind_stacks (aTHX); |
|
|
1024 | coro_destruct_stacks (aTHX); |
|
|
1025 | |
|
|
1026 | // now save some sv's to be free'd later |
|
|
1027 | svf [0] = GvSV (PL_defgv); |
|
|
1028 | svf [1] = (SV *)GvAV (PL_defgv); |
|
|
1029 | svf [2] = GvSV (PL_errgv); |
|
|
1030 | svf [3] = (SV *)PL_defoutgv; |
|
|
1031 | svf [4] = PL_rs; |
|
|
1032 | svf [5] = GvSV (irsgv); |
|
|
1033 | svf [6] = (SV *)GvHV (PL_hintgv); |
|
|
1034 | svf [7] = PL_diehook; |
|
|
1035 | svf [8] = PL_warnhook; |
|
|
1036 | assert (9 == sizeof (svf) / sizeof (*svf)); |
|
|
1037 | |
|
|
1038 | load_perl (aTHX_ current); |
|
|
1039 | } |
|
|
1040 | |
|
|
1041 | { |
|
|
1042 | int i; |
|
|
1043 | |
|
|
1044 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
|
|
1045 | SvREFCNT_dec (svf [i]); |
|
|
1046 | |
973 | SvREFCNT_dec (coro->saved_deffh); |
1047 | SvREFCNT_dec (coro->saved_deffh); |
974 | SvREFCNT_dec (coro->rouse_cb); |
1048 | SvREFCNT_dec (coro->rouse_cb); |
975 | SvREFCNT_dec (coro->invoke_cb); |
1049 | SvREFCNT_dec (coro->invoke_cb); |
976 | SvREFCNT_dec (coro->invoke_av); |
1050 | SvREFCNT_dec (coro->invoke_av); |
977 | |
1051 | } |
978 | coro_destruct_stacks (aTHX); |
|
|
979 | } |
1052 | } |
980 | |
1053 | |
981 | INLINE void |
1054 | INLINE void |
982 | free_coro_mortal (pTHX) |
1055 | free_coro_mortal (pTHX) |
983 | { |
1056 | { |
… | |
… | |
1277 | cctx_destroy (coro_cctx *cctx) |
1350 | cctx_destroy (coro_cctx *cctx) |
1278 | { |
1351 | { |
1279 | if (!cctx) |
1352 | if (!cctx) |
1280 | return; |
1353 | return; |
1281 | |
1354 | |
1282 | assert (cctx != cctx_current);//D temporary |
1355 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1283 | |
1356 | |
1284 | --cctx_count; |
1357 | --cctx_count; |
1285 | coro_destroy (&cctx->cctx); |
1358 | coro_destroy (&cctx->cctx); |
1286 | |
1359 | |
1287 | /* coro_transfer creates new, empty cctx's */ |
1360 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1351 | /* TODO: throwing up here is considered harmful */ |
1424 | /* TODO: throwing up here is considered harmful */ |
1352 | |
1425 | |
1353 | if (expect_true (prev != next)) |
1426 | if (expect_true (prev != next)) |
1354 | { |
1427 | { |
1355 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1428 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1356 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1429 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1357 | |
1430 | |
1358 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1359 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
|
|
1360 | |
|
|
1361 | if (expect_false (next->flags & CF_DESTROYED)) |
1431 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1362 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1432 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1363 | |
1433 | |
1364 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1434 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1365 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1435 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1366 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1436 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1367 | #endif |
1437 | #endif |
… | |
… | |
1405 | coro_setup (aTHX_ next); |
1475 | coro_setup (aTHX_ next); |
1406 | } |
1476 | } |
1407 | else |
1477 | else |
1408 | load_perl (aTHX_ next); |
1478 | load_perl (aTHX_ next); |
1409 | |
1479 | |
1410 | assert (!prev->cctx);//D temporary |
|
|
1411 | |
|
|
1412 | /* possibly untie and reuse the cctx */ |
1480 | /* possibly untie and reuse the cctx */ |
1413 | if (expect_true ( |
1481 | if (expect_true ( |
1414 | cctx_current->idle_sp == STACKLEVEL |
1482 | cctx_current->idle_sp == STACKLEVEL |
1415 | && !(cctx_current->flags & CC_TRACE) |
1483 | && !(cctx_current->flags & CC_TRACE) |
1416 | && !force_cctx |
1484 | && !force_cctx |
… | |
… | |
1457 | coro_state_destroy (pTHX_ struct coro *coro) |
1525 | coro_state_destroy (pTHX_ struct coro *coro) |
1458 | { |
1526 | { |
1459 | if (coro->flags & CF_DESTROYED) |
1527 | if (coro->flags & CF_DESTROYED) |
1460 | return 0; |
1528 | return 0; |
1461 | |
1529 | |
1462 | if (coro->on_destroy) |
1530 | if (coro->on_destroy && !PL_dirty) |
1463 | coro->on_destroy (aTHX_ coro); |
1531 | coro->on_destroy (aTHX_ coro); |
1464 | |
1532 | |
1465 | coro->flags |= CF_DESTROYED; |
1533 | coro->flags |= CF_DESTROYED; |
1466 | |
1534 | |
1467 | if (coro->flags & CF_READY) |
1535 | if (coro->flags & CF_READY) |
… | |
… | |
1473 | else |
1541 | else |
1474 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1542 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1475 | |
1543 | |
1476 | if (coro->mainstack |
1544 | if (coro->mainstack |
1477 | && coro->mainstack != main_mainstack |
1545 | && coro->mainstack != main_mainstack |
|
|
1546 | && coro->slot |
1478 | && !PL_dirty) |
1547 | && !PL_dirty) |
1479 | { |
|
|
1480 | struct coro temp; |
|
|
1481 | |
|
|
1482 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
|
|
1483 | |
|
|
1484 | save_perl (aTHX_ &temp); |
|
|
1485 | load_perl (aTHX_ coro); |
|
|
1486 | |
|
|
1487 | coro_destruct_perl (aTHX_ coro); |
1548 | coro_destruct_perl (aTHX_ coro); |
1488 | |
|
|
1489 | load_perl (aTHX_ &temp); |
|
|
1490 | |
|
|
1491 | coro->slot = 0; |
|
|
1492 | } |
|
|
1493 | |
1549 | |
1494 | cctx_destroy (coro->cctx); |
1550 | cctx_destroy (coro->cctx); |
1495 | SvREFCNT_dec (coro->startcv); |
1551 | SvREFCNT_dec (coro->startcv); |
1496 | SvREFCNT_dec (coro->args); |
1552 | SvREFCNT_dec (coro->args); |
1497 | SvREFCNT_dec (CORO_THROW); |
1553 | SvREFCNT_dec (CORO_THROW); |
… | |
… | |
1584 | /** Coro ********************************************************************/ |
1640 | /** Coro ********************************************************************/ |
1585 | |
1641 | |
1586 | INLINE void |
1642 | INLINE void |
1587 | coro_enq (pTHX_ struct coro *coro) |
1643 | coro_enq (pTHX_ struct coro *coro) |
1588 | { |
1644 | { |
1589 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1645 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1590 | } |
|
|
1591 | |
1646 | |
1592 | INLINE SV * |
1647 | SvREFCNT_inc_NN (coro->hv); |
|
|
1648 | |
|
|
1649 | coro->next_ready = 0; |
|
|
1650 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
|
|
1651 | ready [1] = coro; |
|
|
1652 | } |
|
|
1653 | |
|
|
1654 | INLINE struct coro * |
1593 | coro_deq (pTHX) |
1655 | coro_deq (pTHX) |
1594 | { |
1656 | { |
1595 | int prio; |
1657 | int prio; |
1596 | |
1658 | |
1597 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1659 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1598 | if (AvFILLp (coro_ready [prio]) >= 0) |
1660 | { |
1599 | return av_shift (coro_ready [prio]); |
1661 | struct coro **ready = coro_ready [prio]; |
|
|
1662 | |
|
|
1663 | if (ready [0]) |
|
|
1664 | { |
|
|
1665 | struct coro *coro = ready [0]; |
|
|
1666 | ready [0] = coro->next_ready; |
|
|
1667 | return coro; |
|
|
1668 | } |
|
|
1669 | } |
1600 | |
1670 | |
1601 | return 0; |
1671 | return 0; |
1602 | } |
1672 | } |
1603 | |
1673 | |
1604 | static int |
1674 | static int |
… | |
… | |
1668 | static void |
1738 | static void |
1669 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1739 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1670 | { |
1740 | { |
1671 | for (;;) |
1741 | for (;;) |
1672 | { |
1742 | { |
1673 | SV *next_sv = coro_deq (aTHX); |
1743 | struct coro *next = coro_deq (aTHX); |
1674 | |
1744 | |
1675 | if (expect_true (next_sv)) |
1745 | if (expect_true (next)) |
1676 | { |
1746 | { |
1677 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1678 | |
|
|
1679 | /* cannot transfer to destroyed coros, skip and look for next */ |
1747 | /* cannot transfer to destroyed coros, skip and look for next */ |
1680 | if (expect_false (next->flags & CF_DESTROYED)) |
1748 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1681 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1749 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1682 | else |
1750 | else |
1683 | { |
1751 | { |
1684 | next->flags &= ~CF_READY; |
1752 | next->flags &= ~CF_READY; |
1685 | --coro_nready; |
1753 | --coro_nready; |
1686 | |
1754 | |
… | |
… | |
1864 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1932 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1865 | api_ready (aTHX_ sv_manager); |
1933 | api_ready (aTHX_ sv_manager); |
1866 | |
1934 | |
1867 | frame->prepare = prepare_schedule; |
1935 | frame->prepare = prepare_schedule; |
1868 | frame->check = slf_check_repeat; |
1936 | frame->check = slf_check_repeat; |
|
|
1937 | |
|
|
1938 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1939 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1940 | /*coro_unwind_stacks (aTHX);*/ |
1869 | } |
1941 | } |
1870 | |
1942 | |
1871 | /*****************************************************************************/ |
1943 | /*****************************************************************************/ |
1872 | /* async pool handler */ |
1944 | /* async pool handler */ |
1873 | |
1945 | |
… | |
… | |
1956 | SV *data = (SV *)GENSUB_ARG; |
2028 | SV *data = (SV *)GENSUB_ARG; |
1957 | |
2029 | |
1958 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2030 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1959 | { |
2031 | { |
1960 | /* first call, set args */ |
2032 | /* first call, set args */ |
|
|
2033 | SV *coro = SvRV (data); |
1961 | AV *av = newAV (); |
2034 | AV *av = newAV (); |
1962 | SV *coro = SvRV (data); |
|
|
1963 | |
2035 | |
1964 | SvRV_set (data, (SV *)av); |
2036 | SvRV_set (data, (SV *)av); |
1965 | api_ready (aTHX_ coro); |
|
|
1966 | SvREFCNT_dec (coro); |
|
|
1967 | |
2037 | |
1968 | /* better take a full copy of the arguments */ |
2038 | /* better take a full copy of the arguments */ |
1969 | while (items--) |
2039 | while (items--) |
1970 | av_store (av, items, newSVsv (ST (items))); |
2040 | av_store (av, items, newSVsv (ST (items))); |
|
|
2041 | |
|
|
2042 | api_ready (aTHX_ coro); |
|
|
2043 | SvREFCNT_dec (coro); |
1971 | } |
2044 | } |
1972 | |
2045 | |
1973 | XSRETURN_EMPTY; |
2046 | XSRETURN_EMPTY; |
1974 | } |
2047 | } |
1975 | |
2048 | |
… | |
… | |
1992 | |
2065 | |
1993 | EXTEND (SP, AvFILLp (av) + 1); |
2066 | EXTEND (SP, AvFILLp (av) + 1); |
1994 | for (i = 0; i <= AvFILLp (av); ++i) |
2067 | for (i = 0; i <= AvFILLp (av); ++i) |
1995 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2068 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
1996 | |
2069 | |
1997 | /* we have stolen the elements, so ste length to zero and free */ |
2070 | /* we have stolen the elements, so set length to zero and free */ |
1998 | AvFILLp (av) = -1; |
2071 | AvFILLp (av) = -1; |
1999 | av_undef (av); |
2072 | av_undef (av); |
2000 | |
2073 | |
2001 | PUTBACK; |
2074 | PUTBACK; |
2002 | } |
2075 | } |
… | |
… | |
2292 | |
2365 | |
2293 | PL_op->op_ppaddr = pp_slf; |
2366 | PL_op->op_ppaddr = pp_slf; |
2294 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2367 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2295 | |
2368 | |
2296 | PL_op = (OP *)&slf_restore; |
2369 | PL_op = (OP *)&slf_restore; |
|
|
2370 | } |
|
|
2371 | |
|
|
2372 | /*****************************************************************************/ |
|
|
2373 | /* dynamic wind */ |
|
|
2374 | |
|
|
2375 | static void |
|
|
2376 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2377 | { |
|
|
2378 | dSP; |
|
|
2379 | |
|
|
2380 | PUSHSTACK; |
|
|
2381 | |
|
|
2382 | PUSHMARK (SP); |
|
|
2383 | PUTBACK; |
|
|
2384 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2385 | SPAGAIN; |
|
|
2386 | |
|
|
2387 | POPSTACK; |
|
|
2388 | } |
|
|
2389 | |
|
|
2390 | static SV * |
|
|
2391 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2392 | { |
|
|
2393 | AV *av = *avp; |
|
|
2394 | SV *res = av_pop (av); |
|
|
2395 | |
|
|
2396 | if (AvFILLp (av) < 0) |
|
|
2397 | { |
|
|
2398 | *avp = 0; |
|
|
2399 | SvREFCNT_dec (av); |
|
|
2400 | } |
|
|
2401 | |
|
|
2402 | return res; |
|
|
2403 | } |
|
|
2404 | |
|
|
2405 | static void |
|
|
2406 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2407 | { |
|
|
2408 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2409 | SvREFCNT_dec (cb); |
|
|
2410 | } |
|
|
2411 | |
|
|
2412 | static void |
|
|
2413 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2414 | { |
|
|
2415 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2416 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2297 | } |
2417 | } |
2298 | |
2418 | |
2299 | /*****************************************************************************/ |
2419 | /*****************************************************************************/ |
2300 | /* PerlIO::cede */ |
2420 | /* PerlIO::cede */ |
2301 | |
2421 | |
… | |
… | |
2375 | /* Coro::Semaphore & Coro::Signal */ |
2495 | /* Coro::Semaphore & Coro::Signal */ |
2376 | |
2496 | |
2377 | static SV * |
2497 | static SV * |
2378 | coro_waitarray_new (pTHX_ int count) |
2498 | coro_waitarray_new (pTHX_ int count) |
2379 | { |
2499 | { |
2380 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2500 | /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2381 | AV *av = newAV (); |
2501 | AV *av = newAV (); |
2382 | SV **ary; |
2502 | SV **ary; |
2383 | |
2503 | |
2384 | /* unfortunately, building manually saves memory */ |
2504 | /* unfortunately, building manually saves memory */ |
2385 | Newx (ary, 2, SV *); |
2505 | Newx (ary, 2, SV *); |
… | |
… | |
2529 | { |
2649 | { |
2530 | /* callback form */ |
2650 | /* callback form */ |
2531 | AV *av = (AV *)SvRV (arg [0]); |
2651 | AV *av = (AV *)SvRV (arg [0]); |
2532 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2652 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2533 | |
2653 | |
2534 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2654 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2535 | |
2655 | |
2536 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2656 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2537 | coro_semaphore_adjust (aTHX_ av, 0); |
2657 | coro_semaphore_adjust (aTHX_ av, 0); |
2538 | |
2658 | |
2539 | frame->prepare = prepare_nop; |
2659 | frame->prepare = prepare_nop; |
… | |
… | |
2563 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2683 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2564 | AvARRAY (av)[1] = cb; |
2684 | AvARRAY (av)[1] = cb; |
2565 | |
2685 | |
2566 | cb = av_shift (av); |
2686 | cb = av_shift (av); |
2567 | |
2687 | |
|
|
2688 | if (SvTYPE (cb) == SVt_PVCV) |
|
|
2689 | { |
|
|
2690 | dSP; |
|
|
2691 | PUSHMARK (SP); |
|
|
2692 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2693 | PUTBACK; |
|
|
2694 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2695 | } |
|
|
2696 | else |
|
|
2697 | { |
2568 | api_ready (aTHX_ cb); |
2698 | api_ready (aTHX_ cb); |
2569 | sv_setiv (cb, 0); /* signal waiter */ |
2699 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2700 | } |
|
|
2701 | |
2570 | SvREFCNT_dec (cb); |
2702 | SvREFCNT_dec (cb); |
2571 | |
2703 | |
2572 | --count; |
2704 | --count; |
2573 | } |
2705 | } |
2574 | } |
2706 | } |
… | |
… | |
2583 | static void |
2715 | static void |
2584 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2716 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2585 | { |
2717 | { |
2586 | AV *av = (AV *)SvRV (arg [0]); |
2718 | AV *av = (AV *)SvRV (arg [0]); |
2587 | |
2719 | |
|
|
2720 | if (items >= 2) |
|
|
2721 | { |
|
|
2722 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2723 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
|
|
2724 | |
2588 | if (SvIVX (AvARRAY (av)[0])) |
2725 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2726 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
|
|
2727 | |
|
|
2728 | frame->prepare = prepare_nop; |
|
|
2729 | frame->check = slf_check_nop; |
|
|
2730 | } |
|
|
2731 | else if (SvIVX (AvARRAY (av)[0])) |
2589 | { |
2732 | { |
2590 | SvIVX (AvARRAY (av)[0]) = 0; |
2733 | SvIVX (AvARRAY (av)[0]) = 0; |
2591 | frame->prepare = prepare_nop; |
2734 | frame->prepare = prepare_nop; |
2592 | frame->check = slf_check_nop; |
2735 | frame->check = slf_check_nop; |
2593 | } |
2736 | } |
2594 | else |
2737 | else |
2595 | { |
2738 | { |
2596 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
2739 | SV *waiter = newSVsv (coro_current); /* owned by signal av */ |
2597 | |
2740 | |
2598 | av_push (av, waiter); |
2741 | av_push (av, waiter); |
2599 | |
2742 | |
2600 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2743 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2601 | frame->prepare = prepare_schedule; |
2744 | frame->prepare = prepare_schedule; |
… | |
… | |
2842 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2985 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2843 | |
2986 | |
2844 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
2987 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
2845 | } |
2988 | } |
2846 | |
2989 | |
2847 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2990 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2848 | } |
2991 | } |
2849 | |
2992 | |
2850 | SV * |
2993 | SV * |
2851 | new (char *klass, ...) |
2994 | new (char *klass, ...) |
2852 | ALIAS: |
2995 | ALIAS: |
… | |
… | |
2999 | eval = 1 |
3142 | eval = 1 |
3000 | CODE: |
3143 | CODE: |
3001 | { |
3144 | { |
3002 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3145 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3003 | { |
3146 | { |
3004 | struct coro temp; |
3147 | struct coro *current = SvSTATE_current; |
3005 | |
3148 | |
3006 | if (!(coro->flags & CF_RUNNING)) |
3149 | if (current != coro) |
3007 | { |
3150 | { |
3008 | PUTBACK; |
3151 | PUTBACK; |
3009 | save_perl (aTHX_ &temp); |
3152 | save_perl (aTHX_ current); |
3010 | load_perl (aTHX_ coro); |
3153 | load_perl (aTHX_ coro); |
|
|
3154 | SPAGAIN; |
3011 | } |
3155 | } |
3012 | |
3156 | |
3013 | { |
|
|
3014 | dSP; |
|
|
3015 | ENTER; |
|
|
3016 | SAVETMPS; |
|
|
3017 | PUTBACK; |
|
|
3018 | PUSHSTACK; |
3157 | PUSHSTACK; |
|
|
3158 | |
3019 | PUSHMARK (SP); |
3159 | PUSHMARK (SP); |
|
|
3160 | PUTBACK; |
3020 | |
3161 | |
3021 | if (ix) |
3162 | if (ix) |
3022 | eval_sv (coderef, 0); |
3163 | eval_sv (coderef, 0); |
3023 | else |
3164 | else |
3024 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3165 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3025 | |
3166 | |
3026 | POPSTACK; |
3167 | POPSTACK; |
3027 | SPAGAIN; |
3168 | SPAGAIN; |
3028 | FREETMPS; |
|
|
3029 | LEAVE; |
|
|
3030 | PUTBACK; |
|
|
3031 | } |
|
|
3032 | |
3169 | |
3033 | if (!(coro->flags & CF_RUNNING)) |
3170 | if (current != coro) |
3034 | { |
3171 | { |
|
|
3172 | PUTBACK; |
3035 | save_perl (aTHX_ coro); |
3173 | save_perl (aTHX_ coro); |
3036 | load_perl (aTHX_ &temp); |
3174 | load_perl (aTHX_ current); |
3037 | SPAGAIN; |
3175 | SPAGAIN; |
3038 | } |
3176 | } |
3039 | } |
3177 | } |
3040 | } |
3178 | } |
3041 | |
3179 | |
… | |
… | |
3045 | ALIAS: |
3183 | ALIAS: |
3046 | is_ready = CF_READY |
3184 | is_ready = CF_READY |
3047 | is_running = CF_RUNNING |
3185 | is_running = CF_RUNNING |
3048 | is_new = CF_NEW |
3186 | is_new = CF_NEW |
3049 | is_destroyed = CF_DESTROYED |
3187 | is_destroyed = CF_DESTROYED |
|
|
3188 | is_suspended = CF_SUSPENDED |
3050 | CODE: |
3189 | CODE: |
3051 | RETVAL = boolSV (coro->flags & ix); |
3190 | RETVAL = boolSV (coro->flags & ix); |
3052 | OUTPUT: |
3191 | OUTPUT: |
3053 | RETVAL |
3192 | RETVAL |
3054 | |
3193 | |
… | |
… | |
3058 | CODE: |
3197 | CODE: |
3059 | { |
3198 | { |
3060 | struct coro *current = SvSTATE_current; |
3199 | struct coro *current = SvSTATE_current; |
3061 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3200 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3062 | SvREFCNT_dec (*throwp); |
3201 | SvREFCNT_dec (*throwp); |
|
|
3202 | SvGETMAGIC (throw); |
3063 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3203 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3064 | } |
3204 | } |
3065 | |
3205 | |
3066 | void |
3206 | void |
3067 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3207 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
… | |
… | |
3119 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3259 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3120 | |
3260 | |
3121 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3261 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3122 | } |
3262 | } |
3123 | |
3263 | |
|
|
3264 | void |
|
|
3265 | cancel (Coro::State self) |
|
|
3266 | CODE: |
|
|
3267 | coro_state_destroy (aTHX_ self); |
|
|
3268 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3269 | |
3124 | |
3270 | |
3125 | MODULE = Coro::State PACKAGE = Coro |
3271 | MODULE = Coro::State PACKAGE = Coro |
3126 | |
3272 | |
3127 | BOOT: |
3273 | BOOT: |
3128 | { |
3274 | { |
3129 | int i; |
|
|
3130 | |
|
|
3131 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3275 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3132 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3276 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3133 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3277 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3134 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3278 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3135 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3279 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
… | |
… | |
3143 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3287 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3144 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3288 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3145 | |
3289 | |
3146 | coro_stash = gv_stashpv ("Coro", TRUE); |
3290 | coro_stash = gv_stashpv ("Coro", TRUE); |
3147 | |
3291 | |
3148 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3292 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3149 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3293 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3150 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3294 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3151 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3295 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW)); |
3152 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3296 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE)); |
3153 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3297 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN)); |
3154 | |
|
|
3155 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
3156 | coro_ready[i] = newAV (); |
|
|
3157 | |
3298 | |
3158 | { |
3299 | { |
3159 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3300 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3160 | |
3301 | |
3161 | coroapi.schedule = api_schedule; |
3302 | coroapi.schedule = api_schedule; |
… | |
… | |
3202 | cede_notself (...) |
3343 | cede_notself (...) |
3203 | CODE: |
3344 | CODE: |
3204 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3345 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3205 | |
3346 | |
3206 | void |
3347 | void |
3207 | _cancel (Coro::State self) |
|
|
3208 | CODE: |
|
|
3209 | coro_state_destroy (aTHX_ self); |
|
|
3210 | coro_call_on_destroy (aTHX_ self); |
|
|
3211 | |
|
|
3212 | void |
|
|
3213 | _set_current (SV *current) |
3348 | _set_current (SV *current) |
3214 | PROTOTYPE: $ |
3349 | PROTOTYPE: $ |
3215 | CODE: |
3350 | CODE: |
3216 | SvREFCNT_dec (SvRV (coro_current)); |
3351 | SvREFCNT_dec (SvRV (coro_current)); |
3217 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3352 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
… | |
… | |
3219 | void |
3354 | void |
3220 | _set_readyhook (SV *hook) |
3355 | _set_readyhook (SV *hook) |
3221 | PROTOTYPE: $ |
3356 | PROTOTYPE: $ |
3222 | CODE: |
3357 | CODE: |
3223 | SvREFCNT_dec (coro_readyhook); |
3358 | SvREFCNT_dec (coro_readyhook); |
|
|
3359 | SvGETMAGIC (hook); |
3224 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3360 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3225 | |
3361 | |
3226 | int |
3362 | int |
3227 | prio (Coro::State coro, int newprio = 0) |
3363 | prio (Coro::State coro, int newprio = 0) |
3228 | PROTOTYPE: $;$ |
3364 | PROTOTYPE: $;$ |
… | |
… | |
3235 | if (items > 1) |
3371 | if (items > 1) |
3236 | { |
3372 | { |
3237 | if (ix) |
3373 | if (ix) |
3238 | newprio = coro->prio - newprio; |
3374 | newprio = coro->prio - newprio; |
3239 | |
3375 | |
3240 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
3376 | if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; |
3241 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
3377 | if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; |
3242 | |
3378 | |
3243 | coro->prio = newprio; |
3379 | coro->prio = newprio; |
3244 | } |
3380 | } |
3245 | } |
3381 | } |
3246 | OUTPUT: |
3382 | OUTPUT: |
… | |
… | |
3259 | PROTOTYPE: |
3395 | PROTOTYPE: |
3260 | CODE: |
3396 | CODE: |
3261 | RETVAL = coro_nready; |
3397 | RETVAL = coro_nready; |
3262 | OUTPUT: |
3398 | OUTPUT: |
3263 | RETVAL |
3399 | RETVAL |
|
|
3400 | |
|
|
3401 | void |
|
|
3402 | suspend (Coro::State self) |
|
|
3403 | PROTOTYPE: $ |
|
|
3404 | CODE: |
|
|
3405 | self->flags |= CF_SUSPENDED; |
|
|
3406 | |
|
|
3407 | void |
|
|
3408 | resume (Coro::State self) |
|
|
3409 | PROTOTYPE: $ |
|
|
3410 | CODE: |
|
|
3411 | self->flags &= ~CF_SUSPENDED; |
3264 | |
3412 | |
3265 | void |
3413 | void |
3266 | _pool_handler (...) |
3414 | _pool_handler (...) |
3267 | CODE: |
3415 | CODE: |
3268 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
3416 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
… | |
… | |
3323 | rouse_wait (...) |
3471 | rouse_wait (...) |
3324 | PROTOTYPE: ;$ |
3472 | PROTOTYPE: ;$ |
3325 | PPCODE: |
3473 | PPCODE: |
3326 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3474 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3327 | |
3475 | |
|
|
3476 | void |
|
|
3477 | on_enter (SV *block) |
|
|
3478 | ALIAS: |
|
|
3479 | on_leave = 1 |
|
|
3480 | PROTOTYPE: & |
|
|
3481 | CODE: |
|
|
3482 | { |
|
|
3483 | struct coro *coro = SvSTATE_current; |
|
|
3484 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3485 | |
|
|
3486 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3487 | |
|
|
3488 | if (!*avp) |
|
|
3489 | *avp = newAV (); |
|
|
3490 | |
|
|
3491 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3492 | |
|
|
3493 | if (!ix) |
|
|
3494 | on_enterleave_call (aTHX_ block); |
|
|
3495 | |
|
|
3496 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3497 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3498 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3499 | } |
|
|
3500 | |
3328 | |
3501 | |
3329 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3502 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3330 | |
3503 | |
3331 | BOOT: |
3504 | BOOT: |
3332 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3505 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
3335 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3508 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3336 | |
3509 | |
3337 | SV * |
3510 | SV * |
3338 | new (SV *klass, SV *count = 0) |
3511 | new (SV *klass, SV *count = 0) |
3339 | CODE: |
3512 | CODE: |
|
|
3513 | { |
|
|
3514 | int semcnt = 1; |
|
|
3515 | |
|
|
3516 | if (count) |
|
|
3517 | { |
|
|
3518 | SvGETMAGIC (count); |
|
|
3519 | |
|
|
3520 | if (SvOK (count)) |
|
|
3521 | semcnt = SvIV (count); |
|
|
3522 | } |
|
|
3523 | |
3340 | RETVAL = sv_bless ( |
3524 | RETVAL = sv_bless ( |
3341 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
3525 | coro_waitarray_new (aTHX_ semcnt), |
3342 | GvSTASH (CvGV (cv)) |
3526 | GvSTASH (CvGV (cv)) |
3343 | ); |
3527 | ); |
|
|
3528 | } |
3344 | OUTPUT: |
3529 | OUTPUT: |
3345 | RETVAL |
3530 | RETVAL |
3346 | |
3531 | |
3347 | # helper for Coro::Channel |
3532 | # helper for Coro::Channel and others |
3348 | SV * |
3533 | SV * |
3349 | _alloc (int count) |
3534 | _alloc (int count) |
3350 | CODE: |
3535 | CODE: |
3351 | RETVAL = coro_waitarray_new (aTHX_ count); |
3536 | RETVAL = coro_waitarray_new (aTHX_ count); |
3352 | OUTPUT: |
3537 | OUTPUT: |
… | |
… | |
3410 | for (i = 1; i <= wcount; ++i) |
3595 | for (i = 1; i <= wcount; ++i) |
3411 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3596 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3412 | } |
3597 | } |
3413 | } |
3598 | } |
3414 | |
3599 | |
|
|
3600 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
|
|
3601 | |
|
|
3602 | void |
|
|
3603 | _may_delete (SV *sem, int count, int extra_refs) |
|
|
3604 | PPCODE: |
|
|
3605 | { |
|
|
3606 | AV *av = (AV *)SvRV (sem); |
|
|
3607 | |
|
|
3608 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
|
|
3609 | && AvFILLp (av) == 0 /* no waiters, just count */ |
|
|
3610 | && SvIV (AvARRAY (av)[0]) == count) |
|
|
3611 | XSRETURN_YES; |
|
|
3612 | |
|
|
3613 | XSRETURN_NO; |
|
|
3614 | } |
|
|
3615 | |
3415 | MODULE = Coro::State PACKAGE = Coro::Signal |
3616 | MODULE = Coro::State PACKAGE = Coro::Signal |
3416 | |
3617 | |
3417 | SV * |
3618 | SV * |
3418 | new (SV *klass) |
3619 | new (SV *klass) |
3419 | CODE: |
3620 | CODE: |