scheduler.c 33 KB

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