Jens Axboe | 2ae0b20 | 2013-05-28 14:16:55 +0200 | [diff] [blame] | 1 | #ifndef FIO_IO_U_QUEUE |
| 2 | #define FIO_IO_U_QUEUE |
| 3 | |
| 4 | #include <assert.h> |
| 5 | |
| 6 | struct io_u; |
| 7 | |
| 8 | struct io_u_queue { |
| 9 | struct io_u **io_us; |
| 10 | unsigned int nr; |
| 11 | }; |
| 12 | |
| 13 | static inline struct io_u *io_u_qpop(struct io_u_queue *q) |
| 14 | { |
Jens Axboe | e69fdf7 | 2014-07-23 16:11:43 +0200 | [diff] [blame] | 15 | if (q->nr) { |
| 16 | const unsigned int next = --q->nr; |
| 17 | struct io_u *io_u = q->io_us[next]; |
| 18 | |
| 19 | q->io_us[next] = NULL; |
| 20 | return io_u; |
| 21 | } |
Jens Axboe | 2ae0b20 | 2013-05-28 14:16:55 +0200 | [diff] [blame] | 22 | |
| 23 | return NULL; |
| 24 | } |
| 25 | |
| 26 | static inline void io_u_qpush(struct io_u_queue *q, struct io_u *io_u) |
| 27 | { |
| 28 | q->io_us[q->nr++] = io_u; |
| 29 | } |
| 30 | |
Jens Axboe | 5a48d30 | 2014-09-30 13:29:57 -0600 | [diff] [blame] | 31 | static inline int io_u_qempty(const struct io_u_queue *q) |
Jens Axboe | 2ae0b20 | 2013-05-28 14:16:55 +0200 | [diff] [blame] | 32 | { |
| 33 | return !q->nr; |
| 34 | } |
| 35 | |
| 36 | #define io_u_qiter(q, io_u, i) \ |
Sitsofe Wheeler | 6a7a921 | 2014-02-13 07:06:40 +0000 | [diff] [blame] | 37 | for (i = 0; i < (q)->nr && (io_u = (q)->io_us[i]); i++) |
Jens Axboe | 2ae0b20 | 2013-05-28 14:16:55 +0200 | [diff] [blame] | 38 | |
| 39 | int io_u_qinit(struct io_u_queue *q, unsigned int nr); |
| 40 | void io_u_qexit(struct io_u_queue *q); |
| 41 | |
| 42 | struct io_u_ring { |
| 43 | unsigned int head; |
| 44 | unsigned int tail; |
| 45 | unsigned int max; |
| 46 | struct io_u **ring; |
| 47 | }; |
| 48 | |
| 49 | int io_u_rinit(struct io_u_ring *ring, unsigned int nr); |
| 50 | void io_u_rexit(struct io_u_ring *ring); |
| 51 | |
| 52 | static inline void io_u_rpush(struct io_u_ring *r, struct io_u *io_u) |
| 53 | { |
| 54 | if (r->head + 1 != r->tail) { |
| 55 | r->ring[r->head] = io_u; |
| 56 | r->head = (r->head + 1) & (r->max - 1); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | assert(0); |
| 61 | } |
| 62 | |
| 63 | static inline struct io_u *io_u_rpop(struct io_u_ring *r) |
| 64 | { |
| 65 | if (r->head != r->tail) { |
| 66 | struct io_u *io_u = r->ring[r->tail]; |
| 67 | |
| 68 | r->tail = (r->tail + 1) & (r->max - 1); |
| 69 | return io_u; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | static inline int io_u_rempty(struct io_u_ring *ring) |
| 76 | { |
| 77 | return ring->head == ring->tail; |
| 78 | } |
| 79 | |
| 80 | #endif |