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.4 by root, Sun Jul 10 20:57:00 2005 UTC vs.
Revision 1.13 by root, Fri Jul 22 08:25:22 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines