scheduler.c 28 KB

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