]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "aio.h" | |
5 | ||
6 | #if defined(HAVE_LIBAIO) | |
7 | ||
8 | int aio_queue_t::submit(aio_t &aio, int *retries) | |
9 | { | |
10 | // 2^16 * 125us = ~8 seconds, so max sleep is ~16 seconds | |
11 | int attempts = 16; | |
12 | int delay = 125; | |
13 | iocb *piocb = &aio.iocb; | |
14 | while (true) { | |
15 | int r = io_submit(ctx, 1, &piocb); | |
16 | if (r < 0) { | |
17 | if (r == -EAGAIN && attempts-- > 0) { | |
18 | usleep(delay); | |
19 | delay *= 2; | |
20 | (*retries)++; | |
21 | continue; | |
22 | } | |
23 | return r; | |
24 | } | |
25 | assert(r == 1); | |
26 | break; | |
27 | } | |
28 | return 0; | |
29 | } | |
30 | ||
31 | int aio_queue_t::get_next_completed(int timeout_ms, aio_t **paio, int max) | |
32 | { | |
33 | io_event event[max]; | |
34 | struct timespec t = { | |
35 | timeout_ms / 1000, | |
36 | (timeout_ms % 1000) * 1000 * 1000 | |
37 | }; | |
38 | ||
39 | int r = 0; | |
40 | do { | |
41 | r = io_getevents(ctx, 1, max, event, &t); | |
42 | } while (r == -EINTR); | |
43 | ||
44 | for (int i=0; i<r; ++i) { | |
45 | paio[i] = (aio_t *)event[i].obj; | |
46 | paio[i]->rval = event[i].res; | |
47 | } | |
48 | return r; | |
49 | } | |
50 | ||
51 | #endif |