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.33 by root, Mon Aug 22 23:21:57 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>
31# else
32# error sendfile support requested but not available
28# endif 33# endif
29#endif 34#endif
30 35
31#if __ia64 36#if __ia64
32# define STACKSIZE 65536 37# define STACKSIZE 65536
33#else 38#else
34# define STACKSIZE 4096 39# define STACKSIZE 8192
35#endif 40#endif
36 41
37enum { 42enum {
38 REQ_QUIT, 43 REQ_QUIT,
39 REQ_OPEN, REQ_CLOSE, 44 REQ_OPEN, REQ_CLOSE,
40 REQ_READ, REQ_WRITE, REQ_READAHEAD, 45 REQ_READ, REQ_WRITE, REQ_READAHEAD,
41 REQ_SENDFILE, 46 REQ_SENDFILE,
42 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 47 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
43 REQ_FSYNC, REQ_FDATASYNC, 48 REQ_FSYNC, REQ_FDATASYNC,
44 REQ_UNLINK, REQ_RMDIR, 49 REQ_UNLINK, REQ_RMDIR,
50 REQ_READDIR,
45 REQ_SYMLINK, 51 REQ_SYMLINK,
46}; 52};
47 53
48typedef struct aio_cb { 54typedef struct aio_cb {
49 struct aio_cb *volatile next; 55 struct aio_cb *volatile next;
50 56
51 int type; 57 int type;
52 58
59 /* should receive a cleanup, with unions */
53 int fd, fd2; 60 int fd, fd2;
54 off_t offset; 61 off_t offset;
55 size_t length; 62 size_t length;
56 ssize_t result; 63 ssize_t result;
57 mode_t mode; /* open */ 64 mode_t mode; /* open */
93 Safefree (req->statdata); 100 Safefree (req->statdata);
94 101
95 if (req->callback) 102 if (req->callback)
96 SvREFCNT_dec (req->callback); 103 SvREFCNT_dec (req->callback);
97 104
105 if (req->type == REQ_READDIR && req->result >= 0)
106 free (req->data2ptr);
107
98 Safefree (req); 108 Safefree (req);
99} 109}
100 110
101static void 111static void
102poll_wait () 112poll_wait ()
166 PL_statcache = *(req->statdata); 176 PL_statcache = *(req->statdata);
167 } 177 }
168 178
169 ENTER; 179 ENTER;
170 PUSHMARK (SP); 180 PUSHMARK (SP);
171 XPUSHs (sv_2mortal (newSViv (req->result)));
172 181
173 if (req->type == REQ_OPEN) 182 if (req->type == REQ_READDIR)
174 { 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 {
175 /* convert fd to fh */ 211 /* convert fd to fh */
176 SV *fh; 212 SV *fh;
177 213
178 PUTBACK; 214 PUTBACK;
179 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 215 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
180 SPAGAIN; 216 SPAGAIN;
181 217
182 fh = SvREFCNT_inc (POPs); 218 fh = SvREFCNT_inc (POPs);
183 219
184 PUSHMARK (SP); 220 PUSHMARK (SP);
185 XPUSHs (sv_2mortal (fh)); 221 XPUSHs (sv_2mortal (fh));
222 }
186 } 223 }
187 224
188 if (SvOK (req->callback)) 225 if (SvOK (req->callback))
189 { 226 {
190 PUTBACK; 227 PUTBACK;
313 350
314 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 351 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
315 croak ("cannot set result pipe to nonblocking mode"); 352 croak ("cannot set result pipe to nonblocking mode");
316} 353}
317 354
318static void atfork_prepare (void)
319{
320 pthread_mutex_lock (&reqlock);
321 pthread_mutex_lock (&reslock);
322}
323
324static void atfork_parent (void)
325{
326 pthread_mutex_unlock (&reslock);
327 pthread_mutex_unlock (&reqlock);
328}
329
330static void atfork_child (void)
331{
332 aio_req prv;
333
334 started = 0;
335
336 while (reqs)
337 {
338 prv = reqs;
339 reqs = prv->next;
340 free_req (prv);
341 }
342
343 reqs = reqe = 0;
344
345 while (ress)
346 {
347 prv = ress;
348 ress = prv->next;
349 free_req (prv);
350 }
351
352 ress = rese = 0;
353
354 close (respipe [0]);
355 close (respipe [1]);
356 create_pipe ();
357
358 atfork_parent ();
359}
360
361/*****************************************************************************/ 355/*****************************************************************************/
362/* work around various missing functions */ 356/* work around various missing functions */
363 357
364#if !HAVE_PREADWRITE 358#if !HAVE_PREADWRITE
365# define pread aio_pread 359# define pread aio_pread
368/* 362/*
369 * make our pread/pwrite safe against themselves, but not against 363 * make our pread/pwrite safe against themselves, but not against
370 * 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,
371 * but that's your problem, not mine. 365 * but that's your problem, not mine.
372 */ 366 */
373static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 367static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
374 368
375static ssize_t 369static ssize_t
376pread (int fd, void *buf, size_t count, off_t offset) 370pread (int fd, void *buf, size_t count, off_t offset)
377{ 371{
378 ssize_t res; 372 ssize_t res;
379 off_t ooffset; 373 off_t ooffset;
380 374
381 pthread_mutex_lock (&iolock); 375 pthread_mutex_lock (&preadwritelock);
382 ooffset = lseek (fd, 0, SEEK_CUR); 376 ooffset = lseek (fd, 0, SEEK_CUR);
383 lseek (fd, offset, SEEK_SET); 377 lseek (fd, offset, SEEK_SET);
384 res = read (fd, buf, count); 378 res = read (fd, buf, count);
385 lseek (fd, ooffset, SEEK_SET); 379 lseek (fd, ooffset, SEEK_SET);
386 pthread_mutex_unlock (&iolock); 380 pthread_mutex_unlock (&preadwritelock);
387 381
388 return res; 382 return res;
389} 383}
390 384
391static ssize_t 385static ssize_t
392pwrite (int fd, void *buf, size_t count, off_t offset) 386pwrite (int fd, void *buf, size_t count, off_t offset)
393{ 387{
394 ssize_t res; 388 ssize_t res;
395 off_t ooffset; 389 off_t ooffset;
396 390
397 pthread_mutex_lock (&iolock); 391 pthread_mutex_lock (&preadwritelock);
398 ooffset = lseek (fd, 0, SEEK_CUR); 392 ooffset = lseek (fd, 0, SEEK_CUR);
399 lseek (fd, offset, SEEK_SET); 393 lseek (fd, offset, SEEK_SET);
400 res = write (fd, buf, count); 394 res = write (fd, buf, count);
401 lseek (fd, offset, SEEK_SET); 395 lseek (fd, offset, SEEK_SET);
402 pthread_mutex_unlock (&iolock); 396 pthread_mutex_unlock (&preadwritelock);
403 397
404 return res; 398 return res;
405} 399}
406#endif 400#endif
407 401
410#endif 404#endif
411 405
412#if !HAVE_READAHEAD 406#if !HAVE_READAHEAD
413# define readahead aio_readahead 407# define readahead aio_readahead
414 408
415static char readahead_buf[4096];
416
417static ssize_t 409static ssize_t
418readahead (int fd, off_t offset, size_t count) 410readahead (int fd, off_t offset, size_t count)
419{ 411{
412 char readahead_buf[4096];
413
420 while (count > 0) 414 while (count > 0)
421 { 415 {
422 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 416 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
423 417
424 pread (fd, readahead_buf, len, offset); 418 pread (fd, readahead_buf, len, offset);
428 422
429 errno = 0; 423 errno = 0;
430} 424}
431#endif 425#endif
432 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
433/* sendfile always needs emulation */ 458/* sendfile always needs emulation */
434static ssize_t 459static ssize_t
435sendfile_ (int ofd, int ifd, off_t offset, size_t count) 460sendfile_ (int ofd, int ifd, off_t offset, size_t count)
436{ 461{
437 ssize_t res; 462 ssize_t res;
438 463
439 if (!count) 464 if (!count)
440 return 0; 465 return 0;
441 466
467#if HAVE_SENDFILE
442#if __linux 468# if __linux
443 res = sendfile (ofd, ifd, &offset, count); 469 res = sendfile (ofd, ifd, &offset, count);
444 470
445#elif __freebsd 471# elif __freebsd
446 /* 472 /*
447 * 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
448 * wasted on making it similar to other i/o functions. 474 * wasted on making it similar to other I/O functions.
449 */ 475 */
450 { 476 {
451 off_t sbytes; 477 off_t sbytes;
452 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 478 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
453 479
454 if (!res && errno == EAGAIN) 480 if (res < 0 && sbytes)
455 /* maybe on others, too, as usual, the manpage leaves you guessing */ 481 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
456 res = sbytes; 482 res = sbytes;
457 } 483 }
458 484
459#elif __hpux 485# elif __hpux
460 res = sendfile (ofd, ifd, offset, count, 0, 0); 486 res = sendfile (ofd, ifd, offset, count, 0, 0);
461 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
462#else 504# else
463 res = -1; 505 res = -1;
464 errno = ENOSYS; 506 errno = ENOSYS;
465#endif 507# endif
508#endif
466 509
510 if (res < 0
467 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 )
468 { 517 {
469 /* emulate sendfile. this is a major pain in the ass */ 518 /* emulate sendfile. this is a major pain in the ass */
470 char *buf = malloc (4096); 519 char buf[4096];
471 res = 0; 520 res = 0;
472 521
473 for (;;) 522 for (;;)
474 { 523 {
475 ssize_t cnt; 524 ssize_t cnt;
476 525
477 cnt = pread (ifd, buf, 4096, offset); 526 cnt = pread (ifd, buf, 4096, offset);
478 527
479 if (cnt <= 0) 528 if (cnt <= 0)
480 { 529 {
481 if (cnt && !res) res = -1; 530 if (cnt && !res) res = -1;
482 break; 531 break;
483 } 532 }
484 533
485 cnt = write (ofd, buf, cnt); 534 cnt = write (ofd, buf, cnt);
486 535
487 if (cnt <= 0) 536 if (cnt <= 0)
488 { 537 {
489 if (cnt && !res) res = -1; 538 if (cnt && !res) res = -1;
490 break; 539 break;
491 } 540 }
492 541
493 offset += cnt; 542 offset += cnt;
494 res += cnt; 543 res += cnt;
495 } 544 }
496
497 {
498 int errorno = errno;
499 free (buf);
500 errno = errorno;
501 }
502 } 545 }
503 546
504 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;
505} 612}
506 613
507/*****************************************************************************/ 614/*****************************************************************************/
508 615
509static void * 616static void *
556 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 663 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
557 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 664 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
558 665
559 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 666 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
560 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;
561 669
562 case REQ_QUIT: 670 case REQ_QUIT:
563 break; 671 break;
564 672
565 default: 673 default:
589 pthread_mutex_unlock (&reslock); 697 pthread_mutex_unlock (&reslock);
590 } 698 }
591 while (type != REQ_QUIT); 699 while (type != REQ_QUIT);
592 700
593 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 ();
594} 759}
595 760
596#define dREQ \ 761#define dREQ \
597 aio_req req; \ 762 aio_req req; \
598 \ 763 \
849 1014
850 send_req (req); 1015 send_req (req);
851} 1016}
852 1017
853void 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
854flush() 1034flush()
855 PROTOTYPE: 1035 PROTOTYPE:
856 CODE: 1036 CODE:
857 while (nreqs) 1037 while (nreqs)
858 { 1038 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines