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.274 by root, Sat Nov 15 00:08:30 2008 UTC vs.
Revision 1.286 by root, Mon Nov 17 04:19:49 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;
181static struct CoroAPI coroapi; 166static struct CoroAPI coroapi;
182static AV *main_mainstack; /* used to differentiate between $main and others */ 167static AV *main_mainstack; /* used to differentiate between $main and others */
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;
187 171
188static GV *irsgv; /* $/ */ 172static GV *irsgv; /* $/ */
189static GV *stdoutgv; /* *STDOUT */ 173static GV *stdoutgv; /* *STDOUT */
190static SV *rv_diehook; 174static SV *rv_diehook;
191static SV *rv_warnhook; 175static SV *rv_warnhook;
257/* this is a structure representing a perl-level coroutine */ 241/* this is a structure representing a perl-level coroutine */
258struct coro { 242struct coro {
259 /* the C coroutine allocated to this perl coroutine, if any */ 243 /* the C coroutine allocated to this perl coroutine, if any */
260 coro_cctx *cctx; 244 coro_cctx *cctx;
261 245
262 /* process data */ 246 /* state data */
263 struct CoroSLF slf_frame; /* saved slf frame */ 247 struct CoroSLF slf_frame; /* saved slf frame */
264 AV *mainstack; 248 AV *mainstack;
265 perl_slots *slot; /* basically the saved sp */ 249 perl_slots *slot; /* basically the saved sp */
266 250
267 AV *args; /* data associated with this coroutine (initial args) */ 251 AV *args; /* data associated with this coroutine (initial args) */
268 int refcnt; /* coroutines are refcounted, yes */ 252 int refcnt; /* coroutines are refcounted, yes */
269 int flags; /* CF_ flags */ 253 int flags; /* CF_ flags */
270 HV *hv; /* the perl hash associated with this coro, if any */ 254 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro);
271 256
272 /* statistics */ 257 /* statistics */
273 int usecount; /* number of transfers to this coro */ 258 int usecount; /* number of transfers to this coro */
274 259
275 /* coro process data */ 260 /* coro process data */
284}; 269};
285 270
286typedef struct coro *Coro__State; 271typedef struct coro *Coro__State;
287typedef struct coro *Coro__State_or_hashref; 272typedef struct coro *Coro__State_or_hashref;
288 273
274/* the following variables are effectively part of the perl context */
275/* and get copied between struct coro and these variables */
276/* the mainr easonw e don't support windows process emulation */
289static struct CoroSLF slf_frame; /* the current slf frame */ 277static struct CoroSLF slf_frame; /* the current slf frame */
278static SV *coro_throw;
290 279
291/** Coro ********************************************************************/ 280/** Coro ********************************************************************/
292 281
293#define PRIO_MAX 3 282#define PRIO_MAX 3
294#define PRIO_HIGH 1 283#define PRIO_HIGH 1
403static MGVTBL coro_cv_vtbl = { 392static MGVTBL coro_cv_vtbl = {
404 0, 0, 0, 0, 393 0, 0, 0, 0,
405 coro_cv_free 394 coro_cv_free
406}; 395};
407 396
408#define CORO_MAGIC(sv, type) \ 397#define CORO_MAGIC(sv, type) \
409 SvMAGIC (sv) \ 398 expect_true (SvMAGIC (sv)) \
410 ? SvMAGIC (sv)->mg_type == type \ 399 ? expect_true (SvMAGIC (sv)->mg_type == type) \
411 ? SvMAGIC (sv) \ 400 ? SvMAGIC (sv) \
412 : mg_find (sv, type) \ 401 : mg_find (sv, type) \
413 : 0 402 : 0
414 403
415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 404#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) 405#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
417 406
438 mg = CORO_MAGIC_state (coro); 427 mg = CORO_MAGIC_state (coro);
439 return (struct coro *)mg->mg_ptr; 428 return (struct coro *)mg->mg_ptr;
440} 429}
441 430
442#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 431#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
432
433/* fastert than SvSTATE, but expects a coroutine hv */
434INLINE struct coro *
435SvSTATE_hv (SV *sv)
436{
437 MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state)
438 ? SvMAGIC (sv)
439 : mg_find (sv, CORO_MAGIC_type_state);
440
441 return (struct coro *)mg->mg_ptr;
442}
443
444#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
443 445
444/* the next two functions merely cache the padlists */ 446/* the next two functions merely cache the padlists */
445static void 447static void
446get_padlist (pTHX_ CV *cv) 448get_padlist (pTHX_ CV *cv)
447{ 449{
515 } 517 }
516 518
517 PUTBACK; 519 PUTBACK;
518 } 520 }
519 521
520 slf_frame = c->slf_frame; 522 slf_frame = c->slf_frame;
523 coro_throw = c->throw;
521} 524}
522 525
523static void 526static void
524save_perl (pTHX_ Coro__State c) 527save_perl (pTHX_ Coro__State c)
525{ 528{
529 c->throw = coro_throw;
526 c->slf_frame = slf_frame; 530 c->slf_frame = slf_frame;
527 531
528 { 532 {
529 dSP; 533 dSP;
530 I32 cxix = cxstack_ix; 534 I32 cxix = cxstack_ix;
811 815
812 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 816 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
813} 817}
814 818
815static void 819static void
816prepare_nop (aTHX_ struct coro_transfer_args *ta) 820prepare_nop (pTHX_ struct coro_transfer_args *ta)
817{ 821{
818 /* kind of mega-hacky, but works */ 822 /* kind of mega-hacky, but works */
819 ta->next = ta->prev = (struct coro *)ta; 823 ta->next = ta->prev = (struct coro *)ta;
820} 824}
821 825
822static int 826static int
823slf_check_nop (aTHX) 827slf_check_nop (pTHX_ struct CoroSLF *frame)
824{ 828{
825 return 0; 829 return 0;
826} 830}
827 831
828static void 832static void NOINLINE /* noinline to keep it out of the transfer fast path */
829coro_setup (pTHX_ struct coro *coro) 833coro_setup (pTHX_ struct coro *coro)
830{ 834{
831 /* 835 /*
832 * emulate part of the perl startup here. 836 * emulate part of the perl startup here.
833 */ 837 */
876 /* this newly created coroutine might be run on an existing cctx which most 880 /* this newly created coroutine might be run on an existing cctx which most
877 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 881 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
878 */ 882 */
879 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 883 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 */ 884 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
885
886 coro_throw = coro->throw;
881} 887}
882 888
883static void 889static void
884coro_destruct (pTHX_ struct coro *coro) 890coro_destruct (pTHX_ struct coro *coro)
885{ 891{
909 915
910 SvREFCNT_dec (PL_diehook); 916 SvREFCNT_dec (PL_diehook);
911 SvREFCNT_dec (PL_warnhook); 917 SvREFCNT_dec (PL_warnhook);
912 918
913 SvREFCNT_dec (coro->saved_deffh); 919 SvREFCNT_dec (coro->saved_deffh);
914 SvREFCNT_dec (coro->throw); 920 SvREFCNT_dec (coro_throw);
915 921
916 coro_destruct_stacks (aTHX); 922 coro_destruct_stacks (aTHX);
917} 923}
918 924
919INLINE void 925INLINE void
929static int 935static int
930runops_trace (pTHX) 936runops_trace (pTHX)
931{ 937{
932 COP *oldcop = 0; 938 COP *oldcop = 0;
933 int oldcxix = -2; 939 int oldcxix = -2;
934 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ 940 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
935 coro_cctx *cctx = coro->cctx; 941 coro_cctx *cctx = coro->cctx;
936 942
937 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 943 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
938 { 944 {
939 PERL_ASYNC_CHECK (); 945 PERL_ASYNC_CHECK ();
1088 1094
1089/* the tail of transfer: execute stuff we can only do after a transfer */ 1095/* the tail of transfer: execute stuff we can only do after a transfer */
1090INLINE void 1096INLINE void
1091transfer_tail (pTHX) 1097transfer_tail (pTHX)
1092{ 1098{
1093 struct coro *next = (struct coro *)transfer_next;
1094 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1095 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1096
1097 free_coro_mortal (aTHX); 1099 free_coro_mortal (aTHX);
1098 UNLOCK;
1099
1100 if (expect_false (next->throw))
1101 {
1102 SV *exception = sv_2mortal (next->throw);
1103
1104 next->throw = 0;
1105 sv_setsv (ERRSV, exception);
1106 croak (0);
1107 }
1108} 1100}
1109 1101
1110/* 1102/*
1111 * this is a _very_ stripped down perl interpreter ;) 1103 * this is a _very_ stripped down perl interpreter ;)
1112 */ 1104 */
1127 1119
1128 /* inject a fake subroutine call to cctx_init */ 1120 /* inject a fake subroutine call to cctx_init */
1129 cctx_prepare (aTHX_ (coro_cctx *)arg); 1121 cctx_prepare (aTHX_ (coro_cctx *)arg);
1130 1122
1131 /* cctx_run is the alternative tail of transfer() */ 1123 /* cctx_run is the alternative tail of transfer() */
1132 /* TODO: throwing an exception here might be deadly, VERIFY */
1133 transfer_tail (aTHX); 1124 transfer_tail (aTHX);
1134 1125
1135 /* somebody or something will hit me for both perl_run and PL_restartop */ 1126 /* somebody or something will hit me for both perl_run and PL_restartop */
1136 PL_restartop = PL_op; 1127 PL_restartop = PL_op;
1137 perl_run (PL_curinterp); 1128 perl_run (PL_curinterp);
1293/** coroutine switching *****************************************************/ 1284/** coroutine switching *****************************************************/
1294 1285
1295static void 1286static void
1296transfer_check (pTHX_ struct coro *prev, struct coro *next) 1287transfer_check (pTHX_ struct coro *prev, struct coro *next)
1297{ 1288{
1289 /* TODO: throwing up here is considered harmful */
1290
1298 if (expect_true (prev != next)) 1291 if (expect_true (prev != next))
1299 { 1292 {
1300 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1293 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1301 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1294 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,");
1302 1295
1312#endif 1305#endif
1313 } 1306 }
1314} 1307}
1315 1308
1316/* always use the TRANSFER macro */ 1309/* always use the TRANSFER macro */
1317static void NOINLINE 1310static void NOINLINE /* noinline so we have a fixed stackframe */
1318transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1311transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1319{ 1312{
1320 dSTACKLEVEL; 1313 dSTACKLEVEL;
1321 1314
1322 /* sometimes transfer is only called to set idle_sp */ 1315 /* sometimes transfer is only called to set idle_sp */
1323 if (expect_false (!next)) 1316 if (expect_false (!next))
1324 { 1317 {
1325 ((coro_cctx *)prev)->idle_sp = stacklevel; 1318 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel;
1326 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1319 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1327 } 1320 }
1328 else if (expect_true (prev != next)) 1321 else if (expect_true (prev != next))
1329 { 1322 {
1330 coro_cctx *prev__cctx; 1323 coro_cctx *prev__cctx;
1337 prev->flags |= CF_RUNNING; 1330 prev->flags |= CF_RUNNING;
1338 } 1331 }
1339 1332
1340 prev->flags &= ~CF_RUNNING; 1333 prev->flags &= ~CF_RUNNING;
1341 next->flags |= CF_RUNNING; 1334 next->flags |= CF_RUNNING;
1342
1343 LOCK;
1344 1335
1345 /* first get rid of the old state */ 1336 /* first get rid of the old state */
1346 save_perl (aTHX_ prev); 1337 save_perl (aTHX_ prev);
1347 1338
1348 if (expect_false (next->flags & CF_NEW)) 1339 if (expect_false (next->flags & CF_NEW))
1357 1348
1358 prev__cctx = prev->cctx; 1349 prev__cctx = prev->cctx;
1359 1350
1360 /* possibly untie and reuse the cctx */ 1351 /* possibly untie and reuse the cctx */
1361 if (expect_true ( 1352 if (expect_true (
1362 prev__cctx->idle_sp == stacklevel 1353 prev__cctx->idle_sp == (void *)stacklevel
1363 && !(prev__cctx->flags & CC_TRACE) 1354 && !(prev__cctx->flags & CC_TRACE)
1364 && !force_cctx 1355 && !force_cctx
1365 )) 1356 ))
1366 { 1357 {
1367 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1358 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1381 ++next->usecount; 1372 ++next->usecount;
1382 1373
1383 if (expect_true (!next->cctx)) 1374 if (expect_true (!next->cctx))
1384 next->cctx = cctx_get (aTHX); 1375 next->cctx = cctx_get (aTHX);
1385 1376
1386 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1387 transfer_next = next;
1388
1389 if (expect_false (prev__cctx != next->cctx)) 1377 if (expect_false (prev__cctx != next->cctx))
1390 { 1378 {
1391 prev__cctx->top_env = PL_top_env; 1379 prev__cctx->top_env = PL_top_env;
1392 PL_top_env = next->cctx->top_env; 1380 PL_top_env = next->cctx->top_env;
1393 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1381 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1406coro_state_destroy (pTHX_ struct coro *coro) 1394coro_state_destroy (pTHX_ struct coro *coro)
1407{ 1395{
1408 if (coro->flags & CF_DESTROYED) 1396 if (coro->flags & CF_DESTROYED)
1409 return 0; 1397 return 0;
1410 1398
1399 if (coro->on_destroy)
1400 coro->on_destroy (aTHX_ coro);
1401
1411 coro->flags |= CF_DESTROYED; 1402 coro->flags |= CF_DESTROYED;
1412 1403
1413 if (coro->flags & CF_READY) 1404 if (coro->flags & CF_READY)
1414 { 1405 {
1415 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1406 /* reduce nready, as destroying a ready coro effectively unreadies it */
1416 /* alternative: look through all ready queues and remove the coro */ 1407 /* alternative: look through all ready queues and remove the coro */
1417 LOCK;
1418 --coro_nready; 1408 --coro_nready;
1419 UNLOCK;
1420 } 1409 }
1421 else 1410 else
1422 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1411 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1423 1412
1424 if (coro->mainstack && coro->mainstack != main_mainstack) 1413 if (coro->mainstack && coro->mainstack != main_mainstack)
1502 TRANSFER (ta, 1); 1491 TRANSFER (ta, 1);
1503} 1492}
1504 1493
1505/** Coro ********************************************************************/ 1494/** Coro ********************************************************************/
1506 1495
1507static void 1496INLINE void
1508coro_enq (pTHX_ SV *coro_sv) 1497coro_enq (pTHX_ struct coro *coro)
1509{ 1498{
1510 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1499 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv));
1511} 1500}
1512 1501
1513static SV * 1502INLINE SV *
1514coro_deq (pTHX) 1503coro_deq (pTHX)
1515{ 1504{
1516 int prio; 1505 int prio;
1517 1506
1518 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1507 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1537 if (coro->flags & CF_READY) 1526 if (coro->flags & CF_READY)
1538 return 0; 1527 return 0;
1539 1528
1540 coro->flags |= CF_READY; 1529 coro->flags |= CF_READY;
1541 1530
1542 LOCK;
1543
1544 sv_hook = coro_nready ? 0 : coro_readyhook; 1531 sv_hook = coro_nready ? 0 : coro_readyhook;
1545 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1532 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1546 1533
1547 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); 1534 coro_enq (aTHX_ coro);
1548 ++coro_nready; 1535 ++coro_nready;
1549 1536
1550 UNLOCK;
1551
1552 if (sv_hook) 1537 if (sv_hook)
1553 { 1538 {
1554 dSP; 1539 dSP;
1555 1540
1556 ENTER; 1541 ENTER;
1582{ 1567{
1583 SV *prev_sv, *next_sv; 1568 SV *prev_sv, *next_sv;
1584 1569
1585 for (;;) 1570 for (;;)
1586 { 1571 {
1587 LOCK;
1588 next_sv = coro_deq (aTHX); 1572 next_sv = coro_deq (aTHX);
1589 1573
1590 /* nothing to schedule: call the idle handler */ 1574 /* nothing to schedule: call the idle handler */
1591 if (expect_false (!next_sv)) 1575 if (expect_false (!next_sv))
1592 { 1576 {
1593 dSP; 1577 dSP;
1594 UNLOCK;
1595 1578
1596 ENTER; 1579 ENTER;
1597 SAVETMPS; 1580 SAVETMPS;
1598 1581
1599 PUSHMARK (SP); 1582 PUSHMARK (SP);
1604 FREETMPS; 1587 FREETMPS;
1605 LEAVE; 1588 LEAVE;
1606 continue; 1589 continue;
1607 } 1590 }
1608 1591
1609 ta->next = SvSTATE (next_sv); 1592 ta->next = SvSTATE_hv (next_sv);
1610 1593
1611 /* cannot transfer to destroyed coros, skip and look for next */ 1594 /* cannot transfer to destroyed coros, skip and look for next */
1612 if (expect_false (ta->next->flags & CF_DESTROYED)) 1595 if (expect_false (ta->next->flags & CF_DESTROYED))
1613 { 1596 {
1614 UNLOCK;
1615 SvREFCNT_dec (next_sv); 1597 SvREFCNT_dec (next_sv);
1616 /* coro_nready has already been taken care of by destroy */ 1598 /* coro_nready has already been taken care of by destroy */
1617 continue; 1599 continue;
1618 } 1600 }
1619 1601
1620 --coro_nready; 1602 --coro_nready;
1621 UNLOCK;
1622 break; 1603 break;
1623 } 1604 }
1624 1605
1625 /* free this only after the transfer */ 1606 /* free this only after the transfer */
1626 prev_sv = SvRV (coro_current); 1607 prev_sv = SvRV (coro_current);
1627 ta->prev = SvSTATE (prev_sv); 1608 ta->prev = SvSTATE_hv (prev_sv);
1628 TRANSFER_CHECK (*ta); 1609 TRANSFER_CHECK (*ta);
1629 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1610 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1630 ta->next->flags &= ~CF_READY; 1611 ta->next->flags &= ~CF_READY;
1631 SvRV_set (coro_current, next_sv); 1612 SvRV_set (coro_current, next_sv);
1632 1613
1633 LOCK;
1634 free_coro_mortal (aTHX); 1614 free_coro_mortal (aTHX);
1635 coro_mortal = prev_sv; 1615 coro_mortal = prev_sv;
1636 UNLOCK;
1637} 1616}
1638 1617
1639INLINE void 1618INLINE void
1640prepare_cede (pTHX_ struct coro_transfer_args *ta) 1619prepare_cede (pTHX_ struct coro_transfer_args *ta)
1641{ 1620{
1719 PL_runops = RUNOPS_DEFAULT; 1698 PL_runops = RUNOPS_DEFAULT;
1720 else 1699 else
1721 coro->slot->runops = RUNOPS_DEFAULT; 1700 coro->slot->runops = RUNOPS_DEFAULT;
1722 } 1701 }
1723} 1702}
1724
1725#if 0
1726static int
1727coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1728{
1729 AV *padlist;
1730 AV *av = (AV *)mg->mg_obj;
1731
1732 abort ();
1733
1734 return 0;
1735}
1736
1737static MGVTBL coro_gensub_vtbl = {
1738 0, 0, 0, 0,
1739 coro_gensub_free
1740};
1741#endif
1742 1703
1743/*****************************************************************************/ 1704/*****************************************************************************/
1744/* PerlIO::cede */ 1705/* PerlIO::cede */
1745 1706
1746typedef struct 1707typedef struct
1815 PerlIOBuf_set_ptrcnt, 1776 PerlIOBuf_set_ptrcnt,
1816}; 1777};
1817 1778
1818/*****************************************************************************/ 1779/*****************************************************************************/
1819 1780
1820static const CV *slf_cv; /* for quick consistency check */
1821
1822static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ 1781static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1782static const CV *slf_cv;
1823static SV *slf_arg0; 1783static SV **slf_argv;
1824static SV *slf_arg1; 1784static int slf_argc, slf_arga; /* count, allocated */
1825static SV *slf_arg2; 1785static I32 slf_ax; /* top of stack, for restore */
1826 1786
1827/* this restores the stack in the case we patched the entersub, to */ 1787/* this restores the stack in the case we patched the entersub, to */
1828/* recreate the stack frame as perl will on following calls */ 1788/* recreate the stack frame as perl will on following calls */
1829/* since entersub cleared the stack */ 1789/* since entersub cleared the stack */
1830static OP * 1790static OP *
1831pp_restore (pTHX) 1791pp_restore (pTHX)
1832{ 1792{
1833 dSP; 1793 int i;
1794 SV **SP = PL_stack_base + slf_ax;
1834 1795
1835 PUSHMARK (SP); 1796 PUSHMARK (SP);
1836 1797
1837 EXTEND (SP, 3); 1798 EXTEND (SP, slf_argc + 1);
1799
1800 for (i = 0; i < slf_argc; ++i)
1838 if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); 1801 PUSHs (sv_2mortal (slf_argv [i]));
1839 if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); 1802
1840 if (slf_arg2) PUSHs (sv_2mortal (slf_arg2));
1841 PUSHs ((SV *)CvGV (slf_cv)); 1803 PUSHs ((SV *)CvGV (slf_cv));
1842 1804
1843 RETURNOP (slf_restore.op_first); 1805 RETURNOP (slf_restore.op_first);
1844} 1806}
1845 1807
1848{ 1810{
1849 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); 1811 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1850} 1812}
1851 1813
1852static void 1814static void
1853slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items) 1815slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1854{ 1816{
1855 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); 1817 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1856 1818
1857 frame->prepare = slf_prepare_set_stacklevel; 1819 frame->prepare = slf_prepare_set_stacklevel;
1858 frame->check = slf_check_nop; 1820 frame->check = slf_check_nop;
1862static void 1824static void
1863slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 1825slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1864{ 1826{
1865 SV **arg = (SV **)slf_frame.data; 1827 SV **arg = (SV **)slf_frame.data;
1866 1828
1867 prepare_transfer (ta, arg [0], arg [1]); 1829 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1868} 1830}
1869 1831
1870static void 1832static void
1871slf_init_transfer (pTHX_ struct CoroSLF *frame, SV **arg, int items) 1833slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1872{ 1834{
1873 if (items != 2) 1835 if (items != 2)
1874 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); 1836 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items);
1875 1837
1876 frame->prepare = slf_prepare_transfer; 1838 frame->prepare = slf_prepare_transfer;
1877 frame->check = slf_check_nop; 1839 frame->check = slf_check_nop;
1878 frame->data = (void *)arg; /* let's hope it will stay valid */ 1840 frame->data = (void *)arg; /* let's hope it will stay valid */
1879} 1841}
1880 1842
1881static void 1843static void
1882slf_init_schedule (pTHX_ struct CoroSLF *frame, SV **arg, int items) 1844slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1883{ 1845{
1884 frame->prepare = prepare_schedule; 1846 frame->prepare = prepare_schedule;
1885 frame->check = slf_check_nop; 1847 frame->check = slf_check_nop;
1886} 1848}
1887 1849
1888static void 1850static void
1889slf_init_cede (pTHX_ struct CoroSLF *frame, SV **arg, int items) 1851slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1890{ 1852{
1891 frame->prepare = prepare_cede; 1853 frame->prepare = prepare_cede;
1892 frame->check = slf_check_nop; 1854 frame->check = slf_check_nop;
1893} 1855}
1894 1856
1895static void 1857static void
1896slf_init_cede_notself (pTHX_ struct CoroSLF *frame, SV **arg, int items) 1858slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1897{ 1859{
1898 frame->prepare = prepare_cede_notself; 1860 frame->prepare = prepare_cede_notself;
1899 frame->check = slf_check_nop; 1861 frame->check = slf_check_nop;
1900} 1862}
1901 1863
1927 /* do a quick consistency check on the "function" object, and if it isn't */ 1889 /* do a quick consistency check on the "function" object, and if it isn't */
1928 /* for us, divert to the real entersub */ 1890 /* for us, divert to the real entersub */
1929 if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) 1891 if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF))
1930 return PL_ppaddr[OP_ENTERSUB](aTHX); 1892 return PL_ppaddr[OP_ENTERSUB](aTHX);
1931 1893
1932 /* pop args */
1933 SP = PL_stack_base + POPMARK;
1934
1935 if (!(PL_op->op_flags & OPf_STACKED)) 1894 if (!(PL_op->op_flags & OPf_STACKED))
1936 { 1895 {
1937 /* ampersand-form of call, use @_ instead of stack */ 1896 /* ampersand-form of call, use @_ instead of stack */
1938 AV *av = GvAV (PL_defgv); 1897 AV *av = GvAV (PL_defgv);
1939 arg = AvARRAY (av); 1898 arg = AvARRAY (av);
1940 items = AvFILLp (av) + 1; 1899 items = AvFILLp (av) + 1;
1941 } 1900 }
1942 1901
1902 /* now call the init function, which needs to set up slf_frame */
1903 ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr)
1904 (aTHX_ &slf_frame, GvCV (gv), arg, items);
1905
1906 /* pop args */
1907 SP = PL_stack_base + POPMARK;
1908
1943 PUTBACK; 1909 PUTBACK;
1944
1945 ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items);
1946 } 1910 }
1947 1911
1948 /* now interpret the slf_frame */ 1912 /* now that we have a slf_frame, interpret it! */
1949 /* we use a callback system not to make the code needlessly */ 1913 /* we use a callback system not to make the code needlessly */
1950 /* complicated, but so we can run multiple perl coros from one cctx */ 1914 /* complicated, but so we can run multiple perl coros from one cctx */
1951 1915
1952 do 1916 do
1953 { 1917 {
1958 1922
1959 checkmark = PL_stack_sp - PL_stack_base; 1923 checkmark = PL_stack_sp - PL_stack_base;
1960 } 1924 }
1961 while (slf_frame.check (aTHX_ &slf_frame)); 1925 while (slf_frame.check (aTHX_ &slf_frame));
1962 1926
1927 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1928
1929 /* return value handling - mostly like entersub */
1963 { 1930 {
1964 dSP; 1931 dSP;
1965 SV **bot = PL_stack_base + checkmark; 1932 SV **bot = PL_stack_base + checkmark;
1966 int gimme = GIMME_V; 1933 int gimme = GIMME_V;
1967
1968 slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */
1969 1934
1970 /* make sure we put something on the stack in scalar context */ 1935 /* make sure we put something on the stack in scalar context */
1971 if (gimme == G_SCALAR) 1936 if (gimme == G_SCALAR)
1972 { 1937 {
1973 if (sp == bot) 1938 if (sp == bot)
1977 } 1942 }
1978 1943
1979 PUTBACK; 1944 PUTBACK;
1980 } 1945 }
1981 1946
1947 /* exception handling */
1948 if (expect_false (coro_throw))
1949 {
1950 SV *exception = sv_2mortal (coro_throw);
1951
1952 coro_throw = 0;
1953 sv_setsv (ERRSV, exception);
1954 croak (0);
1955 }
1956
1982 return NORMAL; 1957 return NORMAL;
1983} 1958}
1984 1959
1985static void 1960static void
1986api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) 1961api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax)
1987{ 1962{
1963 int i;
1964 SV **arg = PL_stack_base + ax;
1965 int items = PL_stack_sp - arg + 1;
1966
1988 assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); 1967 assert (("FATAL: SLF call with illegal CV value", !CvANON (cv)));
1989 1968
1990 if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] 1969 if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB]
1991 && PL_op->op_ppaddr != pp_slf) 1970 && 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"); 1971 croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught");
1993 1972
1994 if (items > 3)
1995 croak ("Coro only supports up to three arguments to SLF functions currently, caught");
1996
1997 CvFLAGS (cv) |= CVf_SLF; 1973 CvFLAGS (cv) |= CVf_SLF;
1998 CvXSUBANY (cv).any_ptr = (void *)init_cb; 1974 CvXSUBANY (cv).any_ptr = (void *)init_cb;
1999 slf_cv = cv; 1975 slf_cv = cv;
2000 1976
2001 /* we patch the op, and then re-run the whole call */ 1977 /* 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 */ 1978 /* we have to put the same argument on the stack for this to work */
2003 /* and this will be done by pp_restore */ 1979 /* and this will be done by pp_restore */
2004 slf_restore.op_next = (OP *)&slf_restore; 1980 slf_restore.op_next = (OP *)&slf_restore;
2005 slf_restore.op_type = OP_NULL; 1981 slf_restore.op_type = OP_CUSTOM;
2006 slf_restore.op_ppaddr = pp_restore; 1982 slf_restore.op_ppaddr = pp_restore;
2007 slf_restore.op_first = PL_op; 1983 slf_restore.op_first = PL_op;
2008 1984
2009 slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; 1985 slf_ax = ax - 1; /* undo the ax++ inside dAXMARK */
2010 slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; 1986
2011 slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; 1987 if (PL_op->op_flags & OPf_STACKED)
1988 {
1989 if (items > slf_arga)
1990 {
1991 slf_arga = items;
1992 free (slf_argv);
1993 slf_argv = malloc (slf_arga * sizeof (SV *));
1994 }
1995
1996 slf_argc = items;
1997
1998 for (i = 0; i < items; ++i)
1999 slf_argv [i] = SvREFCNT_inc (arg [i]);
2000 }
2001 else
2002 slf_argc = 0;
2012 2003
2013 PL_op->op_ppaddr = pp_slf; 2004 PL_op->op_ppaddr = pp_slf;
2005 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */
2014 2006
2015 PL_op = (OP *)&slf_restore; 2007 PL_op = (OP *)&slf_restore;
2016} 2008}
2017 2009
2010/*****************************************************************************/
2011
2012static void
2013coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2014{
2015 SV *count_sv = AvARRAY (av)[0];
2016 IV count = SvIVX (count_sv);
2017
2018 count += adjust;
2019 SvIVX (count_sv) = count;
2020
2021 /* now wake up as many waiters as are expected to lock */
2022 while (count > 0 && AvFILLp (av) > 0)
2023 {
2024 SV *cb;
2025
2026 /* swap first two elements so we can shift a waiter */
2027 AvARRAY (av)[0] = AvARRAY (av)[1];
2028 AvARRAY (av)[1] = count_sv;
2029 cb = av_shift (av);
2030
2031 if (SvOBJECT (cb))
2032 api_ready (aTHX_ cb);
2033 else
2034 croak ("callbacks not yet supported");
2035
2036 SvREFCNT_dec (cb);
2037
2038 --count;
2039 }
2040}
2041
2042static void
2043coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2044{
2045 /* call $sem->adjust (0) to possibly wake up some other waiters */
2046 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2047}
2048
2049static int
2050slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2051{
2052 AV *av = (AV *)frame->data;
2053 SV *count_sv = AvARRAY (av)[0];
2054
2055 if (SvIVX (count_sv) > 0)
2056 {
2057 SvSTATE_current->on_destroy = 0;
2058 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2059 return 0;
2060 }
2061 else
2062 {
2063 int i;
2064 /* if we were woken up but can't down, we look through the whole */
2065 /* waiters list and only add us if we aren't in there already */
2066 /* this avoids some degenerate memory usage cases */
2067
2068 for (i = 1; i <= AvFILLp (av); ++i)
2069 if (AvARRAY (av)[i] == SvRV (coro_current))
2070 return 1;
2071
2072 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2073 return 1;
2074 }
2075}
2076
2077static void
2078slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2079{
2080 AV *av = (AV *)SvRV (arg [0]);
2081
2082 if (SvIVX (AvARRAY (av)[0]) > 0)
2083 {
2084 frame->data = (void *)av;
2085 frame->prepare = prepare_nop;
2086 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2087 }
2088 else
2089 {
2090 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2091
2092 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2093 frame->prepare = prepare_schedule;
2094
2095 /* to avoid race conditions when a woken-up coro gets terminated */
2096 /* we arrange for a temporary on_destroy that calls adjust (0) */
2097 assert (!SvSTATE_current->on_destroy);//D
2098 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2099 }
2100
2101 frame->check = slf_check_semaphore_down;
2102
2103}
2104
2105/*****************************************************************************/
2106
2107#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2108
2109/* create a closure from XS, returns a code reference */
2110/* the arg can be accessed via GENSUB_ARG from the callback */
2111/* the callback must use dXSARGS/XSRETURN */
2112static SV *
2113gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2114{
2115 CV *cv = (CV *)NEWSV (0, 0);
2116
2117 sv_upgrade ((SV *)cv, SVt_PVCV);
2118
2119 CvANON_on (cv);
2120 CvISXSUB_on (cv);
2121 CvXSUB (cv) = xsub;
2122 GENSUB_ARG = arg;
2123
2124 return newRV_noinc ((SV *)cv);
2125}
2126
2127/*****************************************************************************/
2128
2018MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2129MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2019 2130
2020PROTOTYPES: DISABLE 2131PROTOTYPES: DISABLE
2021 2132
2022BOOT: 2133BOOT:
2023{ 2134{
2024#ifdef USE_ITHREADS 2135#ifdef USE_ITHREADS
2025 MUTEX_INIT (&coro_lock);
2026# if CORO_PTHREAD 2136# if CORO_PTHREAD
2027 coro_thx = PERL_GET_CONTEXT; 2137 coro_thx = PERL_GET_CONTEXT;
2028# endif 2138# endif
2029#endif 2139#endif
2030 BOOT_PAGESIZE; 2140 BOOT_PAGESIZE;
2050 main_mainstack = PL_mainstack; 2160 main_mainstack = PL_mainstack;
2051 main_top_env = PL_top_env; 2161 main_top_env = PL_top_env;
2052 2162
2053 while (main_top_env->je_prev) 2163 while (main_top_env->je_prev)
2054 main_top_env = main_top_env->je_prev; 2164 main_top_env = main_top_env->je_prev;
2165
2166 {
2167 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2168
2169 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2170 hv_store_ent (PL_custom_op_names, slf,
2171 newSVpv ("coro_slf", 0), 0);
2172
2173 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2174 hv_store_ent (PL_custom_op_descs, slf,
2175 newSVpv ("coro schedule like function", 0), 0);
2176 }
2055 2177
2056 coroapi.ver = CORO_API_VERSION; 2178 coroapi.ver = CORO_API_VERSION;
2057 coroapi.rev = CORO_API_REVISION; 2179 coroapi.rev = CORO_API_REVISION;
2058 2180
2059 coroapi.transfer = api_transfer; 2181 coroapi.transfer = api_transfer;
2107 RETVAL 2229 RETVAL
2108 2230
2109void 2231void
2110_set_stacklevel (...) 2232_set_stacklevel (...)
2111 CODE: 2233 CODE:
2112 api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); 2234 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2113 2235
2114void 2236void
2115transfer (...) 2237transfer (...)
2238 PROTOTYPE: $$
2116 CODE: 2239 CODE:
2117 api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); 2240 CORO_EXECUTE_SLF_XS (slf_init_transfer);
2118 2241
2119bool 2242bool
2120_destroy (SV *coro_sv) 2243_destroy (SV *coro_sv)
2121 CODE: 2244 CODE:
2122 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2245 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2129 CODE: 2252 CODE:
2130 _exit (code); 2253 _exit (code);
2131 2254
2132int 2255int
2133cctx_stacksize (int new_stacksize = 0) 2256cctx_stacksize (int new_stacksize = 0)
2257 PROTOTYPE: ;$
2134 CODE: 2258 CODE:
2135 RETVAL = cctx_stacksize; 2259 RETVAL = cctx_stacksize;
2136 if (new_stacksize) 2260 if (new_stacksize)
2137 { 2261 {
2138 cctx_stacksize = new_stacksize; 2262 cctx_stacksize = new_stacksize;
2141 OUTPUT: 2265 OUTPUT:
2142 RETVAL 2266 RETVAL
2143 2267
2144int 2268int
2145cctx_max_idle (int max_idle = 0) 2269cctx_max_idle (int max_idle = 0)
2270 PROTOTYPE: ;$
2146 CODE: 2271 CODE:
2147 RETVAL = cctx_max_idle; 2272 RETVAL = cctx_max_idle;
2148 if (max_idle > 1) 2273 if (max_idle > 1)
2149 cctx_max_idle = max_idle; 2274 cctx_max_idle = max_idle;
2150 OUTPUT: 2275 OUTPUT:
2151 RETVAL 2276 RETVAL
2152 2277
2153int 2278int
2154cctx_count () 2279cctx_count ()
2280 PROTOTYPE:
2155 CODE: 2281 CODE:
2156 RETVAL = cctx_count; 2282 RETVAL = cctx_count;
2157 OUTPUT: 2283 OUTPUT:
2158 RETVAL 2284 RETVAL
2159 2285
2160int 2286int
2161cctx_idle () 2287cctx_idle ()
2288 PROTOTYPE:
2162 CODE: 2289 CODE:
2163 RETVAL = cctx_idle; 2290 RETVAL = cctx_idle;
2164 OUTPUT: 2291 OUTPUT:
2165 RETVAL 2292 RETVAL
2166 2293
2167void 2294void
2168list () 2295list ()
2296 PROTOTYPE:
2169 PPCODE: 2297 PPCODE:
2170{ 2298{
2171 struct coro *coro; 2299 struct coro *coro;
2172 for (coro = coro_first; coro; coro = coro->next) 2300 for (coro = coro_first; coro; coro = coro->next)
2173 if (coro->hv) 2301 if (coro->hv)
2235 2363
2236void 2364void
2237throw (Coro::State self, SV *throw = &PL_sv_undef) 2365throw (Coro::State self, SV *throw = &PL_sv_undef)
2238 PROTOTYPE: $;$ 2366 PROTOTYPE: $;$
2239 CODE: 2367 CODE:
2368{
2369 struct coro *current = SvSTATE_current;
2370 SV **throwp = self == current ? &coro_throw : &self->throw;
2240 SvREFCNT_dec (self->throw); 2371 SvREFCNT_dec (*throwp);
2241 self->throw = SvOK (throw) ? newSVsv (throw) : 0; 2372 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2373}
2242 2374
2243void 2375void
2244api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2376api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2377 PROTOTYPE: $;$
2245 C_ARGS: aTHX_ coro, flags 2378 C_ARGS: aTHX_ coro, flags
2246 2379
2247SV * 2380SV *
2248has_cctx (Coro::State coro) 2381has_cctx (Coro::State coro)
2249 PROTOTYPE: $ 2382 PROTOTYPE: $
2274 OUTPUT: 2407 OUTPUT:
2275 RETVAL 2408 RETVAL
2276 2409
2277void 2410void
2278force_cctx () 2411force_cctx ()
2412 PROTOTYPE:
2279 CODE: 2413 CODE:
2280 struct coro *coro = SvSTATE (coro_current);
2281 coro->cctx->idle_sp = 0; 2414 SvSTATE_current->cctx->idle_sp = 0;
2282 2415
2283void 2416void
2284swap_defsv (Coro::State self) 2417swap_defsv (Coro::State self)
2285 PROTOTYPE: $ 2418 PROTOTYPE: $
2286 ALIAS: 2419 ALIAS:
2339} 2472}
2340 2473
2341void 2474void
2342schedule (...) 2475schedule (...)
2343 CODE: 2476 CODE:
2344 api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), items); 2477 CORO_EXECUTE_SLF_XS (slf_init_schedule);
2345 2478
2346void 2479void
2347cede (...) 2480cede (...)
2348 CODE: 2481 CODE:
2349 api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), items); 2482 CORO_EXECUTE_SLF_XS (slf_init_cede);
2350 2483
2351void 2484void
2352cede_notself (...) 2485cede_notself (...)
2353 CODE: 2486 CODE:
2354 api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), items); 2487 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
2355 2488
2356void 2489void
2357_set_current (SV *current) 2490_set_current (SV *current)
2358 PROTOTYPE: $ 2491 PROTOTYPE: $
2359 CODE: 2492 CODE:
2362 2495
2363void 2496void
2364_set_readyhook (SV *hook) 2497_set_readyhook (SV *hook)
2365 PROTOTYPE: $ 2498 PROTOTYPE: $
2366 CODE: 2499 CODE:
2367 LOCK;
2368 SvREFCNT_dec (coro_readyhook); 2500 SvREFCNT_dec (coro_readyhook);
2369 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 2501 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2370 UNLOCK;
2371 2502
2372int 2503int
2373prio (Coro::State coro, int newprio = 0) 2504prio (Coro::State coro, int newprio = 0)
2505 PROTOTYPE: $;$
2374 ALIAS: 2506 ALIAS:
2375 nice = 1 2507 nice = 1
2376 CODE: 2508 CODE:
2377{ 2509{
2378 RETVAL = coro->prio; 2510 RETVAL = coro->prio;
2410# for async_pool speedup 2542# for async_pool speedup
2411void 2543void
2412_pool_1 (SV *cb) 2544_pool_1 (SV *cb)
2413 CODE: 2545 CODE:
2414{ 2546{
2415 struct coro *coro = SvSTATE (coro_current);
2416 HV *hv = (HV *)SvRV (coro_current); 2547 HV *hv = (HV *)SvRV (coro_current);
2548 struct coro *coro = SvSTATE_hv ((SV *)hv);
2417 AV *defav = GvAV (PL_defgv); 2549 AV *defav = GvAV (PL_defgv);
2418 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 2550 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2419 AV *invoke_av; 2551 AV *invoke_av;
2420 int i, len; 2552 int i, len;
2421 2553
2442 { 2574 {
2443 av_fill (defav, len - 1); 2575 av_fill (defav, len - 1);
2444 for (i = 0; i < len; ++i) 2576 for (i = 0; i < len; ++i)
2445 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 2577 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2446 } 2578 }
2447
2448 SvREFCNT_dec (invoke);
2449} 2579}
2450 2580
2451void 2581void
2452_pool_2 (SV *cb) 2582_pool_2 (SV *cb)
2453 CODE: 2583 CODE:
2454{ 2584{
2455 struct coro *coro = SvSTATE (coro_current); 2585 HV *hv = (HV *)SvRV (coro_current);
2586 struct coro *coro = SvSTATE_hv ((SV *)hv);
2456 2587
2457 sv_setsv (cb, &PL_sv_undef); 2588 sv_setsv (cb, &PL_sv_undef);
2458 2589
2459 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2590 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2460 coro->saved_deffh = 0; 2591 coro->saved_deffh = 0;
2467 SvREFCNT_dec (old); 2598 SvREFCNT_dec (old);
2468 croak ("\3async_pool terminate\2\n"); 2599 croak ("\3async_pool terminate\2\n");
2469 } 2600 }
2470 2601
2471 av_clear (GvAV (PL_defgv)); 2602 av_clear (GvAV (PL_defgv));
2472 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2603 hv_store (hv, "desc", sizeof ("desc") - 1,
2473 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 2604 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2474 2605
2475 coro->prio = 0; 2606 coro->prio = 0;
2476 2607
2477 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2608 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2478 api_trace (aTHX_ coro_current, 0); 2609 api_trace (aTHX_ coro_current, 0);
2479 2610
2480 av_push (av_async_pool, newSVsv (coro_current)); 2611 av_push (av_async_pool, newSVsv (coro_current));
2481} 2612}
2482 2613
2483#if 0
2484
2485void
2486_generator_call (...)
2487 PROTOTYPE: @
2488 PPCODE:
2489 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2490 xxxx
2491 abort ();
2492
2493SV *
2494gensub (SV *sub, ...)
2495 PROTOTYPE: &;@
2496 CODE:
2497{
2498 struct coro *coro;
2499 MAGIC *mg;
2500 CV *xcv;
2501 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2502 int i;
2503
2504 CvGV (ncv) = CvGV (cv);
2505 CvFILE (ncv) = CvFILE (cv);
2506
2507 Newz (0, coro, 1, struct coro);
2508 coro->args = newAV ();
2509 coro->flags = CF_NEW;
2510
2511 av_extend (coro->args, items - 1);
2512 for (i = 1; i < items; i++)
2513 av_push (coro->args, newSVsv (ST (i)));
2514
2515 CvISXSUB_on (ncv);
2516 CvXSUBANY (ncv).any_ptr = (void *)coro;
2517
2518 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2519
2520 CvXSUB (ncv) = CvXSUB (xcv);
2521 CvANON_on (ncv);
2522
2523 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2524 RETVAL = newRV_noinc ((SV *)ncv);
2525}
2526 OUTPUT:
2527 RETVAL
2528
2529#endif
2530
2531 2614
2532MODULE = Coro::State PACKAGE = Coro::AIO 2615MODULE = Coro::State PACKAGE = Coro::AIO
2533 2616
2534void 2617void
2535_get_state (SV *self) 2618_get_state (SV *self)
2619 PROTOTYPE: $
2536 PPCODE: 2620 PPCODE:
2537{ 2621{
2538 AV *defav = GvAV (PL_defgv); 2622 AV *defav = GvAV (PL_defgv);
2539 AV *av = newAV (); 2623 AV *av = newAV ();
2540 int i; 2624 int i;
2583MODULE = Coro::State PACKAGE = Coro::AnyEvent 2667MODULE = Coro::State PACKAGE = Coro::AnyEvent
2584 2668
2585BOOT: 2669BOOT:
2586 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); 2670 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2587 2671
2588SV * 2672void
2589_schedule (...) 2673_schedule (...)
2590 PROTOTYPE: @
2591 CODE: 2674 CODE:
2592{ 2675{
2593 static int incede; 2676 static int incede;
2594 2677
2595 api_cede_notself (aTHX); 2678 api_cede_notself (aTHX);
2614MODULE = Coro::State PACKAGE = PerlIO::cede 2697MODULE = Coro::State PACKAGE = PerlIO::cede
2615 2698
2616BOOT: 2699BOOT:
2617 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2700 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2618 2701
2702MODULE = Coro::State PACKAGE = Coro::Semaphore
2703
2704SV *
2705new (SV *klass, SV *count_ = 0)
2706 CODE:
2707{
2708 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2709 AV *av = newAV ();
2710 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1));
2711 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2712}
2713 OUTPUT:
2714 RETVAL
2715
2716SV *
2717count (SV *self)
2718 CODE:
2719 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]);
2720 OUTPUT:
2721 RETVAL
2722
2723void
2724up (SV *self, int adjust = 1)
2725 ALIAS:
2726 adjust = 1
2727 CODE:
2728 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2729
2730void
2731down (SV *self)
2732 CODE:
2733 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
2734
2735void
2736try (SV *self)
2737 PPCODE:
2738{
2739 AV *av = (AV *)SvRV (self);
2740 SV *count_sv = AvARRAY (av)[0];
2741 IV count = SvIVX (count_sv);
2742
2743 if (count > 0)
2744 {
2745 --count;
2746 SvIVX (count_sv) = count;
2747 XSRETURN_YES;
2748 }
2749 else
2750 XSRETURN_NO;
2751}
2752
2753void
2754waiters (SV *self)
2755 CODE:
2756{
2757 AV *av = (AV *)SvRV (self);
2758
2759 if (GIMME_V == G_SCALAR)
2760 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0])));
2761 else
2762 {
2763 int i;
2764 EXTEND (SP, AvFILLp (av) + 1 - 1);
2765 for (i = 1; i <= AvFILLp (av); ++i)
2766 PUSHs (newSVsv (AvARRAY (av)[i]));
2767 }
2768}
2769

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines