scheduler.c 25 KB

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