scheduler.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Copyright (c) 2006-2022, 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. */
  32. #include <rtthread.h>
  33. #include <rthw.h>
  34. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  35. rt_uint32_t rt_thread_ready_priority_group;
  36. #if RT_THREAD_PRIORITY_MAX > 32
  37. /* Maximum priority level, 256 */
  38. rt_uint8_t rt_thread_ready_table[32];
  39. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  40. #ifndef RT_USING_SMP
  41. extern volatile rt_uint8_t rt_interrupt_nest;
  42. static rt_int16_t rt_scheduler_lock_nest;
  43. struct rt_thread *rt_current_thread = RT_NULL;
  44. rt_uint8_t rt_current_priority;
  45. #endif /* RT_USING_SMP */
  46. #ifndef __on_rt_scheduler_hook
  47. #define __on_rt_scheduler_hook(from, to) __ON_HOOK_ARGS(rt_scheduler_hook, (from, to))
  48. #endif
  49. #ifndef __on_rt_scheduler_switch_hook
  50. #define __on_rt_scheduler_switch_hook(tid) __ON_HOOK_ARGS(rt_scheduler_switch_hook, (tid))
  51. #endif
  52. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  53. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  54. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  55. /**
  56. * @addtogroup Hook
  57. */
  58. /**@{*/
  59. /**
  60. * @brief This function will set a hook function, which will be invoked when thread
  61. * switch happens.
  62. *
  63. * @param hook is the hook function.
  64. */
  65. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  66. {
  67. rt_scheduler_hook = hook;
  68. }
  69. /**
  70. * @brief This function will set a hook function, which will be invoked when context
  71. * switch happens.
  72. *
  73. * @param hook is the hook function.
  74. */
  75. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  76. {
  77. rt_scheduler_switch_hook = hook;
  78. }
  79. /**@}*/
  80. #endif /* RT_USING_HOOK */
  81. #ifdef RT_USING_OVERFLOW_CHECK
  82. static void _scheduler_stack_check(struct rt_thread *thread)
  83. {
  84. RT_ASSERT(thread != RT_NULL);
  85. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  86. if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
  87. #else
  88. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
  89. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  90. (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
  91. (rt_ubase_t)thread->sp >
  92. (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
  93. {
  94. rt_base_t level;
  95. rt_kprintf("thread:%s stack overflow\n", thread->name);
  96. level = rt_hw_interrupt_disable();
  97. while (level);
  98. }
  99. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  100. else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
  101. {
  102. rt_kprintf("warning: %s stack is close to the top of stack address.\n",
  103. thread->name);
  104. }
  105. #else
  106. else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
  107. {
  108. rt_kprintf("warning: %s stack is close to end of stack address.\n",
  109. thread->name);
  110. }
  111. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  112. }
  113. #endif /* RT_USING_OVERFLOW_CHECK */
  114. /*
  115. * get the highest priority thread in ready queue
  116. */
  117. #ifdef RT_USING_SMP
  118. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  119. {
  120. struct rt_thread *highest_priority_thread;
  121. rt_ubase_t highest_ready_priority, local_highest_ready_priority;
  122. struct rt_cpu* pcpu = rt_cpu_self();
  123. #if RT_THREAD_PRIORITY_MAX > 32
  124. rt_ubase_t number;
  125. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  126. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  127. number = __rt_ffs(pcpu->priority_group) - 1;
  128. local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1;
  129. #else
  130. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  131. local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1;
  132. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  133. /* get highest ready priority thread */
  134. if (highest_ready_priority < local_highest_ready_priority)
  135. {
  136. *highest_prio = highest_ready_priority;
  137. highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  138. struct rt_thread,
  139. tlist);
  140. }
  141. else
  142. {
  143. *highest_prio = local_highest_ready_priority;
  144. highest_priority_thread = rt_list_entry(pcpu->priority_table[local_highest_ready_priority].next,
  145. struct rt_thread,
  146. tlist);
  147. }
  148. return highest_priority_thread;
  149. }
  150. #else
  151. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  152. {
  153. struct rt_thread *highest_priority_thread;
  154. rt_ubase_t highest_ready_priority;
  155. #if RT_THREAD_PRIORITY_MAX > 32
  156. rt_ubase_t number;
  157. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  158. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  159. #else
  160. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  161. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  162. /* get highest ready priority thread */
  163. highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  164. struct rt_thread,
  165. tlist);
  166. *highest_prio = highest_ready_priority;
  167. return highest_priority_thread;
  168. }
  169. #endif /* RT_USING_SMP */
  170. /**
  171. * @brief This function will initialize the system scheduler.
  172. */
  173. void rt_system_scheduler_init(void)
  174. {
  175. #ifdef RT_USING_SMP
  176. int cpu;
  177. #endif /* RT_USING_SMP */
  178. rt_base_t offset;
  179. #ifndef RT_USING_SMP
  180. rt_scheduler_lock_nest = 0;
  181. #endif /* RT_USING_SMP */
  182. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
  183. RT_THREAD_PRIORITY_MAX));
  184. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  185. {
  186. rt_list_init(&rt_thread_priority_table[offset]);
  187. }
  188. #ifdef RT_USING_SMP
  189. for (cpu = 0; cpu < RT_CPUS_NR; cpu++)
  190. {
  191. struct rt_cpu *pcpu = rt_cpu_index(cpu);
  192. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  193. {
  194. rt_list_init(&pcpu->priority_table[offset]);
  195. }
  196. pcpu->irq_switch_flag = 0;
  197. pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1;
  198. pcpu->current_thread = RT_NULL;
  199. pcpu->priority_group = 0;
  200. #if RT_THREAD_PRIORITY_MAX > 32
  201. rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table));
  202. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  203. }
  204. #endif /* RT_USING_SMP */
  205. /* initialize ready priority group */
  206. rt_thread_ready_priority_group = 0;
  207. #if RT_THREAD_PRIORITY_MAX > 32
  208. /* initialize ready table */
  209. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  210. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  211. }
  212. /**
  213. * @brief This function will startup the scheduler. It will select one thread
  214. * with the highest priority level, then switch to it.
  215. */
  216. void rt_system_scheduler_start(void)
  217. {
  218. struct rt_thread *to_thread;
  219. rt_ubase_t highest_ready_priority;
  220. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  221. #ifdef RT_USING_SMP
  222. to_thread->oncpu = rt_hw_cpu_id();
  223. #else
  224. rt_current_thread = to_thread;
  225. #endif /* RT_USING_SMP */
  226. rt_schedule_remove_thread(to_thread);
  227. to_thread->stat = RT_THREAD_RUNNING;
  228. /* switch to new thread */
  229. #ifdef RT_USING_SMP
  230. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread);
  231. #else
  232. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp);
  233. #endif /* RT_USING_SMP */
  234. /* never come back */
  235. }
  236. /**
  237. * @addtogroup Thread
  238. */
  239. /**@{*/
  240. #ifdef RT_USING_SMP
  241. /**
  242. * @brief This function will handle IPI interrupt and do a scheduling in system.
  243. *
  244. * @param vector is the number of IPI interrupt for system scheduling.
  245. *
  246. * @param param is not used, and can be set to RT_NULL.
  247. *
  248. * @note this function should be invoke or register as ISR in BSP.
  249. */
  250. void rt_scheduler_ipi_handler(int vector, void *param)
  251. {
  252. rt_schedule();
  253. }
  254. /**
  255. * @brief This function will perform one scheduling. It will select one thread
  256. * with the highest priority level in global ready queue or local ready queue,
  257. * then switch to it.
  258. */
  259. void rt_schedule(void)
  260. {
  261. rt_base_t level;
  262. struct rt_thread *to_thread;
  263. struct rt_thread *current_thread;
  264. struct rt_cpu *pcpu;
  265. int cpu_id;
  266. /* disable interrupt */
  267. level = rt_hw_interrupt_disable();
  268. cpu_id = rt_hw_cpu_id();
  269. pcpu = rt_cpu_index(cpu_id);
  270. current_thread = pcpu->current_thread;
  271. /* whether do switch in interrupt */
  272. if (pcpu->irq_nest)
  273. {
  274. pcpu->irq_switch_flag = 1;
  275. rt_hw_interrupt_enable(level);
  276. goto __exit;
  277. }
  278. #ifdef RT_USING_SIGNALS
  279. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)
  280. {
  281. /* if current_thread signal is in pending */
  282. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  283. {
  284. rt_thread_resume(current_thread);
  285. }
  286. }
  287. #endif /* RT_USING_SIGNALS */
  288. if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */
  289. {
  290. rt_ubase_t highest_ready_priority;
  291. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  292. {
  293. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  294. current_thread->oncpu = RT_CPU_DETACHED;
  295. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  296. {
  297. if (current_thread->current_priority < highest_ready_priority)
  298. {
  299. to_thread = current_thread;
  300. }
  301. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  302. {
  303. to_thread = current_thread;
  304. }
  305. else
  306. {
  307. rt_schedule_insert_thread(current_thread);
  308. }
  309. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  310. }
  311. to_thread->oncpu = cpu_id;
  312. if (to_thread != current_thread)
  313. {
  314. /* if the destination thread is not the same as current thread */
  315. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  316. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  317. rt_schedule_remove_thread(to_thread);
  318. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  319. /* switch to new thread */
  320. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  321. ("[%d]switch to priority#%d "
  322. "thread:%.*s(sp:0x%08x), "
  323. "from thread:%.*s(sp: 0x%08x)\n",
  324. pcpu->irq_nest, highest_ready_priority,
  325. RT_NAME_MAX, to_thread->name, to_thread->sp,
  326. RT_NAME_MAX, current_thread->name, current_thread->sp));
  327. #ifdef RT_USING_OVERFLOW_CHECK
  328. _scheduler_stack_check(to_thread);
  329. #endif /* RT_USING_OVERFLOW_CHECK */
  330. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  331. rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
  332. (rt_ubase_t)&to_thread->sp, to_thread);
  333. }
  334. }
  335. }
  336. /* enable interrupt */
  337. rt_hw_interrupt_enable(level);
  338. #ifdef RT_USING_SIGNALS
  339. /* check stat of thread for signal */
  340. level = rt_hw_interrupt_disable();
  341. if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  342. {
  343. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  344. current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  345. rt_hw_interrupt_enable(level);
  346. /* check signal status */
  347. rt_thread_handle_sig(RT_TRUE);
  348. }
  349. else
  350. {
  351. rt_hw_interrupt_enable(level);
  352. }
  353. #endif /* RT_USING_SIGNALS */
  354. __exit:
  355. return ;
  356. }
  357. #else
  358. /**
  359. * @brief This function will perform scheduling once. It will select one thread
  360. * with the highest priority, and switch to it immediately.
  361. */
  362. void rt_schedule(void)
  363. {
  364. rt_base_t level;
  365. struct rt_thread *to_thread;
  366. struct rt_thread *from_thread;
  367. /* disable interrupt */
  368. level = rt_hw_interrupt_disable();
  369. /* check the scheduler is enabled or not */
  370. if (rt_scheduler_lock_nest == 0)
  371. {
  372. rt_ubase_t highest_ready_priority;
  373. if (rt_thread_ready_priority_group != 0)
  374. {
  375. /* need_insert_from_thread: need to insert from_thread to ready queue */
  376. int need_insert_from_thread = 0;
  377. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  378. if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  379. {
  380. if (rt_current_thread->current_priority < highest_ready_priority)
  381. {
  382. to_thread = rt_current_thread;
  383. }
  384. else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  385. {
  386. to_thread = rt_current_thread;
  387. }
  388. else
  389. {
  390. need_insert_from_thread = 1;
  391. }
  392. rt_current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  393. }
  394. if (to_thread != rt_current_thread)
  395. {
  396. /* if the destination thread is not the same as current thread */
  397. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  398. from_thread = rt_current_thread;
  399. rt_current_thread = to_thread;
  400. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  401. if (need_insert_from_thread)
  402. {
  403. rt_schedule_insert_thread(from_thread);
  404. }
  405. rt_schedule_remove_thread(to_thread);
  406. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  407. /* switch to new thread */
  408. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  409. ("[%d]switch to priority#%d "
  410. "thread:%.*s(sp:0x%08x), "
  411. "from thread:%.*s(sp: 0x%08x)\n",
  412. rt_interrupt_nest, highest_ready_priority,
  413. RT_NAME_MAX, to_thread->name, to_thread->sp,
  414. RT_NAME_MAX, from_thread->name, from_thread->sp));
  415. #ifdef RT_USING_OVERFLOW_CHECK
  416. _scheduler_stack_check(to_thread);
  417. #endif /* RT_USING_OVERFLOW_CHECK */
  418. if (rt_interrupt_nest == 0)
  419. {
  420. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  421. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
  422. rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  423. (rt_ubase_t)&to_thread->sp);
  424. /* enable interrupt */
  425. rt_hw_interrupt_enable(level);
  426. #ifdef RT_USING_SIGNALS
  427. /* check stat of thread for signal */
  428. level = rt_hw_interrupt_disable();
  429. if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  430. {
  431. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  432. rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  433. rt_hw_interrupt_enable(level);
  434. /* check signal status */
  435. rt_thread_handle_sig(RT_TRUE);
  436. }
  437. else
  438. {
  439. rt_hw_interrupt_enable(level);
  440. }
  441. #endif /* RT_USING_SIGNALS */
  442. goto __exit;
  443. }
  444. else
  445. {
  446. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  447. rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  448. (rt_ubase_t)&to_thread->sp);
  449. }
  450. }
  451. else
  452. {
  453. rt_schedule_remove_thread(rt_current_thread);
  454. rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
  455. }
  456. }
  457. }
  458. /* enable interrupt */
  459. rt_hw_interrupt_enable(level);
  460. __exit:
  461. return;
  462. }
  463. #endif /* RT_USING_SMP */
  464. /**
  465. * @brief This function checks whether a scheduling is needed after an IRQ context switching. If yes,
  466. * it will select one thread with the highest priority level, and then switch
  467. * to it.
  468. */
  469. #ifdef RT_USING_SMP
  470. void rt_scheduler_do_irq_switch(void *context)
  471. {
  472. int cpu_id;
  473. rt_base_t level;
  474. struct rt_cpu* pcpu;
  475. struct rt_thread *to_thread;
  476. struct rt_thread *current_thread;
  477. level = rt_hw_interrupt_disable();
  478. cpu_id = rt_hw_cpu_id();
  479. pcpu = rt_cpu_index(cpu_id);
  480. current_thread = pcpu->current_thread;
  481. #ifdef RT_USING_SIGNALS
  482. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)
  483. {
  484. /* if current_thread signal is in pending */
  485. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  486. {
  487. rt_thread_resume(current_thread);
  488. }
  489. }
  490. #endif /* RT_USING_SIGNALS */
  491. if (pcpu->irq_switch_flag == 0)
  492. {
  493. rt_hw_interrupt_enable(level);
  494. return;
  495. }
  496. if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0)
  497. {
  498. rt_ubase_t highest_ready_priority;
  499. /* clear irq switch flag */
  500. pcpu->irq_switch_flag = 0;
  501. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  502. {
  503. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  504. current_thread->oncpu = RT_CPU_DETACHED;
  505. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  506. {
  507. if (current_thread->current_priority < highest_ready_priority)
  508. {
  509. to_thread = current_thread;
  510. }
  511. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  512. {
  513. to_thread = current_thread;
  514. }
  515. else
  516. {
  517. rt_schedule_insert_thread(current_thread);
  518. }
  519. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  520. }
  521. to_thread->oncpu = cpu_id;
  522. if (to_thread != current_thread)
  523. {
  524. /* if the destination thread is not the same as current thread */
  525. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  526. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  527. rt_schedule_remove_thread(to_thread);
  528. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  529. #ifdef RT_USING_OVERFLOW_CHECK
  530. _scheduler_stack_check(to_thread);
  531. #endif /* RT_USING_OVERFLOW_CHECK */
  532. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  533. current_thread->cpus_lock_nest--;
  534. current_thread->scheduler_lock_nest--;
  535. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  536. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  537. (rt_ubase_t)&to_thread->sp, to_thread);
  538. }
  539. }
  540. }
  541. rt_hw_interrupt_enable(level);
  542. }
  543. #endif /* RT_USING_SMP */
  544. /**
  545. * @brief This function will insert a thread to the system ready queue. The state of
  546. * thread will be set as READY and the thread will be removed from suspend queue.
  547. *
  548. * @param thread is the thread to be inserted.
  549. *
  550. * @note Please do not invoke this function in user application.
  551. */
  552. #ifdef RT_USING_SMP
  553. void rt_schedule_insert_thread(struct rt_thread *thread)
  554. {
  555. int cpu_id;
  556. int bind_cpu;
  557. rt_uint32_t cpu_mask;
  558. rt_base_t level;
  559. RT_ASSERT(thread != RT_NULL);
  560. /* disable interrupt */
  561. level = rt_hw_interrupt_disable();
  562. /* it should be RUNNING thread */
  563. if (thread->oncpu != RT_CPU_DETACHED)
  564. {
  565. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  566. goto __exit;
  567. }
  568. /* READY thread, insert to ready queue */
  569. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  570. cpu_id = rt_hw_cpu_id();
  571. bind_cpu = thread->bind_cpu ;
  572. /* insert thread to ready list */
  573. if (bind_cpu == RT_CPUS_NR)
  574. {
  575. #if RT_THREAD_PRIORITY_MAX > 32
  576. rt_thread_ready_table[thread->number] |= thread->high_mask;
  577. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  578. rt_thread_ready_priority_group |= thread->number_mask;
  579. /* there is no time slices left(YIELD), inserting thread before ready list*/
  580. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  581. {
  582. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  583. &(thread->tlist));
  584. }
  585. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  586. else
  587. {
  588. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  589. &(thread->tlist));
  590. }
  591. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  592. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  593. }
  594. else
  595. {
  596. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  597. #if RT_THREAD_PRIORITY_MAX > 32
  598. pcpu->ready_table[thread->number] |= thread->high_mask;
  599. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  600. pcpu->priority_group |= thread->number_mask;
  601. /* there is no time slices left(YIELD), inserting thread before ready list*/
  602. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  603. {
  604. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  605. &(thread->tlist));
  606. }
  607. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  608. else
  609. {
  610. rt_list_insert_after(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  611. &(thread->tlist));
  612. }
  613. if (cpu_id != bind_cpu)
  614. {
  615. cpu_mask = 1 << bind_cpu;
  616. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  617. }
  618. }
  619. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  620. RT_NAME_MAX, thread->name, thread->current_priority));
  621. __exit:
  622. /* enable interrupt */
  623. rt_hw_interrupt_enable(level);
  624. }
  625. #else
  626. void rt_schedule_insert_thread(struct rt_thread *thread)
  627. {
  628. rt_base_t level;
  629. RT_ASSERT(thread != RT_NULL);
  630. /* disable interrupt */
  631. level = rt_hw_interrupt_disable();
  632. /* it's current thread, it should be RUNNING thread */
  633. if (thread == rt_current_thread)
  634. {
  635. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  636. goto __exit;
  637. }
  638. /* READY thread, insert to ready queue */
  639. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  640. /* there is no time slices left(YIELD), inserting thread before ready list*/
  641. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  642. {
  643. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  644. &(thread->tlist));
  645. }
  646. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  647. else
  648. {
  649. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  650. &(thread->tlist));
  651. }
  652. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  653. RT_NAME_MAX, thread->name, thread->current_priority));
  654. /* set priority mask */
  655. #if RT_THREAD_PRIORITY_MAX > 32
  656. rt_thread_ready_table[thread->number] |= thread->high_mask;
  657. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  658. rt_thread_ready_priority_group |= thread->number_mask;
  659. __exit:
  660. /* enable interrupt */
  661. rt_hw_interrupt_enable(level);
  662. }
  663. #endif /* RT_USING_SMP */
  664. /**
  665. * @brief This function will remove a thread from system ready queue.
  666. *
  667. * @param thread is the thread to be removed.
  668. *
  669. * @note Please do not invoke this function in user application.
  670. */
  671. #ifdef RT_USING_SMP
  672. void rt_schedule_remove_thread(struct rt_thread *thread)
  673. {
  674. rt_base_t level;
  675. RT_ASSERT(thread != RT_NULL);
  676. /* disable interrupt */
  677. level = rt_hw_interrupt_disable();
  678. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  679. RT_NAME_MAX, thread->name,
  680. thread->current_priority));
  681. /* remove thread from ready list */
  682. rt_list_remove(&(thread->tlist));
  683. if (thread->bind_cpu == RT_CPUS_NR)
  684. {
  685. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  686. {
  687. #if RT_THREAD_PRIORITY_MAX > 32
  688. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  689. if (rt_thread_ready_table[thread->number] == 0)
  690. {
  691. rt_thread_ready_priority_group &= ~thread->number_mask;
  692. }
  693. #else
  694. rt_thread_ready_priority_group &= ~thread->number_mask;
  695. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  696. }
  697. }
  698. else
  699. {
  700. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  701. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  702. {
  703. #if RT_THREAD_PRIORITY_MAX > 32
  704. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  705. if (pcpu->ready_table[thread->number] == 0)
  706. {
  707. pcpu->priority_group &= ~thread->number_mask;
  708. }
  709. #else
  710. pcpu->priority_group &= ~thread->number_mask;
  711. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  712. }
  713. }
  714. /* enable interrupt */
  715. rt_hw_interrupt_enable(level);
  716. }
  717. #else
  718. void rt_schedule_remove_thread(struct rt_thread *thread)
  719. {
  720. rt_base_t level;
  721. RT_ASSERT(thread != RT_NULL);
  722. /* disable interrupt */
  723. level = rt_hw_interrupt_disable();
  724. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  725. RT_NAME_MAX, thread->name,
  726. thread->current_priority));
  727. /* remove thread from ready list */
  728. rt_list_remove(&(thread->tlist));
  729. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  730. {
  731. #if RT_THREAD_PRIORITY_MAX > 32
  732. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  733. if (rt_thread_ready_table[thread->number] == 0)
  734. {
  735. rt_thread_ready_priority_group &= ~thread->number_mask;
  736. }
  737. #else
  738. rt_thread_ready_priority_group &= ~thread->number_mask;
  739. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  740. }
  741. /* enable interrupt */
  742. rt_hw_interrupt_enable(level);
  743. }
  744. #endif /* RT_USING_SMP */
  745. /**
  746. * @brief This function will lock the thread scheduler.
  747. */
  748. #ifdef RT_USING_SMP
  749. void rt_enter_critical(void)
  750. {
  751. rt_base_t level;
  752. struct rt_thread *current_thread;
  753. /* disable interrupt */
  754. level = rt_hw_local_irq_disable();
  755. current_thread = rt_cpu_self()->current_thread;
  756. if (!current_thread)
  757. {
  758. rt_hw_local_irq_enable(level);
  759. return;
  760. }
  761. /*
  762. * the maximal number of nest is RT_UINT16_MAX, which is big
  763. * enough and does not check here
  764. */
  765. {
  766. rt_uint16_t lock_nest = current_thread->cpus_lock_nest;
  767. current_thread->cpus_lock_nest++;
  768. if (lock_nest == 0)
  769. {
  770. current_thread->scheduler_lock_nest ++;
  771. rt_hw_spin_lock(&_cpus_lock);
  772. }
  773. }
  774. /* critical for local cpu */
  775. current_thread->critical_lock_nest ++;
  776. /* lock scheduler for local cpu */
  777. current_thread->scheduler_lock_nest ++;
  778. /* enable interrupt */
  779. rt_hw_local_irq_enable(level);
  780. }
  781. #else
  782. void rt_enter_critical(void)
  783. {
  784. rt_base_t level;
  785. /* disable interrupt */
  786. level = rt_hw_interrupt_disable();
  787. /*
  788. * the maximal number of nest is RT_UINT16_MAX, which is big
  789. * enough and does not check here
  790. */
  791. rt_scheduler_lock_nest ++;
  792. /* enable interrupt */
  793. rt_hw_interrupt_enable(level);
  794. }
  795. #endif /* RT_USING_SMP */
  796. RTM_EXPORT(rt_enter_critical);
  797. /**
  798. * @brief This function will unlock the thread scheduler.
  799. */
  800. #ifdef RT_USING_SMP
  801. void rt_exit_critical(void)
  802. {
  803. rt_base_t level;
  804. struct rt_thread *current_thread;
  805. /* disable interrupt */
  806. level = rt_hw_local_irq_disable();
  807. current_thread = rt_cpu_self()->current_thread;
  808. if (!current_thread)
  809. {
  810. rt_hw_local_irq_enable(level);
  811. return;
  812. }
  813. current_thread->scheduler_lock_nest --;
  814. current_thread->critical_lock_nest --;
  815. current_thread->cpus_lock_nest--;
  816. if (current_thread->cpus_lock_nest == 0)
  817. {
  818. current_thread->scheduler_lock_nest --;
  819. rt_hw_spin_unlock(&_cpus_lock);
  820. }
  821. if (current_thread->scheduler_lock_nest <= 0)
  822. {
  823. current_thread->scheduler_lock_nest = 0;
  824. /* enable interrupt */
  825. rt_hw_local_irq_enable(level);
  826. rt_schedule();
  827. }
  828. else
  829. {
  830. /* enable interrupt */
  831. rt_hw_local_irq_enable(level);
  832. }
  833. }
  834. #else
  835. void rt_exit_critical(void)
  836. {
  837. rt_base_t level;
  838. /* disable interrupt */
  839. level = rt_hw_interrupt_disable();
  840. rt_scheduler_lock_nest --;
  841. if (rt_scheduler_lock_nest <= 0)
  842. {
  843. rt_scheduler_lock_nest = 0;
  844. /* enable interrupt */
  845. rt_hw_interrupt_enable(level);
  846. if (rt_current_thread)
  847. {
  848. /* if scheduler is started, do a schedule */
  849. rt_schedule();
  850. }
  851. }
  852. else
  853. {
  854. /* enable interrupt */
  855. rt_hw_interrupt_enable(level);
  856. }
  857. }
  858. #endif /* RT_USING_SMP */
  859. RTM_EXPORT(rt_exit_critical);
  860. /**
  861. * @brief Get the scheduler lock level.
  862. *
  863. * @return the level of the scheduler lock. 0 means unlocked.
  864. */
  865. rt_uint16_t rt_critical_level(void)
  866. {
  867. #ifdef RT_USING_SMP
  868. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  869. return current_thread->critical_lock_nest;
  870. #else
  871. return rt_scheduler_lock_nest;
  872. #endif /* RT_USING_SMP */
  873. }
  874. RTM_EXPORT(rt_critical_level);
  875. /**@}*/