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