2 * libeio implementation
4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libeio@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License ("GPL") version 2 or any later version,
30 * in which case the provisions of the GPL are applicable instead of
31 * the above. If you wish to allow the use of your version of this file
32 * only under the terms of the GPL and not to allow others to use your
33 * version of this file under the BSD license, indicate your decision
34 * by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL.
43 # define XTHREAD_STACKSIZE EIO_STACKSIZE
46 // For statically-linked pthreads-w32, use:
48 // # define PTW32_STATIC_LIB 1
57 #include <sys/types.h>
64 #include <sys/statvfs.h>
68 # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
72 # define EIO_DESTROY(req) do { if ((req)->destroy) (req)->destroy (req); } while (0)
76 # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
82 # include <sys/time.h>
89 # define ENOTSOCK WSAENOTSOCK
90 # define EOPNOTSUPP WSAEOPNOTSUPP
91 # define ECANCELED 140
93 # ifndef EIO_STRUCT_DIRENT
94 # define EIO_STRUCT_DIRENT struct dirent
100 # include <sys/time.h>
101 # include <sys/select.h>
107 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
108 # include <sys/mman.h>
111 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
112 # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
113 # define _DIRENT_HAVE_D_TYPE /* sigh */
114 # define D_INO(de) (de)->d_fileno
115 # define D_NAMLEN(de) (de)->d_namlen
116 # elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
117 # define D_INO(de) (de)->d_ino
120 #ifdef _D_EXACT_NAMLEN
122 # define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
125 # ifdef _DIRENT_HAVE_D_TYPE
126 # define D_TYPE(de) (de)->d_type
129 # ifndef EIO_STRUCT_DIRENT
130 # define EIO_STRUCT_DIRENT struct dirent
137 # include <sys/sendfile.h>
138 # elif __FreeBSD__ || defined __APPLE__
139 # include <sys/socket.h>
140 # include <sys/uio.h>
142 # include <sys/socket.h>
144 # include <sys/sendfile.h>
146 # error sendfile support requested but not available
151 # define D_TYPE(de) 0
157 # define D_NAMLEN(de) strlen ((de)->d_name)
160 /* number of seconds after which an idle threads exit */
161 #define IDLE_TIMEOUT 10
163 /* used for struct dirent, AIX doesn't provide it */
165 # define NAME_MAX 4096
168 /* used for readlink etc. */
170 # define PATH_MAX 4096
173 /* buffer size for various temporary buffers */
174 #define EIO_BUFSIZE 65536
178 ETP_WORKER_LOCK (self); \
179 self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \
180 ETP_WORKER_UNLOCK (self); \
185 #define EIO_TICKS ((1000000 + 1023) >> 10)
187 /*****************************************************************************/
190 # define expect(expr,value) __builtin_expect ((expr),(value))
192 # define expect(expr,value) (expr)
195 #define expect_false(expr) expect ((expr) != 0, 0)
196 #define expect_true(expr) expect ((expr) != 0, 1)
198 /*****************************************************************************/
200 #define ETP_PRI_MIN EIO_PRI_MIN
201 #define ETP_PRI_MAX EIO_PRI_MAX
205 #define ETP_REQ eio_req
206 #define ETP_DESTROY(req) eio_destroy (req)
207 static int eio_finish (eio_req *req);
208 #define ETP_FINISH(req) eio_finish (req)
209 static void eio_execute (struct etp_worker *self, eio_req *req);
210 #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req)
212 #define ETP_WORKER_CLEAR(req) \
221 closedir (wrk->dirp); \
225 #define ETP_WORKER_COMMON \
229 /*****************************************************************************/
231 #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
233 /* calculate time difference in ~1/EIO_TICKS of a second */
234 static int tvdiff (struct timeval *tv1, struct timeval *tv2)
236 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
237 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
240 static unsigned int started, idle, wanted = 4;
242 static void (*want_poll_cb) (void);
243 static void (*done_poll_cb) (void);
245 static unsigned int max_poll_time; /* reslock */
246 static unsigned int max_poll_reqs; /* reslock */
248 static volatile unsigned int nreqs; /* reqlock */
249 static volatile unsigned int nready; /* reqlock */
250 static volatile unsigned int npending; /* reqlock */
251 static volatile unsigned int max_idle = 4;
253 static xmutex_t wrklock = X_MUTEX_INIT;
254 static xmutex_t reslock = X_MUTEX_INIT;
255 static xmutex_t reqlock = X_MUTEX_INIT;
256 static xcond_t reqwait = X_COND_INIT;
258 #if defined (__APPLE__)
259 static xmutex_t apple_bug_writelock = X_MUTEX_INIT;
264 * make our pread/pwrite emulation safe against themselves, but not against
265 * normal read/write by using a mutex. slows down execution a lot,
266 * but that's your problem, not mine.
268 static xmutex_t preadwritelock = X_MUTEX_INIT;
271 typedef struct etp_worker
273 /* locked by wrklock */
274 struct etp_worker *prev, *next;
278 /* locked by reslock, reqlock or wrklock */
279 ETP_REQ *req; /* currently processed request */
284 static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */
286 #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
287 #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
289 /* worker threads management */
291 static void etp_worker_clear (etp_worker *wrk)
293 ETP_WORKER_CLEAR (wrk);
296 static void etp_worker_free (etp_worker *wrk)
298 wrk->next->prev = wrk->prev;
299 wrk->prev->next = wrk->next;
304 static unsigned int etp_nreqs (void)
307 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
309 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
313 static unsigned int etp_nready (void)
317 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
319 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
324 static unsigned int etp_npending (void)
328 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
330 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
335 static unsigned int etp_nthreads (void)
339 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
341 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
347 * a somewhat faster data structure might be nice, but
348 * with 8 priorities this actually needs <20 insns
349 * per shift, the most expensive operation.
352 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
356 static etp_reqq req_queue;
357 static etp_reqq res_queue;
359 static int reqq_push (etp_reqq *q, ETP_REQ *req)
366 q->qe[pri]->next = req;
370 q->qe[pri] = q->qs[pri] = req;
375 static ETP_REQ *reqq_shift (etp_reqq *q)
384 for (pri = ETP_NUM_PRI; pri--; )
386 eio_req *req = q->qs[pri];
390 if (!(q->qs[pri] = (eio_req *)req->next))
400 static void etp_atfork_prepare (void)
406 X_LOCK (preadwritelock);
410 static void etp_atfork_parent (void)
413 X_UNLOCK (preadwritelock);
420 static void etp_atfork_child (void)
424 while ((prv = reqq_shift (&req_queue)))
427 while ((prv = reqq_shift (&res_queue)))
430 while (wrk_first.next != &wrk_first)
432 etp_worker *wrk = wrk_first.next;
435 ETP_DESTROY (wrk->req);
437 etp_worker_clear (wrk);
438 etp_worker_free (wrk);
447 etp_atfork_parent ();
453 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
457 etp_init (void (*want_poll)(void), void (*done_poll)(void))
459 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
461 pthread_once (&doinit, etp_once_init);
463 want_poll_cb = want_poll;
464 done_poll_cb = done_poll;
469 X_THREAD_PROC (etp_proc);
471 static void etp_start_thread (void)
473 etp_worker *wrk = calloc (1, sizeof (etp_worker));
476 assert (("unable to allocate worker thread data", wrk));
480 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
482 wrk->prev = &wrk_first;
483 wrk->next = wrk_first.next;
484 wrk_first.next->prev = wrk;
485 wrk_first.next = wrk;
494 static void etp_maybe_start_thread (void)
496 if (expect_true (etp_nthreads () >= wanted))
499 /* todo: maybe use idle here, but might be less exact */
500 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
506 static void etp_end_thread (void)
508 eio_req *req = calloc (1, sizeof (eio_req));
511 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
514 reqq_push (&req_queue, req);
515 X_COND_SIGNAL (reqwait);
523 static int etp_poll (void)
525 unsigned int maxreqs;
526 unsigned int maxtime;
527 struct timeval tv_start, tv_now;
530 maxreqs = max_poll_reqs;
531 maxtime = max_poll_time;
535 gettimeofday (&tv_start, 0);
541 etp_maybe_start_thread ();
544 req = reqq_shift (&res_queue);
550 if (!res_queue.size && done_poll_cb)
563 if (expect_false (req->type == EIO_GROUP && req->size))
565 req->int1 = 1; /* mark request as delayed */
570 int res = ETP_FINISH (req);
571 if (expect_false (res))
575 if (expect_false (maxreqs && !--maxreqs))
580 gettimeofday (&tv_now, 0);
582 if (tvdiff (&tv_start, &tv_now) >= maxtime)
591 static void etp_cancel (ETP_REQ *req)
594 req->flags |= EIO_FLAG_CANCELLED;
597 eio_grp_cancel (req);
600 static void etp_submit (ETP_REQ *req)
602 req->pri -= ETP_PRI_MIN;
604 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
605 if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
607 if (expect_false (req->type == EIO_GROUP))
609 /* I hope this is worth it :/ */
618 if (!reqq_push (&res_queue, req) && want_poll_cb)
628 reqq_push (&req_queue, req);
629 X_COND_SIGNAL (reqwait);
632 etp_maybe_start_thread ();
636 static void etp_set_max_poll_time (double nseconds)
638 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
639 max_poll_time = nseconds * EIO_TICKS;
640 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
643 static void etp_set_max_poll_reqs (unsigned int maxreqs)
645 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
646 max_poll_reqs = maxreqs;
647 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
650 static void etp_set_max_idle (unsigned int nthreads)
652 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
653 max_idle = nthreads <= 0 ? 1 : nthreads;
654 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
657 static void etp_set_min_parallel (unsigned int nthreads)
659 if (wanted < nthreads)
663 static void etp_set_max_parallel (unsigned int nthreads)
665 if (wanted > nthreads)
668 while (started > wanted)
672 /*****************************************************************************/
674 static void grp_try_feed (eio_req *grp)
676 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
678 grp->flags &= ~EIO_FLAG_GROUPADD;
682 /* stop if no progress has been made */
683 if (!(grp->flags & EIO_FLAG_GROUPADD))
691 static int grp_dec (eio_req *grp)
695 /* call feeder, if applicable */
698 /* finish, if done */
699 if (!grp->size && grp->int1)
700 return eio_finish (grp);
705 void eio_destroy (eio_req *req)
707 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
708 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
713 static int eio_finish (eio_req *req)
715 int res = EIO_FINISH (req);
720 eio_req *grp = req->grp;
723 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
724 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
726 if (grp->grp_first == req)
727 grp->grp_first = req->grp_next;
729 res2 = grp_dec (grp);
740 void eio_grp_cancel (eio_req *grp)
742 for (grp = grp->grp_first; grp; grp = grp->grp_next)
746 void eio_cancel (eio_req *req)
751 void eio_submit (eio_req *req)
756 unsigned int eio_nreqs (void)
761 unsigned int eio_nready (void)
763 return etp_nready ();
766 unsigned int eio_npending (void)
768 return etp_npending ();
771 unsigned int eio_nthreads (void)
773 return etp_nthreads ();
776 void eio_set_max_poll_time (double nseconds)
778 etp_set_max_poll_time (nseconds);
781 void eio_set_max_poll_reqs (unsigned int maxreqs)
783 etp_set_max_poll_reqs (maxreqs);
786 void eio_set_max_idle (unsigned int nthreads)
788 etp_set_max_idle (nthreads);
791 void eio_set_min_parallel (unsigned int nthreads)
793 etp_set_min_parallel (nthreads);
796 void eio_set_max_parallel (unsigned int nthreads)
798 etp_set_max_parallel (nthreads);
806 /*****************************************************************************/
807 /* work around various missing functions */
812 # define pread eio__pread
813 # define pwrite eio__pwrite
816 eio__pread (int fd, void *buf, size_t count, off_t offset)
821 X_LOCK (preadwritelock);
822 ooffset = lseek (fd, 0, SEEK_CUR);
823 lseek (fd, offset, SEEK_SET);
824 res = read (fd, buf, count);
825 lseek (fd, ooffset, SEEK_SET);
826 X_UNLOCK (preadwritelock);
832 eio__pwrite (int fd, void *buf, size_t count, off_t offset)
837 X_LOCK (preadwritelock);
838 ooffset = lseek (fd, 0, SEEK_CUR);
839 lseek (fd, offset, SEEK_SET);
840 res = write (fd, buf, count);
841 lseek (fd, ooffset, SEEK_SET);
842 X_UNLOCK (preadwritelock);
851 # define utimes(path,times) eio__utimes (path, times)
854 eio__utimes (const char *filename, const struct timeval times[2])
860 buf.actime = times[0].tv_sec;
861 buf.modtime = times[1].tv_sec;
863 return utime (filename, &buf);
866 return utime (filename, 0);
874 # define futimes(fd,times) eio__futimes (fd, times)
876 static int eio__futimes (int fd, const struct timeval tv[2])
885 # define fsync(fd) (FlushFileBuffers((HANDLE)_get_osfhandle(fd)) ? 0 : -1)
890 # define fdatasync(fd) fsync (fd)
893 // Use unicode and big file aware stat on windows
897 # define stat _stati64
898 # define fstat _fstati64
901 /* sync_file_range always needs emulation */
903 eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
905 #if HAVE_SYNC_FILE_RANGE
908 if (EIO_SYNC_FILE_RANGE_WAIT_BEFORE != SYNC_FILE_RANGE_WAIT_BEFORE
909 || EIO_SYNC_FILE_RANGE_WRITE != SYNC_FILE_RANGE_WRITE
910 || EIO_SYNC_FILE_RANGE_WAIT_AFTER != SYNC_FILE_RANGE_WAIT_AFTER)
913 | (flags & EIO_SYNC_FILE_RANGE_WAIT_BEFORE ? SYNC_FILE_RANGE_WAIT_BEFORE : 0)
914 | (flags & EIO_SYNC_FILE_RANGE_WRITE ? SYNC_FILE_RANGE_WRITE : 0)
915 | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0);
918 res = sync_file_range (fd, offset, nbytes, flags);
920 if (!res || errno != ENOSYS)
924 /* even though we could play tricks with the flags, it's better to always
925 * call fdatasync, as that matches the expectation of its users best */
926 return fdatasync (fd);
931 # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
934 eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
941 size_t len = todo < EIO_BUFSIZE ? todo : EIO_BUFSIZE;
943 pread (fd, eio_buf, len, offset);
954 /* sendfile always needs emulation */
956 eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
965 res = sendfile (ofd, ifd, &offset, count);
969 * Of course, the freebsd sendfile is a dire hack with no thoughts
970 * wasted on making it similar to other I/O functions.
974 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
976 #if 0 /* according to the manpage, this is correct, but broken behaviour */
977 /* freebsd' sendfile will return 0 on success */
978 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
979 /* not on e.g. EIO or EPIPE - sounds broken */
980 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
984 /* according to source inspection, this is correct, and useful behaviour */
989 # elif defined (__APPLE__)
992 off_t sbytes = count;
993 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
995 /* according to the manpage, sbytes is always valid */
1001 res = sendfile (ofd, ifd, offset, count, 0, 0);
1005 struct sendfilevec vec;
1010 vec.sfv_off = offset;
1011 vec.sfv_len = count;
1013 res = sendfilev (ofd, &vec, 1, &sbytes);
1015 if (res < 0 && sbytes)
1021 //#elif defined (_WIN32)
1023 // /* does not work, just for documentation of what would need to be done */
1025 // HANDLE h = TO_SOCKET (ifd);
1026 // SetFilePointer (h, offset, 0, FILE_BEGIN);
1027 // res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1036 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1038 #ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1041 || errno == EOPNOTSUPP /* BSDs */
1043 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1048 /* emulate sendfile. this is a major pain in the ass */
1057 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1061 if (cnt && !res) res = -1;
1065 cnt = write (ofd, eio_buf, cnt);
1069 if (cnt && !res) res = -1;
1083 eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1085 return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */
1086 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0;
1089 #define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0
1091 #define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1092 #define EIO_SORT_FAST 60 /* when to only use insertion sort */
1095 eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits)
1097 unsigned char bits [9 + sizeof (ino_t) * 8];
1098 unsigned char *bit = bits;
1100 assert (CHAR_BIT == 8);
1101 assert (sizeof (eio_dirent) * 8 < 256);
1102 assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */
1103 assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */
1105 if (size <= EIO_SORT_FAST)
1108 /* first prepare an array of bits to test in our radix sort */
1109 /* try to take endianness into account, as well as differences in ino_t sizes */
1110 /* inode_bits must contain all inodes ORed together */
1111 /* which is used to skip bits that are 0 everywhere, which is very common */
1116 /* we store the byte offset of byte n into byte n of "endianness" */
1117 for (i = 0; i < sizeof (ino_t); ++i)
1118 ((unsigned char *)&endianness)[i] = i;
1122 for (i = 0; i < sizeof (ino_t); ++i)
1124 /* shifting off the byte offsets out of "endianness" */
1125 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1128 for (j = 0; j < 8; ++j)
1129 if (inode_bits & (((ino_t)1) << (i * 8 + j)))
1133 for (j = 0; j < 8; ++j)
1134 if (score_bits & (1 << j))
1135 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1138 /* now actually do the sorting (a variant of MSD radix sort) */
1140 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base;
1141 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end;
1142 unsigned char *bit_stk [9 + sizeof (ino_t) * 8];
1145 base_stk [stk_idx] = dents;
1146 end_stk [stk_idx] = dents + size;
1147 bit_stk [stk_idx] = bit - 1;
1151 base = base_stk [stk_idx];
1152 end = end_stk [stk_idx];
1153 bit = bit_stk [stk_idx];
1157 unsigned char O = *bit >> 3;
1158 unsigned char M = 1 << (*bit & 7);
1160 eio_dirent *a = base;
1161 eio_dirent *b = end;
1163 if (b - a < EIO_SORT_CUTOFF)
1166 /* now bit-partition the array on the bit */
1167 /* this ugly asymmetric loop seems to perform much better than typical */
1168 /* partition algos found in the literature */
1170 if (!(((unsigned char *)a)[O] & M))
1172 else if (!(((unsigned char *)--b)[O] & M))
1174 eio_dirent tmp = *a; *a = *b; *b = tmp;
1179 /* next bit, or stop, if no bits left in this path */
1183 base_stk [stk_idx] = a;
1184 end_stk [stk_idx] = end;
1185 bit_stk [stk_idx] = bit;
1196 eio_dent_insertion_sort (eio_dirent *dents, int size)
1198 /* first move the smallest element to the front, to act as a sentinel */
1201 eio_dirent *min = dents;
1203 /* the radix pre-pass ensures that the minimum element is in the first EIO_SORT_CUTOFF + 1 elements */
1204 for (i = size > EIO_SORT_FAST ? EIO_SORT_CUTOFF + 1 : size; --i; )
1205 if (EIO_DENT_CMP (dents [i], <, *min))
1208 /* swap elements 0 and j (minimum) */
1210 eio_dirent tmp = *dents; *dents = *min; *min = tmp;
1214 /* then do standard insertion sort, assuming that all elements are >= dents [0] */
1218 for (i = dents + 1; i < dents + size; ++i)
1220 eio_dirent value = *i;
1222 for (j = i - 1; EIO_DENT_CMP (*j, >, value); --j)
1231 eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits)
1234 return; /* our insertion sort relies on size > 0 */
1236 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1237 /* and stop sorting when the partitions are <= EIO_SORT_CUTOFF */
1238 eio_dent_radix_sort (dents, size, score_bits, inode_bits);
1240 /* use an insertion sort at the end, or for small arrays, */
1241 /* as insertion sort is more efficient for small partitions */
1242 eio_dent_insertion_sort (dents, size);
1245 /* read a full directory */
1247 eio__scandir (eio_req *req, etp_worker *self)
1250 EIO_STRUCT_DIRENT *entp;
1252 int namesalloc = 4096;
1254 int flags = req->int1;
1255 eio_dirent *dents = 0;
1256 int dentalloc = 128;
1258 ino_t inode_bits = 0;
1262 if (!(flags & EIO_READDIR_DENTS))
1263 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1266 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1267 self->dirp = dirp = opendir (req->ptr1);
1268 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1269 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1270 req->ptr2 = names = malloc (namesalloc);
1273 if (dirp && names && (!flags || dents))
1277 entp = readdir (dirp);
1286 req->result = dentoffs;
1288 if (flags & EIO_READDIR_STAT_ORDER)
1289 eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */
1290 else if (flags & EIO_READDIR_DIRS_FIRST)
1291 if (flags & EIO_READDIR_FOUND_UNKNOWN)
1292 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */
1295 /* in this case, all is known, and we just put dirs first and sort them */
1296 eio_dirent *oth = dents + dentoffs;
1297 eio_dirent *dir = dents;
1299 /* now partition dirs to the front, and non-dirs to the back */
1300 /* by walking from both sides and swapping if necessary */
1301 /* also clear score, so it doesn't influence sorting */
1304 if (dir->type == EIO_DT_DIR)
1306 else if ((--oth)->type == EIO_DT_DIR)
1308 eio_dirent tmp = *dir; *dir = *oth; *oth = tmp;
1314 /* now sort the dirs only */
1315 eio_dent_sort (dents, dir - dents, 0, inode_bits);
1321 /* now add the entry to our list(s) */
1322 name = entp->d_name;
1324 /* skip . and .. entries */
1325 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1327 int len = D_NAMLEN (entp) + 1;
1329 while (expect_false (namesoffs + len > namesalloc))
1333 req->ptr2 = names = realloc (names, namesalloc);
1340 memcpy (names + namesoffs, name, len);
1344 struct eio_dirent *ent;
1346 if (expect_false (dentoffs == dentalloc))
1350 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1357 ent = dents + dentoffs;
1359 ent->nameofs = namesoffs; /* rather dirtily we store the offset in the pointer */
1360 ent->namelen = len - 1;
1361 ent->inode = D_INO (entp);
1363 inode_bits |= ent->inode;
1365 switch (D_TYPE (entp))
1368 ent->type = EIO_DT_UNKNOWN;
1369 flags |= EIO_READDIR_FOUND_UNKNOWN;
1373 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1376 case DT_CHR: ent->type = EIO_DT_CHR; break;
1379 case DT_MPC: ent->type = EIO_DT_MPC; break;
1382 case DT_DIR: ent->type = EIO_DT_DIR; break;
1385 case DT_NAM: ent->type = EIO_DT_NAM; break;
1388 case DT_BLK: ent->type = EIO_DT_BLK; break;
1391 case DT_MPB: ent->type = EIO_DT_MPB; break;
1394 case DT_REG: ent->type = EIO_DT_REG; break;
1397 case DT_NWK: ent->type = EIO_DT_NWK; break;
1400 case DT_CMP: ent->type = EIO_DT_CMP; break;
1403 case DT_LNK: ent->type = EIO_DT_LNK; break;
1406 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1409 case DT_DOOR: ent->type = EIO_DT_DOOR; break;
1412 case DT_WHT: ent->type = EIO_DT_WHT; break;
1418 if (flags & EIO_READDIR_DIRS_FIRST)
1420 if (ent->type == EIO_DT_UNKNOWN)
1422 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1424 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */
1425 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1427 else if (ent->type == EIO_DT_DIR)
1436 if (EIO_CANCELLED (req))
1445 # define eio_pagesize() PAGESIZE
1447 #elif defined(_WIN32)
1454 return si.dwPageSize;
1462 static intptr_t page;
1465 page = sysconf (_SC_PAGESIZE);
1472 eio_page_align (void **addr, size_t *length)
1474 intptr_t mask = eio_pagesize () - 1;
1476 /* round down addr */
1477 intptr_t adj = mask & (intptr_t)*addr;
1479 *addr = (void *)((intptr_t)*addr - adj);
1482 /* round up length */
1483 *length = (*length + mask) & ~mask;
1487 # define eio__mlockall(a) ((errno = ENOSYS), -1)
1491 eio__mlockall (int flags)
1493 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1494 extern int mallopt (int, int);
1495 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1498 if (EIO_MCL_CURRENT != MCL_CURRENT
1499 || EIO_MCL_FUTURE != MCL_FUTURE)
1502 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1503 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1506 return mlockall (flags);
1510 #if !_POSIX_MEMLOCK_RANGE
1511 # define eio__mlock(a,b) ((errno = ENOSYS), -1)
1515 eio__mlock (void *addr, size_t length)
1517 eio_page_align (&addr, &length);
1519 return mlock (addr, length);
1524 #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1525 # define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1529 eio__msync (void *mem, size_t len, int flags)
1531 eio_page_align (&mem, &len);
1533 if (EIO_MS_ASYNC != MS_SYNC
1534 || EIO_MS_INVALIDATE != MS_INVALIDATE
1535 || EIO_MS_SYNC != MS_SYNC)
1538 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1539 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1540 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1543 return msync (mem, len, flags);
1549 eio__mtouch (void *mem, size_t len, int flags)
1551 eio_page_align (&mem, &len);
1554 intptr_t addr = (intptr_t)mem;
1555 intptr_t end = addr + len;
1556 intptr_t page = eio_pagesize ();
1559 if (flags & EIO_MT_MODIFY) /* modify */
1560 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len);
1562 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len);
1568 /*****************************************************************************/
1570 #define ALLOC(len) \
1574 req->flags |= EIO_FLAG_PTR2_FREE; \
1575 X_UNLOCK (wrklock); \
1576 req->ptr2 = malloc (len); \
1585 X_THREAD_PROC (etp_proc)
1589 etp_worker *self = (etp_worker *)thr_arg;
1591 /* try to distribute timeouts somewhat randomly */
1592 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1600 self->req = req = reqq_shift (&req_queue);
1607 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1608 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1610 if (idle > max_idle)
1620 /* we are allowed to idle, so do so without any timeout */
1621 X_COND_WAIT (reqwait, reqlock);
1634 if (!EIO_CANCELLED (req))
1635 ETP_EXECUTE (self, req);
1641 if (!reqq_push (&res_queue, req) && want_poll_cb)
1645 etp_worker_clear (self);
1652 etp_worker_free (self);
1658 /*****************************************************************************/
1660 int eio_init (void (*want_poll)(void), void (*done_poll)(void))
1662 return etp_init (want_poll, done_poll);
1665 static void eio_api_destroy (eio_req *req)
1670 #define REQ(rtype) \
1673 req = (eio_req *)calloc (1, sizeof *req); \
1677 req->type = rtype; \
1681 req->destroy = eio_api_destroy;
1683 #define SEND eio_submit (req); return req
1686 req->flags |= EIO_FLAG_PTR1_FREE; \
1687 req->ptr1 = strdup (path); \
1690 eio_api_destroy (req); \
1694 static void eio_execute (etp_worker *self, eio_req *req)
1698 case EIO_READ: ALLOC (req->size);
1699 req->result = req->offs >= 0
1700 ? pread (req->int1, req->ptr2, req->size, req->offs)
1701 : read (req->int1, req->ptr2, req->size); break;
1703 #if defined (__APPLE__)
1704 pthread_mutex_lock (&apple_bug_writelock);
1707 req->result = req->offs >= 0
1708 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1709 : write (req->int1, req->ptr2, req->size);
1711 #if defined (__APPLE__)
1712 pthread_mutex_unlock (&apple_bug_writelock);
1716 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1717 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break;
1719 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1720 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1722 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1723 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1725 case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1726 req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break;
1729 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
1730 req->result = statvfs (req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1731 case EIO_FSTATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
1732 req->result = fstatvfs (req->int1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1734 case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1735 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1737 case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break;
1739 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
1740 case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1742 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1744 case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break;
1745 case EIO_CLOSE: req->result = close (req->int1); break;
1746 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
1747 case EIO_UNLINK: req->result = unlink (req->ptr1); break;
1748 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1750 case EIO_MKDIR: req->result = mkdir (req->ptr1); break;
1752 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1754 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1756 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1757 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1758 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break;
1762 case EIO_READLINK: ALLOC (PATH_MAX);
1763 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1767 case EIO_SYNC: req->result = 0; sync (); break;
1769 case EIO_FSYNC: req->result = fsync (req->int1); break;
1770 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1771 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1772 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break;
1773 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1774 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1775 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1777 case EIO_READDIR: eio__scandir (req, self); break;
1781 Sleep (req->nv1 * 1e3);
1786 tv.tv_sec = req->nv1;
1787 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6;
1789 req->result = select (0, 0, 0, 0, &tv);
1797 struct timeval tv[2];
1798 struct timeval *times;
1800 if (req->nv1 != -1. || req->nv2 != -1.)
1802 tv[0].tv_sec = req->nv1;
1803 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1804 tv[1].tv_sec = req->nv2;
1805 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1812 req->result = req->type == EIO_FUTIME
1813 ? futimes (req->int1, times)
1814 : utimes (req->ptr1, times);
1819 abort (); /* handled in eio_request */
1826 ((void (*)(eio_req *))req->feed) (req);
1835 req->errorno = errno;
1838 #ifndef EIO_NO_WRAPPERS
1840 eio_req *eio_nop (int pri, eio_cb cb, void *data)
1842 REQ (EIO_NOP); SEND;
1845 eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data)
1847 REQ (EIO_BUSY); req->nv1 = delay; SEND;
1850 eio_req *eio_sync (int pri, eio_cb cb, void *data)
1852 REQ (EIO_SYNC); SEND;
1855 eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data)
1857 REQ (EIO_FSYNC); req->int1 = fd; SEND;
1860 eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1862 REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1865 eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1867 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1870 eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data)
1872 REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND;
1875 eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data)
1877 REQ (EIO_MLOCKALL); req->int1 = flags; SEND;
1880 eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1882 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1885 eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1887 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1890 eio_req *eio_close (int fd, int pri, eio_cb cb, void *data)
1892 REQ (EIO_CLOSE); req->int1 = fd; SEND;
1895 eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
1897 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
1900 eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1902 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1905 eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1907 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1910 eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
1912 REQ (EIO_FSTAT); req->int1 = fd; SEND;
1915 eio_req *eio_fstatvfs (int fd, int pri, eio_cb cb, void *data)
1917 REQ (EIO_FSTATVFS); req->int1 = fd; SEND;
1920 eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data)
1922 REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND;
1925 eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data)
1927 REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND;
1930 eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
1932 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
1935 eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1937 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1940 eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
1942 REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND;
1945 eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, int pri, eio_cb cb, void *data)
1947 REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND;
1950 eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data)
1952 REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND;
1955 eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data)
1957 REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND;
1960 eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
1962 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
1965 eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1967 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1970 eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1972 REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND;
1975 eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1977 REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND;
1981 eio__1path (int type, const char *path, int pri, eio_cb cb, void *data)
1983 REQ (type); PATH; SEND;
1986 eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1988 return eio__1path (EIO_READLINK, path, pri, cb, data);
1991 eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1993 return eio__1path (EIO_STAT, path, pri, cb, data);
1996 eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data)
1998 return eio__1path (EIO_LSTAT, path, pri, cb, data);
2001 eio_req *eio_statvfs (const char *path, int pri, eio_cb cb, void *data)
2003 return eio__1path (EIO_STATVFS, path, pri, cb, data);
2006 eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data)
2008 return eio__1path (EIO_UNLINK, path, pri, cb, data);
2011 eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data)
2013 return eio__1path (EIO_RMDIR, path, pri, cb, data);
2016 eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data)
2018 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND;
2021 eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
2023 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND;
2027 eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2031 req->flags |= EIO_FLAG_PTR2_FREE;
2032 req->ptr2 = strdup (new_path);
2035 eio_api_destroy (req);
2042 eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2044 return eio__2path (EIO_LINK, path, new_path, pri, cb, data);
2047 eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2049 return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data);
2052 eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2054 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2057 eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data)
2059 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND;
2064 eio_req *eio_grp (eio_cb cb, void *data)
2066 const int pri = EIO_PRI_MAX;
2068 REQ (EIO_GROUP); SEND;
2075 /*****************************************************************************/
2078 void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
2086 void eio_grp_limit (eio_req *grp, int limit)
2093 void eio_grp_add (eio_req *grp, eio_req *req)
2095 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2097 grp->flags |= EIO_FLAG_GROUPADD;
2103 req->grp_next = grp->grp_first;
2106 grp->grp_first->grp_prev = req;
2108 grp->grp_first = req;
2111 /*****************************************************************************/
2114 ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2121 ret = eio__sendfile (ofd, ifd, offset, count, &wrk);