ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
Revision: 1.23
Committed: Sun Sep 21 00:23:45 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +43 -2 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 root 1.23 #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
956     # define msync(a,b,c) ENOSYS
957     #endif
958    
959     int
960     eio__mtouch (void *mem, size_t len, int flags)
961     {
962     intptr_t addr = (intptr_t)mem;
963     intptr_t end = addr + len;
964     #ifdef PAGESIZE
965     const intptr_t page = PAGESIZE;
966     #else
967     static intptr_t page;
968    
969     if (!page)
970     page = sysconf (_SC_PAGESIZE);
971     #endif
972    
973     addr &= ~(page - 1); /* assume page size is always a power of two */
974    
975     if (addr < end)
976     if (flags) /* modify */
977     do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len);
978     else
979     do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len);
980    
981     return 0;
982     }
983    
984 root 1.1 /*****************************************************************************/
985    
986 root 1.6 #define ALLOC(len) \
987     if (!req->ptr2) \
988     { \
989 root 1.14 X_LOCK (wrklock); \
990 root 1.7 req->flags |= EIO_FLAG_PTR2_FREE; \
991 root 1.14 X_UNLOCK (wrklock); \
992 root 1.7 req->ptr2 = malloc (len); \
993     if (!req->ptr2) \
994     { \
995     errno = ENOMEM; \
996     req->result = -1; \
997     break; \
998     } \
999 root 1.6 }
1000    
1001 root 1.14 X_THREAD_PROC (etp_proc)
1002 root 1.1 {
1003 root 1.14 ETP_REQ *req;
1004 root 1.1 struct timespec ts;
1005 root 1.14 etp_worker *self = (etp_worker *)thr_arg;
1006 root 1.1
1007     /* try to distribute timeouts somewhat randomly */
1008     ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1009    
1010     for (;;)
1011     {
1012     X_LOCK (reqlock);
1013    
1014     for (;;)
1015     {
1016     self->req = req = reqq_shift (&req_queue);
1017    
1018     if (req)
1019     break;
1020    
1021     ++idle;
1022    
1023 root 1.14 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1024 root 1.1 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1025     {
1026     if (idle > max_idle)
1027     {
1028     --idle;
1029     X_UNLOCK (reqlock);
1030 root 1.14 X_LOCK (wrklock);
1031 root 1.1 --started;
1032 root 1.14 X_UNLOCK (wrklock);
1033 root 1.1 goto quit;
1034     }
1035    
1036     /* we are allowed to idle, so do so without any timeout */
1037     X_COND_WAIT (reqwait, reqlock);
1038     }
1039    
1040     --idle;
1041     }
1042    
1043     --nready;
1044    
1045     X_UNLOCK (reqlock);
1046    
1047 root 1.15 if (req->type < 0)
1048     goto quit;
1049 root 1.1
1050     if (!EIO_CANCELLED (req))
1051 root 1.15 ETP_EXECUTE (self, req);
1052 root 1.1
1053     X_LOCK (reslock);
1054    
1055     ++npending;
1056    
1057 root 1.14 if (!reqq_push (&res_queue, req) && want_poll_cb)
1058     want_poll_cb ();
1059 root 1.1
1060     self->req = 0;
1061 root 1.14 etp_worker_clear (self);
1062 root 1.1
1063     X_UNLOCK (reslock);
1064     }
1065    
1066     quit:
1067 root 1.14 X_LOCK (wrklock);
1068     etp_worker_free (self);
1069     X_UNLOCK (wrklock);
1070 root 1.1
1071     return 0;
1072     }
1073    
1074     /*****************************************************************************/
1075    
1076     int eio_init (void (*want_poll)(void), void (*done_poll)(void))
1077     {
1078 root 1.19 return etp_init (want_poll, done_poll);
1079 root 1.1 }
1080    
1081 root 1.6 static void eio_api_destroy (eio_req *req)
1082     {
1083     free (req);
1084     }
1085    
1086     #define REQ(rtype) \
1087     eio_req *req; \
1088     \
1089     req = (eio_req *)calloc (1, sizeof *req); \
1090     if (!req) \
1091     return 0; \
1092     \
1093 root 1.11 req->type = rtype; \
1094     req->pri = pri; \
1095     req->finish = cb; \
1096     req->data = data; \
1097 root 1.6 req->destroy = eio_api_destroy;
1098    
1099     #define SEND eio_submit (req); return req
1100    
1101 root 1.9 #define PATH \
1102     req->flags |= EIO_FLAG_PTR1_FREE; \
1103     req->ptr1 = strdup (path); \
1104     if (!req->ptr1) \
1105     { \
1106     eio_api_destroy (req); \
1107     return 0; \
1108     }
1109    
1110 root 1.15 static void eio_execute (etp_worker *self, eio_req *req)
1111     {
1112     errno = 0;
1113    
1114     switch (req->type)
1115     {
1116     case EIO_READ: ALLOC (req->size);
1117     req->result = req->offs >= 0
1118     ? pread (req->int1, req->ptr2, req->size, req->offs)
1119     : read (req->int1, req->ptr2, req->size); break;
1120     case EIO_WRITE: req->result = req->offs >= 0
1121     ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1122     : write (req->int1, req->ptr2, req->size); break;
1123    
1124     case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1125     case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break;
1126    
1127     case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1128     req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1129     case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1130     req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break;
1131     case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
1132     req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break;
1133    
1134     case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1135     case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1136     case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break;
1137     case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
1138     case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1139     case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1140    
1141     case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break;
1142     case EIO_CLOSE: req->result = close (req->int1); break;
1143     case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
1144     case EIO_UNLINK: req->result = unlink (req->ptr1); break;
1145     case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1146     case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1147     case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1148     case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1149     case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1150 root 1.18 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break;
1151 root 1.15
1152     case EIO_READLINK: ALLOC (NAME_MAX);
1153     req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break;
1154    
1155     case EIO_SYNC: req->result = 0; sync (); break;
1156     case EIO_FSYNC: req->result = fsync (req->int1); break;
1157     case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1158 root 1.23 case EIO_MSYNC: req->result = msync (req->ptr2, req->size, req->int1); break;
1159     case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break;
1160 root 1.15
1161     case EIO_READDIR: eio__scandir (req, self); break;
1162    
1163     case EIO_BUSY:
1164     #ifdef _WIN32
1165     Sleep (req->nv1 * 1000.);
1166     #else
1167     {
1168     struct timeval tv;
1169    
1170     tv.tv_sec = req->nv1;
1171     tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1172    
1173     req->result = select (0, 0, 0, 0, &tv);
1174     }
1175     #endif
1176     break;
1177    
1178     case EIO_UTIME:
1179     case EIO_FUTIME:
1180     {
1181     struct timeval tv[2];
1182     struct timeval *times;
1183    
1184     if (req->nv1 != -1. || req->nv2 != -1.)
1185     {
1186     tv[0].tv_sec = req->nv1;
1187     tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1188     tv[1].tv_sec = req->nv2;
1189     tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1190    
1191     times = tv;
1192     }
1193     else
1194     times = 0;
1195    
1196    
1197     req->result = req->type == EIO_FUTIME
1198     ? futimes (req->int1, times)
1199     : utimes (req->ptr1, times);
1200     }
1201    
1202     case EIO_GROUP:
1203     case EIO_NOP:
1204     req->result = 0;
1205     break;
1206    
1207     case EIO_CUSTOM:
1208 root 1.23 ((void (*)(eio_req *))req->feed) (req);
1209 root 1.15 break;
1210    
1211     default:
1212     req->result = -1;
1213     break;
1214     }
1215    
1216     req->errorno = errno;
1217     }
1218    
1219 root 1.12 #ifndef EIO_NO_WRAPPERS
1220    
1221 root 1.10 eio_req *eio_nop (int pri, eio_cb cb, void *data)
1222 root 1.9 {
1223     REQ (EIO_NOP); SEND;
1224     }
1225    
1226 root 1.10 eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data)
1227 root 1.9 {
1228     REQ (EIO_BUSY); req->nv1 = delay; SEND;
1229     }
1230    
1231 root 1.10 eio_req *eio_sync (int pri, eio_cb cb, void *data)
1232 root 1.9 {
1233     REQ (EIO_SYNC); SEND;
1234     }
1235 root 1.6
1236 root 1.10 eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data)
1237 root 1.6 {
1238 root 1.9 REQ (EIO_FSYNC); req->int1 = fd; SEND;
1239 root 1.6 }
1240    
1241 root 1.23 eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1242     {
1243     REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1244     }
1245    
1246     eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1247     {
1248     REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1249     }
1250    
1251 root 1.10 eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1252 root 1.6 {
1253 root 1.9 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1254     }
1255    
1256 root 1.10 eio_req *eio_close (int fd, int pri, eio_cb cb, void *data)
1257 root 1.9 {
1258     REQ (EIO_CLOSE); req->int1 = fd; SEND;
1259 root 1.6 }
1260    
1261 root 1.10 eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
1262 root 1.6 {
1263 root 1.9 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
1264 root 1.6 }
1265    
1266 root 1.10 eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1267 root 1.6 {
1268 root 1.10 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1269 root 1.6 }
1270    
1271 root 1.10 eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
1272 root 1.6 {
1273 root 1.10 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
1274 root 1.6 }
1275    
1276 root 1.10 eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
1277 root 1.6 {
1278 root 1.9 REQ (EIO_FSTAT); req->int1 = fd; SEND;
1279 root 1.6 }
1280    
1281 root 1.10 eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data)
1282 root 1.6 {
1283 root 1.9 REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND;
1284 root 1.6 }
1285    
1286 root 1.10 eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data)
1287 root 1.6 {
1288 root 1.9 REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND;
1289 root 1.6 }
1290    
1291 root 1.10 eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
1292 root 1.6 {
1293 root 1.9 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
1294 root 1.6 }
1295    
1296 root 1.10 eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1297 root 1.6 {
1298 root 1.9 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1299 root 1.6 }
1300    
1301 root 1.10 eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
1302 root 1.6 {
1303 root 1.9 REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND;
1304 root 1.6 }
1305    
1306 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)
1307 root 1.6 {
1308 root 1.9 REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND;
1309 root 1.6 }
1310    
1311 root 1.10 eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data)
1312 root 1.6 {
1313 root 1.9 REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND;
1314 root 1.6 }
1315    
1316 root 1.10 eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data)
1317 root 1.6 {
1318 root 1.9 REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND;
1319 root 1.6 }
1320    
1321 root 1.10 eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
1322 root 1.6 {
1323 root 1.9 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
1324 root 1.6 }
1325    
1326 root 1.10 eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data)
1327 root 1.6 {
1328 root 1.9 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1329 root 1.6 }
1330    
1331 root 1.10 eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1332 root 1.6 {
1333 root 1.9 REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND;
1334 root 1.6 }
1335    
1336 root 1.10 eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1337 root 1.6 {
1338 root 1.9 REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND;
1339 root 1.6 }
1340    
1341 root 1.9 static eio_req *
1342 root 1.10 eio__1path (int type, const char *path, int pri, eio_cb cb, void *data)
1343 root 1.6 {
1344 root 1.9 REQ (type); PATH; SEND;
1345 root 1.6 }
1346    
1347 root 1.10 eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1348 root 1.6 {
1349 root 1.10 return eio__1path (EIO_READLINK, path, pri, cb, data);
1350 root 1.6 }
1351    
1352 root 1.10 eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1353 root 1.6 {
1354 root 1.10 return eio__1path (EIO_STAT, path, pri, cb, data);
1355 root 1.6 }
1356    
1357 root 1.10 eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data)
1358 root 1.6 {
1359 root 1.10 return eio__1path (EIO_LSTAT, path, pri, cb, data);
1360 root 1.6 }
1361    
1362 root 1.10 eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data)
1363 root 1.6 {
1364 root 1.10 return eio__1path (EIO_UNLINK, path, pri, cb, data);
1365 root 1.6 }
1366    
1367 root 1.10 eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data)
1368 root 1.6 {
1369 root 1.10 return eio__1path (EIO_RMDIR, path, pri, cb, data);
1370 root 1.6 }
1371    
1372 root 1.10 eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data)
1373 root 1.1 {
1374 root 1.10 return eio__1path (EIO_READDIR, path, pri, cb, data);
1375 root 1.1 }
1376    
1377 root 1.10 eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1378 root 1.1 {
1379 root 1.18 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND;
1380 root 1.1 }
1381    
1382 root 1.9 static eio_req *
1383 root 1.10 eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1384 root 1.1 {
1385 root 1.9 REQ (type); PATH;
1386 root 1.1
1387 root 1.9 req->flags |= EIO_FLAG_PTR2_FREE;
1388     req->ptr2 = strdup (new_path);
1389     if (!req->ptr2)
1390     {
1391     eio_api_destroy (req);
1392     return 0;
1393     }
1394 root 1.1
1395 root 1.9 SEND;
1396 root 1.1 }
1397    
1398 root 1.10 eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1399 root 1.1 {
1400 root 1.10 return eio__2path (EIO_LINK, path, new_path, pri, cb, data);
1401 root 1.1 }
1402    
1403 root 1.10 eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1404 root 1.1 {
1405 root 1.10 return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data);
1406 root 1.1 }
1407    
1408 root 1.10 eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1409 root 1.1 {
1410 root 1.10 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1411     }
1412    
1413 root 1.15 eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data)
1414     {
1415 root 1.23 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND;
1416 root 1.15 }
1417    
1418 root 1.12 #endif
1419    
1420 root 1.10 eio_req *eio_grp (eio_cb cb, void *data)
1421     {
1422     const int pri = EIO_PRI_MAX;
1423    
1424     REQ (EIO_GROUP); SEND;
1425 root 1.1 }
1426    
1427 root 1.9 #undef REQ
1428     #undef PATH
1429     #undef SEND
1430 root 1.1
1431 root 1.9 /*****************************************************************************/
1432     /* grp functions */
1433 root 1.1
1434 root 1.2 void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
1435 root 1.1 {
1436     grp->int2 = limit;
1437     grp->feed = feed;
1438 root 1.2
1439     grp_try_feed (grp);
1440     }
1441    
1442     void eio_grp_limit (eio_req *grp, int limit)
1443     {
1444     grp->int2 = limit;
1445    
1446     grp_try_feed (grp);
1447 root 1.1 }
1448    
1449     void eio_grp_add (eio_req *grp, eio_req *req)
1450     {
1451     assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
1452    
1453     ++grp->size;
1454     req->grp = grp;
1455    
1456     req->grp_prev = 0;
1457     req->grp_next = grp->grp_first;
1458    
1459     if (grp->grp_first)
1460     grp->grp_first->grp_prev = req;
1461    
1462     grp->grp_first = req;
1463     }
1464    
1465 root 1.9 /*****************************************************************************/
1466     /* misc garbage */
1467    
1468     ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1469     {
1470 root 1.14 etp_worker wrk;
1471 root 1.9
1472     wrk.dbuf = 0;
1473    
1474     eio__sendfile (ofd, ifd, offset, count, &wrk);
1475    
1476     if (wrk.dbuf)
1477     free (wrk.dbuf);
1478     }
1479 root 1.1