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.11 by root, Wed Jul 20 21:55:27 2005 UTC vs.
Revision 1.14 by root, Sat Jul 23 18:15:36 2005 UTC

1#define _XOPEN_SOURCE 500
2
3#include "EXTERN.h"
4#include "perl.h" 1#include "perl.h"
5#include "XSUB.h" 2#include "XSUB.h"
6 3
7#include <sys/types.h> 4#include <sys/types.h>
8#include <sys/stat.h> 5#include <sys/stat.h>
44 off_t offset; 41 off_t offset;
45 size_t length; 42 size_t length;
46 ssize_t result; 43 ssize_t result;
47 mode_t mode; /* open */ 44 mode_t mode; /* open */
48 int errorno; 45 int errorno;
49 SV *data, *callback; 46 SV *data, *callback, *fh;
50 void *dataptr; 47 void *dataptr;
51 STRLEN dataoffset; 48 STRLEN dataoffset;
52 49
53 Stat_t *statdata; 50 Stat_t *statdata;
54} aio_cb; 51} aio_cb;
84poll_cb () 81poll_cb ()
85{ 82{
86 dSP; 83 dSP;
87 int count = 0; 84 int count = 0;
88 aio_req req, prv; 85 aio_req req, prv;
89
90 static int rl;//D
91 //printf ("%d ENTER\n", ++rl);//D
92 86
93 pthread_mutex_lock (&reslock); 87 pthread_mutex_lock (&reslock);
94 88
95 { 89 {
96 /* read any signals sent by the worker threads */ 90 /* read any signals sent by the worker threads */
105 pthread_mutex_unlock (&reslock); 99 pthread_mutex_unlock (&reslock);
106 100
107 while (req) 101 while (req)
108 { 102 {
109 nreqs--; 103 nreqs--;
110 //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D
111 104
112 if (req->type == REQ_QUIT) 105 if (req->type == REQ_QUIT)
113 started--; 106 started--;
114 else 107 else
115 { 108 {
121 + req->result > 0 ? req->result : 0); 114 + req->result > 0 ? req->result : 0);
122 115
123 if (req->data) 116 if (req->data)
124 SvREFCNT_dec (req->data); 117 SvREFCNT_dec (req->data);
125 118
119 if (req->fh)
120 SvREFCNT_dec (req->fh);
121
126 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 122 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
127 { 123 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 124 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result; 125 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata); 126 PL_statcache = *(req->statdata);
131 127
132 Safefree (req->statdata); 128 Safefree (req->statdata);
133 } 129 }
134 130
131 ENTER;
135 PUSHMARK (SP); 132 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 133 XPUSHs (sv_2mortal (newSViv (req->result)));
137 134
138 if (req->type == REQ_OPEN) 135 if (req->type == REQ_OPEN)
139 { 136 {
142 139
143 PUTBACK; 140 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 141 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN; 142 SPAGAIN;
146 143
147 fh = POPs; 144 fh = SvREFCNT_inc (POPs);
148 145
149 PUSHMARK (SP); 146 PUSHMARK (SP);
150 XPUSHs (fh); 147 XPUSHs (sv_2mortal (fh));
151 } 148 }
152 149
153 if (SvOK (req->callback)) 150 if (SvOK (req->callback))
154 { 151 {
155 PUTBACK; 152 PUTBACK;
156 call_sv (req->callback, G_VOID | G_EVAL); 153 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 154 SPAGAIN;
158 } 155 }
156
157 LEAVE;
159 158
160 if (req->callback) 159 if (req->callback)
161 SvREFCNT_dec (req->callback); 160 SvREFCNT_dec (req->callback);
162 161
163 errno = errorno; 162 errno = errorno;
169 Safefree (prv); 168 Safefree (prv);
170 169
171 /* TODO: croak on errors? */ 170 /* TODO: croak on errors? */
172 } 171 }
173 172
174 //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D
175 return count; 173 return count;
176} 174}
177 175
178static void *aio_proc(void *arg); 176static void *aio_proc(void *arg);
179 177
228end_thread (void) 226end_thread (void)
229{ 227{
230 aio_req req; 228 aio_req req;
231 New (0, req, 1, aio_cb); 229 New (0, req, 1, aio_cb);
232 req->type = REQ_QUIT; 230 req->type = REQ_QUIT;
233
234 send_req (req);
235}
236
237static void
238read_write (int dowrite, int fd, off_t offset, size_t length,
239 SV *data, STRLEN dataoffset, SV *callback)
240{
241 aio_req req;
242 STRLEN svlen;
243 char *svptr = SvPV (data, svlen);
244
245 SvUPGRADE (data, SVt_PV);
246 SvPOK_on (data);
247
248 if (dataoffset < 0)
249 dataoffset += svlen;
250
251 if (dataoffset < 0 || dataoffset > svlen)
252 croak ("data offset outside of string");
253
254 if (dowrite)
255 {
256 /* write: check length and adjust. */
257 if (length < 0 || length + dataoffset > svlen)
258 length = svlen - dataoffset;
259 }
260 else
261 {
262 /* read: grow scalar as necessary */
263 svptr = SvGROW (data, length + dataoffset);
264 }
265
266 if (length < 0)
267 croak ("length must not be negative");
268
269 Newz (0, req, 1, aio_cb);
270
271 if (!req)
272 croak ("out of memory during aio_req allocation");
273
274 req->type = dowrite ? REQ_WRITE : REQ_READ;
275 req->fd = fd;
276 req->offset = offset;
277 req->length = length;
278 req->data = SvREFCNT_inc (data);
279 req->dataptr = (char *)svptr + dataoffset;
280 req->callback = SvREFCNT_inc (callback);
281 231
282 send_req (req); 232 send_req (req);
283} 233}
284 234
285static void * 235static void *
447 send_req (req); 397 send_req (req);
448} 398}
449 399
450void 400void
451aio_close(fh,callback=&PL_sv_undef) 401aio_close(fh,callback=&PL_sv_undef)
452 InputStream fh 402 SV * fh
453 SV * callback 403 SV * callback
454 PROTOTYPE: $;$ 404 PROTOTYPE: $;$
455 ALIAS: 405 ALIAS:
456 aio_close = REQ_CLOSE 406 aio_close = REQ_CLOSE
457 aio_fsync = REQ_FSYNC 407 aio_fsync = REQ_FSYNC
458 aio_fdatasync = REQ_FDATASYNC 408 aio_fdatasync = REQ_FDATASYNC
464 414
465 if (!req) 415 if (!req)
466 croak ("out of memory during aio_req allocation"); 416 croak ("out of memory during aio_req allocation");
467 417
468 req->type = ix; 418 req->type = ix;
419 req->fh = newSVsv (fh);
469 req->fd = PerlIO_fileno (fh); 420 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
470 req->callback = SvREFCNT_inc (callback); 421 req->callback = SvREFCNT_inc (callback);
471 422
472 send_req (req); 423 send_req (req);
473} 424}
474 425
475void 426void
476aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 427aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
477 InputStream fh 428 SV * fh
478 UV offset 429 UV offset
479 IV length 430 IV length
480 SV * data 431 SV * data
481 IV dataoffset 432 IV dataoffset
482 SV * callback 433 SV * callback
434 ALIAS:
435 aio_read = REQ_READ
436 aio_write = REQ_WRITE
483 PROTOTYPE: $$$$$;$ 437 PROTOTYPE: $$$$$;$
484 CODE: 438 CODE:
485 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 439{
440 aio_req req;
441 STRLEN svlen;
442 char *svptr = SvPV (data, svlen);
486 443
487void 444 SvUPGRADE (data, SVt_PV);
488aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 445 SvPOK_on (data);
489 OutputStream fh 446
490 UV offset 447 if (dataoffset < 0)
491 IV length 448 dataoffset += svlen;
492 SV * data 449
493 IV dataoffset 450 if (dataoffset < 0 || dataoffset > svlen)
494 SV * callback 451 croak ("data offset outside of string");
495 PROTOTYPE: $$$$$;$ 452
496 CODE: 453 if (ix == REQ_WRITE)
497 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 454 {
455 /* write: check length and adjust. */
456 if (length < 0 || length + dataoffset > svlen)
457 length = svlen - dataoffset;
458 }
459 else
460 {
461 /* read: grow scalar as necessary */
462 svptr = SvGROW (data, length + dataoffset);
463 }
464
465 if (length < 0)
466 croak ("length must not be negative");
467
468 Newz (0, req, 1, aio_cb);
469
470 if (!req)
471 croak ("out of memory during aio_req allocation");
472
473 req->type = ix;
474 req->fh = newSVsv (fh);
475 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
476 : IoOFP (sv_2io (fh)));
477 req->offset = offset;
478 req->length = length;
479 req->data = SvREFCNT_inc (data);
480 req->dataptr = (char *)svptr + dataoffset;
481 req->callback = SvREFCNT_inc (callback);
482
483 send_req (req);
484}
498 485
499void 486void
500aio_readahead(fh,offset,length,callback=&PL_sv_undef) 487aio_readahead(fh,offset,length,callback=&PL_sv_undef)
501 InputStream fh 488 SV * fh
502 UV offset 489 UV offset
503 IV length 490 IV length
504 SV * callback 491 SV * callback
505 PROTOTYPE: $$$;$ 492 PROTOTYPE: $$$;$
506 CODE: 493 CODE:
507{ 494{
508 aio_req req; 495 aio_req req;
509 496
514 501
515 if (!req) 502 if (!req)
516 croak ("out of memory during aio_req allocation"); 503 croak ("out of memory during aio_req allocation");
517 504
518 req->type = REQ_READAHEAD; 505 req->type = REQ_READAHEAD;
506 req->fh = newSVsv (fh);
519 req->fd = PerlIO_fileno (fh); 507 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
520 req->offset = offset; 508 req->offset = offset;
521 req->length = length; 509 req->length = length;
522 req->callback = SvREFCNT_inc (callback); 510 req->callback = SvREFCNT_inc (callback);
523 511
524 send_req (req); 512 send_req (req);
552 req->dataptr = SvPV_nolen (req->data); 540 req->dataptr = SvPV_nolen (req->data);
553 } 541 }
554 else 542 else
555 { 543 {
556 req->type = REQ_FSTAT; 544 req->type = REQ_FSTAT;
545 req->fh = newSVsv (fh_or_path);
557 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 546 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
558 } 547 }
559 548
560 req->callback = SvREFCNT_inc (callback); 549 req->callback = SvREFCNT_inc (callback);
561 550

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines