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.290 by root, Tue Nov 18 05:51:38 2008 UTC vs.
Revision 1.308 by root, Wed Nov 19 15:09:57 2008 UTC

16 16
17#ifdef WIN32 17#ifdef WIN32
18# undef setjmp 18# undef setjmp
19# undef longjmp 19# undef longjmp
20# undef _exit 20# undef _exit
21# define setjmp _setjmp // deep magic, don't ask 21# define setjmp _setjmp /* deep magic */
22#else 22#else
23# include <inttypes.h> /* most portable stdint.h */ 23# include <inttypes.h> /* most portable stdint.h */
24#endif 24#endif
25 25
26#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
120#endif 120#endif
121 121
122/* The next macros try to return the current stack pointer, in an as 122/* The next macros try to return the current stack pointer, in an as
123 * portable way as possible. */ 123 * portable way as possible. */
124#if __GNUC__ >= 4 124#if __GNUC__ >= 4
125# define dSTACKLEVEL int stacklevel_dummy
125# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 126# define STACKLEVEL __builtin_frame_address (0)
126#else 127#else
127# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 128# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel)
128#endif 130#endif
129 131
130#define IN_DESTRUCT (PL_main_cv == Nullcv) 132#define IN_DESTRUCT (PL_main_cv == Nullcv)
131 133
132#if __GNUC__ >= 3 134#if __GNUC__ >= 3
143#define expect_true(expr) expect ((expr) != 0, 1) 145#define expect_true(expr) expect ((expr) != 0, 1)
144 146
145#define NOINLINE attribute ((noinline)) 147#define NOINLINE attribute ((noinline))
146 148
147#include "CoroAPI.h" 149#include "CoroAPI.h"
150#define GCoroAPI (&coroapi) /* very sneaky */
148 151
149#ifdef USE_ITHREADS 152#ifdef USE_ITHREADS
150# if CORO_PTHREAD 153# if CORO_PTHREAD
151static void *coro_thx; 154static void *coro_thx;
152# endif 155# endif
243 /* state data */ 246 /* state data */
244 struct CoroSLF slf_frame; /* saved slf frame */ 247 struct CoroSLF slf_frame; /* saved slf frame */
245 AV *mainstack; 248 AV *mainstack;
246 perl_slots *slot; /* basically the saved sp */ 249 perl_slots *slot; /* basically the saved sp */
247 250
251 CV *startcv; /* the CV to execute */
248 AV *args; /* data associated with this coroutine (initial args) */ 252 AV *args; /* data associated with this coroutine (initial args) */
249 int refcnt; /* coroutines are refcounted, yes */ 253 int refcnt; /* coroutines are refcounted, yes */
250 int flags; /* CF_ flags */ 254 int flags; /* CF_ flags */
251 HV *hv; /* the perl hash associated with this coro, if any */ 255 HV *hv; /* the perl hash associated with this coro, if any */
252 void (*on_destroy)(pTHX_ struct coro *coro); 256 void (*on_destroy)(pTHX_ struct coro *coro);
253 257
254 /* statistics */ 258 /* statistics */
255 int usecount; /* number of transfers to this coro */ 259 int usecount; /* number of transfers to this coro */
256 260
257 /* coro process data */ 261 /* coro process data */
258 int prio; 262 int prio;
259 SV *throw; /* exception to be thrown */ 263 SV *except; /* exception to be thrown */
264 SV *rouse_cb;
260 265
261 /* async_pool */ 266 /* async_pool */
262 SV *saved_deffh; 267 SV *saved_deffh;
263 268
264 /* linked list */ 269 /* linked list */
270 275
271/* the following variables are effectively part of the perl context */ 276/* the following variables are effectively part of the perl context */
272/* and get copied between struct coro and these variables */ 277/* and get copied between struct coro and these variables */
273/* the mainr easonw e don't support windows process emulation */ 278/* the mainr easonw e don't support windows process emulation */
274static struct CoroSLF slf_frame; /* the current slf frame */ 279static struct CoroSLF slf_frame; /* the current slf frame */
275static SV *coro_throw;
276 280
277/** Coro ********************************************************************/ 281/** Coro ********************************************************************/
278 282
279#define PRIO_MAX 3 283#define PRIO_MAX 3
280#define PRIO_HIGH 1 284#define PRIO_HIGH 1
318#if PERL_VERSION_ATLEAST (5,10,0) 322#if PERL_VERSION_ATLEAST (5,10,0)
319 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 323 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
320 get_hv (name, create); 324 get_hv (name, create);
321#endif 325#endif
322 return get_hv (name, create); 326 return get_hv (name, create);
327}
328
329/* may croak */
330INLINE CV *
331coro_sv_2cv (SV *sv)
332{
333 HV *st;
334 GV *gvp;
335 return sv_2cv (sv, &st, &gvp, 0);
323} 336}
324 337
325static AV * 338static AV *
326coro_clone_padlist (pTHX_ CV *cv) 339coro_clone_padlist (pTHX_ CV *cv)
327{ 340{
446 else 459 else
447 { 460 {
448#if CORO_PREFER_PERL_FUNCTIONS 461#if CORO_PREFER_PERL_FUNCTIONS
449 /* this is probably cleaner? but also slower! */ 462 /* this is probably cleaner? but also slower! */
450 /* in practise, it seems to be less stable */ 463 /* in practise, it seems to be less stable */
451 CV *cp = Perl_cv_clone (cv); 464 CV *cp = Perl_cv_clone (aTHX_ cv);
452 CvPADLIST (cv) = CvPADLIST (cp); 465 CvPADLIST (cv) = CvPADLIST (cp);
453 CvPADLIST (cp) = 0; 466 CvPADLIST (cp) = 0;
454 SvREFCNT_dec (cp); 467 SvREFCNT_dec (cp);
455#else 468#else
456 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 469 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
509 522
510 PUTBACK; 523 PUTBACK;
511 } 524 }
512 525
513 slf_frame = c->slf_frame; 526 slf_frame = c->slf_frame;
514 coro_throw = c->throw; 527 CORO_THROW = c->except;
515} 528}
516 529
517static void 530static void
518save_perl (pTHX_ Coro__State c) 531save_perl (pTHX_ Coro__State c)
519{ 532{
520 c->throw = coro_throw; 533 c->except = CORO_THROW;
521 c->slf_frame = slf_frame; 534 c->slf_frame = slf_frame;
522 535
523 { 536 {
524 dSP; 537 dSP;
525 I32 cxix = cxstack_ix; 538 I32 cxix = cxstack_ix;
600 * of perl.c:init_stacks, except that it uses less memory 613 * of perl.c:init_stacks, except that it uses less memory
601 * on the (sometimes correct) assumption that coroutines do 614 * on the (sometimes correct) assumption that coroutines do
602 * not usually need a lot of stackspace. 615 * not usually need a lot of stackspace.
603 */ 616 */
604#if CORO_PREFER_PERL_FUNCTIONS 617#if CORO_PREFER_PERL_FUNCTIONS
605# define coro_init_stacks init_stacks 618# define coro_init_stacks(thx) init_stacks ()
606#else 619#else
607static void 620static void
608coro_init_stacks (pTHX) 621coro_init_stacks (pTHX)
609{ 622{
610 PL_curstackinfo = new_stackinfo(32, 8); 623 PL_curstackinfo = new_stackinfo(32, 8);
857 { 870 {
858 dSP; 871 dSP;
859 UNOP myop; 872 UNOP myop;
860 873
861 Zero (&myop, 1, UNOP); 874 Zero (&myop, 1, UNOP);
862 myop.op_next = Nullop; 875 myop.op_next = Nullop;
876 myop.op_type = OP_ENTERSUB;
863 myop.op_flags = OPf_WANT_VOID; 877 myop.op_flags = OPf_WANT_VOID;
864 878
865 PUSHMARK (SP); 879 PUSHMARK (SP);
866 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 880 PUSHs ((SV *)coro->startcv);
867 PUTBACK; 881 PUTBACK;
868 PL_op = (OP *)&myop; 882 PL_op = (OP *)&myop;
869 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 883 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
870 SPAGAIN;
871 } 884 }
872 885
873 /* this newly created coroutine might be run on an existing cctx which most 886 /* this newly created coroutine might be run on an existing cctx which most
874 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 887 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
875 */ 888 */
876 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 889 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
877 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 890 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
878 891
879 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 892 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
893 coro_setup_op.op_next = PL_op;
880 coro_setup_op.op_type = OP_CUSTOM; 894 coro_setup_op.op_type = OP_CUSTOM;
881 coro_setup_op.op_ppaddr = pp_slf; 895 coro_setup_op.op_ppaddr = pp_slf;
882 coro_setup_op.op_next = PL_op; 896 /* no flags etc. required, as an init function won't be called */
883 897
884 PL_op = (OP *)&coro_setup_op; 898 PL_op = (OP *)&coro_setup_op;
885 899
886 /* copy throw, in case it was set before coro_setup */ 900 /* copy throw, in case it was set before coro_setup */
887 coro_throw = coro->throw; 901 CORO_THROW = coro->except;
888} 902}
889 903
890static void 904static void
891coro_destruct (pTHX_ struct coro *coro) 905coro_destruct (pTHX_ struct coro *coro)
892{ 906{
916 930
917 SvREFCNT_dec (PL_diehook); 931 SvREFCNT_dec (PL_diehook);
918 SvREFCNT_dec (PL_warnhook); 932 SvREFCNT_dec (PL_warnhook);
919 933
920 SvREFCNT_dec (coro->saved_deffh); 934 SvREFCNT_dec (coro->saved_deffh);
921 SvREFCNT_dec (coro_throw); 935 SvREFCNT_dec (coro->rouse_cb);
922 936
923 coro_destruct_stacks (aTHX); 937 coro_destruct_stacks (aTHX);
924} 938}
925 939
926INLINE void 940INLINE void
1013 SAVETMPS; 1027 SAVETMPS;
1014 EXTEND (SP, 3); 1028 EXTEND (SP, 3);
1015 PUSHMARK (SP); 1029 PUSHMARK (SP);
1016 PUSHs (&PL_sv_yes); 1030 PUSHs (&PL_sv_yes);
1017 PUSHs (fullname); 1031 PUSHs (fullname);
1018 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1032 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1019 PUTBACK; 1033 PUTBACK;
1020 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1034 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1021 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1035 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1022 SPAGAIN; 1036 SPAGAIN;
1023 FREETMPS; 1037 FREETMPS;
1070static int 1084static int
1071slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1085slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1072{ 1086{
1073 *frame = cctx_ssl_frame; 1087 *frame = cctx_ssl_frame;
1074 1088
1075 return 0; 1089 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1076} 1090}
1077 1091
1078/* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ 1092/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1079static void NOINLINE 1093static void NOINLINE
1080cctx_prepare (pTHX_ coro_cctx *cctx) 1094cctx_prepare (pTHX_ coro_cctx *cctx)
1081{ 1095{
1082 PL_top_env = &PL_start_env; 1096 PL_top_env = &PL_start_env;
1083 1097
1084 if (cctx->flags & CC_TRACE) 1098 if (cctx->flags & CC_TRACE)
1085 PL_runops = runops_trace; 1099 PL_runops = runops_trace;
1086 1100
1087 /* we already must be in an SLF call, there is no other valid way 1101 /* we already must be executing an SLF op, there is no other valid way
1088 * that can lead to creation of a new cctx */ 1102 * that can lead to creation of a new cctx */
1089 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1103 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1090 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1104 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1091 1105
1106 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1092 cctx_ssl_cctx = cctx; 1107 cctx_ssl_cctx = cctx;
1093 cctx_ssl_frame = slf_frame; 1108 cctx_ssl_frame = slf_frame;
1094 1109
1095 slf_frame.prepare = slf_prepare_set_stacklevel; 1110 slf_frame.prepare = slf_prepare_set_stacklevel;
1096 slf_frame.check = slf_check_set_stacklevel; 1111 slf_frame.check = slf_check_set_stacklevel;
1128 transfer_tail (aTHX); 1143 transfer_tail (aTHX);
1129 1144
1130 /* somebody or something will hit me for both perl_run and PL_restartop */ 1145 /* somebody or something will hit me for both perl_run and PL_restartop */
1131 PL_restartop = PL_op; 1146 PL_restartop = PL_op;
1132 perl_run (PL_curinterp); 1147 perl_run (PL_curinterp);
1148 /*
1149 * Unfortunately, there is no way to get at the return values of the
1150 * coro body here, as perl_run destroys these
1151 */
1133 1152
1134 /* 1153 /*
1135 * If perl-run returns we assume exit() was being called or the coro 1154 * If perl-run returns we assume exit() was being called or the coro
1136 * fell off the end, which seems to be the only valid (non-bug) 1155 * fell off the end, which seems to be the only valid (non-bug)
1137 * reason for perl_run to return. We try to exit by jumping to the 1156 * reason for perl_run to return. We try to exit by jumping to the
1317 dSTACKLEVEL; 1336 dSTACKLEVEL;
1318 1337
1319 /* sometimes transfer is only called to set idle_sp */ 1338 /* sometimes transfer is only called to set idle_sp */
1320 if (expect_false (!next)) 1339 if (expect_false (!next))
1321 { 1340 {
1322 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1341 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1323 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1342 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1324 } 1343 }
1325 else if (expect_true (prev != next)) 1344 else if (expect_true (prev != next))
1326 { 1345 {
1327 coro_cctx *prev__cctx; 1346 coro_cctx *prev__cctx;
1352 1371
1353 prev__cctx = prev->cctx; 1372 prev__cctx = prev->cctx;
1354 1373
1355 /* possibly untie and reuse the cctx */ 1374 /* possibly untie and reuse the cctx */
1356 if (expect_true ( 1375 if (expect_true (
1357 prev__cctx->idle_sp == (void *)stacklevel 1376 prev__cctx->idle_sp == STACKLEVEL
1358 && !(prev__cctx->flags & CC_TRACE) 1377 && !(prev__cctx->flags & CC_TRACE)
1359 && !force_cctx 1378 && !force_cctx
1360 )) 1379 ))
1361 { 1380 {
1362 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1381 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1429 1448
1430 coro->slot = 0; 1449 coro->slot = 0;
1431 } 1450 }
1432 1451
1433 cctx_destroy (coro->cctx); 1452 cctx_destroy (coro->cctx);
1453 SvREFCNT_dec (coro->startcv);
1434 SvREFCNT_dec (coro->args); 1454 SvREFCNT_dec (coro->args);
1455 SvREFCNT_dec (CORO_THROW);
1435 1456
1436 if (coro->next) coro->next->prev = coro->prev; 1457 if (coro->next) coro->next->prev = coro->prev;
1437 if (coro->prev) coro->prev->next = coro->next; 1458 if (coro->prev) coro->prev->next = coro->next;
1438 if (coro == coro_first) coro_first = coro->next; 1459 if (coro == coro_first) coro_first = coro->next;
1439 1460
1493 1514
1494 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1515 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1495 TRANSFER (ta, 1); 1516 TRANSFER (ta, 1);
1496} 1517}
1497 1518
1519/*****************************************************************************/
1520/* gensub: simple closure generation utility */
1521
1522#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1523
1524/* create a closure from XS, returns a code reference */
1525/* the arg can be accessed via GENSUB_ARG from the callback */
1526/* the callback must use dXSARGS/XSRETURN */
1527static SV *
1528gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1529{
1530 CV *cv = (CV *)newSV (0);
1531
1532 sv_upgrade ((SV *)cv, SVt_PVCV);
1533
1534 CvANON_on (cv);
1535 CvISXSUB_on (cv);
1536 CvXSUB (cv) = xsub;
1537 GENSUB_ARG = arg;
1538
1539 return newRV_noinc ((SV *)cv);
1540}
1541
1498/** Coro ********************************************************************/ 1542/** Coro ********************************************************************/
1499 1543
1500INLINE void 1544INLINE void
1501coro_enq (pTHX_ struct coro *coro) 1545coro_enq (pTHX_ struct coro *coro)
1502{ 1546{
1699 if (coro->flags & CF_RUNNING) 1743 if (coro->flags & CF_RUNNING)
1700 PL_runops = RUNOPS_DEFAULT; 1744 PL_runops = RUNOPS_DEFAULT;
1701 else 1745 else
1702 coro->slot->runops = RUNOPS_DEFAULT; 1746 coro->slot->runops = RUNOPS_DEFAULT;
1703 } 1747 }
1748}
1749
1750/*****************************************************************************/
1751/* rouse callback */
1752
1753#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1754
1755static void
1756coro_rouse_callback (pTHX_ CV *cv)
1757{
1758 dXSARGS;
1759 SV *data = (SV *)GENSUB_ARG;
1760
1761 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1762 {
1763 /* first call, set args */
1764 int i;
1765 AV *av = newAV ();
1766 SV *coro = SvRV (data);
1767
1768 SvRV_set (data, (SV *)av);
1769 api_ready (aTHX_ coro);
1770 SvREFCNT_dec (coro);
1771
1772 /* better take a full copy of the arguments */
1773 while (items--)
1774 av_store (av, items, newSVsv (ST (items)));
1775 }
1776
1777 XSRETURN_EMPTY;
1778}
1779
1780static int
1781slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1782{
1783 SV *data = (SV *)frame->data;
1784
1785 if (CORO_THROW)
1786 return 0;
1787
1788 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1789 return 1;
1790
1791 /* now push all results on the stack */
1792 {
1793 dSP;
1794 AV *av = (AV *)SvRV (data);
1795 int i;
1796
1797 EXTEND (SP, AvFILLp (av) + 1);
1798 for (i = 0; i <= AvFILLp (av); ++i)
1799 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1800
1801 /* we have stolen the elements, so ste length to zero and free */
1802 AvFILLp (av) = -1;
1803 av_undef (av);
1804
1805 PUTBACK;
1806 }
1807
1808 return 0;
1809}
1810
1811static void
1812slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1813{
1814 SV *cb;
1815
1816 if (items)
1817 cb = arg [0];
1818 else
1819 {
1820 struct coro *coro = SvSTATE_current;
1821
1822 if (!coro->rouse_cb)
1823 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
1824
1825 cb = sv_2mortal (coro->rouse_cb);
1826 coro->rouse_cb = 0;
1827 }
1828
1829 if (!SvROK (cb)
1830 || SvTYPE (SvRV (cb)) != SVt_PVCV
1831 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
1832 croak ("Coro::rouse_wait called with illegal callback argument,");
1833
1834 {
1835 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
1836 SV *data = (SV *)GENSUB_ARG;
1837
1838 frame->data = (void *)data;
1839 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
1840 frame->check = slf_check_rouse_wait;
1841 }
1842}
1843
1844static SV *
1845coro_new_rouse_cb (pTHX)
1846{
1847 HV *hv = (HV *)SvRV (coro_current);
1848 struct coro *coro = SvSTATE_hv (hv);
1849 SV *data = newRV_inc ((SV *)hv);
1850 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1851
1852 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
1853 SvREFCNT_dec (data); /* magicext increases the refcount */
1854
1855 SvREFCNT_dec (coro->rouse_cb);
1856 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1857
1858 return cb;
1704} 1859}
1705 1860
1706/*****************************************************************************/ 1861/*****************************************************************************/
1707/* schedule-like-function opcode (SLF) */ 1862/* schedule-like-function opcode (SLF) */
1708 1863
1835 } 1990 }
1836 while (slf_frame.check (aTHX_ &slf_frame)); 1991 while (slf_frame.check (aTHX_ &slf_frame));
1837 1992
1838 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 1993 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1839 1994
1995 /* exception handling */
1996 if (expect_false (CORO_THROW))
1997 {
1998 SV *exception = sv_2mortal (CORO_THROW);
1999
2000 CORO_THROW = 0;
2001 sv_setsv (ERRSV, exception);
2002 croak (0);
2003 }
2004
1840 /* return value handling - mostly like entersub */ 2005 /* return value handling - mostly like entersub */
1841 /* make sure we put something on the stack in scalar context */ 2006 /* make sure we put something on the stack in scalar context */
1842 if (GIMME_V == G_SCALAR) 2007 if (GIMME_V == G_SCALAR)
1843 { 2008 {
1844 dSP; 2009 dSP;
1850 bot [1] = *sp; 2015 bot [1] = *sp;
1851 2016
1852 SP = bot + 1; 2017 SP = bot + 1;
1853 2018
1854 PUTBACK; 2019 PUTBACK;
1855 }
1856
1857 /* exception handling */
1858 if (expect_false (coro_throw))
1859 {
1860 SV *exception = sv_2mortal (coro_throw);
1861
1862 coro_throw = 0;
1863 sv_setsv (ERRSV, exception);
1864 croak (0);
1865 } 2020 }
1866 2021
1867 return NORMAL; 2022 return NORMAL;
1868} 2023}
1869 2024
1991 PerlIOBuf_get_cnt, 2146 PerlIOBuf_get_cnt,
1992 PerlIOBuf_set_ptrcnt, 2147 PerlIOBuf_set_ptrcnt,
1993}; 2148};
1994 2149
1995/*****************************************************************************/ 2150/*****************************************************************************/
2151/* Coro::Semaphore & Coro::Signal */
2152
2153static SV *
2154coro_waitarray_new (pTHX_ int count)
2155{
2156 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2157 AV *av = newAV ();
2158 SV **ary;
2159
2160 /* unfortunately, building manually saves memory */
2161 Newx (ary, 2, SV *);
2162 AvALLOC (av) = ary;
2163 /*AvARRAY (av) = ary;*/
2164 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2165 AvMAX (av) = 1;
2166 AvFILLp (av) = 0;
2167 ary [0] = newSViv (count);
2168
2169 return newRV_noinc ((SV *)av);
2170}
2171
1996/* Coro::Semaphore */ 2172/* semaphore */
1997 2173
1998static void 2174static void
1999coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2175coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2000{ 2176{
2001 SV *count_sv = AvARRAY (av)[0]; 2177 SV *count_sv = AvARRAY (av)[0];
2013 AvARRAY (av)[0] = AvARRAY (av)[1]; 2189 AvARRAY (av)[0] = AvARRAY (av)[1];
2014 AvARRAY (av)[1] = count_sv; 2190 AvARRAY (av)[1] = count_sv;
2015 cb = av_shift (av); 2191 cb = av_shift (av);
2016 2192
2017 if (SvOBJECT (cb)) 2193 if (SvOBJECT (cb))
2194 {
2018 api_ready (aTHX_ cb); 2195 api_ready (aTHX_ cb);
2019 else 2196 --count;
2020 croak ("callbacks not yet supported"); 2197 }
2198 else if (SvTYPE (cb) == SVt_PVCV)
2199 {
2200 dSP;
2201 PUSHMARK (SP);
2202 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2203 PUTBACK;
2204 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2205 }
2021 2206
2022 SvREFCNT_dec (cb); 2207 SvREFCNT_dec (cb);
2023
2024 --count;
2025 } 2208 }
2026} 2209}
2027 2210
2028static void 2211static void
2029coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2212coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2031 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2214 /* call $sem->adjust (0) to possibly wake up some other waiters */
2032 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2215 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2033} 2216}
2034 2217
2035static int 2218static int
2036slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2219slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2037{ 2220{
2038 AV *av = (AV *)frame->data; 2221 AV *av = (AV *)frame->data;
2039 SV *count_sv = AvARRAY (av)[0]; 2222 SV *count_sv = AvARRAY (av)[0];
2040 2223
2224 /* if we are about to throw, don't actually acquire the lock, just throw */
2225 if (CORO_THROW)
2226 return 0;
2041 if (SvIVX (count_sv) > 0) 2227 else if (SvIVX (count_sv) > 0)
2042 { 2228 {
2043 SvSTATE_current->on_destroy = 0; 2229 SvSTATE_current->on_destroy = 0;
2230
2231 if (acquire)
2044 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2232 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2233 else
2234 coro_semaphore_adjust (aTHX_ av, 0);
2235
2045 return 0; 2236 return 0;
2046 } 2237 }
2047 else 2238 else
2048 { 2239 {
2049 int i; 2240 int i;
2058 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2249 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2059 return 1; 2250 return 1;
2060 } 2251 }
2061} 2252}
2062 2253
2063static void 2254static int
2255slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2256{
2257 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2258}
2259
2260static int
2261slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2262{
2263 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2264}
2265
2266static void
2064slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2267slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2065{ 2268{
2066 AV *av = (AV *)SvRV (arg [0]); 2269 AV *av = (AV *)SvRV (arg [0]);
2067 2270
2068 if (SvIVX (AvARRAY (av)[0]) > 0) 2271 if (SvIVX (AvARRAY (av)[0]) > 0)
2069 { 2272 {
2070 frame->data = (void *)av; 2273 frame->data = (void *)av;
2071 frame->prepare = prepare_nop; 2274 frame->prepare = prepare_nop;
2072 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2073 } 2275 }
2074 else 2276 else
2075 { 2277 {
2076 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2278 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2077 2279
2078 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2280 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2079 frame->prepare = prepare_schedule; 2281 frame->prepare = prepare_schedule;
2080 2282
2081 /* to avoid race conditions when a woken-up coro gets terminated */ 2283 /* to avoid race conditions when a woken-up coro gets terminated */
2082 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2284 /* we arrange for a temporary on_destroy that calls adjust (0) */
2083 assert (!SvSTATE_current->on_destroy);//D
2084 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2285 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2085 } 2286 }
2287}
2086 2288
2289static void
2290slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2291{
2292 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2087 frame->check = slf_check_semaphore_down; 2293 frame->check = slf_check_semaphore_down;
2088
2089} 2294}
2090 2295
2091/*****************************************************************************/ 2296static void
2092/* gensub: simple closure generation utility */ 2297slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2093
2094#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2095
2096/* create a closure from XS, returns a code reference */
2097/* the arg can be accessed via GENSUB_ARG from the callback */
2098/* the callback must use dXSARGS/XSRETURN */
2099static SV *
2100gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2101{ 2298{
2102 CV *cv = (CV *)newSV (0); 2299 if (items >= 2)
2300 {
2301 /* callback form */
2302 AV *av = (AV *)SvRV (arg [0]);
2303 CV *cb_cv = coro_sv_2cv (arg [1]);
2103 2304
2104 sv_upgrade ((SV *)cv, SVt_PVCV); 2305 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2105 2306
2106 CvANON_on (cv); 2307 if (SvIVX (AvARRAY (av)[0]) > 0)
2107 CvISXSUB_on (cv); 2308 coro_semaphore_adjust (aTHX_ av, 0);
2108 CvXSUB (cv) = xsub;
2109 GENSUB_ARG = arg;
2110 2309
2111 return newRV_noinc ((SV *)cv); 2310 frame->prepare = prepare_nop;
2311 frame->check = slf_check_nop;
2312 }
2313 else
2314 {
2315 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2316 frame->check = slf_check_semaphore_wait;
2317 }
2318}
2319
2320/* signal */
2321
2322static void
2323coro_signal_wake (pTHX_ AV *av, int count)
2324{
2325 SvIVX (AvARRAY (av)[0]) = 0;
2326
2327 /* now signal count waiters */
2328 while (count > 0 && AvFILLp (av) > 0)
2329 {
2330 SV *cb;
2331
2332 /* swap first two elements so we can shift a waiter */
2333 cb = AvARRAY (av)[0];
2334 AvARRAY (av)[0] = AvARRAY (av)[1];
2335 AvARRAY (av)[1] = cb;
2336
2337 cb = av_shift (av);
2338
2339 api_ready (aTHX_ cb);
2340 sv_setiv (cb, 0); /* signal waiter */
2341 SvREFCNT_dec (cb);
2342
2343 --count;
2344 }
2345}
2346
2347static int
2348slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2349{
2350 /* if we are about to throw, also stop waiting */
2351 return SvROK ((SV *)frame->data) && !CORO_THROW;
2352}
2353
2354static void
2355slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2356{
2357 AV *av = (AV *)SvRV (arg [0]);
2358
2359 if (SvIVX (AvARRAY (av)[0]))
2360 {
2361 SvIVX (AvARRAY (av)[0]) = 0;
2362 frame->prepare = prepare_nop;
2363 frame->check = slf_check_nop;
2364 }
2365 else
2366 {
2367 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2368
2369 av_push (av, waiter);
2370
2371 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2372 frame->prepare = prepare_schedule;
2373 frame->check = slf_check_signal_wait;
2374 }
2112} 2375}
2113 2376
2114/*****************************************************************************/ 2377/*****************************************************************************/
2115/* Coro::AIO */ 2378/* Coro::AIO */
2116 2379
2165 2428
2166static int 2429static int
2167slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2430slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2168{ 2431{
2169 AV *state = (AV *)frame->data; 2432 AV *state = (AV *)frame->data;
2433
2434 /* if we are about to throw, return early */
2435 /* this does not cancel the aio request, but at least */
2436 /* it quickly returns */
2437 if (CORO_THROW)
2438 return 0;
2170 2439
2171 /* one element that is an RV? repeat! */ 2440 /* one element that is an RV? repeat! */
2172 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2441 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2173 return 1; 2442 return 1;
2174 2443
2350 CODE: 2619 CODE:
2351{ 2620{
2352 struct coro *coro; 2621 struct coro *coro;
2353 MAGIC *mg; 2622 MAGIC *mg;
2354 HV *hv; 2623 HV *hv;
2624 CV *cb;
2355 int i; 2625 int i;
2626
2627 if (items > 1)
2628 {
2629 cb = coro_sv_2cv (ST (1));
2630
2631 if (CvISXSUB (cb))
2632 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2633
2634 if (!CvROOT (cb))
2635 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2636 }
2356 2637
2357 Newz (0, coro, 1, struct coro); 2638 Newz (0, coro, 1, struct coro);
2358 coro->args = newAV (); 2639 coro->args = newAV ();
2359 coro->flags = CF_NEW; 2640 coro->flags = CF_NEW;
2360 2641
2365 coro->hv = hv = newHV (); 2646 coro->hv = hv = newHV ();
2366 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2647 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2367 mg->mg_flags |= MGf_DUP; 2648 mg->mg_flags |= MGf_DUP;
2368 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2649 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2369 2650
2651 if (items > 1)
2652 {
2653 coro->startcv = SvREFCNT_inc_NN (cb);
2654
2370 av_extend (coro->args, items - 1); 2655 av_extend (coro->args, items - 1);
2371 for (i = 1; i < items; i++) 2656 for (i = 2; i < items; i++)
2372 av_push (coro->args, newSVsv (ST (i))); 2657 av_push (coro->args, newSVsv (ST (i)));
2658 }
2373} 2659}
2374 OUTPUT: 2660 OUTPUT:
2375 RETVAL 2661 RETVAL
2376 2662
2377void 2663void
2506throw (Coro::State self, SV *throw = &PL_sv_undef) 2792throw (Coro::State self, SV *throw = &PL_sv_undef)
2507 PROTOTYPE: $;$ 2793 PROTOTYPE: $;$
2508 CODE: 2794 CODE:
2509{ 2795{
2510 struct coro *current = SvSTATE_current; 2796 struct coro *current = SvSTATE_current;
2511 SV **throwp = self == current ? &coro_throw : &self->throw; 2797 SV **throwp = self == current ? &CORO_THROW : &self->except;
2512 SvREFCNT_dec (*throwp); 2798 SvREFCNT_dec (*throwp);
2513 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2799 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2514} 2800}
2515 2801
2516void 2802void
2605 coroapi.ready = api_ready; 2891 coroapi.ready = api_ready;
2606 coroapi.is_ready = api_is_ready; 2892 coroapi.is_ready = api_is_ready;
2607 coroapi.nready = coro_nready; 2893 coroapi.nready = coro_nready;
2608 coroapi.current = coro_current; 2894 coroapi.current = coro_current;
2609 2895
2610 GCoroAPI = &coroapi; 2896 /*GCoroAPI = &coroapi;*/
2611 sv_setiv (sv, (IV)&coroapi); 2897 sv_setiv (sv, (IV)&coroapi);
2612 SvREADONLY_on (sv); 2898 SvREADONLY_on (sv);
2613 } 2899 }
2614} 2900}
2615 2901
2751 api_trace (aTHX_ coro_current, 0); 3037 api_trace (aTHX_ coro_current, 0);
2752 3038
2753 av_push (av_async_pool, newSVsv (coro_current)); 3039 av_push (av_async_pool, newSVsv (coro_current));
2754} 3040}
2755 3041
3042SV *
3043rouse_cb ()
3044 PROTOTYPE:
3045 CODE:
3046 RETVAL = coro_new_rouse_cb (aTHX);
3047 OUTPUT:
3048 RETVAL
3049
3050void
3051rouse_wait (...)
3052 PROTOTYPE: ;$
3053 PPCODE:
3054 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3055
2756 3056
2757MODULE = Coro::State PACKAGE = PerlIO::cede 3057MODULE = Coro::State PACKAGE = PerlIO::cede
2758 3058
2759BOOT: 3059BOOT:
2760 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3060 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2761 3061
2762 3062
2763MODULE = Coro::State PACKAGE = Coro::Semaphore 3063MODULE = Coro::State PACKAGE = Coro::Semaphore
2764 3064
2765SV * 3065SV *
2766new (SV *klass, SV *count_ = 0) 3066new (SV *klass, SV *count = 0)
2767 CODE: 3067 CODE:
2768{ 3068 RETVAL = sv_bless (
2769 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3069 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2770 AV *av = newAV (); 3070 GvSTASH (CvGV (cv))
2771 SV **ary; 3071 );
3072 OUTPUT:
3073 RETVAL
2772 3074
2773 /* unfortunately, building manually saves memory */ 3075# helper for Coro::Channel
2774 Newx (ary, 2, SV *); 3076SV *
2775 AvALLOC (av) = ary; 3077_alloc (int count)
2776 AvARRAY (av) = ary; 3078 CODE:
2777 AvMAX (av) = 1; 3079 RETVAL = coro_waitarray_new (aTHX_ count);
2778 AvFILLp (av) = 0;
2779 ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1);
2780
2781 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2782}
2783 OUTPUT: 3080 OUTPUT:
2784 RETVAL 3081 RETVAL
2785 3082
2786SV * 3083SV *
2787count (SV *self) 3084count (SV *self)
2796 adjust = 1 3093 adjust = 1
2797 CODE: 3094 CODE:
2798 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3095 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2799 3096
2800void 3097void
2801down (SV *self) 3098down (...)
2802 CODE: 3099 CODE:
2803 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3100 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3101
3102void
3103wait (...)
3104 CODE:
3105 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2804 3106
2805void 3107void
2806try (SV *self) 3108try (SV *self)
2807 PPCODE: 3109 PPCODE:
2808{ 3110{
2820 XSRETURN_NO; 3122 XSRETURN_NO;
2821} 3123}
2822 3124
2823void 3125void
2824waiters (SV *self) 3126waiters (SV *self)
2825 CODE: 3127 PPCODE:
2826{ 3128{
2827 AV *av = (AV *)SvRV (self); 3129 AV *av = (AV *)SvRV (self);
3130 int wcount = AvFILLp (av) + 1 - 1;
2828 3131
2829 if (GIMME_V == G_SCALAR) 3132 if (GIMME_V == G_SCALAR)
2830 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3133 XPUSHs (sv_2mortal (newSViv (wcount)));
2831 else 3134 else
2832 { 3135 {
2833 int i; 3136 int i;
2834 EXTEND (SP, AvFILLp (av) + 1 - 1); 3137 EXTEND (SP, wcount);
2835 for (i = 1; i <= AvFILLp (av); ++i) 3138 for (i = 1; i <= wcount; ++i)
2836 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3139 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2837 } 3140 }
2838} 3141}
3142
3143MODULE = Coro::State PACKAGE = Coro::Signal
3144
3145SV *
3146new (SV *klass)
3147 CODE:
3148 RETVAL = sv_bless (
3149 coro_waitarray_new (aTHX_ 0),
3150 GvSTASH (CvGV (cv))
3151 );
3152 OUTPUT:
3153 RETVAL
3154
3155void
3156wait (...)
3157 CODE:
3158 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3159
3160void
3161broadcast (SV *self)
3162 CODE:
3163{
3164 AV *av = (AV *)SvRV (self);
3165 coro_signal_wake (aTHX_ av, AvFILLp (av));
3166}
3167
3168void
3169send (SV *self)
3170 CODE:
3171{
3172 AV *av = (AV *)SvRV (self);
3173
3174 if (AvFILLp (av))
3175 coro_signal_wake (aTHX_ av, 1);
3176 else
3177 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3178}
3179
3180IV
3181awaited (SV *self)
3182 CODE:
3183 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3184 OUTPUT:
3185 RETVAL
2839 3186
2840 3187
2841MODULE = Coro::State PACKAGE = Coro::AnyEvent 3188MODULE = Coro::State PACKAGE = Coro::AnyEvent
2842 3189
2843BOOT: 3190BOOT:
2871 3218
2872void 3219void
2873_register (char *target, char *proto, SV *req) 3220_register (char *target, char *proto, SV *req)
2874 CODE: 3221 CODE:
2875{ 3222{
2876 HV *st;
2877 GV *gvp;
2878 CV *req_cv = sv_2cv (req, &st, &gvp, 0); 3223 CV *req_cv = coro_sv_2cv (req);
2879 /* newXSproto doesn't return the CV on 5.8 */ 3224 /* newXSproto doesn't return the CV on 5.8 */
2880 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3225 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
2881 sv_setpv ((SV *)slf_cv, proto); 3226 sv_setpv ((SV *)slf_cv, proto);
2882 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3227 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2883} 3228}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines