ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.9
Committed: Sat Mar 22 18:50:32 2003 UTC (21 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-2_5, rel-2_0, rel-2_1, rel-1_1, rel-1_0, rel-1_9, rel-1_2, rel-1_5, rel-1_4, rel-1_7, rel-1_6, rel-1_31
Changes since 1.8: +29 -20 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.6 /* this useful idiom is unfortunately missing... */
8     static void
9     confess (const char *msg)
10     {
11     dSP;
12    
13     PUSHMARK(SP);
14 root 1.9 XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0)));
15 root 1.6 PUTBACK;
16     call_pv ("Carp::confess", G_VOID);
17     }
18    
19 root 1.2 #include "EventAPI.h"
20     #include "../Coro/CoroAPI.h"
21 root 1.1
22 root 1.9 #ifndef PE_PERLCB
23     # define PE_PERLCB 0x020 /* not public, but we need it :( */
24     #endif
25    
26 root 1.3 #define CD_CORO 0
27     #define CD_TYPE 1
28 root 1.4 #define CD_OK 2
29 root 1.6 #define CD_PRIO 3 /* hardcoded in Coro::Event */
30     #define CD_HITS 4 /* hardcoded in Coro::Event */
31     #define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
32     #define CD_MAX 5
33 root 1.3
34 root 1.4 #define EV_CLASS "Coro::Event"
35 root 1.3
36     static pe_idle *scheduler;
37     static int do_schedule;
38    
39     #define NEED_SCHEDULE if (!do_schedule) \
40     { \
41     do_schedule = 1; \
42     GEventAPI->now ((pe_watcher *)scheduler); \
43     }
44 root 1.4
45 root 1.3 static void
46     coro_std_cb(pe_event *pe)
47     {
48 root 1.6 AV *priv = (AV *)pe->ext_data;
49 root 1.4 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
50 root 1.6 SV **cd_coro = &AvARRAY(priv)[CD_CORO];
51    
52     sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
53     sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits);
54 root 1.3
55     if (type == 1)
56 root 1.6 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
57 root 1.3
58 root 1.6 if (*cd_coro != &PL_sv_undef)
59 root 1.3 {
60 root 1.6 CORO_READY (*cd_coro);
61     SvREFCNT_dec (*cd_coro);
62     *cd_coro = &PL_sv_undef;
63 root 1.3 NEED_SCHEDULE;
64     }
65 root 1.4 else
66     {
67 root 1.6 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
68 root 1.4 GEventAPI->stop (pe->up, 0);
69     }
70 root 1.3 }
71    
72     static void
73     scheduler_cb(pe_event *pe)
74     {
75     while (CORO_NREADY)
76     CORO_CEDE;
77    
78     do_schedule = 0;
79     }
80    
81 root 1.1 MODULE = Coro::Event PACKAGE = Coro::Event
82    
83 root 1.3 PROTOTYPES: ENABLE
84    
85 root 1.1 BOOT:
86     {
87 root 1.9 I_EVENT_API ("Coro::Event");
88 root 1.1 I_CORO_API ("Coro::Event");
89 root 1.3
90     /* create a fake idle handler (we only ever call now) */
91     scheduler = GEventAPI->new_idle (0, 0);
92     scheduler->base.callback = scheduler_cb;
93 root 1.6 scheduler->base.prio = PE_PRIO_NORMAL; /* StarvePrio */
94 root 1.3 scheduler->min_interval = newSVnv (0);
95     scheduler->max_interval = newSVnv (0);
96     GEventAPI->stop ((pe_watcher *)scheduler, 0);
97 root 1.1 }
98 root 1.3
99     void
100     _install_std_cb(self,type)
101     SV * self
102     int type
103     CODE:
104     pe_watcher *w = GEventAPI->sv_2watcher (self);
105    
106 root 1.9 if (WaFLAGS (w) & PE_PERLCB)
107     croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught");
108     {
109     AV *priv = newAV ();
110     SV *rv = newRV_noinc ((SV *)priv);
111    
112     av_extend (priv, CD_MAX);
113     av_store (priv, CD_CORO, &PL_sv_undef);
114     av_store (priv, CD_TYPE, newSViv (type));
115     av_store (priv, CD_OK , &PL_sv_no);
116     av_store (priv, CD_PRIO, newSViv (0));
117     av_store (priv, CD_HITS, newSViv (0));
118     av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
119     SvREADONLY_on (priv);
120    
121     w->callback = coro_std_cb;
122     w->ext_data = priv;
123    
124     hv_store ((HV *)SvRV (self),
125     EV_CLASS, strlen (EV_CLASS),
126     rv, 0);
127 root 1.3
128 root 1.9 GEventAPI->start (w, 0);
129     }
130 root 1.8
131 root 1.3 void
132 root 1.4 _next(self)
133 root 1.3 SV * self
134     CODE:
135     pe_watcher *w = GEventAPI->sv_2watcher (self);
136 root 1.6 AV *priv = (AV *)w->ext_data;
137 root 1.3
138 root 1.4 if (!w->running)
139     GEventAPI->start (w, 1);
140 root 1.3
141 root 1.6 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes)
142 root 1.4 {
143 root 1.6 AvARRAY(priv)[CD_OK] = &PL_sv_no;
144 root 1.4 XSRETURN_NO;
145     }
146     else
147     {
148 root 1.6 if (AvARRAY(priv)[CD_CORO] != &PL_sv_undef)
149     confess ("only one coroutine can wait for an event");
150    
151     AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
152 root 1.4 XSRETURN_YES;
153     }
154 root 1.6
155     MODULE = Coro::Event PACKAGE = Coro
156    
157     # overwrite the ready function
158     void
159     ready(self)
160     SV * self
161 root 1.7 PROTOTYPE: $
162 root 1.6 CODE:
163     NEED_SCHEDULE;
164     CORO_READY (self);
165    
166 root 1.3