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.63 by root, Mon Oct 23 23:48:31 2006 UTC vs.
Revision 1.67 by root, Tue Oct 24 03:17:39 2006 UTC

46# define NAME_MAX 4096 46# define NAME_MAX 4096
47#endif 47#endif
48 48
49#if __ia64 49#if __ia64
50# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
51#else 53#else
52# define STACKSIZE 8192 54# define STACKSIZE 16384
53#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)
54 66
55enum { 67enum {
56 REQ_QUIT, 68 REQ_QUIT,
57 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
58 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
105 PRI_MIN = -4, 117 PRI_MIN = -4,
106 PRI_MAX = 4, 118 PRI_MAX = 4,
107 119
108 DEFAULT_PRI = 0, 120 DEFAULT_PRI = 0,
109 PRI_BIAS = -PRI_MIN, 121 PRI_BIAS = -PRI_MIN,
122 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
110}; 123};
111 124
112static int next_pri = DEFAULT_PRI + PRI_BIAS; 125static int next_pri = DEFAULT_PRI + PRI_BIAS;
113 126
114static int started, wanted; 127static int started, wanted;
124 137
125static pthread_mutex_t reslock = AIO_MUTEX_INIT; 138static pthread_mutex_t reslock = AIO_MUTEX_INIT;
126static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 139static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
127static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 140static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
128 141
129static volatile aio_req reqs, reqe; /* queue start, queue end */ 142/*
130static volatile aio_req ress, rese; /* queue start, queue end */ 143 * a somewhat faster data structure might be nice, but
144 * with 8 priorities this actually needs <20 insns
145 * per shift, the most expensive operation.
146 */
147typedef struct {
148 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
149 int size;
150} reqq;
151
152static reqq req_queue;
153static reqq res_queue;
154
155int reqq_push (reqq *q, aio_req req)
156{
157 int pri = req->pri;
158 req->next = 0;
159
160 if (q->qe[pri])
161 {
162 q->qe[pri]->next = req;
163 q->qe[pri] = req;
164 }
165 else
166 q->qe[pri] = q->qs[pri] = req;
167
168 return q->size++;
169}
170
171aio_req reqq_shift (reqq *q)
172{
173 int pri;
174
175 if (!q->size)
176 return 0;
177
178 --q->size;
179
180 for (pri = NUM_PRI; pri--; )
181 {
182 aio_req req = q->qs[pri];
183
184 if (req)
185 {
186 if (!(q->qs[pri] = req->next))
187 q->qe[pri] = 0;
188
189 return req;
190 }
191 }
192
193 abort ();
194}
131 195
132static void req_invoke (aio_req req); 196static void req_invoke (aio_req req);
133static void req_free (aio_req req); 197static void req_free (aio_req req);
134 198
135/* must be called at most once */ 199/* must be called at most once */
169 ENTER; 233 ENTER;
170 SAVETMPS; 234 SAVETMPS;
171 PUSHMARK (SP); 235 PUSHMARK (SP);
172 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 236 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
173 PUTBACK; 237 PUTBACK;
174 call_sv (grp->fh2, G_VOID | G_EVAL); 238 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
175 SPAGAIN; 239 SPAGAIN;
176 FREETMPS; 240 FREETMPS;
177 LEAVE; 241 LEAVE;
178 } 242 }
179 243
206{ 270{
207 fd_set rfd; 271 fd_set rfd;
208 272
209 while (nreqs) 273 while (nreqs)
210 { 274 {
211 aio_req req; 275 int size;
276#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
212 pthread_mutex_lock (&reslock); 277 pthread_mutex_lock (&reslock);
213 req = ress; 278#endif
279 size = res_queue.size;
280#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
214 pthread_mutex_unlock (&reslock); 281 pthread_mutex_unlock (&reslock);
282#endif
215 283
216 if (req) 284 if (size)
217 return; 285 return;
218 286
219 FD_ZERO(&rfd); 287 FD_ZERO(&rfd);
220 FD_SET(respipe [0], &rfd); 288 FD_SET(respipe [0], &rfd);
221 289
224} 292}
225 293
226static void req_invoke (aio_req req) 294static void req_invoke (aio_req req)
227{ 295{
228 dSP; 296 dSP;
229 int errorno = errno;
230 297
231 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) 298 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
232 return; 299 {
233
234 errno = req->errorno; 300 errno = req->errorno;
235 301
236 ENTER; 302 ENTER;
237 SAVETMPS; 303 SAVETMPS;
238 PUSHMARK (SP); 304 PUSHMARK (SP);
239 EXTEND (SP, 1); 305 EXTEND (SP, 1);
240 306
241 switch (req->type) 307 switch (req->type)
242 {
243 case REQ_READDIR:
244 { 308 {
245 SV *rv = &PL_sv_undef; 309 case REQ_READDIR:
246
247 if (req->result >= 0)
248 { 310 {
249 char *buf = req->data2ptr; 311 SV *rv = &PL_sv_undef;
250 AV *av = newAV ();
251 312
252 while (req->result) 313 if (req->result >= 0)
253 { 314 {
315 char *buf = req->data2ptr;
316 AV *av = newAV ();
317
318 while (req->result)
319 {
254 SV *sv = newSVpv (buf, 0); 320 SV *sv = newSVpv (buf, 0);
255 321
256 av_push (av, sv); 322 av_push (av, sv);
257 buf += SvCUR (sv) + 1; 323 buf += SvCUR (sv) + 1;
258 req->result--; 324 req->result--;
325 }
326
327 rv = sv_2mortal (newRV_noinc ((SV *)av));
259 } 328 }
260 329
261 rv = sv_2mortal (newRV_noinc ((SV *)av)); 330 PUSHs (rv);
262 } 331 }
332 break;
263 333
264 PUSHs (rv); 334 case REQ_OPEN:
335 {
336 /* convert fd to fh */
337 SV *fh;
338
339 PUSHs (sv_2mortal (newSViv (req->result)));
340 PUTBACK;
341 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
342 SPAGAIN;
343
344 fh = SvREFCNT_inc (POPs);
345
346 PUSHMARK (SP);
347 XPUSHs (sv_2mortal (fh));
348 }
349 break;
350
351 case REQ_GROUP:
352 req->fd = 2; /* mark group as finished */
353
354 if (req->data)
355 {
356 int i;
357 AV *av = (AV *)req->data;
358
359 EXTEND (SP, AvFILL (av) + 1);
360 for (i = 0; i <= AvFILL (av); ++i)
361 PUSHs (*av_fetch (av, i, 0));
362 }
363 break;
364
365 case REQ_NOP:
366 case REQ_SLEEP:
367 break;
368
369 default:
370 PUSHs (sv_2mortal (newSViv (req->result)));
371 break;
265 } 372 }
266 break;
267 373
268 case REQ_OPEN:
269 {
270 /* convert fd to fh */
271 SV *fh;
272 374
273 PUSHs (sv_2mortal (newSViv (req->result)));
274 PUTBACK; 375 PUTBACK;
275 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
276 SPAGAIN;
277
278 fh = SvREFCNT_inc (POPs);
279
280 PUSHMARK (SP);
281 XPUSHs (sv_2mortal (fh));
282 }
283 break;
284
285 case REQ_GROUP:
286 req->fd = 2; /* mark group as finished */
287
288 if (req->data)
289 {
290 int i;
291 AV *av = (AV *)req->data;
292
293 EXTEND (SP, AvFILL (av) + 1);
294 for (i = 0; i <= AvFILL (av); ++i)
295 PUSHs (*av_fetch (av, i, 0));
296 }
297 break;
298
299 case REQ_NOP:
300 case REQ_SLEEP:
301 break;
302
303 default:
304 PUSHs (sv_2mortal (newSViv (req->result)));
305 break;
306 }
307
308
309 PUTBACK;
310 call_sv (req->callback, G_VOID | G_EVAL); 376 call_sv (req->callback, G_VOID | G_EVAL);
311 SPAGAIN; 377 SPAGAIN;
312 378
313 FREETMPS; 379 FREETMPS;
314 LEAVE; 380 LEAVE;
315
316 errno = errorno;
317
318 if (SvTRUE (ERRSV))
319 { 381 }
320 req_free (req);
321 croak (0);
322 }
323}
324 382
325static void req_free (aio_req req)
326{
327 if (req->grp) 383 if (req->grp)
328 { 384 {
329 aio_req grp = req->grp; 385 aio_req grp = req->grp;
330 386
331 /* unlink request */ 387 /* unlink request */
336 grp->grp_first = req->grp_next; 392 grp->grp_first = req->grp_next;
337 393
338 aio_grp_dec (grp); 394 aio_grp_dec (grp);
339 } 395 }
340 396
397 if (SvTRUE (ERRSV))
398 {
399 req_free (req);
400 croak (0);
401 }
402}
403
404static void req_free (aio_req req)
405{
341 if (req->self) 406 if (req->self)
342 { 407 {
343 sv_unmagic (req->self, PERL_MAGIC_ext); 408 sv_unmagic (req->self, PERL_MAGIC_ext);
344 SvREFCNT_dec (req->self); 409 SvREFCNT_dec (req->self);
345 } 410 }
377 aio_req req; 442 aio_req req;
378 443
379 for (;;) 444 for (;;)
380 { 445 {
381 pthread_mutex_lock (&reslock); 446 pthread_mutex_lock (&reslock);
382 req = ress; 447 req = reqq_shift (&res_queue);
383 448
384 if (req) 449 if (req)
385 { 450 {
386 ress = req->next;
387
388 if (!ress) 451 if (!res_queue.size)
389 { 452 {
390 /* read any signals sent by the worker threads */ 453 /* read any signals sent by the worker threads */
391 char buf [32]; 454 char buf [32];
392 while (read (respipe [0], buf, 32) == 32) 455 while (read (respipe [0], buf, 32) == 32)
393 ; 456 ;
394
395 rese = 0;
396 } 457 }
397 } 458 }
398 459
399 pthread_mutex_unlock (&reslock); 460 pthread_mutex_unlock (&reslock);
400 461
463 start_thread (); 524 start_thread ();
464 525
465 ++nreqs; 526 ++nreqs;
466 527
467 pthread_mutex_lock (&reqlock); 528 pthread_mutex_lock (&reqlock);
468 529 reqq_push (&req_queue, req);
469 req->next = 0;
470
471 if (reqe)
472 {
473 reqe->next = req;
474 reqe = req;
475 }
476 else
477 reqe = reqs = req;
478
479 pthread_cond_signal (&reqwait); 530 pthread_cond_signal (&reqwait);
480 pthread_mutex_unlock (&reqlock); 531 pthread_mutex_unlock (&reqlock);
481 532
482 if (nreqs > max_outstanding) 533 if (nreqs > max_outstanding)
483 for (;;) 534 for (;;)
492} 543}
493 544
494static void end_thread (void) 545static void end_thread (void)
495{ 546{
496 aio_req req; 547 aio_req req;
548
497 Newz (0, req, 1, aio_cb); 549 Newz (0, req, 1, aio_cb);
550
498 req->type = REQ_QUIT; 551 req->type = REQ_QUIT;
552 req->pri = PRI_MAX + PRI_BIAS;
499 553
500 req_send (req); 554 req_send (req);
501} 555}
502 556
503static void min_parallel (int nthreads) 557static void min_parallel (int nthreads)
590#if !HAVE_READAHEAD 644#if !HAVE_READAHEAD
591# define readahead aio_readahead 645# define readahead aio_readahead
592 646
593static ssize_t readahead (int fd, off_t offset, size_t count) 647static ssize_t readahead (int fd, off_t offset, size_t count)
594{ 648{
595 char readahead_buf[4096]; 649 dBUF;
596 650
597 while (count > 0) 651 while (count > 0)
598 { 652 {
599 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 653 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
600 654
601 pread (fd, readahead_buf, len, offset); 655 pread (fd, aio_buf, len, offset);
602 offset += len; 656 offset += len;
603 count -= len; 657 count -= len;
604 } 658 }
659
660 fBUF;
605 661
606 errno = 0; 662 errno = 0;
607} 663}
608#endif 664#endif
609 665
695#endif 751#endif
696 ) 752 )
697 ) 753 )
698 { 754 {
699 /* emulate sendfile. this is a major pain in the ass */ 755 /* emulate sendfile. this is a major pain in the ass */
700 char buf[4096]; 756 dBUF;
757
701 res = 0; 758 res = 0;
702 759
703 while (count) 760 while (count)
704 { 761 {
705 ssize_t cnt; 762 ssize_t cnt;
706 763
707 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 764 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
708 765
709 if (cnt <= 0) 766 if (cnt <= 0)
710 { 767 {
711 if (cnt && !res) res = -1; 768 if (cnt && !res) res = -1;
712 break; 769 break;
713 } 770 }
714 771
715 cnt = write (ofd, buf, cnt); 772 cnt = write (ofd, aio_buf, cnt);
716 773
717 if (cnt <= 0) 774 if (cnt <= 0)
718 { 775 {
719 if (cnt && !res) res = -1; 776 if (cnt && !res) res = -1;
720 break; 777 break;
722 779
723 offset += cnt; 780 offset += cnt;
724 res += cnt; 781 res += cnt;
725 count -= cnt; 782 count -= cnt;
726 } 783 }
784
785 fBUF;
727 } 786 }
728 787
729 return res; 788 return res;
730} 789}
731 790
732/* read a full directory */ 791/* read a full directory */
733static int scandir_ (const char *path, void **namesp) 792static int scandir_ (const char *path, void **namesp)
734{ 793{
735 DIR *dirp = opendir (path); 794 DIR *dirp;
736 union 795 union
737 { 796 {
738 struct dirent d; 797 struct dirent d;
739 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 798 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
740 } u; 799 } *u;
741 struct dirent *entp; 800 struct dirent *entp;
742 char *name, *names; 801 char *name, *names;
743 int memlen = 4096; 802 int memlen = 4096;
744 int memofs = 0; 803 int memofs = 0;
745 int res = 0; 804 int res = 0;
746 int errorno; 805 int errorno;
747 806
807 dirp = opendir (path);
748 if (!dirp) 808 if (!dirp)
749 return -1; 809 return -1;
750 810
811 u = malloc (sizeof (*u));
751 names = malloc (memlen); 812 names = malloc (memlen);
752 813
814 if (u && names)
753 for (;;) 815 for (;;)
754 { 816 {
817 errno = 0;
755 errno = 0, readdir_r (dirp, &u.d, &entp); 818 readdir_r (dirp, &u->d, &entp);
756 819
757 if (!entp) 820 if (!entp)
758 break; 821 break;
759 822
760 name = entp->d_name; 823 name = entp->d_name;
761 824
762 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 825 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
763 { 826 {
764 int len = strlen (name) + 1; 827 int len = strlen (name) + 1;
765 828
766 res++; 829 res++;
767 830
768 while (memofs + len > memlen) 831 while (memofs + len > memlen)
769 { 832 {
770 memlen *= 2; 833 memlen *= 2;
771 names = realloc (names, memlen); 834 names = realloc (names, memlen);
772 if (!names) 835 if (!names)
773 break; 836 break;
774 } 837 }
775 838
776 memcpy (names + memofs, name, len); 839 memcpy (names + memofs, name, len);
777 memofs += len; 840 memofs += len;
778 } 841 }
779 } 842 }
780 843
781 errorno = errno; 844 errorno = errno;
845 free (u);
782 closedir (dirp); 846 closedir (dirp);
783 847
784 if (errorno) 848 if (errorno)
785 { 849 {
786 free (names); 850 free (names);
803 { 867 {
804 pthread_mutex_lock (&reqlock); 868 pthread_mutex_lock (&reqlock);
805 869
806 for (;;) 870 for (;;)
807 { 871 {
808 req = reqs; 872 req = reqq_shift (&req_queue);
809
810 if (reqs)
811 {
812 reqs = reqs->next;
813 if (!reqs) reqe = 0;
814 }
815 873
816 if (req) 874 if (req)
817 break; 875 break;
818 876
819 pthread_cond_wait (&reqwait, &reqlock); 877 pthread_cond_wait (&reqwait, &reqlock);
871 929
872 req->errorno = errno; 930 req->errorno = errno;
873 931
874 pthread_mutex_lock (&reslock); 932 pthread_mutex_lock (&reslock);
875 933
876 req->next = 0; 934 if (!reqq_push (&res_queue, req))
877
878 if (rese)
879 {
880 rese->next = req;
881 rese = req;
882 }
883 else
884 {
885 rese = ress = req;
886
887 /* write a dummy byte to the pipe so fh becomes ready */ 935 /* write a dummy byte to the pipe so fh becomes ready */
888 write (respipe [1], &respipe, 1); 936 write (respipe [1], &respipe, 1);
889 }
890 937
891 pthread_mutex_unlock (&reslock); 938 pthread_mutex_unlock (&reslock);
892 } 939 }
893 while (type != REQ_QUIT); 940 while (type != REQ_QUIT);
894 941
925{ 972{
926 aio_req prv; 973 aio_req prv;
927 974
928 started = 0; 975 started = 0;
929 976
930 while (reqs) 977 while (prv = reqq_shift (&req_queue))
931 {
932 prv = reqs;
933 reqs = prv->next;
934 req_free (prv); 978 req_free (prv);
935 }
936 979
937 reqs = reqe = 0; 980 while (prv = reqq_shift (&res_queue))
938
939 while (ress)
940 {
941 prv = ress;
942 ress = prv->next;
943 req_free (prv); 981 req_free (prv);
944 } 982
945
946 ress = rese = 0;
947
948 close (respipe [0]); 983 close (respipe [0]);
949 close (respipe [1]); 984 close (respipe [1]);
950 create_pipe (); 985 create_pipe ();
951 986
952 atfork_parent (); 987 atfork_parent ();
1267 req->type = REQ_NOP; 1302 req->type = REQ_NOP;
1268 1303
1269 REQ_SEND; 1304 REQ_SEND;
1270} 1305}
1271 1306
1272#if 0
1273
1274void 1307void
1275aio_pri (int pri = DEFAULT_PRI) 1308aioreq_pri (int pri = DEFAULT_PRI)
1276 CODE: 1309 CODE:
1277 if (pri < PRI_MIN) pri = PRI_MIN; 1310 if (pri < PRI_MIN) pri = PRI_MIN;
1278 if (pri > PRI_MAX) pri = PRI_MAX; 1311 if (pri > PRI_MAX) pri = PRI_MAX;
1279 next_pri = pri + PRI_BIAS; 1312 next_pri = pri + PRI_BIAS;
1280
1281#endif
1282 1313
1283void 1314void
1284flush () 1315flush ()
1285 PROTOTYPE: 1316 PROTOTYPE:
1286 CODE: 1317 CODE:
1335 1366
1336MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1367MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1337 1368
1338void 1369void
1339cancel (aio_req_ornot req) 1370cancel (aio_req_ornot req)
1340 PROTOTYPE:
1341 CODE: 1371 CODE:
1342 req_cancel (req); 1372 req_cancel (req);
1343 1373
1344void 1374void
1345cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1375cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1395 SvREFCNT_dec (grp->data); 1425 SvREFCNT_dec (grp->data);
1396 grp->data = (SV *)av; 1426 grp->data = (SV *)av;
1397} 1427}
1398 1428
1399void 1429void
1400feed_limit (aio_req grp, int limit) 1430limit (aio_req grp, int limit)
1401 CODE: 1431 CODE:
1402 grp->fd2 = limit; 1432 grp->fd2 = limit;
1403 aio_grp_feed (grp); 1433 aio_grp_feed (grp);
1404 1434
1405void 1435void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines