scheduler_up.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * Copyright (c) 2006-2025 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 _scheduler_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. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to scheduler.c
  31. * 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c
  32. * 2023-10-17 ChuShicheng Modify the timing of clearing RT_THREAD_STAT_YIELD flag bits
  33. */
  34. #define __RT_IPC_SOURCE__
  35. #include <rtthread.h>
  36. #include <rthw.h>
  37. #define DBG_TAG "kernel.scheduler"
  38. #define DBG_LVL DBG_INFO
  39. #include <rtdbg.h>
  40. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  41. rt_uint32_t rt_thread_ready_priority_group;
  42. #if RT_THREAD_PRIORITY_MAX > 32
  43. /* Maximum priority level, 256 */
  44. rt_uint8_t rt_thread_ready_table[32];
  45. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  46. extern volatile rt_atomic_t rt_interrupt_nest;
  47. static rt_int16_t rt_scheduler_lock_nest;
  48. rt_uint8_t rt_current_priority;
  49. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  50. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  51. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  52. /**
  53. * @addtogroup group_Hook
  54. */
  55. /**@{*/
  56. /**
  57. * @brief This function will set a hook function, which will be invoked when thread
  58. * switch happens.
  59. *
  60. * @param hook is the hook function.
  61. */
  62. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  63. {
  64. rt_scheduler_hook = hook;
  65. }
  66. /**
  67. * @brief This function will set a hook function, which will be invoked when context
  68. * switch happens.
  69. *
  70. * @param hook is the hook function.
  71. */
  72. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  73. {
  74. rt_scheduler_switch_hook = hook;
  75. }
  76. /**@}*/
  77. #endif /* RT_USING_HOOK */
  78. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  79. {
  80. struct rt_thread *highest_priority_thread;
  81. rt_ubase_t highest_ready_priority;
  82. #if RT_THREAD_PRIORITY_MAX > 32
  83. rt_ubase_t number;
  84. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  85. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  86. #else
  87. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  88. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  89. /* get highest ready priority thread */
  90. highest_priority_thread = RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[highest_ready_priority].next);
  91. *highest_prio = highest_ready_priority;
  92. return highest_priority_thread;
  93. }
  94. /**
  95. * @brief Lock the scheduler and save the interrupt level
  96. *
  97. * @param plvl Pointer to store the interrupt level before locking
  98. *
  99. * @return rt_err_t
  100. * - RT_EOK on success
  101. * - -RT_EINVAL if plvl is NULL
  102. *
  103. * @details This function:
  104. * - Disables interrupts to prevent preemption
  105. * - Saves the previous interrupt level in plvl
  106. * - Must be paired with rt_sched_unlock() to restore interrupts
  107. *
  108. * @note The lock is implemented by disabling interrupts
  109. * Caller must ensure plvl is valid
  110. */
  111. rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
  112. {
  113. rt_base_t level;
  114. if (!plvl)
  115. return -RT_EINVAL;
  116. level = rt_hw_interrupt_disable();
  117. *plvl = level;
  118. return RT_EOK;
  119. }
  120. /**
  121. * @brief Unlock the scheduler and restore the interrupt level
  122. *
  123. * @param level The interrupt level to restore (previously saved by rt_sched_lock)
  124. * @return rt_err_t Always returns RT_EOK
  125. *
  126. * @details This function:
  127. * - Restores the interrupt level that was saved when locking the scheduler
  128. * - Must be called to match each rt_sched_lock() call
  129. *
  130. * @note Must be called with the same interrupt level that was saved by rt_sched_lock()
  131. * Should not be called without a corresponding rt_sched_lock() first
  132. */
  133. rt_err_t rt_sched_unlock(rt_sched_lock_level_t level)
  134. {
  135. rt_hw_interrupt_enable(level);
  136. return RT_EOK;
  137. }
  138. /**
  139. * @brief Unlock scheduler and trigger a reschedule if needed
  140. *
  141. * @param level The interrupt level to restore (previously saved by rt_sched_lock)
  142. * @return rt_err_t Always returns RT_EOK
  143. *
  144. * @details This function:
  145. * - Restores the interrupt level that was saved when locking the scheduler
  146. * - Triggers a reschedule if the scheduler is available (rt_thread_self() != NULL)
  147. * - Combines the functionality of rt_sched_unlock() and rt_schedule()
  148. */
  149. rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
  150. {
  151. if (rt_thread_self())
  152. {
  153. /* if scheduler is available */
  154. rt_schedule();
  155. }
  156. rt_hw_interrupt_enable(level);
  157. return RT_EOK;
  158. }
  159. /**
  160. * @brief Initialize the system scheduler for single-core systems
  161. *
  162. * @details This function performs the following initialization tasks:
  163. * - Resets the scheduler lock nest counter to 0
  164. * - Initializes the priority table for all priority levels
  165. * - Clears the ready priority group bitmap
  166. * - For systems with >32 priority levels, initializes the ready table
  167. *
  168. * @note This function must be called before any thread scheduling can occur.
  169. * It prepares the scheduler data structures for single-core operation
  170. */
  171. void rt_system_scheduler_init(void)
  172. {
  173. rt_base_t offset;
  174. rt_scheduler_lock_nest = 0;
  175. LOG_D("start scheduler: max priority 0x%02x",
  176. RT_THREAD_PRIORITY_MAX);
  177. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  178. {
  179. rt_list_init(&rt_thread_priority_table[offset]);
  180. }
  181. /* initialize ready priority group */
  182. rt_thread_ready_priority_group = 0;
  183. #if RT_THREAD_PRIORITY_MAX > 32
  184. /* initialize ready table */
  185. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  186. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  187. }
  188. /**
  189. * @brief Start the system scheduler and switch to the highest priority thread
  190. *
  191. * @details This function:
  192. * - Gets the highest priority ready thread using _scheduler_get_highest_priority_thread()
  193. * - Sets it as the current thread for the CPU
  194. * - Removes the thread from ready queue and sets its status to RUNNING
  195. * - Performs a context switch to the selected thread using rt_hw_context_switch_to()
  196. *
  197. * @note This function does not return as it switches to the first thread to run.
  198. * Must be called after rt_system_scheduler_init().
  199. * The selected thread will begin execution immediately
  200. */
  201. void rt_system_scheduler_start(void)
  202. {
  203. struct rt_thread *to_thread;
  204. rt_ubase_t highest_ready_priority;
  205. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  206. rt_cpu_self()->current_thread = to_thread;
  207. rt_sched_remove_thread(to_thread);
  208. RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING;
  209. /* switch to new thread */
  210. rt_hw_context_switch_to((rt_uintptr_t)&to_thread->sp);
  211. /* never come back */
  212. }
  213. /**
  214. * @addtogroup group_Thread
  215. * @cond
  216. */
  217. /**@{*/
  218. /**
  219. * @brief Perform thread scheduling once. Select the highest priority thread and switch to it.
  220. *
  221. * @details This function:
  222. * - Disables interrupts to prevent preemption during scheduling
  223. * - Checks if scheduler is enabled (lock_nest == 0)
  224. * - Gets the highest priority ready thread
  225. * - Determines if current thread should continue running or be preempted
  226. * - Performs context switch if needed:
  227. * * From current thread to new thread (normal case)
  228. * * Handles special cases like interrupt context switches
  229. * - Manages thread states (READY/RUNNING) and priority queues
  230. * - Handles thread yield flags and signal processing
  231. */
  232. void rt_schedule(void)
  233. {
  234. rt_base_t level;
  235. struct rt_thread *to_thread;
  236. struct rt_thread *from_thread;
  237. /* using local variable to avoid unecessary function call */
  238. struct rt_thread *curr_thread = rt_thread_self();
  239. /* disable interrupt */
  240. level = rt_hw_interrupt_disable();
  241. /* check the scheduler is enabled or not */
  242. if (rt_scheduler_lock_nest == 0)
  243. {
  244. rt_ubase_t highest_ready_priority;
  245. if (rt_thread_ready_priority_group != 0)
  246. {
  247. /* need_insert_from_thread: need to insert from_thread to ready queue */
  248. int need_insert_from_thread = 0;
  249. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  250. if ((RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  251. {
  252. if (RT_SCHED_PRIV(curr_thread).current_priority < highest_ready_priority)
  253. {
  254. to_thread = curr_thread;
  255. }
  256. else if (RT_SCHED_PRIV(curr_thread).current_priority == highest_ready_priority
  257. && (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  258. {
  259. to_thread = curr_thread;
  260. }
  261. else
  262. {
  263. need_insert_from_thread = 1;
  264. }
  265. }
  266. if (to_thread != curr_thread)
  267. {
  268. /* if the destination thread is not the same as current thread */
  269. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  270. from_thread = curr_thread;
  271. rt_cpu_self()->current_thread = to_thread;
  272. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  273. if (need_insert_from_thread)
  274. {
  275. rt_sched_insert_thread(from_thread);
  276. }
  277. if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  278. {
  279. RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK;
  280. }
  281. rt_sched_remove_thread(to_thread);
  282. RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK);
  283. /* switch to new thread */
  284. LOG_D("[%d]switch to priority#%d "
  285. "thread:%.*s(sp:0x%08x), "
  286. "from thread:%.*s(sp: 0x%08x)",
  287. rt_interrupt_nest, highest_ready_priority,
  288. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  289. RT_NAME_MAX, from_thread->parent.name, from_thread->sp);
  290. RT_SCHEDULER_STACK_CHECK(to_thread);
  291. if (rt_interrupt_nest == 0)
  292. {
  293. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  294. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
  295. rt_hw_context_switch((rt_uintptr_t)&from_thread->sp,
  296. (rt_uintptr_t)&to_thread->sp);
  297. /* enable interrupt */
  298. rt_hw_interrupt_enable(level);
  299. #ifdef RT_USING_SIGNALS
  300. /* check stat of thread for signal */
  301. level = rt_hw_interrupt_disable();
  302. if (RT_SCHED_CTX(curr_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING)
  303. {
  304. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  305. RT_SCHED_CTX(curr_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  306. rt_hw_interrupt_enable(level);
  307. /* check signal status */
  308. rt_thread_handle_sig(RT_TRUE);
  309. }
  310. else
  311. {
  312. rt_hw_interrupt_enable(level);
  313. }
  314. #endif /* RT_USING_SIGNALS */
  315. goto __exit;
  316. }
  317. else
  318. {
  319. LOG_D("switch in interrupt");
  320. rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp,
  321. (rt_uintptr_t)&to_thread->sp, from_thread, to_thread);
  322. }
  323. }
  324. else
  325. {
  326. rt_sched_remove_thread(curr_thread);
  327. RT_SCHED_CTX(curr_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(curr_thread).stat & ~RT_THREAD_STAT_MASK);
  328. }
  329. }
  330. }
  331. /* enable interrupt */
  332. rt_hw_interrupt_enable(level);
  333. __exit:
  334. return;
  335. }
  336. /**
  337. * @brief Initialize thread scheduling attributes for startup
  338. *
  339. * @param thread The thread to be initialized
  340. *
  341. * @details This function:
  342. * - For systems with >32 priority levels:
  343. * * Sets the thread's priority group number (5 bits)
  344. * * Creates number mask for the priority group
  345. * * Creates high mask for the specific priority (3 bits)
  346. * - For systems with <=32 priority levels:
  347. * * Creates a simple number mask for the priority
  348. * - Sets thread state to SUSPEND to prepare for later activation
  349. *
  350. * @note This function must be called before a thread can be scheduled.
  351. * It prepares the thread's priority-related data structures.
  352. * Normally, there isn't anyone racing with us so this operation is lockless
  353. */
  354. void rt_sched_thread_startup(struct rt_thread *thread)
  355. {
  356. #if RT_THREAD_PRIORITY_MAX > 32
  357. RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */
  358. RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number;
  359. RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */
  360. #else
  361. RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority;
  362. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  363. /* change thread stat, so we can resume it */
  364. RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND;
  365. }
  366. /**
  367. * @brief Initialize thread's scheduling private data
  368. *
  369. * @param thread Pointer to the thread control block
  370. * @param tick Initial time slice value for the thread
  371. * @param priority Initial priority of the thread
  372. *
  373. * @details This function:
  374. * - Initializes the thread's list node
  375. * - Sets initial and current priority (must be < RT_THREAD_PRIORITY_MAX)
  376. * - Initializes priority masks (number_mask, number, high_mask for >32 priorities)
  377. * - Sets initial and remaining time slice ticks
  378. */
  379. void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority)
  380. {
  381. rt_list_init(&RT_THREAD_LIST_NODE(thread));
  382. /* priority init */
  383. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  384. RT_SCHED_PRIV(thread).init_priority = priority;
  385. RT_SCHED_PRIV(thread).current_priority = priority;
  386. /* don't add to scheduler queue as init thread */
  387. RT_SCHED_PRIV(thread).number_mask = 0;
  388. #if RT_THREAD_PRIORITY_MAX > 32
  389. RT_SCHED_PRIV(thread).number = 0;
  390. RT_SCHED_PRIV(thread).high_mask = 0;
  391. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  392. /* tick init */
  393. RT_SCHED_PRIV(thread).init_tick = tick;
  394. RT_SCHED_PRIV(thread).remaining_tick = tick;
  395. }
  396. /**
  397. * @brief This function will insert a thread to the system ready queue. The state of
  398. * thread will be set as READY and the thread will be removed from suspend queue.
  399. *
  400. * @param thread is the thread to be inserted.
  401. *
  402. * @note Please do not invoke this function in user application.
  403. */
  404. void rt_sched_insert_thread(struct rt_thread *thread)
  405. {
  406. rt_base_t level;
  407. RT_ASSERT(thread != RT_NULL);
  408. /* disable interrupt */
  409. level = rt_hw_interrupt_disable();
  410. /* it's current thread, it should be RUNNING thread */
  411. if (thread == rt_current_thread)
  412. {
  413. RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  414. goto __exit;
  415. }
  416. /* READY thread, insert to ready queue */
  417. RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  418. /* there is no time slices left(YIELD), inserting thread before ready list*/
  419. if((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  420. {
  421. rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]),
  422. &RT_THREAD_LIST_NODE(thread));
  423. }
  424. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  425. else
  426. {
  427. rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]),
  428. &RT_THREAD_LIST_NODE(thread));
  429. }
  430. LOG_D("insert thread[%.*s], the priority: %d",
  431. RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority);
  432. /* set priority mask */
  433. #if RT_THREAD_PRIORITY_MAX > 32
  434. rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask;
  435. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  436. rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask;
  437. __exit:
  438. /* enable interrupt */
  439. rt_hw_interrupt_enable(level);
  440. }
  441. /**
  442. * @brief This function will remove a thread from system ready queue.
  443. *
  444. * @param thread is the thread to be removed.
  445. *
  446. * @note Please do not invoke this function in user application.
  447. */
  448. void rt_sched_remove_thread(struct rt_thread *thread)
  449. {
  450. rt_base_t level;
  451. RT_ASSERT(thread != RT_NULL);
  452. /* disable interrupt */
  453. level = rt_hw_interrupt_disable();
  454. LOG_D("remove thread[%.*s], the priority: %d",
  455. RT_NAME_MAX, thread->parent.name,
  456. RT_SCHED_PRIV(rt_current_thread).current_priority);
  457. /* remove thread from ready list */
  458. rt_list_remove(&RT_THREAD_LIST_NODE(thread));
  459. if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority])))
  460. {
  461. #if RT_THREAD_PRIORITY_MAX > 32
  462. rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask;
  463. if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0)
  464. {
  465. rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  466. }
  467. #else
  468. rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  469. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  470. }
  471. /* enable interrupt */
  472. rt_hw_interrupt_enable(level);
  473. }
  474. #ifdef RT_DEBUGING_CRITICAL
  475. static volatile int _critical_error_occurred = 0;
  476. /**
  477. * @brief Safely exit critical section with level checking
  478. *
  479. * @param critical_level The expected critical level to match current lock nest
  480. *
  481. * @details This function:
  482. * - Disables interrupts to prevent preemption during check
  483. * - Verifies the provided critical_level matches current rt_scheduler_lock_nest
  484. * - If mismatch detected (debug mode only):
  485. * * Sets error flag
  486. * * Prints debug information including backtrace
  487. * * Enters infinite loop to halt system
  488. * - Always calls rt_exit_critical() to perform actual exit
  489. *
  490. * @note This is a debug version that adds safety checks for critical section exit.
  491. */
  492. void rt_exit_critical_safe(rt_base_t critical_level)
  493. {
  494. rt_base_t level;
  495. /* disable interrupt */
  496. level = rt_hw_interrupt_disable();
  497. if (!_critical_error_occurred)
  498. {
  499. if (critical_level != rt_scheduler_lock_nest)
  500. {
  501. int dummy = 1;
  502. _critical_error_occurred = 1;
  503. rt_kprintf("%s: un-compatible critical level\n" \
  504. "\tCurrent %d\n\tCaller %d\n",
  505. __func__, rt_scheduler_lock_nest,
  506. critical_level);
  507. rt_backtrace();
  508. while (dummy) ;
  509. }
  510. }
  511. rt_hw_interrupt_enable(level);
  512. rt_exit_critical();
  513. }
  514. #else /* !RT_DEBUGING_CRITICAL */
  515. /**
  516. * @brief Safely exit critical section (non-debug version)
  517. *
  518. * @param critical_level The expected critical level (unused in non-debug build)
  519. *
  520. * @details This is the non-debug version that simply calls rt_exit_critical().
  521. * The critical_level parameter is ignored in this implementation.
  522. */
  523. void rt_exit_critical_safe(rt_base_t critical_level)
  524. {
  525. rt_exit_critical();
  526. }
  527. #endif/* RT_DEBUGING_CRITICAL */
  528. RTM_EXPORT(rt_exit_critical_safe);
  529. /**
  530. * @brief Enter critical section and lock the scheduler
  531. *
  532. * @return rt_base_t The current critical level (nesting count)
  533. *
  534. * @details This function:
  535. * - Disables interrupts to prevent preemption
  536. * - Increments the scheduler lock nesting count
  537. * - Returns the new nesting count as critical level
  538. * - Re-enables interrupts while maintaining the lock
  539. *
  540. * @note The nesting count can go up to RT_UINT16_MAX.
  541. * Must be paired with rt_exit_critical().
  542. * Interrupts are only disabled during the lock operation.
  543. */
  544. rt_base_t rt_enter_critical(void)
  545. {
  546. rt_base_t level;
  547. rt_base_t critical_level;
  548. /* disable interrupt */
  549. level = rt_hw_interrupt_disable();
  550. /*
  551. * the maximal number of nest is RT_UINT16_MAX, which is big
  552. * enough and does not check here
  553. */
  554. rt_scheduler_lock_nest ++;
  555. critical_level = rt_scheduler_lock_nest;
  556. /* enable interrupt */
  557. rt_hw_interrupt_enable(level);
  558. return critical_level;
  559. }
  560. RTM_EXPORT(rt_enter_critical);
  561. /**
  562. * @brief Exit critical section and unlock scheduler
  563. *
  564. * @details This function:
  565. * - Decrements the scheduler lock nesting count
  566. * - If nesting count reaches zero:
  567. * * Resets the nesting count
  568. * * Re-enables interrupts
  569. * * Triggers a scheduler run if current thread exists
  570. * - If nesting count still positive:
  571. * * Just re-enables interrupts while maintaining lock
  572. *
  573. * @note Must be paired with rt_enter_critical().
  574. * Interrupts are only disabled during the lock operation.
  575. * Scheduling only occurs when fully unlocked (nest=0)
  576. */
  577. void rt_exit_critical(void)
  578. {
  579. rt_base_t level;
  580. /* disable interrupt */
  581. level = rt_hw_interrupt_disable();
  582. rt_scheduler_lock_nest --;
  583. if (rt_scheduler_lock_nest <= 0)
  584. {
  585. rt_scheduler_lock_nest = 0;
  586. /* enable interrupt */
  587. rt_hw_interrupt_enable(level);
  588. if (rt_current_thread)
  589. {
  590. /* if scheduler is started, do a schedule */
  591. rt_schedule();
  592. }
  593. }
  594. else
  595. {
  596. /* enable interrupt */
  597. rt_hw_interrupt_enable(level);
  598. }
  599. }
  600. RTM_EXPORT(rt_exit_critical);
  601. /**
  602. * @brief Get the scheduler lock level.
  603. *
  604. * @return the level of the scheduler lock. 0 means unlocked.
  605. */
  606. rt_uint16_t rt_critical_level(void)
  607. {
  608. return rt_scheduler_lock_nest;
  609. }
  610. RTM_EXPORT(rt_critical_level);
  611. rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu)
  612. {
  613. return -RT_EINVAL;
  614. }
  615. /**@}*/
  616. /**@endcond*/