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