… | |
… | |
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 | |
… | |
… | |
23 | |
29 | |
24 | typedef struct { |
30 | typedef struct { |
25 | int magic; |
31 | int magic; |
26 | SV *corohandle_sv, *corohandle; |
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 xlen |
34 | our_read (Vio *vio, xgptr p, int len) |
43 | our_read (Vio *vio, xgptr p, xlen 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->corohandle); |
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 | |
… | |
… | |
73 | our->bufcnt -= len; |
92 | our->bufcnt -= len; |
74 | |
93 | |
75 | return len; |
94 | return len; |
76 | } |
95 | } |
77 | |
96 | |
78 | static int |
97 | static xlen |
79 | our_write (Vio *vio, const xgptr p, int len) |
98 | our_write (Vio *vio, cxgptr p, xlen 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->corohandle); |
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; |
… | |
… | |
110 | } |
143 | } |
111 | |
144 | |
112 | static int |
145 | static int |
113 | our_close (Vio *vio) |
146 | our_close (Vio *vio) |
114 | { |
147 | { |
|
|
148 | ourdata *our = OURDATAPTR; |
|
|
149 | |
115 | if (vio->read != our_read) |
150 | if (vio->read != our_read) |
116 | croak ("vio.read has unexpected content during unpatch - wtf?"); |
151 | croak ("vio.read has unexpected content during unpatch - wtf?"); |
117 | |
152 | |
118 | if (vio->write != our_write) |
153 | if (vio->write != our_write) |
119 | croak ("vio.write has unexpected content during unpatch - wtf?"); |
154 | croak ("vio.write has unexpected content during unpatch - wtf?"); |
120 | |
155 | |
121 | if (vio->vioclose != our_close) |
156 | if (vio->vioclose != our_close) |
122 | croak ("vio.vioclose has unexpected content during unpatch - wtf?"); |
157 | croak ("vio.vioclose has unexpected content during unpatch - wtf?"); |
123 | |
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 | |
124 | SvREFCNT_dec (OURDATAPTR->corohandle); |
167 | SvREFCNT_dec (our->corohandle); |
125 | SvREFCNT_dec (OURDATAPTR->corohandle_sv); |
168 | SvREFCNT_dec (our->corohandle_sv); |
126 | |
169 | |
127 | Safefree (OURDATAPTR); |
170 | Safefree (our); |
128 | |
171 | |
129 | vio->read = vio_read; |
172 | vio->read = vio_read; |
130 | vio->write = vio_write; |
173 | vio->write = vio_write; |
131 | vio->vioclose = vio_close; |
174 | vio->vioclose = vio_close; |
132 | |
175 | |
133 | vio->vioclose (vio); |
176 | vio->vioclose (vio); |
134 | } |
177 | } |
135 | |
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 | |
136 | MODULE = Coro::Mysql PACKAGE = Coro::Mysql |
188 | MODULE = Coro::Mysql PACKAGE = Coro::Mysql |
137 | |
189 | |
138 | BOOT: |
190 | BOOT: |
139 | { |
191 | { |
140 | readable = get_cv ("Coro::Mysql::readable", 0); |
192 | readable = get_cv ("Coro::Mysql::readable", 0); |
… | |
… | |
142 | } |
194 | } |
143 | |
195 | |
144 | PROTOTYPES: ENABLE |
196 | PROTOTYPES: ENABLE |
145 | |
197 | |
146 | void |
198 | void |
|
|
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 |
147 | _patch (IV sock, int fd, SV *corohandle_sv, SV *corohandle) |
218 | _patch (IV sock, int fd, unsigned long client_version, SV *corohandle_sv, SV *corohandle) |
148 | CODE: |
219 | CODE: |
149 | { |
220 | { |
150 | MYSQL *my = (MYSQL *)sock; |
221 | MYSQL *my = (MYSQL *)sock; |
151 | Vio *vio = my->net.vio; |
222 | Vio *vio = my->net.vio; |
152 | ourdata *our; |
223 | ourdata *our; |
|
|
224 | |
|
|
225 | /* matching versions are required but not sufficient */ |
|
|
226 | if (client_version != mysql_get_client_version ()) |
|
|
227 | croak ("DBD::mysql linked against different libmysqlclient library than Coro::Mysql (%lu vs. %lu).", |
|
|
228 | client_version, mysql_get_client_version ()); |
153 | |
229 | |
154 | if (fd != my->net.fd) |
230 | if (fd != my->net.fd) |
155 | croak ("DBD::mysql fd and libmysql disagree - library mismatch, unsupported transport or API changes?"); |
231 | croak ("DBD::mysql fd and libmysql disagree - library mismatch, unsupported transport or API changes?"); |
156 | |
232 | |
157 | if (fd != vio->sd) |
233 | if (fd != vio->sd) |
… | |
… | |
169 | |
245 | |
170 | Newz (0, our, 1, ourdata); |
246 | Newz (0, our, 1, ourdata); |
171 | our->magic = CoMy_MAGIC; |
247 | our->magic = CoMy_MAGIC; |
172 | our->corohandle_sv = newSVsv (corohandle_sv); |
248 | our->corohandle_sv = newSVsv (corohandle_sv); |
173 | our->corohandle = newSVsv (corohandle); |
249 | our->corohandle = newSVsv (corohandle); |
|
|
250 | #if HAVE_EV |
|
|
251 | if (use_ev) |
|
|
252 | { |
|
|
253 | ev_io_init (&(our->rw), iocb, vio->sd, EV_READ); |
|
|
254 | ev_io_init (&(our->ww), iocb, vio->sd, EV_WRITE); |
|
|
255 | } |
|
|
256 | #endif |
174 | |
257 | |
175 | vio->desc [DESC_OFFSET - 1] = 0; |
258 | vio->desc [DESC_OFFSET - 1] = 0; |
176 | OURDATAPTR = our; |
259 | OURDATAPTR = our; |
177 | |
260 | |
178 | vio->vioclose = our_close; |
261 | vio->vioclose = our_close; |