fs/drivers: Avoid causing a busy loop in the program due to context switching induced by sem_post.

examples:
There are two threads involved: thread A with a priority of 100 and
thread B with a priority of 101. Here's how they interact:

When thread A releases a semaphore, thread B is scheduled to execute
some code and may reacquire the semaphore. If no other tasks are ready,
thread A will be scheduled to run.
This continuous process can lead to a busy loop.

Thread A:                                                   Thread B:

while (nxsem_get_value(&priv->wait, &semcount) >= 0 &&  <---
       semcount <= 0)                                      |  2)context switch
  {                                  1)contex switch       |
    nxsem_post(&priv->wait);         ------------->     run some code and nxsem_wait again
  }

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1
2025-02-17 22:04:26 +08:00
committed by Alin Jerpelea
parent 7a96bee46f
commit cfd359141f
8 changed files with 40 additions and 17 deletions
+5 -3
View File
@@ -561,10 +561,12 @@ static void bt_slip_unack_handle(FAR struct sliphci_s *priv)
{
int semcount;
while (nxsem_get_value(&priv->sem, &semcount) >= 0 &&
semcount <= 0)
if (nxsem_get_value(&priv->sem, &semcount) >= 0)
{
nxsem_post(&priv->sem);
while (semcount++ <= 0)
{
nxsem_post(&priv->sem);
}
}
}