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