scheduler_mp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. */
  33. #include <rtthread.h>
  34. #include <rthw.h>
  35. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  36. rt_uint32_t rt_thread_ready_priority_group;
  37. #if RT_THREAD_PRIORITY_MAX > 32
  38. /* Maximum priority level, 256 */
  39. rt_uint8_t rt_thread_ready_table[32];
  40. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  41. #ifndef __on_rt_scheduler_hook
  42. #define __on_rt_scheduler_hook(from, to) __ON_HOOK_ARGS(rt_scheduler_hook, (from, to))
  43. #endif
  44. #ifndef __on_rt_scheduler_switch_hook
  45. #define __on_rt_scheduler_switch_hook(tid) __ON_HOOK_ARGS(rt_scheduler_switch_hook, (tid))
  46. #endif
  47. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  48. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  49. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  50. /**
  51. * @addtogroup Hook
  52. */
  53. /**@{*/
  54. /**
  55. * @brief This function will set a hook function, which will be invoked when thread
  56. * switch happens.
  57. *
  58. * @param hook is the hook function.
  59. */
  60. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  61. {
  62. rt_scheduler_hook = hook;
  63. }
  64. /**
  65. * @brief This function will set a hook function, which will be invoked when context
  66. * switch happens.
  67. *
  68. * @param hook is the hook function.
  69. */
  70. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  71. {
  72. rt_scheduler_switch_hook = hook;
  73. }
  74. /**@}*/
  75. #endif /* RT_USING_HOOK */
  76. #ifdef RT_USING_OVERFLOW_CHECK
  77. static void _scheduler_stack_check(struct rt_thread *thread)
  78. {
  79. RT_ASSERT(thread != RT_NULL);
  80. #ifdef RT_USING_SMART
  81. #ifndef ARCH_MM_MMU
  82. struct rt_lwp *lwp = thread ? (struct rt_lwp *)thread->lwp : 0;
  83. /* if stack pointer locate in user data section skip stack check. */
  84. if (lwp && ((rt_uint32_t)thread->sp > (rt_uint32_t)lwp->data_entry &&
  85. (rt_uint32_t)thread->sp <= (rt_uint32_t)lwp->data_entry + (rt_uint32_t)lwp->data_size))
  86. {
  87. return;
  88. }
  89. #endif /* not defined ARCH_MM_MMU */
  90. #endif /* RT_USING_SMART */
  91. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  92. if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
  93. #else
  94. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
  95. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  96. (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
  97. (rt_ubase_t)thread->sp >
  98. (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
  99. {
  100. rt_base_t level;
  101. rt_kprintf("thread:%s stack overflow\n", thread->parent.name);
  102. level = rt_hw_interrupt_disable();
  103. while (level);
  104. }
  105. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  106. else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
  107. {
  108. rt_kprintf("warning: %s stack is close to the top of stack address.\n",
  109. thread->parent.name);
  110. }
  111. #else
  112. else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
  113. {
  114. rt_kprintf("warning: %s stack is close to end of stack address.\n",
  115. thread->parent.name);
  116. }
  117. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  118. }
  119. #endif /* RT_USING_OVERFLOW_CHECK */
  120. /*
  121. * get the highest priority thread in ready queue
  122. */
  123. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  124. {
  125. struct rt_thread *highest_priority_thread;
  126. rt_ubase_t highest_ready_priority, local_highest_ready_priority;
  127. struct rt_cpu* pcpu = rt_cpu_self();
  128. #if RT_THREAD_PRIORITY_MAX > 32
  129. rt_ubase_t number;
  130. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  131. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  132. number = __rt_ffs(pcpu->priority_group) - 1;
  133. local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1;
  134. #else
  135. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  136. local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1;
  137. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  138. /* get highest ready priority thread */
  139. if (highest_ready_priority < local_highest_ready_priority)
  140. {
  141. *highest_prio = highest_ready_priority;
  142. highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  143. struct rt_thread,
  144. tlist);
  145. }
  146. else
  147. {
  148. *highest_prio = local_highest_ready_priority;
  149. highest_priority_thread = rt_list_entry(pcpu->priority_table[local_highest_ready_priority].next,
  150. struct rt_thread,
  151. tlist);
  152. }
  153. return highest_priority_thread;
  154. }
  155. /**
  156. * @brief This function will initialize the system scheduler.
  157. */
  158. void rt_system_scheduler_init(void)
  159. {
  160. int cpu;
  161. rt_base_t offset;
  162. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
  163. RT_THREAD_PRIORITY_MAX));
  164. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  165. {
  166. rt_list_init(&rt_thread_priority_table[offset]);
  167. }
  168. for (cpu = 0; cpu < RT_CPUS_NR; cpu++)
  169. {
  170. struct rt_cpu *pcpu = rt_cpu_index(cpu);
  171. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  172. {
  173. rt_list_init(&pcpu->priority_table[offset]);
  174. }
  175. pcpu->irq_switch_flag = 0;
  176. pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1;
  177. pcpu->current_thread = RT_NULL;
  178. pcpu->priority_group = 0;
  179. #if RT_THREAD_PRIORITY_MAX > 32
  180. rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table));
  181. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  182. }
  183. /* initialize ready priority group */
  184. rt_thread_ready_priority_group = 0;
  185. #if RT_THREAD_PRIORITY_MAX > 32
  186. /* initialize ready table */
  187. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  188. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  189. }
  190. /**
  191. * @brief This function will startup the scheduler. It will select one thread
  192. * with the highest priority level, then switch to it.
  193. */
  194. void rt_system_scheduler_start(void)
  195. {
  196. struct rt_thread *to_thread;
  197. rt_ubase_t highest_ready_priority;
  198. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  199. to_thread->oncpu = rt_hw_cpu_id();
  200. rt_schedule_remove_thread(to_thread);
  201. to_thread->stat = RT_THREAD_RUNNING;
  202. /* switch to new thread */
  203. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread);
  204. /* never come back */
  205. }
  206. /**
  207. * @addtogroup Thread
  208. * @cond
  209. */
  210. /**@{*/
  211. /**
  212. * @brief This function will handle IPI interrupt and do a scheduling in system.
  213. *
  214. * @param vector is the number of IPI interrupt for system scheduling.
  215. *
  216. * @param param is not used, and can be set to RT_NULL.
  217. *
  218. * @note this function should be invoke or register as ISR in BSP.
  219. */
  220. void rt_scheduler_ipi_handler(int vector, void *param)
  221. {
  222. rt_schedule();
  223. }
  224. /**
  225. * @brief This function will perform one scheduling. It will select one thread
  226. * with the highest priority level in global ready queue or local ready queue,
  227. * then switch to it.
  228. */
  229. void rt_schedule(void)
  230. {
  231. rt_base_t level;
  232. struct rt_thread *to_thread;
  233. struct rt_thread *current_thread;
  234. struct rt_cpu *pcpu;
  235. int cpu_id;
  236. /* disable interrupt */
  237. level = rt_hw_interrupt_disable();
  238. cpu_id = rt_hw_cpu_id();
  239. pcpu = rt_cpu_index(cpu_id);
  240. current_thread = pcpu->current_thread;
  241. /* whether do switch in interrupt */
  242. if (pcpu->irq_nest)
  243. {
  244. pcpu->irq_switch_flag = 1;
  245. rt_hw_interrupt_enable(level);
  246. goto __exit;
  247. }
  248. #ifdef RT_USING_SIGNALS
  249. if ((current_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  250. {
  251. /* if current_thread signal is in pending */
  252. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  253. {
  254. #ifdef RT_USING_SMART
  255. rt_thread_wakeup(current_thread);
  256. #else
  257. rt_thread_resume(current_thread);
  258. #endif
  259. }
  260. }
  261. #endif /* RT_USING_SIGNALS */
  262. if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */
  263. {
  264. rt_ubase_t highest_ready_priority;
  265. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  266. {
  267. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  268. current_thread->oncpu = RT_CPU_DETACHED;
  269. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  270. {
  271. if (current_thread->bind_cpu == RT_CPUS_NR || current_thread->bind_cpu == cpu_id)
  272. {
  273. if (current_thread->current_priority < highest_ready_priority)
  274. {
  275. to_thread = current_thread;
  276. }
  277. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  278. {
  279. to_thread = current_thread;
  280. }
  281. else
  282. {
  283. rt_schedule_insert_thread(current_thread);
  284. }
  285. }
  286. else
  287. {
  288. rt_schedule_insert_thread(current_thread);
  289. }
  290. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  291. }
  292. to_thread->oncpu = cpu_id;
  293. if (to_thread != current_thread)
  294. {
  295. /* if the destination thread is not the same as current thread */
  296. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  297. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  298. rt_schedule_remove_thread(to_thread);
  299. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  300. /* switch to new thread */
  301. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  302. ("[%d]switch to priority#%d "
  303. "thread:%.*s(sp:0x%08x), "
  304. "from thread:%.*s(sp: 0x%08x)\n",
  305. pcpu->irq_nest, highest_ready_priority,
  306. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  307. RT_NAME_MAX, current_thread->parent.name, current_thread->sp));
  308. #ifdef RT_USING_OVERFLOW_CHECK
  309. _scheduler_stack_check(to_thread);
  310. #endif /* RT_USING_OVERFLOW_CHECK */
  311. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  312. rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
  313. (rt_ubase_t)&to_thread->sp, to_thread);
  314. }
  315. }
  316. }
  317. /* enable interrupt */
  318. rt_hw_interrupt_enable(level);
  319. #ifdef RT_USING_SIGNALS
  320. /* check stat of thread for signal */
  321. level = rt_hw_interrupt_disable();
  322. if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  323. {
  324. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  325. current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  326. rt_hw_interrupt_enable(level);
  327. /* check signal status */
  328. rt_thread_handle_sig(RT_TRUE);
  329. }
  330. else
  331. {
  332. rt_hw_interrupt_enable(level);
  333. }
  334. #endif /* RT_USING_SIGNALS */
  335. __exit:
  336. return ;
  337. }
  338. /**
  339. * @brief This function checks whether a scheduling is needed after an IRQ context switching. If yes,
  340. * it will select one thread with the highest priority level, and then switch
  341. * to it.
  342. */
  343. void rt_scheduler_do_irq_switch(void *context)
  344. {
  345. int cpu_id;
  346. rt_base_t level;
  347. struct rt_cpu* pcpu;
  348. struct rt_thread *to_thread;
  349. struct rt_thread *current_thread;
  350. level = rt_hw_interrupt_disable();
  351. cpu_id = rt_hw_cpu_id();
  352. pcpu = rt_cpu_index(cpu_id);
  353. current_thread = pcpu->current_thread;
  354. #ifdef RT_USING_SIGNALS
  355. if ((current_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  356. {
  357. /* if current_thread signal is in pending */
  358. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  359. {
  360. #ifdef RT_USING_SMART
  361. rt_thread_wakeup(current_thread);
  362. #else
  363. rt_thread_resume(current_thread);
  364. #endif
  365. }
  366. }
  367. #endif /* RT_USING_SIGNALS */
  368. if (pcpu->irq_switch_flag == 0)
  369. {
  370. rt_hw_interrupt_enable(level);
  371. return;
  372. }
  373. if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0)
  374. {
  375. rt_ubase_t highest_ready_priority;
  376. /* clear irq switch flag */
  377. pcpu->irq_switch_flag = 0;
  378. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  379. {
  380. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  381. current_thread->oncpu = RT_CPU_DETACHED;
  382. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  383. {
  384. if (current_thread->bind_cpu == RT_CPUS_NR || current_thread->bind_cpu == cpu_id)
  385. {
  386. if (current_thread->current_priority < highest_ready_priority)
  387. {
  388. to_thread = current_thread;
  389. }
  390. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  391. {
  392. to_thread = current_thread;
  393. }
  394. else
  395. {
  396. rt_schedule_insert_thread(current_thread);
  397. }
  398. }
  399. else
  400. {
  401. rt_schedule_insert_thread(current_thread);
  402. }
  403. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  404. }
  405. to_thread->oncpu = cpu_id;
  406. if (to_thread != current_thread)
  407. {
  408. /* if the destination thread is not the same as current thread */
  409. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  410. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  411. rt_schedule_remove_thread(to_thread);
  412. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  413. #ifdef RT_USING_OVERFLOW_CHECK
  414. _scheduler_stack_check(to_thread);
  415. #endif /* RT_USING_OVERFLOW_CHECK */
  416. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  417. RT_ASSERT(current_thread->cpus_lock_nest > 0);
  418. current_thread->cpus_lock_nest--;
  419. current_thread->scheduler_lock_nest--;
  420. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  421. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  422. (rt_ubase_t)&to_thread->sp, to_thread);
  423. }
  424. }
  425. }
  426. rt_hw_interrupt_enable(level);
  427. }
  428. /**
  429. * @brief This function will insert a thread to the system ready queue. The state of
  430. * thread will be set as READY and the thread will be removed from suspend queue.
  431. *
  432. * @param thread is the thread to be inserted.
  433. *
  434. * @note Please do not invoke this function in user application.
  435. */
  436. void rt_schedule_insert_thread(struct rt_thread *thread)
  437. {
  438. int cpu_id;
  439. int bind_cpu;
  440. rt_uint32_t cpu_mask;
  441. rt_base_t level;
  442. RT_ASSERT(thread != RT_NULL);
  443. /* disable interrupt */
  444. level = rt_hw_interrupt_disable();
  445. /* it should be RUNNING thread */
  446. if (thread->oncpu != RT_CPU_DETACHED)
  447. {
  448. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  449. goto __exit;
  450. }
  451. /* READY thread, insert to ready queue */
  452. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  453. cpu_id = rt_hw_cpu_id();
  454. bind_cpu = thread->bind_cpu ;
  455. /* insert thread to ready list */
  456. if (bind_cpu == RT_CPUS_NR)
  457. {
  458. #if RT_THREAD_PRIORITY_MAX > 32
  459. rt_thread_ready_table[thread->number] |= thread->high_mask;
  460. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  461. rt_thread_ready_priority_group |= thread->number_mask;
  462. /* there is no time slices left(YIELD), inserting thread before ready list*/
  463. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  464. {
  465. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  466. &(thread->tlist));
  467. }
  468. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  469. else
  470. {
  471. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  472. &(thread->tlist));
  473. }
  474. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  475. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  476. }
  477. else
  478. {
  479. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  480. #if RT_THREAD_PRIORITY_MAX > 32
  481. pcpu->ready_table[thread->number] |= thread->high_mask;
  482. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  483. pcpu->priority_group |= thread->number_mask;
  484. /* there is no time slices left(YIELD), inserting thread before ready list*/
  485. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  486. {
  487. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  488. &(thread->tlist));
  489. }
  490. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  491. else
  492. {
  493. rt_list_insert_after(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  494. &(thread->tlist));
  495. }
  496. if (cpu_id != bind_cpu)
  497. {
  498. cpu_mask = 1 << bind_cpu;
  499. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  500. }
  501. }
  502. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  503. RT_NAME_MAX, thread->parent.name, thread->current_priority));
  504. __exit:
  505. /* enable interrupt */
  506. rt_hw_interrupt_enable(level);
  507. }
  508. /**
  509. * @brief This function will remove a thread from system ready queue.
  510. *
  511. * @param thread is the thread to be removed.
  512. *
  513. * @note Please do not invoke this function in user application.
  514. */
  515. void rt_schedule_remove_thread(struct rt_thread *thread)
  516. {
  517. rt_base_t level;
  518. RT_ASSERT(thread != RT_NULL);
  519. /* disable interrupt */
  520. level = rt_hw_interrupt_disable();
  521. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  522. RT_NAME_MAX, thread->parent.name,
  523. thread->current_priority));
  524. /* remove thread from ready list */
  525. rt_list_remove(&(thread->tlist));
  526. if (thread->bind_cpu == RT_CPUS_NR)
  527. {
  528. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  529. {
  530. #if RT_THREAD_PRIORITY_MAX > 32
  531. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  532. if (rt_thread_ready_table[thread->number] == 0)
  533. {
  534. rt_thread_ready_priority_group &= ~thread->number_mask;
  535. }
  536. #else
  537. rt_thread_ready_priority_group &= ~thread->number_mask;
  538. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  539. }
  540. }
  541. else
  542. {
  543. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  544. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  545. {
  546. #if RT_THREAD_PRIORITY_MAX > 32
  547. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  548. if (pcpu->ready_table[thread->number] == 0)
  549. {
  550. pcpu->priority_group &= ~thread->number_mask;
  551. }
  552. #else
  553. pcpu->priority_group &= ~thread->number_mask;
  554. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  555. }
  556. }
  557. /* enable interrupt */
  558. rt_hw_interrupt_enable(level);
  559. }
  560. /**
  561. * @brief This function will lock the thread scheduler.
  562. */
  563. void rt_enter_critical(void)
  564. {
  565. rt_base_t level;
  566. struct rt_thread *current_thread;
  567. /* disable interrupt */
  568. level = rt_hw_local_irq_disable();
  569. current_thread = rt_cpu_self()->current_thread;
  570. if (!current_thread)
  571. {
  572. rt_hw_local_irq_enable(level);
  573. return;
  574. }
  575. /*
  576. * the maximal number of nest is RT_UINT16_MAX, which is big
  577. * enough and does not check here
  578. */
  579. {
  580. rt_uint16_t lock_nest = current_thread->cpus_lock_nest;
  581. current_thread->cpus_lock_nest++;
  582. RT_ASSERT(current_thread->cpus_lock_nest != 0);
  583. if (lock_nest == 0)
  584. {
  585. current_thread->scheduler_lock_nest ++;
  586. rt_hw_spin_lock(&_cpus_lock);
  587. }
  588. }
  589. /* critical for local cpu */
  590. current_thread->critical_lock_nest ++;
  591. /* lock scheduler for local cpu */
  592. current_thread->scheduler_lock_nest ++;
  593. /* enable interrupt */
  594. rt_hw_local_irq_enable(level);
  595. }
  596. RTM_EXPORT(rt_enter_critical);
  597. /**
  598. * @brief This function will unlock the thread scheduler.
  599. */
  600. void rt_exit_critical(void)
  601. {
  602. rt_base_t level;
  603. struct rt_thread *current_thread;
  604. /* disable interrupt */
  605. level = rt_hw_local_irq_disable();
  606. current_thread = rt_cpu_self()->current_thread;
  607. if (!current_thread)
  608. {
  609. rt_hw_local_irq_enable(level);
  610. return;
  611. }
  612. current_thread->scheduler_lock_nest --;
  613. current_thread->critical_lock_nest --;
  614. RT_ASSERT(current_thread->cpus_lock_nest > 0);
  615. current_thread->cpus_lock_nest--;
  616. if (current_thread->cpus_lock_nest == 0)
  617. {
  618. current_thread->scheduler_lock_nest --;
  619. rt_hw_spin_unlock(&_cpus_lock);
  620. }
  621. if (current_thread->scheduler_lock_nest <= 0)
  622. {
  623. current_thread->scheduler_lock_nest = 0;
  624. /* enable interrupt */
  625. rt_hw_local_irq_enable(level);
  626. rt_schedule();
  627. }
  628. else
  629. {
  630. /* enable interrupt */
  631. rt_hw_local_irq_enable(level);
  632. }
  633. }
  634. RTM_EXPORT(rt_exit_critical);
  635. /**
  636. * @brief Get the scheduler lock level.
  637. *
  638. * @return the level of the scheduler lock. 0 means unlocked.
  639. */
  640. rt_uint16_t rt_critical_level(void)
  641. {
  642. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  643. return current_thread->critical_lock_nest;
  644. }
  645. RTM_EXPORT(rt_critical_level);
  646. /**@}*/
  647. /**@endcond*/