scheduler.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 _scheduler_get_highest_priority_thread to find highest priority task
  27. * rt_schedule_insert_thread won't insert current task to ready queue
  28. * in smp version, rt_hw_context_switch_interrupt maybe switch to
  29. * new task directly
  30. *
  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. #ifdef RT_USING_HOOK
  47. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  48. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  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. void
  65. rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  66. {
  67. rt_scheduler_switch_hook = hook;
  68. }
  69. /**@}*/
  70. #endif /* RT_USING_HOOK */
  71. #ifdef RT_USING_OVERFLOW_CHECK
  72. static void _rt_scheduler_stack_check(struct rt_thread *thread)
  73. {
  74. RT_ASSERT(thread != RT_NULL);
  75. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  76. if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
  77. #else
  78. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
  79. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  80. (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
  81. (rt_ubase_t)thread->sp >
  82. (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
  83. {
  84. rt_ubase_t level;
  85. rt_kprintf("thread:%s stack overflow\n", thread->name);
  86. level = rt_hw_interrupt_disable();
  87. while (level);
  88. }
  89. #ifdef 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 /* ARCH_CPU_STACK_GROWS_UPWARD */
  102. }
  103. #endif /* RT_USING_OVERFLOW_CHECK */
  104. /*
  105. * get the highest priority thread in ready queue
  106. */
  107. #ifdef RT_USING_SMP
  108. static struct rt_thread* _scheduler_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 /* RT_THREAD_PRIORITY_MAX > 32 */
  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* _scheduler_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 /* RT_THREAD_PRIORITY_MAX > 32 */
  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 /* RT_USING_SMP */
  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 /* RT_THREAD_PRIORITY_MAX > 32 */
  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 /* RT_THREAD_PRIORITY_MAX > 32 */
  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 = _scheduler_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_STAT_MASK) == RT_THREAD_SUSPEND)
  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. rt_thread_resume(current_thread);
  276. }
  277. }
  278. #endif /* RT_USING_SIGNALS */
  279. if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */
  280. {
  281. rt_ubase_t highest_ready_priority;
  282. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  283. {
  284. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  285. current_thread->oncpu = RT_CPU_DETACHED;
  286. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  287. {
  288. if (current_thread->current_priority < highest_ready_priority)
  289. {
  290. to_thread = current_thread;
  291. }
  292. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  293. {
  294. to_thread = current_thread;
  295. }
  296. else
  297. {
  298. rt_schedule_insert_thread(current_thread);
  299. }
  300. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  301. }
  302. to_thread->oncpu = cpu_id;
  303. if (to_thread != current_thread)
  304. {
  305. /* if the destination thread is not the same as current thread */
  306. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  307. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  308. rt_schedule_remove_thread(to_thread);
  309. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  310. /* switch to new thread */
  311. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  312. ("[%d]switch to priority#%d "
  313. "thread:%.*s(sp:0x%08x), "
  314. "from thread:%.*s(sp: 0x%08x)\n",
  315. pcpu->irq_nest, highest_ready_priority,
  316. RT_NAME_MAX, to_thread->name, to_thread->sp,
  317. RT_NAME_MAX, current_thread->name, current_thread->sp));
  318. #ifdef RT_USING_OVERFLOW_CHECK
  319. _rt_scheduler_stack_check(to_thread);
  320. #endif /* RT_USING_OVERFLOW_CHECK */
  321. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  322. rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
  323. (rt_ubase_t)&to_thread->sp, to_thread);
  324. }
  325. }
  326. }
  327. /* enable interrupt */
  328. rt_hw_interrupt_enable(level);
  329. #ifdef RT_USING_SIGNALS
  330. /* check stat of thread for signal */
  331. level = rt_hw_interrupt_disable();
  332. if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  333. {
  334. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  335. current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  336. rt_hw_interrupt_enable(level);
  337. /* check signal status */
  338. rt_thread_handle_sig(RT_TRUE);
  339. }
  340. else
  341. {
  342. rt_hw_interrupt_enable(level);
  343. }
  344. #endif /* RT_USING_SIGNALS */
  345. __exit:
  346. return ;
  347. }
  348. #else
  349. /**
  350. * This function will perform one schedule. It will select one thread
  351. * with the highest priority level, and switch to it immediately.
  352. */
  353. void rt_schedule(void)
  354. {
  355. rt_base_t level;
  356. struct rt_thread *to_thread;
  357. struct rt_thread *from_thread;
  358. /* disable interrupt */
  359. level = rt_hw_interrupt_disable();
  360. /* check the scheduler is enabled or not */
  361. if (rt_scheduler_lock_nest == 0)
  362. {
  363. rt_ubase_t highest_ready_priority;
  364. if (rt_thread_ready_priority_group != 0)
  365. {
  366. /* need_insert_from_thread: need to insert from_thread to ready queue */
  367. int need_insert_from_thread = 0;
  368. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  369. if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  370. {
  371. if (rt_current_thread->current_priority < highest_ready_priority)
  372. {
  373. to_thread = rt_current_thread;
  374. }
  375. else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  376. {
  377. to_thread = rt_current_thread;
  378. }
  379. else
  380. {
  381. need_insert_from_thread = 1;
  382. }
  383. rt_current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  384. }
  385. if (to_thread != rt_current_thread)
  386. {
  387. /* if the destination thread is not the same as current thread */
  388. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  389. from_thread = rt_current_thread;
  390. rt_current_thread = to_thread;
  391. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  392. if (need_insert_from_thread)
  393. {
  394. rt_schedule_insert_thread(from_thread);
  395. }
  396. rt_schedule_remove_thread(to_thread);
  397. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  398. /* switch to new thread */
  399. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  400. ("[%d]switch to priority#%d "
  401. "thread:%.*s(sp:0x%08x), "
  402. "from thread:%.*s(sp: 0x%08x)\n",
  403. rt_interrupt_nest, highest_ready_priority,
  404. RT_NAME_MAX, to_thread->name, to_thread->sp,
  405. RT_NAME_MAX, from_thread->name, from_thread->sp));
  406. #ifdef RT_USING_OVERFLOW_CHECK
  407. _rt_scheduler_stack_check(to_thread);
  408. #endif /* RT_USING_OVERFLOW_CHECK */
  409. if (rt_interrupt_nest == 0)
  410. {
  411. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  412. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
  413. rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  414. (rt_ubase_t)&to_thread->sp);
  415. /* enable interrupt */
  416. rt_hw_interrupt_enable(level);
  417. #ifdef RT_USING_SIGNALS
  418. /* check stat of thread for signal */
  419. level = rt_hw_interrupt_disable();
  420. if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  421. {
  422. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  423. rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  424. rt_hw_interrupt_enable(level);
  425. /* check signal status */
  426. rt_thread_handle_sig(RT_TRUE);
  427. }
  428. else
  429. {
  430. rt_hw_interrupt_enable(level);
  431. }
  432. #endif /* RT_USING_SIGNALS */
  433. goto __exit;
  434. }
  435. else
  436. {
  437. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  438. rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  439. (rt_ubase_t)&to_thread->sp);
  440. }
  441. }
  442. else
  443. {
  444. rt_schedule_remove_thread(rt_current_thread);
  445. rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
  446. }
  447. }
  448. }
  449. /* enable interrupt */
  450. rt_hw_interrupt_enable(level);
  451. __exit:
  452. return;
  453. }
  454. #endif /* RT_USING_SMP */
  455. /**
  456. * This function checks if a scheduling is needed after IRQ context. If yes,
  457. * it will select one thread with the highest priority level, and then switch
  458. * to it.
  459. */
  460. #ifdef RT_USING_SMP
  461. void rt_scheduler_do_irq_switch(void *context)
  462. {
  463. int cpu_id;
  464. rt_base_t level;
  465. struct rt_cpu* pcpu;
  466. struct rt_thread *to_thread;
  467. struct rt_thread *current_thread;
  468. level = rt_hw_interrupt_disable();
  469. cpu_id = rt_hw_cpu_id();
  470. pcpu = rt_cpu_index(cpu_id);
  471. current_thread = pcpu->current_thread;
  472. #ifdef RT_USING_SIGNALS
  473. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)
  474. {
  475. /* if current_thread signal is in pending */
  476. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  477. {
  478. rt_thread_resume(current_thread);
  479. }
  480. }
  481. #endif /* RT_USING_SIGNALS */
  482. if (pcpu->irq_switch_flag == 0)
  483. {
  484. rt_hw_interrupt_enable(level);
  485. return;
  486. }
  487. if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0)
  488. {
  489. rt_ubase_t highest_ready_priority;
  490. /* clear irq switch flag */
  491. pcpu->irq_switch_flag = 0;
  492. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  493. {
  494. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  495. current_thread->oncpu = RT_CPU_DETACHED;
  496. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  497. {
  498. if (current_thread->current_priority < highest_ready_priority)
  499. {
  500. to_thread = current_thread;
  501. }
  502. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  503. {
  504. to_thread = current_thread;
  505. }
  506. else
  507. {
  508. rt_schedule_insert_thread(current_thread);
  509. }
  510. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  511. }
  512. to_thread->oncpu = cpu_id;
  513. if (to_thread != current_thread)
  514. {
  515. /* if the destination thread is not the same as current thread */
  516. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  517. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  518. rt_schedule_remove_thread(to_thread);
  519. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  520. #ifdef RT_USING_OVERFLOW_CHECK
  521. _rt_scheduler_stack_check(to_thread);
  522. #endif /* RT_USING_OVERFLOW_CHECK */
  523. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  524. current_thread->cpus_lock_nest--;
  525. current_thread->scheduler_lock_nest--;
  526. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  527. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  528. (rt_ubase_t)&to_thread->sp, to_thread);
  529. }
  530. }
  531. }
  532. rt_hw_interrupt_enable(level);
  533. }
  534. #endif /* RT_USING_SMP */
  535. /*
  536. * This function will insert a thread to system ready queue. The state of
  537. * thread will be set as READY and remove from suspend queue.
  538. *
  539. * @param thread the thread to be inserted
  540. * @note Please do not invoke this function in user application.
  541. */
  542. #ifdef RT_USING_SMP
  543. void rt_schedule_insert_thread(struct rt_thread *thread)
  544. {
  545. int cpu_id;
  546. int bind_cpu;
  547. rt_uint32_t cpu_mask;
  548. register rt_base_t level;
  549. RT_ASSERT(thread != RT_NULL);
  550. /* disable interrupt */
  551. level = rt_hw_interrupt_disable();
  552. /* it should be RUNNING thread */
  553. if (thread->oncpu != RT_CPU_DETACHED)
  554. {
  555. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  556. goto __exit;
  557. }
  558. /* READY thread, insert to ready queue */
  559. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  560. cpu_id = rt_hw_cpu_id();
  561. bind_cpu = thread->bind_cpu ;
  562. /* insert thread to ready list */
  563. if (bind_cpu == RT_CPUS_NR)
  564. {
  565. #if RT_THREAD_PRIORITY_MAX > 32
  566. rt_thread_ready_table[thread->number] |= thread->high_mask;
  567. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  568. rt_thread_ready_priority_group |= thread->number_mask;
  569. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  570. &(thread->tlist));
  571. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  572. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  573. }
  574. else
  575. {
  576. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  577. #if RT_THREAD_PRIORITY_MAX > 32
  578. pcpu->ready_table[thread->number] |= thread->high_mask;
  579. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  580. pcpu->priority_group |= thread->number_mask;
  581. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  582. &(thread->tlist));
  583. if (cpu_id != bind_cpu)
  584. {
  585. cpu_mask = 1 << bind_cpu;
  586. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  587. }
  588. }
  589. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  590. RT_NAME_MAX, thread->name, thread->current_priority));
  591. __exit:
  592. /* enable interrupt */
  593. rt_hw_interrupt_enable(level);
  594. }
  595. #else
  596. void rt_schedule_insert_thread(struct rt_thread *thread)
  597. {
  598. register rt_base_t temp;
  599. RT_ASSERT(thread != RT_NULL);
  600. /* disable interrupt */
  601. temp = rt_hw_interrupt_disable();
  602. /* it's current thread, it should be RUNNING thread */
  603. if (thread == rt_current_thread)
  604. {
  605. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  606. goto __exit;
  607. }
  608. /* READY thread, insert to ready queue */
  609. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  610. /* insert thread to ready list */
  611. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  612. &(thread->tlist));
  613. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  614. RT_NAME_MAX, thread->name, thread->current_priority));
  615. /* set priority mask */
  616. #if RT_THREAD_PRIORITY_MAX > 32
  617. rt_thread_ready_table[thread->number] |= thread->high_mask;
  618. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  619. rt_thread_ready_priority_group |= thread->number_mask;
  620. __exit:
  621. /* enable interrupt */
  622. rt_hw_interrupt_enable(temp);
  623. }
  624. #endif /* RT_USING_SMP */
  625. /*
  626. * This function will remove a thread from system ready queue.
  627. *
  628. * @param thread the thread to be removed
  629. *
  630. * @note Please do not invoke this function in user application.
  631. */
  632. #ifdef RT_USING_SMP
  633. void rt_schedule_remove_thread(struct rt_thread *thread)
  634. {
  635. register rt_base_t level;
  636. RT_ASSERT(thread != RT_NULL);
  637. /* disable interrupt */
  638. level = rt_hw_interrupt_disable();
  639. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  640. RT_NAME_MAX, thread->name,
  641. thread->current_priority));
  642. /* remove thread from ready list */
  643. rt_list_remove(&(thread->tlist));
  644. if (thread->bind_cpu == RT_CPUS_NR)
  645. {
  646. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  647. {
  648. #if RT_THREAD_PRIORITY_MAX > 32
  649. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  650. if (rt_thread_ready_table[thread->number] == 0)
  651. {
  652. rt_thread_ready_priority_group &= ~thread->number_mask;
  653. }
  654. #else
  655. rt_thread_ready_priority_group &= ~thread->number_mask;
  656. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  657. }
  658. }
  659. else
  660. {
  661. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  662. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  663. {
  664. #if RT_THREAD_PRIORITY_MAX > 32
  665. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  666. if (pcpu->ready_table[thread->number] == 0)
  667. {
  668. pcpu->priority_group &= ~thread->number_mask;
  669. }
  670. #else
  671. pcpu->priority_group &= ~thread->number_mask;
  672. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  673. }
  674. }
  675. /* enable interrupt */
  676. rt_hw_interrupt_enable(level);
  677. }
  678. #else
  679. void rt_schedule_remove_thread(struct rt_thread *thread)
  680. {
  681. register rt_base_t level;
  682. RT_ASSERT(thread != RT_NULL);
  683. /* disable interrupt */
  684. level = rt_hw_interrupt_disable();
  685. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  686. RT_NAME_MAX, thread->name,
  687. thread->current_priority));
  688. /* remove thread from ready list */
  689. rt_list_remove(&(thread->tlist));
  690. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  691. {
  692. #if RT_THREAD_PRIORITY_MAX > 32
  693. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  694. if (rt_thread_ready_table[thread->number] == 0)
  695. {
  696. rt_thread_ready_priority_group &= ~thread->number_mask;
  697. }
  698. #else
  699. rt_thread_ready_priority_group &= ~thread->number_mask;
  700. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  701. }
  702. /* enable interrupt */
  703. rt_hw_interrupt_enable(level);
  704. }
  705. #endif /* RT_USING_SMP */
  706. /**
  707. * This function will lock the thread scheduler.
  708. */
  709. #ifdef RT_USING_SMP
  710. void rt_enter_critical(void)
  711. {
  712. register rt_base_t level;
  713. struct rt_thread *current_thread;
  714. /* disable interrupt */
  715. level = rt_hw_local_irq_disable();
  716. current_thread = rt_cpu_self()->current_thread;
  717. if (!current_thread)
  718. {
  719. rt_hw_local_irq_enable(level);
  720. return;
  721. }
  722. /*
  723. * the maximal number of nest is RT_UINT16_MAX, which is big
  724. * enough and does not check here
  725. */
  726. {
  727. register rt_uint16_t lock_nest = current_thread->cpus_lock_nest;
  728. current_thread->cpus_lock_nest++;
  729. if (lock_nest == 0)
  730. {
  731. current_thread->scheduler_lock_nest ++;
  732. rt_hw_spin_lock(&_cpus_lock);
  733. }
  734. }
  735. /* critical for local cpu */
  736. current_thread->critical_lock_nest ++;
  737. /* lock scheduler for local cpu */
  738. current_thread->scheduler_lock_nest ++;
  739. /* enable interrupt */
  740. rt_hw_local_irq_enable(level);
  741. }
  742. #else
  743. void rt_enter_critical(void)
  744. {
  745. register rt_base_t level;
  746. /* disable interrupt */
  747. level = rt_hw_interrupt_disable();
  748. /*
  749. * the maximal number of nest is RT_UINT16_MAX, which is big
  750. * enough and does not check here
  751. */
  752. rt_scheduler_lock_nest ++;
  753. /* enable interrupt */
  754. rt_hw_interrupt_enable(level);
  755. }
  756. #endif /* RT_USING_SMP */
  757. RTM_EXPORT(rt_enter_critical);
  758. /**
  759. * This function will unlock the thread scheduler.
  760. */
  761. #ifdef RT_USING_SMP
  762. void rt_exit_critical(void)
  763. {
  764. register rt_base_t level;
  765. struct rt_thread *current_thread;
  766. /* disable interrupt */
  767. level = rt_hw_local_irq_disable();
  768. current_thread = rt_cpu_self()->current_thread;
  769. if (!current_thread)
  770. {
  771. rt_hw_local_irq_enable(level);
  772. return;
  773. }
  774. current_thread->scheduler_lock_nest --;
  775. current_thread->critical_lock_nest --;
  776. current_thread->cpus_lock_nest--;
  777. if (current_thread->cpus_lock_nest == 0)
  778. {
  779. current_thread->scheduler_lock_nest --;
  780. rt_hw_spin_unlock(&_cpus_lock);
  781. }
  782. if (current_thread->scheduler_lock_nest <= 0)
  783. {
  784. current_thread->scheduler_lock_nest = 0;
  785. /* enable interrupt */
  786. rt_hw_local_irq_enable(level);
  787. rt_schedule();
  788. }
  789. else
  790. {
  791. /* enable interrupt */
  792. rt_hw_local_irq_enable(level);
  793. }
  794. }
  795. #else
  796. void rt_exit_critical(void)
  797. {
  798. register rt_base_t level;
  799. /* disable interrupt */
  800. level = rt_hw_interrupt_disable();
  801. rt_scheduler_lock_nest --;
  802. if (rt_scheduler_lock_nest <= 0)
  803. {
  804. rt_scheduler_lock_nest = 0;
  805. /* enable interrupt */
  806. rt_hw_interrupt_enable(level);
  807. if (rt_current_thread)
  808. {
  809. /* if scheduler is started, do a schedule */
  810. rt_schedule();
  811. }
  812. }
  813. else
  814. {
  815. /* enable interrupt */
  816. rt_hw_interrupt_enable(level);
  817. }
  818. }
  819. #endif /* RT_USING_SMP */
  820. RTM_EXPORT(rt_exit_critical);
  821. /**
  822. * Get the scheduler lock level
  823. *
  824. * @return the level of the scheduler lock. 0 means unlocked.
  825. */
  826. rt_uint16_t rt_critical_level(void)
  827. {
  828. #ifdef RT_USING_SMP
  829. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  830. return current_thread->critical_lock_nest;
  831. #else
  832. return rt_scheduler_lock_nest;
  833. #endif /* RT_USING_SMP */
  834. }
  835. RTM_EXPORT(rt_critical_level);
  836. /**@}*/