scheduler.c 30 KB

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