blob: bda40d5ea263920055f6d75c62bdca8e4559034c [file] [log] [blame]
Jens Axboe2ae0b202013-05-28 14:16:55 +02001#ifndef FIO_IO_U_QUEUE
2#define FIO_IO_U_QUEUE
3
4#include <assert.h>
5
6struct io_u;
7
8struct io_u_queue {
9 struct io_u **io_us;
10 unsigned int nr;
11};
12
13static inline struct io_u *io_u_qpop(struct io_u_queue *q)
14{
Jens Axboee69fdf72014-07-23 16:11:43 +020015 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 Axboe2ae0b202013-05-28 14:16:55 +020022
23 return NULL;
24}
25
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
Jens Axboe5a48d302014-09-30 13:29:57 -060031static inline int io_u_qempty(const struct io_u_queue *q)
Jens Axboe2ae0b202013-05-28 14:16:55 +020032{
33 return !q->nr;
34}
35
36#define io_u_qiter(q, io_u, i) \
Sitsofe Wheeler6a7a9212014-02-13 07:06:40 +000037 for (i = 0; i < (q)->nr && (io_u = (q)->io_us[i]); i++)
Jens Axboe2ae0b202013-05-28 14:16:55 +020038
39int io_u_qinit(struct io_u_queue *q, unsigned int nr);
40void io_u_qexit(struct io_u_queue *q);
41
42struct io_u_ring {
43 unsigned int head;
44 unsigned int tail;
45 unsigned int max;
46 struct io_u **ring;
47};
48
49int io_u_rinit(struct io_u_ring *ring, unsigned int nr);
50void io_u_rexit(struct io_u_ring *ring);
51
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
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
75static inline int io_u_rempty(struct io_u_ring *ring)
76{
77 return ring->head == ring->tail;
78}
79
80#endif