ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
Revision: 1.15
Committed: Sat May 17 12:26:57 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_03
Changes since 1.14: +119 -101 lines
Log Message:
*** empty log message ***

File Contents

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