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.55 by root, Mon Oct 23 00:47:29 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,
66 83
67typedef struct aio_cb 84typedef struct aio_cb
68{ 85{
69 struct aio_cb *volatile next; 86 struct aio_cb *volatile next;
70 87
71 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
72
73 SV *self; /* the perl counterpart of this request, if any */
74
75 SV *data, *callback; 88 SV *data, *callback;
76 SV *fh, *fh2; 89 SV *fh, *fh2;
77 void *dataptr, *data2ptr; 90 void *dataptr, *data2ptr;
78 Stat_t *statdata; 91 Stat_t *statdata;
79 off_t offset; 92 off_t offset;
80 size_t length; 93 size_t length;
81 ssize_t result; 94 ssize_t result;
82 95
96 STRLEN dataoffset;
83 int type; 97 int type;
84 int fd, fd2; 98 int fd, fd2;
85 int errorno; 99 int errorno;
86 STRLEN dataoffset;
87 mode_t mode; /* open */ 100 mode_t mode; /* open */
101
88 unsigned char cancelled; 102 unsigned char flags;
103 unsigned char pri;
104
105 SV *self; /* the perl counterpart of this request, if any */
106 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
89} aio_cb; 107} aio_cb;
108
109enum {
110 FLAG_CANCELLED = 0x01,
111};
90 112
91typedef aio_cb *aio_req; 113typedef aio_cb *aio_req;
92typedef aio_cb *aio_req_ornot; 114typedef aio_cb *aio_req_ornot;
115
116enum {
117 PRI_MIN = -4,
118 PRI_MAX = 4,
119
120 DEFAULT_PRI = 0,
121 PRI_BIAS = -PRI_MIN,
122};
123
124static int next_pri = DEFAULT_PRI + PRI_BIAS;
93 125
94static int started, wanted; 126static int started, wanted;
95static volatile int nreqs; 127static volatile int nreqs;
96static int max_outstanding = 1<<30; 128static int max_outstanding = 1<<30;
97static int respipe [2]; 129static int respipe [2];
98 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
99static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 137static pthread_mutex_t reslock = AIO_MUTEX_INIT;
100static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
101static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 139static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
102 140
103static volatile aio_req reqs, reqe; /* queue start, queue end */ 141static volatile aio_req reqs, reqe; /* queue start, queue end */
104static volatile aio_req ress, rese; /* queue start, queue end */ 142static volatile aio_req ress, rese; /* queue start, queue end */
105 143
130 return mg ? (aio_req)mg->mg_ptr : 0; 168 return mg ? (aio_req)mg->mg_ptr : 0;
131} 169}
132 170
133static void aio_grp_feed (aio_req grp) 171static void aio_grp_feed (aio_req grp)
134{ 172{
135 while (grp->length < grp->fd2) 173 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
136 { 174 {
137 int old_len = grp->length; 175 int old_len = grp->length;
138 176
139 if (grp->fh2 && SvOK (grp->fh2)) 177 if (grp->fh2 && SvOK (grp->fh2))
140 { 178 {
176 } 214 }
177} 215}
178 216
179static void poll_wait () 217static void poll_wait ()
180{ 218{
181 if (nreqs && !ress)
182 {
183 fd_set rfd; 219 fd_set rfd;
220
221 while (nreqs)
222 {
223 aio_req req;
224#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
225 pthread_mutex_lock (&reslock);
226#endif
227 req = ress;
228#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
229 pthread_mutex_unlock (&reslock);
230#endif
231
232 if (req)
233 return;
234
184 FD_ZERO(&rfd); 235 FD_ZERO(&rfd);
185 FD_SET(respipe [0], &rfd); 236 FD_SET(respipe [0], &rfd);
186 237
187 select (respipe [0] + 1, &rfd, 0, 0, 0); 238 select (respipe [0] + 1, &rfd, 0, 0, 0);
188 } 239 }
191static void req_invoke (aio_req req) 242static void req_invoke (aio_req req)
192{ 243{
193 dSP; 244 dSP;
194 int errorno = errno; 245 int errorno = errno;
195 246
196 if (req->cancelled || !SvOK (req->callback)) 247 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback))
197 return; 248 return;
198 249
199 errno = req->errorno; 250 errno = req->errorno;
200 251
201 ENTER; 252 ENTER;
321 Safefree (req); 372 Safefree (req);
322} 373}
323 374
324static void req_cancel (aio_req req) 375static void req_cancel (aio_req req)
325{ 376{
326 req->cancelled = 1; 377 req->flags |= FLAG_CANCELLED;
327 378
328 if (req->type == REQ_GROUP) 379 if (req->type == REQ_GROUP)
329 { 380 {
330 aio_req sub; 381 aio_req sub;
331 382
555#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
556# define readahead aio_readahead 607# define readahead aio_readahead
557 608
558static ssize_t readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
559{ 610{
560 char readahead_buf[4096]; 611 dBUF;
561 612
562 while (count > 0) 613 while (count > 0)
563 { 614 {
564 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
565 616
566 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
567 offset += len; 618 offset += len;
568 count -= len; 619 count -= len;
569 } 620 }
621
622 fBUF;
570 623
571 errno = 0; 624 errno = 0;
572} 625}
573#endif 626#endif
574 627
660#endif 713#endif
661 ) 714 )
662 ) 715 )
663 { 716 {
664 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
665 char buf[4096]; 718 dBUF;
719
666 res = 0; 720 res = 0;
667 721
668 while (count) 722 while (count)
669 { 723 {
670 ssize_t cnt; 724 ssize_t cnt;
671 725
672 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
673 727
674 if (cnt <= 0) 728 if (cnt <= 0)
675 { 729 {
676 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
677 break; 731 break;
678 } 732 }
679 733
680 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
681 735
682 if (cnt <= 0) 736 if (cnt <= 0)
683 { 737 {
684 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
685 break; 739 break;
687 741
688 offset += cnt; 742 offset += cnt;
689 res += cnt; 743 res += cnt;
690 count -= cnt; 744 count -= cnt;
691 } 745 }
746
747 fBUF;
692 } 748 }
693 749
694 return res; 750 return res;
695} 751}
696 752
697/* read a full directory */ 753/* read a full directory */
698static int scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
699{ 755{
700 DIR *dirp = opendir (path); 756 DIR *dirp;
701 union 757 union
702 { 758 {
703 struct dirent d; 759 struct dirent d;
704 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
705 } u; 761 } *u;
706 struct dirent *entp; 762 struct dirent *entp;
707 char *name, *names; 763 char *name, *names;
708 int memlen = 4096; 764 int memlen = 4096;
709 int memofs = 0; 765 int memofs = 0;
710 int res = 0; 766 int res = 0;
711 int errorno; 767 int errorno;
712 768
769 dirp = opendir (path);
713 if (!dirp) 770 if (!dirp)
714 return -1; 771 return -1;
715 772
773 u = malloc (sizeof (*u));
716 names = malloc (memlen); 774 names = malloc (memlen);
717 775
776 if (u && names)
718 for (;;) 777 for (;;)
719 { 778 {
779 errno = 0;
720 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
721 781
722 if (!entp) 782 if (!entp)
723 break; 783 break;
724 784
725 name = entp->d_name; 785 name = entp->d_name;
726 786
727 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
728 { 788 {
729 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
730 790
731 res++; 791 res++;
732 792
733 while (memofs + len > memlen) 793 while (memofs + len > memlen)
734 { 794 {
735 memlen *= 2; 795 memlen *= 2;
736 names = realloc (names, memlen); 796 names = realloc (names, memlen);
737 if (!names) 797 if (!names)
738 break; 798 break;
739 } 799 }
740 800
741 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
742 memofs += len; 802 memofs += len;
743 } 803 }
744 } 804 }
745 805
746 errorno = errno; 806 errorno = errno;
807 free (u);
747 closedir (dirp); 808 closedir (dirp);
748 809
749 if (errorno) 810 if (errorno)
750 { 811 {
751 free (names); 812 free (names);
785 } 846 }
786 847
787 pthread_mutex_unlock (&reqlock); 848 pthread_mutex_unlock (&reqlock);
788 849
789 errno = 0; /* strictly unnecessary */ 850 errno = 0; /* strictly unnecessary */
790
791 if (!req->cancelled)
792 switch (type = req->type) /* remember type for QUIT check */ 851 type = req->type; /* remember type for QUIT check */
852
853 if (!(req->flags & FLAG_CANCELLED))
854 switch (type)
793 { 855 {
794 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 856 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
795 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 857 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
796 858
797 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 859 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
916 atfork_parent (); 978 atfork_parent ();
917} 979}
918 980
919#define dREQ \ 981#define dREQ \
920 aio_req req; \ 982 aio_req req; \
983 int req_pri = next_pri; \
984 next_pri = DEFAULT_PRI + PRI_BIAS; \
921 \ 985 \
922 if (SvOK (callback) && !SvROK (callback)) \ 986 if (SvOK (callback) && !SvROK (callback)) \
923 croak ("callback must be undef or of reference type"); \ 987 croak ("callback must be undef or of reference type"); \
924 \ 988 \
925 Newz (0, req, 1, aio_cb); \ 989 Newz (0, req, 1, aio_cb); \
926 if (!req) \ 990 if (!req) \
927 croak ("out of memory during aio_req allocation"); \ 991 croak ("out of memory during aio_req allocation"); \
928 \ 992 \
929 req->callback = newSVsv (callback) 993 req->callback = newSVsv (callback); \
994 req->pri = req_pri
930 995
931#define REQ_SEND \ 996#define REQ_SEND \
932 req_send (req); \ 997 req_send (req); \
933 \ 998 \
934 if (GIMME_V != G_VOID) \ 999 if (GIMME_V != G_VOID) \
1209 SV * callback 1274 SV * callback
1210 PROTOTYPE: ;$ 1275 PROTOTYPE: ;$
1211 PPCODE: 1276 PPCODE:
1212{ 1277{
1213 dREQ; 1278 dREQ;
1279
1214 req->type = REQ_GROUP; 1280 req->type = REQ_GROUP;
1215 req_send (req); 1281 req_send (req);
1282
1216 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1283 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1217} 1284}
1218 1285
1219void 1286void
1220aio_nop (callback=&PL_sv_undef) 1287aio_nop (callback=&PL_sv_undef)
1225 1292
1226 req->type = REQ_NOP; 1293 req->type = REQ_NOP;
1227 1294
1228 REQ_SEND; 1295 REQ_SEND;
1229} 1296}
1297
1298#if 0
1299
1300void
1301aio_pri (int pri = DEFAULT_PRI)
1302 CODE:
1303 if (pri < PRI_MIN) pri = PRI_MIN;
1304 if (pri > PRI_MAX) pri = PRI_MAX;
1305 next_pri = pri + PRI_BIAS;
1306
1307#endif
1230 1308
1231void 1309void
1232flush () 1310flush ()
1233 PROTOTYPE: 1311 PROTOTYPE:
1234 CODE: 1312 CODE:
1283 1361
1284MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1362MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1285 1363
1286void 1364void
1287cancel (aio_req_ornot req) 1365cancel (aio_req_ornot req)
1288 PROTOTYPE:
1289 CODE: 1366 CODE:
1290 req_cancel (req); 1367 req_cancel (req);
1368
1369void
1370cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1371 CODE:
1372 SvREFCNT_dec (req->callback);
1373 req->callback = newSVsv (callback);
1291 1374
1292MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1375MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1293 1376
1294void 1377void
1295add (aio_req grp, ...) 1378add (aio_req grp, ...)
1337 SvREFCNT_dec (grp->data); 1420 SvREFCNT_dec (grp->data);
1338 grp->data = (SV *)av; 1421 grp->data = (SV *)av;
1339} 1422}
1340 1423
1341void 1424void
1342lock (aio_req grp)
1343 CODE:
1344 ++grp->length;
1345
1346void
1347unlock (aio_req grp)
1348 CODE:
1349 aio_grp_dec (grp);
1350
1351void
1352feeder_limit (aio_req grp, int limit) 1425feed_limit (aio_req grp, int limit)
1353 CODE: 1426 CODE:
1354 grp->fd2 = limit; 1427 grp->fd2 = limit;
1355 aio_grp_feed (grp); 1428 aio_grp_feed (grp);
1356 1429
1357void 1430void
1358set_feeder (aio_req grp, SV *callback=&PL_sv_undef) 1431feed (aio_req grp, SV *callback=&PL_sv_undef)
1359 CODE: 1432 CODE:
1360{ 1433{
1361 SvREFCNT_dec (grp->fh2); 1434 SvREFCNT_dec (grp->fh2);
1362 grp->fh2 = newSVsv (callback); 1435 grp->fh2 = newSVsv (callback);
1363 1436

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines