scheduler.c 29 KB

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