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.338 by root, Sun Dec 7 15:33:21 2008 UTC vs.
Revision 1.360 by root, Mon Jun 29 06:14:23 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
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;
199 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;
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 {
204 CC_MAPPED = 0x01, 221 CC_MAPPED = 0x01,
238enum { 255enum {
239 CF_RUNNING = 0x0001, /* coroutine is running */ 256 CF_RUNNING = 0x0001, /* coroutine is running */
240 CF_READY = 0x0002, /* coroutine is ready */ 257 CF_READY = 0x0002, /* coroutine is ready */
241 CF_NEW = 0x0004, /* has never been switched to */ 258 CF_NEW = 0x0004, /* has never been switched to */
242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 259 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
260 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
243}; 261};
244 262
245/* the structure where most of the perl state is stored, overlaid on the cxstack */ 263/* the structure where most of the perl state is stored, overlaid on the cxstack */
246typedef struct 264typedef struct
247{ 265{
260/* this is a structure representing a perl-level coroutine */ 278/* this is a structure representing a perl-level coroutine */
261struct coro { 279struct coro {
262 /* the C coroutine allocated to this perl coroutine, if any */ 280 /* the C coroutine allocated to this perl coroutine, if any */
263 coro_cctx *cctx; 281 coro_cctx *cctx;
264 282
283 /* ready queue */
284 struct coro *next_ready;
285
265 /* state data */ 286 /* state data */
266 struct CoroSLF slf_frame; /* saved slf frame */ 287 struct CoroSLF slf_frame; /* saved slf frame */
267 AV *mainstack; 288 AV *mainstack;
268 perl_slots *slot; /* basically the saved sp */ 289 perl_slots *slot; /* basically the saved sp */
269 290
285 /* async_pool */ 306 /* async_pool */
286 SV *saved_deffh; 307 SV *saved_deffh;
287 SV *invoke_cb; 308 SV *invoke_cb;
288 AV *invoke_av; 309 AV *invoke_av;
289 310
311 /* on_enter/on_leave */
312 AV *on_enter;
313 AV *on_leave;
314
315 /* times */
316 coro_ts t_cpu, t_real;
317
290 /* linked list */ 318 /* linked list */
291 struct coro *next, *prev; 319 struct coro *next, *prev;
292}; 320};
293 321
294typedef struct coro *Coro__State; 322typedef struct coro *Coro__State;
299/* the mainr easonw e don't support windows process emulation */ 327/* the mainr easonw e don't support windows process emulation */
300static struct CoroSLF slf_frame; /* the current slf frame */ 328static struct CoroSLF slf_frame; /* the current slf frame */
301 329
302/** Coro ********************************************************************/ 330/** Coro ********************************************************************/
303 331
304#define PRIO_MAX 3 332#define CORO_PRIO_MAX 3
305#define PRIO_HIGH 1 333#define CORO_PRIO_HIGH 1
306#define PRIO_NORMAL 0 334#define CORO_PRIO_NORMAL 0
307#define PRIO_LOW -1 335#define CORO_PRIO_LOW -1
308#define PRIO_IDLE -3 336#define CORO_PRIO_IDLE -3
309#define PRIO_MIN -4 337#define CORO_PRIO_MIN -4
310 338
311/* for Coro.pm */ 339/* for Coro.pm */
312static SV *coro_current; 340static SV *coro_current;
313static SV *coro_readyhook; 341static SV *coro_readyhook;
314static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 342static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
315static CV *cv_coro_run, *cv_coro_terminate; 343static CV *cv_coro_run, *cv_coro_terminate;
316static struct coro *coro_first; 344static struct coro *coro_first;
317#define coro_nready coroapi.nready 345#define coro_nready coroapi.nready
318 346
319/** lowlevel stuff **********************************************************/ 347/** lowlevel stuff **********************************************************/
352INLINE CV * 380INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv) 381coro_sv_2cv (pTHX_ SV *sv)
354{ 382{
355 HV *st; 383 HV *st;
356 GV *gvp; 384 GV *gvp;
357 return sv_2cv (sv, &st, &gvp, 0); 385 CV *cv = sv_2cv (sv, &st, &gvp, 0);
386
387 if (!cv)
388 croak ("code reference expected");
389
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 UV tv[2];
408
409 u2time (aTHX_ &tv);
410 time_real [0] = tv [0];
411 time_real [1] = tv [1] * 1000;
412#endif
413}
414
415INLINE void
416coro_times_add (struct coro *c)
417{
418 c->t_real [1] += time_real [1];
419 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
420 c->t_real [0] += time_real [0];
421
422 c->t_cpu [1] += time_cpu [1];
423 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
424 c->t_cpu [0] += time_cpu [0];
425}
426
427INLINE void
428coro_times_sub (struct coro *c)
429{
430 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
431 c->t_real [1] -= time_real [1];
432 c->t_real [0] -= time_real [0];
433
434 if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; }
435 c->t_cpu [1] -= time_cpu [1];
436 c->t_cpu [0] -= time_cpu [0];
358} 437}
359 438
360/*****************************************************************************/ 439/*****************************************************************************/
361/* magic glue */ 440/* magic glue */
362 441
462static int 541static int
463coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 542coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
464{ 543{
465 AV *padlist; 544 AV *padlist;
466 AV *av = (AV *)mg->mg_obj; 545 AV *av = (AV *)mg->mg_obj;
546
547 /* perl manages to free our internal AV and _then_ call us */
548 if (IN_DESTRUCT)
549 return;
467 550
468 /* casting is fun. */ 551 /* casting is fun. */
469 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 552 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
470 free_padlist (aTHX_ padlist); 553 free_padlist (aTHX_ padlist);
471 554
521} 604}
522 605
523/** load & save, init *******************************************************/ 606/** load & save, init *******************************************************/
524 607
525static void 608static void
609on_enterleave_call (pTHX_ SV *cb);
610
611static void
526load_perl (pTHX_ Coro__State c) 612load_perl (pTHX_ Coro__State c)
527{ 613{
528 perl_slots *slot = c->slot; 614 perl_slots *slot = c->slot;
529 c->slot = 0; 615 c->slot = 0;
530 616
556 PUTBACK; 642 PUTBACK;
557 } 643 }
558 644
559 slf_frame = c->slf_frame; 645 slf_frame = c->slf_frame;
560 CORO_THROW = c->except; 646 CORO_THROW = c->except;
647
648 if (expect_false (enable_times))
649 {
650 if (expect_false (!times_valid))
651 coro_times_update ();
652
653 coro_times_sub (c);
654 }
655
656 if (expect_false (c->on_enter))
657 {
658 int i;
659
660 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
661 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
662 }
561} 663}
562 664
563static void 665static void
564save_perl (pTHX_ Coro__State c) 666save_perl (pTHX_ Coro__State c)
565{ 667{
668 if (expect_false (c->on_leave))
669 {
670 int i;
671
672 for (i = AvFILLp (c->on_leave); i >= 0; --i)
673 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
674 }
675
676 times_valid = 0;
677
678 if (expect_false (enable_times))
679 {
680 coro_times_update (); times_valid = 1;
681 coro_times_add (c);
682 }
683
566 c->except = CORO_THROW; 684 c->except = CORO_THROW;
567 c->slf_frame = slf_frame; 685 c->slf_frame = slf_frame;
568 686
569 { 687 {
570 dSP; 688 dSP;
583 { 701 {
584 while (expect_true (cxix >= 0)) 702 while (expect_true (cxix >= 0))
585 { 703 {
586 PERL_CONTEXT *cx = &ccstk[cxix--]; 704 PERL_CONTEXT *cx = &ccstk[cxix--];
587 705
588 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 706 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
589 { 707 {
590 CV *cv = cx->blk_sub.cv; 708 CV *cv = cx->blk_sub.cv;
591 709
592 if (expect_true (CvDEPTH (cv))) 710 if (expect_true (CvDEPTH (cv)))
593 { 711 {
932 1050
933 PL_op = (OP *)&coro_setup_op; 1051 PL_op = (OP *)&coro_setup_op;
934 1052
935 /* copy throw, in case it was set before coro_setup */ 1053 /* copy throw, in case it was set before coro_setup */
936 CORO_THROW = coro->except; 1054 CORO_THROW = coro->except;
1055
1056 if (expect_false (enable_times))
1057 {
1058 coro_times_update ();
1059 coro_times_sub (coro);
1060 }
937} 1061}
938 1062
939static void 1063static void
940coro_unwind_stacks (pTHX) 1064coro_unwind_stacks (pTHX)
941{ 1065{
958} 1082}
959 1083
960static void 1084static void
961coro_destruct_perl (pTHX_ struct coro *coro) 1085coro_destruct_perl (pTHX_ struct coro *coro)
962{ 1086{
1087 SV *svf [9];
1088
1089 {
1090 struct coro *current = SvSTATE_current;
1091
1092 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1093
1094 save_perl (aTHX_ current);
1095 load_perl (aTHX_ coro);
1096
963 coro_unwind_stacks (aTHX); 1097 coro_unwind_stacks (aTHX);
1098 coro_destruct_stacks (aTHX);
964 1099
965 SvREFCNT_dec (GvSV (PL_defgv)); 1100 // now save some sv's to be free'd later
966 SvREFCNT_dec (GvAV (PL_defgv)); 1101 svf [0] = GvSV (PL_defgv);
967 SvREFCNT_dec (GvSV (PL_errgv)); 1102 svf [1] = (SV *)GvAV (PL_defgv);
968 SvREFCNT_dec (PL_defoutgv); 1103 svf [2] = GvSV (PL_errgv);
969 SvREFCNT_dec (PL_rs); 1104 svf [3] = (SV *)PL_defoutgv;
970 SvREFCNT_dec (GvSV (irsgv)); 1105 svf [4] = PL_rs;
971 SvREFCNT_dec (GvHV (PL_hintgv)); 1106 svf [5] = GvSV (irsgv);
1107 svf [6] = (SV *)GvHV (PL_hintgv);
1108 svf [7] = PL_diehook;
1109 svf [8] = PL_warnhook;
1110 assert (9 == sizeof (svf) / sizeof (*svf));
972 1111
973 SvREFCNT_dec (PL_diehook); 1112 load_perl (aTHX_ current);
974 SvREFCNT_dec (PL_warnhook);
975 1113 }
1114
1115 {
1116 int i;
1117
1118 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1119 SvREFCNT_dec (svf [i]);
1120
976 SvREFCNT_dec (coro->saved_deffh); 1121 SvREFCNT_dec (coro->saved_deffh);
977 SvREFCNT_dec (coro->rouse_cb); 1122 SvREFCNT_dec (coro->rouse_cb);
978 SvREFCNT_dec (coro->invoke_cb); 1123 SvREFCNT_dec (coro->invoke_cb);
979 SvREFCNT_dec (coro->invoke_av); 1124 SvREFCNT_dec (coro->invoke_av);
980 1125 }
981 coro_destruct_stacks (aTHX);
982} 1126}
983 1127
984INLINE void 1128INLINE void
985free_coro_mortal (pTHX) 1129free_coro_mortal (pTHX)
986{ 1130{
1354 /* TODO: throwing up here is considered harmful */ 1498 /* TODO: throwing up here is considered harmful */
1355 1499
1356 if (expect_true (prev != next)) 1500 if (expect_true (prev != next))
1357 { 1501 {
1358 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1502 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1359 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); 1503 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1360 1504
1361 if (expect_false (next->flags & CF_RUNNING))
1362 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1363
1364 if (expect_false (next->flags & CF_DESTROYED)) 1505 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1365 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1506 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1366 1507
1367#if !PERL_VERSION_ATLEAST (5,10,0) 1508#if !PERL_VERSION_ATLEAST (5,10,0)
1368 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1509 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1369 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1510 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1370#endif 1511#endif
1458coro_state_destroy (pTHX_ struct coro *coro) 1599coro_state_destroy (pTHX_ struct coro *coro)
1459{ 1600{
1460 if (coro->flags & CF_DESTROYED) 1601 if (coro->flags & CF_DESTROYED)
1461 return 0; 1602 return 0;
1462 1603
1463 if (coro->on_destroy) 1604 if (coro->on_destroy && !PL_dirty)
1464 coro->on_destroy (aTHX_ coro); 1605 coro->on_destroy (aTHX_ coro);
1465 1606
1466 coro->flags |= CF_DESTROYED; 1607 coro->flags |= CF_DESTROYED;
1467 1608
1468 if (coro->flags & CF_READY) 1609 if (coro->flags & CF_READY)
1476 1617
1477 if (coro->mainstack 1618 if (coro->mainstack
1478 && coro->mainstack != main_mainstack 1619 && coro->mainstack != main_mainstack
1479 && coro->slot 1620 && coro->slot
1480 && !PL_dirty) 1621 && !PL_dirty)
1481 {
1482 struct coro temp;
1483
1484 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1485
1486 save_perl (aTHX_ &temp);
1487 load_perl (aTHX_ coro);
1488
1489 coro_destruct_perl (aTHX_ coro); 1622 coro_destruct_perl (aTHX_ coro);
1490
1491 load_perl (aTHX_ &temp);
1492
1493 coro->slot = 0;
1494 }
1495 1623
1496 cctx_destroy (coro->cctx); 1624 cctx_destroy (coro->cctx);
1497 SvREFCNT_dec (coro->startcv); 1625 SvREFCNT_dec (coro->startcv);
1498 SvREFCNT_dec (coro->args); 1626 SvREFCNT_dec (coro->args);
1499 SvREFCNT_dec (CORO_THROW); 1627 SvREFCNT_dec (CORO_THROW);
1586/** Coro ********************************************************************/ 1714/** Coro ********************************************************************/
1587 1715
1588INLINE void 1716INLINE void
1589coro_enq (pTHX_ struct coro *coro) 1717coro_enq (pTHX_ struct coro *coro)
1590{ 1718{
1591 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1719 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1592}
1593 1720
1594INLINE SV * 1721 SvREFCNT_inc_NN (coro->hv);
1722
1723 coro->next_ready = 0;
1724 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1725 ready [1] = coro;
1726}
1727
1728INLINE struct coro *
1595coro_deq (pTHX) 1729coro_deq (pTHX)
1596{ 1730{
1597 int prio; 1731 int prio;
1598 1732
1599 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1733 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1600 if (AvFILLp (coro_ready [prio]) >= 0) 1734 {
1601 return av_shift (coro_ready [prio]); 1735 struct coro **ready = coro_ready [prio];
1736
1737 if (ready [0])
1738 {
1739 struct coro *coro = ready [0];
1740 ready [0] = coro->next_ready;
1741 return coro;
1742 }
1743 }
1602 1744
1603 return 0; 1745 return 0;
1604} 1746}
1605 1747
1606static int 1748static int
1670static void 1812static void
1671prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1813prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1672{ 1814{
1673 for (;;) 1815 for (;;)
1674 { 1816 {
1675 SV *next_sv = coro_deq (aTHX); 1817 struct coro *next = coro_deq (aTHX);
1676 1818
1677 if (expect_true (next_sv)) 1819 if (expect_true (next))
1678 { 1820 {
1679 struct coro *next = SvSTATE_hv (next_sv);
1680
1681 /* cannot transfer to destroyed coros, skip and look for next */ 1821 /* cannot transfer to destroyed coros, skip and look for next */
1682 if (expect_false (next->flags & CF_DESTROYED)) 1822 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1683 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1823 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1684 else 1824 else
1685 { 1825 {
1686 next->flags &= ~CF_READY; 1826 next->flags &= ~CF_READY;
1687 --coro_nready; 1827 --coro_nready;
1688 1828
1962 SV *data = (SV *)GENSUB_ARG; 2102 SV *data = (SV *)GENSUB_ARG;
1963 2103
1964 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2104 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1965 { 2105 {
1966 /* first call, set args */ 2106 /* first call, set args */
2107 SV *coro = SvRV (data);
1967 AV *av = newAV (); 2108 AV *av = newAV ();
1968 SV *coro = SvRV (data);
1969 2109
1970 SvRV_set (data, (SV *)av); 2110 SvRV_set (data, (SV *)av);
1971 api_ready (aTHX_ coro);
1972 SvREFCNT_dec (coro);
1973 2111
1974 /* better take a full copy of the arguments */ 2112 /* better take a full copy of the arguments */
1975 while (items--) 2113 while (items--)
1976 av_store (av, items, newSVsv (ST (items))); 2114 av_store (av, items, newSVsv (ST (items)));
2115
2116 api_ready (aTHX_ coro);
2117 SvREFCNT_dec (coro);
1977 } 2118 }
1978 2119
1979 XSRETURN_EMPTY; 2120 XSRETURN_EMPTY;
1980} 2121}
1981 2122
1998 2139
1999 EXTEND (SP, AvFILLp (av) + 1); 2140 EXTEND (SP, AvFILLp (av) + 1);
2000 for (i = 0; i <= AvFILLp (av); ++i) 2141 for (i = 0; i <= AvFILLp (av); ++i)
2001 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2142 PUSHs (sv_2mortal (AvARRAY (av)[i]));
2002 2143
2003 /* we have stolen the elements, so ste length to zero and free */ 2144 /* we have stolen the elements, so set length to zero and free */
2004 AvFILLp (av) = -1; 2145 AvFILLp (av) = -1;
2005 av_undef (av); 2146 av_undef (av);
2006 2147
2007 PUTBACK; 2148 PUTBACK;
2008 } 2149 }
2298 2439
2299 PL_op->op_ppaddr = pp_slf; 2440 PL_op->op_ppaddr = pp_slf;
2300 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2441 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2301 2442
2302 PL_op = (OP *)&slf_restore; 2443 PL_op = (OP *)&slf_restore;
2444}
2445
2446/*****************************************************************************/
2447/* dynamic wind */
2448
2449static void
2450on_enterleave_call (pTHX_ SV *cb)
2451{
2452 dSP;
2453
2454 PUSHSTACK;
2455
2456 PUSHMARK (SP);
2457 PUTBACK;
2458 call_sv (cb, G_VOID | G_DISCARD);
2459 SPAGAIN;
2460
2461 POPSTACK;
2462}
2463
2464static SV *
2465coro_avp_pop_and_free (pTHX_ AV **avp)
2466{
2467 AV *av = *avp;
2468 SV *res = av_pop (av);
2469
2470 if (AvFILLp (av) < 0)
2471 {
2472 *avp = 0;
2473 SvREFCNT_dec (av);
2474 }
2475
2476 return res;
2477}
2478
2479static void
2480coro_pop_on_enter (pTHX_ void *coro)
2481{
2482 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2483 SvREFCNT_dec (cb);
2484}
2485
2486static void
2487coro_pop_on_leave (pTHX_ void *coro)
2488{
2489 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2490 on_enterleave_call (aTHX_ sv_2mortal (cb));
2303} 2491}
2304 2492
2305/*****************************************************************************/ 2493/*****************************************************************************/
2306/* PerlIO::cede */ 2494/* PerlIO::cede */
2307 2495
2381/* Coro::Semaphore & Coro::Signal */ 2569/* Coro::Semaphore & Coro::Signal */
2382 2570
2383static SV * 2571static SV *
2384coro_waitarray_new (pTHX_ int count) 2572coro_waitarray_new (pTHX_ int count)
2385{ 2573{
2386 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2574 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2387 AV *av = newAV (); 2575 AV *av = newAV ();
2388 SV **ary; 2576 SV **ary;
2389 2577
2390 /* unfortunately, building manually saves memory */ 2578 /* unfortunately, building manually saves memory */
2391 Newx (ary, 2, SV *); 2579 Newx (ary, 2, SV *);
2535 { 2723 {
2536 /* callback form */ 2724 /* callback form */
2537 AV *av = (AV *)SvRV (arg [0]); 2725 AV *av = (AV *)SvRV (arg [0]);
2538 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2726 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2539 2727
2540 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2728 av_push (av, SvREFCNT_inc_NN (cb_cv));
2541 2729
2542 if (SvIVX (AvARRAY (av)[0]) > 0) 2730 if (SvIVX (AvARRAY (av)[0]) > 0)
2543 coro_semaphore_adjust (aTHX_ av, 0); 2731 coro_semaphore_adjust (aTHX_ av, 0);
2544 2732
2545 frame->prepare = prepare_nop; 2733 frame->prepare = prepare_nop;
2569 AvARRAY (av)[0] = AvARRAY (av)[1]; 2757 AvARRAY (av)[0] = AvARRAY (av)[1];
2570 AvARRAY (av)[1] = cb; 2758 AvARRAY (av)[1] = cb;
2571 2759
2572 cb = av_shift (av); 2760 cb = av_shift (av);
2573 2761
2762 if (SvTYPE (cb) == SVt_PVCV)
2763 {
2764 dSP;
2765 PUSHMARK (SP);
2766 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2767 PUTBACK;
2768 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2769 }
2770 else
2771 {
2574 api_ready (aTHX_ cb); 2772 api_ready (aTHX_ cb);
2575 sv_setiv (cb, 0); /* signal waiter */ 2773 sv_setiv (cb, 0); /* signal waiter */
2774 }
2775
2576 SvREFCNT_dec (cb); 2776 SvREFCNT_dec (cb);
2577 2777
2578 --count; 2778 --count;
2579 } 2779 }
2580} 2780}
2589static void 2789static void
2590slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2790slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2591{ 2791{
2592 AV *av = (AV *)SvRV (arg [0]); 2792 AV *av = (AV *)SvRV (arg [0]);
2593 2793
2794 if (items >= 2)
2795 {
2796 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2797 av_push (av, SvREFCNT_inc_NN (cb_cv));
2798
2594 if (SvIVX (AvARRAY (av)[0])) 2799 if (SvIVX (AvARRAY (av)[0]))
2800 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2801
2802 frame->prepare = prepare_nop;
2803 frame->check = slf_check_nop;
2804 }
2805 else if (SvIVX (AvARRAY (av)[0]))
2595 { 2806 {
2596 SvIVX (AvARRAY (av)[0]) = 0; 2807 SvIVX (AvARRAY (av)[0]) = 0;
2597 frame->prepare = prepare_nop; 2808 frame->prepare = prepare_nop;
2598 frame->check = slf_check_nop; 2809 frame->check = slf_check_nop;
2599 } 2810 }
2600 else 2811 else
2601 { 2812 {
2602 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2813 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2603 2814
2604 av_push (av, waiter); 2815 av_push (av, waiter);
2605 2816
2606 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2817 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2607 frame->prepare = prepare_schedule; 2818 frame->prepare = prepare_schedule;
2846 3057
2847 if (!svp) croak ("Time::HiRes is required"); 3058 if (!svp) croak ("Time::HiRes is required");
2848 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 3059 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2849 3060
2850 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 3061 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3062
3063 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3064 u2time = INT2PTR (double (*)(), SvIV (*svp));
2851 } 3065 }
2852 3066
2853 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 3067 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2854} 3068}
2855 3069
2856SV * 3070SV *
2857new (char *klass, ...) 3071new (char *klass, ...)
2858 ALIAS: 3072 ALIAS:
3005 eval = 1 3219 eval = 1
3006 CODE: 3220 CODE:
3007{ 3221{
3008 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3222 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3009 { 3223 {
3010 struct coro temp; 3224 struct coro *current = SvSTATE_current;
3011 3225
3012 if (!(coro->flags & CF_RUNNING)) 3226 if (current != coro)
3013 { 3227 {
3014 PUTBACK; 3228 PUTBACK;
3015 save_perl (aTHX_ &temp); 3229 save_perl (aTHX_ current);
3016 load_perl (aTHX_ coro); 3230 load_perl (aTHX_ coro);
3231 SPAGAIN;
3017 } 3232 }
3018 3233
3019 {
3020 dSP;
3021 ENTER;
3022 SAVETMPS;
3023 PUTBACK;
3024 PUSHSTACK; 3234 PUSHSTACK;
3235
3025 PUSHMARK (SP); 3236 PUSHMARK (SP);
3237 PUTBACK;
3026 3238
3027 if (ix) 3239 if (ix)
3028 eval_sv (coderef, 0); 3240 eval_sv (coderef, 0);
3029 else 3241 else
3030 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3242 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3031 3243
3032 POPSTACK; 3244 POPSTACK;
3033 SPAGAIN; 3245 SPAGAIN;
3034 FREETMPS;
3035 LEAVE;
3036 PUTBACK;
3037 }
3038 3246
3039 if (!(coro->flags & CF_RUNNING)) 3247 if (current != coro)
3040 { 3248 {
3249 PUTBACK;
3041 save_perl (aTHX_ coro); 3250 save_perl (aTHX_ coro);
3042 load_perl (aTHX_ &temp); 3251 load_perl (aTHX_ current);
3043 SPAGAIN; 3252 SPAGAIN;
3044 } 3253 }
3045 } 3254 }
3046} 3255}
3047 3256
3051 ALIAS: 3260 ALIAS:
3052 is_ready = CF_READY 3261 is_ready = CF_READY
3053 is_running = CF_RUNNING 3262 is_running = CF_RUNNING
3054 is_new = CF_NEW 3263 is_new = CF_NEW
3055 is_destroyed = CF_DESTROYED 3264 is_destroyed = CF_DESTROYED
3265 is_suspended = CF_SUSPENDED
3056 CODE: 3266 CODE:
3057 RETVAL = boolSV (coro->flags & ix); 3267 RETVAL = boolSV (coro->flags & ix);
3058 OUTPUT: 3268 OUTPUT:
3059 RETVAL 3269 RETVAL
3060 3270
3064 CODE: 3274 CODE:
3065{ 3275{
3066 struct coro *current = SvSTATE_current; 3276 struct coro *current = SvSTATE_current;
3067 SV **throwp = self == current ? &CORO_THROW : &self->except; 3277 SV **throwp = self == current ? &CORO_THROW : &self->except;
3068 SvREFCNT_dec (*throwp); 3278 SvREFCNT_dec (*throwp);
3279 SvGETMAGIC (throw);
3069 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3280 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
3070} 3281}
3071 3282
3072void 3283void
3073api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3284api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3125 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3336 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3126 3337
3127 SV *tmp = *src; *src = *dst; *dst = tmp; 3338 SV *tmp = *src; *src = *dst; *dst = tmp;
3128 } 3339 }
3129 3340
3341void
3342cancel (Coro::State self)
3343 CODE:
3344 coro_state_destroy (aTHX_ self);
3345 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3346
3347
3348SV *
3349enable_times (int enabled = enable_times)
3350 CODE:
3351{
3352 RETVAL = boolSV (enable_times);
3353
3354 if (enabled != enable_times)
3355 {
3356 enable_times = enabled;
3357
3358 coro_times_update ();
3359 (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current));
3360 }
3361}
3362 OUTPUT:
3363 RETVAL
3364
3365void
3366times (Coro::State self)
3367 PPCODE:
3368{
3369 struct coro *current = SvSTATE (coro_current);
3370
3371 if (expect_false (current == self))
3372 {
3373 coro_times_update ();
3374 coro_times_add (SvSTATE (coro_current));
3375 }
3376
3377 EXTEND (SP, 2);
3378 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3379 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3380
3381 if (expect_false (current == self))
3382 coro_times_sub (SvSTATE (coro_current));
3383}
3130 3384
3131MODULE = Coro::State PACKAGE = Coro 3385MODULE = Coro::State PACKAGE = Coro
3132 3386
3133BOOT: 3387BOOT:
3134{ 3388{
3135 int i;
3136
3137 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3389 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3138 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3390 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3139 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3391 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3140 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3392 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3141 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3393 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3149 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3401 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3150 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3402 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3151 3403
3152 coro_stash = gv_stashpv ("Coro", TRUE); 3404 coro_stash = gv_stashpv ("Coro", TRUE);
3153 3405
3154 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3406 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3155 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3407 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3156 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3408 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3157 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3409 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW));
3158 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3410 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE));
3159 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3411 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN));
3160
3161 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3162 coro_ready[i] = newAV ();
3163 3412
3164 { 3413 {
3165 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3414 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3166 3415
3167 coroapi.schedule = api_schedule; 3416 coroapi.schedule = api_schedule;
3208cede_notself (...) 3457cede_notself (...)
3209 CODE: 3458 CODE:
3210 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3459 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3211 3460
3212void 3461void
3213_cancel (Coro::State self)
3214 CODE:
3215 coro_state_destroy (aTHX_ self);
3216 coro_call_on_destroy (aTHX_ self);
3217
3218void
3219_set_current (SV *current) 3462_set_current (SV *current)
3220 PROTOTYPE: $ 3463 PROTOTYPE: $
3221 CODE: 3464 CODE:
3222 SvREFCNT_dec (SvRV (coro_current)); 3465 SvREFCNT_dec (SvRV (coro_current));
3223 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3466 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3225void 3468void
3226_set_readyhook (SV *hook) 3469_set_readyhook (SV *hook)
3227 PROTOTYPE: $ 3470 PROTOTYPE: $
3228 CODE: 3471 CODE:
3229 SvREFCNT_dec (coro_readyhook); 3472 SvREFCNT_dec (coro_readyhook);
3473 SvGETMAGIC (hook);
3230 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3474 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
3231 3475
3232int 3476int
3233prio (Coro::State coro, int newprio = 0) 3477prio (Coro::State coro, int newprio = 0)
3234 PROTOTYPE: $;$ 3478 PROTOTYPE: $;$
3241 if (items > 1) 3485 if (items > 1)
3242 { 3486 {
3243 if (ix) 3487 if (ix)
3244 newprio = coro->prio - newprio; 3488 newprio = coro->prio - newprio;
3245 3489
3246 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 3490 if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN;
3247 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 3491 if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX;
3248 3492
3249 coro->prio = newprio; 3493 coro->prio = newprio;
3250 } 3494 }
3251} 3495}
3252 OUTPUT: 3496 OUTPUT:
3265 PROTOTYPE: 3509 PROTOTYPE:
3266 CODE: 3510 CODE:
3267 RETVAL = coro_nready; 3511 RETVAL = coro_nready;
3268 OUTPUT: 3512 OUTPUT:
3269 RETVAL 3513 RETVAL
3514
3515void
3516suspend (Coro::State self)
3517 PROTOTYPE: $
3518 CODE:
3519 self->flags |= CF_SUSPENDED;
3520
3521void
3522resume (Coro::State self)
3523 PROTOTYPE: $
3524 CODE:
3525 self->flags &= ~CF_SUSPENDED;
3270 3526
3271void 3527void
3272_pool_handler (...) 3528_pool_handler (...)
3273 CODE: 3529 CODE:
3274 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3530 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3329rouse_wait (...) 3585rouse_wait (...)
3330 PROTOTYPE: ;$ 3586 PROTOTYPE: ;$
3331 PPCODE: 3587 PPCODE:
3332 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3588 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3333 3589
3590void
3591on_enter (SV *block)
3592 ALIAS:
3593 on_leave = 1
3594 PROTOTYPE: &
3595 CODE:
3596{
3597 struct coro *coro = SvSTATE_current;
3598 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3599
3600 block = (SV *)coro_sv_2cv (aTHX_ block);
3601
3602 if (!*avp)
3603 *avp = newAV ();
3604
3605 av_push (*avp, SvREFCNT_inc (block));
3606
3607 if (!ix)
3608 on_enterleave_call (aTHX_ block);
3609
3610 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3611 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3612 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3613}
3614
3334 3615
3335MODULE = Coro::State PACKAGE = PerlIO::cede 3616MODULE = Coro::State PACKAGE = PerlIO::cede
3336 3617
3337BOOT: 3618BOOT:
3338 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3619 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3341MODULE = Coro::State PACKAGE = Coro::Semaphore 3622MODULE = Coro::State PACKAGE = Coro::Semaphore
3342 3623
3343SV * 3624SV *
3344new (SV *klass, SV *count = 0) 3625new (SV *klass, SV *count = 0)
3345 CODE: 3626 CODE:
3627{
3628 int semcnt = 1;
3629
3630 if (count)
3631 {
3632 SvGETMAGIC (count);
3633
3634 if (SvOK (count))
3635 semcnt = SvIV (count);
3636 }
3637
3346 RETVAL = sv_bless ( 3638 RETVAL = sv_bless (
3347 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), 3639 coro_waitarray_new (aTHX_ semcnt),
3348 GvSTASH (CvGV (cv)) 3640 GvSTASH (CvGV (cv))
3349 ); 3641 );
3642}
3350 OUTPUT: 3643 OUTPUT:
3351 RETVAL 3644 RETVAL
3352 3645
3353# helper for Coro::Channel 3646# helper for Coro::Channel and others
3354SV * 3647SV *
3355_alloc (int count) 3648_alloc (int count)
3356 CODE: 3649 CODE:
3357 RETVAL = coro_waitarray_new (aTHX_ count); 3650 RETVAL = coro_waitarray_new (aTHX_ count);
3358 OUTPUT: 3651 OUTPUT:
3416 for (i = 1; i <= wcount; ++i) 3709 for (i = 1; i <= wcount; ++i)
3417 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3710 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3418 } 3711 }
3419} 3712}
3420 3713
3714MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3715
3716void
3717_may_delete (SV *sem, int count, int extra_refs)
3718 PPCODE:
3719{
3720 AV *av = (AV *)SvRV (sem);
3721
3722 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3723 && AvFILLp (av) == 0 /* no waiters, just count */
3724 && SvIV (AvARRAY (av)[0]) == count)
3725 XSRETURN_YES;
3726
3727 XSRETURN_NO;
3728}
3729
3421MODULE = Coro::State PACKAGE = Coro::Signal 3730MODULE = Coro::State PACKAGE = Coro::Signal
3422 3731
3423SV * 3732SV *
3424new (SV *klass) 3733new (SV *klass)
3425 CODE: 3734 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines