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.6 by root, Mon Jul 11 01:03:17 2005 UTC vs.
Revision 1.15 by root, Sat Jul 23 18:19:56 2005 UTC

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 <sys/stat.h>
7
7#include <unistd.h> 8#include <unistd.h>
8#include <fcntl.h> 9#include <fcntl.h>
9#include <signal.h> 10#include <signal.h>
10#include <sched.h> 11#include <sched.h>
11#include <endian.h> 12#if __linux
13#include <sys/syscall.h>
14#endif
12 15
13#include <pthread.h> 16#include <pthread.h>
14#include <sys/syscall.h>
15 17
16typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 18typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 19typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
18typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
19 21
40 off_t offset; 42 off_t offset;
41 size_t length; 43 size_t length;
42 ssize_t result; 44 ssize_t result;
43 mode_t mode; /* open */ 45 mode_t mode; /* open */
44 int errorno; 46 int errorno;
45 SV *data, *callback; 47 SV *data, *callback, *fh;
46 void *dataptr; 48 void *dataptr;
47 STRLEN dataoffset; 49 STRLEN dataoffset;
48 50
49 Stat_t *statdata; 51 Stat_t *statdata;
50} aio_cb; 52} aio_cb;
51 53
52typedef aio_cb *aio_req; 54typedef aio_cb *aio_req;
53 55
54static int started; 56static int started;
55static int nreqs; 57static volatile int nreqs;
56static int max_outstanding = 1<<30; 58static int max_outstanding = 1<<30;
57static int respipe [2]; 59static int respipe [2];
58 60
59static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 61static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 62static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
64static volatile aio_req ress, rese; /* queue start, queue end */ 66static volatile aio_req ress, rese; /* queue start, queue end */
65 67
66static void 68static void
67poll_wait () 69poll_wait ()
68{ 70{
69 if (!nreqs) 71 if (nreqs && !ress)
70 return; 72 {
71
72 fd_set rfd; 73 fd_set rfd;
73 FD_ZERO(&rfd); 74 FD_ZERO(&rfd);
74 FD_SET(respipe [0], &rfd); 75 FD_SET(respipe [0], &rfd);
75 76
76 select (respipe [0] + 1, &rfd, 0, 0, 0); 77 select (respipe [0] + 1, &rfd, 0, 0, 0);
78 }
77} 79}
78 80
79static int 81static int
80poll_cb () 82poll_cb ()
81{ 83{
82 dSP; 84 dSP;
83 int count = 0; 85 int count = 0;
84 aio_req req; 86 aio_req req, prv;
85 87
88 pthread_mutex_lock (&reslock);
89
86 { 90 {
87 /* read and signals sent by the worker threads */ 91 /* read any signals sent by the worker threads */
88 char buf [32]; 92 char buf [32];
89 while (read (respipe [0], buf, 32) > 0) 93 while (read (respipe [0], buf, 32) > 0)
90 ; 94 ;
91 } 95 }
92 96
93 for (;;) 97 req = ress;
98 ress = rese = 0;
99
100 pthread_mutex_unlock (&reslock);
101
102 while (req)
94 { 103 {
95 pthread_mutex_lock (&reslock);
96
97 req = ress;
98
99 if (ress)
100 {
101 ress = ress->next;
102 if (!ress) rese = 0;
103 }
104
105 pthread_mutex_unlock (&reslock);
106
107 if (!req)
108 break;
109
110 nreqs--; 104 nreqs--;
111 105
112 if (req->type == REQ_QUIT) 106 if (req->type == REQ_QUIT)
113 started--; 107 started--;
114 else 108 else
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
151 if (SvOK (req->callback))
152 {
153 PUTBACK; 153 PUTBACK;
154 call_sv (req->callback, G_VOID | G_EVAL); 154 call_sv (req->callback, G_VOID | G_EVAL);
155 SPAGAIN; 155 SPAGAIN;
156 }
157
158 LEAVE;
156 159
157 if (req->callback) 160 if (req->callback)
158 SvREFCNT_dec (req->callback); 161 SvREFCNT_dec (req->callback);
159 162
160 errno = errorno; 163 errno = errorno;
161 count++; 164 count++;
162 } 165 }
163 166
167 prv = req;
168 req = req->next;
164 Safefree (req); 169 Safefree (prv);
170
171 /* TODO: croak on errors? */
165 } 172 }
166 173
167 return count; 174 return count;
168} 175}
169 176
224 req->type = REQ_QUIT; 231 req->type = REQ_QUIT;
225 232
226 send_req (req); 233 send_req (req);
227} 234}
228 235
229static void
230read_write (int dowrite, int fd, off_t offset, size_t length,
231 SV *data, STRLEN dataoffset, SV *callback)
232{
233 aio_req req;
234 STRLEN svlen;
235 char *svptr = SvPV (data, svlen);
236
237 SvUPGRADE (data, SVt_PV);
238 SvPOK_on (data);
239
240 if (dataoffset < 0)
241 dataoffset += svlen;
242
243 if (dataoffset < 0 || dataoffset > svlen)
244 croak ("data offset outside of string");
245
246 if (dowrite)
247 {
248 /* write: check length and adjust. */
249 if (length < 0 || length + dataoffset > svlen)
250 length = svlen - dataoffset;
251 }
252 else
253 {
254 /* read: grow scalar as necessary */
255 svptr = SvGROW (data, length + dataoffset);
256 }
257
258 if (length < 0)
259 croak ("length must not be negative");
260
261 Newz (0, req, 1, aio_cb);
262
263 if (!req)
264 croak ("out of memory during aio_req allocation");
265
266 req->type = dowrite ? REQ_WRITE : REQ_READ;
267 req->fd = fd;
268 req->offset = offset;
269 req->length = length;
270 req->data = SvREFCNT_inc (data);
271 req->dataptr = (char *)svptr + dataoffset;
272 req->callback = SvREFCNT_inc (callback);
273
274 send_req (req);
275}
276
277static void * 236static void *
278aio_proc (void *thr_arg) 237aio_proc (void *thr_arg)
279{ 238{
280 aio_req req; 239 aio_req req;
281 int type; 240 int type;
306 265
307 type = req->type; 266 type = req->type;
308 267
309 switch (type) 268 switch (type)
310 { 269 {
311 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 270 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
312 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; 271 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
313#if SYS_readahead 272#if SYS_readahead
314 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 273 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
315#else 274#else
316 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; 275 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
317#endif 276#endif
361 return 0; 320 return 0;
362} 321}
363 322
364MODULE = IO::AIO PACKAGE = IO::AIO 323MODULE = IO::AIO PACKAGE = IO::AIO
365 324
325PROTOTYPES: ENABLE
326
366BOOT: 327BOOT:
367{ 328{
368 if (pipe (respipe)) 329 if (pipe (respipe))
369 croak ("unable to initialize result pipe"); 330 croak ("unable to initialize result pipe");
370 331
410 CODE: 371 CODE:
411 RETVAL = max_outstanding; 372 RETVAL = max_outstanding;
412 max_outstanding = nreqs; 373 max_outstanding = nreqs;
413 374
414void 375void
415aio_open(pathname,flags,mode,callback) 376aio_open(pathname,flags,mode,callback=&PL_sv_undef)
416 SV * pathname 377 SV * pathname
417 int flags 378 int flags
418 int mode 379 int mode
419 SV * callback 380 SV * callback
420 PROTOTYPE: $$$$ 381 PROTOTYPE: $$$;$
421 CODE: 382 CODE:
422{ 383{
423 aio_req req; 384 aio_req req;
424 385
425 Newz (0, req, 1, aio_cb); 386 Newz (0, req, 1, aio_cb);
436 397
437 send_req (req); 398 send_req (req);
438} 399}
439 400
440void 401void
441aio_close(fh,callback) 402aio_close(fh,callback=&PL_sv_undef)
442 InputStream fh 403 SV * fh
443 SV * callback 404 SV * callback
444 PROTOTYPE: $$ 405 PROTOTYPE: $;$
445 ALIAS: 406 ALIAS:
446 aio_close = REQ_CLOSE 407 aio_close = REQ_CLOSE
447 aio_fsync = REQ_FSYNC 408 aio_fsync = REQ_FSYNC
448 aio_fdatasync = REQ_FDATASYNC 409 aio_fdatasync = REQ_FDATASYNC
449 CODE: 410 CODE:
454 415
455 if (!req) 416 if (!req)
456 croak ("out of memory during aio_req allocation"); 417 croak ("out of memory during aio_req allocation");
457 418
458 req->type = ix; 419 req->type = ix;
420 req->fh = newSVsv (fh);
459 req->fd = PerlIO_fileno (fh); 421 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
460 req->callback = SvREFCNT_inc (callback); 422 req->callback = SvREFCNT_inc (callback);
461 423
462 send_req (req); 424 send_req (req);
463} 425}
464 426
465void 427void
466aio_read(fh,offset,length,data,dataoffset,callback) 428aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
467 InputStream fh 429 SV * fh
468 UV offset 430 UV offset
469 IV length 431 IV length
470 SV * data 432 SV * data
471 IV dataoffset 433 IV dataoffset
472 SV * callback 434 SV * callback
435 ALIAS:
436 aio_read = REQ_READ
437 aio_write = REQ_WRITE
473 PROTOTYPE: $$$$$$ 438 PROTOTYPE: $$$$$;$
474 CODE: 439 CODE:
475 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
476
477void
478aio_write(fh,offset,length,data,dataoffset,callback)
479 OutputStream fh
480 UV offset
481 IV length
482 SV * data
483 IV dataoffset
484 SV * callback
485 PROTOTYPE: $$$$$$
486 CODE:
487 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
488
489void
490aio_readahead(fh,offset,length,callback)
491 InputStream fh
492 UV offset
493 IV length
494 SV * callback
495 PROTOTYPE: $$$$
496 CODE:
497{ 440{
498 aio_req req; 441 aio_req req;
442 STRLEN svlen;
443 char *svptr = SvPV (data, svlen);
444
445 SvUPGRADE (data, SVt_PV);
446 SvPOK_on (data);
447
448 if (dataoffset < 0)
449 dataoffset += svlen;
450
451 if (dataoffset < 0 || dataoffset > svlen)
452 croak ("data offset outside of string");
453
454 if (ix == REQ_WRITE)
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 }
499 465
500 if (length < 0) 466 if (length < 0)
501 croak ("length must not be negative"); 467 croak ("length must not be negative");
502 468
503 Newz (0, req, 1, aio_cb); 469 Newz (0, req, 1, aio_cb);
504 470
505 if (!req) 471 if (!req)
506 croak ("out of memory during aio_req allocation"); 472 croak ("out of memory during aio_req allocation");
507 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}
486
487void
488aio_readahead(fh,offset,length,callback=&PL_sv_undef)
489 SV * fh
490 UV offset
491 IV length
492 SV * callback
493 PROTOTYPE: $$$;$
494 CODE:
495{
496 aio_req req;
497
498 if (length < 0)
499 croak ("length must not be negative");
500
501 Newz (0, req, 1, aio_cb);
502
503 if (!req)
504 croak ("out of memory during aio_req allocation");
505
508 req->type = REQ_READAHEAD; 506 req->type = REQ_READAHEAD;
507 req->fh = newSVsv (fh);
509 req->fd = PerlIO_fileno (fh); 508 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
510 req->offset = offset; 509 req->offset = offset;
511 req->length = length; 510 req->length = length;
512 req->callback = SvREFCNT_inc (callback); 511 req->callback = SvREFCNT_inc (callback);
513 512
514 send_req (req); 513 send_req (req);
515} 514}
516 515
517void 516void
518aio_stat(fh_or_path,callback) 517aio_stat(fh_or_path,callback=&PL_sv_undef)
519 SV * fh_or_path 518 SV * fh_or_path
520 SV * callback 519 SV * callback
521 PROTOTYPE: $$
522 ALIAS: 520 ALIAS:
521 aio_stat = REQ_STAT
523 aio_lstat = 1 522 aio_lstat = REQ_LSTAT
524 CODE: 523 CODE:
525{ 524{
526 aio_req req; 525 aio_req req;
527 526
528 Newz (0, req, 1, aio_cb); 527 Newz (0, req, 1, aio_cb);
535 if (!req->statdata) 534 if (!req->statdata)
536 croak ("out of memory during aio_req->statdata allocation"); 535 croak ("out of memory during aio_req->statdata allocation");
537 536
538 if (SvPOK (fh_or_path)) 537 if (SvPOK (fh_or_path))
539 { 538 {
540 req->type = ix ? REQ_LSTAT : REQ_STAT; 539 req->type = ix;
541 req->data = newSVsv (fh_or_path); 540 req->data = newSVsv (fh_or_path);
542 req->dataptr = SvPV_nolen (req->data); 541 req->dataptr = SvPV_nolen (req->data);
543 } 542 }
544 else 543 else
545 { 544 {
546 req->type = REQ_FSTAT; 545 req->type = REQ_FSTAT;
546 req->fh = newSVsv (fh_or_path);
547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
548 } 548 }
549 549
550 req->callback = SvREFCNT_inc (callback); 550 req->callback = SvREFCNT_inc (callback);
551 551
552 send_req (req); 552 send_req (req);
553} 553}
554 554
555void 555void
556aio_unlink(pathname,callback) 556aio_unlink(pathname,callback=&PL_sv_undef)
557 SV * pathname 557 SV * pathname
558 SV * callback 558 SV * callback
559 PROTOTYPE: $$
560 CODE: 559 CODE:
561{ 560{
562 aio_req req; 561 aio_req req;
563 562
564 Newz (0, req, 1, aio_cb); 563 Newz (0, req, 1, aio_cb);
582 { 581 {
583 poll_wait (); 582 poll_wait ();
584 poll_cb (); 583 poll_cb ();
585 } 584 }
586 585
586void
587poll()
588 PROTOTYPE:
589 CODE:
590 if (nreqs)
591 {
592 poll_wait ();
593 poll_cb ();
594 }
595
587int 596int
588poll_fileno() 597poll_fileno()
589 PROTOTYPE: 598 PROTOTYPE:
590 CODE: 599 CODE:
591 RETVAL = respipe [0]; 600 RETVAL = respipe [0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines