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 | { |
| 15 | if (q->nr) |
| 16 | return q->io_us[--q->nr]; |
| 17 | |
| 18 | return NULL; |
| 19 | } |
| 20 | |
| 21 | static inline void io_u_qpush(struct io_u_queue *q, struct io_u *io_u) |
| 22 | { |
| 23 | q->io_us[q->nr++] = io_u; |
| 24 | } |
| 25 | |
| 26 | static inline int io_u_qempty(struct io_u_queue *q) |
| 27 | { |
| 28 | return !q->nr; |
| 29 | } |
| 30 | |
| 31 | #define io_u_qiter(q, io_u, i) \ |
| 32 | for (i = 0, io_u = (q)->io_us[0]; i < (q)->nr; i++, io_u = (q)->io_us[i]) |
| 33 | |
| 34 | int io_u_qinit(struct io_u_queue *q, unsigned int nr); |
| 35 | void io_u_qexit(struct io_u_queue *q); |
| 36 | |
| 37 | struct io_u_ring { |
| 38 | unsigned int head; |
| 39 | unsigned int tail; |
| 40 | unsigned int max; |
| 41 | struct io_u **ring; |
| 42 | }; |
| 43 | |
| 44 | int io_u_rinit(struct io_u_ring *ring, unsigned int nr); |
| 45 | void io_u_rexit(struct io_u_ring *ring); |
| 46 | |
| 47 | static inline void io_u_rpush(struct io_u_ring *r, struct io_u *io_u) |
| 48 | { |
| 49 | if (r->head + 1 != r->tail) { |
| 50 | r->ring[r->head] = io_u; |
| 51 | r->head = (r->head + 1) & (r->max - 1); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | assert(0); |
| 56 | } |
| 57 | |
| 58 | static inline struct io_u *io_u_rpop(struct io_u_ring *r) |
| 59 | { |
| 60 | if (r->head != r->tail) { |
| 61 | struct io_u *io_u = r->ring[r->tail]; |
| 62 | |
| 63 | r->tail = (r->tail + 1) & (r->max - 1); |
| 64 | return io_u; |
| 65 | } |
| 66 | |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | static inline int io_u_rempty(struct io_u_ring *ring) |
| 71 | { |
| 72 | return ring->head == ring->tail; |
| 73 | } |
| 74 | |
| 75 | #endif |