ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
Revision: 1.19
Committed: Tue Jun 17 23:34:20 2008 UTC (15 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_04
Changes since 1.18: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * libeio implementation
3 *
4 * Copyright (c) 2007,2008 Marc Alexander Lehmann <libeio@schmorp.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License ("GPL") version 2 or any later version,
30 * in which case the provisions of the GPL are applicable instead of
31 * the above. If you wish to allow the use of your version of this file
32 * only under the terms of the GPL and not to allow others to use your
33 * version of this file under the BSD license, indicate your decision
34 * by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL.
38 */
39
40 #include "eio.h"
41 #include "xthread.h"
42
43 #include <errno.h>
44 #include <stddef.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <limits.h>
51 #include <fcntl.h>
52 #include <assert.h>
53
54 #ifndef EIO_FINISH
55 # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
56 #endif
57
58 #ifndef EIO_DESTROY
59 # define EIO_DESTROY(req) do { if ((req)->destroy) (req)->destroy (req); } while (0)
60 #endif
61
62 #ifndef EIO_FEED
63 # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
64 #endif
65
66 #ifdef _WIN32
67
68 /*doh*/
69
70 #else
71
72 # include "config.h"
73 # include <sys/time.h>
74 # include <sys/select.h>
75 # include <unistd.h>
76 # include <utime.h>
77 # include <signal.h>
78 # include <dirent.h>
79
80 # ifndef EIO_STRUCT_DIRENT
81 # define EIO_STRUCT_DIRENT struct dirent
82 # endif
83
84 #endif
85
86 #if HAVE_SENDFILE
87 # if __linux
88 # include <sys/sendfile.h>
89 # elif __freebsd
90 # include <sys/socket.h>
91 # include <sys/uio.h>
92 # elif __hpux
93 # include <sys/socket.h>
94 # elif __solaris /* not yet */
95 # include <sys/sendfile.h>
96 # else
97 # error sendfile support requested but not available
98 # endif
99 #endif
100
101 /* number of seconds after which an idle threads exit */
102 #define IDLE_TIMEOUT 10
103
104 /* used for struct dirent, AIX doesn't provide it */
105 #ifndef NAME_MAX
106 # define NAME_MAX 4096
107 #endif
108
109 /* buffer size for various temporary buffers */
110 #define EIO_BUFSIZE 65536
111
112 #define dBUF \
113 char *eio_buf; \
114 ETP_WORKER_LOCK (self); \
115 self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \
116 ETP_WORKER_UNLOCK (self); \
117 errno = ENOMEM; \
118 if (!eio_buf) \
119 return -1;
120
121 #define EIO_TICKS ((1000000 + 1023) >> 10)
122
123 /*****************************************************************************/
124
125 #define ETP_PRI_MIN EIO_PRI_MIN
126 #define ETP_PRI_MAX EIO_PRI_MAX
127
128 struct etp_worker;
129
130 #define ETP_REQ eio_req
131 #define ETP_DESTROY(req) eio_destroy (req)
132 static int eio_finish (eio_req *req);
133 #define ETP_FINISH(req) eio_finish (req)
134 static void eio_execute (struct etp_worker *self, eio_req *req);
135 #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req)
136
137 #define ETP_WORKER_CLEAR(req) \
138 if (wrk->dbuf) \
139 { \
140 free (wrk->dbuf); \
141 wrk->dbuf = 0; \
142 } \
143 \
144 if (wrk->dirp) \
145 { \
146 closedir (wrk->dirp); \
147 wrk->dirp = 0; \
148 }
149 #define ETP_WORKER_COMMON \
150 void *dbuf; \
151 DIR *dirp;
152
153 /*****************************************************************************/
154
155 #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
156
157 /* calculcate time difference in ~1/EIO_TICKS of a second */
158 static int tvdiff (struct timeval *tv1, struct timeval *tv2)
159 {
160 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
161 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
162 }
163
164 static unsigned int started, idle, wanted = 4;
165
166 static void (*want_poll_cb) (void);
167 static void (*done_poll_cb) (void);
168
169 static unsigned int max_poll_time; /* reslock */
170 static unsigned int max_poll_reqs; /* reslock */
171
172 static volatile unsigned int nreqs; /* reqlock */
173 static volatile unsigned int nready; /* reqlock */
174 static volatile unsigned int npending; /* reqlock */
175 static volatile unsigned int max_idle = 4;
176
177 static mutex_t wrklock = X_MUTEX_INIT;
178 static mutex_t reslock = X_MUTEX_INIT;
179 static mutex_t reqlock = X_MUTEX_INIT;
180 static cond_t reqwait = X_COND_INIT;
181
182 typedef struct etp_worker
183 {
184 /* locked by wrklock */
185 struct etp_worker *prev, *next;
186
187 thread_t tid;
188
189 /* locked by reslock, reqlock or wrklock */
190 ETP_REQ *req; /* currently processed request */
191
192 ETP_WORKER_COMMON
193 } etp_worker;
194
195 static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */
196
197 #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
198 #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
199
200 /* worker threads management */
201
202 static void etp_worker_clear (etp_worker *wrk)
203 {
204 ETP_WORKER_CLEAR (wrk);
205 }
206
207 static void etp_worker_free (etp_worker *wrk)
208 {
209 wrk->next->prev = wrk->prev;
210 wrk->prev->next = wrk->next;
211
212 free (wrk);
213 }
214
215 static unsigned int etp_nreqs (void)
216 {
217 int retval;
218 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
219 retval = nreqs;
220 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
221 return retval;
222 }
223
224 static unsigned int etp_nready (void)
225 {
226 unsigned int retval;
227
228 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
229 retval = nready;
230 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
231
232 return retval;
233 }
234
235 static unsigned int etp_npending (void)
236 {
237 unsigned int retval;
238
239 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
240 retval = npending;
241 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
242
243 return retval;
244 }
245
246 static unsigned int etp_nthreads (void)
247 {
248 unsigned int retval;
249
250 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
251 retval = started;
252 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
253
254 return retval;
255 }
256
257 /*
258 * a somewhat faster data structure might be nice, but
259 * with 8 priorities this actually needs <20 insns
260 * per shift, the most expensive operation.
261 */
262 typedef struct {
263 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
264 int size;
265 } etp_reqq;
266
267 static etp_reqq req_queue;
268 static etp_reqq res_queue;
269
270 static int reqq_push (etp_reqq *q, ETP_REQ *req)
271 {
272 int pri = req->pri;
273 req->next = 0;
274
275 if (q->qe[pri])
276 {
277 q->qe[pri]->next = req;
278 q->qe[pri] = req;
279 }
280 else
281 q->qe[pri] = q->qs[pri] = req;
282
283 return q->size++;
284 }
285
286 static ETP_REQ *reqq_shift (etp_reqq *q)
287 {
288 int pri;
289
290 if (!q->size)
291 return 0;
292
293 --q->size;
294
295 for (pri = ETP_NUM_PRI; pri--; )
296 {
297 eio_req *req = q->qs[pri];
298
299 if (req)
300 {
301 if (!(q->qs[pri] = (eio_req *)req->next))
302 q->qe[pri] = 0;
303
304 return req;
305 }
306 }
307
308 abort ();
309 }
310
311 static void etp_atfork_prepare (void)
312 {
313 X_LOCK (wrklock);
314 X_LOCK (reqlock);
315 X_LOCK (reslock);
316 #if !HAVE_PREADWRITE
317 X_LOCK (preadwritelock);
318 #endif
319 }
320
321 static void etp_atfork_parent (void)
322 {
323 #if !HAVE_PREADWRITE
324 X_UNLOCK (preadwritelock);
325 #endif
326 X_UNLOCK (reslock);
327 X_UNLOCK (reqlock);
328 X_UNLOCK (wrklock);
329 }
330
331 static void etp_atfork_child (void)
332 {
333 ETP_REQ *prv;
334
335 while (prv = reqq_shift (&req_queue))
336 ETP_DESTROY (prv);
337
338 while (prv = reqq_shift (&res_queue))
339 ETP_DESTROY (prv);
340
341 while (wrk_first.next != &wrk_first)
342 {
343 etp_worker *wrk = wrk_first.next;
344
345 if (wrk->req)
346 ETP_DESTROY (wrk->req);
347
348 etp_worker_clear (wrk);
349 etp_worker_free (wrk);
350 }
351
352 started = 0;
353 idle = 0;
354 nreqs = 0;
355 nready = 0;
356 npending = 0;
357
358 etp_atfork_parent ();
359 }
360
361 static void
362 etp_once_init (void)
363 {
364 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
365 }
366
367 static int
368 etp_init (void (*want_poll)(void), void (*done_poll)(void))
369 {
370 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
371
372 pthread_once (&doinit, etp_once_init);
373
374 want_poll_cb = want_poll;
375 done_poll_cb = done_poll;
376
377 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 self->dirp = dirp = opendir (req->ptr1);
909 req->flags |= EIO_FLAG_PTR2_FREE;
910 req->ptr2 = names = malloc (memlen);
911 X_UNLOCK (wrklock);
912
913 if (dirp && names)
914 for (;;)
915 {
916 errno = 0;
917 entp = readdir (dirp);
918
919 if (!entp)
920 break;
921
922 name = entp->d_name;
923
924 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
925 {
926 int len = strlen (name) + 1;
927
928 res++;
929
930 while (memofs + len > memlen)
931 {
932 memlen *= 2;
933 X_LOCK (wrklock);
934 req->ptr2 = names = realloc (names, memlen);
935 X_UNLOCK (wrklock);
936
937 if (!names)
938 break;
939 }
940
941 memcpy (names + memofs, name, len);
942 memofs += len;
943 }
944 }
945
946 if (errno)
947 res = -1;
948
949 req->result = res;
950 }
951
952 /*****************************************************************************/
953
954 #define ALLOC(len) \
955 if (!req->ptr2) \
956 { \
957 X_LOCK (wrklock); \
958 req->flags |= EIO_FLAG_PTR2_FREE; \
959 X_UNLOCK (wrklock); \
960 req->ptr2 = malloc (len); \
961 if (!req->ptr2) \
962 { \
963 errno = ENOMEM; \
964 req->result = -1; \
965 break; \
966 } \
967 }
968
969 X_THREAD_PROC (etp_proc)
970 {
971 ETP_REQ *req;
972 struct timespec ts;
973 etp_worker *self = (etp_worker *)thr_arg;
974
975 /* try to distribute timeouts somewhat randomly */
976 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
977
978 for (;;)
979 {
980 X_LOCK (reqlock);
981
982 for (;;)
983 {
984 self->req = req = reqq_shift (&req_queue);
985
986 if (req)
987 break;
988
989 ++idle;
990
991 ts.tv_sec = time (0) + IDLE_TIMEOUT;
992 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
993 {
994 if (idle > max_idle)
995 {
996 --idle;
997 X_UNLOCK (reqlock);
998 X_LOCK (wrklock);
999 --started;
1000 X_UNLOCK (wrklock);
1001 goto quit;
1002 }
1003
1004 /* we are allowed to idle, so do so without any timeout */
1005 X_COND_WAIT (reqwait, reqlock);
1006 }
1007
1008 --idle;
1009 }
1010
1011 --nready;
1012
1013 X_UNLOCK (reqlock);
1014
1015 if (req->type < 0)
1016 goto quit;
1017
1018 if (!EIO_CANCELLED (req))
1019 ETP_EXECUTE (self, req);
1020
1021 X_LOCK (reslock);
1022
1023 ++npending;
1024
1025 if (!reqq_push (&res_queue, req) && want_poll_cb)
1026 want_poll_cb ();
1027
1028 self->req = 0;
1029 etp_worker_clear (self);
1030
1031 X_UNLOCK (reslock);
1032 }
1033
1034 quit:
1035 X_LOCK (wrklock);
1036 etp_worker_free (self);
1037 X_UNLOCK (wrklock);
1038
1039 return 0;
1040 }
1041
1042 /*****************************************************************************/
1043
1044 int eio_init (void (*want_poll)(void), void (*done_poll)(void))
1045 {
1046 return etp_init (want_poll, done_poll);
1047 }
1048
1049 static void eio_api_destroy (eio_req *req)
1050 {
1051 free (req);
1052 }
1053
1054 #define REQ(rtype) \
1055 eio_req *req; \
1056 \
1057 req = (eio_req *)calloc (1, sizeof *req); \
1058 if (!req) \
1059 return 0; \
1060 \
1061 req->type = rtype; \
1062 req->pri = pri; \
1063 req->finish = cb; \
1064 req->data = data; \
1065 req->destroy = eio_api_destroy;
1066
1067 #define SEND eio_submit (req); return req
1068
1069 #define PATH \
1070 req->flags |= EIO_FLAG_PTR1_FREE; \
1071 req->ptr1 = strdup (path); \
1072 if (!req->ptr1) \
1073 { \
1074 eio_api_destroy (req); \
1075 return 0; \
1076 }
1077
1078 static void eio_execute (etp_worker *self, eio_req *req)
1079 {
1080 errno = 0;
1081
1082 switch (req->type)
1083 {
1084 case EIO_READ: ALLOC (req->size);
1085 req->result = req->offs >= 0
1086 ? pread (req->int1, req->ptr2, req->size, req->offs)
1087 : read (req->int1, req->ptr2, req->size); break;
1088 case EIO_WRITE: req->result = req->offs >= 0
1089 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1090 : write (req->int1, req->ptr2, req->size); break;
1091
1092 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1093 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break;
1094
1095 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1096 req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1097 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1098 req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1099 case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1100 req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break;
1101
1102 case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1103 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1104 case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break;
1105 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
1106 case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1107 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1108
1109 case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break;
1110 case EIO_CLOSE: req->result = close (req->int1); break;
1111 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
1112 case EIO_UNLINK: req->result = unlink (req->ptr1); break;
1113 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1114 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1115 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1116 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1117 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1118 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break;
1119
1120 case EIO_READLINK: ALLOC (NAME_MAX);
1121 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break;
1122
1123 case EIO_SYNC: req->result = 0; sync (); break;
1124 case EIO_FSYNC: req->result = fsync (req->int1); break;
1125 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1126
1127 case EIO_READDIR: eio__scandir (req, self); break;
1128
1129 case EIO_BUSY:
1130 #ifdef _WIN32
1131 Sleep (req->nv1 * 1000.);
1132 #else
1133 {
1134 struct timeval tv;
1135
1136 tv.tv_sec = req->nv1;
1137 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1138
1139 req->result = select (0, 0, 0, 0, &tv);
1140 }
1141 #endif
1142 break;
1143
1144 case EIO_UTIME:
1145 case EIO_FUTIME:
1146 {
1147 struct timeval tv[2];
1148 struct timeval *times;
1149
1150 if (req->nv1 != -1. || req->nv2 != -1.)
1151 {
1152 tv[0].tv_sec = req->nv1;
1153 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1154 tv[1].tv_sec = req->nv2;
1155 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1156
1157 times = tv;
1158 }
1159 else
1160 times = 0;
1161
1162
1163 req->result = req->type == EIO_FUTIME
1164 ? futimes (req->int1, times)
1165 : utimes (req->ptr1, times);
1166 }
1167
1168 case EIO_GROUP:
1169 case EIO_NOP:
1170 req->result = 0;
1171 break;
1172
1173 case EIO_CUSTOM:
1174 req->feed (req);
1175 break;
1176
1177 default:
1178 req->result = -1;
1179 break;
1180 }
1181
1182 req->errorno = errno;
1183 }
1184
1185 #ifndef EIO_NO_WRAPPERS
1186
1187 eio_req *eio_nop (int pri, eio_cb cb, void *data)
1188 {
1189 REQ (EIO_NOP); SEND;
1190 }
1191
1192 eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data)
1193 {
1194 REQ (EIO_BUSY); req->nv1 = delay; SEND;
1195 }
1196
1197 eio_req *eio_sync (int pri, eio_cb cb, void *data)
1198 {
1199 REQ (EIO_SYNC); SEND;
1200 }
1201
1202 eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data)
1203 {
1204 REQ (EIO_FSYNC); req->int1 = fd; SEND;
1205 }
1206
1207 eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1208 {
1209 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1210 }
1211
1212 eio_req *eio_close (int fd, int pri, eio_cb cb, void *data)
1213 {
1214 REQ (EIO_CLOSE); req->int1 = fd; SEND;
1215 }
1216
1217 eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
1218 {
1219 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
1220 }
1221
1222 eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1223 {
1224 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1225 }
1226
1227 eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1228 {
1229 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1230 }
1231
1232 eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
1233 {
1234 REQ (EIO_FSTAT); req->int1 = fd; SEND;
1235 }
1236
1237 eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data)
1238 {
1239 REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND;
1240 }
1241
1242 eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data)
1243 {
1244 REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND;
1245 }
1246
1247 eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
1248 {
1249 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
1250 }
1251
1252 eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1253 {
1254 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1255 }
1256
1257 eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
1258 {
1259 REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND;
1260 }
1261
1262 eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, int pri, eio_cb cb, void *data)
1263 {
1264 REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND;
1265 }
1266
1267 eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data)
1268 {
1269 REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND;
1270 }
1271
1272 eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data)
1273 {
1274 REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND;
1275 }
1276
1277 eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
1278 {
1279 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
1280 }
1281
1282 eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1283 {
1284 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1285 }
1286
1287 eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1288 {
1289 REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND;
1290 }
1291
1292 eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1293 {
1294 REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND;
1295 }
1296
1297 static eio_req *
1298 eio__1path (int type, const char *path, int pri, eio_cb cb, void *data)
1299 {
1300 REQ (type); PATH; SEND;
1301 }
1302
1303 eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1304 {
1305 return eio__1path (EIO_READLINK, path, pri, cb, data);
1306 }
1307
1308 eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1309 {
1310 return eio__1path (EIO_STAT, path, pri, cb, data);
1311 }
1312
1313 eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data)
1314 {
1315 return eio__1path (EIO_LSTAT, path, pri, cb, data);
1316 }
1317
1318 eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data)
1319 {
1320 return eio__1path (EIO_UNLINK, path, pri, cb, data);
1321 }
1322
1323 eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data)
1324 {
1325 return eio__1path (EIO_RMDIR, path, pri, cb, data);
1326 }
1327
1328 eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data)
1329 {
1330 return eio__1path (EIO_READDIR, path, pri, cb, data);
1331 }
1332
1333 eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1334 {
1335 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND;
1336 }
1337
1338 static eio_req *
1339 eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1340 {
1341 REQ (type); PATH;
1342
1343 req->flags |= EIO_FLAG_PTR2_FREE;
1344 req->ptr2 = strdup (new_path);
1345 if (!req->ptr2)
1346 {
1347 eio_api_destroy (req);
1348 return 0;
1349 }
1350
1351 SEND;
1352 }
1353
1354 eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1355 {
1356 return eio__2path (EIO_LINK, path, new_path, pri, cb, data);
1357 }
1358
1359 eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1360 {
1361 return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data);
1362 }
1363
1364 eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1365 {
1366 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1367 }
1368
1369 eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data)
1370 {
1371 REQ (EIO_CUSTOM); req->feed = execute; SEND;
1372 }
1373
1374 #endif
1375
1376 eio_req *eio_grp (eio_cb cb, void *data)
1377 {
1378 const int pri = EIO_PRI_MAX;
1379
1380 REQ (EIO_GROUP); SEND;
1381 }
1382
1383 #undef REQ
1384 #undef PATH
1385 #undef SEND
1386
1387 /*****************************************************************************/
1388 /* grp functions */
1389
1390 void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
1391 {
1392 grp->int2 = limit;
1393 grp->feed = feed;
1394
1395 grp_try_feed (grp);
1396 }
1397
1398 void eio_grp_limit (eio_req *grp, int limit)
1399 {
1400 grp->int2 = limit;
1401
1402 grp_try_feed (grp);
1403 }
1404
1405 void eio_grp_add (eio_req *grp, eio_req *req)
1406 {
1407 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
1408
1409 ++grp->size;
1410 req->grp = grp;
1411
1412 req->grp_prev = 0;
1413 req->grp_next = grp->grp_first;
1414
1415 if (grp->grp_first)
1416 grp->grp_first->grp_prev = req;
1417
1418 grp->grp_first = req;
1419 }
1420
1421 /*****************************************************************************/
1422 /* misc garbage */
1423
1424 ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1425 {
1426 etp_worker wrk;
1427
1428 wrk.dbuf = 0;
1429
1430 eio__sendfile (ofd, ifd, offset, count, &wrk);
1431
1432 if (wrk.dbuf)
1433 free (wrk.dbuf);
1434 }
1435