ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/EV/EV.xs
Revision: 1.3
Committed: Thu Nov 1 06:58:21 2007 UTC (16 years, 9 months ago) by root
Branch: MAIN
Changes since 1.2: +5 -5 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     prepare_cb (struct ev_watcher *w, int revents)
35     {
36     while (CORO_NREADY && CORO_CEDE)
37     ;
38    
39     /* if still ready, then we have lower-priority coroutines.
40     * cede once, then poll nonblocking
41     */
42     if (CORO_NREADY)
43     {
44     CORO_CEDE_NOTSELF;
45    
46     if (CORO_NREADY)
47 root 1.3 ev_idle_start (&idler);
48 root 1.2 }
49     }
50    
51 root 1.1 MODULE = Coro::EV PACKAGE = Coro::EV
52    
53     PROTOTYPES: ENABLE
54    
55     BOOT:
56     {
57     I_EV_API ("Coro::EV");
58     I_CORO_API ("Coro::Event");
59 root 1.2
60 root 1.3 ev_prepare_init (&scheduler, prepare_cb);
61     ev_prepare_start (&scheduler);
62 root 1.2
63 root 1.3 ev_idle_init (&idler, idle_cb);
64 root 1.1 }
65    
66     void
67     _timed_io_once (...)
68     CODE:
69     {
70     ONCE_INIT;
71     assert (AvFILLp (av) >= 1);
72 root 1.2 ev_once (
73     sv_fileno (AvARRAY (av)[0]),
74     SvIV (AvARRAY (av)[1]),
75     AvFILLp (av) >= 2 && SvOK (AvARRAY (av)[2]) ? SvNV (AvARRAY (av)[2]) : -1.,
76     once_cb,
77     (void *)SvREFCNT_inc (av)
78 root 1.1 );
79     ONCE_DONE;
80     }
81    
82     void
83     _timer_once (...)
84     CODE:
85     {
86     ONCE_INIT;
87     NV after = SvNV (AvARRAY (av)[0]);
88 root 1.2 ev_once (
89 root 1.1 -1,
90 root 1.2 0,
91 root 1.1 after > 0. ? after : 1e-30,
92 root 1.2 once_cb,
93     (void *)SvREFCNT_inc (av)
94 root 1.1 );
95     ONCE_DONE;
96     }
97    
98    
99