--- IO-AIO/AIO.xs 2005/08/23 00:03:14 1.34 +++ IO-AIO/AIO.xs 2005/08/23 01:16:50 1.35 @@ -25,6 +25,8 @@ # include # elif __hpux # include +# elif __solaris /* not yet */ +# include # else # error sendfile support requested but not available # endif @@ -360,6 +362,10 @@ atfork_parent (); } +/* currently noops */ +#define LOCK_FD(fd) do { } while (0) +#define UNLOCK_FD(fd) do { } while (0) + /*****************************************************************************/ /* work around various missing functions */ @@ -380,12 +386,14 @@ ssize_t res; off_t ooffset; - pthread_mutex_lock (&iolock); + LOCK_FD (fd); + pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */ ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = read (fd, buf, count); lseek (fd, ooffset, SEEK_SET); pthread_mutex_unlock (&iolock); + UNLOCK_FD (d); return res; } @@ -396,12 +404,14 @@ ssize_t res; off_t ooffset; - pthread_mutex_lock (&iolock); + LOCK_FD (fd); + pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */ ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = write (fd, buf, count); lseek (fd, offset, SEEK_SET); pthread_mutex_unlock (&iolock); + UNLOCK_FD (d); return res; } @@ -441,32 +451,58 @@ if (!count) return 0; -#if __linux + LOCK_FD (ofd); + +#if HAVE_SENDFILE +# if __linux res = sendfile (ofd, ifd, &offset, count); -#elif __freebsd +# elif __freebsd /* * Of course, the freebsd sendfile is a dire hack with no thoughts - * wasted on making it similar to other i/o functions. + * wasted on making it similar to other I/O functions. */ { off_t sbytes; res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); - if (!res && errno == EAGAIN) - /* maybe on others, too, as usual, the manpage leaves you guessing */ + if (res < 0 && sbytes) + /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ res = sbytes; } -#elif __hpux +# elif __hpux res = sendfile (ofd, ifd, offset, count, 0, 0); -#else +# elif __solaris + { + struct sendfilevec vec; + size_t sbytes; + + vec.sfv_fd = ifd; + vec.sfv_flag = 0; + vec.sfv_off = offset; + vec.sfv_len = count; + + res = sendfilev (ofd, &vec, 1, &sbytes); + + if (res < 0 && sbytes) + res = sbytes; + } + +# else res = -1; errno = ENOSYS; +# endif #endif - if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) + if (res < 0 + && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK +#if __solaris + || errno == EAFNOSUPPORT || errno == EPROTOTYPE +#endif + ) + ) { /* emulate sendfile. this is a major pain in the ass */ char *buf = malloc (4096); @@ -503,6 +539,8 @@ } } + UNLOCK_FD (ofd); + return res; }