workqueue: remove knowledge of td queue state
[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 typedef void (workqueue_work_fn)(struct thread_data *, struct workqueue_work *);
11 typedef bool (workqueue_pre_sleep_flush_fn)(struct thread_data *);
12 typedef void (workqueue_pre_sleep_fn)(struct thread_data *);
13
14 struct workqueue_ops {
15         workqueue_work_fn *fn;
16         workqueue_pre_sleep_flush_fn *pre_sleep_flush_fn;
17         workqueue_pre_sleep_fn *pre_sleep_fn;
18 };
19
20 struct workqueue {
21         unsigned int max_workers;
22
23         struct thread_data *td;
24         struct workqueue_ops ops;
25
26         uint64_t work_seq;
27         struct submit_worker *workers;
28         unsigned int next_free_worker;
29
30         pthread_cond_t flush_cond;
31         pthread_mutex_t flush_lock;
32         pthread_mutex_t stat_lock;
33         volatile int wake_idle;
34 };
35
36 int workqueue_init(struct thread_data *td, struct workqueue *wq, struct workqueue_ops *ops, unsigned int max_workers);
37 void workqueue_exit(struct workqueue *wq);
38
39 bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work);
40 void workqueue_flush(struct workqueue *wq);
41
42 static inline bool workqueue_pre_sleep_check(struct workqueue *wq)
43 {
44         if (!wq->ops.pre_sleep_flush_fn)
45                 return false;
46
47         return wq->ops.pre_sleep_flush_fn(wq->td);
48 }
49
50 static inline void workqueue_pre_sleep(struct workqueue *wq)
51 {
52         if (wq->ops.pre_sleep_fn)
53                 wq->ops.pre_sleep_fn(wq->td);
54 }
55
56 #endif