]>
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 | ||
224ce89b | 8 | |
7c673cae FG |
9 | int aio_queue_t::submit(aio_t &aio, int *retries) |
10 | { | |
11 | // 2^16 * 125us = ~8 seconds, so max sleep is ~16 seconds | |
12 | int attempts = 16; | |
13 | int delay = 125; | |
14 | iocb *piocb = &aio.iocb; | |
224ce89b | 15 | int r; |
7c673cae | 16 | while (true) { |
224ce89b | 17 | r = io_submit(ctx, 1, &piocb); |
7c673cae FG |
18 | if (r < 0) { |
19 | if (r == -EAGAIN && attempts-- > 0) { | |
20 | usleep(delay); | |
21 | delay *= 2; | |
22 | (*retries)++; | |
23 | continue; | |
24 | } | |
7c673cae FG |
25 | } |
26 | assert(r == 1); | |
27 | break; | |
28 | } | |
224ce89b WB |
29 | return r; |
30 | } | |
31 | ||
32 | int aio_queue_t::submit_batch(aio_iter begin, aio_iter end, | |
33 | uint16_t aios_size, void *priv, | |
34 | int *retries) | |
35 | { | |
36 | // 2^16 * 125us = ~8 seconds, so max sleep is ~16 seconds | |
37 | int attempts = 16; | |
38 | int delay = 125; | |
39 | ||
40 | aio_iter cur = begin; | |
41 | struct iocb *piocb[aios_size]; | |
42 | int r, pos = 0; | |
43 | while (cur != end) { | |
44 | cur->priv = priv; | |
45 | *(piocb+pos) = &cur->iocb; | |
46 | ++pos; | |
47 | ++cur; | |
48 | } | |
49 | while (true) { | |
50 | r = io_submit(ctx, pos, piocb); | |
51 | if (r < 0) { | |
52 | if (r == -EAGAIN && attempts-- > 0) { | |
53 | usleep(delay); | |
54 | delay *= 2; | |
55 | (*retries)++; | |
56 | continue; | |
57 | } | |
58 | } | |
59 | break; | |
60 | } | |
61 | return r; | |
7c673cae FG |
62 | } |
63 | ||
64 | int aio_queue_t::get_next_completed(int timeout_ms, aio_t **paio, int max) | |
65 | { | |
66 | io_event event[max]; | |
67 | struct timespec t = { | |
68 | timeout_ms / 1000, | |
69 | (timeout_ms % 1000) * 1000 * 1000 | |
70 | }; | |
71 | ||
72 | int r = 0; | |
73 | do { | |
74 | r = io_getevents(ctx, 1, max, event, &t); | |
75 | } while (r == -EINTR); | |
76 | ||
77 | for (int i=0; i<r; ++i) { | |
78 | paio[i] = (aio_t *)event[i].obj; | |
79 | paio[i]->rval = event[i].res; | |
80 | } | |
81 | return r; | |
82 | } | |
83 | ||
84 | #endif |