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