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.289 by root, Mon Nov 17 08:25:06 2008 UTC vs.
Revision 1.309 by root, Wed Nov 19 15:29: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
153#endif 156#endif
154 157
155static double (*nvtime)(); /* so why doesn't it take void? */ 158static double (*nvtime)(); /* so why doesn't it take void? */
159
160/* we hijack an hopefully unused CV flag for our purposes */
161#define CVf_SLF 0x4000
162static OP *pp_slf (pTHX);
156 163
157static U32 cctx_gen; 164static U32 cctx_gen;
158static size_t cctx_stacksize = CORO_STACKSIZE; 165static size_t cctx_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 166static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 167static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 168static JMPENV *main_top_env;
162static HV *coro_state_stash, *coro_stash; 169static HV *coro_state_stash, *coro_stash;
170static CV *coro_run_cv;
163static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 171static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
164 172
165static GV *irsgv; /* $/ */ 173static GV *irsgv; /* $/ */
166static GV *stdoutgv; /* *STDOUT */ 174static GV *stdoutgv; /* *STDOUT */
167static SV *rv_diehook; 175static SV *rv_diehook;
239 /* state data */ 247 /* state data */
240 struct CoroSLF slf_frame; /* saved slf frame */ 248 struct CoroSLF slf_frame; /* saved slf frame */
241 AV *mainstack; 249 AV *mainstack;
242 perl_slots *slot; /* basically the saved sp */ 250 perl_slots *slot; /* basically the saved sp */
243 251
252 CV *startcv; /* the CV to execute */
244 AV *args; /* data associated with this coroutine (initial args) */ 253 AV *args; /* data associated with this coroutine (initial args) */
245 int refcnt; /* coroutines are refcounted, yes */ 254 int refcnt; /* coroutines are refcounted, yes */
246 int flags; /* CF_ flags */ 255 int flags; /* CF_ flags */
247 HV *hv; /* the perl hash associated with this coro, if any */ 256 HV *hv; /* the perl hash associated with this coro, if any */
248 void (*on_destroy)(pTHX_ struct coro *coro); 257 void (*on_destroy)(pTHX_ struct coro *coro);
249 258
250 /* statistics */ 259 /* statistics */
251 int usecount; /* number of transfers to this coro */ 260 int usecount; /* number of transfers to this coro */
252 261
253 /* coro process data */ 262 /* coro process data */
254 int prio; 263 int prio;
255 SV *throw; /* exception to be thrown */ 264 SV *except; /* exception to be thrown */
265 SV *rouse_cb;
256 266
257 /* async_pool */ 267 /* async_pool */
258 SV *saved_deffh; 268 SV *saved_deffh;
259 269
260 /* linked list */ 270 /* linked list */
266 276
267/* the following variables are effectively part of the perl context */ 277/* the following variables are effectively part of the perl context */
268/* and get copied between struct coro and these variables */ 278/* and get copied between struct coro and these variables */
269/* the mainr easonw e don't support windows process emulation */ 279/* the mainr easonw e don't support windows process emulation */
270static struct CoroSLF slf_frame; /* the current slf frame */ 280static struct CoroSLF slf_frame; /* the current slf frame */
271static SV *coro_throw;
272 281
273/** Coro ********************************************************************/ 282/** Coro ********************************************************************/
274 283
275#define PRIO_MAX 3 284#define PRIO_MAX 3
276#define PRIO_HIGH 1 285#define PRIO_HIGH 1
314#if PERL_VERSION_ATLEAST (5,10,0) 323#if PERL_VERSION_ATLEAST (5,10,0)
315 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 324 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
316 get_hv (name, create); 325 get_hv (name, create);
317#endif 326#endif
318 return get_hv (name, create); 327 return get_hv (name, create);
328}
329
330/* may croak */
331INLINE CV *
332coro_sv_2cv (SV *sv)
333{
334 HV *st;
335 GV *gvp;
336 return sv_2cv (sv, &st, &gvp, 0);
319} 337}
320 338
321static AV * 339static AV *
322coro_clone_padlist (pTHX_ CV *cv) 340coro_clone_padlist (pTHX_ CV *cv)
323{ 341{
442 else 460 else
443 { 461 {
444#if CORO_PREFER_PERL_FUNCTIONS 462#if CORO_PREFER_PERL_FUNCTIONS
445 /* this is probably cleaner? but also slower! */ 463 /* this is probably cleaner? but also slower! */
446 /* in practise, it seems to be less stable */ 464 /* in practise, it seems to be less stable */
447 CV *cp = Perl_cv_clone (cv); 465 CV *cp = Perl_cv_clone (aTHX_ cv);
448 CvPADLIST (cv) = CvPADLIST (cp); 466 CvPADLIST (cv) = CvPADLIST (cp);
449 CvPADLIST (cp) = 0; 467 CvPADLIST (cp) = 0;
450 SvREFCNT_dec (cp); 468 SvREFCNT_dec (cp);
451#else 469#else
452 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 470 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
505 523
506 PUTBACK; 524 PUTBACK;
507 } 525 }
508 526
509 slf_frame = c->slf_frame; 527 slf_frame = c->slf_frame;
510 coro_throw = c->throw; 528 CORO_THROW = c->except;
511} 529}
512 530
513static void 531static void
514save_perl (pTHX_ Coro__State c) 532save_perl (pTHX_ Coro__State c)
515{ 533{
516 c->throw = coro_throw; 534 c->except = CORO_THROW;
517 c->slf_frame = slf_frame; 535 c->slf_frame = slf_frame;
518 536
519 { 537 {
520 dSP; 538 dSP;
521 I32 cxix = cxstack_ix; 539 I32 cxix = cxstack_ix;
596 * of perl.c:init_stacks, except that it uses less memory 614 * of perl.c:init_stacks, except that it uses less memory
597 * on the (sometimes correct) assumption that coroutines do 615 * on the (sometimes correct) assumption that coroutines do
598 * not usually need a lot of stackspace. 616 * not usually need a lot of stackspace.
599 */ 617 */
600#if CORO_PREFER_PERL_FUNCTIONS 618#if CORO_PREFER_PERL_FUNCTIONS
601# define coro_init_stacks init_stacks 619# define coro_init_stacks(thx) init_stacks ()
602#else 620#else
603static void 621static void
604coro_init_stacks (pTHX) 622coro_init_stacks (pTHX)
605{ 623{
606 PL_curstackinfo = new_stackinfo(32, 8); 624 PL_curstackinfo = new_stackinfo(32, 8);
814slf_check_nop (pTHX_ struct CoroSLF *frame) 832slf_check_nop (pTHX_ struct CoroSLF *frame)
815{ 833{
816 return 0; 834 return 0;
817} 835}
818 836
837static UNOP coro_setup_op;
838
819static void NOINLINE /* noinline to keep it out of the transfer fast path */ 839static void NOINLINE /* noinline to keep it out of the transfer fast path */
820coro_setup (pTHX_ struct coro *coro) 840coro_setup (pTHX_ struct coro *coro)
821{ 841{
822 /* 842 /*
823 * emulate part of the perl startup here. 843 * emulate part of the perl startup here.
851 { 871 {
852 dSP; 872 dSP;
853 UNOP myop; 873 UNOP myop;
854 874
855 Zero (&myop, 1, UNOP); 875 Zero (&myop, 1, UNOP);
856 myop.op_next = Nullop; 876 myop.op_next = Nullop;
877 myop.op_type = OP_ENTERSUB;
857 myop.op_flags = OPf_WANT_VOID; 878 myop.op_flags = OPf_WANT_VOID;
858 879
859 PUSHMARK (SP); 880 PUSHMARK (SP);
860 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 881 PUSHs ((SV *)coro->startcv);
861 PUTBACK; 882 PUTBACK;
862 PL_op = (OP *)&myop; 883 PL_op = (OP *)&myop;
863 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 884 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
864 SPAGAIN;
865 } 885 }
866 886
867 /* this newly created coroutine might be run on an existing cctx which most 887 /* this newly created coroutine might be run on an existing cctx which most
868 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 888 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
869 */ 889 */
870 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 890 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
871 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 891 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
872 892
873 coro_throw = coro->throw; 893 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
894 coro_setup_op.op_next = PL_op;
895 coro_setup_op.op_type = OP_CUSTOM;
896 coro_setup_op.op_ppaddr = pp_slf;
897 /* no flags etc. required, as an init function won't be called */
898
899 PL_op = (OP *)&coro_setup_op;
900
901 /* copy throw, in case it was set before coro_setup */
902 CORO_THROW = coro->except;
874} 903}
875 904
876static void 905static void
877coro_destruct (pTHX_ struct coro *coro) 906coro_destruct (pTHX_ struct coro *coro)
878{ 907{
902 931
903 SvREFCNT_dec (PL_diehook); 932 SvREFCNT_dec (PL_diehook);
904 SvREFCNT_dec (PL_warnhook); 933 SvREFCNT_dec (PL_warnhook);
905 934
906 SvREFCNT_dec (coro->saved_deffh); 935 SvREFCNT_dec (coro->saved_deffh);
907 SvREFCNT_dec (coro_throw); 936 SvREFCNT_dec (coro->rouse_cb);
908 937
909 coro_destruct_stacks (aTHX); 938 coro_destruct_stacks (aTHX);
910} 939}
911 940
912INLINE void 941INLINE void
999 SAVETMPS; 1028 SAVETMPS;
1000 EXTEND (SP, 3); 1029 EXTEND (SP, 3);
1001 PUSHMARK (SP); 1030 PUSHMARK (SP);
1002 PUSHs (&PL_sv_yes); 1031 PUSHs (&PL_sv_yes);
1003 PUSHs (fullname); 1032 PUSHs (fullname);
1004 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1033 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1005 PUTBACK; 1034 PUTBACK;
1006 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1035 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1007 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1036 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1008 SPAGAIN; 1037 SPAGAIN;
1009 FREETMPS; 1038 FREETMPS;
1041 1070
1042 TAINT_NOT; 1071 TAINT_NOT;
1043 return 0; 1072 return 0;
1044} 1073}
1045 1074
1075static struct coro_cctx *cctx_ssl_cctx;
1076static struct CoroSLF cctx_ssl_frame;
1077
1046static void 1078static void
1047prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) 1079slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1048{ 1080{
1049 ta->prev = (struct coro *)cctx; 1081 ta->prev = (struct coro *)cctx_ssl_cctx;
1050 ta->next = 0; 1082 ta->next = 0;
1051} 1083}
1052 1084
1053/* inject a fake call to Coro::State::_cctx_init into the execution */ 1085static int
1054/* _cctx_init should be careful, as it could be called at almost any time */ 1086slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1055/* during execution of a perl program */ 1087{
1056/* also initialises PL_top_env */ 1088 *frame = cctx_ssl_frame;
1089
1090 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1091}
1092
1093/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1057static void NOINLINE 1094static void NOINLINE
1058cctx_prepare (pTHX_ coro_cctx *cctx) 1095cctx_prepare (pTHX_ coro_cctx *cctx)
1059{ 1096{
1060 dSP;
1061 UNOP myop;
1062
1063 PL_top_env = &PL_start_env; 1097 PL_top_env = &PL_start_env;
1064 1098
1065 if (cctx->flags & CC_TRACE) 1099 if (cctx->flags & CC_TRACE)
1066 PL_runops = runops_trace; 1100 PL_runops = runops_trace;
1067 1101
1068 Zero (&myop, 1, UNOP); 1102 /* we already must be executing an SLF op, there is no other valid way
1069 myop.op_next = PL_op; 1103 * that can lead to creation of a new cctx */
1070 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1104 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1105 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1071 1106
1072 PUSHMARK (SP); 1107 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1073 EXTEND (SP, 2); 1108 cctx_ssl_cctx = cctx;
1074 PUSHs (sv_2mortal (newSViv ((IV)cctx))); 1109 cctx_ssl_frame = slf_frame;
1075 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1110
1076 PUTBACK; 1111 slf_frame.prepare = slf_prepare_set_stacklevel;
1077 PL_op = (OP *)&myop; 1112 slf_frame.check = slf_check_set_stacklevel;
1078 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1079 SPAGAIN;
1080} 1113}
1081 1114
1082/* the tail of transfer: execute stuff we can only do after a transfer */ 1115/* the tail of transfer: execute stuff we can only do after a transfer */
1083INLINE void 1116INLINE void
1084transfer_tail (pTHX) 1117transfer_tail (pTHX)
1111 transfer_tail (aTHX); 1144 transfer_tail (aTHX);
1112 1145
1113 /* somebody or something will hit me for both perl_run and PL_restartop */ 1146 /* somebody or something will hit me for both perl_run and PL_restartop */
1114 PL_restartop = PL_op; 1147 PL_restartop = PL_op;
1115 perl_run (PL_curinterp); 1148 perl_run (PL_curinterp);
1149 /*
1150 * Unfortunately, there is no way to get at the return values of the
1151 * coro body here, as perl_run destroys these
1152 */
1116 1153
1117 /* 1154 /*
1118 * If perl-run returns we assume exit() was being called or the coro 1155 * If perl-run returns we assume exit() was being called or the coro
1119 * fell off the end, which seems to be the only valid (non-bug) 1156 * fell off the end, which seems to be the only valid (non-bug)
1120 * reason for perl_run to return. We try to exit by jumping to the 1157 * reason for perl_run to return. We try to exit by jumping to the
1300 dSTACKLEVEL; 1337 dSTACKLEVEL;
1301 1338
1302 /* sometimes transfer is only called to set idle_sp */ 1339 /* sometimes transfer is only called to set idle_sp */
1303 if (expect_false (!next)) 1340 if (expect_false (!next))
1304 { 1341 {
1305 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1342 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1306 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1343 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1307 } 1344 }
1308 else if (expect_true (prev != next)) 1345 else if (expect_true (prev != next))
1309 { 1346 {
1310 coro_cctx *prev__cctx; 1347 coro_cctx *prev__cctx;
1335 1372
1336 prev__cctx = prev->cctx; 1373 prev__cctx = prev->cctx;
1337 1374
1338 /* possibly untie and reuse the cctx */ 1375 /* possibly untie and reuse the cctx */
1339 if (expect_true ( 1376 if (expect_true (
1340 prev__cctx->idle_sp == (void *)stacklevel 1377 prev__cctx->idle_sp == STACKLEVEL
1341 && !(prev__cctx->flags & CC_TRACE) 1378 && !(prev__cctx->flags & CC_TRACE)
1342 && !force_cctx 1379 && !force_cctx
1343 )) 1380 ))
1344 { 1381 {
1345 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1382 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1412 1449
1413 coro->slot = 0; 1450 coro->slot = 0;
1414 } 1451 }
1415 1452
1416 cctx_destroy (coro->cctx); 1453 cctx_destroy (coro->cctx);
1454 SvREFCNT_dec (coro->startcv);
1417 SvREFCNT_dec (coro->args); 1455 SvREFCNT_dec (coro->args);
1456 SvREFCNT_dec (CORO_THROW);
1418 1457
1419 if (coro->next) coro->next->prev = coro->prev; 1458 if (coro->next) coro->next->prev = coro->prev;
1420 if (coro->prev) coro->prev->next = coro->next; 1459 if (coro->prev) coro->prev->next = coro->next;
1421 if (coro == coro_first) coro_first = coro->next; 1460 if (coro == coro_first) coro_first = coro->next;
1422 1461
1476 1515
1477 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1516 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1478 TRANSFER (ta, 1); 1517 TRANSFER (ta, 1);
1479} 1518}
1480 1519
1520/*****************************************************************************/
1521/* gensub: simple closure generation utility */
1522
1523#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1524
1525/* create a closure from XS, returns a code reference */
1526/* the arg can be accessed via GENSUB_ARG from the callback */
1527/* the callback must use dXSARGS/XSRETURN */
1528static SV *
1529gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1530{
1531 CV *cv = (CV *)newSV (0);
1532
1533 sv_upgrade ((SV *)cv, SVt_PVCV);
1534
1535 CvANON_on (cv);
1536 CvISXSUB_on (cv);
1537 CvXSUB (cv) = xsub;
1538 GENSUB_ARG = arg;
1539
1540 return newRV_noinc ((SV *)cv);
1541}
1542
1481/** Coro ********************************************************************/ 1543/** Coro ********************************************************************/
1482 1544
1483INLINE void 1545INLINE void
1484coro_enq (pTHX_ struct coro *coro) 1546coro_enq (pTHX_ struct coro *coro)
1485{ 1547{
1528 ENTER; 1590 ENTER;
1529 SAVETMPS; 1591 SAVETMPS;
1530 1592
1531 PUSHMARK (SP); 1593 PUSHMARK (SP);
1532 PUTBACK; 1594 PUTBACK;
1533 call_sv (sv_hook, G_DISCARD); 1595 call_sv (sv_hook, G_VOID | G_DISCARD);
1534 SPAGAIN;
1535 1596
1536 FREETMPS; 1597 FREETMPS;
1537 LEAVE; 1598 LEAVE;
1538 } 1599 }
1539 1600
1566 ENTER; 1627 ENTER;
1567 SAVETMPS; 1628 SAVETMPS;
1568 1629
1569 PUSHMARK (SP); 1630 PUSHMARK (SP);
1570 PUTBACK; 1631 PUTBACK;
1571 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1632 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1572 SPAGAIN;
1573 1633
1574 FREETMPS; 1634 FREETMPS;
1575 LEAVE; 1635 LEAVE;
1576 continue; 1636 continue;
1577 } 1637 }
1687 coro->slot->runops = RUNOPS_DEFAULT; 1747 coro->slot->runops = RUNOPS_DEFAULT;
1688 } 1748 }
1689} 1749}
1690 1750
1691/*****************************************************************************/ 1751/*****************************************************************************/
1752/* rouse callback */
1753
1754#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1755
1756static void
1757coro_rouse_callback (pTHX_ CV *cv)
1758{
1759 dXSARGS;
1760 SV *data = (SV *)GENSUB_ARG;
1761
1762 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1763 {
1764 /* first call, set args */
1765 int i;
1766 AV *av = newAV ();
1767 SV *coro = SvRV (data);
1768
1769 SvRV_set (data, (SV *)av);
1770 api_ready (aTHX_ coro);
1771 SvREFCNT_dec (coro);
1772
1773 /* better take a full copy of the arguments */
1774 while (items--)
1775 av_store (av, items, newSVsv (ST (items)));
1776 }
1777
1778 XSRETURN_EMPTY;
1779}
1780
1781static int
1782slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1783{
1784 SV *data = (SV *)frame->data;
1785
1786 if (CORO_THROW)
1787 return 0;
1788
1789 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1790 return 1;
1791
1792 /* now push all results on the stack */
1793 {
1794 dSP;
1795 AV *av = (AV *)SvRV (data);
1796 int i;
1797
1798 EXTEND (SP, AvFILLp (av) + 1);
1799 for (i = 0; i <= AvFILLp (av); ++i)
1800 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1801
1802 /* we have stolen the elements, so ste length to zero and free */
1803 AvFILLp (av) = -1;
1804 av_undef (av);
1805
1806 PUTBACK;
1807 }
1808
1809 return 0;
1810}
1811
1812static void
1813slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1814{
1815 SV *cb;
1816
1817 if (items)
1818 cb = arg [0];
1819 else
1820 {
1821 struct coro *coro = SvSTATE_current;
1822
1823 if (!coro->rouse_cb)
1824 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
1825
1826 cb = sv_2mortal (coro->rouse_cb);
1827 coro->rouse_cb = 0;
1828 }
1829
1830 if (!SvROK (cb)
1831 || SvTYPE (SvRV (cb)) != SVt_PVCV
1832 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
1833 croak ("Coro::rouse_wait called with illegal callback argument,");
1834
1835 {
1836 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
1837 SV *data = (SV *)GENSUB_ARG;
1838
1839 frame->data = (void *)data;
1840 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
1841 frame->check = slf_check_rouse_wait;
1842 }
1843}
1844
1845static SV *
1846coro_new_rouse_cb (pTHX)
1847{
1848 HV *hv = (HV *)SvRV (coro_current);
1849 struct coro *coro = SvSTATE_hv (hv);
1850 SV *data = newRV_inc ((SV *)hv);
1851 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1852
1853 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
1854 SvREFCNT_dec (data); /* magicext increases the refcount */
1855
1856 SvREFCNT_dec (coro->rouse_cb);
1857 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1858
1859 return cb;
1860}
1861
1862/*****************************************************************************/
1692/* schedule-like-function opcode (SLF) */ 1863/* schedule-like-function opcode (SLF) */
1693 1864
1694static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ 1865static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1695static const CV *slf_cv; 1866static const CV *slf_cv;
1696static SV **slf_argv; 1867static SV **slf_argv;
1717 1888
1718 RETURNOP (slf_restore.op_first); 1889 RETURNOP (slf_restore.op_first);
1719} 1890}
1720 1891
1721static void 1892static void
1722slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1723{
1724 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1725}
1726
1727static void
1728slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1729{
1730 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1731
1732 frame->prepare = slf_prepare_set_stacklevel;
1733 frame->check = slf_check_nop;
1734 frame->data = (void *)SvIV (arg [0]);
1735}
1736
1737static void
1738slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 1893slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1739{ 1894{
1740 SV **arg = (SV **)slf_frame.data; 1895 SV **arg = (SV **)slf_frame.data;
1741 1896
1742 prepare_transfer (aTHX_ ta, arg [0], arg [1]); 1897 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1771slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 1926slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1772{ 1927{
1773 frame->prepare = prepare_cede_notself; 1928 frame->prepare = prepare_cede_notself;
1774 frame->check = slf_check_nop; 1929 frame->check = slf_check_nop;
1775} 1930}
1776
1777/* we hijack an hopefully unused CV flag for our purposes */
1778#define CVf_SLF 0x4000
1779 1931
1780/* 1932/*
1781 * these not obviously related functions are all rolled into one 1933 * these not obviously related functions are all rolled into one
1782 * function to increase chances that they all will call transfer with the same 1934 * function to increase chances that they all will call transfer with the same
1783 * stack offset 1935 * stack offset
1839 } 1991 }
1840 while (slf_frame.check (aTHX_ &slf_frame)); 1992 while (slf_frame.check (aTHX_ &slf_frame));
1841 1993
1842 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 1994 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1843 1995
1844 /* return value handling - mostly like entersub */
1845 {
1846 dSP;
1847 SV **bot = PL_stack_base + checkmark;
1848 int gimme = GIMME_V;
1849
1850 /* make sure we put something on the stack in scalar context */
1851 if (gimme == G_SCALAR)
1852 {
1853 if (sp == bot)
1854 XPUSHs (&PL_sv_undef);
1855
1856 SP = bot + 1;
1857 }
1858
1859 PUTBACK;
1860 }
1861
1862 /* exception handling */ 1996 /* exception handling */
1863 if (expect_false (coro_throw)) 1997 if (expect_false (CORO_THROW))
1864 { 1998 {
1865 SV *exception = sv_2mortal (coro_throw); 1999 SV *exception = sv_2mortal (CORO_THROW);
1866 2000
1867 coro_throw = 0; 2001 CORO_THROW = 0;
1868 sv_setsv (ERRSV, exception); 2002 sv_setsv (ERRSV, exception);
1869 croak (0); 2003 croak (0);
2004 }
2005
2006 /* return value handling - mostly like entersub */
2007 /* make sure we put something on the stack in scalar context */
2008 if (GIMME_V == G_SCALAR)
2009 {
2010 dSP;
2011 SV **bot = PL_stack_base + checkmark;
2012
2013 if (sp == bot) /* too few, push undef */
2014 bot [1] = &PL_sv_undef;
2015 else if (sp != bot + 1) /* too many, take last one */
2016 bot [1] = *sp;
2017
2018 SP = bot + 1;
2019
2020 PUTBACK;
1870 } 2021 }
1871 2022
1872 return NORMAL; 2023 return NORMAL;
1873} 2024}
1874 2025
1996 PerlIOBuf_get_cnt, 2147 PerlIOBuf_get_cnt,
1997 PerlIOBuf_set_ptrcnt, 2148 PerlIOBuf_set_ptrcnt,
1998}; 2149};
1999 2150
2000/*****************************************************************************/ 2151/*****************************************************************************/
2152/* Coro::Semaphore & Coro::Signal */
2153
2154static SV *
2155coro_waitarray_new (pTHX_ int count)
2156{
2157 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2158 AV *av = newAV ();
2159 SV **ary;
2160
2161 /* unfortunately, building manually saves memory */
2162 Newx (ary, 2, SV *);
2163 AvALLOC (av) = ary;
2164 /*AvARRAY (av) = ary;*/
2165 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2166 AvMAX (av) = 1;
2167 AvFILLp (av) = 0;
2168 ary [0] = newSViv (count);
2169
2170 return newRV_noinc ((SV *)av);
2171}
2172
2001/* Coro::Semaphore */ 2173/* semaphore */
2002 2174
2003static void 2175static void
2004coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2176coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2005{ 2177{
2006 SV *count_sv = AvARRAY (av)[0]; 2178 SV *count_sv = AvARRAY (av)[0];
2018 AvARRAY (av)[0] = AvARRAY (av)[1]; 2190 AvARRAY (av)[0] = AvARRAY (av)[1];
2019 AvARRAY (av)[1] = count_sv; 2191 AvARRAY (av)[1] = count_sv;
2020 cb = av_shift (av); 2192 cb = av_shift (av);
2021 2193
2022 if (SvOBJECT (cb)) 2194 if (SvOBJECT (cb))
2195 {
2023 api_ready (aTHX_ cb); 2196 api_ready (aTHX_ cb);
2024 else 2197 --count;
2025 croak ("callbacks not yet supported"); 2198 }
2199 else if (SvTYPE (cb) == SVt_PVCV)
2200 {
2201 dSP;
2202 PUSHMARK (SP);
2203 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2204 PUTBACK;
2205 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2206 }
2026 2207
2027 SvREFCNT_dec (cb); 2208 SvREFCNT_dec (cb);
2028
2029 --count;
2030 } 2209 }
2031} 2210}
2032 2211
2033static void 2212static void
2034coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2213coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2036 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2215 /* call $sem->adjust (0) to possibly wake up some other waiters */
2037 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2216 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2038} 2217}
2039 2218
2040static int 2219static int
2041slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2220slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2042{ 2221{
2043 AV *av = (AV *)frame->data; 2222 AV *av = (AV *)frame->data;
2044 SV *count_sv = AvARRAY (av)[0]; 2223 SV *count_sv = AvARRAY (av)[0];
2045 2224
2225 /* if we are about to throw, don't actually acquire the lock, just throw */
2226 if (CORO_THROW)
2227 return 0;
2046 if (SvIVX (count_sv) > 0) 2228 else if (SvIVX (count_sv) > 0)
2047 { 2229 {
2048 SvSTATE_current->on_destroy = 0; 2230 SvSTATE_current->on_destroy = 0;
2231
2232 if (acquire)
2049 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2233 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2234 else
2235 coro_semaphore_adjust (aTHX_ av, 0);
2236
2050 return 0; 2237 return 0;
2051 } 2238 }
2052 else 2239 else
2053 { 2240 {
2054 int i; 2241 int i;
2063 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2250 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2064 return 1; 2251 return 1;
2065 } 2252 }
2066} 2253}
2067 2254
2068static void 2255static int
2256slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2257{
2258 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2259}
2260
2261static int
2262slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2263{
2264 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2265}
2266
2267static void
2069slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2268slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2070{ 2269{
2071 AV *av = (AV *)SvRV (arg [0]); 2270 AV *av = (AV *)SvRV (arg [0]);
2072 2271
2073 if (SvIVX (AvARRAY (av)[0]) > 0) 2272 if (SvIVX (AvARRAY (av)[0]) > 0)
2074 { 2273 {
2075 frame->data = (void *)av; 2274 frame->data = (void *)av;
2076 frame->prepare = prepare_nop; 2275 frame->prepare = prepare_nop;
2077 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2078 } 2276 }
2079 else 2277 else
2080 { 2278 {
2081 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2279 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2082 2280
2083 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2281 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2084 frame->prepare = prepare_schedule; 2282 frame->prepare = prepare_schedule;
2085 2283
2086 /* to avoid race conditions when a woken-up coro gets terminated */ 2284 /* to avoid race conditions when a woken-up coro gets terminated */
2087 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2285 /* we arrange for a temporary on_destroy that calls adjust (0) */
2088 assert (!SvSTATE_current->on_destroy);//D
2089 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2286 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2090 } 2287 }
2288}
2091 2289
2290static void
2291slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2292{
2293 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2092 frame->check = slf_check_semaphore_down; 2294 frame->check = slf_check_semaphore_down;
2093
2094} 2295}
2095 2296
2096/*****************************************************************************/ 2297static void
2097/* gensub: simple closure generation utility */ 2298slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2098
2099#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2100
2101/* create a closure from XS, returns a code reference */
2102/* the arg can be accessed via GENSUB_ARG from the callback */
2103/* the callback must use dXSARGS/XSRETURN */
2104static SV *
2105gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2106{ 2299{
2107 CV *cv = (CV *)newSV (0); 2300 if (items >= 2)
2301 {
2302 /* callback form */
2303 AV *av = (AV *)SvRV (arg [0]);
2304 CV *cb_cv = coro_sv_2cv (arg [1]);
2108 2305
2109 sv_upgrade ((SV *)cv, SVt_PVCV); 2306 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2110 2307
2111 CvANON_on (cv); 2308 if (SvIVX (AvARRAY (av)[0]) > 0)
2112 CvISXSUB_on (cv); 2309 coro_semaphore_adjust (aTHX_ av, 0);
2113 CvXSUB (cv) = xsub;
2114 GENSUB_ARG = arg;
2115 2310
2116 return newRV_noinc ((SV *)cv); 2311 frame->prepare = prepare_nop;
2312 frame->check = slf_check_nop;
2313 }
2314 else
2315 {
2316 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2317 frame->check = slf_check_semaphore_wait;
2318 }
2319}
2320
2321/* signal */
2322
2323static void
2324coro_signal_wake (pTHX_ AV *av, int count)
2325{
2326 SvIVX (AvARRAY (av)[0]) = 0;
2327
2328 /* now signal count waiters */
2329 while (count > 0 && AvFILLp (av) > 0)
2330 {
2331 SV *cb;
2332
2333 /* swap first two elements so we can shift a waiter */
2334 cb = AvARRAY (av)[0];
2335 AvARRAY (av)[0] = AvARRAY (av)[1];
2336 AvARRAY (av)[1] = cb;
2337
2338 cb = av_shift (av);
2339
2340 api_ready (aTHX_ cb);
2341 sv_setiv (cb, 0); /* signal waiter */
2342 SvREFCNT_dec (cb);
2343
2344 --count;
2345 }
2346}
2347
2348static int
2349slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2350{
2351 /* if we are about to throw, also stop waiting */
2352 return SvROK ((SV *)frame->data) && !CORO_THROW;
2353}
2354
2355static void
2356slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2357{
2358 AV *av = (AV *)SvRV (arg [0]);
2359
2360 if (SvIVX (AvARRAY (av)[0]))
2361 {
2362 SvIVX (AvARRAY (av)[0]) = 0;
2363 frame->prepare = prepare_nop;
2364 frame->check = slf_check_nop;
2365 }
2366 else
2367 {
2368 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2369
2370 av_push (av, waiter);
2371
2372 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2373 frame->prepare = prepare_schedule;
2374 frame->check = slf_check_signal_wait;
2375 }
2117} 2376}
2118 2377
2119/*****************************************************************************/ 2378/*****************************************************************************/
2120/* Coro::AIO */ 2379/* Coro::AIO */
2121 2380
2170 2429
2171static int 2430static int
2172slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2431slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2173{ 2432{
2174 AV *state = (AV *)frame->data; 2433 AV *state = (AV *)frame->data;
2434
2435 /* if we are about to throw, return early */
2436 /* this does not cancel the aio request, but at least */
2437 /* it quickly returns */
2438 if (CORO_THROW)
2439 return 0;
2175 2440
2176 /* one element that is an RV? repeat! */ 2441 /* one element that is an RV? repeat! */
2177 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2442 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2178 return 1; 2443 return 1;
2179 2444
2350 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2615 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2351} 2616}
2352 2617
2353SV * 2618SV *
2354new (char *klass, ...) 2619new (char *klass, ...)
2620 ALIAS:
2621 Coro::new = 1
2355 CODE: 2622 CODE:
2356{ 2623{
2357 struct coro *coro; 2624 struct coro *coro;
2358 MAGIC *mg; 2625 MAGIC *mg;
2359 HV *hv; 2626 HV *hv;
2627 CV *cb;
2360 int i; 2628 int i;
2629
2630 if (items > 1)
2631 {
2632 cb = coro_sv_2cv (ST (1));
2633
2634 if (CvISXSUB (cb))
2635 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2636
2637 if (!CvROOT (cb))
2638 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2639 }
2361 2640
2362 Newz (0, coro, 1, struct coro); 2641 Newz (0, coro, 1, struct coro);
2363 coro->args = newAV (); 2642 coro->args = newAV ();
2364 coro->flags = CF_NEW; 2643 coro->flags = CF_NEW;
2365 2644
2370 coro->hv = hv = newHV (); 2649 coro->hv = hv = newHV ();
2371 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2650 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2372 mg->mg_flags |= MGf_DUP; 2651 mg->mg_flags |= MGf_DUP;
2373 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2652 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2374 2653
2654 if (items > 1)
2655 {
2375 av_extend (coro->args, items - 1); 2656 av_extend (coro->args, items - 1 + ix);
2657
2658 if (ix)
2659 {
2660 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2661 cb = coro_run_cv;
2662 }
2663
2664 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2665
2376 for (i = 1; i < items; i++) 2666 for (i = 2; i < items; i++)
2377 av_push (coro->args, newSVsv (ST (i))); 2667 av_push (coro->args, newSVsv (ST (i)));
2668 }
2378} 2669}
2379 OUTPUT: 2670 OUTPUT:
2380 RETVAL 2671 RETVAL
2381
2382void
2383_set_stacklevel (...)
2384 CODE:
2385 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2386 2672
2387void 2673void
2388transfer (...) 2674transfer (...)
2389 PROTOTYPE: $$ 2675 PROTOTYPE: $$
2390 CODE: 2676 CODE:
2516throw (Coro::State self, SV *throw = &PL_sv_undef) 2802throw (Coro::State self, SV *throw = &PL_sv_undef)
2517 PROTOTYPE: $;$ 2803 PROTOTYPE: $;$
2518 CODE: 2804 CODE:
2519{ 2805{
2520 struct coro *current = SvSTATE_current; 2806 struct coro *current = SvSTATE_current;
2521 SV **throwp = self == current ? &coro_throw : &self->throw; 2807 SV **throwp = self == current ? &CORO_THROW : &self->except;
2522 SvREFCNT_dec (*throwp); 2808 SvREFCNT_dec (*throwp);
2523 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2809 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2524} 2810}
2525 2811
2526void 2812void
2588 int i; 2874 int i;
2589 2875
2590 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 2876 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2591 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2877 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2592 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2878 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2593 2879 coro_run_cv = get_cv (aTHX_ "Coro::_terminate", GV_ADD);
2594 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2880 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE);
2595 SvREADONLY_on (coro_current); 2881 SvREADONLY_on (coro_current);
2596 2882
2597 coro_stash = gv_stashpv ("Coro", TRUE); 2883 coro_stash = gv_stashpv ("Coro", TRUE);
2598 2884
2599 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2885 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2615 coroapi.ready = api_ready; 2901 coroapi.ready = api_ready;
2616 coroapi.is_ready = api_is_ready; 2902 coroapi.is_ready = api_is_ready;
2617 coroapi.nready = coro_nready; 2903 coroapi.nready = coro_nready;
2618 coroapi.current = coro_current; 2904 coroapi.current = coro_current;
2619 2905
2620 GCoroAPI = &coroapi; 2906 /*GCoroAPI = &coroapi;*/
2621 sv_setiv (sv, (IV)&coroapi); 2907 sv_setiv (sv, (IV)&coroapi);
2622 SvREADONLY_on (sv); 2908 SvREADONLY_on (sv);
2623 } 2909 }
2624} 2910}
2625 2911
2761 api_trace (aTHX_ coro_current, 0); 3047 api_trace (aTHX_ coro_current, 0);
2762 3048
2763 av_push (av_async_pool, newSVsv (coro_current)); 3049 av_push (av_async_pool, newSVsv (coro_current));
2764} 3050}
2765 3051
3052SV *
3053rouse_cb ()
3054 PROTOTYPE:
3055 CODE:
3056 RETVAL = coro_new_rouse_cb (aTHX);
3057 OUTPUT:
3058 RETVAL
3059
3060void
3061rouse_wait (...)
3062 PROTOTYPE: ;$
3063 PPCODE:
3064 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3065
2766 3066
2767MODULE = Coro::State PACKAGE = PerlIO::cede 3067MODULE = Coro::State PACKAGE = PerlIO::cede
2768 3068
2769BOOT: 3069BOOT:
2770 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3070 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2771 3071
2772 3072
2773MODULE = Coro::State PACKAGE = Coro::Semaphore 3073MODULE = Coro::State PACKAGE = Coro::Semaphore
2774 3074
2775SV * 3075SV *
2776new (SV *klass, SV *count_ = 0) 3076new (SV *klass, SV *count = 0)
2777 CODE: 3077 CODE:
2778{ 3078 RETVAL = sv_bless (
2779 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3079 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2780 AV *av = newAV (); 3080 GvSTASH (CvGV (cv))
2781 SV **ary; 3081 );
3082 OUTPUT:
3083 RETVAL
2782 3084
2783 /* unfortunately, building manually saves memory */ 3085# helper for Coro::Channel
2784 Newx (ary, 2, SV *); 3086SV *
2785 AvALLOC (av) = ary; 3087_alloc (int count)
2786 AvARRAY (av) = ary; 3088 CODE:
2787 AvMAX (av) = 1; 3089 RETVAL = coro_waitarray_new (aTHX_ count);
2788 AvFILLp (av) = 0;
2789 ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1);
2790
2791 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2792}
2793 OUTPUT: 3090 OUTPUT:
2794 RETVAL 3091 RETVAL
2795 3092
2796SV * 3093SV *
2797count (SV *self) 3094count (SV *self)
2806 adjust = 1 3103 adjust = 1
2807 CODE: 3104 CODE:
2808 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3105 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2809 3106
2810void 3107void
2811down (SV *self) 3108down (...)
2812 CODE: 3109 CODE:
2813 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3110 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3111
3112void
3113wait (...)
3114 CODE:
3115 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2814 3116
2815void 3117void
2816try (SV *self) 3118try (SV *self)
2817 PPCODE: 3119 PPCODE:
2818{ 3120{
2830 XSRETURN_NO; 3132 XSRETURN_NO;
2831} 3133}
2832 3134
2833void 3135void
2834waiters (SV *self) 3136waiters (SV *self)
2835 CODE: 3137 PPCODE:
2836{ 3138{
2837 AV *av = (AV *)SvRV (self); 3139 AV *av = (AV *)SvRV (self);
3140 int wcount = AvFILLp (av) + 1 - 1;
2838 3141
2839 if (GIMME_V == G_SCALAR) 3142 if (GIMME_V == G_SCALAR)
2840 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3143 XPUSHs (sv_2mortal (newSViv (wcount)));
2841 else 3144 else
2842 { 3145 {
2843 int i; 3146 int i;
2844 EXTEND (SP, AvFILLp (av) + 1 - 1); 3147 EXTEND (SP, wcount);
2845 for (i = 1; i <= AvFILLp (av); ++i) 3148 for (i = 1; i <= wcount; ++i)
2846 PUSHs (newSVsv (AvARRAY (av)[i])); 3149 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2847 } 3150 }
2848} 3151}
3152
3153MODULE = Coro::State PACKAGE = Coro::Signal
3154
3155SV *
3156new (SV *klass)
3157 CODE:
3158 RETVAL = sv_bless (
3159 coro_waitarray_new (aTHX_ 0),
3160 GvSTASH (CvGV (cv))
3161 );
3162 OUTPUT:
3163 RETVAL
3164
3165void
3166wait (...)
3167 CODE:
3168 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3169
3170void
3171broadcast (SV *self)
3172 CODE:
3173{
3174 AV *av = (AV *)SvRV (self);
3175 coro_signal_wake (aTHX_ av, AvFILLp (av));
3176}
3177
3178void
3179send (SV *self)
3180 CODE:
3181{
3182 AV *av = (AV *)SvRV (self);
3183
3184 if (AvFILLp (av))
3185 coro_signal_wake (aTHX_ av, 1);
3186 else
3187 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3188}
3189
3190IV
3191awaited (SV *self)
3192 CODE:
3193 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3194 OUTPUT:
3195 RETVAL
2849 3196
2850 3197
2851MODULE = Coro::State PACKAGE = Coro::AnyEvent 3198MODULE = Coro::State PACKAGE = Coro::AnyEvent
2852 3199
2853BOOT: 3200BOOT:
2868 sv_setsv (sv_activity, &PL_sv_undef); 3215 sv_setsv (sv_activity, &PL_sv_undef);
2869 if (coro_nready >= incede) 3216 if (coro_nready >= incede)
2870 { 3217 {
2871 PUSHMARK (SP); 3218 PUSHMARK (SP);
2872 PUTBACK; 3219 PUTBACK;
2873 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); 3220 call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2874 SPAGAIN;
2875 } 3221 }
2876 3222
2877 --incede; 3223 --incede;
2878} 3224}
2879 3225
2882 3228
2883void 3229void
2884_register (char *target, char *proto, SV *req) 3230_register (char *target, char *proto, SV *req)
2885 CODE: 3231 CODE:
2886{ 3232{
2887 HV *st;
2888 GV *gvp;
2889 CV *req_cv = sv_2cv (req, &st, &gvp, 0); 3233 CV *req_cv = coro_sv_2cv (req);
2890 /* newXSproto doesn't return the CV on 5.8 */ 3234 /* newXSproto doesn't return the CV on 5.8 */
2891 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3235 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
2892 sv_setpv ((SV *)slf_cv, proto); 3236 sv_setpv ((SV *)slf_cv, proto);
2893 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3237 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2894} 3238}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines