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.12 by root, Wed Jul 20 21:57:04 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
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
150 144
151 PUSHMARK (SP); 145 PUSHMARK (SP);
152 XPUSHs (fh); 146 XPUSHs (fh);
153 } 147 }
154 148
149 if (SvOK (req->callback))
150 {
155 PUTBACK; 151 PUTBACK;
156 call_sv (req->callback, G_VOID | G_EVAL); 152 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 153 SPAGAIN;
154 }
158 155
159 if (req->callback) 156 if (req->callback)
160 SvREFCNT_dec (req->callback); 157 SvREFCNT_dec (req->callback);
161 158
162 errno = errorno; 159 errno = errorno;
163 count++; 160 count++;
164 } 161 }
165 162
163 prv = req;
164 req = req->next;
166 Safefree (req); 165 Safefree (prv);
166
167 /* TODO: croak on errors? */
167 } 168 }
168 169
169 return count; 170 return count;
170} 171}
171 172
227 228
228 send_req (req); 229 send_req (req);
229} 230}
230 231
231static void 232static void
232read_write (pTHX_
233 int dowrite, int fd, off_t offset, size_t length, 233read_write (int dowrite, int fd, off_t offset, size_t length,
234 SV *data, STRLEN dataoffset, SV *callback) 234 SV *data, STRLEN dataoffset, SV *callback)
235{ 235{
236 aio_req req; 236 aio_req req;
237 STRLEN svlen; 237 STRLEN svlen;
238 char *svptr = SvPV (data, svlen); 238 char *svptr = SvPV (data, svlen);
309 309
310 type = req->type; 310 type = req->type;
311 311
312 switch (type) 312 switch (type)
313 { 313 {
314 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 314 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; 315 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
316#if SYS_readahead 316#if SYS_readahead
317 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 317 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
318#else 318#else
319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; 319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
320#endif 320#endif
364 return 0; 364 return 0;
365} 365}
366 366
367MODULE = IO::AIO PACKAGE = IO::AIO 367MODULE = IO::AIO PACKAGE = IO::AIO
368 368
369PROTOTYPES: ENABLE
370
369BOOT: 371BOOT:
370{ 372{
371 if (pipe (respipe)) 373 if (pipe (respipe))
372 croak ("unable to initialize result pipe"); 374 croak ("unable to initialize result pipe");
373 375
400 } 402 }
401 403
402 while (started > nthreads) 404 while (started > nthreads)
403 { 405 {
404 poll_wait (); 406 poll_wait ();
405 poll_cb (aTHX); 407 poll_cb ();
406 } 408 }
407} 409}
408 410
409int 411int
410max_outstanding(nreqs) 412max_outstanding(nreqs)
413 CODE: 415 CODE:
414 RETVAL = max_outstanding; 416 RETVAL = max_outstanding;
415 max_outstanding = nreqs; 417 max_outstanding = nreqs;
416 418
417void 419void
418aio_open(pathname,flags,mode,callback) 420aio_open(pathname,flags,mode,callback=&PL_sv_undef)
419 SV * pathname 421 SV * pathname
420 int flags 422 int flags
421 int mode 423 int mode
422 SV * callback 424 SV * callback
423 PROTOTYPE: $$$$ 425 PROTOTYPE: $$$;$
424 CODE: 426 CODE:
425{ 427{
426 aio_req req; 428 aio_req req;
427 429
428 Newz (0, req, 1, aio_cb); 430 Newz (0, req, 1, aio_cb);
439 441
440 send_req (req); 442 send_req (req);
441} 443}
442 444
443void 445void
444aio_close(fh,callback) 446aio_close(fh,callback=&PL_sv_undef)
445 InputStream fh 447 InputStream fh
446 SV * callback 448 SV * callback
447 PROTOTYPE: $$ 449 PROTOTYPE: $;$
448 ALIAS: 450 ALIAS:
449 aio_close = REQ_CLOSE 451 aio_close = REQ_CLOSE
450 aio_fsync = REQ_FSYNC 452 aio_fsync = REQ_FSYNC
451 aio_fdatasync = REQ_FDATASYNC 453 aio_fdatasync = REQ_FDATASYNC
452 CODE: 454 CODE:
464 466
465 send_req (req); 467 send_req (req);
466} 468}
467 469
468void 470void
469aio_read(fh,offset,length,data,dataoffset,callback) 471aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
470 InputStream fh 472 InputStream fh
471 UV offset 473 UV offset
472 IV length 474 IV length
473 SV * data 475 SV * data
474 IV dataoffset 476 IV dataoffset
475 SV * callback 477 SV * callback
476 PROTOTYPE: $$$$$$ 478 PROTOTYPE: $$$$$;$
477 CODE: 479 CODE:
478 read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 480 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
479 481
480void 482void
481aio_write(fh,offset,length,data,dataoffset,callback) 483aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
482 OutputStream fh 484 OutputStream fh
483 UV offset 485 UV offset
484 IV length 486 IV length
485 SV * data 487 SV * data
486 IV dataoffset 488 IV dataoffset
487 SV * callback 489 SV * callback
488 PROTOTYPE: $$$$$$ 490 PROTOTYPE: $$$$$;$
489 CODE: 491 CODE:
490 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 492 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
491 493
492void 494void
493aio_readahead(fh,offset,length,callback) 495aio_readahead(fh,offset,length,callback=&PL_sv_undef)
494 InputStream fh 496 InputStream fh
495 UV offset 497 UV offset
496 IV length 498 IV length
497 SV * callback 499 SV * callback
498 PROTOTYPE: $$$$ 500 PROTOTYPE: $$$;$
499 CODE: 501 CODE:
500{ 502{
501 aio_req req; 503 aio_req req;
502 504
503 if (length < 0) 505 if (length < 0)
516 518
517 send_req (req); 519 send_req (req);
518} 520}
519 521
520void 522void
521aio_stat(fh_or_path,callback) 523aio_stat(fh_or_path,callback=&PL_sv_undef)
522 SV * fh_or_path 524 SV * fh_or_path
523 SV * callback 525 SV * callback
524 PROTOTYPE: $$
525 ALIAS: 526 ALIAS:
527 aio_stat = REQ_STAT
526 aio_lstat = 1 528 aio_lstat = REQ_LSTAT
527 CODE: 529 CODE:
528{ 530{
529 aio_req req; 531 aio_req req;
530 532
531 Newz (0, req, 1, aio_cb); 533 Newz (0, req, 1, aio_cb);
538 if (!req->statdata) 540 if (!req->statdata)
539 croak ("out of memory during aio_req->statdata allocation"); 541 croak ("out of memory during aio_req->statdata allocation");
540 542
541 if (SvPOK (fh_or_path)) 543 if (SvPOK (fh_or_path))
542 { 544 {
543 req->type = ix ? REQ_LSTAT : REQ_STAT; 545 req->type = ix;
544 req->data = newSVsv (fh_or_path); 546 req->data = newSVsv (fh_or_path);
545 req->dataptr = SvPV_nolen (req->data); 547 req->dataptr = SvPV_nolen (req->data);
546 } 548 }
547 else 549 else
548 { 550 {
554 556
555 send_req (req); 557 send_req (req);
556} 558}
557 559
558void 560void
559aio_unlink(pathname,callback) 561aio_unlink(pathname,callback=&PL_sv_undef)
560 SV * pathname 562 SV * pathname
561 SV * callback 563 SV * callback
562 PROTOTYPE: $$
563 CODE: 564 CODE:
564{ 565{
565 aio_req req; 566 aio_req req;
566 567
567 Newz (0, req, 1, aio_cb); 568 Newz (0, req, 1, aio_cb);
575 req->callback = SvREFCNT_inc (callback); 576 req->callback = SvREFCNT_inc (callback);
576 577
577 send_req (req); 578 send_req (req);
578} 579}
579 580
581void
582flush()
583 PROTOTYPE:
584 CODE:
585 while (nreqs)
586 {
587 poll_wait ();
588 poll_cb ();
589 }
590
591void
592poll()
593 PROTOTYPE:
594 CODE:
595 if (nreqs)
596 {
597 poll_wait ();
598 poll_cb ();
599 }
600
580int 601int
581poll_fileno() 602poll_fileno()
582 PROTOTYPE: 603 PROTOTYPE:
583 CODE: 604 CODE:
584 RETVAL = respipe [0]; 605 RETVAL = respipe [0];
587 608
588int 609int
589poll_cb(...) 610poll_cb(...)
590 PROTOTYPE: 611 PROTOTYPE:
591 CODE: 612 CODE:
592 RETVAL = poll_cb (aTHX); 613 RETVAL = poll_cb ();
593 OUTPUT: 614 OUTPUT:
594 RETVAL 615 RETVAL
595 616
596void 617void
597poll_wait() 618poll_wait()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines