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