scheduler.c 29 KB

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