-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkqueue.c
More file actions
292 lines (234 loc) · 6.67 KB
/
Copy pathworkqueue.c
File metadata and controls
292 lines (234 loc) · 6.67 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2026
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include "workqueue.h"
/* 全局默认队列 */
struct workqueue_struct *system_wq = NULL;
/* 静态池:支持创建少量额外队列 */
#define WQ_MAX_QUEUES 2
static struct workqueue_struct wq_pool[WQ_MAX_QUEUES];
static int wq_pool_idx = 0;
/* ───── 位操作辅助 ───── */
static inline void set_bit(int nr, unsigned long *addr)
{
*addr |= (1UL << nr);
}
static inline void clear_bit(int nr, unsigned long *addr)
{
*addr &= ~(1UL << nr);
}
static inline int test_bit(int nr, unsigned long addr)
{
return (addr >> nr) & 1;
}
/* ───── 平台互斥锁钩子(弱定义,用户可覆盖) ───── */
__attribute__((weak))
unsigned long wq_platform_lock_irqsave(void)
{
/* 默认空实现:适用于单线程或关中断的单核 MCU */
return 0;
}
__attribute__((weak))
void wq_platform_unlock_irqrestore(unsigned long flags)
{
(void)flags;
}
static inline unsigned long wq_lock_irqsave(void)
{
return wq_platform_lock_irqsave();
}
static inline void wq_unlock_irqrestore(unsigned long flags)
{
wq_platform_unlock_irqrestore(flags);
}
/* ───── 初始化 ───── */
void INIT_WORK(struct work_struct *work, void (*func)(struct work_struct *))
{
INIT_LIST_HEAD(&work->entry);
work->func = func;
work->flags = 0;
}
void INIT_DELAYED_WORK(struct delayed_work *dwork,
void (*func)(struct work_struct *))
{
INIT_WORK(&dwork->work, func);
dwork->tick_expire = 0;
dwork->wq = NULL;
}
/* ───── 调度 ───── */
bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
{
unsigned long flags;
flags = wq_lock_irqsave();
if (test_bit(WORK_STRUCT_PENDING, work->flags)) {
wq_unlock_irqrestore(flags);
return false;
}
set_bit(WORK_STRUCT_PENDING, &work->flags);
list_add_tail(&work->entry, &wq->work_list);
wq_unlock_irqrestore(flags);
return true;
}
bool queue_delayed_work(struct workqueue_struct *wq,
struct delayed_work *dwork,
unsigned long delay_ticks)
{
struct delayed_work *pos;
unsigned long flags;
flags = wq_lock_irqsave();
if (test_bit(WORK_STRUCT_PENDING, dwork->work.flags)) {
wq_unlock_irqrestore(flags);
return false;
}
set_bit(WORK_STRUCT_PENDING, &dwork->work.flags);
dwork->tick_expire = jiffies + delay_ticks;
dwork->wq = wq;
/* 按 tick_expire 升序插入,相同 tick 排后面(FIFO) */
list_for_each_entry(pos, &wq->delayed_list, work.entry) {
if (time_after(pos->tick_expire, dwork->tick_expire))
break;
}
list_add_tail(&dwork->work.entry, &pos->work.entry);
wq_unlock_irqrestore(flags);
return true;
}
bool schedule_work(struct work_struct *work)
{
return queue_work(system_wq, work);
}
bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay_ticks)
{
return queue_delayed_work(system_wq, dwork, delay_ticks);
}
/* ───── 内部:执行单个工作 ───── */
static void __workqueue_run_one(struct workqueue_struct *wq)
{
struct work_struct *work = NULL;
unsigned long flags;
flags = wq_lock_irqsave();
if (!list_empty(&wq->work_list)) {
work = list_first_entry(&wq->work_list,
struct work_struct, entry);
list_del_init(&work->entry);
clear_bit(WORK_STRUCT_PENDING, &work->flags);
set_bit(WORK_STRUCT_RUNNING, &work->flags);
wq->nr_running++;
}
wq_unlock_irqrestore(flags);
if (work) {
work->func(work);
flags = wq_lock_irqsave();
clear_bit(WORK_STRUCT_RUNNING, &work->flags);
wq->nr_running--;
wq_unlock_irqrestore(flags);
}
}
void workqueue_run_one(struct workqueue_struct *wq)
{
__workqueue_run_one(wq);
}
void workqueue_run_all(struct workqueue_struct *wq)
{
while (!list_empty(&wq->work_list))
__workqueue_run_one(wq);
}
/* ───── tick 中断处理:将到期延迟工作移入执行队列 ───── */
void workqueue_tick_handler(struct workqueue_struct *wq,
unsigned long current_tick)
{
struct delayed_work *pos, *n;
unsigned long flags;
flags = wq_lock_irqsave();
/* 链表按 tick_expire 升序排列,扫到第一个未到期的就停 */
list_for_each_entry_safe(pos, n, &wq->delayed_list, work.entry) {
if (!time_after_eq(current_tick, pos->tick_expire))
break;
list_del_init(&pos->work.entry);
clear_bit(WORK_STRUCT_PENDING, &pos->work.flags);
pos->wq = NULL;
list_add_tail(&pos->work.entry, &wq->work_list);
set_bit(WORK_STRUCT_PENDING, &pos->work.flags);
}
wq_unlock_irqrestore(flags);
}
/* ───── 取消 ───── */
bool cancel_work_sync(struct work_struct *work)
{
unsigned long flags;
flags = wq_lock_irqsave();
if (test_bit(WORK_STRUCT_PENDING, work->flags)) {
list_del_init(&work->entry);
clear_bit(WORK_STRUCT_PENDING, &work->flags);
wq_unlock_irqrestore(flags);
return true;
}
wq_unlock_irqrestore(flags);
/* 若正在运行,忙等其完成 */
while (test_bit(WORK_STRUCT_RUNNING, work->flags))
; /* MCU 实际场景可插入 wfi/任务切换 */
return true;
}
bool cancel_delayed_work_sync(struct delayed_work *dwork)
{
unsigned long flags;
flags = wq_lock_irqsave();
if (test_bit(WORK_STRUCT_PENDING, dwork->work.flags)) {
list_del_init(&dwork->work.entry);
clear_bit(WORK_STRUCT_PENDING, &dwork->work.flags);
dwork->wq = NULL;
wq_unlock_irqrestore(flags);
return true;
}
wq_unlock_irqrestore(flags);
return cancel_work_sync(&dwork->work);
}
/* ───── 刷新 ───── */
void flush_work(struct work_struct *work)
{
while (test_bit(WORK_STRUCT_PENDING, work->flags) ||
test_bit(WORK_STRUCT_RUNNING, work->flags)) {
workqueue_run_one(system_wq);
}
}
void flush_delayed_work(struct delayed_work *dwork)
{
flush_work(&dwork->work);
}
void flush_workqueue(struct workqueue_struct *wq)
{
while (!list_empty(&wq->work_list) || !list_empty(&wq->delayed_list))
workqueue_run_all(wq);
}
/* ───── 队列管理 ───── */
struct workqueue_struct *create_workqueue(const char *name)
{
struct workqueue_struct *wq;
if (wq_pool_idx >= WQ_MAX_QUEUES)
return NULL;
wq = &wq_pool[wq_pool_idx++];
INIT_LIST_HEAD(&wq->work_list);
INIT_LIST_HEAD(&wq->delayed_list);
wq->name = name;
wq->nr_running = 0;
return wq;
}
void destroy_workqueue(struct workqueue_struct *wq)
{
(void)wq;
/* 静态池,无需释放 */
}
void workqueue_init(void)
{
static struct workqueue_struct sys_wq;
INIT_LIST_HEAD(&sys_wq.work_list);
INIT_LIST_HEAD(&sys_wq.delayed_list);
sys_wq.name = "system_wq";
sys_wq.nr_running = 0;
system_wq = &sys_wq;
}