scheduler.c 26 KB

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