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.34 by root, Tue Aug 23 00:03:14 2005 UTC vs.
Revision 1.39 by root, Mon Feb 6 23:54:12 2006 UTC

7 7
8#include "autoconf/config.h" 8#include "autoconf/config.h"
9 9
10#include <pthread.h> 10#include <pthread.h>
11 11
12#include <stddef.h>
12#include <sys/types.h> 13#include <sys/types.h>
13#include <sys/stat.h> 14#include <sys/stat.h>
14 15#include <limits.h>
15#include <unistd.h> 16#include <unistd.h>
16#include <fcntl.h> 17#include <fcntl.h>
17#include <signal.h> 18#include <signal.h>
18#include <sched.h> 19#include <sched.h>
19 20
23# elif __freebsd 24# elif __freebsd
24# include <sys/socket.h> 25# include <sys/socket.h>
25# include <sys/uio.h> 26# include <sys/uio.h>
26# elif __hpux 27# elif __hpux
27# include <sys/socket.h> 28# include <sys/socket.h>
29# elif __solaris /* not yet */
30# include <sys/sendfile.h>
28# else 31# else
29# error sendfile support requested but not available 32# error sendfile support requested but not available
30# endif 33# endif
31#endif 34#endif
32 35
36/* used for struct dirent, AIX doesn't provide it */
37#ifndef NAME_MAX
38# define NAME_MAX 4096
39#endif
40
33#if __ia64 41#if __ia64
34# define STACKSIZE 65536 42# define STACKSIZE 65536
35#else 43#else
36# define STACKSIZE 4096 44# define STACKSIZE 8192
37#endif 45#endif
38 46
39enum { 47enum {
40 REQ_QUIT, 48 REQ_QUIT,
41 REQ_OPEN, REQ_CLOSE, 49 REQ_OPEN, REQ_CLOSE,
42 REQ_READ, REQ_WRITE, REQ_READAHEAD, 50 REQ_READ, REQ_WRITE, REQ_READAHEAD,
43 REQ_SENDFILE, 51 REQ_SENDFILE,
44 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 52 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
45 REQ_FSYNC, REQ_FDATASYNC, 53 REQ_FSYNC, REQ_FDATASYNC,
46 REQ_UNLINK, REQ_RMDIR, 54 REQ_UNLINK, REQ_RMDIR,
55 REQ_READDIR,
47 REQ_SYMLINK, 56 REQ_SYMLINK,
48}; 57};
49 58
50typedef struct aio_cb { 59typedef struct aio_cb {
51 struct aio_cb *volatile next; 60 struct aio_cb *volatile next;
52 61
53 int type; 62 int type;
54 63
64 /* should receive a cleanup, with unions */
55 int fd, fd2; 65 int fd, fd2;
56 off_t offset; 66 off_t offset;
57 size_t length; 67 size_t length;
58 ssize_t result; 68 ssize_t result;
59 mode_t mode; /* open */ 69 mode_t mode; /* open */
95 Safefree (req->statdata); 105 Safefree (req->statdata);
96 106
97 if (req->callback) 107 if (req->callback)
98 SvREFCNT_dec (req->callback); 108 SvREFCNT_dec (req->callback);
99 109
110 if (req->type == REQ_READDIR && req->result >= 0)
111 free (req->data2ptr);
112
100 Safefree (req); 113 Safefree (req);
101} 114}
102 115
103static void 116static void
104poll_wait () 117poll_wait ()
168 PL_statcache = *(req->statdata); 181 PL_statcache = *(req->statdata);
169 } 182 }
170 183
171 ENTER; 184 ENTER;
172 PUSHMARK (SP); 185 PUSHMARK (SP);
173 XPUSHs (sv_2mortal (newSViv (req->result)));
174 186
175 if (req->type == REQ_OPEN) 187 if (req->type == REQ_READDIR)
176 { 188 {
189 SV *rv = &PL_sv_undef;
190
191 if (req->result >= 0)
192 {
193 char *buf = req->data2ptr;
194 AV *av = newAV ();
195
196 while (req->result)
197 {
198 SV *sv = newSVpv (buf, 0);
199
200 av_push (av, sv);
201 buf += SvCUR (sv) + 1;
202 req->result--;
203 }
204
205 rv = sv_2mortal (newRV_noinc ((SV *)av));
206 }
207
208 XPUSHs (rv);
209 }
210 else
211 {
212 XPUSHs (sv_2mortal (newSViv (req->result)));
213
214 if (req->type == REQ_OPEN)
215 {
177 /* convert fd to fh */ 216 /* convert fd to fh */
178 SV *fh; 217 SV *fh;
179 218
180 PUTBACK; 219 PUTBACK;
181 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 220 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
182 SPAGAIN; 221 SPAGAIN;
183 222
184 fh = SvREFCNT_inc (POPs); 223 fh = SvREFCNT_inc (POPs);
185 224
186 PUSHMARK (SP); 225 PUSHMARK (SP);
187 XPUSHs (sv_2mortal (fh)); 226 XPUSHs (sv_2mortal (fh));
227 }
188 } 228 }
189 229
190 if (SvOK (req->callback)) 230 if (SvOK (req->callback))
191 { 231 {
192 PUTBACK; 232 PUTBACK;
315 355
316 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 356 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
317 croak ("cannot set result pipe to nonblocking mode"); 357 croak ("cannot set result pipe to nonblocking mode");
318} 358}
319 359
320static void atfork_prepare (void)
321{
322 pthread_mutex_lock (&reqlock);
323 pthread_mutex_lock (&reslock);
324}
325
326static void atfork_parent (void)
327{
328 pthread_mutex_unlock (&reslock);
329 pthread_mutex_unlock (&reqlock);
330}
331
332static void atfork_child (void)
333{
334 aio_req prv;
335
336 started = 0;
337
338 while (reqs)
339 {
340 prv = reqs;
341 reqs = prv->next;
342 free_req (prv);
343 }
344
345 reqs = reqe = 0;
346
347 while (ress)
348 {
349 prv = ress;
350 ress = prv->next;
351 free_req (prv);
352 }
353
354 ress = rese = 0;
355
356 close (respipe [0]);
357 close (respipe [1]);
358 create_pipe ();
359
360 atfork_parent ();
361}
362
363/*****************************************************************************/ 360/*****************************************************************************/
364/* work around various missing functions */ 361/* work around various missing functions */
365 362
366#if !HAVE_PREADWRITE 363#if !HAVE_PREADWRITE
367# define pread aio_pread 364# define pread aio_pread
370/* 367/*
371 * make our pread/pwrite safe against themselves, but not against 368 * make our pread/pwrite safe against themselves, but not against
372 * normal read/write by using a mutex. slows down execution a lot, 369 * normal read/write by using a mutex. slows down execution a lot,
373 * but that's your problem, not mine. 370 * but that's your problem, not mine.
374 */ 371 */
375static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 372static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
376 373
377static ssize_t 374static ssize_t
378pread (int fd, void *buf, size_t count, off_t offset) 375pread (int fd, void *buf, size_t count, off_t offset)
379{ 376{
380 ssize_t res; 377 ssize_t res;
381 off_t ooffset; 378 off_t ooffset;
382 379
383 pthread_mutex_lock (&iolock); 380 pthread_mutex_lock (&preadwritelock);
384 ooffset = lseek (fd, 0, SEEK_CUR); 381 ooffset = lseek (fd, 0, SEEK_CUR);
385 lseek (fd, offset, SEEK_SET); 382 lseek (fd, offset, SEEK_SET);
386 res = read (fd, buf, count); 383 res = read (fd, buf, count);
387 lseek (fd, ooffset, SEEK_SET); 384 lseek (fd, ooffset, SEEK_SET);
388 pthread_mutex_unlock (&iolock); 385 pthread_mutex_unlock (&preadwritelock);
389 386
390 return res; 387 return res;
391} 388}
392 389
393static ssize_t 390static ssize_t
394pwrite (int fd, void *buf, size_t count, off_t offset) 391pwrite (int fd, void *buf, size_t count, off_t offset)
395{ 392{
396 ssize_t res; 393 ssize_t res;
397 off_t ooffset; 394 off_t ooffset;
398 395
399 pthread_mutex_lock (&iolock); 396 pthread_mutex_lock (&preadwritelock);
400 ooffset = lseek (fd, 0, SEEK_CUR); 397 ooffset = lseek (fd, 0, SEEK_CUR);
401 lseek (fd, offset, SEEK_SET); 398 lseek (fd, offset, SEEK_SET);
402 res = write (fd, buf, count); 399 res = write (fd, buf, count);
403 lseek (fd, offset, SEEK_SET); 400 lseek (fd, offset, SEEK_SET);
404 pthread_mutex_unlock (&iolock); 401 pthread_mutex_unlock (&preadwritelock);
405 402
406 return res; 403 return res;
407} 404}
408#endif 405#endif
409 406
412#endif 409#endif
413 410
414#if !HAVE_READAHEAD 411#if !HAVE_READAHEAD
415# define readahead aio_readahead 412# define readahead aio_readahead
416 413
417static char readahead_buf[4096];
418
419static ssize_t 414static ssize_t
420readahead (int fd, off_t offset, size_t count) 415readahead (int fd, off_t offset, size_t count)
421{ 416{
417 char readahead_buf[4096];
418
422 while (count > 0) 419 while (count > 0)
423 { 420 {
424 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 421 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
425 422
426 pread (fd, readahead_buf, len, offset); 423 pread (fd, readahead_buf, len, offset);
430 427
431 errno = 0; 428 errno = 0;
432} 429}
433#endif 430#endif
434 431
432#if !HAVE_READDIR_R
433# define readdir_r aio_readdir_r
434
435static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
436
437static int
438readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
439{
440 struct dirent *e;
441 int errorno;
442
443 pthread_mutex_lock (&readdirlock);
444
445 e = readdir (dirp);
446 errorno = errno;
447
448 if (e)
449 {
450 *res = ent;
451 strcpy (ent->d_name, e->d_name);
452 }
453 else
454 *res = 0;
455
456 pthread_mutex_unlock (&readdirlock);
457
458 errno = errorno;
459 return e ? 0 : -1;
460}
461#endif
462
435/* sendfile always needs emulation */ 463/* sendfile always needs emulation */
436static ssize_t 464static ssize_t
437sendfile_ (int ofd, int ifd, off_t offset, size_t count) 465sendfile_ (int ofd, int ifd, off_t offset, size_t count)
438{ 466{
439 ssize_t res; 467 ssize_t res;
440 468
441 if (!count) 469 if (!count)
442 return 0; 470 return 0;
443 471
472#if HAVE_SENDFILE
444#if __linux 473# if __linux
445 res = sendfile (ofd, ifd, &offset, count); 474 res = sendfile (ofd, ifd, &offset, count);
446 475
447#elif __freebsd 476# elif __freebsd
448 /* 477 /*
449 * Of course, the freebsd sendfile is a dire hack with no thoughts 478 * Of course, the freebsd sendfile is a dire hack with no thoughts
450 * wasted on making it similar to other i/o functions. 479 * wasted on making it similar to other I/O functions.
451 */ 480 */
452 { 481 {
453 off_t sbytes; 482 off_t sbytes;
454 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 483 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
455 484
456 if (!res && errno == EAGAIN) 485 if (res < 0 && sbytes)
457 /* maybe on others, too, as usual, the manpage leaves you guessing */ 486 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
458 res = sbytes; 487 res = sbytes;
459 } 488 }
460 489
461#elif __hpux 490# elif __hpux
462 res = sendfile (ofd, ifd, offset, count, 0, 0); 491 res = sendfile (ofd, ifd, offset, count, 0, 0);
463 492
493# elif __solaris
494 {
495 struct sendfilevec vec;
496 size_t sbytes;
497
498 vec.sfv_fd = ifd;
499 vec.sfv_flag = 0;
500 vec.sfv_off = offset;
501 vec.sfv_len = count;
502
503 res = sendfilev (ofd, &vec, 1, &sbytes);
504
505 if (res < 0 && sbytes)
506 res = sbytes;
507 }
508
509# endif
464#else 510#else
465 res = -1; 511 res = -1;
466 errno = ENOSYS; 512 errno = ENOSYS;
467#endif 513#endif
468 514
515 if (res < 0
469 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) 516 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
517#if __solaris
518 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
519#endif
520 )
521 )
470 { 522 {
471 /* emulate sendfile. this is a major pain in the ass */ 523 /* emulate sendfile. this is a major pain in the ass */
472 char *buf = malloc (4096); 524 char buf[4096];
473 res = 0; 525 res = 0;
474 526
475 for (;;) 527 while (count)
476 { 528 {
477 ssize_t cnt; 529 ssize_t cnt;
478 530
479 cnt = pread (ifd, buf, 4096, offset); 531 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
480 532
481 if (cnt <= 0) 533 if (cnt <= 0)
482 { 534 {
483 if (cnt && !res) res = -1; 535 if (cnt && !res) res = -1;
484 break; 536 break;
485 } 537 }
486 538
487 cnt = write (ofd, buf, cnt); 539 cnt = write (ofd, buf, cnt);
488 540
489 if (cnt <= 0) 541 if (cnt <= 0)
490 { 542 {
491 if (cnt && !res) res = -1; 543 if (cnt && !res) res = -1;
492 break; 544 break;
493 } 545 }
494 546
495 offset += cnt; 547 offset += cnt;
496 res += cnt; 548 res += cnt;
549 count -= cnt;
497 } 550 }
498
499 {
500 int errorno = errno;
501 free (buf);
502 errno = errorno;
503 }
504 } 551 }
505 552
506 return res; 553 return res;
554}
555
556/* read a full directory */
557static int
558scandir_ (const char *path, void **namesp)
559{
560 DIR *dirp = opendir (path);
561 union
562 {
563 struct dirent d;
564 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
565 } u;
566 struct dirent *entp;
567 char *name, *names;
568 int memlen = 4096;
569 int memofs = 0;
570 int res = 0;
571 int errorno;
572
573 if (!dirp)
574 return -1;
575
576 names = malloc (memlen);
577
578 for (;;)
579 {
580 errno = 0, readdir_r (dirp, &u.d, &entp);
581
582 if (!entp)
583 break;
584
585 name = entp->d_name;
586
587 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
588 {
589 int len = strlen (name) + 1;
590
591 res++;
592
593 while (memofs + len > memlen)
594 {
595 memlen *= 2;
596 names = realloc (names, memlen);
597 if (!names)
598 break;
599 }
600
601 memcpy (names + memofs, name, len);
602 memofs += len;
603 }
604 }
605
606 errorno = errno;
607 closedir (dirp);
608
609 if (errorno)
610 {
611 free (names);
612 errno = errorno;
613 res = -1;
614 }
615
616 *namesp = (void *)names;
617 return res;
507} 618}
508 619
509/*****************************************************************************/ 620/*****************************************************************************/
510 621
511static void * 622static void *
558 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 669 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
559 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 670 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
560 671
561 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 672 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
562 case REQ_FSYNC: req->result = fsync (req->fd); break; 673 case REQ_FSYNC: req->result = fsync (req->fd); break;
674 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
563 675
564 case REQ_QUIT: 676 case REQ_QUIT:
565 break; 677 break;
566 678
567 default: 679 default:
591 pthread_mutex_unlock (&reslock); 703 pthread_mutex_unlock (&reslock);
592 } 704 }
593 while (type != REQ_QUIT); 705 while (type != REQ_QUIT);
594 706
595 return 0; 707 return 0;
708}
709
710/*****************************************************************************/
711
712static void atfork_prepare (void)
713{
714 pthread_mutex_lock (&reqlock);
715 pthread_mutex_lock (&reslock);
716#if !HAVE_PREADWRITE
717 pthread_mutex_lock (&preadwritelock);
718#endif
719#if !HAVE_READDIR_R
720 pthread_mutex_lock (&readdirlock);
721#endif
722}
723
724static void atfork_parent (void)
725{
726#if !HAVE_READDIR_R
727 pthread_mutex_unlock (&readdirlock);
728#endif
729#if !HAVE_PREADWRITE
730 pthread_mutex_unlock (&preadwritelock);
731#endif
732 pthread_mutex_unlock (&reslock);
733 pthread_mutex_unlock (&reqlock);
734}
735
736static void atfork_child (void)
737{
738 aio_req prv;
739
740 started = 0;
741
742 while (reqs)
743 {
744 prv = reqs;
745 reqs = prv->next;
746 free_req (prv);
747 }
748
749 reqs = reqe = 0;
750
751 while (ress)
752 {
753 prv = ress;
754 ress = prv->next;
755 free_req (prv);
756 }
757
758 ress = rese = 0;
759
760 close (respipe [0]);
761 close (respipe [1]);
762 create_pipe ();
763
764 atfork_parent ();
596} 765}
597 766
598#define dREQ \ 767#define dREQ \
599 aio_req req; \ 768 aio_req req; \
600 \ 769 \
851 1020
852 send_req (req); 1021 send_req (req);
853} 1022}
854 1023
855void 1024void
1025aio_readdir(pathname,callback=&PL_sv_undef)
1026 SV * pathname
1027 SV * callback
1028 CODE:
1029{
1030 dREQ;
1031
1032 req->type = REQ_READDIR;
1033 req->data = newSVsv (pathname);
1034 req->dataptr = SvPVbyte_nolen (req->data);
1035
1036 send_req (req);
1037}
1038
1039void
856flush() 1040flush()
857 PROTOTYPE: 1041 PROTOTYPE:
858 CODE: 1042 CODE:
859 while (nreqs) 1043 while (nreqs)
860 { 1044 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines