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.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;
84poll_cb () 82poll_cb ()
85{ 83{
86 dSP; 84 dSP;
87 int count = 0; 85 int count = 0;
88 aio_req req, prv; 86 aio_req req, prv;
89
90 static int rl;//D
91 //printf ("%d ENTER\n", ++rl);//D
92 87
93 pthread_mutex_lock (&reslock); 88 pthread_mutex_lock (&reslock);
94 89
95 { 90 {
96 /* read any signals sent by the worker threads */ 91 /* read any signals sent by the worker threads */
105 pthread_mutex_unlock (&reslock); 100 pthread_mutex_unlock (&reslock);
106 101
107 while (req) 102 while (req)
108 { 103 {
109 nreqs--; 104 nreqs--;
110 //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D
111 105
112 if (req->type == REQ_QUIT) 106 if (req->type == REQ_QUIT)
113 started--; 107 started--;
114 else 108 else
115 { 109 {
121 + req->result > 0 ? req->result : 0); 115 + req->result > 0 ? req->result : 0);
122 116
123 if (req->data) 117 if (req->data)
124 SvREFCNT_dec (req->data); 118 SvREFCNT_dec (req->data);
125 119
120 if (req->fh)
121 SvREFCNT_dec (req->fh);
122
126 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)
127 { 124 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 125 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result; 126 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata); 127 PL_statcache = *(req->statdata);
131 128
132 Safefree (req->statdata); 129 Safefree (req->statdata);
133 } 130 }
134 131
132 ENTER;
135 PUSHMARK (SP); 133 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 134 XPUSHs (sv_2mortal (newSViv (req->result)));
137 135
138 if (req->type == REQ_OPEN) 136 if (req->type == REQ_OPEN)
139 { 137 {
142 140
143 PUTBACK; 141 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 142 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN; 143 SPAGAIN;
146 144
147 fh = POPs; 145 fh = SvREFCNT_inc (POPs);
148 146
149 PUSHMARK (SP); 147 PUSHMARK (SP);
150 XPUSHs (fh); 148 XPUSHs (sv_2mortal (fh));
151 } 149 }
152 150
153 if (SvOK (req->callback)) 151 if (SvOK (req->callback))
154 { 152 {
155 PUTBACK; 153 PUTBACK;
156 call_sv (req->callback, G_VOID | G_EVAL); 154 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 155 SPAGAIN;
158 } 156 }
157
158 LEAVE;
159 159
160 if (req->callback) 160 if (req->callback)
161 SvREFCNT_dec (req->callback); 161 SvREFCNT_dec (req->callback);
162 162
163 errno = errorno; 163 errno = errorno;
169 Safefree (prv); 169 Safefree (prv);
170 170
171 /* TODO: croak on errors? */ 171 /* TODO: croak on errors? */
172 } 172 }
173 173
174 //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D
175 return count; 174 return count;
176} 175}
177 176
178static void *aio_proc(void *arg); 177static void *aio_proc(void *arg);
179 178
228end_thread (void) 227end_thread (void)
229{ 228{
230 aio_req req; 229 aio_req req;
231 New (0, req, 1, aio_cb); 230 New (0, req, 1, aio_cb);
232 req->type = REQ_QUIT; 231 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 232
282 send_req (req); 233 send_req (req);
283} 234}
284 235
285static void * 236static void *
447 send_req (req); 398 send_req (req);
448} 399}
449 400
450void 401void
451aio_close(fh,callback=&PL_sv_undef) 402aio_close(fh,callback=&PL_sv_undef)
452 InputStream fh 403 SV * fh
453 SV * callback 404 SV * callback
454 PROTOTYPE: $;$ 405 PROTOTYPE: $;$
455 ALIAS: 406 ALIAS:
456 aio_close = REQ_CLOSE 407 aio_close = REQ_CLOSE
457 aio_fsync = REQ_FSYNC 408 aio_fsync = REQ_FSYNC
458 aio_fdatasync = REQ_FDATASYNC 409 aio_fdatasync = REQ_FDATASYNC
464 415
465 if (!req) 416 if (!req)
466 croak ("out of memory during aio_req allocation"); 417 croak ("out of memory during aio_req allocation");
467 418
468 req->type = ix; 419 req->type = ix;
420 req->fh = newSVsv (fh);
469 req->fd = PerlIO_fileno (fh); 421 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
470 req->callback = SvREFCNT_inc (callback); 422 req->callback = SvREFCNT_inc (callback);
471 423
472 send_req (req); 424 send_req (req);
473} 425}
474 426
475void 427void
476aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 428aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
477 InputStream fh 429 SV * fh
478 UV offset 430 UV offset
479 IV length 431 IV length
480 SV * data 432 SV * data
481 IV dataoffset 433 IV dataoffset
482 SV * callback 434 SV * callback
435 ALIAS:
436 aio_read = REQ_READ
437 aio_write = REQ_WRITE
483 PROTOTYPE: $$$$$;$ 438 PROTOTYPE: $$$$$;$
484 CODE: 439 CODE:
485 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);
486 444
487void 445 SvUPGRADE (data, SVt_PV);
488aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 446 SvPOK_on (data);
489 OutputStream fh 447
490 UV offset 448 if (dataoffset < 0)
491 IV length 449 dataoffset += svlen;
492 SV * data 450
493 IV dataoffset 451 if (dataoffset < 0 || dataoffset > svlen)
494 SV * callback 452 croak ("data offset outside of string");
495 PROTOTYPE: $$$$$;$ 453
496 CODE: 454 if (ix == REQ_WRITE)
497 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}
498 486
499void 487void
500aio_readahead(fh,offset,length,callback=&PL_sv_undef) 488aio_readahead(fh,offset,length,callback=&PL_sv_undef)
501 InputStream fh 489 SV * fh
502 UV offset 490 UV offset
503 IV length 491 IV length
504 SV * callback 492 SV * callback
505 PROTOTYPE: $$$;$ 493 PROTOTYPE: $$$;$
506 CODE: 494 CODE:
507{ 495{
508 aio_req req; 496 aio_req req;
509 497
514 502
515 if (!req) 503 if (!req)
516 croak ("out of memory during aio_req allocation"); 504 croak ("out of memory during aio_req allocation");
517 505
518 req->type = REQ_READAHEAD; 506 req->type = REQ_READAHEAD;
507 req->fh = newSVsv (fh);
519 req->fd = PerlIO_fileno (fh); 508 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
520 req->offset = offset; 509 req->offset = offset;
521 req->length = length; 510 req->length = length;
522 req->callback = SvREFCNT_inc (callback); 511 req->callback = SvREFCNT_inc (callback);
523 512
524 send_req (req); 513 send_req (req);
552 req->dataptr = SvPV_nolen (req->data); 541 req->dataptr = SvPV_nolen (req->data);
553 } 542 }
554 else 543 else
555 { 544 {
556 req->type = REQ_FSTAT; 545 req->type = REQ_FSTAT;
546 req->fh = newSVsv (fh_or_path);
557 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
558 } 548 }
559 549
560 req->callback = SvREFCNT_inc (callback); 550 req->callback = SvREFCNT_inc (callback);
561 551

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines