1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <sys/types.h> |
5 | #include <sys/types.h> |
|
|
6 | #include <sys/stat.h> |
6 | #include <unistd.h> |
7 | #include <unistd.h> |
7 | #include <fcntl.h> |
8 | #include <fcntl.h> |
8 | #include <signal.h> |
9 | #include <signal.h> |
9 | #include <sched.h> |
10 | #include <sched.h> |
10 | |
11 | |
|
|
12 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
13 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
14 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
15 | |
11 | #define STACKSIZE 1024 /* yeah */ |
16 | #define STACKSIZE 1024 /* yeah */ |
12 | |
17 | |
13 | enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE }; |
18 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
14 | |
19 | |
15 | typedef struct { |
20 | typedef struct { |
16 | char stack[STACKSIZE]; |
21 | char stack[STACKSIZE]; |
17 | } aio_thread; |
22 | } aio_thread; |
18 | |
23 | |
19 | typedef struct { |
24 | typedef struct { |
20 | int type; |
25 | int type; |
21 | aio_thread *thread; |
26 | aio_thread *thread; |
22 | |
27 | |
23 | /* read/write */ |
28 | SV *savesv; |
|
|
29 | |
24 | int fd; |
30 | int fd; |
25 | off_t offset; |
31 | off_t offset; |
26 | size_t length; |
32 | size_t length; |
27 | ssize_t result; |
33 | ssize_t result; |
28 | mode_t mode; /* open */ |
34 | mode_t mode; /* open */ |
29 | int errorno; |
35 | int errorno; |
30 | SV *data, *callback; |
36 | SV *data, *callback; |
31 | void *dataptr; |
37 | void *dataptr; |
32 | STRLEN dataoffset; |
38 | STRLEN dataoffset; |
|
|
39 | |
|
|
40 | struct stat64 *statdata; |
33 | } aio_cb; |
41 | } aio_cb; |
34 | |
42 | |
35 | typedef aio_cb *aio_req; |
43 | typedef aio_cb *aio_req; |
36 | |
44 | |
37 | static int started; |
45 | static int started; |
… | |
… | |
77 | SV *data, STRLEN dataoffset, SV*callback) |
85 | SV *data, STRLEN dataoffset, SV*callback) |
78 | { |
86 | { |
79 | aio_req req; |
87 | aio_req req; |
80 | STRLEN svlen; |
88 | STRLEN svlen; |
81 | char *svptr = SvPV (data, svlen); |
89 | char *svptr = SvPV (data, svlen); |
|
|
90 | |
|
|
91 | SvUPGRADE (data, SVt_PV); |
|
|
92 | SvPOK_on (data); |
82 | |
93 | |
83 | if (dataoffset < 0) |
94 | if (dataoffset < 0) |
84 | dataoffset += svlen; |
95 | dataoffset += svlen; |
85 | |
96 | |
86 | if (dataoffset < 0 || dataoffset > svlen) |
97 | if (dataoffset < 0 || dataoffset > svlen) |
… | |
… | |
134 | else |
145 | else |
135 | { |
146 | { |
136 | int errorno = errno; |
147 | int errorno = errno; |
137 | errno = req->errorno; |
148 | errno = req->errorno; |
138 | |
149 | |
|
|
150 | if (req->savesv) |
|
|
151 | SvREFCNT_dec (req->savesv); |
|
|
152 | |
139 | if (req->type == REQ_READ) |
153 | if (req->type == REQ_READ) |
140 | SvCUR_set (req->data, req->dataoffset |
154 | SvCUR_set (req->data, req->dataoffset |
141 | + req->result > 0 ? req->result : 0); |
155 | + req->result > 0 ? req->result : 0); |
|
|
156 | |
|
|
157 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
158 | { |
|
|
159 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
160 | PL_laststatval = req->result; |
|
|
161 | PL_statcache.st_dev = req->statdata->st_dev; |
|
|
162 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
163 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
164 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
165 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
166 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
167 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
168 | PL_statcache.st_size = req->statdata->st_size; |
|
|
169 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
170 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
171 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
172 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
173 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
174 | |
|
|
175 | Safefree (req->statdata); |
|
|
176 | } |
142 | |
177 | |
143 | PUSHMARK (SP); |
178 | PUSHMARK (SP); |
144 | XPUSHs (sv_2mortal (newSViv (req->result))); |
179 | XPUSHs (sv_2mortal (newSViv (req->result))); |
145 | PUTBACK; |
180 | PUTBACK; |
146 | call_sv (req->callback, G_VOID); |
181 | call_sv (req->callback, G_VOID); |
… | |
… | |
171 | aio_thread *thr = thr_arg; |
206 | aio_thread *thr = thr_arg; |
172 | aio_req req; |
207 | aio_req req; |
173 | int errno; |
208 | int errno; |
174 | |
209 | |
175 | /* we rely on gcc's ability to create closures. */ |
210 | /* we rely on gcc's ability to create closures. */ |
176 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence) |
|
|
177 | _syscall3(int,read,int,fd,char *,buf,off_t,count) |
211 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
178 | _syscall3(int,write,int,fd,char *,buf,off_t,count) |
212 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
|
|
213 | |
179 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
214 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
180 | _syscall1(int,close,int,fd) |
215 | _syscall1(int,close,int,fd) |
|
|
216 | |
|
|
217 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) |
|
|
218 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) |
|
|
219 | |
|
|
220 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
|
|
221 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
|
|
222 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
181 | |
223 | |
182 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
224 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
183 | |
225 | |
184 | /* then loop */ |
226 | /* then loop */ |
185 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
227 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
186 | { |
228 | { |
187 | req->thread = thr; |
229 | req->thread = thr; |
188 | errno = 0; |
230 | errno = 0; |
189 | |
231 | |
190 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
|
|
191 | { |
|
|
192 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
|
|
193 | { |
|
|
194 | if (req->type == REQ_READ) |
232 | if (req->type == REQ_READ) |
195 | req->result = read (req->fd, req->dataptr, req->length); |
233 | req->result = pread (req->fd, req->dataptr, req->length, req->offset); |
196 | else |
234 | else if (req->type == REQ_WRITE) |
197 | req->result = write(req->fd, req->dataptr, req->length); |
235 | req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); |
198 | } |
|
|
199 | } |
|
|
200 | else if (req->type == REQ_OPEN) |
236 | else if (req->type == REQ_OPEN) |
201 | { |
|
|
202 | req->result = open (req->dataptr, req->fd, req->mode); |
237 | req->result = open (req->dataptr, req->fd, req->mode); |
203 | } |
|
|
204 | else if (req->type == REQ_CLOSE) |
238 | else if (req->type == REQ_CLOSE) |
205 | { |
|
|
206 | req->result = close (req->fd); |
239 | req->result = close (req->fd); |
207 | } |
240 | else if (req->type == REQ_STAT) |
|
|
241 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
242 | else if (req->type == REQ_LSTAT) |
|
|
243 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
244 | else if (req->type == REQ_FSTAT) |
|
|
245 | req->result = fstat64 (req->fd, req->statdata); |
208 | else |
246 | else |
209 | { |
247 | { |
210 | write (respipe[1], (void *)&req, sizeof (req)); |
248 | write (respipe[1], (void *)&req, sizeof (req)); |
211 | break; |
249 | break; |
212 | } |
250 | } |
… | |
… | |
257 | |
295 | |
258 | poll_cb (); |
296 | poll_cb (); |
259 | while (started > nthreads) |
297 | while (started > nthreads) |
260 | { |
298 | { |
261 | sched_yield (); |
299 | sched_yield (); |
|
|
300 | fcntl (respipe[0], F_SETFL, 0); |
262 | poll_cb (); |
301 | poll_cb (); |
|
|
302 | fcntl (respipe[0], F_SETFL, O_NONBLOCK); |
263 | } |
303 | } |
264 | |
304 | |
265 | void |
305 | void |
266 | aio_read(fh,offset,length,data,dataoffset,callback) |
|
|
267 | PerlIO * fh |
|
|
268 | UV offset |
|
|
269 | STRLEN length |
|
|
270 | SV * data |
|
|
271 | STRLEN dataoffset |
|
|
272 | SV * callback |
|
|
273 | PROTOTYPE: $$$$$$ |
|
|
274 | ALIAS: |
|
|
275 | aio_write = 1 |
|
|
276 | CODE: |
|
|
277 | SvUPGRADE (data, SVt_PV); |
|
|
278 | SvPOK_on (data); |
|
|
279 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
280 | |
|
|
281 | void |
|
|
282 | aio_open(pathname,flags,mode,callback) |
306 | aio_open(pathname,flags,mode,callback) |
283 | char * pathname |
307 | SV * pathname |
284 | int flags |
308 | int flags |
285 | int mode |
309 | int mode |
286 | SV * callback |
310 | SV * callback |
287 | PROTOTYPE: $$$$ |
311 | PROTOTYPE: $$$$ |
288 | CODE: |
312 | CODE: |
… | |
… | |
292 | |
316 | |
293 | if (!req) |
317 | if (!req) |
294 | croak ("out of memory during aio_req allocation"); |
318 | croak ("out of memory during aio_req allocation"); |
295 | |
319 | |
296 | req->type = REQ_OPEN; |
320 | req->type = REQ_OPEN; |
297 | req->dataptr = pathname; |
321 | req->savesv = newSVsv (pathname); |
|
|
322 | req->dataptr = SvPV_nolen (req->savesv); |
298 | req->fd = flags; |
323 | req->fd = flags; |
299 | req->mode = mode; |
324 | req->mode = mode; |
300 | req->callback = SvREFCNT_inc (callback); |
325 | req->callback = SvREFCNT_inc (callback); |
301 | |
326 | |
302 | send_req (req); |
327 | send_req (req); |
303 | |
328 | |
304 | void |
329 | void |
305 | aio_close(fh,callback) |
330 | aio_close(fh,callback) |
306 | PerlIO * fh |
331 | InputStream fh |
307 | SV * callback |
332 | SV * callback |
308 | PROTOTYPE: $ |
333 | PROTOTYPE: $ |
309 | CODE: |
334 | CODE: |
310 | aio_req req; |
335 | aio_req req; |
311 | |
336 | |
… | |
… | |
318 | req->fd = PerlIO_fileno (fh); |
343 | req->fd = PerlIO_fileno (fh); |
319 | req->callback = SvREFCNT_inc (callback); |
344 | req->callback = SvREFCNT_inc (callback); |
320 | |
345 | |
321 | send_req (req); |
346 | send_req (req); |
322 | |
347 | |
|
|
348 | void |
|
|
349 | aio_read(fh,offset,length,data,dataoffset,callback) |
|
|
350 | InputStream fh |
|
|
351 | UV offset |
|
|
352 | IV length |
|
|
353 | SV * data |
|
|
354 | IV dataoffset |
|
|
355 | SV * callback |
|
|
356 | PROTOTYPE: $$$$$$ |
|
|
357 | CODE: |
|
|
358 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
359 | |
|
|
360 | void |
|
|
361 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
362 | OutputStream fh |
|
|
363 | UV offset |
|
|
364 | IV length |
|
|
365 | SV * data |
|
|
366 | IV dataoffset |
|
|
367 | SV * callback |
|
|
368 | PROTOTYPE: $$$$$$ |
|
|
369 | CODE: |
|
|
370 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
371 | |
|
|
372 | void |
|
|
373 | aio_stat(fh_or_path,callback) |
|
|
374 | SV * fh_or_path |
|
|
375 | SV * callback |
|
|
376 | PROTOTYPE: $$ |
|
|
377 | ALIAS: |
|
|
378 | aio_lstat = 1 |
|
|
379 | CODE: |
|
|
380 | aio_req req; |
|
|
381 | |
|
|
382 | New (0, req, 1, aio_cb); |
|
|
383 | |
|
|
384 | if (!req) |
|
|
385 | croak ("out of memory during aio_req allocation"); |
|
|
386 | |
|
|
387 | New (0, req->statdata, 1, struct stat64); |
|
|
388 | |
|
|
389 | if (!req->statdata) |
|
|
390 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
391 | |
|
|
392 | if (SvPOK (fh_or_path)) |
|
|
393 | { |
|
|
394 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
|
|
395 | req->savesv = newSVsv (fh_or_path); |
|
|
396 | req->dataptr = SvPV_nolen (req->savesv); |
|
|
397 | } |
|
|
398 | else |
|
|
399 | { |
|
|
400 | req->type = REQ_FSTAT; |
|
|
401 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
402 | } |
|
|
403 | |
|
|
404 | req->callback = SvREFCNT_inc (callback); |
|
|
405 | |
|
|
406 | send_req (req); |
|
|
407 | |
323 | int |
408 | int |
324 | poll_fileno() |
409 | poll_fileno() |
325 | PROTOTYPE: |
410 | PROTOTYPE: |
326 | CODE: |
411 | CODE: |
327 | RETVAL = respipe[0]; |
412 | RETVAL = respipe[0]; |