ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.344 by root, Mon Dec 15 19:39:40 2008 UTC vs.
Revision 1.361 by root, Wed Jul 1 07:26:40 2009 UTC

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
159# if CORO_PTHREAD 159# if CORO_PTHREAD
160static void *coro_thx; 160static void *coro_thx;
161# endif 161# endif
162#endif 162#endif
163 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
171# endif
172#endif
173
164static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
175static void (*u2time)(pTHX_ UV ret[2]);
165 176
166/* we hijack an hopefully unused CV flag for our purposes */ 177/* we hijack an hopefully unused CV flag for our purposes */
167#define CVf_SLF 0x4000 178#define CVf_SLF 0x4000
168static OP *pp_slf (pTHX); 179static OP *pp_slf (pTHX);
169 180
194static CV *cv_pool_handler; 205static CV *cv_pool_handler;
195static CV *cv_coro_state_new; 206static CV *cv_coro_state_new;
196 207
197/* Coro::AnyEvent */ 208/* Coro::AnyEvent */
198static SV *sv_activity; 209static SV *sv_activity;
210
211/* enable processtime/realtime profiling */
212static char enable_times;
213typedef U32 coro_ts[2];
214static coro_ts time_real, time_cpu;
215static char times_valid;
199 216
200static struct coro_cctx *cctx_first; 217static struct coro_cctx *cctx_first;
201static int cctx_count, cctx_idle; 218static int cctx_count, cctx_idle;
202 219
203enum { 220enum {
261/* this is a structure representing a perl-level coroutine */ 278/* this is a structure representing a perl-level coroutine */
262struct coro { 279struct coro {
263 /* the C coroutine allocated to this perl coroutine, if any */ 280 /* the C coroutine allocated to this perl coroutine, if any */
264 coro_cctx *cctx; 281 coro_cctx *cctx;
265 282
283 /* ready queue */
284 struct coro *next_ready;
285
266 /* state data */ 286 /* state data */
267 struct CoroSLF slf_frame; /* saved slf frame */ 287 struct CoroSLF slf_frame; /* saved slf frame */
268 AV *mainstack; 288 AV *mainstack;
269 perl_slots *slot; /* basically the saved sp */ 289 perl_slots *slot; /* basically the saved sp */
270 290
290 310
291 /* on_enter/on_leave */ 311 /* on_enter/on_leave */
292 AV *on_enter; 312 AV *on_enter;
293 AV *on_leave; 313 AV *on_leave;
294 314
315 /* times */
316 coro_ts t_cpu, t_real;
317
295 /* linked list */ 318 /* linked list */
296 struct coro *next, *prev; 319 struct coro *next, *prev;
297}; 320};
298 321
299typedef struct coro *Coro__State; 322typedef struct coro *Coro__State;
304/* the mainr easonw e don't support windows process emulation */ 327/* the mainr easonw e don't support windows process emulation */
305static struct CoroSLF slf_frame; /* the current slf frame */ 328static struct CoroSLF slf_frame; /* the current slf frame */
306 329
307/** Coro ********************************************************************/ 330/** Coro ********************************************************************/
308 331
309#define PRIO_MAX 3 332#define CORO_PRIO_MAX 3
310#define PRIO_HIGH 1 333#define CORO_PRIO_HIGH 1
311#define PRIO_NORMAL 0 334#define CORO_PRIO_NORMAL 0
312#define PRIO_LOW -1 335#define CORO_PRIO_LOW -1
313#define PRIO_IDLE -3 336#define CORO_PRIO_IDLE -3
314#define PRIO_MIN -4 337#define CORO_PRIO_MIN -4
315 338
316/* for Coro.pm */ 339/* for Coro.pm */
317static SV *coro_current; 340static SV *coro_current;
318static SV *coro_readyhook; 341static SV *coro_readyhook;
319static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 342static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
320static CV *cv_coro_run, *cv_coro_terminate; 343static CV *cv_coro_run, *cv_coro_terminate;
321static struct coro *coro_first; 344static struct coro *coro_first;
322#define coro_nready coroapi.nready 345#define coro_nready coroapi.nready
323 346
324/** lowlevel stuff **********************************************************/ 347/** lowlevel stuff **********************************************************/
363 386
364 if (!cv) 387 if (!cv)
365 croak ("code reference expected"); 388 croak ("code reference expected");
366 389
367 return cv; 390 return cv;
391}
392
393INLINE void
394coro_times_update ()
395{
396#ifdef coro_clock_gettime
397 struct timespec ts;
398
399 ts.tv_sec = ts.tv_nsec = 0;
400 coro_clock_gettime (CORO_CLOCK_THREAD_CPUTIME_ID, &ts);
401 time_cpu [0] = ts.tv_sec; time_cpu [1] = ts.tv_nsec;
402
403 ts.tv_sec = ts.tv_nsec = 0;
404 coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts);
405 time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec;
406#else
407 dTHX;
408 UV tv[2];
409
410 u2time (aTHX_ &tv);
411 time_real [0] = tv [0];
412 time_real [1] = tv [1] * 1000;
413#endif
414}
415
416INLINE void
417coro_times_add (struct coro *c)
418{
419 c->t_real [1] += time_real [1];
420 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
421 c->t_real [0] += time_real [0];
422
423 c->t_cpu [1] += time_cpu [1];
424 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
425 c->t_cpu [0] += time_cpu [0];
426}
427
428INLINE void
429coro_times_sub (struct coro *c)
430{
431 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
432 c->t_real [1] -= time_real [1];
433 c->t_real [0] -= time_real [0];
434
435 if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; }
436 c->t_cpu [1] -= time_cpu [1];
437 c->t_cpu [0] -= time_cpu [0];
368} 438}
369 439
370/*****************************************************************************/ 440/*****************************************************************************/
371/* magic glue */ 441/* magic glue */
372 442
472static int 542static int
473coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 543coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
474{ 544{
475 AV *padlist; 545 AV *padlist;
476 AV *av = (AV *)mg->mg_obj; 546 AV *av = (AV *)mg->mg_obj;
547
548 /* perl manages to free our internal AV and _then_ call us */
549 if (IN_DESTRUCT)
550 return;
477 551
478 /* casting is fun. */ 552 /* casting is fun. */
479 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 553 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
480 free_padlist (aTHX_ padlist); 554 free_padlist (aTHX_ padlist);
481 555
570 } 644 }
571 645
572 slf_frame = c->slf_frame; 646 slf_frame = c->slf_frame;
573 CORO_THROW = c->except; 647 CORO_THROW = c->except;
574 648
649 if (expect_false (enable_times))
650 {
651 if (expect_false (!times_valid))
652 coro_times_update ();
653
654 coro_times_sub (c);
655 }
656
575 if (expect_false (c->on_enter)) 657 if (expect_false (c->on_enter))
576 { 658 {
577 int i; 659 int i;
578 660
579 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 661 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
588 { 670 {
589 int i; 671 int i;
590 672
591 for (i = AvFILLp (c->on_leave); i >= 0; --i) 673 for (i = AvFILLp (c->on_leave); i >= 0; --i)
592 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); 674 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
675 }
676
677 times_valid = 0;
678
679 if (expect_false (enable_times))
680 {
681 coro_times_update (); times_valid = 1;
682 coro_times_add (c);
593 } 683 }
594 684
595 c->except = CORO_THROW; 685 c->except = CORO_THROW;
596 c->slf_frame = slf_frame; 686 c->slf_frame = slf_frame;
597 687
612 { 702 {
613 while (expect_true (cxix >= 0)) 703 while (expect_true (cxix >= 0))
614 { 704 {
615 PERL_CONTEXT *cx = &ccstk[cxix--]; 705 PERL_CONTEXT *cx = &ccstk[cxix--];
616 706
617 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 707 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
618 { 708 {
619 CV *cv = cx->blk_sub.cv; 709 CV *cv = cx->blk_sub.cv;
620 710
621 if (expect_true (CvDEPTH (cv))) 711 if (expect_true (CvDEPTH (cv)))
622 { 712 {
961 1051
962 PL_op = (OP *)&coro_setup_op; 1052 PL_op = (OP *)&coro_setup_op;
963 1053
964 /* copy throw, in case it was set before coro_setup */ 1054 /* copy throw, in case it was set before coro_setup */
965 CORO_THROW = coro->except; 1055 CORO_THROW = coro->except;
1056
1057 if (expect_false (enable_times))
1058 {
1059 coro_times_update ();
1060 coro_times_sub (coro);
1061 }
966} 1062}
967 1063
968static void 1064static void
969coro_unwind_stacks (pTHX) 1065coro_unwind_stacks (pTHX)
970{ 1066{
987} 1083}
988 1084
989static void 1085static void
990coro_destruct_perl (pTHX_ struct coro *coro) 1086coro_destruct_perl (pTHX_ struct coro *coro)
991{ 1087{
1088 SV *svf [9];
1089
1090 {
1091 struct coro *current = SvSTATE_current;
1092
1093 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1094
1095 save_perl (aTHX_ current);
1096 load_perl (aTHX_ coro);
1097
992 coro_unwind_stacks (aTHX); 1098 coro_unwind_stacks (aTHX);
1099 coro_destruct_stacks (aTHX);
993 1100
994 SvREFCNT_dec (GvSV (PL_defgv)); 1101 // now save some sv's to be free'd later
995 SvREFCNT_dec (GvAV (PL_defgv)); 1102 svf [0] = GvSV (PL_defgv);
996 SvREFCNT_dec (GvSV (PL_errgv)); 1103 svf [1] = (SV *)GvAV (PL_defgv);
997 SvREFCNT_dec (PL_defoutgv); 1104 svf [2] = GvSV (PL_errgv);
998 SvREFCNT_dec (PL_rs); 1105 svf [3] = (SV *)PL_defoutgv;
999 SvREFCNT_dec (GvSV (irsgv)); 1106 svf [4] = PL_rs;
1000 SvREFCNT_dec (GvHV (PL_hintgv)); 1107 svf [5] = GvSV (irsgv);
1108 svf [6] = (SV *)GvHV (PL_hintgv);
1109 svf [7] = PL_diehook;
1110 svf [8] = PL_warnhook;
1111 assert (9 == sizeof (svf) / sizeof (*svf));
1001 1112
1002 SvREFCNT_dec (PL_diehook); 1113 load_perl (aTHX_ current);
1003 SvREFCNT_dec (PL_warnhook);
1004 1114 }
1115
1116 {
1117 int i;
1118
1119 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1120 SvREFCNT_dec (svf [i]);
1121
1005 SvREFCNT_dec (coro->saved_deffh); 1122 SvREFCNT_dec (coro->saved_deffh);
1006 SvREFCNT_dec (coro->rouse_cb); 1123 SvREFCNT_dec (coro->rouse_cb);
1007 SvREFCNT_dec (coro->invoke_cb); 1124 SvREFCNT_dec (coro->invoke_cb);
1008 SvREFCNT_dec (coro->invoke_av); 1125 SvREFCNT_dec (coro->invoke_av);
1009 1126 }
1010 coro_destruct_stacks (aTHX);
1011} 1127}
1012 1128
1013INLINE void 1129INLINE void
1014free_coro_mortal (pTHX) 1130free_coro_mortal (pTHX)
1015{ 1131{
1502 1618
1503 if (coro->mainstack 1619 if (coro->mainstack
1504 && coro->mainstack != main_mainstack 1620 && coro->mainstack != main_mainstack
1505 && coro->slot 1621 && coro->slot
1506 && !PL_dirty) 1622 && !PL_dirty)
1507 {
1508 struct coro *current = SvSTATE_current;
1509
1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1511
1512 save_perl (aTHX_ current);
1513 load_perl (aTHX_ coro);
1514
1515 coro_destruct_perl (aTHX_ coro); 1623 coro_destruct_perl (aTHX_ coro);
1516
1517 load_perl (aTHX_ current);
1518
1519 coro->slot = 0;
1520 }
1521 1624
1522 cctx_destroy (coro->cctx); 1625 cctx_destroy (coro->cctx);
1523 SvREFCNT_dec (coro->startcv); 1626 SvREFCNT_dec (coro->startcv);
1524 SvREFCNT_dec (coro->args); 1627 SvREFCNT_dec (coro->args);
1525 SvREFCNT_dec (CORO_THROW); 1628 SvREFCNT_dec (CORO_THROW);
1612/** Coro ********************************************************************/ 1715/** Coro ********************************************************************/
1613 1716
1614INLINE void 1717INLINE void
1615coro_enq (pTHX_ struct coro *coro) 1718coro_enq (pTHX_ struct coro *coro)
1616{ 1719{
1617 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1720 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1618}
1619 1721
1620INLINE SV * 1722 SvREFCNT_inc_NN (coro->hv);
1723
1724 coro->next_ready = 0;
1725 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1726 ready [1] = coro;
1727}
1728
1729INLINE struct coro *
1621coro_deq (pTHX) 1730coro_deq (pTHX)
1622{ 1731{
1623 int prio; 1732 int prio;
1624 1733
1625 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1734 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1626 if (AvFILLp (coro_ready [prio]) >= 0) 1735 {
1627 return av_shift (coro_ready [prio]); 1736 struct coro **ready = coro_ready [prio];
1737
1738 if (ready [0])
1739 {
1740 struct coro *coro = ready [0];
1741 ready [0] = coro->next_ready;
1742 return coro;
1743 }
1744 }
1628 1745
1629 return 0; 1746 return 0;
1630} 1747}
1631 1748
1632static int 1749static int
1696static void 1813static void
1697prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1814prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1698{ 1815{
1699 for (;;) 1816 for (;;)
1700 { 1817 {
1701 SV *next_sv = coro_deq (aTHX); 1818 struct coro *next = coro_deq (aTHX);
1702 1819
1703 if (expect_true (next_sv)) 1820 if (expect_true (next))
1704 { 1821 {
1705 struct coro *next = SvSTATE_hv (next_sv);
1706
1707 /* cannot transfer to destroyed coros, skip and look for next */ 1822 /* cannot transfer to destroyed coros, skip and look for next */
1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) 1823 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1824 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1710 else 1825 else
1711 { 1826 {
1712 next->flags &= ~CF_READY; 1827 next->flags &= ~CF_READY;
1713 --coro_nready; 1828 --coro_nready;
1714 1829
1988 SV *data = (SV *)GENSUB_ARG; 2103 SV *data = (SV *)GENSUB_ARG;
1989 2104
1990 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2105 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1991 { 2106 {
1992 /* first call, set args */ 2107 /* first call, set args */
2108 SV *coro = SvRV (data);
1993 AV *av = newAV (); 2109 AV *av = newAV ();
1994 SV *coro = SvRV (data);
1995 2110
1996 SvRV_set (data, (SV *)av); 2111 SvRV_set (data, (SV *)av);
1997 api_ready (aTHX_ coro);
1998 SvREFCNT_dec (coro);
1999 2112
2000 /* better take a full copy of the arguments */ 2113 /* better take a full copy of the arguments */
2001 while (items--) 2114 while (items--)
2002 av_store (av, items, newSVsv (ST (items))); 2115 av_store (av, items, newSVsv (ST (items)));
2116
2117 api_ready (aTHX_ coro);
2118 SvREFCNT_dec (coro);
2003 } 2119 }
2004 2120
2005 XSRETURN_EMPTY; 2121 XSRETURN_EMPTY;
2006} 2122}
2007 2123
2024 2140
2025 EXTEND (SP, AvFILLp (av) + 1); 2141 EXTEND (SP, AvFILLp (av) + 1);
2026 for (i = 0; i <= AvFILLp (av); ++i) 2142 for (i = 0; i <= AvFILLp (av); ++i)
2027 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2143 PUSHs (sv_2mortal (AvARRAY (av)[i]));
2028 2144
2029 /* we have stolen the elements, so ste length to zero and free */ 2145 /* we have stolen the elements, so set length to zero and free */
2030 AvFILLp (av) = -1; 2146 AvFILLp (av) = -1;
2031 av_undef (av); 2147 av_undef (av);
2032 2148
2033 PUTBACK; 2149 PUTBACK;
2034 } 2150 }
2454/* Coro::Semaphore & Coro::Signal */ 2570/* Coro::Semaphore & Coro::Signal */
2455 2571
2456static SV * 2572static SV *
2457coro_waitarray_new (pTHX_ int count) 2573coro_waitarray_new (pTHX_ int count)
2458{ 2574{
2459 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2575 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2460 AV *av = newAV (); 2576 AV *av = newAV ();
2461 SV **ary; 2577 SV **ary;
2462 2578
2463 /* unfortunately, building manually saves memory */ 2579 /* unfortunately, building manually saves memory */
2464 Newx (ary, 2, SV *); 2580 Newx (ary, 2, SV *);
2608 { 2724 {
2609 /* callback form */ 2725 /* callback form */
2610 AV *av = (AV *)SvRV (arg [0]); 2726 AV *av = (AV *)SvRV (arg [0]);
2611 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2727 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2612 2728
2613 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2729 av_push (av, SvREFCNT_inc_NN (cb_cv));
2614 2730
2615 if (SvIVX (AvARRAY (av)[0]) > 0) 2731 if (SvIVX (AvARRAY (av)[0]) > 0)
2616 coro_semaphore_adjust (aTHX_ av, 0); 2732 coro_semaphore_adjust (aTHX_ av, 0);
2617 2733
2618 frame->prepare = prepare_nop; 2734 frame->prepare = prepare_nop;
2642 AvARRAY (av)[0] = AvARRAY (av)[1]; 2758 AvARRAY (av)[0] = AvARRAY (av)[1];
2643 AvARRAY (av)[1] = cb; 2759 AvARRAY (av)[1] = cb;
2644 2760
2645 cb = av_shift (av); 2761 cb = av_shift (av);
2646 2762
2763 if (SvTYPE (cb) == SVt_PVCV)
2764 {
2765 dSP;
2766 PUSHMARK (SP);
2767 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2768 PUTBACK;
2769 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2770 }
2771 else
2772 {
2647 api_ready (aTHX_ cb); 2773 api_ready (aTHX_ cb);
2648 sv_setiv (cb, 0); /* signal waiter */ 2774 sv_setiv (cb, 0); /* signal waiter */
2775 }
2776
2649 SvREFCNT_dec (cb); 2777 SvREFCNT_dec (cb);
2650 2778
2651 --count; 2779 --count;
2652 } 2780 }
2653} 2781}
2662static void 2790static void
2663slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2791slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2664{ 2792{
2665 AV *av = (AV *)SvRV (arg [0]); 2793 AV *av = (AV *)SvRV (arg [0]);
2666 2794
2795 if (items >= 2)
2796 {
2797 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2798 av_push (av, SvREFCNT_inc_NN (cb_cv));
2799
2667 if (SvIVX (AvARRAY (av)[0])) 2800 if (SvIVX (AvARRAY (av)[0]))
2801 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2802
2803 frame->prepare = prepare_nop;
2804 frame->check = slf_check_nop;
2805 }
2806 else if (SvIVX (AvARRAY (av)[0]))
2668 { 2807 {
2669 SvIVX (AvARRAY (av)[0]) = 0; 2808 SvIVX (AvARRAY (av)[0]) = 0;
2670 frame->prepare = prepare_nop; 2809 frame->prepare = prepare_nop;
2671 frame->check = slf_check_nop; 2810 frame->check = slf_check_nop;
2672 } 2811 }
2673 else 2812 else
2674 { 2813 {
2675 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2814 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2676 2815
2677 av_push (av, waiter); 2816 av_push (av, waiter);
2678 2817
2679 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2818 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2680 frame->prepare = prepare_schedule; 2819 frame->prepare = prepare_schedule;
2919 3058
2920 if (!svp) croak ("Time::HiRes is required"); 3059 if (!svp) croak ("Time::HiRes is required");
2921 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 3060 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2922 3061
2923 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 3062 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3063
3064 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3065 u2time = INT2PTR (double (*)(), SvIV (*svp));
2924 } 3066 }
2925 3067
2926 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 3068 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2927} 3069}
2928 3070
2929SV * 3071SV *
2930new (char *klass, ...) 3072new (char *klass, ...)
2931 ALIAS: 3073 ALIAS:
3133 CODE: 3275 CODE:
3134{ 3276{
3135 struct coro *current = SvSTATE_current; 3277 struct coro *current = SvSTATE_current;
3136 SV **throwp = self == current ? &CORO_THROW : &self->except; 3278 SV **throwp = self == current ? &CORO_THROW : &self->except;
3137 SvREFCNT_dec (*throwp); 3279 SvREFCNT_dec (*throwp);
3280 SvGETMAGIC (throw);
3138 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3281 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
3139} 3282}
3140 3283
3141void 3284void
3142api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3285api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3201 CODE: 3344 CODE:
3202 coro_state_destroy (aTHX_ self); 3345 coro_state_destroy (aTHX_ self);
3203 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ 3346 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3204 3347
3205 3348
3349SV *
3350enable_times (int enabled = enable_times)
3351 CODE:
3352{
3353 RETVAL = boolSV (enable_times);
3354
3355 if (enabled != enable_times)
3356 {
3357 enable_times = enabled;
3358
3359 coro_times_update ();
3360 (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current));
3361 }
3362}
3363 OUTPUT:
3364 RETVAL
3365
3366void
3367times (Coro::State self)
3368 PPCODE:
3369{
3370 struct coro *current = SvSTATE (coro_current);
3371
3372 if (expect_false (current == self))
3373 {
3374 coro_times_update ();
3375 coro_times_add (SvSTATE (coro_current));
3376 }
3377
3378 EXTEND (SP, 2);
3379 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3380 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3381
3382 if (expect_false (current == self))
3383 coro_times_sub (SvSTATE (coro_current));
3384}
3385
3206MODULE = Coro::State PACKAGE = Coro 3386MODULE = Coro::State PACKAGE = Coro
3207 3387
3208BOOT: 3388BOOT:
3209{ 3389{
3210 int i;
3211
3212 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3390 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3213 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3391 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3214 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3392 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3215 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3393 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3216 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3394 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3224 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3402 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3225 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3403 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3226 3404
3227 coro_stash = gv_stashpv ("Coro", TRUE); 3405 coro_stash = gv_stashpv ("Coro", TRUE);
3228 3406
3229 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3407 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3230 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3408 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3231 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3409 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3232 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3410 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW));
3233 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3411 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE));
3234 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3412 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN));
3235
3236 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3237 coro_ready[i] = newAV ();
3238 3413
3239 { 3414 {
3240 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3415 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3241 3416
3242 coroapi.schedule = api_schedule; 3417 coroapi.schedule = api_schedule;
3294void 3469void
3295_set_readyhook (SV *hook) 3470_set_readyhook (SV *hook)
3296 PROTOTYPE: $ 3471 PROTOTYPE: $
3297 CODE: 3472 CODE:
3298 SvREFCNT_dec (coro_readyhook); 3473 SvREFCNT_dec (coro_readyhook);
3474 SvGETMAGIC (hook);
3299 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3475 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
3300 3476
3301int 3477int
3302prio (Coro::State coro, int newprio = 0) 3478prio (Coro::State coro, int newprio = 0)
3303 PROTOTYPE: $;$ 3479 PROTOTYPE: $;$
3310 if (items > 1) 3486 if (items > 1)
3311 { 3487 {
3312 if (ix) 3488 if (ix)
3313 newprio = coro->prio - newprio; 3489 newprio = coro->prio - newprio;
3314 3490
3315 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 3491 if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN;
3316 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 3492 if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX;
3317 3493
3318 coro->prio = newprio; 3494 coro->prio = newprio;
3319 } 3495 }
3320} 3496}
3321 OUTPUT: 3497 OUTPUT:
3334 PROTOTYPE: 3510 PROTOTYPE:
3335 CODE: 3511 CODE:
3336 RETVAL = coro_nready; 3512 RETVAL = coro_nready;
3337 OUTPUT: 3513 OUTPUT:
3338 RETVAL 3514 RETVAL
3515
3516void
3517suspend (Coro::State self)
3518 PROTOTYPE: $
3519 CODE:
3520 self->flags |= CF_SUSPENDED;
3521
3522void
3523resume (Coro::State self)
3524 PROTOTYPE: $
3525 CODE:
3526 self->flags &= ~CF_SUSPENDED;
3339 3527
3340void 3528void
3341_pool_handler (...) 3529_pool_handler (...)
3342 CODE: 3530 CODE:
3343 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3531 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3418 av_push (*avp, SvREFCNT_inc (block)); 3606 av_push (*avp, SvREFCNT_inc (block));
3419 3607
3420 if (!ix) 3608 if (!ix)
3421 on_enterleave_call (aTHX_ block); 3609 on_enterleave_call (aTHX_ block);
3422 3610
3423 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ 3611 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3424 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); 3612 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3425 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ 3613 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3426} 3614}
3427 3615
3428 3616
3429MODULE = Coro::State PACKAGE = PerlIO::cede 3617MODULE = Coro::State PACKAGE = PerlIO::cede
3430 3618
3435MODULE = Coro::State PACKAGE = Coro::Semaphore 3623MODULE = Coro::State PACKAGE = Coro::Semaphore
3436 3624
3437SV * 3625SV *
3438new (SV *klass, SV *count = 0) 3626new (SV *klass, SV *count = 0)
3439 CODE: 3627 CODE:
3628{
3629 int semcnt = 1;
3630
3631 if (count)
3632 {
3633 SvGETMAGIC (count);
3634
3635 if (SvOK (count))
3636 semcnt = SvIV (count);
3637 }
3638
3440 RETVAL = sv_bless ( 3639 RETVAL = sv_bless (
3441 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), 3640 coro_waitarray_new (aTHX_ semcnt),
3442 GvSTASH (CvGV (cv)) 3641 GvSTASH (CvGV (cv))
3443 ); 3642 );
3643}
3444 OUTPUT: 3644 OUTPUT:
3445 RETVAL 3645 RETVAL
3446 3646
3447# helper for Coro::Channel and others 3647# helper for Coro::Channel and others
3448SV * 3648SV *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines