-
Notifications
You must be signed in to change notification settings - Fork 12
/
workqueue.c
242 lines (211 loc) · 6.31 KB
/
workqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* glue code for library version of Linux kernel
* Copyright (c) 2015 INRIA, Hajime Tazaki
*
* Author: Mathieu Lacage <mathieu.lacage@gmail.com>
* Hajime Tazaki <tazaki@sfc.wide.ad.jp>
*/
#include <linux/workqueue.h>
#include <linux/slab.h>
#include "sim.h"
#include "sim-assert.h"
/* copy from kernel/workqueue.c */
typedef unsigned long mayday_mask_t;
struct workqueue_struct {
unsigned int flags; /* W: WQ_* flags */
union {
struct cpu_workqueue_struct __percpu *pcpu;
struct cpu_workqueue_struct *single;
unsigned long v;
} cpu_wq; /* I: cwq's */
struct list_head list; /* W: list of all workqueues */
struct mutex flush_mutex; /* protects wq flushing */
int work_color; /* F: current work color */
int flush_color; /* F: current flush color */
atomic_t nr_cwqs_to_flush; /* flush in progress */
struct wq_flusher *first_flusher; /* F: first flusher */
struct list_head flusher_queue; /* F: flush waiters */
struct list_head flusher_overflow; /* F: flush overflow list */
mayday_mask_t mayday_mask; /* cpus requesting rescue */
struct worker *rescuer; /* I: rescue worker */
int nr_drainers; /* W: drain in progress */
int saved_max_active; /* W: saved cwq max_active */
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
char name[]; /* I: workqueue name */
};
struct wq_barrier {
struct SimTask *waiter;
struct workqueue_struct wq;
};
static void
workqueue_function(void *context)
{
struct workqueue_struct *wq = context;
while (true) {
lib_task_wait();
while (!list_empty(&wq->list)) {
struct work_struct *work =
list_first_entry(&wq->list, struct work_struct,
entry);
work_func_t f = work->func;
if (work->entry.prev != LIST_POISON2) {
list_del_init(&work->entry);
clear_bit(WORK_STRUCT_PENDING_BIT,
work_data_bits(work));
f(work);
}
}
}
}
static struct SimTask *workqueue_task(struct workqueue_struct *wq)
{
struct wq_barrier *barr = container_of(wq, struct wq_barrier, wq);
if (barr->waiter == 0)
barr->waiter = lib_task_start(&workqueue_function, wq);
return barr->waiter;
}
static int flush_entry(struct workqueue_struct *wq, struct list_head *prev)
{
int active = 0;
if (!list_empty(&wq->list)) {
active = 1;
lib_task_wakeup(workqueue_task(wq));
/* XXX: should wait for completion? but this will block
and init won't return.. */
/* lib_task_wait (); */
}
return active;
}
void delayed_work_timer_fn(unsigned long data)
{
struct delayed_work *dwork = (struct delayed_work *)data;
struct work_struct *work = &dwork->work;
list_add_tail(&work->entry, &dwork->wq->list);
lib_task_wakeup(workqueue_task(dwork->wq));
}
bool queue_work_on(int cpu, struct workqueue_struct *wq,
struct work_struct *work)
{
int ret = 0;
if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
list_add_tail(&work->entry, &wq->list);
lib_task_wakeup(workqueue_task(wq));
ret = 1;
}
return ret;
}
void flush_scheduled_work(void)
{
flush_entry(system_wq, system_wq->list.prev);
}
bool flush_work(struct work_struct *work)
{
return flush_entry(system_wq, &work->entry);
}
void flush_workqueue(struct workqueue_struct *wq)
{
flush_entry(wq, wq->list.prev);
}
bool cancel_work_sync(struct work_struct *work)
{
int retval = 0;
if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
/* work was not yet queued */
return 0;
if (!list_empty(&work->entry)) {
/* work was queued. now unqueued. */
if (work->entry.prev != LIST_POISON2) {
list_del_init(&work->entry);
clear_bit(WORK_STRUCT_PENDING_BIT,
work_data_bits(work));
retval = 1;
}
}
return retval;
}
bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
struct delayed_work *dwork, unsigned long delay)
{
int ret = 0;
struct timer_list *timer = &dwork->timer;
struct work_struct *work = &dwork->work;
if (delay == 0)
return queue_work(wq, work);
if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
lib_assert(!timer_pending(timer));
dwork->wq = wq;
/* This stores cwq for the moment, for the timer_fn */
timer->expires = jiffies + delay;
timer->data = (unsigned long)dwork;
timer->function = delayed_work_timer_fn;
add_timer(timer);
ret = 1;
}
return ret;
}
bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
struct delayed_work *dwork, unsigned long delay)
{
del_timer(&dwork->timer);
__clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&dwork->work));
return queue_delayed_work(wq, dwork, delay);
}
bool cancel_delayed_work(struct delayed_work *dwork)
{
del_timer(&dwork->timer);
return cancel_work_sync(&dwork->work);
}
struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
unsigned int flags,
int max_active,
struct lock_class_key *key,
const char *lock_name, ...)
{
va_list args, args1;
struct wq_barrier *barr;
struct workqueue_struct *wq;
size_t namelen;
/* determine namelen, allocate wq and format name */
va_start(args, lock_name);
va_copy(args1, args);
namelen = vsnprintf(NULL, 0, fmt, args) + 1;
barr = kzalloc(sizeof(*barr) + namelen, GFP_KERNEL);
if (!barr)
goto err;
barr->waiter = 0;
wq = &barr->wq;
vsnprintf(wq->name, namelen, fmt, args1);
va_end(args);
va_end(args1);
max_active = max_active ? : WQ_DFL_ACTIVE;
/* init wq */
wq->flags = flags;
wq->saved_max_active = max_active;
mutex_init(&wq->flush_mutex);
atomic_set(&wq->nr_cwqs_to_flush, 0);
INIT_LIST_HEAD(&wq->flusher_queue);
INIT_LIST_HEAD(&wq->flusher_overflow);
lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
INIT_LIST_HEAD(&wq->list);
/* start waiter task */
workqueue_task(wq);
return wq;
err:
if (barr)
kfree(barr);
return NULL;
}
struct workqueue_struct *system_wq __read_mostly;
struct workqueue_struct *system_power_efficient_wq __read_mostly;
/* from linux/workqueue.h */
#define system_nrt_wq __system_nrt_wq()
static int __init init_workqueues(void)
{
system_wq = alloc_workqueue("events", 0, 0);
system_power_efficient_wq = alloc_workqueue("events_power_efficient",
WQ_POWER_EFFICIENT, 0);
return 0;
}
early_initcall(init_workqueues);