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.268 by root, Fri Nov 14 06:41:41 2008 UTC vs.
Revision 1.282 by root, Sun Nov 16 10:33:08 2008 UTC

142#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
143 143
144#include "CoroAPI.h" 144#include "CoroAPI.h"
145 145
146#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
148static perl_mutex coro_lock;
149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD 147# if CORO_PTHREAD
152static void *coro_thx; 148static void *coro_thx;
153# endif 149# endif
154
155#else
156
157# define LOCK (void)0
158# define UNLOCK (void)0
159
160#endif 150#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
166 151
167/* helper storage struct for Coro::AIO */ 152/* helper storage struct for Coro::AIO */
168struct io_state 153struct io_state
169{ 154{
170 AV *res; 155 AV *res;
183static JMPENV *main_top_env; 168static JMPENV *main_top_env;
184static HV *coro_state_stash, *coro_stash; 169static HV *coro_state_stash, *coro_stash;
185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
186static volatile struct coro *transfer_next; 171static volatile struct coro *transfer_next;
187 172
188struct transfer_args
189{
190 struct coro *prev, *next;
191};
192
193static GV *irsgv; /* $/ */ 173static GV *irsgv; /* $/ */
194static GV *stdoutgv; /* *STDOUT */ 174static GV *stdoutgv; /* *STDOUT */
195static SV *rv_diehook; 175static SV *rv_diehook;
196static SV *rv_warnhook; 176static SV *rv_warnhook;
197static HV *hv_sig; /* %SIG */ 177static HV *hv_sig; /* %SIG */
215 CC_TRACE_LINE = 0x10, /* trace each statement */ 195 CC_TRACE_LINE = 0x10, /* trace each statement */
216 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, 196 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
217}; 197};
218 198
219/* this is a structure representing a c-level coroutine */ 199/* this is a structure representing a c-level coroutine */
220typedef struct coro_cctx { 200typedef struct coro_cctx
201{
221 struct coro_cctx *next; 202 struct coro_cctx *next;
222 203
223 /* the stack */ 204 /* the stack */
224 void *sptr; 205 void *sptr;
225 size_t ssize; 206 size_t ssize;
243 CF_NEW = 0x0004, /* has never been switched to */ 224 CF_NEW = 0x0004, /* has never been switched to */
244 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 225 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
245}; 226};
246 227
247/* the structure where most of the perl state is stored, overlaid on the cxstack */ 228/* the structure where most of the perl state is stored, overlaid on the cxstack */
248typedef struct { 229typedef struct
230{
249 SV *defsv; 231 SV *defsv;
250 AV *defav; 232 AV *defav;
251 SV *errsv; 233 SV *errsv;
252 SV *irsgv; 234 SV *irsgv;
253#define VAR(name,type) type name; 235#define VAR(name,type) type name;
257 239
258#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 240#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
259 241
260/* this is a structure representing a perl-level coroutine */ 242/* this is a structure representing a perl-level coroutine */
261struct coro { 243struct coro {
262 /* the c coroutine allocated to this perl coroutine, if any */ 244 /* the C coroutine allocated to this perl coroutine, if any */
263 coro_cctx *cctx; 245 coro_cctx *cctx;
264 246
265 /* process data */ 247 /* process data */
248 struct CoroSLF slf_frame; /* saved slf frame */
266 AV *mainstack; 249 AV *mainstack;
267 perl_slots *slot; /* basically the saved sp */ 250 perl_slots *slot; /* basically the saved sp */
268 251
269 AV *args; /* data associated with this coroutine (initial args) */ 252 AV *args; /* data associated with this coroutine (initial args) */
270 int refcnt; /* coroutines are refcounted, yes */ 253 int refcnt; /* coroutines are refcounted, yes */
271 int flags; /* CF_ flags */ 254 int flags; /* CF_ flags */
272 HV *hv; /* the perl hash associated with this coro, if any */ 255 HV *hv; /* the perl hash associated with this coro, if any */
256 void (*on_destroy)(pTHX_ struct coro *coro);
273 257
274 /* statistics */ 258 /* statistics */
275 int usecount; /* number of transfers to this coro */ 259 int usecount; /* number of transfers to this coro */
276 260
277 /* coro process data */ 261 /* coro process data */
285 struct coro *next, *prev; 269 struct coro *next, *prev;
286}; 270};
287 271
288typedef struct coro *Coro__State; 272typedef struct coro *Coro__State;
289typedef struct coro *Coro__State_or_hashref; 273typedef struct coro *Coro__State_or_hashref;
274
275static struct CoroSLF slf_frame; /* the current slf frame */
290 276
291/** Coro ********************************************************************/ 277/** Coro ********************************************************************/
292 278
293#define PRIO_MAX 3 279#define PRIO_MAX 3
294#define PRIO_HIGH 1 280#define PRIO_HIGH 1
299 285
300/* for Coro.pm */ 286/* for Coro.pm */
301static SV *coro_current; 287static SV *coro_current;
302static SV *coro_readyhook; 288static SV *coro_readyhook;
303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 289static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
304static int coro_nready;
305static struct coro *coro_first; 290static struct coro *coro_first;
291#define coro_nready coroapi.nready
306 292
307/** lowlevel stuff **********************************************************/ 293/** lowlevel stuff **********************************************************/
308 294
309static SV * 295static SV *
310coro_get_sv (pTHX_ const char *name, int create) 296coro_get_sv (pTHX_ const char *name, int create)
403static MGVTBL coro_cv_vtbl = { 389static MGVTBL coro_cv_vtbl = {
404 0, 0, 0, 0, 390 0, 0, 0, 0,
405 coro_cv_free 391 coro_cv_free
406}; 392};
407 393
408#define CORO_MAGIC(sv, type) \ 394#define CORO_MAGIC(sv, type) \
409 SvMAGIC (sv) \ 395 expect_true (SvMAGIC (sv)) \
410 ? SvMAGIC (sv)->mg_type == type \ 396 ? expect_true (SvMAGIC (sv)->mg_type == type) \
411 ? SvMAGIC (sv) \ 397 ? SvMAGIC (sv) \
412 : mg_find (sv, type) \ 398 : mg_find (sv, type) \
413 : 0 399 : 0
414 400
415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 401#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 402#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
417 403
438 mg = CORO_MAGIC_state (coro); 424 mg = CORO_MAGIC_state (coro);
439 return (struct coro *)mg->mg_ptr; 425 return (struct coro *)mg->mg_ptr;
440} 426}
441 427
442#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 428#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
429
430/* fastert than SvSTATE, but expects a coroutine hv */
431INLINE struct coro *
432SvSTATE_hv (SV *sv)
433{
434 MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state)
435 ? SvMAGIC (sv)
436 : mg_find (sv, CORO_MAGIC_type_state);
437
438 return (struct coro *)mg->mg_ptr;
439}
440
441#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
443 442
444/* the next two functions merely cache the padlists */ 443/* the next two functions merely cache the padlists */
445static void 444static void
446get_padlist (pTHX_ CV *cv) 445get_padlist (pTHX_ CV *cv)
447{ 446{
514 CvPADLIST (cv) = (AV *)POPs; 513 CvPADLIST (cv) = (AV *)POPs;
515 } 514 }
516 515
517 PUTBACK; 516 PUTBACK;
518 } 517 }
518
519 slf_frame = c->slf_frame;
519} 520}
520 521
521static void 522static void
522save_perl (pTHX_ Coro__State c) 523save_perl (pTHX_ Coro__State c)
523{ 524{
525 c->slf_frame = slf_frame;
526
524 { 527 {
525 dSP; 528 dSP;
526 I32 cxix = cxstack_ix; 529 I32 cxix = cxstack_ix;
527 PERL_CONTEXT *ccstk = cxstack; 530 PERL_CONTEXT *ccstk = cxstack;
528 PERL_SI *top_si = PL_curstackinfo; 531 PERL_SI *top_si = PL_curstackinfo;
595 #undef VAR 598 #undef VAR
596 } 599 }
597} 600}
598 601
599/* 602/*
600 * allocate various perl stacks. This is an exact copy 603 * allocate various perl stacks. This is almost an exact copy
601 * of perl.c:init_stacks, except that it uses less memory 604 * of perl.c:init_stacks, except that it uses less memory
602 * on the (sometimes correct) assumption that coroutines do 605 * on the (sometimes correct) assumption that coroutines do
603 * not usually need a lot of stackspace. 606 * not usually need a lot of stackspace.
604 */ 607 */
605#if CORO_PREFER_PERL_FUNCTIONS 608#if CORO_PREFER_PERL_FUNCTIONS
712#endif 715#endif
713 } 716 }
714 } 717 }
715 718
716 return rss; 719 return rss;
717}
718
719/** set stacklevel support **************************************************/
720
721/* we sometimes need to create the effect of pp_set_stacklevel calling us */
722#define SSL_HEAD (void)0
723/* we sometimes need to create the effect of leaving via pp_set_stacklevel */
724#define SSL_TAIL set_stacklevel_tail (aTHX)
725
726INLINE void
727set_stacklevel_tail (pTHX)
728{
729 dSP;
730 SV **bot = SP;
731
732 int gimme = GIMME_V;
733
734 /* make sure we put something on the stack in scalar context */
735 if (gimme == G_SCALAR)
736 {
737 if (sp == bot)
738 XPUSHs (&PL_sv_undef);
739
740 SP = bot + 1;
741 }
742
743 PUTBACK;
744} 720}
745 721
746/** coroutine stack handling ************************************************/ 722/** coroutine stack handling ************************************************/
747 723
748static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 724static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
834 810
835 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 811 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
836} 812}
837 813
838static void 814static void
815prepare_nop (pTHX_ struct coro_transfer_args *ta)
816{
817 /* kind of mega-hacky, but works */
818 ta->next = ta->prev = (struct coro *)ta;
819}
820
821static int
822slf_check_nop (pTHX_ struct CoroSLF *frame)
823{
824 return 0;
825}
826
827static void
839coro_setup (pTHX_ struct coro *coro) 828coro_setup (pTHX_ struct coro *coro)
840{ 829{
841 /* 830 /*
842 * emulate part of the perl startup here. 831 * emulate part of the perl startup here.
843 */ 832 */
882 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 871 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
883 SPAGAIN; 872 SPAGAIN;
884 } 873 }
885 874
886 /* this newly created coroutine might be run on an existing cctx which most 875 /* this newly created coroutine might be run on an existing cctx which most
887 * likely was suspended in set_stacklevel, called from pp_set_stacklevel, 876 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
888 * so we have to emulate entering pp_set_stacklevel here.
889 */ 877 */
890 SSL_HEAD; 878 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
879 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
891} 880}
892 881
893static void 882static void
894coro_destruct (pTHX_ struct coro *coro) 883coro_destruct (pTHX_ struct coro *coro)
895{ 884{
939static int 928static int
940runops_trace (pTHX) 929runops_trace (pTHX)
941{ 930{
942 COP *oldcop = 0; 931 COP *oldcop = 0;
943 int oldcxix = -2; 932 int oldcxix = -2;
944 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ 933 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
945 coro_cctx *cctx = coro->cctx; 934 coro_cctx *cctx = coro->cctx;
946 935
947 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 936 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
948 { 937 {
949 PERL_ASYNC_CHECK (); 938 PERL_ASYNC_CHECK ();
1059 TAINT_NOT; 1048 TAINT_NOT;
1060 return 0; 1049 return 0;
1061} 1050}
1062 1051
1063static void 1052static void
1064prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) 1053prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx)
1065{ 1054{
1066 ta->prev = (struct coro *)cctx; 1055 ta->prev = (struct coro *)cctx;
1067 ta->next = 0; 1056 ta->next = 0;
1068} 1057}
1069 1058
1103 struct coro *next = (struct coro *)transfer_next; 1092 struct coro *next = (struct coro *)transfer_next;
1104 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ 1093 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1105 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); 1094 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1106 1095
1107 free_coro_mortal (aTHX); 1096 free_coro_mortal (aTHX);
1108 UNLOCK;
1109 1097
1110 if (expect_false (next->throw)) 1098 if (expect_false (next->throw))
1111 { 1099 {
1112 SV *exception = sv_2mortal (next->throw); 1100 SV *exception = sv_2mortal (next->throw);
1113 1101
1129# endif 1117# endif
1130#endif 1118#endif
1131 { 1119 {
1132 dTHX; 1120 dTHX;
1133 1121
1134 /* we are the alternative tail to pp_set_stacklevel */ 1122 /* normally we would need to skip the entersub here */
1135 /* so do the same things here */ 1123 /* not doing so will re-execute it, which is exactly what we want */
1136 SSL_TAIL;
1137
1138 /* we now skip the op that did lead to transfer() */
1139 PL_op = PL_op->op_next; 1124 /* PL_nop = PL_nop->op_next */
1140 1125
1141 /* inject a fake subroutine call to cctx_init */ 1126 /* inject a fake subroutine call to cctx_init */
1142 cctx_prepare (aTHX_ (coro_cctx *)arg); 1127 cctx_prepare (aTHX_ (coro_cctx *)arg);
1143 1128
1144 /* cctx_run is the alternative tail of transfer() */ 1129 /* cctx_run is the alternative tail of transfer() */
1130 /* TODO: throwing an exception here might be deadly, VERIFY */
1145 transfer_tail (aTHX); 1131 transfer_tail (aTHX);
1146 1132
1147 /* somebody or something will hit me for both perl_run and PL_restartop */ 1133 /* somebody or something will hit me for both perl_run and PL_restartop */
1148 PL_restartop = PL_op; 1134 PL_restartop = PL_op;
1149 perl_run (PL_curinterp); 1135 perl_run (PL_curinterp);
1308transfer_check (pTHX_ struct coro *prev, struct coro *next) 1294transfer_check (pTHX_ struct coro *prev, struct coro *next)
1309{ 1295{
1310 if (expect_true (prev != next)) 1296 if (expect_true (prev != next))
1311 { 1297 {
1312 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1298 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1313 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); 1299 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,");
1314 1300
1315 if (expect_false (next->flags & CF_RUNNING)) 1301 if (expect_false (next->flags & CF_RUNNING))
1316 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1302 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1317 1303
1318 if (expect_false (next->flags & CF_DESTROYED)) 1304 if (expect_false (next->flags & CF_DESTROYED))
1319 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1305 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,");
1320 1306
1321#if !PERL_VERSION_ATLEAST (5,10,0) 1307#if !PERL_VERSION_ATLEAST (5,10,0)
1322 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1308 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1323 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); 1309 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1324#endif 1310#endif
1325 } 1311 }
1326} 1312}
1327 1313
1328/* always use the TRANSFER macro */ 1314/* always use the TRANSFER macro */
1332 dSTACKLEVEL; 1318 dSTACKLEVEL;
1333 1319
1334 /* sometimes transfer is only called to set idle_sp */ 1320 /* sometimes transfer is only called to set idle_sp */
1335 if (expect_false (!next)) 1321 if (expect_false (!next))
1336 { 1322 {
1337 ((coro_cctx *)prev)->idle_sp = stacklevel; 1323 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel;
1338 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1324 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1339 } 1325 }
1340 else if (expect_true (prev != next)) 1326 else if (expect_true (prev != next))
1341 { 1327 {
1342 coro_cctx *prev__cctx; 1328 coro_cctx *prev__cctx;
1349 prev->flags |= CF_RUNNING; 1335 prev->flags |= CF_RUNNING;
1350 } 1336 }
1351 1337
1352 prev->flags &= ~CF_RUNNING; 1338 prev->flags &= ~CF_RUNNING;
1353 next->flags |= CF_RUNNING; 1339 next->flags |= CF_RUNNING;
1354
1355 LOCK;
1356 1340
1357 /* first get rid of the old state */ 1341 /* first get rid of the old state */
1358 save_perl (aTHX_ prev); 1342 save_perl (aTHX_ prev);
1359 1343
1360 if (expect_false (next->flags & CF_NEW)) 1344 if (expect_false (next->flags & CF_NEW))
1369 1353
1370 prev__cctx = prev->cctx; 1354 prev__cctx = prev->cctx;
1371 1355
1372 /* possibly untie and reuse the cctx */ 1356 /* possibly untie and reuse the cctx */
1373 if (expect_true ( 1357 if (expect_true (
1374 prev__cctx->idle_sp == stacklevel 1358 prev__cctx->idle_sp == (void *)stacklevel
1375 && !(prev__cctx->flags & CC_TRACE) 1359 && !(prev__cctx->flags & CC_TRACE)
1376 && !force_cctx 1360 && !force_cctx
1377 )) 1361 ))
1378 { 1362 {
1379 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1363 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1418coro_state_destroy (pTHX_ struct coro *coro) 1402coro_state_destroy (pTHX_ struct coro *coro)
1419{ 1403{
1420 if (coro->flags & CF_DESTROYED) 1404 if (coro->flags & CF_DESTROYED)
1421 return 0; 1405 return 0;
1422 1406
1407 if (coro->on_destroy)
1408 coro->on_destroy (aTHX_ coro);
1409
1423 coro->flags |= CF_DESTROYED; 1410 coro->flags |= CF_DESTROYED;
1424 1411
1425 if (coro->flags & CF_READY) 1412 if (coro->flags & CF_READY)
1426 { 1413 {
1427 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1414 /* reduce nready, as destroying a ready coro effectively unreadies it */
1428 /* alternative: look through all ready queues and remove the coro */ 1415 /* alternative: look through all ready queues and remove the coro */
1429 LOCK;
1430 --coro_nready; 1416 --coro_nready;
1431 UNLOCK;
1432 } 1417 }
1433 else 1418 else
1434 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1419 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1435 1420
1436 if (coro->mainstack && coro->mainstack != main_mainstack) 1421 if (coro->mainstack && coro->mainstack != main_mainstack)
1437 { 1422 {
1438 struct coro temp; 1423 struct coro temp;
1439 1424
1440 if (coro->flags & CF_RUNNING) 1425 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1441 croak ("FATAL: tried to destroy currently running coroutine");
1442 1426
1443 save_perl (aTHX_ &temp); 1427 save_perl (aTHX_ &temp);
1444 load_perl (aTHX_ coro); 1428 load_perl (aTHX_ coro);
1445 1429
1446 coro_destruct (aTHX_ coro); 1430 coro_destruct (aTHX_ coro);
1497# define MGf_DUP 0 1481# define MGf_DUP 0
1498#endif 1482#endif
1499}; 1483};
1500 1484
1501static void 1485static void
1502prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1486prepare_transfer (pTHX_ struct coro_transfer_args *ta, SV *prev_sv, SV *next_sv)
1503{ 1487{
1504 ta->prev = SvSTATE (prev_sv); 1488 ta->prev = SvSTATE (prev_sv);
1505 ta->next = SvSTATE (next_sv); 1489 ta->next = SvSTATE (next_sv);
1506 TRANSFER_CHECK (*ta); 1490 TRANSFER_CHECK (*ta);
1507} 1491}
1508 1492
1509static void 1493static void
1510api_transfer (SV *prev_sv, SV *next_sv) 1494api_transfer (pTHX_ SV *prev_sv, SV *next_sv)
1511{ 1495{
1512 dTHX;
1513 struct transfer_args ta; 1496 struct coro_transfer_args ta;
1514 1497
1515 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1498 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1516 TRANSFER (ta, 1); 1499 TRANSFER (ta, 1);
1517} 1500}
1518 1501
1519/** Coro ********************************************************************/ 1502/** Coro ********************************************************************/
1520 1503
1521static void 1504INLINE void
1522coro_enq (pTHX_ SV *coro_sv) 1505coro_enq (pTHX_ struct coro *coro)
1523{ 1506{
1524 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1507 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv));
1525} 1508}
1526 1509
1527static SV * 1510INLINE SV *
1528coro_deq (pTHX) 1511coro_deq (pTHX)
1529{ 1512{
1530 int prio; 1513 int prio;
1531 1514
1532 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1515 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1535 1518
1536 return 0; 1519 return 0;
1537} 1520}
1538 1521
1539static int 1522static int
1540api_ready (SV *coro_sv) 1523api_ready (pTHX_ SV *coro_sv)
1541{ 1524{
1542 dTHX;
1543 struct coro *coro; 1525 struct coro *coro;
1544 SV *sv_hook; 1526 SV *sv_hook;
1545 void (*xs_hook)(void); 1527 void (*xs_hook)(void);
1546 1528
1547 if (SvROK (coro_sv)) 1529 if (SvROK (coro_sv))
1552 if (coro->flags & CF_READY) 1534 if (coro->flags & CF_READY)
1553 return 0; 1535 return 0;
1554 1536
1555 coro->flags |= CF_READY; 1537 coro->flags |= CF_READY;
1556 1538
1557 LOCK;
1558
1559 sv_hook = coro_nready ? 0 : coro_readyhook; 1539 sv_hook = coro_nready ? 0 : coro_readyhook;
1560 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1540 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1561 1541
1562 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); 1542 coro_enq (aTHX_ coro);
1563 ++coro_nready; 1543 ++coro_nready;
1564 1544
1565 UNLOCK;
1566
1567 if (sv_hook) 1545 if (sv_hook)
1568 { 1546 {
1569 dSP; 1547 dSP;
1570 1548
1571 ENTER; 1549 ENTER;
1585 1563
1586 return 1; 1564 return 1;
1587} 1565}
1588 1566
1589static int 1567static int
1590api_is_ready (SV *coro_sv) 1568api_is_ready (pTHX_ SV *coro_sv)
1591{ 1569{
1592 dTHX;
1593
1594 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1570 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1595} 1571}
1596 1572
1597INLINE void 1573INLINE void
1598prepare_schedule (pTHX_ struct transfer_args *ta) 1574prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1599{ 1575{
1600 SV *prev_sv, *next_sv; 1576 SV *prev_sv, *next_sv;
1601 1577
1602 for (;;) 1578 for (;;)
1603 { 1579 {
1604 LOCK;
1605 next_sv = coro_deq (aTHX); 1580 next_sv = coro_deq (aTHX);
1606 1581
1607 /* nothing to schedule: call the idle handler */ 1582 /* nothing to schedule: call the idle handler */
1608 if (expect_false (!next_sv)) 1583 if (expect_false (!next_sv))
1609 { 1584 {
1610 dSP; 1585 dSP;
1611 UNLOCK;
1612 1586
1613 ENTER; 1587 ENTER;
1614 SAVETMPS; 1588 SAVETMPS;
1615 1589
1616 PUSHMARK (SP); 1590 PUSHMARK (SP);
1621 FREETMPS; 1595 FREETMPS;
1622 LEAVE; 1596 LEAVE;
1623 continue; 1597 continue;
1624 } 1598 }
1625 1599
1626 ta->next = SvSTATE (next_sv); 1600 ta->next = SvSTATE_hv (next_sv);
1627 1601
1628 /* cannot transfer to destroyed coros, skip and look for next */ 1602 /* cannot transfer to destroyed coros, skip and look for next */
1629 if (expect_false (ta->next->flags & CF_DESTROYED)) 1603 if (expect_false (ta->next->flags & CF_DESTROYED))
1630 { 1604 {
1631 UNLOCK;
1632 SvREFCNT_dec (next_sv); 1605 SvREFCNT_dec (next_sv);
1633 /* coro_nready has already been taken care of by destroy */ 1606 /* coro_nready has already been taken care of by destroy */
1634 continue; 1607 continue;
1635 } 1608 }
1636 1609
1637 --coro_nready; 1610 --coro_nready;
1638 UNLOCK;
1639 break; 1611 break;
1640 } 1612 }
1641 1613
1642 /* free this only after the transfer */ 1614 /* free this only after the transfer */
1643 prev_sv = SvRV (coro_current); 1615 prev_sv = SvRV (coro_current);
1644 ta->prev = SvSTATE (prev_sv); 1616 ta->prev = SvSTATE_hv (prev_sv);
1645 TRANSFER_CHECK (*ta); 1617 TRANSFER_CHECK (*ta);
1646 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1618 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1647 ta->next->flags &= ~CF_READY; 1619 ta->next->flags &= ~CF_READY;
1648 SvRV_set (coro_current, next_sv); 1620 SvRV_set (coro_current, next_sv);
1649 1621
1650 LOCK;
1651 free_coro_mortal (aTHX); 1622 free_coro_mortal (aTHX);
1652 coro_mortal = prev_sv; 1623 coro_mortal = prev_sv;
1653 UNLOCK;
1654} 1624}
1655 1625
1656INLINE void 1626INLINE void
1657prepare_cede (pTHX_ struct transfer_args *ta) 1627prepare_cede (pTHX_ struct coro_transfer_args *ta)
1658{ 1628{
1659 api_ready (coro_current); 1629 api_ready (aTHX_ coro_current);
1660 prepare_schedule (aTHX_ ta); 1630 prepare_schedule (aTHX_ ta);
1661} 1631}
1662 1632
1663static void 1633INLINE void
1664prepare_cede_notself (pTHX_ struct transfer_args *ta) 1634prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1665{ 1635{
1666 SV *prev = SvRV (coro_current); 1636 SV *prev = SvRV (coro_current);
1667 1637
1668 if (coro_nready) 1638 if (coro_nready)
1669 { 1639 {
1670 prepare_schedule (aTHX_ ta); 1640 prepare_schedule (aTHX_ ta);
1671 api_ready (prev); 1641 api_ready (aTHX_ prev);
1672 } 1642 }
1673 else 1643 else
1674 ta->prev = ta->next = SvSTATE (prev); 1644 prepare_nop (aTHX_ ta);
1675} 1645}
1676 1646
1677static void 1647static void
1678api_schedule (void) 1648api_schedule (pTHX)
1679{ 1649{
1680 dTHX;
1681 struct transfer_args ta; 1650 struct coro_transfer_args ta;
1682 1651
1683 prepare_schedule (aTHX_ &ta); 1652 prepare_schedule (aTHX_ &ta);
1684 TRANSFER (ta, 1); 1653 TRANSFER (ta, 1);
1685} 1654}
1686 1655
1687static int 1656static int
1688api_cede (void) 1657api_cede (pTHX)
1689{ 1658{
1690 dTHX;
1691 struct transfer_args ta; 1659 struct coro_transfer_args ta;
1692 1660
1693 prepare_cede (aTHX_ &ta); 1661 prepare_cede (aTHX_ &ta);
1694 1662
1695 if (expect_true (ta.prev != ta.next)) 1663 if (expect_true (ta.prev != ta.next))
1696 { 1664 {
1700 else 1668 else
1701 return 0; 1669 return 0;
1702} 1670}
1703 1671
1704static int 1672static int
1705api_cede_notself (void) 1673api_cede_notself (pTHX)
1706{ 1674{
1707 if (coro_nready) 1675 if (coro_nready)
1708 { 1676 {
1709 dTHX;
1710 struct transfer_args ta; 1677 struct coro_transfer_args ta;
1711 1678
1712 prepare_cede_notself (aTHX_ &ta); 1679 prepare_cede_notself (aTHX_ &ta);
1713 TRANSFER (ta, 1); 1680 TRANSFER (ta, 1);
1714 return 1; 1681 return 1;
1715 } 1682 }
1716 else 1683 else
1717 return 0; 1684 return 0;
1718} 1685}
1719 1686
1720static void 1687static void
1721api_trace (SV *coro_sv, int flags) 1688api_trace (pTHX_ SV *coro_sv, int flags)
1722{ 1689{
1723 dTHX;
1724 struct coro *coro = SvSTATE (coro_sv); 1690 struct coro *coro = SvSTATE (coro_sv);
1725 1691
1726 if (flags & CC_TRACE) 1692 if (flags & CC_TRACE)
1727 { 1693 {
1728 if (!coro->cctx) 1694 if (!coro->cctx)
1729 coro->cctx = cctx_new_run (); 1695 coro->cctx = cctx_new_run ();
1730 else if (!(coro->cctx->flags & CC_TRACE)) 1696 else if (!(coro->cctx->flags & CC_TRACE))
1731 croak ("cannot enable tracing on coroutine with custom stack"); 1697 croak ("cannot enable tracing on coroutine with custom stack,");
1732 1698
1733 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1699 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1734 } 1700 }
1735 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1701 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1736 { 1702 {
1741 else 1707 else
1742 coro->slot->runops = RUNOPS_DEFAULT; 1708 coro->slot->runops = RUNOPS_DEFAULT;
1743 } 1709 }
1744} 1710}
1745 1711
1746#if 0
1747static int
1748coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1749{
1750 AV *padlist;
1751 AV *av = (AV *)mg->mg_obj;
1752
1753 abort ();
1754
1755 return 0;
1756}
1757
1758static MGVTBL coro_gensub_vtbl = {
1759 0, 0, 0, 0,
1760 coro_gensub_free
1761};
1762#endif
1763
1764/*****************************************************************************/ 1712/*****************************************************************************/
1765/* PerlIO::cede */ 1713/* PerlIO::cede */
1766 1714
1767typedef struct 1715typedef struct
1768{ 1716{
1795 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1743 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1796 double now = nvtime (); 1744 double now = nvtime ();
1797 1745
1798 if (now >= self->next) 1746 if (now >= self->next)
1799 { 1747 {
1800 api_cede (); 1748 api_cede (aTHX);
1801 self->next = now + self->every; 1749 self->next = now + self->every;
1802 } 1750 }
1803 1751
1804 return PerlIOBuf_flush (aTHX_ f); 1752 return PerlIOBuf_flush (aTHX_ f);
1805} 1753}
1836 PerlIOBuf_set_ptrcnt, 1784 PerlIOBuf_set_ptrcnt,
1837}; 1785};
1838 1786
1839/*****************************************************************************/ 1787/*****************************************************************************/
1840 1788
1841static const CV *ssl_cv; /* for quick consistency check */ 1789static const CV *slf_cv; /* for quick consistency check */
1842 1790
1843static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ 1791static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1844static SV *ssl_arg0; 1792static SV *slf_arg0;
1845static SV *ssl_arg1; 1793static SV *slf_arg1;
1794static SV *slf_arg2;
1846 1795
1847/* this restores the stack in the case we patched the entersub, to */ 1796/* this restores the stack in the case we patched the entersub, to */
1848/* recreate the stack frame as perl will on following calls */ 1797/* recreate the stack frame as perl will on following calls */
1849/* since entersub cleared the stack */ 1798/* since entersub cleared the stack */
1850static OP * 1799static OP *
1853 dSP; 1802 dSP;
1854 1803
1855 PUSHMARK (SP); 1804 PUSHMARK (SP);
1856 1805
1857 EXTEND (SP, 3); 1806 EXTEND (SP, 3);
1858 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; 1807 if (slf_arg0) PUSHs (sv_2mortal (slf_arg0));
1859 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; 1808 if (slf_arg1) PUSHs (sv_2mortal (slf_arg1));
1809 if (slf_arg2) PUSHs (sv_2mortal (slf_arg2));
1860 PUSHs ((SV *)CvGV (ssl_cv)); 1810 PUSHs ((SV *)CvGV (slf_cv));
1861 1811
1862 RETURNOP (ssl_restore.op_first); 1812 RETURNOP (slf_restore.op_first);
1863} 1813}
1864 1814
1865#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */ 1815static void
1816slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1817{
1818 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1819}
1866 1820
1867/* declare prototype */ 1821static void
1868XS(XS_Coro__State__set_stacklevel); 1822slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1823{
1824 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1825
1826 frame->prepare = slf_prepare_set_stacklevel;
1827 frame->check = slf_check_nop;
1828 frame->data = (void *)SvIV (arg [0]);
1829}
1830
1831static void
1832slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1833{
1834 SV **arg = (SV **)slf_frame.data;
1835
1836 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1837}
1838
1839static void
1840slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1841{
1842 if (items != 2)
1843 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items);
1844
1845 frame->prepare = slf_prepare_transfer;
1846 frame->check = slf_check_nop;
1847 frame->data = (void *)arg; /* let's hope it will stay valid */
1848}
1849
1850static void
1851slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1852{
1853 frame->prepare = prepare_schedule;
1854 frame->check = slf_check_nop;
1855}
1856
1857static void
1858slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1859{
1860 frame->prepare = prepare_cede;
1861 frame->check = slf_check_nop;
1862}
1863
1864static void
1865slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1866{
1867 frame->prepare = prepare_cede_notself;
1868 frame->check = slf_check_nop;
1869}
1870
1871/* we hijack an hopefully unused CV flag for our purposes */
1872#define CVf_SLF 0x4000
1869 1873
1870/* 1874/*
1871 * these not obviously related functions are all rolled into one 1875 * these not obviously related functions are all rolled into one
1872 * function to increase chances that they all will call transfer with the same 1876 * function to increase chances that they all will call transfer with the same
1873 * stack offset 1877 * stack offset
1878 * SLF stands for "schedule-like-function".
1874 */ 1879 */
1875static OP * 1880static OP *
1876pp_set_stacklevel (pTHX) 1881pp_slf (pTHX)
1877{ 1882{
1883 I32 checkmark; /* mark SP to see how many elements check has pushed */
1884
1885 /* set up the slf frame, unless it has already been set-up */
1886 /* the latter happens when a new coro has been started */
1887 /* or when a new cctx was attached to an existing coroutine */
1888 if (expect_true (!slf_frame.prepare))
1889 {
1890 /* first iteration */
1878 dSP; 1891 dSP;
1879 struct transfer_args ta;
1880 SV **arg = PL_stack_base + TOPMARK + 1; 1892 SV **arg = PL_stack_base + TOPMARK + 1;
1881 int items = SP - arg; /* args without function object */ 1893 int items = SP - arg; /* args without function object */
1894 SV *gv = *sp;
1882 1895
1883 /* do a quick consistency check on the "function" object, and if it isn't */ 1896 /* do a quick consistency check on the "function" object, and if it isn't */
1884 /* for us, divert to the real entersub */ 1897 /* for us, divert to the real entersub */
1885 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) 1898 if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF))
1886 return PL_ppaddr[OP_ENTERSUB](aTHX); 1899 return PL_ppaddr[OP_ENTERSUB](aTHX);
1887 1900
1888 /* pop args */ 1901 /* pop args */
1889 SP = PL_stack_base + POPMARK; 1902 SP = PL_stack_base + POPMARK;
1890 1903
1891 if (!(PL_op->op_flags & OPf_STACKED)) 1904 if (!(PL_op->op_flags & OPf_STACKED))
1892 { 1905 {
1893 /* ampersand-form of call, use @_ instead of stack */ 1906 /* ampersand-form of call, use @_ instead of stack */
1894 AV *av = GvAV (PL_defgv); 1907 AV *av = GvAV (PL_defgv);
1895 arg = AvARRAY (av); 1908 arg = AvARRAY (av);
1896 items = AvFILLp (av) + 1; 1909 items = AvFILLp (av) + 1;
1910 }
1911
1912 PUTBACK;
1913
1914 /* now call the init function, which needs to set up slf_frame */
1915 ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr)
1916 (aTHX_ &slf_frame, GvCV (gv), arg, items);
1917 }
1918
1919 /* now that we have a slf_frame, interpret it! */
1920 /* we use a callback system not to make the code needlessly */
1921 /* complicated, but so we can run multiple perl coros from one cctx */
1922
1923 do
1924 {
1925 struct coro_transfer_args ta;
1926
1927 slf_frame.prepare (aTHX_ &ta);
1928 TRANSFER (ta, 0);
1929
1930 checkmark = PL_stack_sp - PL_stack_base;
1931 }
1932 while (slf_frame.check (aTHX_ &slf_frame));
1933
1934 {
1935 dSP;
1936 SV **bot = PL_stack_base + checkmark;
1937 int gimme = GIMME_V;
1938
1939 slf_frame.prepare = 0; /* invalidate the frame, so it gets initialised again next time */
1940
1941 /* make sure we put something on the stack in scalar context */
1942 if (gimme == G_SCALAR)
1943 {
1944 if (sp == bot)
1945 XPUSHs (&PL_sv_undef);
1946
1947 SP = bot + 1;
1897 } 1948 }
1898 1949
1899 PUTBACK; 1950 PUTBACK;
1900 switch (PL_op->op_private & OPpENTERSUB_SSL)
1901 {
1902 case 0:
1903 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1904 break;
1905
1906 case 1:
1907 if (items != 2)
1908 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1909
1910 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1911 break;
1912
1913 case 2:
1914 prepare_schedule (aTHX_ &ta);
1915 break;
1916
1917 case 3:
1918 prepare_cede (aTHX_ &ta);
1919 break;
1920
1921 case 4:
1922 prepare_cede_notself (aTHX_ &ta);
1923 break;
1924 } 1951 }
1925 1952
1926 TRANSFER (ta, 0); 1953 return NORMAL;
1927 SPAGAIN;
1928
1929skip:
1930 PUTBACK;
1931 SSL_TAIL;
1932 SPAGAIN;
1933 RETURN;
1934} 1954}
1935 1955
1936static void 1956static void
1937coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items) 1957api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items)
1938{ 1958{
1939 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1940
1941 assert (("FATAL: ssl call with illegal CV value", CvGV (cv))); 1959 assert (("FATAL: SLF call with illegal CV value", !CvANON (cv)));
1960
1961 if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB]
1962 && PL_op->op_ppaddr != pp_slf)
1963 croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught");
1964
1965 if (items > 3)
1966 croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items);
1967
1968 CvFLAGS (cv) |= CVf_SLF;
1969 CvXSUBANY (cv).any_ptr = (void *)init_cb;
1942 ssl_cv = cv; 1970 slf_cv = cv;
1943 1971
1944 /* we patch the op, and then re-run the whole call */ 1972 /* we patch the op, and then re-run the whole call */
1945 /* we have to put some dummy argument on the stack for this to work */ 1973 /* we have to put the same argument on the stack for this to work */
1974 /* and this will be done by pp_restore */
1946 ssl_restore.op_next = (OP *)&ssl_restore; 1975 slf_restore.op_next = (OP *)&slf_restore;
1947 ssl_restore.op_type = OP_NULL; 1976 slf_restore.op_type = OP_CUSTOM;
1948 ssl_restore.op_ppaddr = pp_restore; 1977 slf_restore.op_ppaddr = pp_restore;
1949 ssl_restore.op_first = PL_op; 1978 slf_restore.op_first = PL_op;
1950 1979
1951 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; 1980 slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0;
1952 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; 1981 slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0;
1982 slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0;
1953 1983
1954 PL_op->op_ppaddr = pp_set_stacklevel; 1984 PL_op->op_ppaddr = pp_slf;
1955 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1956 1985
1957 PL_op = (OP *)&ssl_restore; 1986 PL_op = (OP *)&slf_restore;
1958} 1987}
1988
1989/*****************************************************************************/
1990
1991static void
1992coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
1993{
1994 SV *count_sv = AvARRAY (av)[0];
1995 IV count = SvIVX (count_sv);
1996
1997 count += adjust;
1998 SvIVX (count_sv) = count;
1999
2000 /* now wake up as many waiters as are expected to lock */
2001 while (count > 0 && AvFILLp (av) > 0)
2002 {
2003 SV *cb;
2004
2005 /* swap first two elements so we can shift a waiter */
2006 AvARRAY (av)[0] = AvARRAY (av)[1];
2007 AvARRAY (av)[1] = count_sv;
2008 cb = av_shift (av);
2009
2010 if (SvOBJECT (cb))
2011 api_ready (aTHX_ cb);
2012 else
2013 croak ("callbacks not yet supported");
2014
2015 SvREFCNT_dec (cb);
2016
2017 --count;
2018 }
2019}
2020
2021static void
2022coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2023{
2024 /* call $sem->adjust (0) to possibly wake up some other waiters */
2025 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2026}
2027
2028static int
2029slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2030{
2031 AV *av = (AV *)frame->data;
2032 SV *count_sv = AvARRAY (av)[0];
2033
2034 if (SvIVX (count_sv) > 0)
2035 {
2036 SvSTATE_current->on_destroy = 0;
2037 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2038 return 0;
2039 }
2040 else
2041 {
2042 int i;
2043 /* if we were woken up but can't down, we look through the whole */
2044 /* waiters list and only add us if we aren't in there already */
2045 /* this avoids some degenerate memory usage cases */
2046
2047 for (i = 1; i <= AvFILLp (av); ++i)
2048 if (AvARRAY (av)[i] == SvRV (coro_current))
2049 return 1;
2050
2051 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2052 return 1;
2053 }
2054}
2055
2056static void
2057slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2058{
2059 AV *av = (AV *)SvRV (arg [0]);
2060
2061 if (SvIVX (AvARRAY (av)[0]) > 0)
2062 {
2063 frame->data = (void *)av;
2064 frame->prepare = prepare_nop;
2065 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2066 }
2067 else
2068 {
2069 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2070
2071 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2072 frame->prepare = prepare_schedule;
2073
2074 /* to avoid race conditions when a woken-up coro gets terminated */
2075 /* we arrange for a temporary on_destroy that calls adjust (0) */
2076 assert (!SvSTATE_current->on_destroy);//D
2077 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2078 }
2079
2080 frame->check = slf_check_semaphore_down;
2081
2082}
2083
2084/*****************************************************************************/
2085
2086#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2087
2088/* create a closure from XS, returns a code reference */
2089/* the arg can be accessed via GENSUB_ARG from the callback */
2090/* the callback must use dXSARGS/XSRETURN */
2091static SV *
2092gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2093{
2094 CV *cv = (CV *)NEWSV (0, 0);
2095
2096 sv_upgrade ((SV *)cv, SVt_PVCV);
2097
2098 CvANON_on (cv);
2099 CvISXSUB_on (cv);
2100 CvXSUB (cv) = xsub;
2101 GENSUB_ARG = arg;
2102
2103 return newRV_noinc ((SV *)cv);
2104}
2105
2106/*****************************************************************************/
1959 2107
1960MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2108MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1961 2109
1962PROTOTYPES: DISABLE 2110PROTOTYPES: DISABLE
1963 2111
1964BOOT: 2112BOOT:
1965{ 2113{
1966#ifdef USE_ITHREADS 2114#ifdef USE_ITHREADS
1967 MUTEX_INIT (&coro_lock);
1968# if CORO_PTHREAD 2115# if CORO_PTHREAD
1969 coro_thx = PERL_GET_CONTEXT; 2116 coro_thx = PERL_GET_CONTEXT;
1970# endif 2117# endif
1971#endif 2118#endif
1972 BOOT_PAGESIZE; 2119 BOOT_PAGESIZE;
1993 main_top_env = PL_top_env; 2140 main_top_env = PL_top_env;
1994 2141
1995 while (main_top_env->je_prev) 2142 while (main_top_env->je_prev)
1996 main_top_env = main_top_env->je_prev; 2143 main_top_env = main_top_env->je_prev;
1997 2144
2145 {
2146 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2147
2148 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2149 hv_store_ent (PL_custom_op_names, slf,
2150 newSVpv ("coro_slf", 0), 0);
2151
2152 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2153 hv_store_ent (PL_custom_op_descs, slf,
2154 newSVpv ("coro schedule like function", 0), 0);
2155 }
2156
1998 coroapi.ver = CORO_API_VERSION; 2157 coroapi.ver = CORO_API_VERSION;
1999 coroapi.rev = CORO_API_REVISION; 2158 coroapi.rev = CORO_API_REVISION;
2159
2000 coroapi.transfer = api_transfer; 2160 coroapi.transfer = api_transfer;
2161
2162 coroapi.sv_state = SvSTATE_;
2163 coroapi.execute_slf = api_execute_slf;
2164 coroapi.prepare_nop = prepare_nop;
2165 coroapi.prepare_schedule = prepare_schedule;
2166 coroapi.prepare_cede = prepare_cede;
2167 coroapi.prepare_cede_notself = prepare_cede_notself;
2001 2168
2002 { 2169 {
2003 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 2170 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2004 2171
2005 if (!svp) croak ("Time::HiRes is required"); 2172 if (!svp) croak ("Time::HiRes is required");
2040 OUTPUT: 2207 OUTPUT:
2041 RETVAL 2208 RETVAL
2042 2209
2043void 2210void
2044_set_stacklevel (...) 2211_set_stacklevel (...)
2045 ALIAS: 2212 CODE:
2046 Coro::State::transfer = 1 2213 api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items);
2047 Coro::schedule = 2 2214
2048 Coro::cede = 3 2215void
2049 Coro::cede_notself = 4 2216transfer (...)
2050 CODE: 2217 PROTOTYPE: $$
2051 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items); 2218 CODE:
2219 api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items);
2052 2220
2053bool 2221bool
2054_destroy (SV *coro_sv) 2222_destroy (SV *coro_sv)
2055 CODE: 2223 CODE:
2056 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2224 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2063 CODE: 2231 CODE:
2064 _exit (code); 2232 _exit (code);
2065 2233
2066int 2234int
2067cctx_stacksize (int new_stacksize = 0) 2235cctx_stacksize (int new_stacksize = 0)
2236 PROTOTYPE: ;$
2068 CODE: 2237 CODE:
2069 RETVAL = cctx_stacksize; 2238 RETVAL = cctx_stacksize;
2070 if (new_stacksize) 2239 if (new_stacksize)
2071 { 2240 {
2072 cctx_stacksize = new_stacksize; 2241 cctx_stacksize = new_stacksize;
2075 OUTPUT: 2244 OUTPUT:
2076 RETVAL 2245 RETVAL
2077 2246
2078int 2247int
2079cctx_max_idle (int max_idle = 0) 2248cctx_max_idle (int max_idle = 0)
2249 PROTOTYPE: ;$
2080 CODE: 2250 CODE:
2081 RETVAL = cctx_max_idle; 2251 RETVAL = cctx_max_idle;
2082 if (max_idle > 1) 2252 if (max_idle > 1)
2083 cctx_max_idle = max_idle; 2253 cctx_max_idle = max_idle;
2084 OUTPUT: 2254 OUTPUT:
2085 RETVAL 2255 RETVAL
2086 2256
2087int 2257int
2088cctx_count () 2258cctx_count ()
2259 PROTOTYPE:
2089 CODE: 2260 CODE:
2090 RETVAL = cctx_count; 2261 RETVAL = cctx_count;
2091 OUTPUT: 2262 OUTPUT:
2092 RETVAL 2263 RETVAL
2093 2264
2094int 2265int
2095cctx_idle () 2266cctx_idle ()
2267 PROTOTYPE:
2096 CODE: 2268 CODE:
2097 RETVAL = cctx_idle; 2269 RETVAL = cctx_idle;
2098 OUTPUT: 2270 OUTPUT:
2099 RETVAL 2271 RETVAL
2100 2272
2101void 2273void
2102list () 2274list ()
2275 PROTOTYPE:
2103 PPCODE: 2276 PPCODE:
2104{ 2277{
2105 struct coro *coro; 2278 struct coro *coro;
2106 for (coro = coro_first; coro; coro = coro->next) 2279 for (coro = coro_first; coro; coro = coro->next)
2107 if (coro->hv) 2280 if (coro->hv)
2174 SvREFCNT_dec (self->throw); 2347 SvREFCNT_dec (self->throw);
2175 self->throw = SvOK (throw) ? newSVsv (throw) : 0; 2348 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2176 2349
2177void 2350void
2178api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2351api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2352 PROTOTYPE: $;$
2353 C_ARGS: aTHX_ coro, flags
2179 2354
2180SV * 2355SV *
2181has_cctx (Coro::State coro) 2356has_cctx (Coro::State coro)
2182 PROTOTYPE: $ 2357 PROTOTYPE: $
2183 CODE: 2358 CODE:
2207 OUTPUT: 2382 OUTPUT:
2208 RETVAL 2383 RETVAL
2209 2384
2210void 2385void
2211force_cctx () 2386force_cctx ()
2387 PROTOTYPE:
2212 CODE: 2388 CODE:
2213 struct coro *coro = SvSTATE (coro_current);
2214 coro->cctx->idle_sp = 0; 2389 SvSTATE_current->cctx->idle_sp = 0;
2215 2390
2216void 2391void
2217swap_defsv (Coro::State self) 2392swap_defsv (Coro::State self)
2218 PROTOTYPE: $ 2393 PROTOTYPE: $
2219 ALIAS: 2394 ALIAS:
2220 swap_defav = 1 2395 swap_defav = 1
2221 CODE: 2396 CODE:
2222 if (!self->slot) 2397 if (!self->slot)
2223 croak ("cannot swap state with coroutine that has no saved state"); 2398 croak ("cannot swap state with coroutine that has no saved state,");
2224 else 2399 else
2225 { 2400 {
2226 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); 2401 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2227 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2402 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2228 2403
2260 coroapi.schedule = api_schedule; 2435 coroapi.schedule = api_schedule;
2261 coroapi.cede = api_cede; 2436 coroapi.cede = api_cede;
2262 coroapi.cede_notself = api_cede_notself; 2437 coroapi.cede_notself = api_cede_notself;
2263 coroapi.ready = api_ready; 2438 coroapi.ready = api_ready;
2264 coroapi.is_ready = api_is_ready; 2439 coroapi.is_ready = api_is_ready;
2265 coroapi.nready = &coro_nready; 2440 coroapi.nready = coro_nready;
2266 coroapi.current = coro_current; 2441 coroapi.current = coro_current;
2267 2442
2268 GCoroAPI = &coroapi; 2443 GCoroAPI = &coroapi;
2269 sv_setiv (sv, (IV)&coroapi); 2444 sv_setiv (sv, (IV)&coroapi);
2270 SvREADONLY_on (sv); 2445 SvREADONLY_on (sv);
2271 } 2446 }
2272} 2447}
2448
2449void
2450schedule (...)
2451 CODE:
2452 api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0);
2453
2454void
2455cede (...)
2456 CODE:
2457 api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0);
2458
2459void
2460cede_notself (...)
2461 CODE:
2462 api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0);
2273 2463
2274void 2464void
2275_set_current (SV *current) 2465_set_current (SV *current)
2276 PROTOTYPE: $ 2466 PROTOTYPE: $
2277 CODE: 2467 CODE:
2280 2470
2281void 2471void
2282_set_readyhook (SV *hook) 2472_set_readyhook (SV *hook)
2283 PROTOTYPE: $ 2473 PROTOTYPE: $
2284 CODE: 2474 CODE:
2285 LOCK;
2286 SvREFCNT_dec (coro_readyhook); 2475 SvREFCNT_dec (coro_readyhook);
2287 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 2476 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2288 UNLOCK;
2289 2477
2290int 2478int
2291prio (Coro::State coro, int newprio = 0) 2479prio (Coro::State coro, int newprio = 0)
2480 PROTOTYPE: $;$
2292 ALIAS: 2481 ALIAS:
2293 nice = 1 2482 nice = 1
2294 CODE: 2483 CODE:
2295{ 2484{
2296 RETVAL = coro->prio; 2485 RETVAL = coro->prio;
2311 2500
2312SV * 2501SV *
2313ready (SV *self) 2502ready (SV *self)
2314 PROTOTYPE: $ 2503 PROTOTYPE: $
2315 CODE: 2504 CODE:
2316 RETVAL = boolSV (api_ready (self)); 2505 RETVAL = boolSV (api_ready (aTHX_ self));
2317 OUTPUT: 2506 OUTPUT:
2318 RETVAL 2507 RETVAL
2319 2508
2320int 2509int
2321nready (...) 2510nready (...)
2328# for async_pool speedup 2517# for async_pool speedup
2329void 2518void
2330_pool_1 (SV *cb) 2519_pool_1 (SV *cb)
2331 CODE: 2520 CODE:
2332{ 2521{
2333 struct coro *coro = SvSTATE (coro_current);
2334 HV *hv = (HV *)SvRV (coro_current); 2522 HV *hv = (HV *)SvRV (coro_current);
2523 struct coro *coro = SvSTATE_hv ((SV *)hv);
2335 AV *defav = GvAV (PL_defgv); 2524 AV *defav = GvAV (PL_defgv);
2336 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 2525 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2337 AV *invoke_av; 2526 AV *invoke_av;
2338 int i, len; 2527 int i, len;
2339 2528
2360 { 2549 {
2361 av_fill (defav, len - 1); 2550 av_fill (defav, len - 1);
2362 for (i = 0; i < len; ++i) 2551 for (i = 0; i < len; ++i)
2363 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 2552 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2364 } 2553 }
2365
2366 SvREFCNT_dec (invoke);
2367} 2554}
2368 2555
2369void 2556void
2370_pool_2 (SV *cb) 2557_pool_2 (SV *cb)
2371 CODE: 2558 CODE:
2372{ 2559{
2373 struct coro *coro = SvSTATE (coro_current); 2560 struct coro *coro = SvSTATE_current;
2374 2561
2375 sv_setsv (cb, &PL_sv_undef); 2562 sv_setsv (cb, &PL_sv_undef);
2376 2563
2377 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2564 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2378 coro->saved_deffh = 0; 2565 coro->saved_deffh = 0;
2391 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 2578 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2392 2579
2393 coro->prio = 0; 2580 coro->prio = 0;
2394 2581
2395 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2582 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2396 api_trace (coro_current, 0); 2583 api_trace (aTHX_ coro_current, 0);
2397 2584
2398 av_push (av_async_pool, newSVsv (coro_current)); 2585 av_push (av_async_pool, newSVsv (coro_current));
2399} 2586}
2400
2401#if 0
2402
2403void
2404_generator_call (...)
2405 PROTOTYPE: @
2406 PPCODE:
2407 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2408 xxxx
2409 abort ();
2410
2411SV *
2412gensub (SV *sub, ...)
2413 PROTOTYPE: &;@
2414 CODE:
2415{
2416 struct coro *coro;
2417 MAGIC *mg;
2418 CV *xcv;
2419 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2420 int i;
2421
2422 CvGV (ncv) = CvGV (cv);
2423 CvFILE (ncv) = CvFILE (cv);
2424
2425 Newz (0, coro, 1, struct coro);
2426 coro->args = newAV ();
2427 coro->flags = CF_NEW;
2428
2429 av_extend (coro->args, items - 1);
2430 for (i = 1; i < items; i++)
2431 av_push (coro->args, newSVsv (ST (i)));
2432
2433 CvISXSUB_on (ncv);
2434 CvXSUBANY (ncv).any_ptr = (void *)coro;
2435
2436 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2437
2438 CvXSUB (ncv) = CvXSUB (xcv);
2439 CvANON_on (ncv);
2440
2441 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2442 RETVAL = newRV_noinc ((SV *)ncv);
2443}
2444 OUTPUT:
2445 RETVAL
2446
2447#endif
2448 2587
2449 2588
2450MODULE = Coro::State PACKAGE = Coro::AIO 2589MODULE = Coro::State PACKAGE = Coro::AIO
2451 2590
2452void 2591void
2453_get_state (SV *self) 2592_get_state (SV *self)
2593 PROTOTYPE: $
2454 PPCODE: 2594 PPCODE:
2455{ 2595{
2456 AV *defav = GvAV (PL_defgv); 2596 AV *defav = GvAV (PL_defgv);
2457 AV *av = newAV (); 2597 AV *av = newAV ();
2458 int i; 2598 int i;
2473 2613
2474 av_push (av, data_sv); 2614 av_push (av, data_sv);
2475 2615
2476 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 2616 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2477 2617
2478 api_ready (self); 2618 api_ready (aTHX_ self);
2479} 2619}
2480 2620
2481void 2621void
2482_set_state (SV *state) 2622_set_state (SV *state)
2483 PROTOTYPE: $ 2623 PROTOTYPE: $
2501MODULE = Coro::State PACKAGE = Coro::AnyEvent 2641MODULE = Coro::State PACKAGE = Coro::AnyEvent
2502 2642
2503BOOT: 2643BOOT:
2504 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); 2644 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2505 2645
2506SV * 2646void
2507_schedule (...) 2647_schedule (...)
2508 PROTOTYPE: @
2509 CODE: 2648 CODE:
2510{ 2649{
2511 static int incede; 2650 static int incede;
2512 2651
2513 api_cede_notself (); 2652 api_cede_notself (aTHX);
2514 2653
2515 ++incede; 2654 ++incede;
2516 while (coro_nready >= incede && api_cede ()) 2655 while (coro_nready >= incede && api_cede (aTHX))
2517 ; 2656 ;
2518 2657
2519 sv_setsv (sv_activity, &PL_sv_undef); 2658 sv_setsv (sv_activity, &PL_sv_undef);
2520 if (coro_nready >= incede) 2659 if (coro_nready >= incede)
2521 { 2660 {
2532MODULE = Coro::State PACKAGE = PerlIO::cede 2671MODULE = Coro::State PACKAGE = PerlIO::cede
2533 2672
2534BOOT: 2673BOOT:
2535 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2674 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2536 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 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2703
2704void
2705down (SV *self)
2706 CODE:
2707 api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1);
2708
2709void
2710try (SV *self)
2711 PPCODE:
2712{
2713 AV *av = (AV *)SvRV (self);
2714 SV *count_sv = AvARRAY (av)[0];
2715 IV count = SvIVX (count_sv);
2716
2717 if (count > 0)
2718 {
2719 --count;
2720 SvIVX (count_sv) = count;
2721 XSRETURN_YES;
2722 }
2723 else
2724 XSRETURN_NO;
2725}
2726
2727void
2728waiters (SV *self)
2729 CODE:
2730{
2731 AV *av = (AV *)SvRV (self);
2732
2733 if (GIMME_V == G_SCALAR)
2734 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0])));
2735 else
2736 {
2737 int i;
2738 EXTEND (SP, AvFILLp (av) + 1 - 1);
2739 for (i = 1; i <= AvFILLp (av); ++i)
2740 PUSHs (newSVsv (AvARRAY (av)[i]));
2741 }
2742}
2743

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines