ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/EV/EV.xs
Revision: 1.9
Committed: Mon Dec 3 00:41:46 2007 UTC (16 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-4_3, rel-4_31
Changes since 1.8: +12 -1 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 static int inhibit;
27
28 static void
29 idle_cb (struct ev_idle *w, int revents)
30 {
31 ev_idle_stop (w);
32 }
33
34 static void
35 prepare_cb (struct ev_prepare *w, int revents)
36 {
37 static int incede;
38
39 if (inhibit)
40 return;
41
42 ++incede;
43
44 CORO_CEDE_NOTSELF;
45
46 while (CORO_NREADY >= incede && CORO_CEDE)
47 ;
48
49 /* if still ready, then we have lower-priority coroutines.
50 * poll anyways, but do not block.
51 */
52 if (CORO_NREADY >= incede && !ev_is_active (&idler))
53 ev_idle_start (&idler);
54
55 --incede;
56 }
57
58 MODULE = Coro::EV PACKAGE = Coro::EV
59
60 PROTOTYPES: ENABLE
61
62 BOOT:
63 {
64 I_EV_API ("Coro::EV");
65 I_CORO_API ("Coro::Event");
66
67 ev_prepare_init (&scheduler, prepare_cb);
68 ev_prepare_start (EV_DEFAULT_ &scheduler);
69 ev_unref ();
70
71 ev_idle_init (&idler, idle_cb);
72 }
73
74 void
75 _loop_oneshot ()
76 CODE:
77 ++inhibit;
78 ev_loop (EV_DEFAULT_ EVLOOP_ONESHOT);
79 --inhibit;
80
81 void
82 _timed_io_once (...)
83 CODE:
84 {
85 ONCE_INIT;
86 assert (AvFILLp (av) >= 1);
87 ev_once (
88 sv_fileno (AvARRAY (av)[0]),
89 SvIV (AvARRAY (av)[1]),
90 AvFILLp (av) >= 2 && SvOK (AvARRAY (av)[2]) ? SvNV (AvARRAY (av)[2]) : -1.,
91 once_cb,
92 (void *)SvREFCNT_inc (av)
93 );
94 ONCE_DONE;
95 }
96
97 void
98 _timer_once (...)
99 CODE:
100 {
101 ONCE_INIT;
102 NV after = SvNV (AvARRAY (av)[0]);
103 ev_once (
104 -1,
105 0,
106 after >= 0. ? after : 0.,
107 once_cb,
108 (void *)SvREFCNT_inc (av)
109 );
110 ONCE_DONE;
111 }
112
113
114