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