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