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.7 by root, Mon Jul 11 01:49:14 2005 UTC vs.
Revision 1.13 by root, Fri Jul 22 08:25:22 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines