Fix Windows CPU count
[fio.git] / io_u_queue.h
CommitLineData
2ae0b204
JA
1#ifndef FIO_IO_U_QUEUE
2#define FIO_IO_U_QUEUE
3
4#include <assert.h>
3d2d14bc
SW
5#include <stddef.h>
6
34851ad5 7#include "lib/types.h"
2ae0b204
JA
8
9struct io_u;
10
11struct io_u_queue {
12 struct io_u **io_us;
13 unsigned int nr;
6fb1bda0 14 unsigned int max;
2ae0b204
JA
15};
16
17static inline struct io_u *io_u_qpop(struct io_u_queue *q)
18{
f8b0bd10
JA
19 if (q->nr) {
20 const unsigned int next = --q->nr;
21 struct io_u *io_u = q->io_us[next];
22
23 q->io_us[next] = NULL;
24 return io_u;
25 }
2ae0b204
JA
26
27 return NULL;
28}
29
30static inline void io_u_qpush(struct io_u_queue *q, struct io_u *io_u)
31{
6fb1bda0
JA
32 if (q->nr < q->max) {
33 q->io_us[q->nr++] = io_u;
34 return;
35 }
36
37 assert(0);
2ae0b204
JA
38}
39
effd6ff0 40static inline int io_u_qempty(const struct io_u_queue *q)
2ae0b204
JA
41{
42 return !q->nr;
43}
44
45#define io_u_qiter(q, io_u, i) \
6a7a9215 46 for (i = 0; i < (q)->nr && (io_u = (q)->io_us[i]); i++)
2ae0b204 47
3114b675
VF
48bool io_u_qinit(struct io_u_queue *q, unsigned int nr, bool shared);
49void io_u_qexit(struct io_u_queue *q, bool shared);
2ae0b204
JA
50
51struct io_u_ring {
52 unsigned int head;
53 unsigned int tail;
54 unsigned int max;
55 struct io_u **ring;
56};
57
34851ad5 58bool io_u_rinit(struct io_u_ring *ring, unsigned int nr);
2ae0b204
JA
59void io_u_rexit(struct io_u_ring *ring);
60
61static inline void io_u_rpush(struct io_u_ring *r, struct io_u *io_u)
62{
63 if (r->head + 1 != r->tail) {
64 r->ring[r->head] = io_u;
65 r->head = (r->head + 1) & (r->max - 1);
66 return;
67 }
68
69 assert(0);
70}
71
72static inline struct io_u *io_u_rpop(struct io_u_ring *r)
73{
74 if (r->head != r->tail) {
75 struct io_u *io_u = r->ring[r->tail];
76
77 r->tail = (r->tail + 1) & (r->max - 1);
78 return io_u;
79 }
80
81 return NULL;
82}
83
84static inline int io_u_rempty(struct io_u_ring *ring)
85{
86 return ring->head == ring->tail;
87}
88
89#endif