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 io_u_qpop(struct io_u_queue * q)13static inline struct io_u *io_u_qpop(struct io_u_queue *q) 14 { 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 } 22 23 return NULL; 24 } 25 io_u_qpush(struct io_u_queue * q,struct io_u * io_u)26static 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 io_u_qempty(const struct io_u_queue * q)31static inline int io_u_qempty(const struct io_u_queue *q) 32 { 33 return !q->nr; 34 } 35 36 #define io_u_qiter(q, io_u, i) \ 37 for (i = 0; i < (q)->nr && (io_u = (q)->io_us[i]); i++) 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 io_u_rpush(struct io_u_ring * r,struct io_u * io_u)52static 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 io_u_rpop(struct io_u_ring * r)63static 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 io_u_rempty(struct io_u_ring * ring)75static inline int io_u_rempty(struct io_u_ring *ring) 76 { 77 return ring->head == ring->tail; 78 } 79 80 #endif 81