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.263 by root, Wed Nov 12 04:49:06 2008 UTC vs.
Revision 1.275 by root, Sat Nov 15 06:26:52 2008 UTC

116# define CORO_PREFER_PERL_FUNCTIONS 0 116# define CORO_PREFER_PERL_FUNCTIONS 0
117#endif 117#endif
118 118
119/* The next macros try to return the current stack pointer, in an as 119/* The next macros try to return the current stack pointer, in an as
120 * portable way as possible. */ 120 * portable way as possible. */
121#define dSTACKLEVEL volatile char stacklevel 121#if __GNUC__ >= 4
122#define STACKLEVEL ((void *)&stacklevel) 122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0)
123#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel
125#endif
123 126
124#define IN_DESTRUCT (PL_main_cv == Nullcv) 127#define IN_DESTRUCT (PL_main_cv == Nullcv)
125 128
126#if __GNUC__ >= 3 129#if __GNUC__ >= 3
127# define attribute(x) __attribute__(x) 130# define attribute(x) __attribute__(x)
128# define BARRIER __asm__ __volatile__ ("" : : : "memory")
129# define expect(expr,value) __builtin_expect ((expr),(value)) 131# define expect(expr,value) __builtin_expect ((expr),(value))
130# define INLINE static inline 132# define INLINE static inline
131#else 133#else
132# define attribute(x) 134# define attribute(x)
133# define BARRIER
134# define expect(expr,value) (expr) 135# define expect(expr,value) (expr)
135# define INLINE static 136# define INLINE static
136#endif 137#endif
137 138
138#define expect_false(expr) expect ((expr) != 0, 0) 139#define expect_false(expr) expect ((expr) != 0, 0)
182static JMPENV *main_top_env; 183static JMPENV *main_top_env;
183static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
184static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
185static volatile struct coro *transfer_next; 186static volatile struct coro *transfer_next;
186 187
187struct transfer_args
188{
189 struct coro *prev, *next;
190};
191
192static GV *irsgv; /* $/ */ 188static GV *irsgv; /* $/ */
193static GV *stdoutgv; /* *STDOUT */ 189static GV *stdoutgv; /* *STDOUT */
194static SV *rv_diehook; 190static SV *rv_diehook;
195static SV *rv_warnhook; 191static SV *rv_warnhook;
196static HV *hv_sig; /* %SIG */ 192static HV *hv_sig; /* %SIG */
214 CC_TRACE_LINE = 0x10, /* trace each statement */ 210 CC_TRACE_LINE = 0x10, /* trace each statement */
215 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, 211 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
216}; 212};
217 213
218/* this is a structure representing a c-level coroutine */ 214/* this is a structure representing a c-level coroutine */
219typedef struct coro_cctx { 215typedef struct coro_cctx
216{
220 struct coro_cctx *next; 217 struct coro_cctx *next;
221 218
222 /* the stack */ 219 /* the stack */
223 void *sptr; 220 void *sptr;
224 size_t ssize; 221 size_t ssize;
242 CF_NEW = 0x0004, /* has never been switched to */ 239 CF_NEW = 0x0004, /* has never been switched to */
243 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 240 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
244}; 241};
245 242
246/* the structure where most of the perl state is stored, overlaid on the cxstack */ 243/* the structure where most of the perl state is stored, overlaid on the cxstack */
247typedef struct { 244typedef struct
245{
248 SV *defsv; 246 SV *defsv;
249 AV *defav; 247 AV *defav;
250 SV *errsv; 248 SV *errsv;
251 SV *irsgv; 249 SV *irsgv;
252#define VAR(name,type) type name; 250#define VAR(name,type) type name;
256 254
257#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 255#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
258 256
259/* this is a structure representing a perl-level coroutine */ 257/* this is a structure representing a perl-level coroutine */
260struct coro { 258struct coro {
261 /* the c coroutine allocated to this perl coroutine, if any */ 259 /* the C coroutine allocated to this perl coroutine, if any */
262 coro_cctx *cctx; 260 coro_cctx *cctx;
263 261
264 /* process data */ 262 /* process data */
263 struct CoroSLF slf_frame; /* saved slf frame */
265 AV *mainstack; 264 AV *mainstack;
266 perl_slots *slot; /* basically the saved sp */ 265 perl_slots *slot; /* basically the saved sp */
267 266
268 AV *args; /* data associated with this coroutine (initial args) */ 267 AV *args; /* data associated with this coroutine (initial args) */
269 int refcnt; /* coroutines are refcounted, yes */ 268 int refcnt; /* coroutines are refcounted, yes */
284 struct coro *next, *prev; 283 struct coro *next, *prev;
285}; 284};
286 285
287typedef struct coro *Coro__State; 286typedef struct coro *Coro__State;
288typedef struct coro *Coro__State_or_hashref; 287typedef struct coro *Coro__State_or_hashref;
288
289static struct CoroSLF slf_frame; /* the current slf frame */
289 290
290/** Coro ********************************************************************/ 291/** Coro ********************************************************************/
291 292
292#define PRIO_MAX 3 293#define PRIO_MAX 3
293#define PRIO_HIGH 1 294#define PRIO_HIGH 1
298 299
299/* for Coro.pm */ 300/* for Coro.pm */
300static SV *coro_current; 301static SV *coro_current;
301static SV *coro_readyhook; 302static SV *coro_readyhook;
302static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
303static int coro_nready;
304static struct coro *coro_first; 304static struct coro *coro_first;
305#define coro_nready coroapi.nready
305 306
306/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
307 308
308static SV * 309static SV *
309coro_get_sv (pTHX_ const char *name, int create) 310coro_get_sv (pTHX_ const char *name, int create)
513 CvPADLIST (cv) = (AV *)POPs; 514 CvPADLIST (cv) = (AV *)POPs;
514 } 515 }
515 516
516 PUTBACK; 517 PUTBACK;
517 } 518 }
519
520 slf_frame = c->slf_frame;
518} 521}
519 522
520static void 523static void
521save_perl (pTHX_ Coro__State c) 524save_perl (pTHX_ Coro__State c)
522{ 525{
526 c->slf_frame = slf_frame;
527
523 { 528 {
524 dSP; 529 dSP;
525 I32 cxix = cxstack_ix; 530 I32 cxix = cxstack_ix;
526 PERL_CONTEXT *ccstk = cxstack; 531 PERL_CONTEXT *ccstk = cxstack;
527 PERL_SI *top_si = PL_curstackinfo; 532 PERL_SI *top_si = PL_curstackinfo;
594 #undef VAR 599 #undef VAR
595 } 600 }
596} 601}
597 602
598/* 603/*
599 * allocate various perl stacks. This is an exact copy 604 * allocate various perl stacks. This is almost an exact copy
600 * of perl.c:init_stacks, except that it uses less memory 605 * of perl.c:init_stacks, except that it uses less memory
601 * on the (sometimes correct) assumption that coroutines do 606 * on the (sometimes correct) assumption that coroutines do
602 * not usually need a lot of stackspace. 607 * not usually need a lot of stackspace.
603 */ 608 */
604#if CORO_PREFER_PERL_FUNCTIONS 609#if CORO_PREFER_PERL_FUNCTIONS
725#ifndef MgPV_nolen_const 730#ifndef MgPV_nolen_const
726#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 731#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
727 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 732 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
728 (const char*)(mg)->mg_ptr) 733 (const char*)(mg)->mg_ptr)
729#endif 734#endif
730
731/* we sometimes need to create the effect of entersub calling us */
732#define ENTERSUB_HEAD ENTER; SAVETMPS
733/* we somtimes need to create the effect of leaving via entersub */
734#define ENTERSUB_TAIL LEAVE
735 735
736/* 736/*
737 * This overrides the default magic get method of %SIG elements. 737 * This overrides the default magic get method of %SIG elements.
738 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 738 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
739 * and instead of tryign to save and restore the hash elements, we just provide 739 * and instead of tryign to save and restore the hash elements, we just provide
811 811
812 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 812 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
813} 813}
814 814
815static void 815static void
816prepare_nop (aTHX_ struct coro_transfer_args *ta)
817{
818 /* kind of mega-hacky, but works */
819 ta->next = ta->prev = (struct coro *)ta;
820}
821
822static int
823slf_check_nop (aTHX)
824{
825 return 0;
826}
827
828static void
816coro_setup (pTHX_ struct coro *coro) 829coro_setup (pTHX_ struct coro *coro)
817{ 830{
818 /* 831 /*
819 * emulate part of the perl startup here. 832 * emulate part of the perl startup here.
820 */ 833 */
859 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 872 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
860 SPAGAIN; 873 SPAGAIN;
861 } 874 }
862 875
863 /* this newly created coroutine might be run on an existing cctx which most 876 /* this newly created coroutine might be run on an existing cctx which most
864 * likely was suspended in set_stacklevel, called from entersub. 877 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
865 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
866 * so we ENTER here for symmetry.
867 */ 878 */
868 ENTERSUB_HEAD; 879 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
880 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
869} 881}
870 882
871static void 883static void
872coro_destruct (pTHX_ struct coro *coro) 884coro_destruct (pTHX_ struct coro *coro)
873{ 885{
1037 TAINT_NOT; 1049 TAINT_NOT;
1038 return 0; 1050 return 0;
1039} 1051}
1040 1052
1041static void 1053static void
1042prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) 1054prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx)
1043{ 1055{
1044 ta->prev = (struct coro *)cctx; 1056 ta->prev = (struct coro *)cctx;
1045 ta->next = 0; 1057 ta->next = 0;
1046} 1058}
1047 1059
1078INLINE void 1090INLINE void
1079transfer_tail (pTHX) 1091transfer_tail (pTHX)
1080{ 1092{
1081 struct coro *next = (struct coro *)transfer_next; 1093 struct coro *next = (struct coro *)transfer_next;
1082 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ 1094 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1083 assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next)); 1095 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1084 1096
1085 free_coro_mortal (aTHX); 1097 free_coro_mortal (aTHX);
1086 UNLOCK; 1098 UNLOCK;
1087 1099
1088 if (expect_false (next->throw)) 1100 if (expect_false (next->throw))
1107# endif 1119# endif
1108#endif 1120#endif
1109 { 1121 {
1110 dTHX; 1122 dTHX;
1111 1123
1112 /* entersub called ENTER, but we never 'returned', undo that here */ 1124 /* normally we would need to skip the entersub here */
1113 ENTERSUB_TAIL; 1125 /* not doing so will re-execute it, which is exactly what we want */
1114
1115 /* we now skip the entersub that did lead to transfer() */
1116 PL_op = PL_op->op_next; 1126 /* PL_nop = PL_nop->op_next */
1117 1127
1118 /* inject a fake subroutine call to cctx_init */ 1128 /* inject a fake subroutine call to cctx_init */
1119 cctx_prepare (aTHX_ (coro_cctx *)arg); 1129 cctx_prepare (aTHX_ (coro_cctx *)arg);
1120 1130
1121 /* cctx_run is the alternative tail of transfer() */ 1131 /* cctx_run is the alternative tail of transfer() */
1132 /* TODO: throwing an exception here might be deadly, VERIFY */
1122 transfer_tail (aTHX); 1133 transfer_tail (aTHX);
1123 1134
1124 /* somebody or something will hit me for both perl_run and PL_restartop */ 1135 /* somebody or something will hit me for both perl_run and PL_restartop */
1125 PL_restartop = PL_op; 1136 PL_restartop = PL_op;
1126 perl_run (PL_curinterp); 1137 perl_run (PL_curinterp);
1192 cctx->ssize = cctx_stacksize * (long)sizeof (long); 1203 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1193 New (0, cctx->sptr, cctx_stacksize, long); 1204 New (0, cctx->sptr, cctx_stacksize, long);
1194 1205
1195 if (!cctx->sptr) 1206 if (!cctx->sptr)
1196 { 1207 {
1197 perror ("FATAL: unable to allocate stack for coroutine"); 1208 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1198 _exit (EXIT_FAILURE); 1209 _exit (EXIT_FAILURE);
1199 } 1210 }
1200 1211
1201 stack_start = cctx->sptr; 1212 stack_start = cctx->sptr;
1202 stack_size = cctx->ssize; 1213 stack_size = cctx->ssize;
1285transfer_check (pTHX_ struct coro *prev, struct coro *next) 1296transfer_check (pTHX_ struct coro *prev, struct coro *next)
1286{ 1297{
1287 if (expect_true (prev != next)) 1298 if (expect_true (prev != next))
1288 { 1299 {
1289 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1300 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1290 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); 1301 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,");
1291 1302
1292 if (expect_false (next->flags & CF_RUNNING)) 1303 if (expect_false (next->flags & CF_RUNNING))
1293 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1304 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1294 1305
1295 if (expect_false (next->flags & CF_DESTROYED)) 1306 if (expect_false (next->flags & CF_DESTROYED))
1296 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1307 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,");
1297 1308
1298#if !PERL_VERSION_ATLEAST (5,10,0) 1309#if !PERL_VERSION_ATLEAST (5,10,0)
1299 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1310 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1300 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); 1311 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1301#endif 1312#endif
1302 } 1313 }
1303} 1314}
1304 1315
1305/* always use the TRANSFER macro */ 1316/* always use the TRANSFER macro */
1309 dSTACKLEVEL; 1320 dSTACKLEVEL;
1310 1321
1311 /* sometimes transfer is only called to set idle_sp */ 1322 /* sometimes transfer is only called to set idle_sp */
1312 if (expect_false (!next)) 1323 if (expect_false (!next))
1313 { 1324 {
1314 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1325 ((coro_cctx *)prev)->idle_sp = stacklevel;
1315 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1326 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1316 } 1327 }
1317 else if (expect_true (prev != next)) 1328 else if (expect_true (prev != next))
1318 { 1329 {
1319 coro_cctx *prev__cctx; 1330 coro_cctx *prev__cctx;
1346 1357
1347 prev__cctx = prev->cctx; 1358 prev__cctx = prev->cctx;
1348 1359
1349 /* possibly untie and reuse the cctx */ 1360 /* possibly untie and reuse the cctx */
1350 if (expect_true ( 1361 if (expect_true (
1351 prev__cctx->idle_sp == STACKLEVEL 1362 prev__cctx->idle_sp == stacklevel
1352 && !(prev__cctx->flags & CC_TRACE) 1363 && !(prev__cctx->flags & CC_TRACE)
1353 && !force_cctx 1364 && !force_cctx
1354 )) 1365 ))
1355 { 1366 {
1356 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1367 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1357 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1368 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1358 1369
1359 prev->cctx = 0; 1370 prev->cctx = 0;
1360 1371
1361 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1372 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1412 1423
1413 if (coro->mainstack && coro->mainstack != main_mainstack) 1424 if (coro->mainstack && coro->mainstack != main_mainstack)
1414 { 1425 {
1415 struct coro temp; 1426 struct coro temp;
1416 1427
1417 if (coro->flags & CF_RUNNING) 1428 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1418 croak ("FATAL: tried to destroy currently running coroutine");
1419 1429
1420 save_perl (aTHX_ &temp); 1430 save_perl (aTHX_ &temp);
1421 load_perl (aTHX_ coro); 1431 load_perl (aTHX_ coro);
1422 1432
1423 coro_destruct (aTHX_ coro); 1433 coro_destruct (aTHX_ coro);
1474# define MGf_DUP 0 1484# define MGf_DUP 0
1475#endif 1485#endif
1476}; 1486};
1477 1487
1478static void 1488static void
1479prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1489prepare_transfer (pTHX_ struct coro_transfer_args *ta, SV *prev_sv, SV *next_sv)
1480{ 1490{
1481 ta->prev = SvSTATE (prev_sv); 1491 ta->prev = SvSTATE (prev_sv);
1482 ta->next = SvSTATE (next_sv); 1492 ta->next = SvSTATE (next_sv);
1483 TRANSFER_CHECK (*ta); 1493 TRANSFER_CHECK (*ta);
1484} 1494}
1485 1495
1486static void 1496static void
1487api_transfer (SV *prev_sv, SV *next_sv) 1497api_transfer (pTHX_ SV *prev_sv, SV *next_sv)
1488{ 1498{
1489 dTHX;
1490 struct transfer_args ta; 1499 struct coro_transfer_args ta;
1491 1500
1492 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1501 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1493 TRANSFER (ta, 1); 1502 TRANSFER (ta, 1);
1494} 1503}
1495 1504
1512 1521
1513 return 0; 1522 return 0;
1514} 1523}
1515 1524
1516static int 1525static int
1517api_ready (SV *coro_sv) 1526api_ready (pTHX_ SV *coro_sv)
1518{ 1527{
1519 dTHX;
1520 struct coro *coro; 1528 struct coro *coro;
1521 SV *sv_hook; 1529 SV *sv_hook;
1522 void (*xs_hook)(void); 1530 void (*xs_hook)(void);
1523 1531
1524 if (SvROK (coro_sv)) 1532 if (SvROK (coro_sv))
1562 1570
1563 return 1; 1571 return 1;
1564} 1572}
1565 1573
1566static int 1574static int
1567api_is_ready (SV *coro_sv) 1575api_is_ready (pTHX_ SV *coro_sv)
1568{ 1576{
1569 dTHX;
1570
1571 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1577 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1572} 1578}
1573 1579
1574INLINE void 1580INLINE void
1575prepare_schedule (pTHX_ struct transfer_args *ta) 1581prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1576{ 1582{
1577 SV *prev_sv, *next_sv; 1583 SV *prev_sv, *next_sv;
1578 1584
1579 for (;;) 1585 for (;;)
1580 { 1586 {
1629 coro_mortal = prev_sv; 1635 coro_mortal = prev_sv;
1630 UNLOCK; 1636 UNLOCK;
1631} 1637}
1632 1638
1633INLINE void 1639INLINE void
1634prepare_cede (pTHX_ struct transfer_args *ta) 1640prepare_cede (pTHX_ struct coro_transfer_args *ta)
1635{ 1641{
1636 api_ready (coro_current); 1642 api_ready (aTHX_ coro_current);
1637 prepare_schedule (aTHX_ ta); 1643 prepare_schedule (aTHX_ ta);
1638} 1644}
1639 1645
1646INLINE void
1647prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1648{
1649 SV *prev = SvRV (coro_current);
1650
1651 if (coro_nready)
1652 {
1653 prepare_schedule (aTHX_ ta);
1654 api_ready (aTHX_ prev);
1655 }
1656 else
1657 prepare_nop (aTHX_ ta);
1658}
1659
1660static void
1661api_schedule (pTHX)
1662{
1663 struct coro_transfer_args ta;
1664
1665 prepare_schedule (aTHX_ &ta);
1666 TRANSFER (ta, 1);
1667}
1668
1640static int 1669static int
1641prepare_cede_notself (pTHX_ struct transfer_args *ta) 1670api_cede (pTHX)
1642{ 1671{
1643 if (coro_nready) 1672 struct coro_transfer_args ta;
1644 { 1673
1645 SV *prev = SvRV (coro_current);
1646 prepare_schedule (aTHX_ ta); 1674 prepare_cede (aTHX_ &ta);
1647 api_ready (prev); 1675
1676 if (expect_true (ta.prev != ta.next))
1677 {
1678 TRANSFER (ta, 1);
1648 return 1; 1679 return 1;
1649 } 1680 }
1650 else 1681 else
1651 return 0; 1682 return 0;
1652} 1683}
1653 1684
1654static void
1655api_schedule (void)
1656{
1657 dTHX;
1658 struct transfer_args ta;
1659
1660 prepare_schedule (aTHX_ &ta);
1661 TRANSFER (ta, 1);
1662}
1663
1664static int 1685static int
1665api_cede (void) 1686api_cede_notself (pTHX)
1666{ 1687{
1667 dTHX; 1688 if (coro_nready)
1689 {
1668 struct transfer_args ta; 1690 struct coro_transfer_args ta;
1669 1691
1670 prepare_cede (aTHX_ &ta); 1692 prepare_cede_notself (aTHX_ &ta);
1671
1672 if (expect_true (ta.prev != ta.next))
1673 {
1674 TRANSFER (ta, 1); 1693 TRANSFER (ta, 1);
1675 return 1; 1694 return 1;
1676 } 1695 }
1677 else 1696 else
1678 return 0; 1697 return 0;
1679} 1698}
1680 1699
1681static int 1700static void
1682api_cede_notself (void)
1683{
1684 dTHX;
1685 struct transfer_args ta;
1686
1687 if (prepare_cede_notself (aTHX_ &ta))
1688 {
1689 TRANSFER (ta, 1);
1690 return 1;
1691 }
1692 else
1693 return 0;
1694}
1695
1696static void
1697api_trace (SV *coro_sv, int flags) 1701api_trace (pTHX_ SV *coro_sv, int flags)
1698{ 1702{
1699 dTHX;
1700 struct coro *coro = SvSTATE (coro_sv); 1703 struct coro *coro = SvSTATE (coro_sv);
1701 1704
1702 if (flags & CC_TRACE) 1705 if (flags & CC_TRACE)
1703 { 1706 {
1704 if (!coro->cctx) 1707 if (!coro->cctx)
1705 coro->cctx = cctx_new_run (); 1708 coro->cctx = cctx_new_run ();
1706 else if (!(coro->cctx->flags & CC_TRACE)) 1709 else if (!(coro->cctx->flags & CC_TRACE))
1707 croak ("cannot enable tracing on coroutine with custom stack"); 1710 croak ("cannot enable tracing on coroutine with custom stack,");
1708 1711
1709 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1712 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1710 } 1713 }
1711 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1714 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1712 { 1715 {
1771 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1774 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1772 double now = nvtime (); 1775 double now = nvtime ();
1773 1776
1774 if (now >= self->next) 1777 if (now >= self->next)
1775 { 1778 {
1776 api_cede (); 1779 api_cede (aTHX);
1777 self->next = now + self->every; 1780 self->next = now + self->every;
1778 } 1781 }
1779 1782
1780 return PerlIOBuf_flush (aTHX_ f); 1783 return PerlIOBuf_flush (aTHX_ f);
1781} 1784}
1810 PerlIOBuf_get_ptr, 1813 PerlIOBuf_get_ptr,
1811 PerlIOBuf_get_cnt, 1814 PerlIOBuf_get_cnt,
1812 PerlIOBuf_set_ptrcnt, 1815 PerlIOBuf_set_ptrcnt,
1813}; 1816};
1814 1817
1818/*****************************************************************************/
1819
1820static const CV *slf_cv; /* for quick consistency check */
1821
1822static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1823static SV *slf_arg0;
1824static SV *slf_arg1;
1825static SV *slf_arg2;
1826
1827/* this restores the stack in the case we patched the entersub, to */
1828/* recreate the stack frame as perl will on following calls */
1829/* since entersub cleared the stack */
1830static OP *
1831pp_restore (pTHX)
1832{
1833 dSP;
1834
1835 PUSHMARK (SP);
1836
1837 EXTEND (SP, 3);
1838 if (slf_arg0) PUSHs (sv_2mortal (slf_arg0));
1839 if (slf_arg1) PUSHs (sv_2mortal (slf_arg1));
1840 if (slf_arg2) PUSHs (sv_2mortal (slf_arg2));
1841 PUSHs ((SV *)CvGV (slf_cv));
1842
1843 RETURNOP (slf_restore.op_first);
1844}
1845
1846static void
1847slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1848{
1849 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1850}
1851
1852static void
1853slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items)
1854{
1855 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1856
1857 frame->prepare = slf_prepare_set_stacklevel;
1858 frame->check = slf_check_nop;
1859 frame->data = (void *)SvIV (arg [0]);
1860}
1861
1862static void
1863slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1864{
1865 SV **arg = (SV **)slf_frame.data;
1866
1867 prepare_transfer (ta, arg [0], arg [1]);
1868}
1869
1870static void
1871slf_init_transfer (pTHX_ struct CoroSLF *frame, SV **arg, int items)
1872{
1873 if (items != 2)
1874 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items);
1875
1876 frame->prepare = slf_prepare_transfer;
1877 frame->check = slf_check_nop;
1878 frame->data = (void *)arg; /* let's hope it will stay valid */
1879}
1880
1881static void
1882slf_init_schedule (pTHX_ struct CoroSLF *frame, SV **arg, int items)
1883{
1884 frame->prepare = prepare_schedule;
1885 frame->check = slf_check_nop;
1886}
1887
1888static void
1889slf_init_cede (pTHX_ struct CoroSLF *frame, SV **arg, int items)
1890{
1891 frame->prepare = prepare_cede;
1892 frame->check = slf_check_nop;
1893}
1894
1895static void
1896slf_init_cede_notself (pTHX_ struct CoroSLF *frame, SV **arg, int items)
1897{
1898 frame->prepare = prepare_cede_notself;
1899 frame->check = slf_check_nop;
1900}
1901
1902/* we hijack an hopefully unused CV flag for our purposes */
1903#define CVf_SLF 0x4000
1904
1905/*
1906 * these not obviously related functions are all rolled into one
1907 * function to increase chances that they all will call transfer with the same
1908 * stack offset
1909 * SLF stands for "schedule-like-function".
1910 */
1911static OP *
1912pp_slf (pTHX)
1913{
1914 I32 checkmark; /* mark SP to see how many elements check has pushed */
1915
1916 /* set up the slf frame, unless it has already been set-up */
1917 /* the latter happens when a new coro has been started */
1918 /* or when a new cctx was attached to an existing coroutine */
1919 if (expect_true (!slf_frame.prepare))
1920 {
1921 /* first iteration */
1922 dSP;
1923 SV **arg = PL_stack_base + TOPMARK + 1;
1924 int items = SP - arg; /* args without function object */
1925 SV *gv = *sp;
1926
1927 /* do a quick consistency check on the "function" object, and if it isn't */
1928 /* for us, divert to the real entersub */
1929 if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF))
1930 return PL_ppaddr[OP_ENTERSUB](aTHX);
1931
1932 /* pop args */
1933 SP = PL_stack_base + POPMARK;
1934
1935 if (!(PL_op->op_flags & OPf_STACKED))
1936 {
1937 /* ampersand-form of call, use @_ instead of stack */
1938 AV *av = GvAV (PL_defgv);
1939 arg = AvARRAY (av);
1940 items = AvFILLp (av) + 1;
1941 }
1942
1943 PUTBACK;
1944
1945 ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items);
1946 }
1947
1948 /* now interpret the slf_frame */
1949 /* we use a callback system not to make the code needlessly */
1950 /* complicated, but so we can run multiple perl coros from one cctx */
1951
1952 do
1953 {
1954 struct coro_transfer_args ta;
1955
1956 slf_frame.prepare (aTHX_ &ta);
1957 TRANSFER (ta, 0);
1958
1959 checkmark = PL_stack_sp - PL_stack_base;
1960 }
1961 while (slf_frame.check (aTHX_ &slf_frame));
1962
1963 {
1964 dSP;
1965 SV **bot = PL_stack_base + checkmark;
1966 int gimme = GIMME_V;
1967
1968 slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */
1969
1970 /* make sure we put something on the stack in scalar context */
1971 if (gimme == G_SCALAR)
1972 {
1973 if (sp == bot)
1974 XPUSHs (&PL_sv_undef);
1975
1976 SP = bot + 1;
1977 }
1978
1979 PUTBACK;
1980 }
1981
1982 return NORMAL;
1983}
1984
1985static void
1986api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items)
1987{
1988 assert (("FATAL: SLF call with illegal CV value", !CvANON (cv)));
1989
1990 if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB]
1991 && PL_op->op_ppaddr != pp_slf)
1992 croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught");
1993
1994 if (items > 3)
1995 croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items);
1996
1997 CvFLAGS (cv) |= CVf_SLF;
1998 CvXSUBANY (cv).any_ptr = (void *)init_cb;
1999 slf_cv = cv;
2000
2001 /* we patch the op, and then re-run the whole call */
2002 /* we have to put the same argument on the stack for this to work */
2003 /* and this will be done by pp_restore */
2004 slf_restore.op_next = (OP *)&slf_restore;
2005 slf_restore.op_type = OP_NULL;
2006 slf_restore.op_ppaddr = pp_restore;
2007 slf_restore.op_first = PL_op;
2008
2009 slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0;
2010 slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0;
2011 slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0;
2012
2013 PL_op->op_ppaddr = pp_slf;
2014
2015 PL_op = (OP *)&slf_restore;
2016}
2017
2018/*****************************************************************************/
2019
2020static int
2021slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2022{
2023 AV *av = (AV *)frame->data;
2024 SV *count_sv = AvARRAY (av)[0];
2025
2026 if (SvIVX (count_sv) > 0)
2027 {
2028 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2029 return 0;
2030 }
2031 else
2032 {
2033 int i;
2034 /* if we were woken up but can't down, we look through the whole */
2035 /* waiters list and only add us if we aren't in there already */
2036 /* this avoids some degenerate memory usage cases */
2037
2038 for (i = 1; i <= AvFILLp (av); ++i)
2039 if (AvARRAY (av)[i] == SvRV (coro_current))
2040 return 1;
2041
2042 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2043 return 1;
2044 }
2045}
2046
2047static void
2048slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, SV **arg, int items)
2049{
2050 AV *av = (AV *)SvRV (arg [0]);
2051
2052 if (SvIVX (AvARRAY (av)[0]) > 0)
2053 {
2054 frame->data = (void *)av;
2055 frame->prepare = prepare_nop;
2056 }
2057 else
2058 {
2059 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2060
2061 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2062 frame->prepare = prepare_schedule;
2063 }
2064
2065 frame->check = slf_check_semaphore_down;
2066
2067}
1815 2068
1816MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2069MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1817 2070
1818PROTOTYPES: DISABLE 2071PROTOTYPES: DISABLE
1819 2072
1849 main_top_env = PL_top_env; 2102 main_top_env = PL_top_env;
1850 2103
1851 while (main_top_env->je_prev) 2104 while (main_top_env->je_prev)
1852 main_top_env = main_top_env->je_prev; 2105 main_top_env = main_top_env->je_prev;
1853 2106
1854 coroapi.ver = CORO_API_VERSION; 2107 coroapi.ver = CORO_API_VERSION;
1855 coroapi.rev = CORO_API_REVISION; 2108 coroapi.rev = CORO_API_REVISION;
2109
1856 coroapi.transfer = api_transfer; 2110 coroapi.transfer = api_transfer;
2111
2112 coroapi.sv_state = SvSTATE_;
2113 coroapi.execute_slf = api_execute_slf;
2114 coroapi.prepare_nop = prepare_nop;
2115 coroapi.prepare_schedule = prepare_schedule;
2116 coroapi.prepare_cede = prepare_cede;
2117 coroapi.prepare_cede_notself = prepare_cede_notself;
1857 2118
1858 { 2119 {
1859 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 2120 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1860 2121
1861 if (!svp) croak ("Time::HiRes is required"); 2122 if (!svp) croak ("Time::HiRes is required");
1894 av_push (coro->args, newSVsv (ST (i))); 2155 av_push (coro->args, newSVsv (ST (i)));
1895} 2156}
1896 OUTPUT: 2157 OUTPUT:
1897 RETVAL 2158 RETVAL
1898 2159
1899# these not obviously related functions are all rolled into the same xs
1900# function to increase chances that they all will call transfer with the same
1901# stack offset
1902void 2160void
1903_set_stacklevel (...) 2161_set_stacklevel (...)
1904 ALIAS: 2162 CODE:
1905 Coro::State::transfer = 1 2163 api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items);
1906 Coro::schedule = 2
1907 Coro::cede = 3
1908 Coro::cede_notself = 4
1909 CODE:
1910{
1911 struct transfer_args ta;
1912 2164
1913 PUTBACK; 2165void
1914 switch (ix) 2166transfer (...)
1915 { 2167 CODE:
1916 case 0: 2168 api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items);
1917 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1918 break;
1919
1920 case 1:
1921 if (items != 2)
1922 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1923
1924 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1925 break;
1926
1927 case 2:
1928 prepare_schedule (aTHX_ &ta);
1929 break;
1930
1931 case 3:
1932 prepare_cede (aTHX_ &ta);
1933 break;
1934
1935 case 4:
1936 if (!prepare_cede_notself (aTHX_ &ta))
1937 XSRETURN_EMPTY;
1938
1939 break;
1940 }
1941 SPAGAIN;
1942
1943 BARRIER;
1944 PUTBACK;
1945 TRANSFER (ta, 0);
1946 SPAGAIN; /* might be the sp of a different coroutine now */
1947 /* be extra careful not to ever do anything after TRANSFER */
1948}
1949 2169
1950bool 2170bool
1951_destroy (SV *coro_sv) 2171_destroy (SV *coro_sv)
1952 CODE: 2172 CODE:
1953 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2173 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1960 CODE: 2180 CODE:
1961 _exit (code); 2181 _exit (code);
1962 2182
1963int 2183int
1964cctx_stacksize (int new_stacksize = 0) 2184cctx_stacksize (int new_stacksize = 0)
2185 PROTOTYPE: ;$
1965 CODE: 2186 CODE:
1966 RETVAL = cctx_stacksize; 2187 RETVAL = cctx_stacksize;
1967 if (new_stacksize) 2188 if (new_stacksize)
1968 { 2189 {
1969 cctx_stacksize = new_stacksize; 2190 cctx_stacksize = new_stacksize;
1972 OUTPUT: 2193 OUTPUT:
1973 RETVAL 2194 RETVAL
1974 2195
1975int 2196int
1976cctx_max_idle (int max_idle = 0) 2197cctx_max_idle (int max_idle = 0)
2198 PROTOTYPE: ;$
1977 CODE: 2199 CODE:
1978 RETVAL = cctx_max_idle; 2200 RETVAL = cctx_max_idle;
1979 if (max_idle > 1) 2201 if (max_idle > 1)
1980 cctx_max_idle = max_idle; 2202 cctx_max_idle = max_idle;
1981 OUTPUT: 2203 OUTPUT:
1982 RETVAL 2204 RETVAL
1983 2205
1984int 2206int
1985cctx_count () 2207cctx_count ()
2208 PROTOTYPE:
1986 CODE: 2209 CODE:
1987 RETVAL = cctx_count; 2210 RETVAL = cctx_count;
1988 OUTPUT: 2211 OUTPUT:
1989 RETVAL 2212 RETVAL
1990 2213
1991int 2214int
1992cctx_idle () 2215cctx_idle ()
2216 PROTOTYPE:
1993 CODE: 2217 CODE:
1994 RETVAL = cctx_idle; 2218 RETVAL = cctx_idle;
1995 OUTPUT: 2219 OUTPUT:
1996 RETVAL 2220 RETVAL
1997 2221
1998void 2222void
1999list () 2223list ()
2224 PROTOTYPE:
2000 PPCODE: 2225 PPCODE:
2001{ 2226{
2002 struct coro *coro; 2227 struct coro *coro;
2003 for (coro = coro_first; coro; coro = coro->next) 2228 for (coro = coro_first; coro; coro = coro->next)
2004 if (coro->hv) 2229 if (coro->hv)
2071 SvREFCNT_dec (self->throw); 2296 SvREFCNT_dec (self->throw);
2072 self->throw = SvOK (throw) ? newSVsv (throw) : 0; 2297 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2073 2298
2074void 2299void
2075api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2300api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2301 PROTOTYPE: $;$
2302 C_ARGS: aTHX_ coro, flags
2076 2303
2077SV * 2304SV *
2078has_cctx (Coro::State coro) 2305has_cctx (Coro::State coro)
2079 PROTOTYPE: $ 2306 PROTOTYPE: $
2080 CODE: 2307 CODE:
2104 OUTPUT: 2331 OUTPUT:
2105 RETVAL 2332 RETVAL
2106 2333
2107void 2334void
2108force_cctx () 2335force_cctx ()
2336 PROTOTYPE:
2109 CODE: 2337 CODE:
2110 struct coro *coro = SvSTATE (coro_current); 2338 struct coro *coro = SvSTATE (coro_current);
2111 coro->cctx->idle_sp = 0; 2339 coro->cctx->idle_sp = 0;
2112 2340
2113void 2341void
2115 PROTOTYPE: $ 2343 PROTOTYPE: $
2116 ALIAS: 2344 ALIAS:
2117 swap_defav = 1 2345 swap_defav = 1
2118 CODE: 2346 CODE:
2119 if (!self->slot) 2347 if (!self->slot)
2120 croak ("cannot swap state with coroutine that has no saved state"); 2348 croak ("cannot swap state with coroutine that has no saved state,");
2121 else 2349 else
2122 { 2350 {
2123 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); 2351 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2124 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2352 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2125 2353
2157 coroapi.schedule = api_schedule; 2385 coroapi.schedule = api_schedule;
2158 coroapi.cede = api_cede; 2386 coroapi.cede = api_cede;
2159 coroapi.cede_notself = api_cede_notself; 2387 coroapi.cede_notself = api_cede_notself;
2160 coroapi.ready = api_ready; 2388 coroapi.ready = api_ready;
2161 coroapi.is_ready = api_is_ready; 2389 coroapi.is_ready = api_is_ready;
2162 coroapi.nready = &coro_nready; 2390 coroapi.nready = coro_nready;
2163 coroapi.current = coro_current; 2391 coroapi.current = coro_current;
2164 2392
2165 GCoroAPI = &coroapi; 2393 GCoroAPI = &coroapi;
2166 sv_setiv (sv, (IV)&coroapi); 2394 sv_setiv (sv, (IV)&coroapi);
2167 SvREADONLY_on (sv); 2395 SvREADONLY_on (sv);
2168 } 2396 }
2169} 2397}
2398
2399void
2400schedule (...)
2401 CODE:
2402 api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), items);
2403
2404void
2405cede (...)
2406 CODE:
2407 api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), items);
2408
2409void
2410cede_notself (...)
2411 CODE:
2412 api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), items);
2170 2413
2171void 2414void
2172_set_current (SV *current) 2415_set_current (SV *current)
2173 PROTOTYPE: $ 2416 PROTOTYPE: $
2174 CODE: 2417 CODE:
2184 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 2427 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2185 UNLOCK; 2428 UNLOCK;
2186 2429
2187int 2430int
2188prio (Coro::State coro, int newprio = 0) 2431prio (Coro::State coro, int newprio = 0)
2432 PROTOTYPE: $;$
2189 ALIAS: 2433 ALIAS:
2190 nice = 1 2434 nice = 1
2191 CODE: 2435 CODE:
2192{ 2436{
2193 RETVAL = coro->prio; 2437 RETVAL = coro->prio;
2208 2452
2209SV * 2453SV *
2210ready (SV *self) 2454ready (SV *self)
2211 PROTOTYPE: $ 2455 PROTOTYPE: $
2212 CODE: 2456 CODE:
2213 RETVAL = boolSV (api_ready (self)); 2457 RETVAL = boolSV (api_ready (aTHX_ self));
2214 OUTPUT: 2458 OUTPUT:
2215 RETVAL 2459 RETVAL
2216 2460
2217int 2461int
2218nready (...) 2462nready (...)
2257 { 2501 {
2258 av_fill (defav, len - 1); 2502 av_fill (defav, len - 1);
2259 for (i = 0; i < len; ++i) 2503 for (i = 0; i < len; ++i)
2260 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 2504 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2261 } 2505 }
2262
2263 SvREFCNT_dec (invoke);
2264} 2506}
2265 2507
2266void 2508void
2267_pool_2 (SV *cb) 2509_pool_2 (SV *cb)
2268 CODE: 2510 CODE:
2288 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 2530 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2289 2531
2290 coro->prio = 0; 2532 coro->prio = 0;
2291 2533
2292 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2534 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2293 api_trace (coro_current, 0); 2535 api_trace (aTHX_ coro_current, 0);
2294 2536
2295 av_push (av_async_pool, newSVsv (coro_current)); 2537 av_push (av_async_pool, newSVsv (coro_current));
2296} 2538}
2297 2539
2298#if 0 2540#if 0
2346 2588
2347MODULE = Coro::State PACKAGE = Coro::AIO 2589MODULE = Coro::State PACKAGE = Coro::AIO
2348 2590
2349void 2591void
2350_get_state (SV *self) 2592_get_state (SV *self)
2593 PROTOTYPE: $
2351 PPCODE: 2594 PPCODE:
2352{ 2595{
2353 AV *defav = GvAV (PL_defgv); 2596 AV *defav = GvAV (PL_defgv);
2354 AV *av = newAV (); 2597 AV *av = newAV ();
2355 int i; 2598 int i;
2370 2613
2371 av_push (av, data_sv); 2614 av_push (av, data_sv);
2372 2615
2373 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 2616 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2374 2617
2375 api_ready (self); 2618 api_ready (aTHX_ self);
2376} 2619}
2377 2620
2378void 2621void
2379_set_state (SV *state) 2622_set_state (SV *state)
2380 PROTOTYPE: $ 2623 PROTOTYPE: $
2400BOOT: 2643BOOT:
2401 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); 2644 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2402 2645
2403SV * 2646SV *
2404_schedule (...) 2647_schedule (...)
2405 PROTOTYPE: @
2406 CODE: 2648 CODE:
2407{ 2649{
2408 static int incede; 2650 static int incede;
2409 2651
2410 api_cede_notself (); 2652 api_cede_notself (aTHX);
2411 2653
2412 ++incede; 2654 ++incede;
2413 while (coro_nready >= incede && api_cede ()) 2655 while (coro_nready >= incede && api_cede (aTHX))
2414 ; 2656 ;
2415 2657
2416 sv_setsv (sv_activity, &PL_sv_undef); 2658 sv_setsv (sv_activity, &PL_sv_undef);
2417 if (coro_nready >= incede) 2659 if (coro_nready >= incede)
2418 { 2660 {
2429MODULE = Coro::State PACKAGE = PerlIO::cede 2671MODULE = Coro::State PACKAGE = PerlIO::cede
2430 2672
2431BOOT: 2673BOOT:
2432 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2674 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2433 2675
2676MODULE = Coro::State PACKAGE = Coro::Semaphore
2677
2678SV *
2679new (SV *klass, SV *count_ = 0)
2680 CODE:
2681{
2682 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2683 AV *av = newAV ();
2684 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1));
2685 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2686}
2687 OUTPUT:
2688 RETVAL
2689
2690SV *
2691count (SV *self)
2692 CODE:
2693 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]);
2694 OUTPUT:
2695 RETVAL
2696
2697void
2698up (SV *self, int adjust = 1)
2699 ALIAS:
2700 adjust = 1
2701 CODE:
2702{
2703 AV *av = (AV *)SvRV (self);
2704 SV *count_sv = AvARRAY (av)[0];
2705 IV count = SvIVX (count_sv);
2706
2707 count += ix ? adjust : 1;
2708 SvIVX (count_sv) = count;
2709
2710 /* now wake up as many waiters as possible */
2711 while (count > 0 && AvFILLp (av) >= count)
2712 {
2713 SV *cb;
2714
2715 /* swap first two elements so we can shift a waiter */
2716 AvARRAY (av)[0] = AvARRAY (av)[1];
2717 AvARRAY (av)[1] = count_sv;
2718 cb = av_shift (av);
2719
2720 if (SvOBJECT (cb))
2721 api_ready (cb);
2722 else
2723 croak ("callbacks not yet supported");
2724
2725 SvREFCNT_dec (cb);
2726 }
2727}
2728
2729void
2730down (SV *self)
2731 CODE:
2732 api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), items);
2733
2734void
2735try (SV *self)
2736 PPCODE:
2737{
2738 AV *av = (AV *)SvRV (self);
2739 SV *count_sv = AvARRAY (av)[0];
2740 IV count = SvIVX (count_sv);
2741
2742 if (count > 0)
2743 {
2744 --count;
2745 SvIVX (count_sv) = count;
2746 XSRETURN_YES;
2747 }
2748 else
2749 XSRETURN_NO;
2750}
2751
2752void
2753waiters (SV *self)
2754 CODE:
2755{
2756 AV *av = (AV *)SvRV (self);
2757
2758 if (GIMME_V == G_SCALAR)
2759 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0])));
2760 else
2761 {
2762 int i;
2763 EXTEND (SP, AvFILLp (av) + 1 - 1);
2764 for (i = 1; i <= AvFILLp (av); ++i)
2765 PUSHs (newSVsv (AvARRAY (av)[i]));
2766 }
2767}
2768

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines