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

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.12 by root, Wed Jul 20 21:57:04 2005 UTC vs.
Revision 1.15 by root, Sat Jul 23 18:19:56 2005 UTC

1#define _XOPEN_SOURCE 500
2
3#include "EXTERN.h" 1#include "EXTERN.h"
4#include "perl.h" 2#include "perl.h"
5#include "XSUB.h" 3#include "XSUB.h"
6 4
7#include <sys/types.h> 5#include <sys/types.h>
44 off_t offset; 42 off_t offset;
45 size_t length; 43 size_t length;
46 ssize_t result; 44 ssize_t result;
47 mode_t mode; /* open */ 45 mode_t mode; /* open */
48 int errorno; 46 int errorno;
49 SV *data, *callback; 47 SV *data, *callback, *fh;
50 void *dataptr; 48 void *dataptr;
51 STRLEN dataoffset; 49 STRLEN dataoffset;
52 50
53 Stat_t *statdata; 51 Stat_t *statdata;
54} aio_cb; 52} aio_cb;
117 + req->result > 0 ? req->result : 0); 115 + req->result > 0 ? req->result : 0);
118 116
119 if (req->data) 117 if (req->data)
120 SvREFCNT_dec (req->data); 118 SvREFCNT_dec (req->data);
121 119
120 if (req->fh)
121 SvREFCNT_dec (req->fh);
122
122 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 123 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
123 { 124 {
124 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 125 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
125 PL_laststatval = req->result; 126 PL_laststatval = req->result;
126 PL_statcache = *(req->statdata); 127 PL_statcache = *(req->statdata);
127 128
128 Safefree (req->statdata); 129 Safefree (req->statdata);
129 } 130 }
130 131
132 ENTER;
131 PUSHMARK (SP); 133 PUSHMARK (SP);
132 XPUSHs (sv_2mortal (newSViv (req->result))); 134 XPUSHs (sv_2mortal (newSViv (req->result)));
133 135
134 if (req->type == REQ_OPEN) 136 if (req->type == REQ_OPEN)
135 { 137 {
138 140
139 PUTBACK; 141 PUTBACK;
140 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 142 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
141 SPAGAIN; 143 SPAGAIN;
142 144
143 fh = POPs; 145 fh = SvREFCNT_inc (POPs);
144 146
145 PUSHMARK (SP); 147 PUSHMARK (SP);
146 XPUSHs (fh); 148 XPUSHs (sv_2mortal (fh));
147 } 149 }
148 150
149 if (SvOK (req->callback)) 151 if (SvOK (req->callback))
150 { 152 {
151 PUTBACK; 153 PUTBACK;
152 call_sv (req->callback, G_VOID | G_EVAL); 154 call_sv (req->callback, G_VOID | G_EVAL);
153 SPAGAIN; 155 SPAGAIN;
154 } 156 }
157
158 LEAVE;
155 159
156 if (req->callback) 160 if (req->callback)
157 SvREFCNT_dec (req->callback); 161 SvREFCNT_dec (req->callback);
158 162
159 errno = errorno; 163 errno = errorno;
223end_thread (void) 227end_thread (void)
224{ 228{
225 aio_req req; 229 aio_req req;
226 New (0, req, 1, aio_cb); 230 New (0, req, 1, aio_cb);
227 req->type = REQ_QUIT; 231 req->type = REQ_QUIT;
228
229 send_req (req);
230}
231
232static void
233read_write (int dowrite, int fd, off_t offset, size_t length,
234 SV *data, STRLEN dataoffset, SV *callback)
235{
236 aio_req req;
237 STRLEN svlen;
238 char *svptr = SvPV (data, svlen);
239
240 SvUPGRADE (data, SVt_PV);
241 SvPOK_on (data);
242
243 if (dataoffset < 0)
244 dataoffset += svlen;
245
246 if (dataoffset < 0 || dataoffset > svlen)
247 croak ("data offset outside of string");
248
249 if (dowrite)
250 {
251 /* write: check length and adjust. */
252 if (length < 0 || length + dataoffset > svlen)
253 length = svlen - dataoffset;
254 }
255 else
256 {
257 /* read: grow scalar as necessary */
258 svptr = SvGROW (data, length + dataoffset);
259 }
260
261 if (length < 0)
262 croak ("length must not be negative");
263
264 Newz (0, req, 1, aio_cb);
265
266 if (!req)
267 croak ("out of memory during aio_req allocation");
268
269 req->type = dowrite ? REQ_WRITE : REQ_READ;
270 req->fd = fd;
271 req->offset = offset;
272 req->length = length;
273 req->data = SvREFCNT_inc (data);
274 req->dataptr = (char *)svptr + dataoffset;
275 req->callback = SvREFCNT_inc (callback);
276 232
277 send_req (req); 233 send_req (req);
278} 234}
279 235
280static void * 236static void *
442 send_req (req); 398 send_req (req);
443} 399}
444 400
445void 401void
446aio_close(fh,callback=&PL_sv_undef) 402aio_close(fh,callback=&PL_sv_undef)
447 InputStream fh 403 SV * fh
448 SV * callback 404 SV * callback
449 PROTOTYPE: $;$ 405 PROTOTYPE: $;$
450 ALIAS: 406 ALIAS:
451 aio_close = REQ_CLOSE 407 aio_close = REQ_CLOSE
452 aio_fsync = REQ_FSYNC 408 aio_fsync = REQ_FSYNC
453 aio_fdatasync = REQ_FDATASYNC 409 aio_fdatasync = REQ_FDATASYNC
459 415
460 if (!req) 416 if (!req)
461 croak ("out of memory during aio_req allocation"); 417 croak ("out of memory during aio_req allocation");
462 418
463 req->type = ix; 419 req->type = ix;
420 req->fh = newSVsv (fh);
464 req->fd = PerlIO_fileno (fh); 421 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
465 req->callback = SvREFCNT_inc (callback); 422 req->callback = SvREFCNT_inc (callback);
466 423
467 send_req (req); 424 send_req (req);
468} 425}
469 426
470void 427void
471aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 428aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
472 InputStream fh 429 SV * fh
473 UV offset 430 UV offset
474 IV length 431 IV length
475 SV * data 432 SV * data
476 IV dataoffset 433 IV dataoffset
477 SV * callback 434 SV * callback
435 ALIAS:
436 aio_read = REQ_READ
437 aio_write = REQ_WRITE
478 PROTOTYPE: $$$$$;$ 438 PROTOTYPE: $$$$$;$
479 CODE: 439 CODE:
480 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 440{
441 aio_req req;
442 STRLEN svlen;
443 char *svptr = SvPV (data, svlen);
481 444
482void 445 SvUPGRADE (data, SVt_PV);
483aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 446 SvPOK_on (data);
484 OutputStream fh 447
485 UV offset 448 if (dataoffset < 0)
486 IV length 449 dataoffset += svlen;
487 SV * data 450
488 IV dataoffset 451 if (dataoffset < 0 || dataoffset > svlen)
489 SV * callback 452 croak ("data offset outside of string");
490 PROTOTYPE: $$$$$;$ 453
491 CODE: 454 if (ix == REQ_WRITE)
492 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 455 {
456 /* write: check length and adjust. */
457 if (length < 0 || length + dataoffset > svlen)
458 length = svlen - dataoffset;
459 }
460 else
461 {
462 /* read: grow scalar as necessary */
463 svptr = SvGROW (data, length + dataoffset);
464 }
465
466 if (length < 0)
467 croak ("length must not be negative");
468
469 Newz (0, req, 1, aio_cb);
470
471 if (!req)
472 croak ("out of memory during aio_req allocation");
473
474 req->type = ix;
475 req->fh = newSVsv (fh);
476 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
477 : IoOFP (sv_2io (fh)));
478 req->offset = offset;
479 req->length = length;
480 req->data = SvREFCNT_inc (data);
481 req->dataptr = (char *)svptr + dataoffset;
482 req->callback = SvREFCNT_inc (callback);
483
484 send_req (req);
485}
493 486
494void 487void
495aio_readahead(fh,offset,length,callback=&PL_sv_undef) 488aio_readahead(fh,offset,length,callback=&PL_sv_undef)
496 InputStream fh 489 SV * fh
497 UV offset 490 UV offset
498 IV length 491 IV length
499 SV * callback 492 SV * callback
500 PROTOTYPE: $$$;$ 493 PROTOTYPE: $$$;$
501 CODE: 494 CODE:
502{ 495{
503 aio_req req; 496 aio_req req;
504 497
509 502
510 if (!req) 503 if (!req)
511 croak ("out of memory during aio_req allocation"); 504 croak ("out of memory during aio_req allocation");
512 505
513 req->type = REQ_READAHEAD; 506 req->type = REQ_READAHEAD;
507 req->fh = newSVsv (fh);
514 req->fd = PerlIO_fileno (fh); 508 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
515 req->offset = offset; 509 req->offset = offset;
516 req->length = length; 510 req->length = length;
517 req->callback = SvREFCNT_inc (callback); 511 req->callback = SvREFCNT_inc (callback);
518 512
519 send_req (req); 513 send_req (req);
547 req->dataptr = SvPV_nolen (req->data); 541 req->dataptr = SvPV_nolen (req->data);
548 } 542 }
549 else 543 else
550 { 544 {
551 req->type = REQ_FSTAT; 545 req->type = REQ_FSTAT;
546 req->fh = newSVsv (fh_or_path);
552 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
553 } 548 }
554 549
555 req->callback = SvREFCNT_inc (callback); 550 req->callback = SvREFCNT_inc (callback);
556 551

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines