ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.3
Committed: Fri Aug 17 01:45:39 2001 UTC (22 years, 9 months ago) by root
Branch: MAIN
Changes since 1.2: +110 -0 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 root 1.3 #include <string.h>
6    
7 root 1.2 #include "EventAPI.h"
8     #include "../Coro/CoroAPI.h"
9 root 1.1
10 root 1.3 #define CD_CORO 0
11     #define CD_TYPE 1
12     #define CD_W 2
13     #define CD_GOT 3
14     #define CD_MAX 3
15     /* no support for hits and prio so far. */
16    
17     #define EV_CLASS "Coro::Event::Ev"
18    
19     static HV *ev_stash;
20     static pe_idle *scheduler;
21     static int do_schedule;
22    
23     #define NEED_SCHEDULE if (!do_schedule) \
24     { \
25     do_schedule = 1; \
26     GEventAPI->now ((pe_watcher *)scheduler); \
27     }
28     static void
29     coro_std_cb(pe_event *pe)
30     {
31     AV *av = (AV *)SvRV ((SV *)pe->ext_data);
32     IV type = SvIV (*av_fetch (av, CD_TYPE, 1));
33     SV *cd_coro = *av_fetch (av, CD_CORO, 1);
34    
35     av_store (av, CD_W, SvREFCNT_inc (pe->up->mysv));
36    
37     if (type == 1)
38     av_store (av, CD_GOT, newSViv (((pe_ioevent *)pe)->got));
39    
40     GEventAPI->stop (pe->up, 0);
41    
42     if (SvROK (cd_coro))
43     {
44     CORO_READY (cd_coro);
45     NEED_SCHEDULE;
46     }
47     }
48    
49     static void
50     scheduler_cb(pe_event *pe)
51     {
52     while (CORO_NREADY)
53     CORO_CEDE;
54    
55     do_schedule = 0;
56     }
57    
58 root 1.1 MODULE = Coro::Event PACKAGE = Coro::Event
59    
60 root 1.3 PROTOTYPES: ENABLE
61    
62 root 1.1 BOOT:
63     {
64     I_EVENT_API("Coro::Event");
65     I_CORO_API ("Coro::Event");
66 root 1.3
67     ev_stash = gv_stashpv (EV_CLASS, TRUE);
68    
69     /* create a fake idle handler (we only ever call now) */
70     scheduler = GEventAPI->new_idle (0, 0);
71     scheduler->base.callback = scheduler_cb;
72     scheduler->min_interval = newSVnv (0);
73     scheduler->max_interval = newSVnv (0);
74     GEventAPI->stop ((pe_watcher *)scheduler, 0);
75 root 1.1 }
76 root 1.3
77     void
78     _install_std_cb(self,type)
79     SV * self
80     int type
81     CODE:
82     pe_watcher *w = GEventAPI->sv_2watcher (self);
83     AV *priv = newAV ();
84     SV *rv;
85    
86     av_extend (priv, CD_MAX);
87     av_store (priv, CD_TYPE, newSViv (type));
88    
89     rv = sv_bless (newRV_noinc ((SV *)priv), ev_stash);
90    
91     hv_store ((HV *)SvRV (self),
92     EV_CLASS, strlen (EV_CLASS),
93     rv, 0);
94    
95     w->ext_data = rv;
96     w->callback = coro_std_cb;
97    
98     void
99     _next0(self)
100     SV * self
101     CODE:
102     pe_watcher *w = GEventAPI->sv_2watcher (self);
103     AV *priv = (AV *)SvRV ((SV *)w->ext_data);
104    
105     GEventAPI->start (w, 1);
106    
107     if (SvROK (*av_fetch (priv, CD_CORO, 1)))
108     croak ("only one coroutine can wait for an event");
109    
110     av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT));
111    
112     SV *
113     _next1(self)
114     SV * self
115     CODE:
116     pe_watcher *w = GEventAPI->sv_2watcher (self);
117     AV *priv = (AV *)SvRV ((SV *)w->ext_data);
118    
119     av_store (priv, CD_CORO, &PL_sv_undef);
120    
121     RETVAL = SvREFCNT_inc ((SV *)w->ext_data);
122     OUTPUT:
123     RETVAL
124