workqueue: move 'td' private data to the workqueue user
[fio.git] / workqueue.h
CommitLineData
a9da8ab2
JA
1#ifndef FIO_RATE_H
2#define FIO_RATE_H
3
4#include "flist.h"
5
88271841
JA
6struct workqueue_work {
7 struct flist_head list;
8};
9
ee2b6d6e
JA
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 *private;
20};
21
22typedef void (workqueue_work_fn)(struct submit_worker *, struct workqueue_work *);
23typedef bool (workqueue_pre_sleep_flush_fn)(struct submit_worker *);
24typedef void (workqueue_pre_sleep_fn)(struct submit_worker *);
25typedef int (workqueue_alloc_worker_fn)(struct submit_worker *);
26typedef void (workqueue_free_worker_fn)(struct submit_worker *);
5bb79f69
JA
27
28struct 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;
ee2b6d6e
JA
32 workqueue_alloc_worker_fn *alloc_worker_fn;
33 workqueue_free_worker_fn *free_worker_fn;
5bb79f69 34};
a9da8ab2
JA
35
36struct workqueue {
37 unsigned int max_workers;
38
39 struct thread_data *td;
5bb79f69 40 struct workqueue_ops ops;
a9da8ab2
JA
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;
2a274336 48 pthread_mutex_t stat_lock;
a9da8ab2
JA
49 volatile int wake_idle;
50};
51
5bb79f69 52int workqueue_init(struct thread_data *td, struct workqueue *wq, struct workqueue_ops *ops, unsigned int max_workers);
a9da8ab2
JA
53void workqueue_exit(struct workqueue *wq);
54
88271841 55bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work);
a9da8ab2
JA
56void workqueue_flush(struct workqueue *wq);
57
ee2b6d6e 58static inline bool workqueue_pre_sleep_check(struct submit_worker *sw)
5bb79f69 59{
ee2b6d6e
JA
60 struct workqueue *wq = sw->wq;
61
5bb79f69
JA
62 if (!wq->ops.pre_sleep_flush_fn)
63 return false;
64
ee2b6d6e 65 return wq->ops.pre_sleep_flush_fn(sw);
5bb79f69
JA
66}
67
ee2b6d6e 68static inline void workqueue_pre_sleep(struct submit_worker *sw)
5bb79f69 69{
ee2b6d6e
JA
70 struct workqueue *wq = sw->wq;
71
5bb79f69 72 if (wq->ops.pre_sleep_fn)
ee2b6d6e 73 wq->ops.pre_sleep_fn(sw);
5bb79f69
JA
74}
75
a9da8ab2 76#endif