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.230 by root, Mon Apr 14 11:28:59 2008 UTC vs.
Revision 1.238 by root, Sat May 31 12:10:55 2008 UTC

10#include "patchlevel.h" 10#include "patchlevel.h"
11 11
12#include <stdio.h> 12#include <stdio.h>
13#include <errno.h> 13#include <errno.h>
14#include <assert.h> 14#include <assert.h>
15
16#ifdef WIN32
17# undef setjmp
18# undef longjmp
19# undef _exit
20# define setjmp _setjmp // deep magic, don't ask
21#else
15#include <inttypes.h> /* portable stdint.h */ 22# include <inttypes.h> /* most portable stdint.h */
23#endif
16 24
17#ifdef HAVE_MMAP 25#ifdef HAVE_MMAP
18# include <unistd.h> 26# include <unistd.h>
19# include <sys/mman.h> 27# include <sys/mman.h>
20# ifndef MAP_ANONYMOUS 28# ifndef MAP_ANONYMOUS
163/* async_pool helper stuff */ 171/* async_pool helper stuff */
164static SV *sv_pool_rss; 172static SV *sv_pool_rss;
165static SV *sv_pool_size; 173static SV *sv_pool_size;
166static AV *av_async_pool; 174static AV *av_async_pool;
167 175
176/* Coro::AnyEvent */
177static SV *sv_activity;
178
168static struct coro_cctx *cctx_first; 179static struct coro_cctx *cctx_first;
169static int cctx_count, cctx_idle; 180static int cctx_count, cctx_idle;
170 181
171enum { 182enum {
172 CC_MAPPED = 0x01, 183 CC_MAPPED = 0x01,
257#define PRIO_IDLE -3 268#define PRIO_IDLE -3
258#define PRIO_MIN -4 269#define PRIO_MIN -4
259 270
260/* for Coro.pm */ 271/* for Coro.pm */
261static SV *coro_current; 272static SV *coro_current;
273static SV *coro_readyhook;
262static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 274static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
263static int coro_nready; 275static int coro_nready;
264static struct coro *coro_first; 276static struct coro *coro_first;
265 277
266/** lowlevel stuff **********************************************************/ 278/** lowlevel stuff **********************************************************/
502 514
503 if (expect_true (CvDEPTH (cv))) 515 if (expect_true (CvDEPTH (cv)))
504 { 516 {
505 EXTEND (SP, 3); 517 EXTEND (SP, 3);
506 PUSHs ((SV *)CvPADLIST (cv)); 518 PUSHs ((SV *)CvPADLIST (cv));
507 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 519 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
508 PUSHs ((SV *)cv); 520 PUSHs ((SV *)cv);
509 521
510 CvDEPTH (cv) = 0; 522 CvDEPTH (cv) = 0;
511 get_padlist (aTHX_ cv); 523 get_padlist (aTHX_ cv);
512 } 524 }
672/** coroutine stack handling ************************************************/ 684/** coroutine stack handling ************************************************/
673 685
674static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 686static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
675static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 687static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
676 688
689/* apparently < 5.8.8 */
690#undef MgPV_nolen_const
691#ifndef MgPV_nolen_const
692#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
693 SvPV_nolen_const((SV*)((mg)->mg_ptr)) : \
694 (const char*)(mg)->mg_ptr)
695#endif
696
677/* 697/*
678 * This overrides the default magic get method of %SIG elements. 698 * This overrides the default magic get method of %SIG elements.
679 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 699 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
680 * and instead of tryign to save and restore the hash elements, we just provide 700 * and instead of tryign to save and restore the hash elements, we just provide
681 * readback here. 701 * readback here.
712 if (svp) 732 if (svp)
713 { 733 {
714 SV *old = *svp; 734 SV *old = *svp;
715 *svp = newSVsv (sv); 735 *svp = newSVsv (sv);
716 SvREFCNT_dec (old); 736 SvREFCNT_dec (old);
717 return; 737 return 0;
718 } 738 }
719 } 739 }
720 740
721 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 741 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
722} 742}
1371static int 1391static int
1372api_ready (SV *coro_sv) 1392api_ready (SV *coro_sv)
1373{ 1393{
1374 dTHX; 1394 dTHX;
1375 struct coro *coro; 1395 struct coro *coro;
1396 SV *sv_hook;
1397 void (*xs_hook)(void);
1376 1398
1377 if (SvROK (coro_sv)) 1399 if (SvROK (coro_sv))
1378 coro_sv = SvRV (coro_sv); 1400 coro_sv = SvRV (coro_sv);
1379 1401
1380 coro = SvSTATE (coro_sv); 1402 coro = SvSTATE (coro_sv);
1383 return 0; 1405 return 0;
1384 1406
1385 coro->flags |= CF_READY; 1407 coro->flags |= CF_READY;
1386 1408
1387 LOCK; 1409 LOCK;
1410
1411 sv_hook = coro_nready ? 0 : coro_readyhook;
1412 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1413
1388 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1414 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1389 ++coro_nready; 1415 ++coro_nready;
1416
1390 UNLOCK; 1417 UNLOCK;
1418
1419 if (sv_hook)
1420 {
1421 dSP;
1422
1423 ENTER;
1424 SAVETMPS;
1425
1426 PUSHMARK (SP);
1427 PUTBACK;
1428 call_sv (sv_hook, G_DISCARD);
1429 SPAGAIN;
1430
1431 FREETMPS;
1432 LEAVE;
1433 }
1434
1435 if (xs_hook)
1436 xs_hook ();
1391 1437
1392 return 1; 1438 return 1;
1393} 1439}
1394 1440
1395static int 1441static int
1581 main_top_env = PL_top_env; 1627 main_top_env = PL_top_env;
1582 1628
1583 while (main_top_env->je_prev) 1629 while (main_top_env->je_prev)
1584 main_top_env = main_top_env->je_prev; 1630 main_top_env = main_top_env->je_prev;
1585 1631
1586 coroapi.ver = CORO_API_VERSION; 1632 coroapi.ver = CORO_API_VERSION;
1587 coroapi.rev = CORO_API_REVISION; 1633 coroapi.rev = CORO_API_REVISION;
1588 coroapi.transfer = api_transfer; 1634 coroapi.transfer = api_transfer;
1589 1635
1590 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1636 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1591} 1637}
1592 1638
1593SV * 1639SV *
1677 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1723 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1678 OUTPUT: 1724 OUTPUT:
1679 RETVAL 1725 RETVAL
1680 1726
1681void 1727void
1682_exit (code) 1728_exit (int code)
1683 int code
1684 PROTOTYPE: $ 1729 PROTOTYPE: $
1685 CODE: 1730 CODE:
1686 _exit (code); 1731 _exit (code);
1687 1732
1688int 1733int
1779 1824
1780void 1825void
1781api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1826api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1782 1827
1783SV * 1828SV *
1784has_stack (Coro::State coro) 1829has_cctx (Coro::State coro)
1785 PROTOTYPE: $ 1830 PROTOTYPE: $
1786 CODE: 1831 CODE:
1787 RETVAL = boolSV (!!coro->cctx); 1832 RETVAL = boolSV (!!coro->cctx);
1788 OUTPUT: 1833 OUTPUT:
1789 RETVAL 1834 RETVAL
1820 1865
1821BOOT: 1866BOOT:
1822{ 1867{
1823 int i; 1868 int i;
1824 1869
1870 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1825 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 1871 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1826 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 1872 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1827 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1828 1873
1829 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 1874 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1830 SvREADONLY_on (coro_current); 1875 SvREADONLY_on (coro_current);
1831 1876
1832 coro_stash = gv_stashpv ("Coro", TRUE); 1877 coro_stash = gv_stashpv ("Coro", TRUE);
1863_set_current (SV *current) 1908_set_current (SV *current)
1864 PROTOTYPE: $ 1909 PROTOTYPE: $
1865 CODE: 1910 CODE:
1866 SvREFCNT_dec (SvRV (coro_current)); 1911 SvREFCNT_dec (SvRV (coro_current));
1867 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 1912 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1913
1914void
1915_set_readyhook (SV *hook)
1916 PROTOTYPE: $
1917 CODE:
1918 LOCK;
1919 SvREFCNT_dec (coro_readyhook);
1920 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
1921 UNLOCK;
1868 1922
1869int 1923int
1870prio (Coro::State coro, int newprio = 0) 1924prio (Coro::State coro, int newprio = 0)
1871 ALIAS: 1925 ALIAS:
1872 nice = 1 1926 nice = 1
2029 PL_laststype = data->laststype; 2083 PL_laststype = data->laststype;
2030 PL_laststatval = data->laststatval; 2084 PL_laststatval = data->laststatval;
2031 PL_statcache = data->statcache; 2085 PL_statcache = data->statcache;
2032} 2086}
2033 2087
2088
2089MODULE = Coro::State PACKAGE = Coro::AnyEvent
2090
2091BOOT:
2092 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2093
2094SV *
2095_schedule (...)
2096 PROTOTYPE: @
2097 CODE:
2098{
2099 static int incede;
2100
2101 api_cede_notself ();
2102
2103 ++incede;
2104 while (coro_nready >= incede && api_cede ())
2105 ;
2106
2107 sv_setsv (sv_activity, &PL_sv_undef);
2108 if (coro_nready >= incede)
2109 {
2110 PUSHMARK (SP);
2111 PUTBACK;
2112 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2113 SPAGAIN;
2114 }
2115
2116 --incede;
2117}
2118

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines