… | |
… | |
6 | |
6 | |
7 | #include "EXTERN.h" |
7 | #include "EXTERN.h" |
8 | #include "perl.h" |
8 | #include "perl.h" |
9 | #include "XSUB.h" |
9 | #include "XSUB.h" |
10 | |
10 | |
|
|
11 | #if HAVE_EV |
|
|
12 | # include "EVAPI.h" |
|
|
13 | # include "CoroAPI.h" |
|
|
14 | #endif |
|
|
15 | |
11 | #define IN_DESTRUCT PL_dirty |
16 | #define IN_DESTRUCT PL_dirty |
12 | |
17 | |
13 | typedef U16 uint16; |
18 | typedef U16 uint16; |
14 | |
19 | |
15 | /* cached function gv's */ |
20 | /* cached function gv's */ |
16 | static CV *readable, *writable; |
21 | static CV *readable, *writable; |
|
|
22 | static int use_ev; |
17 | |
23 | |
18 | #include "violite.h" |
24 | #include "violite.h" |
19 | |
25 | |
20 | #define DESC_OFFSET 22 |
26 | #define DESC_OFFSET 22 |
21 | |
27 | |
22 | #define CoMy_MAGIC 0x436f4d79 |
28 | #define CoMy_MAGIC 0x436f4d79 |
23 | |
29 | |
24 | typedef struct { |
30 | typedef struct { |
25 | int magic; |
31 | int magic; |
26 | SV *corosocket; |
32 | SV *corohandle_sv, *corohandle; |
27 | int bufofs, bufcnt; |
33 | int bufofs, bufcnt; |
|
|
34 | #if HAVE_EV |
|
|
35 | ev_io rw, ww; |
|
|
36 | #endif |
28 | char buf[VIO_READ_BUFFER_SIZE]; |
37 | char buf[VIO_READ_BUFFER_SIZE]; |
29 | } ourdata; |
38 | } ourdata; |
30 | |
39 | |
31 | #define OURDATAPTR (*((ourdata **)((vio)->desc + DESC_OFFSET))) |
40 | #define OURDATAPTR (*((ourdata **)((vio)->desc + DESC_OFFSET))) |
32 | |
41 | |
33 | static int |
42 | static int |
34 | our_read (Vio *vio, gptr p, int len) |
43 | our_read (Vio *vio, xgptr p, int len) |
35 | { |
44 | { |
36 | ourdata *our = OURDATAPTR; |
45 | ourdata *our = OURDATAPTR; |
37 | |
46 | |
38 | if (!our->bufcnt) |
47 | if (!our->bufcnt) |
39 | { |
48 | { |
… | |
… | |
47 | rd = recv (vio->sd, our->buf, sizeof (our->buf), 0); |
56 | rd = recv (vio->sd, our->buf, sizeof (our->buf), 0); |
48 | |
57 | |
49 | if (rd >= 0 || errno != EAGAIN) |
58 | if (rd >= 0 || errno != EAGAIN) |
50 | break; |
59 | break; |
51 | |
60 | |
|
|
61 | #if HAVE_EV |
|
|
62 | if (use_ev) |
52 | { |
63 | { |
|
|
64 | our->rw.data = (void *)sv_2mortal (SvREFCNT_inc (CORO_CURRENT)); |
|
|
65 | ev_io_start (EV_DEFAULT_UC, &(our->rw)); |
|
|
66 | CORO_SCHEDULE; |
|
|
67 | ev_io_stop (EV_DEFAULT_UC, &(our->rw)); /* avoids races */ |
|
|
68 | } |
|
|
69 | else |
|
|
70 | #endif |
|
|
71 | { |
53 | dSP; |
72 | dSP; |
54 | PUSHMARK (SP); |
73 | PUSHMARK (SP); |
55 | XPUSHs (our->corosocket); |
74 | XPUSHs (our->corohandle); |
56 | PUTBACK; |
75 | PUTBACK; |
57 | call_sv ((SV *)readable, G_VOID | G_DISCARD); |
76 | call_sv ((SV *)readable, G_VOID | G_DISCARD); |
58 | } |
77 | } |
59 | } |
78 | } |
60 | |
79 | |
61 | if (rd <= 0) |
80 | if (rd <= 0) |
62 | return rd; |
81 | return rd; |
63 | |
82 | |
… | |
… | |
74 | |
93 | |
75 | return len; |
94 | return len; |
76 | } |
95 | } |
77 | |
96 | |
78 | static int |
97 | static int |
79 | our_write (Vio *vio, const gptr p, int len) |
98 | our_write (Vio *vio, const xgptr p, int len) |
80 | { |
99 | { |
81 | char *ptr = (char *)p; |
100 | char *ptr = (char *)p; |
82 | my_bool dummy; |
101 | my_bool dummy; |
83 | |
102 | |
84 | vio->vioblocking (vio, 0, &dummy); |
103 | vio->vioblocking (vio, 0, &dummy); |
… | |
… | |
92 | ptr += wr; |
111 | ptr += wr; |
93 | len -= wr; |
112 | len -= wr; |
94 | } |
113 | } |
95 | else if (errno == EAGAIN) |
114 | else if (errno == EAGAIN) |
96 | { |
115 | { |
|
|
116 | ourdata *our = OURDATAPTR; |
|
|
117 | |
|
|
118 | #if HAVE_EV |
|
|
119 | if (use_ev) |
|
|
120 | { |
|
|
121 | our->ww.data = (void *)sv_2mortal (SvREFCNT_inc (CORO_CURRENT)); |
|
|
122 | ev_io_start (EV_DEFAULT_UC, &(our->ww)); |
|
|
123 | CORO_SCHEDULE; |
|
|
124 | ev_io_stop (EV_DEFAULT_UC, &(our->ww)); /* avoids races */ |
|
|
125 | } |
|
|
126 | else |
|
|
127 | #endif |
|
|
128 | { |
97 | dSP; |
129 | dSP; |
98 | PUSHMARK (SP); |
130 | PUSHMARK (SP); |
99 | XPUSHs (OURDATAPTR->corosocket); |
131 | XPUSHs (our->corohandle); |
100 | PUTBACK; |
132 | PUTBACK; |
101 | call_sv ((SV *)writable, G_VOID | G_DISCARD); |
133 | call_sv ((SV *)writable, G_VOID | G_DISCARD); |
|
|
134 | } |
102 | } |
135 | } |
103 | else if (ptr == (char *)p) |
136 | else if (ptr == (char *)p) |
104 | return -1; |
137 | return -1; |
105 | else |
138 | else |
106 | break; |
139 | break; |
107 | } |
140 | } |
108 | |
141 | |
109 | return ptr - (char *)p; |
142 | return ptr - (char *)p; |
110 | } |
143 | } |
111 | |
144 | |
|
|
145 | static int |
|
|
146 | our_close (Vio *vio) |
|
|
147 | { |
|
|
148 | ourdata *our = OURDATAPTR; |
|
|
149 | |
|
|
150 | if (vio->read != our_read) |
|
|
151 | croak ("vio.read has unexpected content during unpatch - wtf?"); |
|
|
152 | |
|
|
153 | if (vio->write != our_write) |
|
|
154 | croak ("vio.write has unexpected content during unpatch - wtf?"); |
|
|
155 | |
|
|
156 | if (vio->vioclose != our_close) |
|
|
157 | croak ("vio.vioclose has unexpected content during unpatch - wtf?"); |
|
|
158 | |
|
|
159 | #if HAVE_EV |
|
|
160 | if (use_ev) |
|
|
161 | { |
|
|
162 | ev_io_stop (EV_DEFAULT_UC, &(our->rw)); |
|
|
163 | ev_io_stop (EV_DEFAULT_UC, &(our->ww)); |
|
|
164 | } |
|
|
165 | #endif |
|
|
166 | |
|
|
167 | SvREFCNT_dec (our->corohandle); |
|
|
168 | SvREFCNT_dec (our->corohandle_sv); |
|
|
169 | |
|
|
170 | Safefree (our); |
|
|
171 | |
|
|
172 | vio->read = vio_read; |
|
|
173 | vio->write = vio_write; |
|
|
174 | vio->vioclose = vio_close; |
|
|
175 | |
|
|
176 | vio->vioclose (vio); |
|
|
177 | } |
|
|
178 | |
|
|
179 | #if HAVE_EV |
|
|
180 | static void |
|
|
181 | iocb (EV_P_ ev_io *w, int revents) |
|
|
182 | { |
|
|
183 | ev_io_stop (EV_A, w); |
|
|
184 | CORO_READY ((SV *)w->data); |
|
|
185 | } |
|
|
186 | #endif |
|
|
187 | |
112 | MODULE = Coro::Mysql PACKAGE = Coro::Mysql |
188 | MODULE = Coro::Mysql PACKAGE = Coro::Mysql |
113 | |
189 | |
114 | BOOT: |
190 | BOOT: |
115 | { |
191 | { |
116 | readable = get_cv ("Coro::Mysql::readable", 0); |
192 | readable = get_cv ("Coro::Mysql::readable", 0); |
… | |
… | |
118 | } |
194 | } |
119 | |
195 | |
120 | PROTOTYPES: ENABLE |
196 | PROTOTYPES: ENABLE |
121 | |
197 | |
122 | void |
198 | void |
123 | _patch (IV sock, int fd, SV *corosocket) |
199 | _use_ev () |
|
|
200 | PPCODE: |
|
|
201 | { |
|
|
202 | static int onceonly; |
|
|
203 | |
|
|
204 | if (!onceonly) |
|
|
205 | { |
|
|
206 | onceonly = 1; |
|
|
207 | #if HAVE_EV |
|
|
208 | I_EV_API ("Coro::Mysql"); |
|
|
209 | I_CORO_API ("Coro::Mysql"); |
|
|
210 | use_ev = 1; |
|
|
211 | #endif |
|
|
212 | } |
|
|
213 | |
|
|
214 | XPUSHs (use_ev ? &PL_sv_yes : &PL_sv_no); |
|
|
215 | } |
|
|
216 | |
|
|
217 | void |
|
|
218 | _patch (IV sock, int fd, SV *corohandle_sv, SV *corohandle) |
124 | CODE: |
219 | CODE: |
125 | { |
220 | { |
126 | MYSQL *my = (MYSQL *)sock; |
221 | MYSQL *my = (MYSQL *)sock; |
127 | Vio *vio = my->net.vio; |
222 | Vio *vio = my->net.vio; |
128 | ourdata *our; |
223 | ourdata *our; |
… | |
… | |
131 | croak ("DBD::mysql fd and libmysql disagree - library mismatch, unsupported transport or API changes?"); |
226 | croak ("DBD::mysql fd and libmysql disagree - library mismatch, unsupported transport or API changes?"); |
132 | |
227 | |
133 | if (fd != vio->sd) |
228 | if (fd != vio->sd) |
134 | croak ("DBD::mysql fd and vio-sd disagree - library mismatch, unsupported transport or API changes?"); |
229 | croak ("DBD::mysql fd and vio-sd disagree - library mismatch, unsupported transport or API changes?"); |
135 | |
230 | |
|
|
231 | if (vio->vioclose != vio_close) |
|
|
232 | croak ("vio.write has unexpected content - library mismatch, unsupported transport or API changes?"); |
|
|
233 | |
136 | if (vio->write != vio_write) |
234 | if (vio->write != vio_write) |
137 | croak ("vio.write has unexpected content - library mismatch, unsupported transport or API changes?"); |
235 | croak ("vio.write has unexpected content - library mismatch, unsupported transport or API changes?"); |
138 | |
236 | |
139 | if (vio->read != vio_read && vio->read != vio_read_buff) |
237 | if (vio->read != vio_read |
|
|
238 | && vio->read != vio_read_buff) |
140 | croak ("vio.read has unexpected content - library mismatch, unsupported transport or API changes?"); |
239 | croak ("vio.read has unexpected content - library mismatch, unsupported transport or API changes?"); |
141 | |
240 | |
142 | Newz (0, our, 1, ourdata); |
241 | Newz (0, our, 1, ourdata); |
143 | our->magic = CoMy_MAGIC; |
242 | our->magic = CoMy_MAGIC; |
|
|
243 | our->corohandle_sv = newSVsv (corohandle_sv); |
144 | our->corosocket = newSVsv (corosocket); |
244 | our->corohandle = newSVsv (corohandle); |
|
|
245 | #if HAVE_EV |
|
|
246 | if (use_ev) |
|
|
247 | { |
|
|
248 | ev_io_init (&(our->rw), iocb, vio->sd, EV_READ); |
|
|
249 | ev_io_init (&(our->ww), iocb, vio->sd, EV_WRITE); |
|
|
250 | } |
|
|
251 | #endif |
145 | |
252 | |
146 | vio->desc [DESC_OFFSET - 1] = 0; |
253 | vio->desc [DESC_OFFSET - 1] = 0; |
147 | OURDATAPTR = our; |
254 | OURDATAPTR = our; |
148 | |
255 | |
|
|
256 | vio->vioclose = our_close; |
149 | vio->write = our_write; |
257 | vio->write = our_write; |
150 | vio->read = our_read; |
258 | vio->read = our_read; |
151 | } |
259 | } |
152 | |
260 | |
153 | void |
|
|
154 | _unpatch (IV sock) |
|
|
155 | CODE: |
|
|
156 | if (IN_DESTRUCT) |
|
|
157 | { |
|
|
158 | /* we currently leak data during global destruction */ |
|
|
159 | /* perl makes it extremely hard to do otherwise, though */ |
|
|
160 | MYSQL *my = (MYSQL *)sock; |
|
|
161 | Vio *vio = my->net.vio; |
|
|
162 | my_bool dummy; |
|
|
163 | |
|
|
164 | if (vio->read != our_read) |
|
|
165 | croak ("vio.read has unexpected content during unpatch - wtf?"); |
|
|
166 | |
|
|
167 | if (vio->write != our_write) |
|
|
168 | croak ("vio.write has unexpected content during unpatch - wtf?"); |
|
|
169 | |
|
|
170 | SvREFCNT_dec (OURDATAPTR->corosocket); |
|
|
171 | |
|
|
172 | Safefree (OURDATAPTR); |
|
|
173 | |
|
|
174 | vio->read = vio_read; |
|
|
175 | vio->write = vio_write; |
|
|
176 | } |
|
|
177 | |
|
|
178 | |
|
|
179 | |
|
|