ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
Revision: 1.22
Committed: Fri Jul 11 10:54:50 2008 UTC (15 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_07, rel-3_06
Changes since 1.21: +9 -7 lines
Log Message:
*** empty log message ***

File Contents

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