workqueue: move 'td' private data to the workqueue user
[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
28 struct workqueue_ops {
29         workqueue_work_fn *fn;
30         workqueue_pre_sleep_flush_fn *pre_sleep_flush_fn;
31         workqueue_pre_sleep_fn *pre_sleep_fn;
32         workqueue_alloc_worker_fn *alloc_worker_fn;
33         workqueue_free_worker_fn *free_worker_fn;
34 };
35
36 struct workqueue {
37         unsigned int max_workers;
38
39         struct thread_data *td;
40         struct workqueue_ops ops;
41
42         uint64_t work_seq;
43         struct submit_worker *workers;
44         unsigned int next_free_worker;
45
46         pthread_cond_t flush_cond;
47         pthread_mutex_t flush_lock;
48         pthread_mutex_t stat_lock;
49         volatile int wake_idle;
50 };
51
52 int workqueue_init(struct thread_data *td, struct workqueue *wq, struct workqueue_ops *ops, unsigned int max_workers);
53 void workqueue_exit(struct workqueue *wq);
54
55 bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work);
56 void workqueue_flush(struct workqueue *wq);
57
58 static inline bool workqueue_pre_sleep_check(struct submit_worker *sw)
59 {
60         struct workqueue *wq = sw->wq;
61
62         if (!wq->ops.pre_sleep_flush_fn)
63                 return false;
64
65         return wq->ops.pre_sleep_flush_fn(sw);
66 }
67
68 static inline void workqueue_pre_sleep(struct submit_worker *sw)
69 {
70         struct workqueue *wq = sw->wq;
71
72         if (wq->ops.pre_sleep_fn)
73                 wq->ops.pre_sleep_fn(sw);
74 }
75
76 #endif