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