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.231 by root, Thu Apr 24 10:31:59 2008 UTC vs.
Revision 1.252 by root, Tue Nov 4 12:08:44 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
15 16
16#ifdef WIN32 17#ifdef WIN32
17# undef setjmp 18# undef setjmp
18# undef longjmp 19# undef longjmp
19# undef _exit 20# undef _exit
20#include <setjmp.h> 21# define setjmp _setjmp // deep magic, don't ask
21#else 22#else
22# include <inttypes.h> /* most portable stdint.h */ 23# include <inttypes.h> /* most portable stdint.h */
23#endif 24#endif
24 25
25#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
80# ifndef IS_PADCONST 81# ifndef IS_PADCONST
81# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
82# endif 83# endif
83#endif 84#endif
84 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
85/* 5.8.8 */ 96/* 5.8.8 */
86#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
87# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
88#endif 99#endif
89#ifndef newSV 100#ifndef newSV
90# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
91#endif
92
93/* 5.11 */
94#ifndef CxHASARGS
95# define CxHASARGS(cx) (cx)->blk_sub.hasargs
96#endif 102#endif
97 103
98/* 5.8.7 */ 104/* 5.8.7 */
99#ifndef SvRV_set 105#ifndef SvRV_set
100# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
147#endif 153#endif
148 154
149/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
150struct io_state 156struct io_state
151{ 157{
158 AV *res;
152 int errorno; 159 int errorno;
153 I32 laststype; 160 I32 laststype;
154 int laststatval; 161 int laststatval;
155 Stat_t statcache; 162 Stat_t statcache;
156}; 163};
157 164
165static double (*nvtime)(); /* so why doesn't it take void? */
166
158static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 170static JMPENV *main_top_env;
162static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
163static volatile SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
164 173
165static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
166static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
167static SV *rv_diehook; 176static SV *rv_diehook;
168static SV *rv_warnhook; 177static SV *rv_warnhook;
170 179
171/* async_pool helper stuff */ 180/* async_pool helper stuff */
172static SV *sv_pool_rss; 181static SV *sv_pool_rss;
173static SV *sv_pool_size; 182static SV *sv_pool_size;
174static AV *av_async_pool; 183static AV *av_async_pool;
184
185/* Coro::AnyEvent */
186static SV *sv_activity;
175 187
176static struct coro_cctx *cctx_first; 188static struct coro_cctx *cctx_first;
177static int cctx_count, cctx_idle; 189static int cctx_count, cctx_idle;
178 190
179enum { 191enum {
265#define PRIO_IDLE -3 277#define PRIO_IDLE -3
266#define PRIO_MIN -4 278#define PRIO_MIN -4
267 279
268/* for Coro.pm */ 280/* for Coro.pm */
269static SV *coro_current; 281static SV *coro_current;
282static SV *coro_readyhook;
270static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
271static int coro_nready; 284static int coro_nready;
272static struct coro *coro_first; 285static struct coro *coro_first;
273 286
274/** lowlevel stuff **********************************************************/ 287/** lowlevel stuff **********************************************************/
317 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
318#endif 331#endif
319 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
320 --AvFILLp (padlist); 333 --AvFILLp (padlist);
321 334
322 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
323 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
324 337
325 return newpadlist; 338 return newpadlist;
326} 339}
327 340
357 370
358 /* casting is fun. */ 371 /* casting is fun. */
359 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
360 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
361 374
375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
376
362 return 0; 377 return 0;
363} 378}
364 379
365#define CORO_MAGIC_type_cv PERL_MAGIC_ext 380#define CORO_MAGIC_type_cv PERL_MAGIC_ext
366#define CORO_MAGIC_type_state PERL_MAGIC_ext 381#define CORO_MAGIC_type_state PERL_MAGIC_ext
416 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
417 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
418 else 433 else
419 { 434 {
420#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
421 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
422 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
423 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
424 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
425 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
426#else 442#else
510 526
511 if (expect_true (CvDEPTH (cv))) 527 if (expect_true (CvDEPTH (cv)))
512 { 528 {
513 EXTEND (SP, 3); 529 EXTEND (SP, 3);
514 PUSHs ((SV *)CvPADLIST (cv)); 530 PUSHs ((SV *)CvPADLIST (cv));
515 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 531 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
516 PUSHs ((SV *)cv); 532 PUSHs ((SV *)cv);
517 533
518 CvDEPTH (cv) = 0; 534 CvDEPTH (cv) = 0;
519 get_padlist (aTHX_ cv); 535 get_padlist (aTHX_ cv);
520 } 536 }
659 #undef VAR 675 #undef VAR
660 } 676 }
661 else 677 else
662 slot = coro->slot; 678 slot = coro->slot;
663 679
680 if (slot)
681 {
664 rss += sizeof (slot->curstackinfo); 682 rss += sizeof (slot->curstackinfo);
665 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 683 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
666 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 684 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
667 rss += slot->tmps_max * sizeof (SV *); 685 rss += slot->tmps_max * sizeof (SV *);
668 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 686 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
669 rss += slot->scopestack_max * sizeof (I32); 687 rss += slot->scopestack_max * sizeof (I32);
670 rss += slot->savestack_max * sizeof (ANY); 688 rss += slot->savestack_max * sizeof (ANY);
671 689
672#if !PERL_VERSION_ATLEAST (5,10,0) 690#if !PERL_VERSION_ATLEAST (5,10,0)
673 rss += slot->retstack_max * sizeof (OP *); 691 rss += slot->retstack_max * sizeof (OP *);
674#endif 692#endif
693 }
675 } 694 }
676 695
677 return rss; 696 return rss;
678} 697}
679 698
680/** coroutine stack handling ************************************************/ 699/** coroutine stack handling ************************************************/
681 700
682static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
683static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 702static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
703static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
704
705/* apparently < 5.8.8 */
706#ifndef MgPV_nolen_const
707#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
708 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
709 (const char*)(mg)->mg_ptr)
710#endif
684 711
685/* 712/*
686 * This overrides the default magic get method of %SIG elements. 713 * This overrides the default magic get method of %SIG elements.
687 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 714 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
688 * and instead of tryign to save and restore the hash elements, we just provide 715 * and instead of tryign to save and restore the hash elements, we just provide
696{ 723{
697 const char *s = MgPV_nolen_const (mg); 724 const char *s = MgPV_nolen_const (mg);
698 725
699 if (*s == '_') 726 if (*s == '_')
700 { 727 {
701 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; 728 SV **svp = 0;
702 if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; 729
730 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
731 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
732
733 if (svp)
734 {
735 sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
736 return 0;
737 }
703 } 738 }
704 739
705 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 740 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
741}
742
743static int
744coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
745{
746 const char *s = MgPV_nolen_const (mg);
747
748 if (*s == '_')
749 {
750 SV **svp = 0;
751
752 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
753 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
754
755 if (svp)
756 {
757 SV *old = *svp;
758 *svp = 0;
759 SvREFCNT_dec (old);
760 return 0;
761 }
762 }
763
764 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
706} 765}
707 766
708static int 767static int
709coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 768coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
710{ 769{
757 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
758 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
759 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
760 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 819 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
761 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
762 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
763 822
764 { 823 {
765 dSP; 824 dSP;
766 LOGOP myop; 825 LOGOP myop;
767 826
863 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
864 : bot; 923 : bot;
865 924
866 av_extend (av, top - bot); 925 av_extend (av, top - bot);
867 while (bot < top) 926 while (bot < top)
868 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
869 928
870 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
871 ENTER; 930 ENTER;
872 SAVETMPS; 931 SAVETMPS;
873 EXTEND (SP, 3); 932 EXTEND (SP, 3);
1149/* always use the TRANSFER macro */ 1208/* always use the TRANSFER macro */
1150static void NOINLINE 1209static void NOINLINE
1151transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1210transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1152{ 1211{
1153 dSTACKLEVEL; 1212 dSTACKLEVEL;
1154 static volatile int has_throw;
1155 1213
1156 /* sometimes transfer is only called to set idle_sp */ 1214 /* sometimes transfer is only called to set idle_sp */
1157 if (expect_false (!next)) 1215 if (expect_false (!next))
1158 { 1216 {
1159 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1217 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1160 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1218 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1161 } 1219 }
1162 else if (expect_true (prev != next)) 1220 else if (expect_true (prev != next))
1163 { 1221 {
1222 static volatile int has_throw;
1164 coro_cctx *prev__cctx; 1223 coro_cctx *prev__cctx;
1165 1224
1166 if (expect_false (prev->flags & CF_NEW)) 1225 if (expect_false (prev->flags & CF_NEW))
1167 { 1226 {
1168 /* create a new empty context */ 1227 /* create a new empty context */
1379static int 1438static int
1380api_ready (SV *coro_sv) 1439api_ready (SV *coro_sv)
1381{ 1440{
1382 dTHX; 1441 dTHX;
1383 struct coro *coro; 1442 struct coro *coro;
1443 SV *sv_hook;
1444 void (*xs_hook)(void);
1384 1445
1385 if (SvROK (coro_sv)) 1446 if (SvROK (coro_sv))
1386 coro_sv = SvRV (coro_sv); 1447 coro_sv = SvRV (coro_sv);
1387 1448
1388 coro = SvSTATE (coro_sv); 1449 coro = SvSTATE (coro_sv);
1391 return 0; 1452 return 0;
1392 1453
1393 coro->flags |= CF_READY; 1454 coro->flags |= CF_READY;
1394 1455
1395 LOCK; 1456 LOCK;
1457
1458 sv_hook = coro_nready ? 0 : coro_readyhook;
1459 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1460
1396 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1461 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1397 ++coro_nready; 1462 ++coro_nready;
1463
1398 UNLOCK; 1464 UNLOCK;
1465
1466 if (sv_hook)
1467 {
1468 dSP;
1469
1470 ENTER;
1471 SAVETMPS;
1472
1473 PUSHMARK (SP);
1474 PUTBACK;
1475 call_sv (sv_hook, G_DISCARD);
1476 SPAGAIN;
1477
1478 FREETMPS;
1479 LEAVE;
1480 }
1481
1482 if (xs_hook)
1483 xs_hook ();
1399 1484
1400 return 1; 1485 return 1;
1401} 1486}
1402 1487
1403static int 1488static int
1553 else 1638 else
1554 coro->slot->runops = RUNOPS_DEFAULT; 1639 coro->slot->runops = RUNOPS_DEFAULT;
1555 } 1640 }
1556} 1641}
1557 1642
1643static int
1644coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1645{
1646 AV *padlist;
1647 AV *av = (AV *)mg->mg_obj;
1648
1649 abort ();
1650
1651 return 0;
1652}
1653
1654static MGVTBL coro_gensub_vtbl = {
1655 0, 0, 0, 0,
1656 coro_gensub_free
1657};
1658
1659/*****************************************************************************/
1660/* PerlIO::cede */
1661
1662typedef struct
1663{
1664 PerlIOBuf base;
1665 NV next, every;
1666} PerlIOCede;
1667
1668static IV
1669PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1670{
1671 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1672
1673 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1674 self->next = nvtime () + self->every;
1675
1676 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1677}
1678
1679static SV *
1680PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1681{
1682 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1683
1684 return newSVnv (self->every);
1685}
1686
1687static IV
1688PerlIOCede_flush (pTHX_ PerlIO *f)
1689{
1690 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1691 double now = nvtime ();
1692
1693 if (now >= self->next)
1694 {
1695 api_cede ();
1696 self->next = now + self->every;
1697 }
1698
1699 return PerlIOBuf_flush (aTHX_ f);
1700}
1701
1702static PerlIO_funcs PerlIO_cede =
1703{
1704 sizeof(PerlIO_funcs),
1705 "cede",
1706 sizeof(PerlIOCede),
1707 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1708 PerlIOCede_pushed,
1709 PerlIOBuf_popped,
1710 PerlIOBuf_open,
1711 PerlIOBase_binmode,
1712 PerlIOCede_getarg,
1713 PerlIOBase_fileno,
1714 PerlIOBuf_dup,
1715 PerlIOBuf_read,
1716 PerlIOBuf_unread,
1717 PerlIOBuf_write,
1718 PerlIOBuf_seek,
1719 PerlIOBuf_tell,
1720 PerlIOBuf_close,
1721 PerlIOCede_flush,
1722 PerlIOBuf_fill,
1723 PerlIOBase_eof,
1724 PerlIOBase_error,
1725 PerlIOBase_clearerr,
1726 PerlIOBase_setlinebuf,
1727 PerlIOBuf_get_base,
1728 PerlIOBuf_bufsiz,
1729 PerlIOBuf_get_ptr,
1730 PerlIOBuf_get_cnt,
1731 PerlIOBuf_set_ptrcnt,
1732};
1733
1734
1558MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1735MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1559 1736
1560PROTOTYPES: DISABLE 1737PROTOTYPES: DISABLE
1561 1738
1562BOOT: 1739BOOT:
1567 BOOT_PAGESIZE; 1744 BOOT_PAGESIZE;
1568 1745
1569 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1746 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1570 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1747 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1571 1748
1572 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 1749 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1573 PL_vtbl_sigelem.svt_get = coro_sigelem_get; 1750 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1574 orig_sigelem_set = PL_vtbl_sigelem.svt_set; 1751 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1575 PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1576 1752
1577 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1753 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1578 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 1754 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1579 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 1755 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1580 1756
1589 main_top_env = PL_top_env; 1765 main_top_env = PL_top_env;
1590 1766
1591 while (main_top_env->je_prev) 1767 while (main_top_env->je_prev)
1592 main_top_env = main_top_env->je_prev; 1768 main_top_env = main_top_env->je_prev;
1593 1769
1594 coroapi.ver = CORO_API_VERSION; 1770 coroapi.ver = CORO_API_VERSION;
1595 coroapi.rev = CORO_API_REVISION; 1771 coroapi.rev = CORO_API_REVISION;
1596 coroapi.transfer = api_transfer; 1772 coroapi.transfer = api_transfer;
1773
1774 {
1775 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1776
1777 if (!svp) croak ("Time::HiRes is required");
1778 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1779
1780 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1781 }
1597 1782
1598 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1783 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1599} 1784}
1600 1785
1601SV * 1786SV *
1649 ta.next = 0; 1834 ta.next = 0;
1650 break; 1835 break;
1651 1836
1652 case 1: 1837 case 1:
1653 if (items != 2) 1838 if (items != 2)
1654 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1839 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1655 1840
1656 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1841 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1657 break; 1842 break;
1658 1843
1659 case 2: 1844 case 2:
1685 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1870 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1686 OUTPUT: 1871 OUTPUT:
1687 RETVAL 1872 RETVAL
1688 1873
1689void 1874void
1690_exit (code) 1875_exit (int code)
1691 int code
1692 PROTOTYPE: $ 1876 PROTOTYPE: $
1693 CODE: 1877 CODE:
1694 _exit (code); 1878 _exit (code);
1695 1879
1696int 1880int
1730call (Coro::State coro, SV *coderef) 1914call (Coro::State coro, SV *coderef)
1731 ALIAS: 1915 ALIAS:
1732 eval = 1 1916 eval = 1
1733 CODE: 1917 CODE:
1734{ 1918{
1735 if (coro->mainstack) 1919 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1736 { 1920 {
1737 struct coro temp; 1921 struct coro temp;
1738 1922
1739 if (!(coro->flags & CF_RUNNING)) 1923 if (!(coro->flags & CF_RUNNING))
1740 { 1924 {
1787 1971
1788void 1972void
1789api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1973api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1790 1974
1791SV * 1975SV *
1792has_stack (Coro::State coro) 1976has_cctx (Coro::State coro)
1793 PROTOTYPE: $ 1977 PROTOTYPE: $
1794 CODE: 1978 CODE:
1795 RETVAL = boolSV (!!coro->cctx); 1979 RETVAL = boolSV (!!coro->cctx);
1796 OUTPUT: 1980 OUTPUT:
1797 RETVAL 1981 RETVAL
1822force_cctx () 2006force_cctx ()
1823 CODE: 2007 CODE:
1824 struct coro *coro = SvSTATE (coro_current); 2008 struct coro *coro = SvSTATE (coro_current);
1825 coro->cctx->idle_sp = 0; 2009 coro->cctx->idle_sp = 0;
1826 2010
1827MODULE = Coro::State PACKAGE = Coro
1828
1829BOOT:
1830{
1831 int i;
1832
1833 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1834 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1835 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1836
1837 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1838 SvREADONLY_on (coro_current);
1839
1840 coro_stash = gv_stashpv ("Coro", TRUE);
1841
1842 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1843 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1844 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1845 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1846 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1847 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1848
1849 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1850 coro_ready[i] = newAV ();
1851
1852 {
1853 SV *sv = perl_get_sv ("Coro::API", TRUE);
1854 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1855
1856 coroapi.schedule = api_schedule;
1857 coroapi.cede = api_cede;
1858 coroapi.cede_notself = api_cede_notself;
1859 coroapi.ready = api_ready;
1860 coroapi.is_ready = api_is_ready;
1861 coroapi.nready = &coro_nready;
1862 coroapi.current = coro_current;
1863
1864 GCoroAPI = &coroapi;
1865 sv_setiv (sv, (IV)&coroapi);
1866 SvREADONLY_on (sv);
1867 }
1868}
1869
1870void
1871_set_current (SV *current)
1872 PROTOTYPE: $
1873 CODE:
1874 SvREFCNT_dec (SvRV (coro_current));
1875 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1876
1877int
1878prio (Coro::State coro, int newprio = 0)
1879 ALIAS:
1880 nice = 1
1881 CODE:
1882{
1883 RETVAL = coro->prio;
1884
1885 if (items > 1)
1886 {
1887 if (ix)
1888 newprio = coro->prio - newprio;
1889
1890 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1891 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1892
1893 coro->prio = newprio;
1894 }
1895}
1896 OUTPUT:
1897 RETVAL
1898
1899SV *
1900ready (SV *self)
1901 PROTOTYPE: $
1902 CODE:
1903 RETVAL = boolSV (api_ready (self));
1904 OUTPUT:
1905 RETVAL
1906
1907int
1908nready (...)
1909 PROTOTYPE:
1910 CODE:
1911 RETVAL = coro_nready;
1912 OUTPUT:
1913 RETVAL
1914
1915void
1916throw (Coro::State self, SV *throw = &PL_sv_undef)
1917 PROTOTYPE: $;$
1918 CODE:
1919 SvREFCNT_dec (self->throw);
1920 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1921
1922void 2011void
1923swap_defsv (Coro::State self) 2012swap_defsv (Coro::State self)
1924 PROTOTYPE: $ 2013 PROTOTYPE: $
1925 ALIAS: 2014 ALIAS:
1926 swap_defav = 1 2015 swap_defav = 1
1933 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2022 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
1934 2023
1935 SV *tmp = *src; *src = *dst; *dst = tmp; 2024 SV *tmp = *src; *src = *dst; *dst = tmp;
1936 } 2025 }
1937 2026
2027MODULE = Coro::State PACKAGE = Coro
2028
2029BOOT:
2030{
2031 int i;
2032
2033 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2034 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2035 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2036
2037 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2038 SvREADONLY_on (coro_current);
2039
2040 coro_stash = gv_stashpv ("Coro", TRUE);
2041
2042 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2043 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2044 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2045 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2046 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2047 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2048
2049 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2050 coro_ready[i] = newAV ();
2051
2052 {
2053 SV *sv = perl_get_sv ("Coro::API", TRUE);
2054 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2055
2056 coroapi.schedule = api_schedule;
2057 coroapi.cede = api_cede;
2058 coroapi.cede_notself = api_cede_notself;
2059 coroapi.ready = api_ready;
2060 coroapi.is_ready = api_is_ready;
2061 coroapi.nready = &coro_nready;
2062 coroapi.current = coro_current;
2063
2064 GCoroAPI = &coroapi;
2065 sv_setiv (sv, (IV)&coroapi);
2066 SvREADONLY_on (sv);
2067 }
2068}
2069
2070void
2071_set_current (SV *current)
2072 PROTOTYPE: $
2073 CODE:
2074 SvREFCNT_dec (SvRV (coro_current));
2075 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2076
2077void
2078_set_readyhook (SV *hook)
2079 PROTOTYPE: $
2080 CODE:
2081 LOCK;
2082 SvREFCNT_dec (coro_readyhook);
2083 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2084 UNLOCK;
2085
2086int
2087prio (Coro::State coro, int newprio = 0)
2088 ALIAS:
2089 nice = 1
2090 CODE:
2091{
2092 RETVAL = coro->prio;
2093
2094 if (items > 1)
2095 {
2096 if (ix)
2097 newprio = coro->prio - newprio;
2098
2099 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2100 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2101
2102 coro->prio = newprio;
2103 }
2104}
2105 OUTPUT:
2106 RETVAL
2107
2108SV *
2109ready (SV *self)
2110 PROTOTYPE: $
2111 CODE:
2112 RETVAL = boolSV (api_ready (self));
2113 OUTPUT:
2114 RETVAL
2115
2116int
2117nready (...)
2118 PROTOTYPE:
2119 CODE:
2120 RETVAL = coro_nready;
2121 OUTPUT:
2122 RETVAL
2123
2124void
2125throw (Coro::State self, SV *throw = &PL_sv_undef)
2126 PROTOTYPE: $;$
2127 CODE:
2128 SvREFCNT_dec (self->throw);
2129 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2130
1938# for async_pool speedup 2131# for async_pool speedup
1939void 2132void
1940_pool_1 (SV *cb) 2133_pool_1 (SV *cb)
1941 CODE: 2134 CODE:
1942{ 2135{
1947 AV *invoke_av; 2140 AV *invoke_av;
1948 int i, len; 2141 int i, len;
1949 2142
1950 if (!invoke) 2143 if (!invoke)
1951 { 2144 {
1952 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2145 SV *old = PL_diehook;
2146 PL_diehook = 0;
2147 SvREFCNT_dec (old);
1953 croak ("\3async_pool terminate\2\n"); 2148 croak ("\3async_pool terminate\2\n");
1954 } 2149 }
1955 2150
1956 SvREFCNT_dec (coro->saved_deffh); 2151 SvREFCNT_dec (coro->saved_deffh);
1957 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2152 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1958 2153
1959 hv_store (hv, "desc", sizeof ("desc") - 1, 2154 hv_store (hv, "desc", sizeof ("desc") - 1,
1960 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2155 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1961 2156
1962 invoke_av = (AV *)SvRV (invoke); 2157 invoke_av = (AV *)SvRV (invoke);
1966 2161
1967 if (len > 0) 2162 if (len > 0)
1968 { 2163 {
1969 av_fill (defav, len - 1); 2164 av_fill (defav, len - 1);
1970 for (i = 0; i < len; ++i) 2165 for (i = 0; i < len; ++i)
1971 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2166 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1972 } 2167 }
1973 2168
1974 SvREFCNT_dec (invoke); 2169 SvREFCNT_dec (invoke);
1975} 2170}
1976 2171
1986 coro->saved_deffh = 0; 2181 coro->saved_deffh = 0;
1987 2182
1988 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2183 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1989 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2184 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1990 { 2185 {
1991 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2186 SV *old = PL_diehook;
2187 PL_diehook = 0;
2188 SvREFCNT_dec (old);
1992 croak ("\3async_pool terminate\2\n"); 2189 croak ("\3async_pool terminate\2\n");
1993 } 2190 }
1994 2191
1995 av_clear (GvAV (PL_defgv)); 2192 av_clear (GvAV (PL_defgv));
1996 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2193 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
2002 api_trace (coro_current, 0); 2199 api_trace (coro_current, 0);
2003 2200
2004 av_push (av_async_pool, newSVsv (coro_current)); 2201 av_push (av_async_pool, newSVsv (coro_current));
2005} 2202}
2006 2203
2204#if 0
2205
2206void
2207_generator_call (...)
2208 PROTOTYPE: @
2209 PPCODE:
2210 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2211 xxxx
2212 abort ();
2213
2214SV *
2215gensub (SV *sub, ...)
2216 PROTOTYPE: &;@
2217 CODE:
2218{
2219 struct coro *coro;
2220 MAGIC *mg;
2221 CV *xcv;
2222 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2223 int i;
2224
2225 CvGV (ncv) = CvGV (cv);
2226 CvFILE (ncv) = CvFILE (cv);
2227
2228 Newz (0, coro, 1, struct coro);
2229 coro->args = newAV ();
2230 coro->flags = CF_NEW;
2231
2232 av_extend (coro->args, items - 1);
2233 for (i = 1; i < items; i++)
2234 av_push (coro->args, newSVsv (ST (i)));
2235
2236 CvISXSUB_on (ncv);
2237 CvXSUBANY (ncv).any_ptr = (void *)coro;
2238
2239 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2240
2241 CvXSUB (ncv) = CvXSUB (xcv);
2242 CvANON_on (ncv);
2243
2244 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2245 RETVAL = newRV_noinc ((SV *)ncv);
2246}
2247 OUTPUT:
2248 RETVAL
2249
2250#endif
2251
2007 2252
2008MODULE = Coro::State PACKAGE = Coro::AIO 2253MODULE = Coro::State PACKAGE = Coro::AIO
2009 2254
2010SV * 2255void
2011_get_state () 2256_get_state (SV *self)
2012 CODE: 2257 PPCODE:
2013{ 2258{
2014 struct io_state *data; 2259 AV *defav = GvAV (PL_defgv);
2015 2260 AV *av = newAV ();
2261 int i;
2016 RETVAL = newSV (sizeof (struct io_state)); 2262 SV *data_sv = newSV (sizeof (struct io_state));
2017 data = (struct io_state *)SvPVX (RETVAL); 2263 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2018 SvCUR_set (RETVAL, sizeof (struct io_state)); 2264 SvCUR_set (data_sv, sizeof (struct io_state));
2019 SvPOK_only (RETVAL); 2265 SvPOK_only (data_sv);
2020 2266
2021 data->errorno = errno; 2267 data->errorno = errno;
2022 data->laststype = PL_laststype; 2268 data->laststype = PL_laststype;
2023 data->laststatval = PL_laststatval; 2269 data->laststatval = PL_laststatval;
2024 data->statcache = PL_statcache; 2270 data->statcache = PL_statcache;
2271
2272 av_extend (av, AvFILLp (defav) + 1 + 1);
2273
2274 for (i = 0; i <= AvFILLp (defav); ++i)
2275 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2276
2277 av_push (av, data_sv);
2278
2279 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2280
2281 api_ready (self);
2025} 2282}
2026 OUTPUT:
2027 RETVAL
2028 2283
2029void 2284void
2030_set_state (char *data_) 2285_set_state (SV *state)
2031 PROTOTYPE: $ 2286 PROTOTYPE: $
2032 CODE: 2287 PPCODE:
2033{ 2288{
2034 struct io_state *data = (void *)data_; 2289 AV *av = (AV *)SvRV (state);
2290 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2291 int i;
2035 2292
2036 errno = data->errorno; 2293 errno = data->errorno;
2037 PL_laststype = data->laststype; 2294 PL_laststype = data->laststype;
2038 PL_laststatval = data->laststatval; 2295 PL_laststatval = data->laststatval;
2039 PL_statcache = data->statcache; 2296 PL_statcache = data->statcache;
2040}
2041 2297
2298 EXTEND (SP, AvFILLp (av));
2299 for (i = 0; i < AvFILLp (av); ++i)
2300 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2301}
2302
2303
2304MODULE = Coro::State PACKAGE = Coro::AnyEvent
2305
2306BOOT:
2307 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2308
2309SV *
2310_schedule (...)
2311 PROTOTYPE: @
2312 CODE:
2313{
2314 static int incede;
2315
2316 api_cede_notself ();
2317
2318 ++incede;
2319 while (coro_nready >= incede && api_cede ())
2320 ;
2321
2322 sv_setsv (sv_activity, &PL_sv_undef);
2323 if (coro_nready >= incede)
2324 {
2325 PUSHMARK (SP);
2326 PUTBACK;
2327 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2328 SPAGAIN;
2329 }
2330
2331 --incede;
2332}
2333
2334
2335MODULE = Coro::State PACKAGE = PerlIO::cede
2336
2337BOOT:
2338 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines