ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.43 by root, Wed Jun 17 18:29:15 2009 UTC vs.
Revision 1.49 by root, Sat Jan 2 14:24:32 2010 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libeio@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
80# include <utime.h> 80# include <utime.h>
81# include <signal.h> 81# include <signal.h>
82# include <dirent.h> 82# include <dirent.h>
83 83
84/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 84/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
85# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 85# if __freebsd || defined __NetBSD__ || defined __OpenBSD__
86# define _DIRENT_HAVE_D_TYPE /* sigh */ 86# define _DIRENT_HAVE_D_TYPE /* sigh */
87# define D_INO(de) (de)->d_fileno 87# define D_INO(de) (de)->d_fileno
88# define D_NAMLEN(de) (de)->d_namlen 88# define D_NAMLEN(de) (de)->d_namlen
89# elif defined(__linux) || defined(d_ino) || _XOPEN_SOURCE >= 600 89# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
90# define D_INO(de) (de)->d_ino 90# define D_INO(de) (de)->d_ino
91# endif 91# endif
92 92
93#ifdef _D_EXACT_NAMLEN 93#ifdef _D_EXACT_NAMLEN
94# undef D_NAMLEN 94# undef D_NAMLEN
106#endif 106#endif
107 107
108#if HAVE_SENDFILE 108#if HAVE_SENDFILE
109# if __linux 109# if __linux
110# include <sys/sendfile.h> 110# include <sys/sendfile.h>
111# elif __freebsd 111# elif __freebsd || defined __APPLE__
112# include <sys/socket.h> 112# include <sys/socket.h>
113# include <sys/uio.h> 113# include <sys/uio.h>
114# elif __hpux 114# elif __hpux
115# include <sys/socket.h> 115# include <sys/socket.h>
116# elif __solaris /* not yet */ 116# elif __solaris
117# include <sys/sendfile.h> 117# include <sys/sendfile.h>
118# else 118# else
119# error sendfile support requested but not available 119# error sendfile support requested but not available
120# endif 120# endif
121#endif 121#endif
196 196
197/*****************************************************************************/ 197/*****************************************************************************/
198 198
199#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 199#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
200 200
201/* calculcate time difference in ~1/EIO_TICKS of a second */ 201/* calculate time difference in ~1/EIO_TICKS of a second */
202static int tvdiff (struct timeval *tv1, struct timeval *tv2) 202static int tvdiff (struct timeval *tv1, struct timeval *tv2)
203{ 203{
204 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 204 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
205 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 205 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
206} 206}
598} 598}
599 599
600static void etp_set_max_poll_time (double nseconds) 600static void etp_set_max_poll_time (double nseconds)
601{ 601{
602 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 602 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
603 max_poll_time = nseconds; 603 max_poll_time = nseconds * EIO_TICKS;
604 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 604 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
605} 605}
606 606
607static void etp_set_max_poll_reqs (unsigned int maxreqs) 607static void etp_set_max_poll_reqs (unsigned int maxreqs)
608{ 608{
923 if (res < 0 && sbytes) 923 if (res < 0 && sbytes)
924 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ 924 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
925 res = sbytes; 925 res = sbytes;
926 } 926 }
927 927
928# elif defined (__APPLE__)
929
930 {
931 off_t sbytes = count;
932 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
933
934 if (res < 0 && errno == EAGAIN && sbytes)
935 res = sbytes;
936 }
937
928# elif __hpux 938# elif __hpux
929 res = sendfile (ofd, ifd, offset, count, 0, 0); 939 res = sendfile (ofd, ifd, offset, count, 0, 0);
930 940
931# elif __solaris 941# elif __solaris
932 { 942 {
943 if (res < 0 && sbytes) 953 if (res < 0 && sbytes)
944 res = sbytes; 954 res = sbytes;
945 } 955 }
946 956
947# endif 957# endif
958
959#elif defined (_WIN32)
960
961 /* does not work, just for documentation of what would need to be done */
962 {
963 HANDLE h = TO_SOCKET (ifd);
964 SetFilePointer (h, offset, 0, FILE_BEGIN);
965 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
966 }
967
948#else 968#else
949 res = -1; 969 res = -1;
950 errno = ENOSYS; 970 errno = ENOSYS;
951#endif 971#endif
952 972
953 if (res < 0 973 if (res < 0
954 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 974 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
975 /* BSDs */
976#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
977 || errno == ENOTSUP
978#endif
979 || errno == EOPNOTSUPP /* BSDs */
955#if __solaris 980#if __solaris
956 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 981 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
957#endif 982#endif
958 ) 983 )
959 ) 984 )
1353 } 1378 }
1354 } 1379 }
1355} 1380}
1356 1381
1357#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1382#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1358# undef msync
1359# define msync(a,b,c) ((errno = ENOSYS), -1) 1383# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1384#else
1385
1386int
1387eio__msync (void *mem, size_t len, int flags)
1388{
1389 if (EIO_MS_ASYNC != MS_SYNC
1390 || EIO_MS_INVALIDATE != MS_INVALIDATE
1391 || EIO_MS_SYNC != MS_SYNC)
1392 {
1393 flags = 0
1394 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1395 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1396 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1397 }
1398
1399 return msync (mem, len, flags);
1400}
1401
1360#endif 1402#endif
1361 1403
1362int 1404int
1363eio__mtouch (void *mem, size_t len, int flags) 1405eio__mtouch (void *mem, size_t len, int flags)
1364{ 1406{
1371 1413
1372 if (!page) 1414 if (!page)
1373 page = sysconf (_SC_PAGESIZE); 1415 page = sysconf (_SC_PAGESIZE);
1374#endif 1416#endif
1375 1417
1418 /* round down to start of page, although this is probably useless */
1376 addr &= ~(page - 1); /* assume page size is always a power of two */ 1419 addr &= ~(page - 1); /* assume page size is always a power of two */
1377 1420
1378 if (addr < end) 1421 if (addr < end)
1379 if (flags) /* modify */ 1422 if (flags & EIO_MT_MODIFY) /* modify */
1380 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); 1423 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len);
1381 else 1424 else
1382 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); 1425 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len);
1383 1426
1384 return 0; 1427 return 0;
1556 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; 1599 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break;
1557 1600
1558 case EIO_SYNC: req->result = 0; sync (); break; 1601 case EIO_SYNC: req->result = 0; sync (); break;
1559 case EIO_FSYNC: req->result = fsync (req->int1); break; 1602 case EIO_FSYNC: req->result = fsync (req->int1); break;
1560 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1603 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1561 case EIO_MSYNC: req->result = msync (req->ptr2, req->size, req->int1); break; 1604 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1562 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1605 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break;
1563 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1606 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1564 1607
1565 case EIO_READDIR: eio__scandir (req, self); break; 1608 case EIO_READDIR: eio__scandir (req, self); break;
1566 1609

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines