workqueue: move init worker private code to the caller
[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 *);
80ea26a8 27typedef int (workqueue_init_worker_fn)(struct submit_worker *);
5bb79f69
JA
28
29struct workqueue_ops {
30 workqueue_work_fn *fn;
31 workqueue_pre_sleep_flush_fn *pre_sleep_flush_fn;
32 workqueue_pre_sleep_fn *pre_sleep_fn;
ee2b6d6e
JA
33 workqueue_alloc_worker_fn *alloc_worker_fn;
34 workqueue_free_worker_fn *free_worker_fn;
80ea26a8 35 workqueue_init_worker_fn *init_worker_fn;
5bb79f69 36};
a9da8ab2
JA
37
38struct workqueue {
39 unsigned int max_workers;
40
41 struct thread_data *td;
5bb79f69 42 struct workqueue_ops ops;
a9da8ab2
JA
43
44 uint64_t work_seq;
45 struct submit_worker *workers;
46 unsigned int next_free_worker;
47
48 pthread_cond_t flush_cond;
49 pthread_mutex_t flush_lock;
2a274336 50 pthread_mutex_t stat_lock;
a9da8ab2
JA
51 volatile int wake_idle;
52};
53
5bb79f69 54int workqueue_init(struct thread_data *td, struct workqueue *wq, struct workqueue_ops *ops, unsigned int max_workers);
a9da8ab2
JA
55void workqueue_exit(struct workqueue *wq);
56
88271841 57bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work);
a9da8ab2
JA
58void workqueue_flush(struct workqueue *wq);
59
ee2b6d6e 60static inline bool workqueue_pre_sleep_check(struct submit_worker *sw)
5bb79f69 61{
ee2b6d6e
JA
62 struct workqueue *wq = sw->wq;
63
5bb79f69
JA
64 if (!wq->ops.pre_sleep_flush_fn)
65 return false;
66
ee2b6d6e 67 return wq->ops.pre_sleep_flush_fn(sw);
5bb79f69
JA
68}
69
ee2b6d6e 70static inline void workqueue_pre_sleep(struct submit_worker *sw)
5bb79f69 71{
ee2b6d6e
JA
72 struct workqueue *wq = sw->wq;
73
5bb79f69 74 if (wq->ops.pre_sleep_fn)
ee2b6d6e 75 wq->ops.pre_sleep_fn(sw);
5bb79f69
JA
76}
77
80ea26a8
JA
78static inline int workqueue_init_worker(struct submit_worker *sw)
79{
80 struct workqueue *wq = sw->wq;
81
82 if (!wq->ops.init_worker_fn)
83 return 0;
84
85 return wq->ops.init_worker_fn(sw);
86}
87
a9da8ab2 88#endif