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.5 by root, Sun Jul 10 23:45:16 2005 UTC vs.
Revision 1.12 by root, Wed Jul 20 21:57:04 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
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
148 144
149 PUSHMARK (SP); 145 PUSHMARK (SP);
150 XPUSHs (fh); 146 XPUSHs (fh);
151 } 147 }
152 148
149 if (SvOK (req->callback))
150 {
153 PUTBACK; 151 PUTBACK;
154 call_sv (req->callback, G_VOID | G_EVAL); 152 call_sv (req->callback, G_VOID | G_EVAL);
155 SPAGAIN; 153 SPAGAIN;
154 }
156 155
157 if (req->callback) 156 if (req->callback)
158 SvREFCNT_dec (req->callback); 157 SvREFCNT_dec (req->callback);
159 158
160 errno = errorno; 159 errno = errorno;
161 count++; 160 count++;
162 } 161 }
163 162
163 prv = req;
164 req = req->next;
164 Safefree (req); 165 Safefree (prv);
166
167 /* TODO: croak on errors? */
165 } 168 }
166 169
167 return count; 170 return count;
168} 171}
169 172
306 309
307 type = req->type; 310 type = req->type;
308 311
309 switch (type) 312 switch (type)
310 { 313 {
311 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;
312 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;
313#if SYS_readahead 316#if SYS_readahead
314 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;
315#else 318#else
316 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; 319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
317#endif 320#endif
361 return 0; 364 return 0;
362} 365}
363 366
364MODULE = IO::AIO PACKAGE = IO::AIO 367MODULE = IO::AIO PACKAGE = IO::AIO
365 368
369PROTOTYPES: ENABLE
370
366BOOT: 371BOOT:
367{ 372{
368 if (pipe (respipe)) 373 if (pipe (respipe))
369 croak ("unable to initialize result pipe"); 374 croak ("unable to initialize result pipe");
370 375
410 CODE: 415 CODE:
411 RETVAL = max_outstanding; 416 RETVAL = max_outstanding;
412 max_outstanding = nreqs; 417 max_outstanding = nreqs;
413 418
414void 419void
415aio_open(pathname,flags,mode,callback) 420aio_open(pathname,flags,mode,callback=&PL_sv_undef)
416 SV * pathname 421 SV * pathname
417 int flags 422 int flags
418 int mode 423 int mode
419 SV * callback 424 SV * callback
420 PROTOTYPE: $$$$ 425 PROTOTYPE: $$$;$
421 CODE: 426 CODE:
422{ 427{
423 aio_req req; 428 aio_req req;
424 429
425 Newz (0, req, 1, aio_cb); 430 Newz (0, req, 1, aio_cb);
436 441
437 send_req (req); 442 send_req (req);
438} 443}
439 444
440void 445void
441aio_close(fh,callback) 446aio_close(fh,callback=&PL_sv_undef)
442 InputStream fh 447 InputStream fh
443 SV * callback 448 SV * callback
444 PROTOTYPE: $$ 449 PROTOTYPE: $;$
445 ALIAS: 450 ALIAS:
446 aio_close = REQ_CLOSE 451 aio_close = REQ_CLOSE
447 aio_fsync = REQ_FSYNC 452 aio_fsync = REQ_FSYNC
448 aio_fdatasync = REQ_FDATASYNC 453 aio_fdatasync = REQ_FDATASYNC
449 CODE: 454 CODE:
461 466
462 send_req (req); 467 send_req (req);
463} 468}
464 469
465void 470void
466aio_read(fh,offset,length,data,dataoffset,callback) 471aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
467 InputStream fh 472 InputStream fh
468 UV offset 473 UV offset
469 IV length 474 IV length
470 SV * data 475 SV * data
471 IV dataoffset 476 IV dataoffset
472 SV * callback 477 SV * callback
473 PROTOTYPE: $$$$$$ 478 PROTOTYPE: $$$$$;$
474 CODE: 479 CODE:
475 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 480 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
476 481
477void 482void
478aio_write(fh,offset,length,data,dataoffset,callback) 483aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
479 OutputStream fh 484 OutputStream fh
480 UV offset 485 UV offset
481 IV length 486 IV length
482 SV * data 487 SV * data
483 IV dataoffset 488 IV dataoffset
484 SV * callback 489 SV * callback
485 PROTOTYPE: $$$$$$ 490 PROTOTYPE: $$$$$;$
486 CODE: 491 CODE:
487 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 492 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
488 493
489void 494void
490aio_readahead(fh,offset,length,callback) 495aio_readahead(fh,offset,length,callback=&PL_sv_undef)
491 InputStream fh 496 InputStream fh
492 UV offset 497 UV offset
493 IV length 498 IV length
494 SV * callback 499 SV * callback
495 PROTOTYPE: $$$$ 500 PROTOTYPE: $$$;$
496 CODE: 501 CODE:
497{ 502{
498 aio_req req; 503 aio_req req;
499 504
500 if (length < 0) 505 if (length < 0)
513 518
514 send_req (req); 519 send_req (req);
515} 520}
516 521
517void 522void
518aio_stat(fh_or_path,callback) 523aio_stat(fh_or_path,callback=&PL_sv_undef)
519 SV * fh_or_path 524 SV * fh_or_path
520 SV * callback 525 SV * callback
521 PROTOTYPE: $$
522 ALIAS: 526 ALIAS:
527 aio_stat = REQ_STAT
523 aio_lstat = 1 528 aio_lstat = REQ_LSTAT
524 CODE: 529 CODE:
525{ 530{
526 aio_req req; 531 aio_req req;
527 532
528 Newz (0, req, 1, aio_cb); 533 Newz (0, req, 1, aio_cb);
535 if (!req->statdata) 540 if (!req->statdata)
536 croak ("out of memory during aio_req->statdata allocation"); 541 croak ("out of memory during aio_req->statdata allocation");
537 542
538 if (SvPOK (fh_or_path)) 543 if (SvPOK (fh_or_path))
539 { 544 {
540 req->type = ix ? REQ_LSTAT : REQ_STAT; 545 req->type = ix;
541 req->data = newSVsv (fh_or_path); 546 req->data = newSVsv (fh_or_path);
542 req->dataptr = SvPV_nolen (req->data); 547 req->dataptr = SvPV_nolen (req->data);
543 } 548 }
544 else 549 else
545 { 550 {
551 556
552 send_req (req); 557 send_req (req);
553} 558}
554 559
555void 560void
556aio_unlink(pathname,callback) 561aio_unlink(pathname,callback=&PL_sv_undef)
557 SV * pathname 562 SV * pathname
558 SV * callback 563 SV * callback
559 PROTOTYPE: $$
560 CODE: 564 CODE:
561{ 565{
562 aio_req req; 566 aio_req req;
563 567
564 Newz (0, req, 1, aio_cb); 568 Newz (0, req, 1, aio_cb);
572 req->callback = SvREFCNT_inc (callback); 576 req->callback = SvREFCNT_inc (callback);
573 577
574 send_req (req); 578 send_req (req);
575} 579}
576 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
577int 601int
578poll_fileno() 602poll_fileno()
579 PROTOTYPE: 603 PROTOTYPE:
580 CODE: 604 CODE:
581 RETVAL = respipe [0]; 605 RETVAL = respipe [0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines