# RT-Thread IPC Scheduler lock ```c rt_enter_critical() /* * critical region */ rt_exit_critical() ``` - 使用此方式可確保當前 thread 不會被 scheduler 踢出,但還是有可能會被中斷影響。 --- ## 進入 scheuler 鎖 :::success **File:** scheduler.c ::: ### `rt_enter_critical` | 功能 | 回傳值 | | --- | ------ | | 進入 scheuler 鎖 | void | ```c=360 /** * This function will lock the thread scheduler. */ void rt_enter_critical(void) { register rt_base_t level; /* disable interrupt */ level = rt_hw_interrupt_disable(); /* * the maximal number of nest is RT_UINT16_MAX, which is big * enough and does not check here */ rt_scheduler_lock_nest ++; /* enable interrupt */ rt_hw_interrupt_enable(level); } RTM_EXPORT(rt_enter_critical); ``` - 即,將 `rt_scheduler_lock_nest` 加一 --- ## 離開 scheduler 鎖 ### `rt_exit_critical` | 功能 | 回傳值 | | --- | ------ | | 離開 scheuler 鎖 | void | ```c=381 /** * This function will unlock the thread scheduler. */ void rt_exit_critical(void) { register rt_base_t level; /* disable interrupt */ level = rt_hw_interrupt_disable(); rt_scheduler_lock_nest --; ``` - 即,將 `rt_scheduler_lock_nest` 減一 ```c=+ if (rt_scheduler_lock_nest <= 0) { rt_scheduler_lock_nest = 0; /* enable interrupt */ rt_hw_interrupt_enable(level); rt_schedule(); } else { /* enable interrupt */ rt_hw_interrupt_enable(level); } } ``` - 如果 `rt_scheduler_lock_nest` 被減至 0 或以下,進行一次調度 --- ### `rt_critical_level` | 功能 | 回傳值 | | --- | ------ | | 回傳 scheuler 鎖的值 | scheuler 鎖的值 | ```c=409 /** * Get the scheduler lock level * * @return the level of the scheduler lock. 0 means unlocked. */ rt_uint16_t rt_critical_level(void) { return rt_scheduler_lock_nest; } RTM_EXPORT(rt_critical_level); ``` - 即,回傳 `rt_scheduler_lock_nest` 值 ###### tags: `RT-Thread` `kernel` `ipc`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up