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