RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
scheduler_up.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2006-03-17 Bernard the first version
9 * 2006-04-28 Bernard fix the scheduler algorthm
10 * 2006-04-30 Bernard add SCHEDULER_DEBUG
11 * 2006-05-27 Bernard fix the scheduler algorthm for same priority
12 * thread schedule
13 * 2006-06-04 Bernard rewrite the scheduler algorithm
14 * 2006-08-03 Bernard add hook support
15 * 2006-09-05 Bernard add 32 priority level support
16 * 2006-09-24 Bernard add rt_system_scheduler_start function
17 * 2009-09-16 Bernard fix _rt_scheduler_stack_check
18 * 2010-04-11 yi.qiu add module feature
19 * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest
20 * issue found by kuronca
21 * 2010-12-13 Bernard add defunct list initialization even if not use heap.
22 * 2011-05-10 Bernard clean scheduler debug log.
23 * 2013-12-21 Grissiom add rt_critical_level
24 * 2018-11-22 Jesven remove the current task from ready queue
25 * add per cpu ready queue
26 * add _scheduler_get_highest_priority_thread to find highest priority task
27 * rt_schedule_insert_thread won't insert current task to ready queue
28 * in smp version, rt_hw_context_switch_interrupt maybe switch to
29 * new task directly
30 * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to scheduler.c
31 * 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c
32 * 2023-10-17 ChuShicheng Modify the timing of clearing RT_THREAD_STAT_YIELD flag bits
33 */
34
35#include <rtthread.h>
36#include <rthw.h>
37
38#define DBG_TAG "kernel.scheduler"
39#define DBG_LVL DBG_INFO
40#include <rtdbg.h>
41
42rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
43rt_uint32_t rt_thread_ready_priority_group;
44#if RT_THREAD_PRIORITY_MAX > 32
45/* Maximum priority level, 256 */
46rt_uint8_t rt_thread_ready_table[32];
47#endif /* RT_THREAD_PRIORITY_MAX > 32 */
48
49extern volatile rt_atomic_t rt_interrupt_nest;
50static rt_int16_t rt_scheduler_lock_nest;
52
53#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
54static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
55static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
56
60
62
69void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
70{
71 rt_scheduler_hook = hook;
72}
73
80void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
81{
82 rt_scheduler_switch_hook = hook;
83}
84
86#endif /* RT_USING_HOOK */
87
88static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
89{
90 struct rt_thread *highest_priority_thread;
91 rt_ubase_t highest_ready_priority;
92
93#if RT_THREAD_PRIORITY_MAX > 32
94 rt_ubase_t number;
95
96 number = __rt_ffs(rt_thread_ready_priority_group) - 1;
97 highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
98#else
99 highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
100#endif /* RT_THREAD_PRIORITY_MAX > 32 */
101
102 /* get highest ready priority thread */
103 highest_priority_thread = RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[highest_ready_priority].next);
104
105 *highest_prio = highest_ready_priority;
106
107 return highest_priority_thread;
108}
109
111{
112 rt_base_t level;
113 if (!plvl)
114 return -RT_EINVAL;
115
116 level = rt_hw_interrupt_disable();
117 *plvl = level;
118
119 return RT_EOK;
120}
121
123{
125
126 return RT_EOK;
127}
128
130{
131 if (rt_thread_self())
132 {
133 /* if scheduler is available */
134 rt_schedule();
135 }
137
138 return RT_EOK;
139}
140
145{
146 rt_base_t offset;
147 rt_scheduler_lock_nest = 0;
148
149 LOG_D("start scheduler: max priority 0x%02x",
150 RT_THREAD_PRIORITY_MAX);
151
152 for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
153 {
155 }
156
157 /* initialize ready priority group */
158 rt_thread_ready_priority_group = 0;
159
160#if RT_THREAD_PRIORITY_MAX > 32
161 /* initialize ready table */
162 rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
163#endif /* RT_THREAD_PRIORITY_MAX > 32 */
164}
165
171{
172 struct rt_thread *to_thread;
173 rt_ubase_t highest_ready_priority;
174
175 to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
176
177 rt_cpu_self()->current_thread = to_thread;
178
179 rt_sched_remove_thread(to_thread);
180 RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING;
181
182 /* switch to new thread */
183
185
186 /* never come back */
187}
188
193
195
200void rt_schedule(void)
201{
202 rt_base_t level;
203 struct rt_thread *to_thread;
204 struct rt_thread *from_thread;
205 /* using local variable to avoid unecessary function call */
206 struct rt_thread *curr_thread = rt_thread_self();
207
208 /* disable interrupt */
209 level = rt_hw_interrupt_disable();
210
211 /* check the scheduler is enabled or not */
212 if (rt_scheduler_lock_nest == 0)
213 {
214 rt_ubase_t highest_ready_priority;
215
216 if (rt_thread_ready_priority_group != 0)
217 {
218 /* need_insert_from_thread: need to insert from_thread to ready queue */
219 int need_insert_from_thread = 0;
220
221 to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
222
224 {
225 if (RT_SCHED_PRIV(curr_thread).current_priority < highest_ready_priority)
226 {
227 to_thread = curr_thread;
228 }
229 else if (RT_SCHED_PRIV(curr_thread).current_priority == highest_ready_priority
230 && (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0)
231 {
232 to_thread = curr_thread;
233 }
234 else
235 {
236 need_insert_from_thread = 1;
237 }
238 }
239
240 if (to_thread != curr_thread)
241 {
242 /* if the destination thread is not the same as current thread */
243 rt_current_priority = (rt_uint8_t)highest_ready_priority;
244 from_thread = curr_thread;
245 rt_cpu_self()->current_thread = to_thread;
246
247 RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
248
249 if (need_insert_from_thread)
250 {
251 rt_sched_insert_thread(from_thread);
252 }
253
254 if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
255 {
256 RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK;
257 }
258
259 rt_sched_remove_thread(to_thread);
260 RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK);
261
262 /* switch to new thread */
263 LOG_D("[%d]switch to priority#%d "
264 "thread:%.*s(sp:0x%08x), "
265 "from thread:%.*s(sp: 0x%08x)",
266 rt_interrupt_nest, highest_ready_priority,
267 RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
268 RT_NAME_MAX, from_thread->parent.name, from_thread->sp);
269
270 RT_SCHEDULER_STACK_CHECK(to_thread);
271
272 if (rt_interrupt_nest == 0)
273 {
274 extern void rt_thread_handle_sig(rt_bool_t clean_state);
275
276 RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
277
278 rt_hw_context_switch((rt_uintptr_t)&from_thread->sp,
279 (rt_uintptr_t)&to_thread->sp);
280
281 /* enable interrupt */
283
284#ifdef RT_USING_SIGNALS
285 /* check stat of thread for signal */
286 level = rt_hw_interrupt_disable();
288 {
289 extern void rt_thread_handle_sig(rt_bool_t clean_state);
290
291 RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
292
294
295 /* check signal status */
296 rt_thread_handle_sig(RT_TRUE);
297 }
298 else
299 {
301 }
302#endif /* RT_USING_SIGNALS */
303 goto __exit;
304 }
305 else
306 {
307 LOG_D("switch in interrupt");
308
310 (rt_uintptr_t)&to_thread->sp, from_thread, to_thread);
311 }
312 }
313 else
314 {
315 rt_sched_remove_thread(curr_thread);
316 RT_SCHED_CTX(curr_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(curr_thread).stat & ~RT_THREAD_STAT_MASK);
317 }
318 }
319 }
320
321 /* enable interrupt */
323
324__exit:
325 return;
326}
327
328/* Normally, there isn't anyone racing with us so this operation is lockless */
329void rt_sched_thread_startup(struct rt_thread *thread)
330{
331#if RT_THREAD_PRIORITY_MAX > 32
332 RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */
333 RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number;
334 RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */
335#else
336 RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority;
337#endif /* RT_THREAD_PRIORITY_MAX > 32 */
338
339 /* change thread stat, so we can resume it */
340 RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND;
341}
342
343void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority)
344{
346
347 /* priority init */
348 RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
349 RT_SCHED_PRIV(thread).init_priority = priority;
350 RT_SCHED_PRIV(thread).current_priority = priority;
351
352 /* don't add to scheduler queue as init thread */
353 RT_SCHED_PRIV(thread).number_mask = 0;
354#if RT_THREAD_PRIORITY_MAX > 32
355 RT_SCHED_PRIV(thread).number = 0;
356 RT_SCHED_PRIV(thread).high_mask = 0;
357#endif /* RT_THREAD_PRIORITY_MAX > 32 */
358
359 /* tick init */
360 RT_SCHED_PRIV(thread).init_tick = tick;
361 RT_SCHED_PRIV(thread).remaining_tick = tick;
362}
363
372void rt_sched_insert_thread(struct rt_thread *thread)
373{
374 rt_base_t level;
375
376 RT_ASSERT(thread != RT_NULL);
377
378 /* disable interrupt */
379 level = rt_hw_interrupt_disable();
380
381 /* it's current thread, it should be RUNNING thread */
382 if (thread == rt_current_thread)
383 {
384 RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
385 goto __exit;
386 }
387
388 /* READY thread, insert to ready queue */
389 RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
390 /* there is no time slices left(YIELD), inserting thread before ready list*/
391 if((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
392 {
394 &RT_THREAD_LIST_NODE(thread));
395 }
396 /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
397 else
398 {
400 &RT_THREAD_LIST_NODE(thread));
401 }
402
403 LOG_D("insert thread[%.*s], the priority: %d",
404 RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority);
405
406 /* set priority mask */
407#if RT_THREAD_PRIORITY_MAX > 32
408 rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask;
409#endif /* RT_THREAD_PRIORITY_MAX > 32 */
410 rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask;
411
412__exit:
413 /* enable interrupt */
415}
416
424void rt_sched_remove_thread(struct rt_thread *thread)
425{
426 rt_base_t level;
427
428 RT_ASSERT(thread != RT_NULL);
429
430 /* disable interrupt */
431 level = rt_hw_interrupt_disable();
432
433 LOG_D("remove thread[%.*s], the priority: %d",
434 RT_NAME_MAX, thread->parent.name,
435 RT_SCHED_PRIV(rt_current_thread).current_priority);
436
437 /* remove thread from ready list */
439 if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority])))
440 {
441#if RT_THREAD_PRIORITY_MAX > 32
442 rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask;
443 if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0)
444 {
445 rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
446 }
447#else
448 rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
449#endif /* RT_THREAD_PRIORITY_MAX > 32 */
450 }
451
452 /* enable interrupt */
454}
455
456#ifdef RT_DEBUGING_CRITICAL
457
458static volatile int _critical_error_occurred = 0;
459
460void rt_exit_critical_safe(rt_base_t critical_level)
461{
462 rt_base_t level;
463 /* disable interrupt */
464 level = rt_hw_interrupt_disable();
465
466 if (!_critical_error_occurred)
467 {
468 if (critical_level != rt_scheduler_lock_nest)
469 {
470 int dummy = 1;
471 _critical_error_occurred = 1;
472
473 rt_kprintf("%s: un-compatible critical level\n" \
474 "\tCurrent %d\n\tCaller %d\n",
475 __func__, rt_scheduler_lock_nest,
476 critical_level);
477 rt_backtrace();
478
479 while (dummy) ;
480 }
481 }
483
485}
486
487#else /* !RT_DEBUGING_CRITICAL */
488
489void rt_exit_critical_safe(rt_base_t critical_level)
490{
492}
493
494#endif/* RT_DEBUGING_CRITICAL */
496
501{
502 rt_base_t level;
503 rt_base_t critical_level;
504
505 /* disable interrupt */
506 level = rt_hw_interrupt_disable();
507
508 /*
509 * the maximal number of nest is RT_UINT16_MAX, which is big
510 * enough and does not check here
511 */
512 rt_scheduler_lock_nest ++;
513 critical_level = rt_scheduler_lock_nest;
514
515 /* enable interrupt */
517
518 return critical_level;
519}
521
525void rt_exit_critical(void)
526{
527 rt_base_t level;
528
529 /* disable interrupt */
530 level = rt_hw_interrupt_disable();
531
532 rt_scheduler_lock_nest --;
533 if (rt_scheduler_lock_nest <= 0)
534 {
535 rt_scheduler_lock_nest = 0;
536 /* enable interrupt */
538
540 {
541 /* if scheduler is started, do a schedule */
542 rt_schedule();
543 }
544 }
545 else
546 {
547 /* enable interrupt */
549 }
550}
552
559{
560 return rt_scheduler_lock_nest;
561}
563
564rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu)
565{
566 return -RT_EINVAL;
567}
568
struct rt_cpu * rt_cpu_self(void)
This fucntion will return current cpu object.
int stat(const char *file, struct stat *buf)
volatile rt_atomic_t rt_interrupt_nest
定义 irq.c:70
#define RT_OBJECT_HOOK_CALL(func, argv)
rt_inline void rt_list_remove(rt_list_t *n)
remove node from list.
rt_inline int rt_list_isempty(const rt_list_t *l)
tests whether a list is empty
#define rt_kprintf(...)
rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
insert a node before a list
#define RT_ASSERT(EX)
rt_inline void rt_list_init(rt_list_t *l)
initialize a list
rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
insert a node after a list
int __rt_ffs(int value)
This function finds the first bit set (beginning with the least significant bit) in value and return ...
rt_weak rt_err_t rt_backtrace(void)
Print backtrace of current thread to system console device
void rt_scheduler_sethook(void(*hook)(rt_thread_t from, rt_thread_t to))
void rt_exit_critical_safe(rt_base_t critical_level)
void rt_exit_critical(void)
This function will unlock the thread scheduler.
#define RT_THREAD_READY
#define RT_THREAD_STAT_MASK
void rt_scheduler_switch_sethook(void(*hook)(struct rt_thread *tid))
void rt_system_scheduler_init(void)
This function will initialize the system scheduler.
rt_thread_t rt_thread_self(void)
This function will return self thread object.
#define RT_THREAD_RUNNING
#define RT_THREAD_STAT_YIELD_MASK
#define RT_SCHEDULER_STACK_CHECK(thr)
void rt_system_scheduler_start(void)
This function will startup the scheduler. It will select one thread with the highest priority level,...
#define RT_THREAD_STAT_SIGNAL_PENDING
rt_base_t rt_enter_critical(void)
This function will lock the thread scheduler.
#define RT_THREAD_SUSPEND
void rt_schedule(void)
This function will perform one scheduling. It will select one thread with the highest priority level ...
rt_uint16_t rt_critical_level(void)
Get the scheduler lock level.
#define rt_current_thread
#define LOG_D(...)
void rt_hw_context_switch_to(rt_ubase_t to)
void rt_hw_interrupt_enable(rt_base_t level)
void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
rt_base_t rt_hw_interrupt_disable(void)
void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
#define RTM_EXPORT(symbol)
定义 rtm.h:33
#define RT_SCHED_CTX(thread)
#define RT_SCHED_PRIV(thread)
#define RT_THREAD_LIST_NODE(thread)
#define RT_THREAD_LIST_NODE_ENTRY(node)
rt_ubase_t rt_sched_lock_level_t
rt_int32_t rt_base_t
rt_base_t rt_uintptr_t
int rt_bool_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
unsigned short rt_uint16_t
#define RT_TRUE
rt_base_t rt_atomic_t
struct rt_list_node rt_list_t
unsigned int rt_uint32_t
rt_uint32_t rt_ubase_t
#define RT_NULL
signed short rt_int16_t
rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]
rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
rt_err_t rt_sched_unlock(rt_sched_lock_level_t level)
rt_uint8_t rt_current_priority
rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
struct rt_thread * current_thread
const char * name
struct rt_object parent
void * sp