scheduler.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  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. /* need_insert_from_thread: need to insert from_thread to ready queue */
  355. int need_insert_from_thread = 0;
  356. to_thread = _get_highest_priority_thread(&highest_ready_priority);
  357. if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  358. {
  359. if (rt_current_thread->current_priority < highest_ready_priority)
  360. {
  361. to_thread = rt_current_thread;
  362. }
  363. else
  364. {
  365. need_insert_from_thread = 1;
  366. }
  367. }
  368. if (to_thread != rt_current_thread)
  369. {
  370. /* if the destination thread is not the same as current thread */
  371. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  372. from_thread = rt_current_thread;
  373. rt_current_thread = to_thread;
  374. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  375. if (need_insert_from_thread)
  376. {
  377. rt_schedule_insert_thread(from_thread);
  378. }
  379. rt_schedule_remove_thread(to_thread);
  380. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  381. /* switch to new thread */
  382. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  383. ("[%d]switch to priority#%d "
  384. "thread:%.*s(sp:0x%08x), "
  385. "from thread:%.*s(sp: 0x%08x)\n",
  386. rt_interrupt_nest, highest_ready_priority,
  387. RT_NAME_MAX, to_thread->name, to_thread->sp,
  388. RT_NAME_MAX, from_thread->name, from_thread->sp));
  389. #ifdef RT_USING_OVERFLOW_CHECK
  390. _rt_scheduler_stack_check(to_thread);
  391. #endif
  392. if (rt_interrupt_nest == 0)
  393. {
  394. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  395. rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  396. (rt_ubase_t)&to_thread->sp);
  397. /* enable interrupt */
  398. rt_hw_interrupt_enable(level);
  399. #ifdef RT_USING_SIGNALS
  400. /* check signal status */
  401. rt_thread_handle_sig(RT_TRUE);
  402. #endif
  403. goto __exit;
  404. }
  405. else
  406. {
  407. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  408. rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  409. (rt_ubase_t)&to_thread->sp);
  410. }
  411. }
  412. else
  413. {
  414. rt_schedule_remove_thread(rt_current_thread);
  415. rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
  416. }
  417. }
  418. }
  419. /* enable interrupt */
  420. rt_hw_interrupt_enable(level);
  421. __exit:
  422. return;
  423. }
  424. #endif /*RT_USING_SMP*/
  425. /**
  426. * This function checks if a scheduling is needed after IRQ context. If yes,
  427. * it will select one thread with the highest priority level, and then switch
  428. * to it.
  429. */
  430. #ifdef RT_USING_SMP
  431. void rt_scheduler_do_irq_switch(void *context)
  432. {
  433. int cpu_id;
  434. rt_base_t level;
  435. struct rt_cpu* pcpu;
  436. struct rt_thread *to_thread;
  437. struct rt_thread *current_thread;
  438. level = rt_hw_interrupt_disable();
  439. cpu_id = rt_hw_cpu_id();
  440. pcpu = rt_cpu_index(cpu_id);
  441. current_thread = pcpu->current_thread;
  442. if (pcpu->irq_switch_flag == 0)
  443. {
  444. rt_hw_interrupt_enable(level);
  445. return;
  446. }
  447. if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0)
  448. {
  449. rt_ubase_t highest_ready_priority;
  450. /* clear irq switch flag */
  451. pcpu->irq_switch_flag = 0;
  452. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  453. {
  454. to_thread = _get_highest_priority_thread(&highest_ready_priority);
  455. current_thread->oncpu = RT_CPU_DETACHED;
  456. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  457. {
  458. if (current_thread->current_priority < highest_ready_priority)
  459. {
  460. to_thread = current_thread;
  461. }
  462. else
  463. {
  464. rt_schedule_insert_thread(current_thread);
  465. }
  466. }
  467. to_thread->oncpu = cpu_id;
  468. if (to_thread != current_thread)
  469. {
  470. /* if the destination thread is not the same as current thread */
  471. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  472. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  473. rt_schedule_remove_thread(to_thread);
  474. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  475. #ifdef RT_USING_OVERFLOW_CHECK
  476. _rt_scheduler_stack_check(to_thread);
  477. #endif
  478. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  479. current_thread->cpus_lock_nest--;
  480. current_thread->scheduler_lock_nest--;
  481. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  482. (rt_ubase_t)&to_thread->sp, to_thread);
  483. }
  484. }
  485. }
  486. rt_hw_interrupt_enable(level);
  487. }
  488. #endif /*RT_USING_SMP*/
  489. /*
  490. * This function will insert a thread to system ready queue. The state of
  491. * thread will be set as READY and remove from suspend queue.
  492. *
  493. * @param thread the thread to be inserted
  494. * @note Please do not invoke this function in user application.
  495. */
  496. #ifdef RT_USING_SMP
  497. void rt_schedule_insert_thread(struct rt_thread *thread)
  498. {
  499. int cpu_id;
  500. int bind_cpu;
  501. rt_uint32_t cpu_mask;
  502. register rt_base_t level;
  503. RT_ASSERT(thread != RT_NULL);
  504. /* disable interrupt */
  505. level = rt_hw_interrupt_disable();
  506. /* it should be RUNNING thread */
  507. if (thread->oncpu != RT_CPU_DETACHED)
  508. {
  509. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  510. goto __exit;
  511. }
  512. /* READY thread, insert to ready queue */
  513. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  514. cpu_id = rt_hw_cpu_id();
  515. bind_cpu = thread->bind_cpu ;
  516. /* insert thread to ready list */
  517. if (bind_cpu == RT_CPUS_NR)
  518. {
  519. #if RT_THREAD_PRIORITY_MAX > 32
  520. rt_thread_ready_table[thread->number] |= thread->high_mask;
  521. #endif
  522. rt_thread_ready_priority_group |= thread->number_mask;
  523. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  524. &(thread->tlist));
  525. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  526. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  527. }
  528. else
  529. {
  530. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  531. #if RT_THREAD_PRIORITY_MAX > 32
  532. pcpu->ready_table[thread->number] |= thread->high_mask;
  533. #endif
  534. pcpu->priority_group |= thread->number_mask;
  535. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  536. &(thread->tlist));
  537. if (cpu_id != bind_cpu)
  538. {
  539. cpu_mask = 1 << bind_cpu;
  540. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  541. }
  542. }
  543. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  544. RT_NAME_MAX, thread->name, thread->current_priority));
  545. __exit:
  546. /* enable interrupt */
  547. rt_hw_interrupt_enable(level);
  548. }
  549. #else
  550. void rt_schedule_insert_thread(struct rt_thread *thread)
  551. {
  552. register rt_base_t temp;
  553. RT_ASSERT(thread != RT_NULL);
  554. /* disable interrupt */
  555. temp = rt_hw_interrupt_disable();
  556. /* it's current thread, it should be RUNNING thread */
  557. if (thread == rt_current_thread)
  558. {
  559. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  560. goto __exit;
  561. }
  562. /* READY thread, insert to ready queue */
  563. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  564. /* insert thread to ready list */
  565. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  566. &(thread->tlist));
  567. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  568. RT_NAME_MAX, thread->name, thread->current_priority));
  569. /* set priority mask */
  570. #if RT_THREAD_PRIORITY_MAX > 32
  571. rt_thread_ready_table[thread->number] |= thread->high_mask;
  572. #endif
  573. rt_thread_ready_priority_group |= thread->number_mask;
  574. __exit:
  575. /* enable interrupt */
  576. rt_hw_interrupt_enable(temp);
  577. }
  578. #endif /*RT_USING_SMP*/
  579. /*
  580. * This function will remove a thread from system ready queue.
  581. *
  582. * @param thread the thread to be removed
  583. *
  584. * @note Please do not invoke this function in user application.
  585. */
  586. #ifdef RT_USING_SMP
  587. void rt_schedule_remove_thread(struct rt_thread *thread)
  588. {
  589. register rt_base_t level;
  590. RT_ASSERT(thread != RT_NULL);
  591. /* disable interrupt */
  592. level = rt_hw_interrupt_disable();
  593. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  594. RT_NAME_MAX, thread->name,
  595. thread->current_priority));
  596. /* remove thread from ready list */
  597. rt_list_remove(&(thread->tlist));
  598. if (thread->bind_cpu == RT_CPUS_NR)
  599. {
  600. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  601. {
  602. #if RT_THREAD_PRIORITY_MAX > 32
  603. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  604. if (rt_thread_ready_table[thread->number] == 0)
  605. {
  606. rt_thread_ready_priority_group &= ~thread->number_mask;
  607. }
  608. #else
  609. rt_thread_ready_priority_group &= ~thread->number_mask;
  610. #endif
  611. }
  612. }
  613. else
  614. {
  615. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  616. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  617. {
  618. #if RT_THREAD_PRIORITY_MAX > 32
  619. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  620. if (rt_thread_ready_table[thread->number] == 0)
  621. {
  622. pcpu->priority_group &= ~thread->number_mask;
  623. }
  624. #else
  625. pcpu->priority_group &= ~thread->number_mask;
  626. #endif
  627. }
  628. }
  629. /* enable interrupt */
  630. rt_hw_interrupt_enable(level);
  631. }
  632. #else
  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 (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  645. {
  646. #if RT_THREAD_PRIORITY_MAX > 32
  647. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  648. if (rt_thread_ready_table[thread->number] == 0)
  649. {
  650. rt_thread_ready_priority_group &= ~thread->number_mask;
  651. }
  652. #else
  653. rt_thread_ready_priority_group &= ~thread->number_mask;
  654. #endif
  655. }
  656. /* enable interrupt */
  657. rt_hw_interrupt_enable(level);
  658. }
  659. #endif /*RT_USING_SMP*/
  660. /**
  661. * This function will lock the thread scheduler.
  662. */
  663. #ifdef RT_USING_SMP
  664. void rt_enter_critical(void)
  665. {
  666. register rt_base_t level;
  667. struct rt_thread *current_thread;
  668. /* disable interrupt */
  669. level = rt_hw_local_irq_disable();
  670. current_thread = rt_cpu_self()->current_thread;
  671. /*
  672. * the maximal number of nest is RT_UINT16_MAX, which is big
  673. * enough and does not check here
  674. */
  675. /* lock scheduler for all cpus */
  676. if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest)
  677. {
  678. rt_hw_spin_lock(&_rt_critical_lock);
  679. }
  680. /* lock scheduler for local cpu */
  681. current_thread->scheduler_lock_nest ++;
  682. /* enable interrupt */
  683. rt_hw_local_irq_enable(level);
  684. }
  685. #else
  686. void rt_enter_critical(void)
  687. {
  688. register rt_base_t level;
  689. /* disable interrupt */
  690. level = rt_hw_interrupt_disable();
  691. /*
  692. * the maximal number of nest is RT_UINT16_MAX, which is big
  693. * enough and does not check here
  694. */
  695. rt_scheduler_lock_nest ++;
  696. /* enable interrupt */
  697. rt_hw_interrupt_enable(level);
  698. }
  699. #endif /*RT_USING_SMP*/
  700. RTM_EXPORT(rt_enter_critical);
  701. /**
  702. * This function will unlock the thread scheduler.
  703. */
  704. #ifdef RT_USING_SMP
  705. void rt_exit_critical(void)
  706. {
  707. register rt_base_t level;
  708. struct rt_thread *current_thread;
  709. /* disable interrupt */
  710. level = rt_hw_local_irq_disable();
  711. current_thread = rt_cpu_self()->current_thread;
  712. current_thread->scheduler_lock_nest --;
  713. if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest)
  714. {
  715. rt_hw_spin_unlock(&_rt_critical_lock);
  716. }
  717. if (current_thread->scheduler_lock_nest <= 0)
  718. {
  719. current_thread->scheduler_lock_nest = 0;
  720. /* enable interrupt */
  721. rt_hw_local_irq_enable(level);
  722. rt_schedule();
  723. }
  724. else
  725. {
  726. /* enable interrupt */
  727. rt_hw_local_irq_enable(level);
  728. }
  729. }
  730. #else
  731. void rt_exit_critical(void)
  732. {
  733. register rt_base_t level;
  734. /* disable interrupt */
  735. level = rt_hw_interrupt_disable();
  736. rt_scheduler_lock_nest --;
  737. if (rt_scheduler_lock_nest <= 0)
  738. {
  739. rt_scheduler_lock_nest = 0;
  740. /* enable interrupt */
  741. rt_hw_interrupt_enable(level);
  742. rt_schedule();
  743. }
  744. else
  745. {
  746. /* enable interrupt */
  747. rt_hw_interrupt_enable(level);
  748. }
  749. }
  750. #endif /*RT_USING_SMP*/
  751. RTM_EXPORT(rt_exit_critical);
  752. /**
  753. * Get the scheduler lock level
  754. *
  755. * @return the level of the scheduler lock. 0 means unlocked.
  756. */
  757. rt_uint16_t rt_critical_level(void)
  758. {
  759. #ifdef RT_USING_SMP
  760. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  761. return current_thread->scheduler_lock_nest;
  762. #else
  763. return rt_scheduler_lock_nest;
  764. #endif /*RT_USING_SMP*/
  765. }
  766. RTM_EXPORT(rt_critical_level);
  767. /**@}*/