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, 7 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

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5     #include <assert.h>
6     #include <string.h>
7    
8 root 1.2 #define EV_PROTOTYPES 0
9 root 1.1 #include "EVAPI.h"
10     #include "../Coro/CoroAPI.h"
11    
12     static void
13 root 1.2 once_cb (int revents, void *arg)
14 root 1.1 {
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 root 1.2 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 root 1.3 ev_idle_stop (w);
31 root 1.2 }
32    
33     static void
34 root 1.5 prepare_cb (struct ev_prepare *w, int revents)
35 root 1.2 {
36 root 1.5 static int incede;
37    
38     ++incede;
39    
40     CORO_CEDE_NOTSELF;
41    
42 root 1.6 while (CORO_NREADY >= incede && CORO_CEDE)
43 root 1.2 ;
44    
45     /* if still ready, then we have lower-priority coroutines.
46 root 1.5 * poll anyways, but do not block.
47 root 1.2 */
48 root 1.6 if (CORO_NREADY >= incede && !ev_is_active (&idler))
49 root 1.5 ev_idle_start (&idler);
50    
51     --incede;
52 root 1.2 }
53    
54 root 1.1 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 root 1.2
63 root 1.3 ev_prepare_init (&scheduler, prepare_cb);
64     ev_prepare_start (&scheduler);
65 root 1.2
66 root 1.3 ev_idle_init (&idler, idle_cb);
67 root 1.1 }
68    
69     void
70     _timed_io_once (...)
71     CODE:
72     {
73     ONCE_INIT;
74     assert (AvFILLp (av) >= 1);
75 root 1.2 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 root 1.1 );
82     ONCE_DONE;
83     }
84    
85     void
86     _timer_once (...)
87     CODE:
88     {
89     ONCE_INIT;
90     NV after = SvNV (AvARRAY (av)[0]);
91 root 1.2 ev_once (
92 root 1.1 -1,
93 root 1.2 0,
94 root 1.4 after >= 0. ? after : 0.,
95 root 1.2 once_cb,
96     (void *)SvREFCNT_inc (av)
97 root 1.1 );
98     ONCE_DONE;
99     }
100    
101    
102