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.240 by root, Sun Jun 29 00:28:17 2008 UTC vs.
Revision 1.253 by root, Fri Nov 7 20:12:26 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>
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)
136#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
137 143
138#include "CoroAPI.h" 144#include "CoroAPI.h"
139 145
140#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
141static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
142# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
143# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
144#else 150#else
145# define LOCK (void)0 151# define LOCK (void)0
146# define UNLOCK (void)0 152# define UNLOCK (void)0
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;
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 331#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 333 --AvFILLp (padlist);
325 334
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
328 337
329 return newpadlist; 338 return newpadlist;
330} 339}
331 340
361 370
362 /* casting is fun. */ 371 /* casting is fun. */
363 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
364 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
365 374
375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
376
366 return 0; 377 return 0;
367} 378}
368 379
369#define CORO_MAGIC_type_cv PERL_MAGIC_ext 380#define CORO_MAGIC_type_cv PERL_MAGIC_ext
370#define CORO_MAGIC_type_state PERL_MAGIC_ext 381#define CORO_MAGIC_type_state PERL_MAGIC_ext
372static MGVTBL coro_cv_vtbl = { 383static MGVTBL coro_cv_vtbl = {
373 0, 0, 0, 0, 384 0, 0, 0, 0,
374 coro_cv_free 385 coro_cv_free
375}; 386};
376 387
377#define CORO_MAGIC(sv,type) \ 388#define CORO_MAGIC(sv, type) \
378 SvMAGIC (sv) \ 389 SvMAGIC (sv) \
379 ? SvMAGIC (sv)->mg_type == type \ 390 ? SvMAGIC (sv)->mg_type == type \
380 ? SvMAGIC (sv) \ 391 ? SvMAGIC (sv) \
381 : mg_find (sv, type) \ 392 : mg_find (sv, type) \
382 : 0 393 : 0
383 394
384#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
385#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
386 397
387static struct coro * 398static struct coro *
420 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
421 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
422 else 433 else
423 { 434 {
424#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
425 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
426 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
427 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
428 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
429 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
430#else 442#else
616 628
617/* 629/*
618 * destroy the stacks, the callchain etc... 630 * destroy the stacks, the callchain etc...
619 */ 631 */
620static void 632static void
621coro_destroy_stacks (pTHX) 633coro_destruct_stacks (pTHX)
622{ 634{
623 while (PL_curstackinfo->si_next) 635 while (PL_curstackinfo->si_next)
624 PL_curstackinfo = PL_curstackinfo->si_next; 636 PL_curstackinfo = PL_curstackinfo->si_next;
625 637
626 while (PL_curstackinfo) 638 while (PL_curstackinfo)
663 #undef VAR 675 #undef VAR
664 } 676 }
665 else 677 else
666 slot = coro->slot; 678 slot = coro->slot;
667 679
680 if (slot)
681 {
668 rss += sizeof (slot->curstackinfo); 682 rss += sizeof (slot->curstackinfo);
669 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 683 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
670 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 *);
671 rss += slot->tmps_max * sizeof (SV *); 685 rss += slot->tmps_max * sizeof (SV *);
672 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 686 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
673 rss += slot->scopestack_max * sizeof (I32); 687 rss += slot->scopestack_max * sizeof (I32);
674 rss += slot->savestack_max * sizeof (ANY); 688 rss += slot->savestack_max * sizeof (ANY);
675 689
676#if !PERL_VERSION_ATLEAST (5,10,0) 690#if !PERL_VERSION_ATLEAST (5,10,0)
677 rss += slot->retstack_max * sizeof (OP *); 691 rss += slot->retstack_max * sizeof (OP *);
678#endif 692#endif
693 }
679 } 694 }
680 695
681 return rss; 696 return rss;
682} 697}
683 698
686static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
687static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 702static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
688static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 703static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
689 704
690/* apparently < 5.8.8 */ 705/* apparently < 5.8.8 */
691#undef MgPV_nolen_const
692#ifndef MgPV_nolen_const 706#ifndef MgPV_nolen_const
693#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 707#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
694 SvPV_nolen_const((SV*)((mg)->mg_ptr)) : \ 708 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
695 (const char*)(mg)->mg_ptr) 709 (const char*)(mg)->mg_ptr)
696#endif 710#endif
697 711
698/* 712/*
699 * This overrides the default magic get method of %SIG elements. 713 * This overrides the default magic get method of %SIG elements.
802 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
803 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
804 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
805 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);
806 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
807 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
808 822
809 { 823 {
810 dSP; 824 dSP;
811 LOGOP myop; 825 LOGOP myop;
812 826
829 */ 843 */
830 ENTER; 844 ENTER;
831} 845}
832 846
833static void 847static void
834coro_destroy (pTHX_ struct coro *coro) 848coro_destruct (pTHX_ struct coro *coro)
835{ 849{
836 if (!IN_DESTRUCT) 850 if (!IN_DESTRUCT)
837 { 851 {
838 /* restore all saved variables and stuff */ 852 /* restore all saved variables and stuff */
839 LEAVE_SCOPE (0); 853 LEAVE_SCOPE (0);
861 SvREFCNT_dec (PL_warnhook); 875 SvREFCNT_dec (PL_warnhook);
862 876
863 SvREFCNT_dec (coro->saved_deffh); 877 SvREFCNT_dec (coro->saved_deffh);
864 SvREFCNT_dec (coro->throw); 878 SvREFCNT_dec (coro->throw);
865 879
866 coro_destroy_stacks (aTHX); 880 coro_destruct_stacks (aTHX);
867} 881}
868 882
869static void 883static void
870free_coro_mortal (pTHX) 884free_coro_mortal (pTHX)
871{ 885{
908 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
909 : bot; 923 : bot;
910 924
911 av_extend (av, top - bot); 925 av_extend (av, top - bot);
912 while (bot < top) 926 while (bot < top)
913 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
914 928
915 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
916 ENTER; 930 ENTER;
917 SAVETMPS; 931 SAVETMPS;
918 EXTEND (SP, 3); 932 EXTEND (SP, 3);
1066 coro_cctx *cctx; 1080 coro_cctx *cctx;
1067 void *stack_start; 1081 void *stack_start;
1068 size_t stack_size; 1082 size_t stack_size;
1069 1083
1070 ++cctx_count; 1084 ++cctx_count;
1071
1072 Newz (0, cctx, 1, coro_cctx); 1085 Newz (0, cctx, 1, coro_cctx);
1073 1086
1074#if HAVE_MMAP 1087#if HAVE_MMAP
1075 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1088 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1076 /* mmap supposedly does allocate-on-write for us */ 1089 /* mmap supposedly does allocate-on-write for us */
1112{ 1125{
1113 if (!cctx) 1126 if (!cctx)
1114 return; 1127 return;
1115 1128
1116 --cctx_count; 1129 --cctx_count;
1130 coro_destroy (&cctx->cctx);
1117 1131
1132 /* coro_transfer creates new, empty cctx's */
1133 if (cctx->sptr)
1134 {
1118#if CORO_USE_VALGRIND 1135#if CORO_USE_VALGRIND
1119 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1136 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1120#endif 1137#endif
1121 1138
1122#if HAVE_MMAP 1139#if HAVE_MMAP
1123 if (cctx->flags & CC_MAPPED) 1140 if (cctx->flags & CC_MAPPED)
1124 munmap (cctx->sptr, cctx->ssize); 1141 munmap (cctx->sptr, cctx->ssize);
1125 else 1142 else
1126#endif 1143#endif
1127 Safefree (cctx->sptr); 1144 Safefree (cctx->sptr);
1145 }
1128 1146
1129 Safefree (cctx); 1147 Safefree (cctx);
1130} 1148}
1131 1149
1132/* wether this cctx should be destructed */ 1150/* wether this cctx should be destructed */
1151} 1169}
1152 1170
1153static void 1171static void
1154cctx_put (coro_cctx *cctx) 1172cctx_put (coro_cctx *cctx)
1155{ 1173{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175
1156 /* free another cctx if overlimit */ 1176 /* free another cctx if overlimit */
1157 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
1158 { 1178 {
1159 coro_cctx *first = cctx_first; 1179 coro_cctx *first = cctx_first;
1160 cctx_first = first->next; 1180 cctx_first = first->next;
1194/* always use the TRANSFER macro */ 1214/* always use the TRANSFER macro */
1195static void NOINLINE 1215static void NOINLINE
1196transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1216transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1197{ 1217{
1198 dSTACKLEVEL; 1218 dSTACKLEVEL;
1199 static volatile int has_throw;
1200 1219
1201 /* sometimes transfer is only called to set idle_sp */ 1220 /* sometimes transfer is only called to set idle_sp */
1202 if (expect_false (!next)) 1221 if (expect_false (!next))
1203 { 1222 {
1204 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1223 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1205 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1224 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1206 } 1225 }
1207 else if (expect_true (prev != next)) 1226 else if (expect_true (prev != next))
1208 { 1227 {
1228 static volatile int has_throw;
1209 coro_cctx *prev__cctx; 1229 coro_cctx *prev__cctx;
1210 1230
1211 if (expect_false (prev->flags & CF_NEW)) 1231 if (expect_false (prev->flags & CF_NEW))
1212 { 1232 {
1213 /* create a new empty context */ 1233 /* create a new empty/source context */
1234 ++cctx_count;
1214 Newz (0, prev->cctx, 1, coro_cctx); 1235 New (0, prev->cctx, 1, coro_cctx);
1236 prev->cctx->sptr = 0;
1237 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1238
1215 prev->flags &= ~CF_NEW; 1239 prev->flags &= ~CF_NEW;
1216 prev->flags |= CF_RUNNING; 1240 prev->flags |= CF_RUNNING;
1217 } 1241 }
1218 1242
1219 prev->flags &= ~CF_RUNNING; 1243 prev->flags &= ~CF_RUNNING;
1326 croak ("FATAL: tried to destroy currently running coroutine"); 1350 croak ("FATAL: tried to destroy currently running coroutine");
1327 1351
1328 save_perl (aTHX_ &temp); 1352 save_perl (aTHX_ &temp);
1329 load_perl (aTHX_ coro); 1353 load_perl (aTHX_ coro);
1330 1354
1331 coro_destroy (aTHX_ coro); 1355 coro_destruct (aTHX_ coro);
1332 1356
1333 load_perl (aTHX_ &temp); 1357 load_perl (aTHX_ &temp);
1334 1358
1335 coro->slot = 0; 1359 coro->slot = 0;
1336 } 1360 }
1442 LOCK; 1466 LOCK;
1443 1467
1444 sv_hook = coro_nready ? 0 : coro_readyhook; 1468 sv_hook = coro_nready ? 0 : coro_readyhook;
1445 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1469 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1446 1470
1447 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1471 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1448 ++coro_nready; 1472 ++coro_nready;
1449 1473
1450 UNLOCK; 1474 UNLOCK;
1451 1475
1452 if (sv_hook) 1476 if (sv_hook)
1624 else 1648 else
1625 coro->slot->runops = RUNOPS_DEFAULT; 1649 coro->slot->runops = RUNOPS_DEFAULT;
1626 } 1650 }
1627} 1651}
1628 1652
1653static int
1654coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1655{
1656 AV *padlist;
1657 AV *av = (AV *)mg->mg_obj;
1658
1659 abort ();
1660
1661 return 0;
1662}
1663
1664static MGVTBL coro_gensub_vtbl = {
1665 0, 0, 0, 0,
1666 coro_gensub_free
1667};
1668
1669/*****************************************************************************/
1670/* PerlIO::cede */
1671
1672typedef struct
1673{
1674 PerlIOBuf base;
1675 NV next, every;
1676} PerlIOCede;
1677
1678static IV
1679PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1680{
1681 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1682
1683 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1684 self->next = nvtime () + self->every;
1685
1686 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1687}
1688
1689static SV *
1690PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1691{
1692 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1693
1694 return newSVnv (self->every);
1695}
1696
1697static IV
1698PerlIOCede_flush (pTHX_ PerlIO *f)
1699{
1700 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1701 double now = nvtime ();
1702
1703 if (now >= self->next)
1704 {
1705 api_cede ();
1706 self->next = now + self->every;
1707 }
1708
1709 return PerlIOBuf_flush (aTHX_ f);
1710}
1711
1712static PerlIO_funcs PerlIO_cede =
1713{
1714 sizeof(PerlIO_funcs),
1715 "cede",
1716 sizeof(PerlIOCede),
1717 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1718 PerlIOCede_pushed,
1719 PerlIOBuf_popped,
1720 PerlIOBuf_open,
1721 PerlIOBase_binmode,
1722 PerlIOCede_getarg,
1723 PerlIOBase_fileno,
1724 PerlIOBuf_dup,
1725 PerlIOBuf_read,
1726 PerlIOBuf_unread,
1727 PerlIOBuf_write,
1728 PerlIOBuf_seek,
1729 PerlIOBuf_tell,
1730 PerlIOBuf_close,
1731 PerlIOCede_flush,
1732 PerlIOBuf_fill,
1733 PerlIOBase_eof,
1734 PerlIOBase_error,
1735 PerlIOBase_clearerr,
1736 PerlIOBase_setlinebuf,
1737 PerlIOBuf_get_base,
1738 PerlIOBuf_bufsiz,
1739 PerlIOBuf_get_ptr,
1740 PerlIOBuf_get_cnt,
1741 PerlIOBuf_set_ptrcnt,
1742};
1743
1744
1629MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1745MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1630 1746
1631PROTOTYPES: DISABLE 1747PROTOTYPES: DISABLE
1632 1748
1633BOOT: 1749BOOT:
1634{ 1750{
1635#ifdef USE_ITHREADS 1751#ifdef USE_ITHREADS
1636 MUTEX_INIT (&coro_mutex); 1752 MUTEX_INIT (&coro_lock);
1637#endif 1753#endif
1638 BOOT_PAGESIZE; 1754 BOOT_PAGESIZE;
1639 1755
1640 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1641 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1662 main_top_env = main_top_env->je_prev; 1778 main_top_env = main_top_env->je_prev;
1663 1779
1664 coroapi.ver = CORO_API_VERSION; 1780 coroapi.ver = CORO_API_VERSION;
1665 coroapi.rev = CORO_API_REVISION; 1781 coroapi.rev = CORO_API_REVISION;
1666 coroapi.transfer = api_transfer; 1782 coroapi.transfer = api_transfer;
1783
1784 {
1785 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1786
1787 if (!svp) croak ("Time::HiRes is required");
1788 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1789
1790 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1791 }
1667 1792
1668 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1793 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1669} 1794}
1670 1795
1671SV * 1796SV *
1719 ta.next = 0; 1844 ta.next = 0;
1720 break; 1845 break;
1721 1846
1722 case 1: 1847 case 1:
1723 if (items != 2) 1848 if (items != 2)
1724 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1849 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1725 1850
1726 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1851 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1727 break; 1852 break;
1728 1853
1729 case 2: 1854 case 2:
1799call (Coro::State coro, SV *coderef) 1924call (Coro::State coro, SV *coderef)
1800 ALIAS: 1925 ALIAS:
1801 eval = 1 1926 eval = 1
1802 CODE: 1927 CODE:
1803{ 1928{
1804 if (coro->mainstack) 1929 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1805 { 1930 {
1806 struct coro temp; 1931 struct coro temp;
1807 1932
1808 if (!(coro->flags & CF_RUNNING)) 1933 if (!(coro->flags & CF_RUNNING))
1809 { 1934 {
1891force_cctx () 2016force_cctx ()
1892 CODE: 2017 CODE:
1893 struct coro *coro = SvSTATE (coro_current); 2018 struct coro *coro = SvSTATE (coro_current);
1894 coro->cctx->idle_sp = 0; 2019 coro->cctx->idle_sp = 0;
1895 2020
1896MODULE = Coro::State PACKAGE = Coro
1897
1898BOOT:
1899{
1900 int i;
1901
1902 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1903 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1904 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1905
1906 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1907 SvREADONLY_on (coro_current);
1908
1909 coro_stash = gv_stashpv ("Coro", TRUE);
1910
1911 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1912 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1913 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1914 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1915 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1916 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1917
1918 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1919 coro_ready[i] = newAV ();
1920
1921 {
1922 SV *sv = perl_get_sv ("Coro::API", TRUE);
1923 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1924
1925 coroapi.schedule = api_schedule;
1926 coroapi.cede = api_cede;
1927 coroapi.cede_notself = api_cede_notself;
1928 coroapi.ready = api_ready;
1929 coroapi.is_ready = api_is_ready;
1930 coroapi.nready = &coro_nready;
1931 coroapi.current = coro_current;
1932
1933 GCoroAPI = &coroapi;
1934 sv_setiv (sv, (IV)&coroapi);
1935 SvREADONLY_on (sv);
1936 }
1937}
1938
1939void
1940_set_current (SV *current)
1941 PROTOTYPE: $
1942 CODE:
1943 SvREFCNT_dec (SvRV (coro_current));
1944 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1945
1946void
1947_set_readyhook (SV *hook)
1948 PROTOTYPE: $
1949 CODE:
1950 LOCK;
1951 SvREFCNT_dec (coro_readyhook);
1952 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
1953 UNLOCK;
1954
1955int
1956prio (Coro::State coro, int newprio = 0)
1957 ALIAS:
1958 nice = 1
1959 CODE:
1960{
1961 RETVAL = coro->prio;
1962
1963 if (items > 1)
1964 {
1965 if (ix)
1966 newprio = coro->prio - newprio;
1967
1968 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1969 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1970
1971 coro->prio = newprio;
1972 }
1973}
1974 OUTPUT:
1975 RETVAL
1976
1977SV *
1978ready (SV *self)
1979 PROTOTYPE: $
1980 CODE:
1981 RETVAL = boolSV (api_ready (self));
1982 OUTPUT:
1983 RETVAL
1984
1985int
1986nready (...)
1987 PROTOTYPE:
1988 CODE:
1989 RETVAL = coro_nready;
1990 OUTPUT:
1991 RETVAL
1992
1993void
1994throw (Coro::State self, SV *throw = &PL_sv_undef)
1995 PROTOTYPE: $;$
1996 CODE:
1997 SvREFCNT_dec (self->throw);
1998 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1999
2000void 2021void
2001swap_defsv (Coro::State self) 2022swap_defsv (Coro::State self)
2002 PROTOTYPE: $ 2023 PROTOTYPE: $
2003 ALIAS: 2024 ALIAS:
2004 swap_defav = 1 2025 swap_defav = 1
2011 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2032 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2012 2033
2013 SV *tmp = *src; *src = *dst; *dst = tmp; 2034 SV *tmp = *src; *src = *dst; *dst = tmp;
2014 } 2035 }
2015 2036
2037MODULE = Coro::State PACKAGE = Coro
2038
2039BOOT:
2040{
2041 int i;
2042
2043 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2044 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2045 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2046
2047 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2048 SvREADONLY_on (coro_current);
2049
2050 coro_stash = gv_stashpv ("Coro", TRUE);
2051
2052 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2053 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2054 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2055 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2056 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2057 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2058
2059 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2060 coro_ready[i] = newAV ();
2061
2062 {
2063 SV *sv = perl_get_sv ("Coro::API", TRUE);
2064 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2065
2066 coroapi.schedule = api_schedule;
2067 coroapi.cede = api_cede;
2068 coroapi.cede_notself = api_cede_notself;
2069 coroapi.ready = api_ready;
2070 coroapi.is_ready = api_is_ready;
2071 coroapi.nready = &coro_nready;
2072 coroapi.current = coro_current;
2073
2074 GCoroAPI = &coroapi;
2075 sv_setiv (sv, (IV)&coroapi);
2076 SvREADONLY_on (sv);
2077 }
2078}
2079
2080void
2081_set_current (SV *current)
2082 PROTOTYPE: $
2083 CODE:
2084 SvREFCNT_dec (SvRV (coro_current));
2085 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2086
2087void
2088_set_readyhook (SV *hook)
2089 PROTOTYPE: $
2090 CODE:
2091 LOCK;
2092 SvREFCNT_dec (coro_readyhook);
2093 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2094 UNLOCK;
2095
2096int
2097prio (Coro::State coro, int newprio = 0)
2098 ALIAS:
2099 nice = 1
2100 CODE:
2101{
2102 RETVAL = coro->prio;
2103
2104 if (items > 1)
2105 {
2106 if (ix)
2107 newprio = coro->prio - newprio;
2108
2109 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2110 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2111
2112 coro->prio = newprio;
2113 }
2114}
2115 OUTPUT:
2116 RETVAL
2117
2118SV *
2119ready (SV *self)
2120 PROTOTYPE: $
2121 CODE:
2122 RETVAL = boolSV (api_ready (self));
2123 OUTPUT:
2124 RETVAL
2125
2126int
2127nready (...)
2128 PROTOTYPE:
2129 CODE:
2130 RETVAL = coro_nready;
2131 OUTPUT:
2132 RETVAL
2133
2134void
2135throw (Coro::State self, SV *throw = &PL_sv_undef)
2136 PROTOTYPE: $;$
2137 CODE:
2138 SvREFCNT_dec (self->throw);
2139 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2140
2016# for async_pool speedup 2141# for async_pool speedup
2017void 2142void
2018_pool_1 (SV *cb) 2143_pool_1 (SV *cb)
2019 CODE: 2144 CODE:
2020{ 2145{
2032 SvREFCNT_dec (old); 2157 SvREFCNT_dec (old);
2033 croak ("\3async_pool terminate\2\n"); 2158 croak ("\3async_pool terminate\2\n");
2034 } 2159 }
2035 2160
2036 SvREFCNT_dec (coro->saved_deffh); 2161 SvREFCNT_dec (coro->saved_deffh);
2037 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2162 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2038 2163
2039 hv_store (hv, "desc", sizeof ("desc") - 1, 2164 hv_store (hv, "desc", sizeof ("desc") - 1,
2040 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2165 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2041 2166
2042 invoke_av = (AV *)SvRV (invoke); 2167 invoke_av = (AV *)SvRV (invoke);
2046 2171
2047 if (len > 0) 2172 if (len > 0)
2048 { 2173 {
2049 av_fill (defav, len - 1); 2174 av_fill (defav, len - 1);
2050 for (i = 0; i < len; ++i) 2175 for (i = 0; i < len; ++i)
2051 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2176 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2052 } 2177 }
2053 2178
2054 SvREFCNT_dec (invoke); 2179 SvREFCNT_dec (invoke);
2055} 2180}
2056 2181
2084 api_trace (coro_current, 0); 2209 api_trace (coro_current, 0);
2085 2210
2086 av_push (av_async_pool, newSVsv (coro_current)); 2211 av_push (av_async_pool, newSVsv (coro_current));
2087} 2212}
2088 2213
2214#if 0
2215
2216void
2217_generator_call (...)
2218 PROTOTYPE: @
2219 PPCODE:
2220 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2221 xxxx
2222 abort ();
2223
2224SV *
2225gensub (SV *sub, ...)
2226 PROTOTYPE: &;@
2227 CODE:
2228{
2229 struct coro *coro;
2230 MAGIC *mg;
2231 CV *xcv;
2232 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2233 int i;
2234
2235 CvGV (ncv) = CvGV (cv);
2236 CvFILE (ncv) = CvFILE (cv);
2237
2238 Newz (0, coro, 1, struct coro);
2239 coro->args = newAV ();
2240 coro->flags = CF_NEW;
2241
2242 av_extend (coro->args, items - 1);
2243 for (i = 1; i < items; i++)
2244 av_push (coro->args, newSVsv (ST (i)));
2245
2246 CvISXSUB_on (ncv);
2247 CvXSUBANY (ncv).any_ptr = (void *)coro;
2248
2249 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2250
2251 CvXSUB (ncv) = CvXSUB (xcv);
2252 CvANON_on (ncv);
2253
2254 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2255 RETVAL = newRV_noinc ((SV *)ncv);
2256}
2257 OUTPUT:
2258 RETVAL
2259
2260#endif
2261
2089 2262
2090MODULE = Coro::State PACKAGE = Coro::AIO 2263MODULE = Coro::State PACKAGE = Coro::AIO
2091 2264
2092SV * 2265void
2093_get_state () 2266_get_state (SV *self)
2094 CODE: 2267 PPCODE:
2095{ 2268{
2096 struct io_state *data; 2269 AV *defav = GvAV (PL_defgv);
2097 2270 AV *av = newAV ();
2271 int i;
2098 RETVAL = newSV (sizeof (struct io_state)); 2272 SV *data_sv = newSV (sizeof (struct io_state));
2099 data = (struct io_state *)SvPVX (RETVAL); 2273 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2100 SvCUR_set (RETVAL, sizeof (struct io_state)); 2274 SvCUR_set (data_sv, sizeof (struct io_state));
2101 SvPOK_only (RETVAL); 2275 SvPOK_only (data_sv);
2102 2276
2103 data->errorno = errno; 2277 data->errorno = errno;
2104 data->laststype = PL_laststype; 2278 data->laststype = PL_laststype;
2105 data->laststatval = PL_laststatval; 2279 data->laststatval = PL_laststatval;
2106 data->statcache = PL_statcache; 2280 data->statcache = PL_statcache;
2281
2282 av_extend (av, AvFILLp (defav) + 1 + 1);
2283
2284 for (i = 0; i <= AvFILLp (defav); ++i)
2285 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2286
2287 av_push (av, data_sv);
2288
2289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2290
2291 api_ready (self);
2107} 2292}
2108 OUTPUT:
2109 RETVAL
2110 2293
2111void 2294void
2112_set_state (char *data_) 2295_set_state (SV *state)
2113 PROTOTYPE: $ 2296 PROTOTYPE: $
2114 CODE: 2297 PPCODE:
2115{ 2298{
2116 struct io_state *data = (void *)data_; 2299 AV *av = (AV *)SvRV (state);
2300 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2301 int i;
2117 2302
2118 errno = data->errorno; 2303 errno = data->errorno;
2119 PL_laststype = data->laststype; 2304 PL_laststype = data->laststype;
2120 PL_laststatval = data->laststatval; 2305 PL_laststatval = data->laststatval;
2121 PL_statcache = data->statcache; 2306 PL_statcache = data->statcache;
2307
2308 EXTEND (SP, AvFILLp (av));
2309 for (i = 0; i < AvFILLp (av); ++i)
2310 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2122} 2311}
2123 2312
2124 2313
2125MODULE = Coro::State PACKAGE = Coro::AnyEvent 2314MODULE = Coro::State PACKAGE = Coro::AnyEvent
2126 2315
2150 } 2339 }
2151 2340
2152 --incede; 2341 --incede;
2153} 2342}
2154 2343
2344
2345MODULE = Coro::State PACKAGE = PerlIO::cede
2346
2347BOOT:
2348 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines