ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
Revision: 1.14
Committed: Sat May 17 12:17:25 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +344 -221 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * libeio implementation
3 *
4 * Copyright (c) 2007,2008 Marc Alexander Lehmann <libeio@schmorp.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
27 *
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.
38 */
39
40 #include "eio.h"
41 #include "xthread.h"
42
43 #include <errno.h>
44 #include <stddef.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <limits.h>
51 #include <fcntl.h>
52 #include <assert.h>
53
54 #ifndef EIO_FINISH
55 # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
56 #endif
57
58 #ifndef EIO_DESTROY
59 # define EIO_DESTROY(req) do { if ((req)->destroy) (req)->destroy (req); } while (0)
60 #endif
61
62 #ifndef EIO_FEED
63 # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
64 #endif
65
66 #ifdef _WIN32
67
68 /*doh*/
69
70 #else
71
72 # include "config.h"
73 # include <sys/time.h>
74 # include <sys/select.h>
75 # include <unistd.h>
76 # include <utime.h>
77 # include <signal.h>
78 # include <dirent.h>
79
80 # ifndef EIO_STRUCT_DIRENT
81 # define EIO_STRUCT_DIRENT struct dirent
82 # endif
83
84 #endif
85
86 #if HAVE_SENDFILE
87 # if __linux
88 # include <sys/sendfile.h>
89 # elif __freebsd
90 # include <sys/socket.h>
91 # include <sys/uio.h>
92 # elif __hpux
93 # include <sys/socket.h>
94 # elif __solaris /* not yet */
95 # include <sys/sendfile.h>
96 # else
97 # error sendfile support requested but not available
98 # endif
99 #endif
100
101 /* number of seconds after which an idle threads exit */
102 #define IDLE_TIMEOUT 10
103
104 /* used for struct dirent, AIX doesn't provide it */
105 #ifndef NAME_MAX
106 # define NAME_MAX 4096
107 #endif
108
109 /* buffer size for various temporary buffers */
110 #define EIO_BUFSIZE 65536
111
112 #define dBUF \
113 char *eio_buf; \
114 ETP_WORKER_LOCK (self); \
115 self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \
116 ETP_WORKER_UNLOCK (self); \
117 errno = ENOMEM; \
118 if (!eio_buf) \
119 return -1;
120
121 #define EIO_TICKS ((1000000 + 1023) >> 10)
122
123 /*****************************************************************************/
124
125 #define ETP_PRI_MIN EIO_PRI_MIN
126 #define ETP_PRI_MAX EIO_PRI_MAX
127
128 #define ETP_REQ eio_req
129 #define ETP_DESTROY(req) eio_destroy (req)
130 static int eio_finish (eio_req *req);
131 #define ETP_FINISH(req) eio_finish (req)
132
133 #define ETP_WORKER_CLEAR(req) \
134 if (wrk->dbuf) \
135 { \
136 free (wrk->dbuf); \
137 wrk->dbuf = 0; \
138 } \
139 \
140 if (wrk->dirp) \
141 { \
142 closedir (wrk->dirp); \
143 wrk->dirp = 0; \
144 }
145 #define ETP_WORKER_COMMON \
146 void *dbuf; \
147 DIR *dirp;
148
149 /*****************************************************************************/
150
151 #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
152
153 /* calculcate time difference in ~1/EIO_TICKS of a second */
154 static int tvdiff (struct timeval *tv1, struct timeval *tv2)
155 {
156 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
157 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
158 }
159
160 static unsigned int started, idle, wanted = 4;
161
162 static void (*want_poll_cb) (void);
163 static void (*done_poll_cb) (void);
164
165 static unsigned int max_poll_time; /* reslock */
166 static unsigned int max_poll_reqs; /* reslock */
167
168 static volatile unsigned int nreqs; /* reqlock */
169 static volatile unsigned int nready; /* reqlock */
170 static volatile unsigned int npending; /* reqlock */
171 static volatile unsigned int max_idle = 4;
172
173 static mutex_t wrklock = X_MUTEX_INIT;
174 static mutex_t reslock = X_MUTEX_INIT;
175 static mutex_t reqlock = X_MUTEX_INIT;
176 static cond_t reqwait = X_COND_INIT;
177
178 typedef struct etp_worker
179 {
180 /* locked by wrklock */
181 struct etp_worker *prev, *next;
182
183 thread_t tid;
184
185 /* locked by reslock, reqlock or wrklock */
186 ETP_REQ *req; /* currently processed request */
187
188 ETP_WORKER_COMMON
189 } etp_worker;
190
191 static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */
192
193 #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
194 #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
195
196 /* worker threads management */
197
198 static void etp_worker_clear (etp_worker *wrk)
199 {
200 ETP_WORKER_CLEAR (wrk);
201 }
202
203 static void etp_worker_free (etp_worker *wrk)
204 {
205 wrk->next->prev = wrk->prev;
206 wrk->prev->next = wrk->next;
207
208 free (wrk);
209 }
210
211 static unsigned int etp_nreqs (void)
212 {
213 int retval;
214 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
215 retval = nreqs;
216 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
217 return retval;
218 }
219
220 static unsigned int etp_nready (void)
221 {
222 unsigned int retval;
223
224 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
225 retval = nready;
226 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
227
228 return retval;
229 }
230
231 static unsigned int etp_npending (void)
232 {
233 unsigned int retval;
234
235 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
236 retval = npending;
237 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
238
239 return retval;
240 }
241
242 static unsigned int etp_nthreads (void)
243 {
244 unsigned int retval;
245
246 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
247 retval = started;
248 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
249
250 return retval;
251 }
252
253 /*
254 * a somewhat faster data structure might be nice, but
255 * with 8 priorities this actually needs <20 insns
256 * per shift, the most expensive operation.
257 */
258 typedef struct {
259 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
260 int size;
261 } etp_reqq;
262
263 static etp_reqq req_queue;
264 static etp_reqq res_queue;
265
266 static int reqq_push (etp_reqq *q, ETP_REQ *req)
267 {
268 int pri = req->pri;
269 req->next = 0;
270
271 if (q->qe[pri])
272 {
273 q->qe[pri]->next = req;
274 q->qe[pri] = req;
275 }
276 else
277 q->qe[pri] = q->qs[pri] = req;
278
279 return q->size++;
280 }
281
282 static ETP_REQ *reqq_shift (etp_reqq *q)
283 {
284 int pri;
285
286 if (!q->size)
287 return 0;
288
289 --q->size;
290
291 for (pri = ETP_NUM_PRI; pri--; )
292 {
293 eio_req *req = q->qs[pri];
294
295 if (req)
296 {
297 if (!(q->qs[pri] = (eio_req *)req->next))
298 q->qe[pri] = 0;
299
300 return req;
301 }
302 }
303
304 abort ();
305 }
306
307 static void etp_atfork_prepare (void)
308 {
309 X_LOCK (wrklock);
310 X_LOCK (reqlock);
311 X_LOCK (reslock);
312 #if !HAVE_PREADWRITE
313 X_LOCK (preadwritelock);
314 #endif
315 #if !HAVE_READDIR_R
316 X_LOCK (readdirlock);
317 #endif
318 }
319
320 static void etp_atfork_parent (void)
321 {
322 #if !HAVE_READDIR_R
323 X_UNLOCK (readdirlock);
324 #endif
325 #if !HAVE_PREADWRITE
326 X_UNLOCK (preadwritelock);
327 #endif
328 X_UNLOCK (reslock);
329 X_UNLOCK (reqlock);
330 X_UNLOCK (wrklock);
331 }
332
333 static void etp_atfork_child (void)
334 {
335 ETP_REQ *prv;
336
337 while (prv = reqq_shift (&req_queue))
338 ETP_DESTROY (prv);
339
340 while (prv = reqq_shift (&res_queue))
341 ETP_DESTROY (prv);
342
343 while (wrk_first.next != &wrk_first)
344 {
345 etp_worker *wrk = wrk_first.next;
346
347 if (wrk->req)
348 ETP_DESTROY (wrk->req);
349
350 etp_worker_clear (wrk);
351 etp_worker_free (wrk);
352 }
353
354 started = 0;
355 idle = 0;
356 nreqs = 0;
357 nready = 0;
358 npending = 0;
359
360 etp_atfork_parent ();
361 }
362
363 static void
364 etp_once_init (void)
365 {
366 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
367 }
368
369 static int
370 etp_init (void (*want_poll)(void), void (*done_poll)(void))
371 {
372 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
373
374 pthread_once (&doinit, etp_once_init);
375
376 want_poll_cb = want_poll;
377 done_poll_cb = done_poll;
378 }
379
380 X_THREAD_PROC (etp_proc);
381
382 static void etp_start_thread (void)
383 {
384 etp_worker *wrk = calloc (1, sizeof (etp_worker));
385
386 /*TODO*/
387 assert (("unable to allocate worker thread data", wrk));
388
389 X_LOCK (wrklock);
390
391 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
392 {
393 wrk->prev = &wrk_first;
394 wrk->next = wrk_first.next;
395 wrk_first.next->prev = wrk;
396 wrk_first.next = wrk;
397 ++started;
398 }
399 else
400 free (wrk);
401
402 X_UNLOCK (wrklock);
403 }
404
405 static void etp_maybe_start_thread (void)
406 {
407 if (etp_nthreads () >= wanted)
408 return;
409
410 /* todo: maybe use idle here, but might be less exact */
411 if (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())
412 return;
413
414 etp_start_thread ();
415 }
416
417 static void etp_end_thread (void)
418 {
419 eio_req *req = calloc (1, sizeof (eio_req));
420
421 req->type = -1;
422 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
423
424 X_LOCK (reqlock);
425 reqq_push (&req_queue, req);
426 X_COND_SIGNAL (reqwait);
427 X_UNLOCK (reqlock);
428
429 X_LOCK (wrklock);
430 --started;
431 X_UNLOCK (wrklock);
432 }
433
434 static int etp_poll (void)
435 {
436 unsigned int maxreqs;
437 unsigned int maxtime;
438 struct timeval tv_start, tv_now;
439
440 X_LOCK (reslock);
441 maxreqs = max_poll_reqs;
442 maxtime = max_poll_time;
443 X_UNLOCK (reslock);
444
445 if (maxtime)
446 gettimeofday (&tv_start, 0);
447
448 for (;;)
449 {
450 ETP_REQ *req;
451
452 etp_maybe_start_thread ();
453
454 X_LOCK (reslock);
455 req = reqq_shift (&res_queue);
456
457 if (req)
458 {
459 --npending;
460
461 if (!res_queue.size && done_poll_cb)
462 done_poll_cb ();
463 }
464
465 X_UNLOCK (reslock);
466
467 if (!req)
468 return 0;
469
470 X_LOCK (reqlock);
471 --nreqs;
472 X_UNLOCK (reqlock);
473
474 if (req->type == EIO_GROUP && req->size)
475 {
476 req->int1 = 1; /* mark request as delayed */
477 continue;
478 }
479 else
480 {
481 int res = ETP_FINISH (req);
482 if (res)
483 return res;
484 }
485
486 if (maxreqs && !--maxreqs)
487 break;
488
489 if (maxtime)
490 {
491 gettimeofday (&tv_now, 0);
492
493 if (tvdiff (&tv_start, &tv_now) >= maxtime)
494 break;
495 }
496 }
497
498 errno = EAGAIN;
499 return -1;
500 }
501
502 static void etp_cancel (ETP_REQ *req)
503 {
504 X_LOCK (wrklock);
505 req->flags |= EIO_FLAG_CANCELLED;
506 X_UNLOCK (wrklock);
507
508 eio_grp_cancel (req);
509 }
510
511 static void etp_submit (ETP_REQ *req)
512 {
513 req->pri -= ETP_PRI_MIN;
514
515 if (req->pri < ETP_PRI_MIN - ETP_PRI_MIN) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
516 if (req->pri > ETP_PRI_MAX - ETP_PRI_MIN) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
517
518 X_LOCK (reqlock);
519 ++nreqs;
520 ++nready;
521 reqq_push (&req_queue, req);
522 X_COND_SIGNAL (reqwait);
523 X_UNLOCK (reqlock);
524
525 etp_maybe_start_thread ();
526 }
527
528 static void etp_set_max_poll_time (double nseconds)
529 {
530 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
531 max_poll_time = nseconds;
532 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
533 }
534
535 static void etp_set_max_poll_reqs (unsigned int maxreqs)
536 {
537 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
538 max_poll_reqs = maxreqs;
539 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
540 }
541
542 static void etp_set_max_idle (unsigned int nthreads)
543 {
544 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
545 max_idle = nthreads <= 0 ? 1 : nthreads;
546 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
547 }
548
549 static void etp_set_min_parallel (unsigned int nthreads)
550 {
551 if (wanted < nthreads)
552 wanted = nthreads;
553 }
554
555 static void etp_set_max_parallel (unsigned int nthreads)
556 {
557 if (wanted > nthreads)
558 wanted = nthreads;
559
560 while (started > wanted)
561 etp_end_thread ();
562 }
563
564 /*****************************************************************************/
565
566 static void grp_try_feed (eio_req *grp)
567 {
568 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
569 {
570 int old_len = grp->size;
571
572 EIO_FEED (grp);
573
574 /* stop if no progress has been made */
575 if (old_len == grp->size)
576 {
577 grp->feed = 0;
578 break;
579 }
580 }
581 }
582
583 static int grp_dec (eio_req *grp)
584 {
585 --grp->size;
586
587 /* call feeder, if applicable */
588 grp_try_feed (grp);
589
590 /* finish, if done */
591 if (!grp->size && grp->int1)
592 return eio_finish (grp);
593 else
594 return 0;
595 }
596
597 void eio_destroy (eio_req *req)
598 {
599 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
600 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
601
602 EIO_DESTROY (req);
603 }
604
605 static int eio_finish (eio_req *req)
606 {
607 int res = EIO_FINISH (req);
608
609 if (req->grp)
610 {
611 int res2;
612 eio_req *grp = req->grp;
613
614 /* unlink request */
615 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
616 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
617
618 if (grp->grp_first == req)
619 grp->grp_first = req->grp_next;
620
621 res2 = grp_dec (grp);
622
623 if (!res && res2)
624 res = res2;
625 }
626
627 eio_destroy (req);
628
629 return res;
630 }
631
632 void eio_grp_cancel (eio_req *grp)
633 {
634 for (grp = grp->grp_first; grp; grp = grp->grp_next)
635 eio_cancel (grp);
636 }
637
638 void eio_cancel (eio_req *req)
639 {
640 etp_cancel (req);
641 }
642
643 void eio_submit (eio_req *req)
644 {
645 etp_submit (req);
646 }
647
648 unsigned int eio_nreqs (void)
649 {
650 return etp_nreqs ();
651 }
652
653 unsigned int eio_nready (void)
654 {
655 return etp_nready ();
656 }
657
658 unsigned int eio_npending (void)
659 {
660 return etp_npending ();
661 }
662
663 unsigned int eio_nthreads (void)
664 {
665 return etp_nthreads ();
666 }
667
668 void eio_set_max_poll_time (double nseconds)
669 {
670 etp_set_max_poll_time (nseconds);
671 }
672
673 void eio_set_max_poll_reqs (unsigned int maxreqs)
674 {
675 etp_set_max_poll_reqs (maxreqs);
676 }
677
678 void eio_set_max_idle (unsigned int nthreads)
679 {
680 etp_set_max_idle (nthreads);
681 }
682
683 void eio_set_min_parallel (unsigned int nthreads)
684 {
685 etp_set_min_parallel (nthreads);
686 }
687
688 void eio_set_max_parallel (unsigned int nthreads)
689 {
690 etp_set_max_parallel (nthreads);
691 }
692
693 int eio_poll (void)
694 {
695 return etp_poll ();
696 }
697
698 /*****************************************************************************/
699 /* work around various missing functions */
700
701 #if !HAVE_PREADWRITE
702 # define pread eio__pread
703 # define pwrite eio__pwrite
704
705 /*
706 * make our pread/pwrite safe against themselves, but not against
707 * normal read/write by using a mutex. slows down execution a lot,
708 * but that's your problem, not mine.
709 */
710 static mutex_t preadwritelock = X_MUTEX_INIT;
711
712 static ssize_t
713 eio__pread (int fd, void *buf, size_t count, off_t offset)
714 {
715 ssize_t res;
716 off_t ooffset;
717
718 X_LOCK (preadwritelock);
719 ooffset = lseek (fd, 0, SEEK_CUR);
720 lseek (fd, offset, SEEK_SET);
721 res = read (fd, buf, count);
722 lseek (fd, ooffset, SEEK_SET);
723 X_UNLOCK (preadwritelock);
724
725 return res;
726 }
727
728 static ssize_t
729 eio__pwrite (int fd, void *buf, size_t count, off_t offset)
730 {
731 ssize_t res;
732 off_t ooffset;
733
734 X_LOCK (preadwritelock);
735 ooffset = lseek (fd, 0, SEEK_CUR);
736 lseek (fd, offset, SEEK_SET);
737 res = write (fd, buf, count);
738 lseek (fd, offset, SEEK_SET);
739 X_UNLOCK (preadwritelock);
740
741 return res;
742 }
743 #endif
744
745 #ifndef HAVE_FUTIMES
746
747 # define utimes(path,times) eio__utimes (path, times)
748 # define futimes(fd,times) eio__futimes (fd, times)
749
750 static int
751 eio__utimes (const char *filename, const struct timeval times[2])
752 {
753 if (times)
754 {
755 struct utimbuf buf;
756
757 buf.actime = times[0].tv_sec;
758 buf.modtime = times[1].tv_sec;
759
760 return utime (filename, &buf);
761 }
762 else
763 return utime (filename, 0);
764 }
765
766 static int eio__futimes (int fd, const struct timeval tv[2])
767 {
768 errno = ENOSYS;
769 return -1;
770 }
771
772 #endif
773
774 #if !HAVE_FDATASYNC
775 # define fdatasync fsync
776 #endif
777
778 #if !HAVE_READAHEAD
779 # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
780
781 static ssize_t
782 eio__readahead (int fd, off_t offset, size_t count, worker *self)
783 {
784 size_t todo = count;
785 dBUF;
786
787 while (todo > 0)
788 {
789 size_t len = todo < EIO_BUFSIZE ? todo : EIO_BUFSIZE;
790
791 pread (fd, eio_buf, len, offset);
792 offset += len;
793 todo -= len;
794 }
795
796 errno = 0;
797 return count;
798 }
799
800 #endif
801
802 #if !HAVE_READDIR_R
803 # define readdir_r eio__readdir_r
804
805 static mutex_t readdirlock = X_MUTEX_INIT;
806
807 static int
808 eio__readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res)
809 {
810 EIO_STRUCT_DIRENT *e;
811 int errorno;
812
813 X_LOCK (readdirlock);
814
815 e = readdir (dirp);
816 errorno = errno;
817
818 if (e)
819 {
820 *res = ent;
821 strcpy (ent->d_name, e->d_name);
822 }
823 else
824 *res = 0;
825
826 X_UNLOCK (readdirlock);
827
828 errno = errorno;
829 return e ? 0 : -1;
830 }
831 #endif
832
833 /* sendfile always needs emulation */
834 static ssize_t
835 eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
836 {
837 ssize_t res;
838
839 if (!count)
840 return 0;
841
842 #if HAVE_SENDFILE
843 # if __linux
844 res = sendfile (ofd, ifd, &offset, count);
845
846 # elif __freebsd
847 /*
848 * Of course, the freebsd sendfile is a dire hack with no thoughts
849 * wasted on making it similar to other I/O functions.
850 */
851 {
852 off_t sbytes;
853 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
854
855 if (res < 0 && sbytes)
856 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
857 res = sbytes;
858 }
859
860 # elif __hpux
861 res = sendfile (ofd, ifd, offset, count, 0, 0);
862
863 # elif __solaris
864 {
865 struct sendfilevec vec;
866 size_t sbytes;
867
868 vec.sfv_fd = ifd;
869 vec.sfv_flag = 0;
870 vec.sfv_off = offset;
871 vec.sfv_len = count;
872
873 res = sendfilev (ofd, &vec, 1, &sbytes);
874
875 if (res < 0 && sbytes)
876 res = sbytes;
877 }
878
879 # endif
880 #else
881 res = -1;
882 errno = ENOSYS;
883 #endif
884
885 if (res < 0
886 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
887 #if __solaris
888 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
889 #endif
890 )
891 )
892 {
893 /* emulate sendfile. this is a major pain in the ass */
894 dBUF;
895
896 res = 0;
897
898 while (count)
899 {
900 ssize_t cnt;
901
902 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
903
904 if (cnt <= 0)
905 {
906 if (cnt && !res) res = -1;
907 break;
908 }
909
910 cnt = write (ofd, eio_buf, cnt);
911
912 if (cnt <= 0)
913 {
914 if (cnt && !res) res = -1;
915 break;
916 }
917
918 offset += cnt;
919 res += cnt;
920 count -= cnt;
921 }
922 }
923
924 return res;
925 }
926
927 /* read a full directory */
928 static void
929 eio__scandir (eio_req *req, etp_worker *self)
930 {
931 DIR *dirp;
932 union
933 {
934 EIO_STRUCT_DIRENT d;
935 char b [offsetof (EIO_STRUCT_DIRENT, d_name) + NAME_MAX + 1];
936 } *u;
937 EIO_STRUCT_DIRENT *entp;
938 char *name, *names;
939 int memlen = 4096;
940 int memofs = 0;
941 int res = 0;
942
943 X_LOCK (wrklock);
944 self->dirp = dirp = opendir (req->ptr1);
945 self->dbuf = u = malloc (sizeof (*u));
946 req->flags |= EIO_FLAG_PTR2_FREE;
947 req->ptr2 = names = malloc (memlen);
948 X_UNLOCK (wrklock);
949
950 if (dirp && u && names)
951 for (;;)
952 {
953 errno = 0;
954 readdir_r (dirp, &u->d, &entp);
955
956 if (!entp)
957 break;
958
959 name = entp->d_name;
960
961 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
962 {
963 int len = strlen (name) + 1;
964
965 res++;
966
967 while (memofs + len > memlen)
968 {
969 memlen *= 2;
970 X_LOCK (wrklock);
971 req->ptr2 = names = realloc (names, memlen);
972 X_UNLOCK (wrklock);
973
974 if (!names)
975 break;
976 }
977
978 memcpy (names + memofs, name, len);
979 memofs += len;
980 }
981 }
982
983 if (errno)
984 res = -1;
985
986 req->result = res;
987 }
988
989 /*****************************************************************************/
990
991 #define ALLOC(len) \
992 if (!req->ptr2) \
993 { \
994 X_LOCK (wrklock); \
995 req->flags |= EIO_FLAG_PTR2_FREE; \
996 X_UNLOCK (wrklock); \
997 req->ptr2 = malloc (len); \
998 if (!req->ptr2) \
999 { \
1000 errno = ENOMEM; \
1001 req->result = -1; \
1002 break; \
1003 } \
1004 }
1005
1006 X_THREAD_PROC (etp_proc)
1007 {
1008 ETP_REQ *req;
1009 struct timespec ts;
1010 etp_worker *self = (etp_worker *)thr_arg;
1011
1012 /* try to distribute timeouts somewhat randomly */
1013 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1014
1015 for (;;)
1016 {
1017 X_LOCK (reqlock);
1018
1019 for (;;)
1020 {
1021 self->req = req = reqq_shift (&req_queue);
1022
1023 if (req)
1024 break;
1025
1026 ++idle;
1027
1028 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1029 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1030 {
1031 if (idle > max_idle)
1032 {
1033 --idle;
1034 X_UNLOCK (reqlock);
1035 X_LOCK (wrklock);
1036 --started;
1037 X_UNLOCK (wrklock);
1038 goto quit;
1039 }
1040
1041 /* we are allowed to idle, so do so without any timeout */
1042 X_COND_WAIT (reqwait, reqlock);
1043 }
1044
1045 --idle;
1046 }
1047
1048 --nready;
1049
1050 X_UNLOCK (reqlock);
1051
1052 errno = 0; /* strictly unnecessary */
1053
1054 if (!EIO_CANCELLED (req))
1055 switch (req->type)
1056 {
1057 case EIO_READ: ALLOC (req->size);
1058 req->result = req->offs >= 0
1059 ? pread (req->int1, req->ptr2, req->size, req->offs)
1060 : read (req->int1, req->ptr2, req->size); break;
1061 case EIO_WRITE: req->result = req->offs >= 0
1062 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1063 : write (req->int1, req->ptr2, req->size); break;
1064
1065 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1066 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break;
1067
1068 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1069 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1070 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1071 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1072 case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1073 req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break;
1074
1075 case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1076 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1077 case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break;
1078 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
1079 case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1080 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1081
1082 case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break;
1083 case EIO_CLOSE: req->result = close (req->int1); break;
1084 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
1085 case EIO_UNLINK: req->result = unlink (req->ptr1); break;
1086 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1087 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1088 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1089 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1090 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1091 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1092
1093 case EIO_READLINK: ALLOC (NAME_MAX);
1094 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break;
1095
1096 case EIO_SYNC: req->result = 0; sync (); break;
1097 case EIO_FSYNC: req->result = fsync (req->int1); break;
1098 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1099
1100 case EIO_READDIR: eio__scandir (req, self); break;
1101
1102 case EIO_BUSY:
1103 #ifdef _WIN32
1104 Sleep (req->nv1 * 1000.);
1105 #else
1106 {
1107 struct timeval tv;
1108
1109 tv.tv_sec = req->nv1;
1110 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1111
1112 req->result = select (0, 0, 0, 0, &tv);
1113 }
1114 #endif
1115 break;
1116
1117 case EIO_UTIME:
1118 case EIO_FUTIME:
1119 {
1120 struct timeval tv[2];
1121 struct timeval *times;
1122
1123 if (req->nv1 != -1. || req->nv2 != -1.)
1124 {
1125 tv[0].tv_sec = req->nv1;
1126 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1127 tv[1].tv_sec = req->nv2;
1128 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1129
1130 times = tv;
1131 }
1132 else
1133 times = 0;
1134
1135
1136 req->result = req->type == EIO_FUTIME
1137 ? futimes (req->int1, times)
1138 : utimes (req->ptr1, times);
1139 }
1140
1141 case EIO_GROUP:
1142 case EIO_NOP:
1143 req->result = 0;
1144 break;
1145
1146 case -1:
1147 goto quit;
1148
1149 default:
1150 req->result = -1;
1151 break;
1152 }
1153
1154 req->errorno = errno;
1155
1156 X_LOCK (reslock);
1157
1158 ++npending;
1159
1160 if (!reqq_push (&res_queue, req) && want_poll_cb)
1161 want_poll_cb ();
1162
1163 self->req = 0;
1164 etp_worker_clear (self);
1165
1166 X_UNLOCK (reslock);
1167 }
1168
1169 quit:
1170 X_LOCK (wrklock);
1171 etp_worker_free (self);
1172 X_UNLOCK (wrklock);
1173
1174 return 0;
1175 }
1176
1177 /*****************************************************************************/
1178
1179 int eio_init (void (*want_poll)(void), void (*done_poll)(void))
1180 {
1181 etp_init (want_poll, done_poll);
1182 }
1183
1184 static void eio_api_destroy (eio_req *req)
1185 {
1186 free (req);
1187 }
1188
1189 #define REQ(rtype) \
1190 eio_req *req; \
1191 \
1192 req = (eio_req *)calloc (1, sizeof *req); \
1193 if (!req) \
1194 return 0; \
1195 \
1196 req->type = rtype; \
1197 req->pri = pri; \
1198 req->finish = cb; \
1199 req->data = data; \
1200 req->destroy = eio_api_destroy;
1201
1202 #define SEND eio_submit (req); return req
1203
1204 #define PATH \
1205 req->flags |= EIO_FLAG_PTR1_FREE; \
1206 req->ptr1 = strdup (path); \
1207 if (!req->ptr1) \
1208 { \
1209 eio_api_destroy (req); \
1210 return 0; \
1211 }
1212
1213 #ifndef EIO_NO_WRAPPERS
1214
1215 eio_req *eio_nop (int pri, eio_cb cb, void *data)
1216 {
1217 REQ (EIO_NOP); SEND;
1218 }
1219
1220 eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data)
1221 {
1222 REQ (EIO_BUSY); req->nv1 = delay; SEND;
1223 }
1224
1225 eio_req *eio_sync (int pri, eio_cb cb, void *data)
1226 {
1227 REQ (EIO_SYNC); SEND;
1228 }
1229
1230 eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data)
1231 {
1232 REQ (EIO_FSYNC); req->int1 = fd; SEND;
1233 }
1234
1235 eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1236 {
1237 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1238 }
1239
1240 eio_req *eio_close (int fd, int pri, eio_cb cb, void *data)
1241 {
1242 REQ (EIO_CLOSE); req->int1 = fd; SEND;
1243 }
1244
1245 eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
1246 {
1247 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
1248 }
1249
1250 eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1251 {
1252 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1253 }
1254
1255 eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1256 {
1257 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1258 }
1259
1260 eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
1261 {
1262 REQ (EIO_FSTAT); req->int1 = fd; SEND;
1263 }
1264
1265 eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data)
1266 {
1267 REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND;
1268 }
1269
1270 eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data)
1271 {
1272 REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND;
1273 }
1274
1275 eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
1276 {
1277 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
1278 }
1279
1280 eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1281 {
1282 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1283 }
1284
1285 eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
1286 {
1287 REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND;
1288 }
1289
1290 eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, int pri, eio_cb cb, void *data)
1291 {
1292 REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND;
1293 }
1294
1295 eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data)
1296 {
1297 REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND;
1298 }
1299
1300 eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data)
1301 {
1302 REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND;
1303 }
1304
1305 eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
1306 {
1307 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
1308 }
1309
1310 eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1311 {
1312 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1313 }
1314
1315 eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1316 {
1317 REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND;
1318 }
1319
1320 eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1321 {
1322 REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND;
1323 }
1324
1325 static eio_req *
1326 eio__1path (int type, const char *path, int pri, eio_cb cb, void *data)
1327 {
1328 REQ (type); PATH; SEND;
1329 }
1330
1331 eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1332 {
1333 return eio__1path (EIO_READLINK, path, pri, cb, data);
1334 }
1335
1336 eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1337 {
1338 return eio__1path (EIO_STAT, path, pri, cb, data);
1339 }
1340
1341 eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data)
1342 {
1343 return eio__1path (EIO_LSTAT, path, pri, cb, data);
1344 }
1345
1346 eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data)
1347 {
1348 return eio__1path (EIO_UNLINK, path, pri, cb, data);
1349 }
1350
1351 eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data)
1352 {
1353 return eio__1path (EIO_RMDIR, path, pri, cb, data);
1354 }
1355
1356 eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data)
1357 {
1358 return eio__1path (EIO_READDIR, path, pri, cb, data);
1359 }
1360
1361 eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1362 {
1363 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND;
1364 }
1365
1366 static eio_req *
1367 eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1368 {
1369 REQ (type); PATH;
1370
1371 req->flags |= EIO_FLAG_PTR2_FREE;
1372 req->ptr2 = strdup (new_path);
1373 if (!req->ptr2)
1374 {
1375 eio_api_destroy (req);
1376 return 0;
1377 }
1378
1379 SEND;
1380 }
1381
1382 eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1383 {
1384 return eio__2path (EIO_LINK, path, new_path, pri, cb, data);
1385 }
1386
1387 eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1388 {
1389 return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data);
1390 }
1391
1392 eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1393 {
1394 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1395 }
1396
1397 #endif
1398
1399 eio_req *eio_grp (eio_cb cb, void *data)
1400 {
1401 const int pri = EIO_PRI_MAX;
1402
1403 REQ (EIO_GROUP); SEND;
1404 }
1405
1406 #undef REQ
1407 #undef PATH
1408 #undef SEND
1409
1410 /*****************************************************************************/
1411 /* grp functions */
1412
1413 void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
1414 {
1415 grp->int2 = limit;
1416 grp->feed = feed;
1417
1418 grp_try_feed (grp);
1419 }
1420
1421 void eio_grp_limit (eio_req *grp, int limit)
1422 {
1423 grp->int2 = limit;
1424
1425 grp_try_feed (grp);
1426 }
1427
1428 void eio_grp_add (eio_req *grp, eio_req *req)
1429 {
1430 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
1431
1432 ++grp->size;
1433 req->grp = grp;
1434
1435 req->grp_prev = 0;
1436 req->grp_next = grp->grp_first;
1437
1438 if (grp->grp_first)
1439 grp->grp_first->grp_prev = req;
1440
1441 grp->grp_first = req;
1442 }
1443
1444 /*****************************************************************************/
1445 /* misc garbage */
1446
1447 ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1448 {
1449 etp_worker wrk;
1450
1451 wrk.dbuf = 0;
1452
1453 eio__sendfile (ofd, ifd, offset, count, &wrk);
1454
1455 if (wrk.dbuf)
1456 free (wrk.dbuf);
1457 }
1458