ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/EV/EV.xs
Revision: 1.6
Committed: Fri Nov 9 19:50:15 2007 UTC (16 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-4_2
Changes since 1.5: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <assert.h>
6 #include <string.h>
7
8 #define EV_PROTOTYPES 0
9 #include "EVAPI.h"
10 #include "../Coro/CoroAPI.h"
11
12 static void
13 once_cb (int revents, void *arg)
14 {
15 AV *av = (AV *)arg; /* @_ */
16 av_push (av, newSViv (revents));
17 CORO_READY (AvARRAY (av)[0]);
18 SvREFCNT_dec (av);
19 }
20
21 #define ONCE_INIT AV *av = GvAV (PL_defgv);
22 #define ONCE_DONE av_clear (av); av_push (av, SvREFCNT_inc (CORO_CURRENT));
23
24 static struct ev_prepare scheduler;
25 static struct ev_idle idler;
26
27 static void
28 idle_cb (struct ev_idle *w, int revents)
29 {
30 ev_idle_stop (w);
31 }
32
33 static void
34 prepare_cb (struct ev_prepare *w, int revents)
35 {
36 static int incede;
37
38 ++incede;
39
40 CORO_CEDE_NOTSELF;
41
42 while (CORO_NREADY >= incede && CORO_CEDE)
43 ;
44
45 /* if still ready, then we have lower-priority coroutines.
46 * poll anyways, but do not block.
47 */
48 if (CORO_NREADY >= incede && !ev_is_active (&idler))
49 ev_idle_start (&idler);
50
51 --incede;
52 }
53
54 MODULE = Coro::EV PACKAGE = Coro::EV
55
56 PROTOTYPES: ENABLE
57
58 BOOT:
59 {
60 I_EV_API ("Coro::EV");
61 I_CORO_API ("Coro::Event");
62
63 ev_prepare_init (&scheduler, prepare_cb);
64 ev_prepare_start (&scheduler);
65
66 ev_idle_init (&idler, idle_cb);
67 }
68
69 void
70 _timed_io_once (...)
71 CODE:
72 {
73 ONCE_INIT;
74 assert (AvFILLp (av) >= 1);
75 ev_once (
76 sv_fileno (AvARRAY (av)[0]),
77 SvIV (AvARRAY (av)[1]),
78 AvFILLp (av) >= 2 && SvOK (AvARRAY (av)[2]) ? SvNV (AvARRAY (av)[2]) : -1.,
79 once_cb,
80 (void *)SvREFCNT_inc (av)
81 );
82 ONCE_DONE;
83 }
84
85 void
86 _timer_once (...)
87 CODE:
88 {
89 ONCE_INIT;
90 NV after = SvNV (AvARRAY (av)[0]);
91 ev_once (
92 -1,
93 0,
94 after >= 0. ? after : 0.,
95 once_cb,
96 (void *)SvREFCNT_inc (av)
97 );
98 ONCE_DONE;
99 }
100
101
102