scheduler.c 30 KB

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