… | |
… | |
4 | #define PERL_EXT |
4 | #define PERL_EXT |
5 | |
5 | |
6 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
7 | #include "perl.h" |
7 | #include "perl.h" |
8 | #include "XSUB.h" |
8 | #include "XSUB.h" |
|
|
9 | #include "perliol.h" |
9 | |
10 | |
10 | #include "patchlevel.h" |
11 | #include "patchlevel.h" |
11 | |
12 | |
12 | #include <stdio.h> |
13 | #include <stdio.h> |
13 | #include <errno.h> |
14 | #include <errno.h> |
… | |
… | |
153 | I32 laststype; |
154 | I32 laststype; |
154 | int laststatval; |
155 | int laststatval; |
155 | Stat_t statcache; |
156 | Stat_t statcache; |
156 | }; |
157 | }; |
157 | |
158 | |
|
|
159 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
160 | |
158 | static size_t coro_stacksize = CORO_STACKSIZE; |
161 | static size_t coro_stacksize = CORO_STACKSIZE; |
159 | static struct CoroAPI coroapi; |
162 | static struct CoroAPI coroapi; |
160 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
163 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
161 | static JMPENV *main_top_env; |
164 | static JMPENV *main_top_env; |
162 | static HV *coro_state_stash, *coro_stash; |
165 | static HV *coro_state_stash, *coro_stash; |
… | |
… | |
1645 | static MGVTBL coro_gensub_vtbl = { |
1648 | static MGVTBL coro_gensub_vtbl = { |
1646 | 0, 0, 0, 0, |
1649 | 0, 0, 0, 0, |
1647 | coro_gensub_free |
1650 | coro_gensub_free |
1648 | }; |
1651 | }; |
1649 | |
1652 | |
|
|
1653 | /*****************************************************************************/ |
|
|
1654 | /* PerlIO::cede */ |
|
|
1655 | |
|
|
1656 | typedef struct |
|
|
1657 | { |
|
|
1658 | PerlIOBuf base; |
|
|
1659 | NV next, every; |
|
|
1660 | } PerlIOCede; |
|
|
1661 | |
|
|
1662 | static IV |
|
|
1663 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
|
|
1664 | { |
|
|
1665 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1666 | |
|
|
1667 | self->every = SvNV (arg); |
|
|
1668 | self->next = nvtime () + self->every; |
|
|
1669 | |
|
|
1670 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
|
|
1671 | } |
|
|
1672 | |
|
|
1673 | static SV * |
|
|
1674 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
|
|
1675 | { |
|
|
1676 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1677 | |
|
|
1678 | return newSVnv (self->every); |
|
|
1679 | } |
|
|
1680 | |
|
|
1681 | static IV |
|
|
1682 | PerlIOCede_flush (pTHX_ PerlIO *f) |
|
|
1683 | { |
|
|
1684 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1685 | double now = nvtime (); |
|
|
1686 | |
|
|
1687 | if (now >= self->next) |
|
|
1688 | { |
|
|
1689 | api_cede (); |
|
|
1690 | self->next = now + self->every; |
|
|
1691 | } |
|
|
1692 | |
|
|
1693 | return PerlIOBuf_flush (f); |
|
|
1694 | } |
|
|
1695 | |
|
|
1696 | static PerlIO_funcs PerlIO_cede = |
|
|
1697 | { |
|
|
1698 | sizeof(PerlIO_funcs), |
|
|
1699 | "cede", |
|
|
1700 | sizeof(PerlIOCede), |
|
|
1701 | PERLIO_K_DESTRUCT | PERLIO_K_RAW, |
|
|
1702 | PerlIOCede_pushed, |
|
|
1703 | PerlIOBuf_popped, |
|
|
1704 | PerlIOBuf_open, |
|
|
1705 | PerlIOBase_binmode, |
|
|
1706 | PerlIOCede_getarg, |
|
|
1707 | PerlIOBase_fileno, |
|
|
1708 | PerlIOBuf_dup, |
|
|
1709 | PerlIOBuf_read, |
|
|
1710 | PerlIOBuf_unread, |
|
|
1711 | PerlIOBuf_write, |
|
|
1712 | PerlIOBuf_seek, |
|
|
1713 | PerlIOBuf_tell, |
|
|
1714 | PerlIOBuf_close, |
|
|
1715 | PerlIOCede_flush, |
|
|
1716 | PerlIOBuf_fill, |
|
|
1717 | PerlIOBase_eof, |
|
|
1718 | PerlIOBase_error, |
|
|
1719 | PerlIOBase_clearerr, |
|
|
1720 | PerlIOBase_setlinebuf, |
|
|
1721 | PerlIOBuf_get_base, |
|
|
1722 | PerlIOBuf_bufsiz, |
|
|
1723 | PerlIOBuf_get_ptr, |
|
|
1724 | PerlIOBuf_get_cnt, |
|
|
1725 | PerlIOBuf_set_ptrcnt, |
|
|
1726 | }; |
|
|
1727 | |
|
|
1728 | |
1650 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1729 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1651 | |
1730 | |
1652 | PROTOTYPES: DISABLE |
1731 | PROTOTYPES: DISABLE |
1653 | |
1732 | |
1654 | BOOT: |
1733 | BOOT: |
… | |
… | |
1683 | main_top_env = main_top_env->je_prev; |
1762 | main_top_env = main_top_env->je_prev; |
1684 | |
1763 | |
1685 | coroapi.ver = CORO_API_VERSION; |
1764 | coroapi.ver = CORO_API_VERSION; |
1686 | coroapi.rev = CORO_API_REVISION; |
1765 | coroapi.rev = CORO_API_REVISION; |
1687 | coroapi.transfer = api_transfer; |
1766 | coroapi.transfer = api_transfer; |
|
|
1767 | |
|
|
1768 | { |
|
|
1769 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
1770 | |
|
|
1771 | if (!svp) croak ("Time::HiRes is required"); |
|
|
1772 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
1773 | |
|
|
1774 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
1775 | } |
1688 | |
1776 | |
1689 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1777 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1690 | } |
1778 | } |
1691 | |
1779 | |
1692 | SV * |
1780 | SV * |
… | |
… | |
2219 | } |
2307 | } |
2220 | |
2308 | |
2221 | --incede; |
2309 | --incede; |
2222 | } |
2310 | } |
2223 | |
2311 | |
|
|
2312 | |
|
|
2313 | MODULE = Coro::State PACKAGE = PerlIO::cede |
|
|
2314 | |
|
|
2315 | BOOT: |
|
|
2316 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |