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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines