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

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <string.h>
6
7 /* this useful idiom is unfortunately missing... */
8 static void
9 confess (const char *msg)
10 {
11 dSP;
12
13 PUSHMARK(SP);
14 XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0)));
15 PUTBACK;
16 call_pv ("Carp::confess", G_VOID);
17 }
18
19 #include "EventAPI.h"
20 #include "../Coro/CoroAPI.h"
21
22 #ifndef PE_PERLCB
23 # define PE_PERLCB 0x020 /* not public, but we need it :( */
24 #endif
25
26 #define CD_CORO 0
27 #define CD_TYPE 1
28 #define CD_OK 2
29 #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
34 #define EV_CLASS "Coro::Event"
35
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
45 static void
46 coro_std_cb(pe_event *pe)
47 {
48 AV *priv = (AV *)pe->ext_data;
49 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
50 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
55 if (type == 1)
56 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
57
58 if (*cd_coro != &PL_sv_undef)
59 {
60 CORO_READY (*cd_coro);
61 SvREFCNT_dec (*cd_coro);
62 *cd_coro = &PL_sv_undef;
63 NEED_SCHEDULE;
64 }
65 else
66 {
67 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
68 GEventAPI->stop (pe->up, 0);
69 }
70 }
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 MODULE = Coro::Event PACKAGE = Coro::Event
82
83 PROTOTYPES: ENABLE
84
85 BOOT:
86 {
87 I_EVENT_API ("Coro::Event");
88 I_CORO_API ("Coro::Event");
89
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 scheduler->base.prio = PE_PRIO_NORMAL; /* StarvePrio */
94 scheduler->min_interval = newSVnv (0);
95 scheduler->max_interval = newSVnv (0);
96 GEventAPI->stop ((pe_watcher *)scheduler, 0);
97 }
98
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 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
128 GEventAPI->start (w, 0);
129 }
130
131 void
132 _next(self)
133 SV * self
134 CODE:
135 pe_watcher *w = GEventAPI->sv_2watcher (self);
136 AV *priv = (AV *)w->ext_data;
137
138 if (!w->running)
139 GEventAPI->start (w, 1);
140
141 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes)
142 {
143 AvARRAY(priv)[CD_OK] = &PL_sv_no;
144 XSRETURN_NO;
145 }
146 else
147 {
148 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 XSRETURN_YES;
153 }
154
155 MODULE = Coro::Event PACKAGE = Coro
156
157 # overwrite the ready function
158 void
159 ready(self)
160 SV * self
161 PROTOTYPE: $
162 CODE:
163 NEED_SCHEDULE;
164 CORO_READY (self);
165
166