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 |
|
42 |
#ifdef EIO_STACKSIZE |
43 |
# define XTHREAD_STACKSIZE EIO_STACKSIZE |
44 |
#endif |
45 |
#include "xthread.h" |
46 |
|
47 |
#include <errno.h> |
48 |
#include <stddef.h> |
49 |
#include <stdlib.h> |
50 |
#include <string.h> |
51 |
#include <errno.h> |
52 |
#include <sys/types.h> |
53 |
#include <sys/stat.h> |
54 |
#include <limits.h> |
55 |
#include <fcntl.h> |
56 |
#include <assert.h> |
57 |
|
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 |
# include <dirent.h> |
83 |
|
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 |
ETP_WORKER_LOCK (self); \ |
119 |
self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ |
120 |
ETP_WORKER_UNLOCK (self); \ |
121 |
errno = ENOMEM; \ |
122 |
if (!eio_buf) \ |
123 |
return -1; |
124 |
|
125 |
#define EIO_TICKS ((1000000 + 1023) >> 10) |
126 |
|
127 |
/*****************************************************************************/ |
128 |
|
129 |
#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 |
#define ETP_PRI_MIN EIO_PRI_MIN |
141 |
#define ETP_PRI_MAX EIO_PRI_MAX |
142 |
|
143 |
struct etp_worker; |
144 |
|
145 |
#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 |
static void eio_execute (struct etp_worker *self, eio_req *req); |
150 |
#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) |
151 |
|
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 |
/* 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 |
static unsigned int started, idle, wanted = 4; |
180 |
|
181 |
static void (*want_poll_cb) (void); |
182 |
static void (*done_poll_cb) (void); |
183 |
|
184 |
static unsigned int max_poll_time; /* reslock */ |
185 |
static unsigned int max_poll_reqs; /* reslock */ |
186 |
|
187 |
static volatile unsigned int nreqs; /* reqlock */ |
188 |
static volatile unsigned int nready; /* reqlock */ |
189 |
static volatile unsigned int npending; /* reqlock */ |
190 |
static volatile unsigned int max_idle = 4; |
191 |
|
192 |
static mutex_t wrklock = X_MUTEX_INIT; |
193 |
static mutex_t reslock = X_MUTEX_INIT; |
194 |
static mutex_t reqlock = X_MUTEX_INIT; |
195 |
static cond_t reqwait = X_COND_INIT; |
196 |
|
197 |
#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 |
typedef struct etp_worker |
207 |
{ |
208 |
/* locked by wrklock */ |
209 |
struct etp_worker *prev, *next; |
210 |
|
211 |
thread_t tid; |
212 |
|
213 |
/* 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 |
|
221 |
#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
222 |
#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
223 |
|
224 |
/* worker threads management */ |
225 |
|
226 |
static void etp_worker_clear (etp_worker *wrk) |
227 |
{ |
228 |
ETP_WORKER_CLEAR (wrk); |
229 |
} |
230 |
|
231 |
static void etp_worker_free (etp_worker *wrk) |
232 |
{ |
233 |
wrk->next->prev = wrk->prev; |
234 |
wrk->prev->next = wrk->next; |
235 |
|
236 |
free (wrk); |
237 |
} |
238 |
|
239 |
static unsigned int etp_nreqs (void) |
240 |
{ |
241 |
int retval; |
242 |
if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
243 |
retval = nreqs; |
244 |
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
245 |
return retval; |
246 |
} |
247 |
|
248 |
static unsigned int etp_nready (void) |
249 |
{ |
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 |
static unsigned int etp_npending (void) |
260 |
{ |
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 |
static unsigned int etp_nthreads (void) |
271 |
{ |
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 |
ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ |
288 |
int size; |
289 |
} etp_reqq; |
290 |
|
291 |
static etp_reqq req_queue; |
292 |
static etp_reqq res_queue; |
293 |
|
294 |
static int reqq_push (etp_reqq *q, ETP_REQ *req) |
295 |
{ |
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 |
static ETP_REQ *reqq_shift (etp_reqq *q) |
311 |
{ |
312 |
int pri; |
313 |
|
314 |
if (!q->size) |
315 |
return 0; |
316 |
|
317 |
--q->size; |
318 |
|
319 |
for (pri = ETP_NUM_PRI; pri--; ) |
320 |
{ |
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 |
static void etp_atfork_prepare (void) |
336 |
{ |
337 |
X_LOCK (wrklock); |
338 |
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 |
X_UNLOCK (wrklock); |
353 |
} |
354 |
|
355 |
static void etp_atfork_child (void) |
356 |
{ |
357 |
ETP_REQ *prv; |
358 |
|
359 |
while ((prv = reqq_shift (&req_queue))) |
360 |
ETP_DESTROY (prv); |
361 |
|
362 |
while ((prv = reqq_shift (&res_queue))) |
363 |
ETP_DESTROY (prv); |
364 |
|
365 |
while (wrk_first.next != &wrk_first) |
366 |
{ |
367 |
etp_worker *wrk = wrk_first.next; |
368 |
|
369 |
if (wrk->req) |
370 |
ETP_DESTROY (wrk->req); |
371 |
|
372 |
etp_worker_clear (wrk); |
373 |
etp_worker_free (wrk); |
374 |
} |
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 |
etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
393 |
{ |
394 |
static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
395 |
|
396 |
pthread_once (&doinit, etp_once_init); |
397 |
|
398 |
want_poll_cb = want_poll; |
399 |
done_poll_cb = done_poll; |
400 |
|
401 |
return 0; |
402 |
} |
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 |
if (expect_true (etp_nthreads () >= wanted)) |
432 |
return; |
433 |
|
434 |
/* todo: maybe use idle here, but might be less exact */ |
435 |
if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
436 |
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 |
if (expect_false (req->type == EIO_GROUP && req->size)) |
499 |
{ |
500 |
req->int1 = 1; /* mark request as delayed */ |
501 |
continue; |
502 |
} |
503 |
else |
504 |
{ |
505 |
int res = ETP_FINISH (req); |
506 |
if (expect_false (res)) |
507 |
return res; |
508 |
} |
509 |
|
510 |
if (expect_false (maxreqs && !--maxreqs)) |
511 |
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 |
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 |
|
556 |
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 |
|
567 |
etp_maybe_start_thread (); |
568 |
} |
569 |
} |
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 |
|
585 |
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 |
} |
591 |
|
592 |
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 |
|
607 |
/*****************************************************************************/ |
608 |
|
609 |
static void grp_try_feed (eio_req *grp) |
610 |
{ |
611 |
while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
612 |
{ |
613 |
grp->flags &= ~EIO_FLAG_GROUPADD; |
614 |
|
615 |
EIO_FEED (grp); |
616 |
|
617 |
/* stop if no progress has been made */ |
618 |
if (!(grp->flags & EIO_FLAG_GROUPADD)) |
619 |
{ |
620 |
grp->feed = 0; |
621 |
break; |
622 |
} |
623 |
} |
624 |
} |
625 |
|
626 |
static int grp_dec (eio_req *grp) |
627 |
{ |
628 |
--grp->size; |
629 |
|
630 |
/* call feeder, if applicable */ |
631 |
grp_try_feed (grp); |
632 |
|
633 |
/* finish, if done */ |
634 |
if (!grp->size && grp->int1) |
635 |
return eio_finish (grp); |
636 |
else |
637 |
return 0; |
638 |
} |
639 |
|
640 |
void eio_destroy (eio_req *req) |
641 |
{ |
642 |
if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
643 |
if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
644 |
|
645 |
EIO_DESTROY (req); |
646 |
} |
647 |
|
648 |
static int eio_finish (eio_req *req) |
649 |
{ |
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 |
etp_cancel (req); |
684 |
} |
685 |
|
686 |
void eio_submit (eio_req *req) |
687 |
{ |
688 |
etp_submit (req); |
689 |
} |
690 |
|
691 |
unsigned int eio_nreqs (void) |
692 |
{ |
693 |
return etp_nreqs (); |
694 |
} |
695 |
|
696 |
unsigned int eio_nready (void) |
697 |
{ |
698 |
return etp_nready (); |
699 |
} |
700 |
|
701 |
unsigned int eio_npending (void) |
702 |
{ |
703 |
return etp_npending (); |
704 |
} |
705 |
|
706 |
unsigned int eio_nthreads (void) |
707 |
{ |
708 |
return etp_nthreads (); |
709 |
} |
710 |
|
711 |
void eio_set_max_poll_time (double nseconds) |
712 |
{ |
713 |
etp_set_max_poll_time (nseconds); |
714 |
} |
715 |
|
716 |
void eio_set_max_poll_reqs (unsigned int maxreqs) |
717 |
{ |
718 |
etp_set_max_poll_reqs (maxreqs); |
719 |
} |
720 |
|
721 |
void eio_set_max_idle (unsigned int nthreads) |
722 |
{ |
723 |
etp_set_max_idle (nthreads); |
724 |
} |
725 |
|
726 |
void eio_set_min_parallel (unsigned int nthreads) |
727 |
{ |
728 |
etp_set_min_parallel (nthreads); |
729 |
} |
730 |
|
731 |
void eio_set_max_parallel (unsigned int nthreads) |
732 |
{ |
733 |
etp_set_max_parallel (nthreads); |
734 |
} |
735 |
|
736 |
int eio_poll (void) |
737 |
{ |
738 |
return etp_poll (); |
739 |
} |
740 |
|
741 |
/*****************************************************************************/ |
742 |
/* work around various missing functions */ |
743 |
|
744 |
#if !HAVE_PREADWRITE |
745 |
# undef pread |
746 |
# undef pwrite |
747 |
# define pread eio__pread |
748 |
# define pwrite eio__pwrite |
749 |
|
750 |
static ssize_t |
751 |
eio__pread (int fd, void *buf, size_t count, off_t offset) |
752 |
{ |
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 |
static ssize_t |
767 |
eio__pwrite (int fd, void *buf, size_t count, off_t offset) |
768 |
{ |
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 |
# undef utimes |
786 |
# undef futimes |
787 |
# define utimes(path,times) eio__utimes (path, times) |
788 |
# define futimes(fd,times) eio__futimes (fd, times) |
789 |
|
790 |
static int |
791 |
eio__utimes (const char *filename, const struct timeval times[2]) |
792 |
{ |
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 |
static int eio__futimes (int fd, const struct timeval tv[2]) |
807 |
{ |
808 |
errno = ENOSYS; |
809 |
return -1; |
810 |
} |
811 |
|
812 |
#endif |
813 |
|
814 |
#if !HAVE_FDATASYNC |
815 |
# undef fdatasync |
816 |
# define fdatasync(fd) fsync (fd) |
817 |
#endif |
818 |
|
819 |
/* 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 |
if (!res || errno != ENOSYS) |
839 |
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 |
#if !HAVE_READAHEAD |
848 |
# undef readahead |
849 |
# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
850 |
|
851 |
static ssize_t |
852 |
eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) |
853 |
{ |
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 |
pread (fd, eio_buf, len, offset); |
862 |
offset += len; |
863 |
todo -= len; |
864 |
} |
865 |
|
866 |
errno = 0; |
867 |
return count; |
868 |
} |
869 |
|
870 |
#endif |
871 |
|
872 |
/* sendfile always needs emulation */ |
873 |
static ssize_t |
874 |
eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
875 |
{ |
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 |
static void |
968 |
eio__scandir (eio_req *req, etp_worker *self) |
969 |
{ |
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 |
X_LOCK (wrklock); |
978 |
/* the corresponding closedir is in ETP_WORKER_CLEAR */ |
979 |
self->dirp = dirp = opendir (req->ptr1); |
980 |
req->flags |= EIO_FLAG_PTR2_FREE; |
981 |
req->ptr2 = names = malloc (memlen); |
982 |
X_UNLOCK (wrklock); |
983 |
|
984 |
if (dirp && names) |
985 |
for (;;) |
986 |
{ |
987 |
errno = 0; |
988 |
entp = readdir (dirp); |
989 |
|
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 |
X_LOCK (wrklock); |
1005 |
req->ptr2 = names = realloc (names, memlen); |
1006 |
X_UNLOCK (wrklock); |
1007 |
|
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 |
#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1024 |
# undef msync |
1025 |
# define msync(a,b,c) ((errno = ENOSYS), -1) |
1026 |
#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 |
/*****************************************************************************/ |
1054 |
|
1055 |
#define ALLOC(len) \ |
1056 |
if (!req->ptr2) \ |
1057 |
{ \ |
1058 |
X_LOCK (wrklock); \ |
1059 |
req->flags |= EIO_FLAG_PTR2_FREE; \ |
1060 |
X_UNLOCK (wrklock); \ |
1061 |
req->ptr2 = malloc (len); \ |
1062 |
if (!req->ptr2) \ |
1063 |
{ \ |
1064 |
errno = ENOMEM; \ |
1065 |
req->result = -1; \ |
1066 |
break; \ |
1067 |
} \ |
1068 |
} |
1069 |
|
1070 |
X_THREAD_PROC (etp_proc) |
1071 |
{ |
1072 |
ETP_REQ *req; |
1073 |
struct timespec ts; |
1074 |
etp_worker *self = (etp_worker *)thr_arg; |
1075 |
|
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 |
ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1093 |
if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
1094 |
{ |
1095 |
if (idle > max_idle) |
1096 |
{ |
1097 |
--idle; |
1098 |
X_UNLOCK (reqlock); |
1099 |
X_LOCK (wrklock); |
1100 |
--started; |
1101 |
X_UNLOCK (wrklock); |
1102 |
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 |
if (req->type < 0) |
1117 |
goto quit; |
1118 |
|
1119 |
if (!EIO_CANCELLED (req)) |
1120 |
ETP_EXECUTE (self, req); |
1121 |
|
1122 |
X_LOCK (reslock); |
1123 |
|
1124 |
++npending; |
1125 |
|
1126 |
if (!reqq_push (&res_queue, req) && want_poll_cb) |
1127 |
want_poll_cb (); |
1128 |
|
1129 |
self->req = 0; |
1130 |
etp_worker_clear (self); |
1131 |
|
1132 |
X_UNLOCK (reslock); |
1133 |
} |
1134 |
|
1135 |
quit: |
1136 |
X_LOCK (wrklock); |
1137 |
etp_worker_free (self); |
1138 |
X_UNLOCK (wrklock); |
1139 |
|
1140 |
return 0; |
1141 |
} |
1142 |
|
1143 |
/*****************************************************************************/ |
1144 |
|
1145 |
int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1146 |
{ |
1147 |
return etp_init (want_poll, done_poll); |
1148 |
} |
1149 |
|
1150 |
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 |
req->type = rtype; \ |
1163 |
req->pri = pri; \ |
1164 |
req->finish = cb; \ |
1165 |
req->data = data; \ |
1166 |
req->destroy = eio_api_destroy; |
1167 |
|
1168 |
#define SEND eio_submit (req); return req |
1169 |
|
1170 |
#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 |
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 |
case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; |
1220 |
|
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 |
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 |
case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
1230 |
|
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 |
break; |
1272 |
|
1273 |
case EIO_GROUP: |
1274 |
abort (); /* handled in eio_request */ |
1275 |
|
1276 |
case EIO_NOP: |
1277 |
req->result = 0; |
1278 |
break; |
1279 |
|
1280 |
case EIO_CUSTOM: |
1281 |
((void (*)(eio_req *))req->feed) (req); |
1282 |
break; |
1283 |
|
1284 |
default: |
1285 |
req->result = -1; |
1286 |
break; |
1287 |
} |
1288 |
|
1289 |
req->errorno = errno; |
1290 |
} |
1291 |
|
1292 |
#ifndef EIO_NO_WRAPPERS |
1293 |
|
1294 |
eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1295 |
{ |
1296 |
REQ (EIO_NOP); SEND; |
1297 |
} |
1298 |
|
1299 |
eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data) |
1300 |
{ |
1301 |
REQ (EIO_BUSY); req->nv1 = delay; SEND; |
1302 |
} |
1303 |
|
1304 |
eio_req *eio_sync (int pri, eio_cb cb, void *data) |
1305 |
{ |
1306 |
REQ (EIO_SYNC); SEND; |
1307 |
} |
1308 |
|
1309 |
eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1310 |
{ |
1311 |
REQ (EIO_FSYNC); req->int1 = fd; SEND; |
1312 |
} |
1313 |
|
1314 |
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 |
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 |
eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1330 |
{ |
1331 |
REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1332 |
} |
1333 |
|
1334 |
eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) |
1335 |
{ |
1336 |
REQ (EIO_CLOSE); req->int1 = fd; SEND; |
1337 |
} |
1338 |
|
1339 |
eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
1340 |
{ |
1341 |
REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
1342 |
} |
1343 |
|
1344 |
eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
1345 |
{ |
1346 |
REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
1347 |
} |
1348 |
|
1349 |
eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
1350 |
{ |
1351 |
REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
1352 |
} |
1353 |
|
1354 |
eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
1355 |
{ |
1356 |
REQ (EIO_FSTAT); req->int1 = fd; SEND; |
1357 |
} |
1358 |
|
1359 |
eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data) |
1360 |
{ |
1361 |
REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND; |
1362 |
} |
1363 |
|
1364 |
eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data) |
1365 |
{ |
1366 |
REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND; |
1367 |
} |
1368 |
|
1369 |
eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) |
1370 |
{ |
1371 |
REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; |
1372 |
} |
1373 |
|
1374 |
eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) |
1375 |
{ |
1376 |
REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; |
1377 |
} |
1378 |
|
1379 |
eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) |
1380 |
{ |
1381 |
REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND; |
1382 |
} |
1383 |
|
1384 |
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 |
{ |
1386 |
REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND; |
1387 |
} |
1388 |
|
1389 |
eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data) |
1390 |
{ |
1391 |
REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND; |
1392 |
} |
1393 |
|
1394 |
eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data) |
1395 |
{ |
1396 |
REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND; |
1397 |
} |
1398 |
|
1399 |
eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) |
1400 |
{ |
1401 |
REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; |
1402 |
} |
1403 |
|
1404 |
eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) |
1405 |
{ |
1406 |
REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; |
1407 |
} |
1408 |
|
1409 |
eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) |
1410 |
{ |
1411 |
REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND; |
1412 |
} |
1413 |
|
1414 |
eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data) |
1415 |
{ |
1416 |
REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND; |
1417 |
} |
1418 |
|
1419 |
static eio_req * |
1420 |
eio__1path (int type, const char *path, int pri, eio_cb cb, void *data) |
1421 |
{ |
1422 |
REQ (type); PATH; SEND; |
1423 |
} |
1424 |
|
1425 |
eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
1426 |
{ |
1427 |
return eio__1path (EIO_READLINK, path, pri, cb, data); |
1428 |
} |
1429 |
|
1430 |
eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
1431 |
{ |
1432 |
return eio__1path (EIO_STAT, path, pri, cb, data); |
1433 |
} |
1434 |
|
1435 |
eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data) |
1436 |
{ |
1437 |
return eio__1path (EIO_LSTAT, path, pri, cb, data); |
1438 |
} |
1439 |
|
1440 |
eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data) |
1441 |
{ |
1442 |
return eio__1path (EIO_UNLINK, path, pri, cb, data); |
1443 |
} |
1444 |
|
1445 |
eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) |
1446 |
{ |
1447 |
return eio__1path (EIO_RMDIR, path, pri, cb, data); |
1448 |
} |
1449 |
|
1450 |
eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) |
1451 |
{ |
1452 |
return eio__1path (EIO_READDIR, path, pri, cb, data); |
1453 |
} |
1454 |
|
1455 |
eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1456 |
{ |
1457 |
REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
1458 |
} |
1459 |
|
1460 |
static eio_req * |
1461 |
eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1462 |
{ |
1463 |
REQ (type); PATH; |
1464 |
|
1465 |
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 |
|
1473 |
SEND; |
1474 |
} |
1475 |
|
1476 |
eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1477 |
{ |
1478 |
return eio__2path (EIO_LINK, path, new_path, pri, cb, data); |
1479 |
} |
1480 |
|
1481 |
eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1482 |
{ |
1483 |
return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data); |
1484 |
} |
1485 |
|
1486 |
eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1487 |
{ |
1488 |
return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1489 |
} |
1490 |
|
1491 |
eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
1492 |
{ |
1493 |
REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
1494 |
} |
1495 |
|
1496 |
#endif |
1497 |
|
1498 |
eio_req *eio_grp (eio_cb cb, void *data) |
1499 |
{ |
1500 |
const int pri = EIO_PRI_MAX; |
1501 |
|
1502 |
REQ (EIO_GROUP); SEND; |
1503 |
} |
1504 |
|
1505 |
#undef REQ |
1506 |
#undef PATH |
1507 |
#undef SEND |
1508 |
|
1509 |
/*****************************************************************************/ |
1510 |
/* grp functions */ |
1511 |
|
1512 |
void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
1513 |
{ |
1514 |
grp->int2 = limit; |
1515 |
grp->feed = feed; |
1516 |
|
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 |
} |
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 |
grp->flags |= EIO_FLAG_GROUPADD; |
1532 |
|
1533 |
++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 |
/*****************************************************************************/ |
1546 |
/* misc garbage */ |
1547 |
|
1548 |
ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
1549 |
{ |
1550 |
etp_worker wrk; |
1551 |
|
1552 |
wrk.dbuf = 0; |
1553 |
|
1554 |
eio__sendfile (ofd, ifd, offset, count, &wrk); |
1555 |
|
1556 |
if (wrk.dbuf) |
1557 |
free (wrk.dbuf); |
1558 |
} |
1559 |
|