scheduler.c 26 KB

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