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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines