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.62 by root, Mon Oct 23 22:45:18 2006 UTC vs.
Revision 1.66 by root, Tue Oct 24 00:34:47 2006 UTC

1#if __linux
2# define _GNU_SOURCE
3#endif
4
1#define _REENTRANT 1 5#define _REENTRANT 1
6
2#include <errno.h> 7#include <errno.h>
3 8
4#include "EXTERN.h" 9#include "EXTERN.h"
5#include "perl.h" 10#include "perl.h"
6#include "XSUB.h" 11#include "XSUB.h"
41# define NAME_MAX 4096 46# define NAME_MAX 4096
42#endif 47#endif
43 48
44#if __ia64 49#if __ia64
45# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
46#else 53#else
47# define STACKSIZE 8192 54# define STACKSIZE 16384
48#endif 55#endif
56
57/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536
59
60#define dBUF \
61 char *aio_buf = malloc (AIO_BUFSIZE); \
62 if (!aio_buf) \
63 return -1;
64
65#define fBUF free (aio_buf)
49 66
50enum { 67enum {
51 REQ_QUIT, 68 REQ_QUIT,
52 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
53 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
109static int started, wanted; 126static int started, wanted;
110static volatile int nreqs; 127static volatile int nreqs;
111static int max_outstanding = 1<<30; 128static int max_outstanding = 1<<30;
112static int respipe [2]; 129static int respipe [2];
113 130
131#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
132# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
133#else
134# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
135#endif
136
114static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 137static pthread_mutex_t reslock = AIO_MUTEX_INIT;
115static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
116static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 139static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
117 140
118static volatile aio_req reqs, reqe; /* queue start, queue end */ 141static volatile aio_req reqs, reqe; /* queue start, queue end */
119static volatile aio_req ress, rese; /* queue start, queue end */ 142static volatile aio_req ress, rese; /* queue start, queue end */
120 143
196 fd_set rfd; 219 fd_set rfd;
197 220
198 while (nreqs) 221 while (nreqs)
199 { 222 {
200 aio_req req; 223 aio_req req;
224#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
201 pthread_mutex_lock (&reslock); 225 pthread_mutex_lock (&reslock);
226#endif
202 req = ress; 227 req = ress;
228#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
203 pthread_mutex_unlock (&reslock); 229 pthread_mutex_unlock (&reslock);
230#endif
204 231
205 if (req) 232 if (req)
206 return; 233 return;
207 234
208 FD_ZERO(&rfd); 235 FD_ZERO(&rfd);
579#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
580# define readahead aio_readahead 607# define readahead aio_readahead
581 608
582static ssize_t readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
583{ 610{
584 char readahead_buf[4096]; 611 dBUF;
585 612
586 while (count > 0) 613 while (count > 0)
587 { 614 {
588 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
589 616
590 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
591 offset += len; 618 offset += len;
592 count -= len; 619 count -= len;
593 } 620 }
621
622 fBUF;
594 623
595 errno = 0; 624 errno = 0;
596} 625}
597#endif 626#endif
598 627
684#endif 713#endif
685 ) 714 )
686 ) 715 )
687 { 716 {
688 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
689 char buf[4096]; 718 dBUF;
719
690 res = 0; 720 res = 0;
691 721
692 while (count) 722 while (count)
693 { 723 {
694 ssize_t cnt; 724 ssize_t cnt;
695 725
696 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
697 727
698 if (cnt <= 0) 728 if (cnt <= 0)
699 { 729 {
700 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
701 break; 731 break;
702 } 732 }
703 733
704 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
705 735
706 if (cnt <= 0) 736 if (cnt <= 0)
707 { 737 {
708 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
709 break; 739 break;
711 741
712 offset += cnt; 742 offset += cnt;
713 res += cnt; 743 res += cnt;
714 count -= cnt; 744 count -= cnt;
715 } 745 }
746
747 fBUF;
716 } 748 }
717 749
718 return res; 750 return res;
719} 751}
720 752
721/* read a full directory */ 753/* read a full directory */
722static int scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
723{ 755{
724 DIR *dirp = opendir (path); 756 DIR *dirp;
725 union 757 union
726 { 758 {
727 struct dirent d; 759 struct dirent d;
728 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
729 } u; 761 } *u;
730 struct dirent *entp; 762 struct dirent *entp;
731 char *name, *names; 763 char *name, *names;
732 int memlen = 4096; 764 int memlen = 4096;
733 int memofs = 0; 765 int memofs = 0;
734 int res = 0; 766 int res = 0;
735 int errorno; 767 int errorno;
736 768
769 dirp = opendir (path);
737 if (!dirp) 770 if (!dirp)
738 return -1; 771 return -1;
739 772
773 u = malloc (sizeof (*u));
740 names = malloc (memlen); 774 names = malloc (memlen);
741 775
776 if (u && names)
742 for (;;) 777 for (;;)
743 { 778 {
779 errno = 0;
744 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
745 781
746 if (!entp) 782 if (!entp)
747 break; 783 break;
748 784
749 name = entp->d_name; 785 name = entp->d_name;
750 786
751 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
752 { 788 {
753 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
754 790
755 res++; 791 res++;
756 792
757 while (memofs + len > memlen) 793 while (memofs + len > memlen)
758 { 794 {
759 memlen *= 2; 795 memlen *= 2;
760 names = realloc (names, memlen); 796 names = realloc (names, memlen);
761 if (!names) 797 if (!names)
762 break; 798 break;
763 } 799 }
764 800
765 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
766 memofs += len; 802 memofs += len;
767 } 803 }
768 } 804 }
769 805
770 errorno = errno; 806 errorno = errno;
807 free (u);
771 closedir (dirp); 808 closedir (dirp);
772 809
773 if (errorno) 810 if (errorno)
774 { 811 {
775 free (names); 812 free (names);
1324 1361
1325MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1362MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1326 1363
1327void 1364void
1328cancel (aio_req_ornot req) 1365cancel (aio_req_ornot req)
1329 PROTOTYPE:
1330 CODE: 1366 CODE:
1331 req_cancel (req); 1367 req_cancel (req);
1332 1368
1333void 1369void
1334cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1370cb (aio_req_ornot req, SV *callback=&PL_sv_undef)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines