ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Mysql/Mysql.xs
(Generate patch)

Comparing Coro-Mysql/Mysql.xs (file contents):
Revision 1.6 by root, Wed Sep 1 16:38:07 2010 UTC vs.
Revision 1.11 by root, Sat Aug 3 02:11:27 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines