Makefile: include linux-blkzoned.c for Android, if set
[fio.git] / workqueue.h
CommitLineData
a9da8ab2
JA
1#ifndef FIO_RATE_H
2#define FIO_RATE_H
3
3d2d14bc
SW
4#include <inttypes.h>
5#include <pthread.h>
6
a9da8ab2 7#include "flist.h"
3d2d14bc
SW
8#include "lib/types.h"
9
10struct sk_out;
11struct thread_data;
a9da8ab2 12
88271841
JA
13struct workqueue_work {
14 struct flist_head list;
15};
16
ee2b6d6e
JA
17struct submit_worker {
18 pthread_t thread;
19 pthread_mutex_t lock;
20 pthread_cond_t cond;
21 struct flist_head work_list;
22 unsigned int flags;
23 unsigned int index;
24 uint64_t seq;
25 struct workqueue *wq;
b86ad8f1 26 void *priv;
24660963 27 struct sk_out *sk_out;
ee2b6d6e
JA
28};
29
155f2f02 30typedef int (workqueue_work_fn)(struct submit_worker *, struct workqueue_work *);
ee2b6d6e
JA
31typedef bool (workqueue_pre_sleep_flush_fn)(struct submit_worker *);
32typedef void (workqueue_pre_sleep_fn)(struct submit_worker *);
33typedef int (workqueue_alloc_worker_fn)(struct submit_worker *);
34typedef void (workqueue_free_worker_fn)(struct submit_worker *);
80ea26a8 35typedef int (workqueue_init_worker_fn)(struct submit_worker *);
c35c582d 36typedef void (workqueue_exit_worker_fn)(struct submit_worker *, unsigned int *);
17ecadc3 37typedef void (workqueue_update_acct_fn)(struct submit_worker *);
5bb79f69
JA
38
39struct workqueue_ops {
40 workqueue_work_fn *fn;
41 workqueue_pre_sleep_flush_fn *pre_sleep_flush_fn;
42 workqueue_pre_sleep_fn *pre_sleep_fn;
df8472e1 43
17ecadc3
JA
44 workqueue_update_acct_fn *update_acct_fn;
45
ee2b6d6e
JA
46 workqueue_alloc_worker_fn *alloc_worker_fn;
47 workqueue_free_worker_fn *free_worker_fn;
df8472e1 48
80ea26a8 49 workqueue_init_worker_fn *init_worker_fn;
df8472e1 50 workqueue_exit_worker_fn *exit_worker_fn;
f6496ba7
JA
51
52 unsigned int nice;
5bb79f69 53};
a9da8ab2
JA
54
55struct workqueue {
56 unsigned int max_workers;
57
58 struct thread_data *td;
5bb79f69 59 struct workqueue_ops ops;
a9da8ab2
JA
60
61 uint64_t work_seq;
62 struct submit_worker *workers;
63 unsigned int next_free_worker;
64
65 pthread_cond_t flush_cond;
66 pthread_mutex_t flush_lock;
2a274336 67 pthread_mutex_t stat_lock;
a9da8ab2
JA
68 volatile int wake_idle;
69};
70
24660963 71int workqueue_init(struct thread_data *td, struct workqueue *wq, struct workqueue_ops *ops, unsigned int max_workers, struct sk_out *sk_out);
a9da8ab2
JA
72void workqueue_exit(struct workqueue *wq);
73
26de50cf 74void workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work);
a9da8ab2
JA
75void workqueue_flush(struct workqueue *wq);
76
ee2b6d6e 77static inline bool workqueue_pre_sleep_check(struct submit_worker *sw)
5bb79f69 78{
ee2b6d6e
JA
79 struct workqueue *wq = sw->wq;
80
5bb79f69
JA
81 if (!wq->ops.pre_sleep_flush_fn)
82 return false;
83
ee2b6d6e 84 return wq->ops.pre_sleep_flush_fn(sw);
5bb79f69
JA
85}
86
ee2b6d6e 87static inline void workqueue_pre_sleep(struct submit_worker *sw)
5bb79f69 88{
ee2b6d6e
JA
89 struct workqueue *wq = sw->wq;
90
5bb79f69 91 if (wq->ops.pre_sleep_fn)
ee2b6d6e 92 wq->ops.pre_sleep_fn(sw);
5bb79f69
JA
93}
94
80ea26a8
JA
95static inline int workqueue_init_worker(struct submit_worker *sw)
96{
97 struct workqueue *wq = sw->wq;
98
99 if (!wq->ops.init_worker_fn)
100 return 0;
101
102 return wq->ops.init_worker_fn(sw);
103}
104
c35c582d
JA
105static inline void workqueue_exit_worker(struct submit_worker *sw,
106 unsigned int *sum_cnt)
df8472e1
JA
107{
108 struct workqueue *wq = sw->wq;
c35c582d 109 unsigned int tmp = 1;
df8472e1
JA
110
111 if (!wq->ops.exit_worker_fn)
112 return;
113
c35c582d
JA
114 if (!sum_cnt)
115 sum_cnt = &tmp;
116
117 wq->ops.exit_worker_fn(sw, sum_cnt);
df8472e1 118}
a9da8ab2 119#endif