scheduler.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * Copyright (c) 2006-2021, 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 _rt_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_ubase_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. register struct rt_thread *highest_priority_thread;
  121. register 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. register 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. register struct rt_thread *highest_priority_thread;
  154. register rt_ubase_t highest_ready_priority;
  155. #if RT_THREAD_PRIORITY_MAX > 32
  156. register 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. register 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. register 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. _rt_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. _rt_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. _rt_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. register 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. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  580. &(thread->tlist));
  581. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  582. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  583. }
  584. else
  585. {
  586. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  587. #if RT_THREAD_PRIORITY_MAX > 32
  588. pcpu->ready_table[thread->number] |= thread->high_mask;
  589. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  590. pcpu->priority_group |= thread->number_mask;
  591. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  592. &(thread->tlist));
  593. if (cpu_id != bind_cpu)
  594. {
  595. cpu_mask = 1 << bind_cpu;
  596. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  597. }
  598. }
  599. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  600. RT_NAME_MAX, thread->name, thread->current_priority));
  601. __exit:
  602. /* enable interrupt */
  603. rt_hw_interrupt_enable(level);
  604. }
  605. #else
  606. void rt_schedule_insert_thread(struct rt_thread *thread)
  607. {
  608. register rt_base_t temp;
  609. RT_ASSERT(thread != RT_NULL);
  610. /* disable interrupt */
  611. temp = rt_hw_interrupt_disable();
  612. /* it's current thread, it should be RUNNING thread */
  613. if (thread == rt_current_thread)
  614. {
  615. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  616. goto __exit;
  617. }
  618. /* READY thread, insert to ready queue */
  619. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  620. /* insert thread to ready list */
  621. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  622. &(thread->tlist));
  623. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  624. RT_NAME_MAX, thread->name, thread->current_priority));
  625. /* set priority mask */
  626. #if RT_THREAD_PRIORITY_MAX > 32
  627. rt_thread_ready_table[thread->number] |= thread->high_mask;
  628. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  629. rt_thread_ready_priority_group |= thread->number_mask;
  630. __exit:
  631. /* enable interrupt */
  632. rt_hw_interrupt_enable(temp);
  633. }
  634. #endif /* RT_USING_SMP */
  635. /**
  636. * @brief This function will remove a thread from system ready queue.
  637. *
  638. * @param thread is the thread to be removed.
  639. *
  640. * @note Please do not invoke this function in user application.
  641. */
  642. #ifdef RT_USING_SMP
  643. void rt_schedule_remove_thread(struct rt_thread *thread)
  644. {
  645. register rt_base_t level;
  646. RT_ASSERT(thread != RT_NULL);
  647. /* disable interrupt */
  648. level = rt_hw_interrupt_disable();
  649. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  650. RT_NAME_MAX, thread->name,
  651. thread->current_priority));
  652. /* remove thread from ready list */
  653. rt_list_remove(&(thread->tlist));
  654. if (thread->bind_cpu == RT_CPUS_NR)
  655. {
  656. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  657. {
  658. #if RT_THREAD_PRIORITY_MAX > 32
  659. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  660. if (rt_thread_ready_table[thread->number] == 0)
  661. {
  662. rt_thread_ready_priority_group &= ~thread->number_mask;
  663. }
  664. #else
  665. rt_thread_ready_priority_group &= ~thread->number_mask;
  666. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  667. }
  668. }
  669. else
  670. {
  671. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  672. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  673. {
  674. #if RT_THREAD_PRIORITY_MAX > 32
  675. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  676. if (pcpu->ready_table[thread->number] == 0)
  677. {
  678. pcpu->priority_group &= ~thread->number_mask;
  679. }
  680. #else
  681. pcpu->priority_group &= ~thread->number_mask;
  682. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  683. }
  684. }
  685. /* enable interrupt */
  686. rt_hw_interrupt_enable(level);
  687. }
  688. #else
  689. void rt_schedule_remove_thread(struct rt_thread *thread)
  690. {
  691. register rt_base_t level;
  692. RT_ASSERT(thread != RT_NULL);
  693. /* disable interrupt */
  694. level = rt_hw_interrupt_disable();
  695. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  696. RT_NAME_MAX, thread->name,
  697. thread->current_priority));
  698. /* remove thread from ready list */
  699. rt_list_remove(&(thread->tlist));
  700. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  701. {
  702. #if RT_THREAD_PRIORITY_MAX > 32
  703. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  704. if (rt_thread_ready_table[thread->number] == 0)
  705. {
  706. rt_thread_ready_priority_group &= ~thread->number_mask;
  707. }
  708. #else
  709. rt_thread_ready_priority_group &= ~thread->number_mask;
  710. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  711. }
  712. /* enable interrupt */
  713. rt_hw_interrupt_enable(level);
  714. }
  715. #endif /* RT_USING_SMP */
  716. /**
  717. * @brief This function will lock the thread scheduler.
  718. */
  719. #ifdef RT_USING_SMP
  720. void rt_enter_critical(void)
  721. {
  722. register rt_base_t level;
  723. struct rt_thread *current_thread;
  724. /* disable interrupt */
  725. level = rt_hw_local_irq_disable();
  726. current_thread = rt_cpu_self()->current_thread;
  727. if (!current_thread)
  728. {
  729. rt_hw_local_irq_enable(level);
  730. return;
  731. }
  732. /*
  733. * the maximal number of nest is RT_UINT16_MAX, which is big
  734. * enough and does not check here
  735. */
  736. {
  737. register rt_uint16_t lock_nest = current_thread->cpus_lock_nest;
  738. current_thread->cpus_lock_nest++;
  739. if (lock_nest == 0)
  740. {
  741. current_thread->scheduler_lock_nest ++;
  742. rt_hw_spin_lock(&_cpus_lock);
  743. }
  744. }
  745. /* critical for local cpu */
  746. current_thread->critical_lock_nest ++;
  747. /* lock scheduler for local cpu */
  748. current_thread->scheduler_lock_nest ++;
  749. /* enable interrupt */
  750. rt_hw_local_irq_enable(level);
  751. }
  752. #else
  753. void rt_enter_critical(void)
  754. {
  755. register rt_base_t level;
  756. /* disable interrupt */
  757. level = rt_hw_interrupt_disable();
  758. /*
  759. * the maximal number of nest is RT_UINT16_MAX, which is big
  760. * enough and does not check here
  761. */
  762. rt_scheduler_lock_nest ++;
  763. /* enable interrupt */
  764. rt_hw_interrupt_enable(level);
  765. }
  766. #endif /* RT_USING_SMP */
  767. RTM_EXPORT(rt_enter_critical);
  768. /**
  769. * @brief This function will unlock the thread scheduler.
  770. */
  771. #ifdef RT_USING_SMP
  772. void rt_exit_critical(void)
  773. {
  774. register rt_base_t level;
  775. struct rt_thread *current_thread;
  776. /* disable interrupt */
  777. level = rt_hw_local_irq_disable();
  778. current_thread = rt_cpu_self()->current_thread;
  779. if (!current_thread)
  780. {
  781. rt_hw_local_irq_enable(level);
  782. return;
  783. }
  784. current_thread->scheduler_lock_nest --;
  785. current_thread->critical_lock_nest --;
  786. current_thread->cpus_lock_nest--;
  787. if (current_thread->cpus_lock_nest == 0)
  788. {
  789. current_thread->scheduler_lock_nest --;
  790. rt_hw_spin_unlock(&_cpus_lock);
  791. }
  792. if (current_thread->scheduler_lock_nest <= 0)
  793. {
  794. current_thread->scheduler_lock_nest = 0;
  795. /* enable interrupt */
  796. rt_hw_local_irq_enable(level);
  797. rt_schedule();
  798. }
  799. else
  800. {
  801. /* enable interrupt */
  802. rt_hw_local_irq_enable(level);
  803. }
  804. }
  805. #else
  806. void rt_exit_critical(void)
  807. {
  808. register rt_base_t level;
  809. /* disable interrupt */
  810. level = rt_hw_interrupt_disable();
  811. rt_scheduler_lock_nest --;
  812. if (rt_scheduler_lock_nest <= 0)
  813. {
  814. rt_scheduler_lock_nest = 0;
  815. /* enable interrupt */
  816. rt_hw_interrupt_enable(level);
  817. if (rt_current_thread)
  818. {
  819. /* if scheduler is started, do a schedule */
  820. rt_schedule();
  821. }
  822. }
  823. else
  824. {
  825. /* enable interrupt */
  826. rt_hw_interrupt_enable(level);
  827. }
  828. }
  829. #endif /* RT_USING_SMP */
  830. RTM_EXPORT(rt_exit_critical);
  831. /**
  832. * @brief Get the scheduler lock level.
  833. *
  834. * @return the level of the scheduler lock. 0 means unlocked.
  835. */
  836. rt_uint16_t rt_critical_level(void)
  837. {
  838. #ifdef RT_USING_SMP
  839. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  840. return current_thread->critical_lock_nest;
  841. #else
  842. return rt_scheduler_lock_nest;
  843. #endif /* RT_USING_SMP */
  844. }
  845. RTM_EXPORT(rt_critical_level);
  846. /**@}*/