scheduler.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. RT_ASSERT(current_thread->cpus_lock_nest > 0);
  570. current_thread->cpus_lock_nest--;
  571. current_thread->scheduler_lock_nest--;
  572. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  573. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  574. (rt_ubase_t)&to_thread->sp, to_thread);
  575. }
  576. }
  577. }
  578. rt_hw_interrupt_enable(level);
  579. }
  580. #endif /* RT_USING_SMP */
  581. /**
  582. * @brief This function will insert a thread to the system ready queue. The state of
  583. * thread will be set as READY and the thread will be removed from suspend queue.
  584. *
  585. * @param thread is the thread to be inserted.
  586. *
  587. * @note Please do not invoke this function in user application.
  588. */
  589. #ifdef RT_USING_SMP
  590. void rt_schedule_insert_thread(struct rt_thread *thread)
  591. {
  592. int cpu_id;
  593. int bind_cpu;
  594. rt_uint32_t cpu_mask;
  595. rt_base_t level;
  596. RT_ASSERT(thread != RT_NULL);
  597. /* disable interrupt */
  598. level = rt_hw_interrupt_disable();
  599. /* it should be RUNNING thread */
  600. if (thread->oncpu != RT_CPU_DETACHED)
  601. {
  602. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  603. goto __exit;
  604. }
  605. /* READY thread, insert to ready queue */
  606. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  607. cpu_id = rt_hw_cpu_id();
  608. bind_cpu = thread->bind_cpu ;
  609. /* insert thread to ready list */
  610. if (bind_cpu == RT_CPUS_NR)
  611. {
  612. #if RT_THREAD_PRIORITY_MAX > 32
  613. rt_thread_ready_table[thread->number] |= thread->high_mask;
  614. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  615. rt_thread_ready_priority_group |= thread->number_mask;
  616. /* there is no time slices left(YIELD), inserting thread before ready list*/
  617. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  618. {
  619. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  620. &(thread->tlist));
  621. }
  622. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  623. else
  624. {
  625. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  626. &(thread->tlist));
  627. }
  628. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  629. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  630. }
  631. else
  632. {
  633. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  634. #if RT_THREAD_PRIORITY_MAX > 32
  635. pcpu->ready_table[thread->number] |= thread->high_mask;
  636. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  637. pcpu->priority_group |= thread->number_mask;
  638. /* there is no time slices left(YIELD), inserting thread before ready list*/
  639. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  640. {
  641. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  642. &(thread->tlist));
  643. }
  644. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  645. else
  646. {
  647. rt_list_insert_after(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  648. &(thread->tlist));
  649. }
  650. if (cpu_id != bind_cpu)
  651. {
  652. cpu_mask = 1 << bind_cpu;
  653. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  654. }
  655. }
  656. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  657. RT_NAME_MAX, thread->name, thread->current_priority));
  658. __exit:
  659. /* enable interrupt */
  660. rt_hw_interrupt_enable(level);
  661. }
  662. #else
  663. void rt_schedule_insert_thread(struct rt_thread *thread)
  664. {
  665. rt_base_t level;
  666. RT_ASSERT(thread != RT_NULL);
  667. /* disable interrupt */
  668. level = rt_hw_interrupt_disable();
  669. /* it's current thread, it should be RUNNING thread */
  670. if (thread == rt_current_thread)
  671. {
  672. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  673. goto __exit;
  674. }
  675. /* READY thread, insert to ready queue */
  676. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  677. /* there is no time slices left(YIELD), inserting thread before ready list*/
  678. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  679. {
  680. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  681. &(thread->tlist));
  682. }
  683. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  684. else
  685. {
  686. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  687. &(thread->tlist));
  688. }
  689. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  690. RT_NAME_MAX, thread->name, thread->current_priority));
  691. /* set priority mask */
  692. #if RT_THREAD_PRIORITY_MAX > 32
  693. rt_thread_ready_table[thread->number] |= thread->high_mask;
  694. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  695. rt_thread_ready_priority_group |= thread->number_mask;
  696. __exit:
  697. /* enable interrupt */
  698. rt_hw_interrupt_enable(level);
  699. }
  700. #endif /* RT_USING_SMP */
  701. /**
  702. * @brief This function will remove a thread from system ready queue.
  703. *
  704. * @param thread is the thread to be removed.
  705. *
  706. * @note Please do not invoke this function in user application.
  707. */
  708. #ifdef RT_USING_SMP
  709. void rt_schedule_remove_thread(struct rt_thread *thread)
  710. {
  711. rt_base_t level;
  712. RT_ASSERT(thread != RT_NULL);
  713. /* disable interrupt */
  714. level = rt_hw_interrupt_disable();
  715. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  716. RT_NAME_MAX, thread->name,
  717. thread->current_priority));
  718. /* remove thread from ready list */
  719. rt_list_remove(&(thread->tlist));
  720. if (thread->bind_cpu == RT_CPUS_NR)
  721. {
  722. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  723. {
  724. #if RT_THREAD_PRIORITY_MAX > 32
  725. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  726. if (rt_thread_ready_table[thread->number] == 0)
  727. {
  728. rt_thread_ready_priority_group &= ~thread->number_mask;
  729. }
  730. #else
  731. rt_thread_ready_priority_group &= ~thread->number_mask;
  732. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  733. }
  734. }
  735. else
  736. {
  737. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  738. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  739. {
  740. #if RT_THREAD_PRIORITY_MAX > 32
  741. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  742. if (pcpu->ready_table[thread->number] == 0)
  743. {
  744. pcpu->priority_group &= ~thread->number_mask;
  745. }
  746. #else
  747. pcpu->priority_group &= ~thread->number_mask;
  748. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  749. }
  750. }
  751. /* enable interrupt */
  752. rt_hw_interrupt_enable(level);
  753. }
  754. #else
  755. void rt_schedule_remove_thread(struct rt_thread *thread)
  756. {
  757. rt_base_t level;
  758. RT_ASSERT(thread != RT_NULL);
  759. /* disable interrupt */
  760. level = rt_hw_interrupt_disable();
  761. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  762. RT_NAME_MAX, thread->name,
  763. thread->current_priority));
  764. /* remove thread from ready list */
  765. rt_list_remove(&(thread->tlist));
  766. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  767. {
  768. #if RT_THREAD_PRIORITY_MAX > 32
  769. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  770. if (rt_thread_ready_table[thread->number] == 0)
  771. {
  772. rt_thread_ready_priority_group &= ~thread->number_mask;
  773. }
  774. #else
  775. rt_thread_ready_priority_group &= ~thread->number_mask;
  776. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  777. }
  778. /* enable interrupt */
  779. rt_hw_interrupt_enable(level);
  780. }
  781. #endif /* RT_USING_SMP */
  782. /**
  783. * @brief This function will lock the thread scheduler.
  784. */
  785. #ifdef RT_USING_SMP
  786. void rt_enter_critical(void)
  787. {
  788. rt_base_t level;
  789. struct rt_thread *current_thread;
  790. /* disable interrupt */
  791. level = rt_hw_local_irq_disable();
  792. current_thread = rt_cpu_self()->current_thread;
  793. if (!current_thread)
  794. {
  795. rt_hw_local_irq_enable(level);
  796. return;
  797. }
  798. /*
  799. * the maximal number of nest is RT_UINT16_MAX, which is big
  800. * enough and does not check here
  801. */
  802. {
  803. rt_uint16_t lock_nest = current_thread->cpus_lock_nest;
  804. current_thread->cpus_lock_nest++;
  805. RT_ASSERT(current_thread->cpus_lock_nest != 0);
  806. if (lock_nest == 0)
  807. {
  808. current_thread->scheduler_lock_nest ++;
  809. rt_hw_spin_lock(&_cpus_lock);
  810. }
  811. }
  812. /* critical for local cpu */
  813. current_thread->critical_lock_nest ++;
  814. /* lock scheduler for local cpu */
  815. current_thread->scheduler_lock_nest ++;
  816. /* enable interrupt */
  817. rt_hw_local_irq_enable(level);
  818. }
  819. #else
  820. void rt_enter_critical(void)
  821. {
  822. rt_base_t level;
  823. /* disable interrupt */
  824. level = rt_hw_interrupt_disable();
  825. /*
  826. * the maximal number of nest is RT_UINT16_MAX, which is big
  827. * enough and does not check here
  828. */
  829. rt_scheduler_lock_nest ++;
  830. /* enable interrupt */
  831. rt_hw_interrupt_enable(level);
  832. }
  833. #endif /* RT_USING_SMP */
  834. RTM_EXPORT(rt_enter_critical);
  835. /**
  836. * @brief This function will unlock the thread scheduler.
  837. */
  838. #ifdef RT_USING_SMP
  839. void rt_exit_critical(void)
  840. {
  841. rt_base_t level;
  842. struct rt_thread *current_thread;
  843. /* disable interrupt */
  844. level = rt_hw_local_irq_disable();
  845. current_thread = rt_cpu_self()->current_thread;
  846. if (!current_thread)
  847. {
  848. rt_hw_local_irq_enable(level);
  849. return;
  850. }
  851. current_thread->scheduler_lock_nest --;
  852. current_thread->critical_lock_nest --;
  853. RT_ASSERT(current_thread->cpus_lock_nest > 0);
  854. current_thread->cpus_lock_nest--;
  855. if (current_thread->cpus_lock_nest == 0)
  856. {
  857. current_thread->scheduler_lock_nest --;
  858. rt_hw_spin_unlock(&_cpus_lock);
  859. }
  860. if (current_thread->scheduler_lock_nest <= 0)
  861. {
  862. current_thread->scheduler_lock_nest = 0;
  863. /* enable interrupt */
  864. rt_hw_local_irq_enable(level);
  865. rt_schedule();
  866. }
  867. else
  868. {
  869. /* enable interrupt */
  870. rt_hw_local_irq_enable(level);
  871. }
  872. }
  873. #else
  874. void rt_exit_critical(void)
  875. {
  876. rt_base_t level;
  877. /* disable interrupt */
  878. level = rt_hw_interrupt_disable();
  879. rt_scheduler_lock_nest --;
  880. if (rt_scheduler_lock_nest <= 0)
  881. {
  882. rt_scheduler_lock_nest = 0;
  883. /* enable interrupt */
  884. rt_hw_interrupt_enable(level);
  885. if (rt_current_thread)
  886. {
  887. /* if scheduler is started, do a schedule */
  888. rt_schedule();
  889. }
  890. }
  891. else
  892. {
  893. /* enable interrupt */
  894. rt_hw_interrupt_enable(level);
  895. }
  896. }
  897. #endif /* RT_USING_SMP */
  898. RTM_EXPORT(rt_exit_critical);
  899. /**
  900. * @brief Get the scheduler lock level.
  901. *
  902. * @return the level of the scheduler lock. 0 means unlocked.
  903. */
  904. rt_uint16_t rt_critical_level(void)
  905. {
  906. #ifdef RT_USING_SMP
  907. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  908. return current_thread->critical_lock_nest;
  909. #else
  910. return rt_scheduler_lock_nest;
  911. #endif /* RT_USING_SMP */
  912. }
  913. RTM_EXPORT(rt_critical_level);
  914. /**@}*/